4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
9 $ perf stat ~/hackbench 10
12 Performance counter stats for '/home/mingo/hackbench':
14 1255.538611 task clock ticks # 10.143 CPU utilization factor
15 54011 context switches # 0.043 M/sec
16 385 CPU migrations # 0.000 M/sec
17 17755 pagefaults # 0.014 M/sec
18 3808323185 CPU cycles # 3033.219 M/sec
19 1575111190 instructions # 1254.530 M/sec
20 17367895 cache references # 13.833 M/sec
21 7674421 cache misses # 6.112 M/sec
23 Wall-clock time elapsed: 123.786620 msecs
26 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
28 * Improvements and fixes by:
30 * Arjan van de Ven <arjan@linux.intel.com>
31 * Yanmin Zhang <yanmin.zhang@intel.com>
32 * Wu Fengguang <fengguang.wu@intel.com>
33 * Mike Galbraith <efault@gmx.de>
34 * Paul Mackerras <paulus@samba.org>
35 * Jaswinder Singh Rajput <jaswinder@kernel.org>
37 * Released under the GPL v2. (and only v2, not any later version)
42 #include "util/util.h"
43 #include "util/parse-options.h"
44 #include "util/parse-events.h"
45 #include "util/event.h"
46 #include "util/evlist.h"
47 #include "util/evsel.h"
48 #include "util/debug.h"
49 #include "util/color.h"
50 #include "util/header.h"
51 #include "util/cpumap.h"
52 #include "util/thread.h"
53 #include "util/thread_map.h"
55 #include <sys/prctl.h>
59 #define DEFAULT_SEPARATOR " "
61 static struct perf_event_attr default_attrs
[] = {
63 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_TASK_CLOCK
},
64 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CONTEXT_SWITCHES
},
65 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CPU_MIGRATIONS
},
66 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_PAGE_FAULTS
},
68 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_CPU_CYCLES
},
69 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
},
70 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_BACKEND
},
71 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_INSTRUCTIONS
},
72 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
},
73 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_MISSES
},
80 static struct perf_event_attr detailed_attrs
[] = {
82 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_TASK_CLOCK
},
83 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CONTEXT_SWITCHES
},
84 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_CPU_MIGRATIONS
},
85 { .type
= PERF_TYPE_SOFTWARE
, .config
= PERF_COUNT_SW_PAGE_FAULTS
},
87 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_CPU_CYCLES
},
88 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
},
89 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_STALLED_CYCLES_BACKEND
},
90 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_INSTRUCTIONS
},
91 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_INSTRUCTIONS
},
92 { .type
= PERF_TYPE_HARDWARE
, .config
= PERF_COUNT_HW_BRANCH_MISSES
},
94 { .type
= PERF_TYPE_HW_CACHE
,
96 PERF_COUNT_HW_CACHE_L1D
<< 0 |
97 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
98 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
100 { .type
= PERF_TYPE_HW_CACHE
,
102 PERF_COUNT_HW_CACHE_L1D
<< 0 |
103 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
104 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
106 { .type
= PERF_TYPE_HW_CACHE
,
108 PERF_COUNT_HW_CACHE_LL
<< 0 |
109 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
110 (PERF_COUNT_HW_CACHE_RESULT_ACCESS
<< 16) },
112 { .type
= PERF_TYPE_HW_CACHE
,
114 PERF_COUNT_HW_CACHE_LL
<< 0 |
115 (PERF_COUNT_HW_CACHE_OP_READ
<< 8) |
116 (PERF_COUNT_HW_CACHE_RESULT_MISS
<< 16) },
119 struct perf_evlist
*evsel_list
;
121 static bool system_wide
= false;
122 static int run_idx
= 0;
124 static int run_count
= 1;
125 static bool no_inherit
= false;
126 static bool scale
= true;
127 static bool no_aggr
= false;
128 static pid_t target_pid
= -1;
129 static pid_t target_tid
= -1;
130 static pid_t child_pid
= -1;
131 static bool null_run
= false;
132 static bool detailed_run
= false;
133 static bool sync_run
= false;
134 static bool big_num
= true;
135 static int big_num_opt
= -1;
136 static const char *cpu_list
;
137 static const char *csv_sep
= NULL
;
138 static bool csv_output
= false;
140 static volatile int done
= 0;
148 struct stats res_stats
[3];
151 static int perf_evsel__alloc_stat_priv(struct perf_evsel
*evsel
)
153 evsel
->priv
= zalloc(sizeof(struct perf_stat
));
154 return evsel
->priv
== NULL
? -ENOMEM
: 0;
157 static void perf_evsel__free_stat_priv(struct perf_evsel
*evsel
)
163 static void update_stats(struct stats
*stats
, u64 val
)
168 delta
= val
- stats
->mean
;
169 stats
->mean
+= delta
/ stats
->n
;
170 stats
->M2
+= delta
*(val
- stats
->mean
);
173 static double avg_stats(struct stats
*stats
)
179 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
181 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
182 * s^2 = -------------------------------
185 * http://en.wikipedia.org/wiki/Stddev
187 * The std dev of the mean is related to the std dev by:
194 static double stddev_stats(struct stats
*stats
)
196 double variance
= stats
->M2
/ (stats
->n
- 1);
197 double variance_mean
= variance
/ stats
->n
;
199 return sqrt(variance_mean
);
202 struct stats runtime_nsecs_stats
[MAX_NR_CPUS
];
203 struct stats runtime_cycles_stats
[MAX_NR_CPUS
];
204 struct stats runtime_stalled_cycles_stats
[MAX_NR_CPUS
];
205 struct stats runtime_branches_stats
[MAX_NR_CPUS
];
206 struct stats runtime_cacherefs_stats
[MAX_NR_CPUS
];
207 struct stats runtime_l1_dcache_stats
[MAX_NR_CPUS
];
208 struct stats walltime_nsecs_stats
;
210 static int create_perf_stat_counter(struct perf_evsel
*evsel
)
212 struct perf_event_attr
*attr
= &evsel
->attr
;
215 attr
->read_format
= PERF_FORMAT_TOTAL_TIME_ENABLED
|
216 PERF_FORMAT_TOTAL_TIME_RUNNING
;
218 attr
->inherit
= !no_inherit
;
221 return perf_evsel__open_per_cpu(evsel
, evsel_list
->cpus
, false);
223 if (target_pid
== -1 && target_tid
== -1) {
225 attr
->enable_on_exec
= 1;
228 return perf_evsel__open_per_thread(evsel
, evsel_list
->threads
, false);
232 * Does the counter have nsecs as a unit?
234 static inline int nsec_counter(struct perf_evsel
*evsel
)
236 if (perf_evsel__match(evsel
, SOFTWARE
, SW_CPU_CLOCK
) ||
237 perf_evsel__match(evsel
, SOFTWARE
, SW_TASK_CLOCK
))
244 * Update various tracking values we maintain to print
245 * more semantic information such as miss/hit ratios,
246 * instruction rates, etc:
248 static void update_shadow_stats(struct perf_evsel
*counter
, u64
*count
)
250 if (perf_evsel__match(counter
, SOFTWARE
, SW_TASK_CLOCK
))
251 update_stats(&runtime_nsecs_stats
[0], count
[0]);
252 else if (perf_evsel__match(counter
, HARDWARE
, HW_CPU_CYCLES
))
253 update_stats(&runtime_cycles_stats
[0], count
[0]);
254 else if (perf_evsel__match(counter
, HARDWARE
, HW_STALLED_CYCLES_BACKEND
))
255 update_stats(&runtime_stalled_cycles_stats
[0], count
[0]);
256 else if (perf_evsel__match(counter
, HARDWARE
, HW_BRANCH_INSTRUCTIONS
))
257 update_stats(&runtime_branches_stats
[0], count
[0]);
258 else if (perf_evsel__match(counter
, HARDWARE
, HW_CACHE_REFERENCES
))
259 update_stats(&runtime_cacherefs_stats
[0], count
[0]);
260 else if (perf_evsel__match(counter
, HW_CACHE
, HW_CACHE_L1D
))
261 update_stats(&runtime_l1_dcache_stats
[0], count
[0]);
265 * Read out the results of a single counter:
266 * aggregate counts across CPUs in system-wide mode
268 static int read_counter_aggr(struct perf_evsel
*counter
)
270 struct perf_stat
*ps
= counter
->priv
;
271 u64
*count
= counter
->counts
->aggr
.values
;
274 if (__perf_evsel__read(counter
, evsel_list
->cpus
->nr
,
275 evsel_list
->threads
->nr
, scale
) < 0)
278 for (i
= 0; i
< 3; i
++)
279 update_stats(&ps
->res_stats
[i
], count
[i
]);
282 fprintf(stderr
, "%s: %" PRIu64
" %" PRIu64
" %" PRIu64
"\n",
283 event_name(counter
), count
[0], count
[1], count
[2]);
287 * Save the full runtime - to allow normalization during printout:
289 update_shadow_stats(counter
, count
);
295 * Read out the results of a single counter:
296 * do not aggregate counts across CPUs in system-wide mode
298 static int read_counter(struct perf_evsel
*counter
)
303 for (cpu
= 0; cpu
< evsel_list
->cpus
->nr
; cpu
++) {
304 if (__perf_evsel__read_on_cpu(counter
, cpu
, 0, scale
) < 0)
307 count
= counter
->counts
->cpu
[cpu
].values
;
309 update_shadow_stats(counter
, count
);
315 static int run_perf_stat(int argc __used
, const char **argv
)
317 unsigned long long t0
, t1
;
318 struct perf_evsel
*counter
;
320 int child_ready_pipe
[2], go_pipe
[2];
321 const bool forks
= (argc
> 0);
324 if (forks
&& (pipe(child_ready_pipe
) < 0 || pipe(go_pipe
) < 0)) {
325 perror("failed to create pipes");
330 if ((child_pid
= fork()) < 0)
331 perror("failed to fork");
334 close(child_ready_pipe
[0]);
336 fcntl(go_pipe
[0], F_SETFD
, FD_CLOEXEC
);
339 * Do a dummy execvp to get the PLT entry resolved,
340 * so we avoid the resolver overhead on the real
343 execvp("", (char **)argv
);
346 * Tell the parent we're ready to go
348 close(child_ready_pipe
[1]);
351 * Wait until the parent tells us to go.
353 if (read(go_pipe
[0], &buf
, 1) == -1)
354 perror("unable to read pipe");
356 execvp(argv
[0], (char **)argv
);
362 if (target_tid
== -1 && target_pid
== -1 && !system_wide
)
363 evsel_list
->threads
->map
[0] = child_pid
;
366 * Wait for the child to be ready to exec.
368 close(child_ready_pipe
[1]);
370 if (read(child_ready_pipe
[0], &buf
, 1) == -1)
371 perror("unable to read pipe");
372 close(child_ready_pipe
[0]);
375 list_for_each_entry(counter
, &evsel_list
->entries
, node
) {
376 if (create_perf_stat_counter(counter
) < 0) {
377 if (errno
== EINVAL
|| errno
== ENOSYS
)
380 if (errno
== EPERM
|| errno
== EACCES
) {
381 error("You may not have permission to collect %sstats.\n"
382 "\t Consider tweaking"
383 " /proc/sys/kernel/perf_event_paranoid or running as root.",
384 system_wide
? "system-wide " : "");
385 } else if (errno
== ENOENT
) {
386 error("%s event is not supported. ", event_name(counter
));
388 error("open_counter returned with %d (%s). "
389 "/bin/dmesg may provide additional information.\n",
390 errno
, strerror(errno
));
393 kill(child_pid
, SIGTERM
);
394 die("Not all events could be opened.\n");
399 if (perf_evlist__set_filters(evsel_list
)) {
400 error("failed to set filter with %d (%s)\n", errno
,
406 * Enable counters and exec the command:
414 while(!done
) sleep(1);
419 update_stats(&walltime_nsecs_stats
, t1
- t0
);
422 list_for_each_entry(counter
, &evsel_list
->entries
, node
) {
423 read_counter(counter
);
424 perf_evsel__close_fd(counter
, evsel_list
->cpus
->nr
, 1);
427 list_for_each_entry(counter
, &evsel_list
->entries
, node
) {
428 read_counter_aggr(counter
);
429 perf_evsel__close_fd(counter
, evsel_list
->cpus
->nr
,
430 evsel_list
->threads
->nr
);
434 return WEXITSTATUS(status
);
437 static void print_noise_pct(double total
, double avg
)
442 pct
= 100.0*total
/avg
;
444 fprintf(stderr
, " ( +-%6.2f%% )", pct
);
447 static void print_noise(struct perf_evsel
*evsel
, double avg
)
449 struct perf_stat
*ps
;
455 print_noise_pct(stddev_stats(&ps
->res_stats
[0]), avg
);
458 static void nsec_printout(int cpu
, struct perf_evsel
*evsel
, double avg
)
460 double msecs
= avg
/ 1e6
;
461 char cpustr
[16] = { '\0', };
462 const char *fmt
= csv_output
? "%s%.6f%s%s" : "%s%18.6f%s%-24s";
465 sprintf(cpustr
, "CPU%*d%s",
467 evsel_list
->cpus
->map
[cpu
], csv_sep
);
469 fprintf(stderr
, fmt
, cpustr
, msecs
, csv_sep
, event_name(evsel
));
472 fprintf(stderr
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
477 if (perf_evsel__match(evsel
, SOFTWARE
, SW_TASK_CLOCK
))
478 fprintf(stderr
, " # %8.3f CPUs utilized ", avg
/ avg_stats(&walltime_nsecs_stats
));
481 static void print_stalled_cycles(int cpu
, struct perf_evsel
*evsel __used
, double avg
)
483 double total
, ratio
= 0.0;
486 total
= avg_stats(&runtime_cycles_stats
[cpu
]);
489 ratio
= avg
/ total
* 100.0;
491 color
= PERF_COLOR_NORMAL
;
493 color
= PERF_COLOR_RED
;
494 else if (ratio
> 50.0)
495 color
= PERF_COLOR_MAGENTA
;
496 else if (ratio
> 25.0)
497 color
= PERF_COLOR_YELLOW
;
499 fprintf(stderr
, " # ");
500 color_fprintf(stderr
, color
, "%5.2f%%", ratio
);
501 fprintf(stderr
, " of all cycles are idle ");
504 static void print_branch_misses(int cpu
, struct perf_evsel
*evsel __used
, double avg
)
506 double total
, ratio
= 0.0;
509 total
= avg_stats(&runtime_branches_stats
[cpu
]);
512 ratio
= avg
/ total
* 100.0;
514 color
= PERF_COLOR_NORMAL
;
516 color
= PERF_COLOR_RED
;
517 else if (ratio
> 10.0)
518 color
= PERF_COLOR_MAGENTA
;
519 else if (ratio
> 5.0)
520 color
= PERF_COLOR_YELLOW
;
522 fprintf(stderr
, " # ");
523 color_fprintf(stderr
, color
, "%5.2f%%", ratio
);
524 fprintf(stderr
, " of all branches ");
527 static void print_l1_dcache_misses(int cpu
, struct perf_evsel
*evsel __used
, double avg
)
529 double total
, ratio
= 0.0;
532 total
= avg_stats(&runtime_l1_dcache_stats
[cpu
]);
535 ratio
= avg
/ total
* 100.0;
537 color
= PERF_COLOR_NORMAL
;
539 color
= PERF_COLOR_RED
;
540 else if (ratio
> 10.0)
541 color
= PERF_COLOR_MAGENTA
;
542 else if (ratio
> 5.0)
543 color
= PERF_COLOR_YELLOW
;
545 fprintf(stderr
, " # ");
546 color_fprintf(stderr
, color
, "%5.2f%%", ratio
);
547 fprintf(stderr
, " of all L1-dcache hits ");
550 static void abs_printout(int cpu
, struct perf_evsel
*evsel
, double avg
)
552 double total
, ratio
= 0.0;
553 char cpustr
[16] = { '\0', };
559 fmt
= "%s%'18.0f%s%-24s";
561 fmt
= "%s%18.0f%s%-24s";
564 sprintf(cpustr
, "CPU%*d%s",
566 evsel_list
->cpus
->map
[cpu
], csv_sep
);
570 fprintf(stderr
, fmt
, cpustr
, avg
, csv_sep
, event_name(evsel
));
573 fprintf(stderr
, "%s%s", csv_sep
, evsel
->cgrp
->name
);
578 if (perf_evsel__match(evsel
, HARDWARE
, HW_INSTRUCTIONS
)) {
579 total
= avg_stats(&runtime_cycles_stats
[cpu
]);
584 fprintf(stderr
, " # %4.2f insns per cycle ", ratio
);
586 total
= avg_stats(&runtime_stalled_cycles_stats
[cpu
]);
590 fprintf(stderr
, "\n # %4.2f stalled cycles per insn", ratio
);
593 } else if (perf_evsel__match(evsel
, HARDWARE
, HW_BRANCH_MISSES
) &&
594 runtime_branches_stats
[cpu
].n
!= 0) {
595 print_branch_misses(cpu
, evsel
, avg
);
597 evsel
->attr
.type
== PERF_TYPE_HW_CACHE
&&
598 evsel
->attr
.config
== ( PERF_COUNT_HW_CACHE_L1D
|
599 ((PERF_COUNT_HW_CACHE_OP_READ
) << 8) |
600 ((PERF_COUNT_HW_CACHE_RESULT_MISS
) << 16)) &&
601 runtime_l1_dcache_stats
[cpu
].n
!= 0) {
602 print_l1_dcache_misses(cpu
, evsel
, avg
);
603 } else if (perf_evsel__match(evsel
, HARDWARE
, HW_CACHE_MISSES
) &&
604 runtime_cacherefs_stats
[cpu
].n
!= 0) {
605 total
= avg_stats(&runtime_cacherefs_stats
[cpu
]);
608 ratio
= avg
* 100 / total
;
610 fprintf(stderr
, " # %8.3f %% of all cache refs ", ratio
);
612 } else if (perf_evsel__match(evsel
, HARDWARE
, HW_STALLED_CYCLES_BACKEND
)) {
613 print_stalled_cycles(cpu
, evsel
, avg
);
614 } else if (perf_evsel__match(evsel
, HARDWARE
, HW_CPU_CYCLES
)) {
615 total
= avg_stats(&runtime_nsecs_stats
[cpu
]);
618 ratio
= 1.0 * avg
/ total
;
620 fprintf(stderr
, " # %8.3f GHz ", ratio
);
621 } else if (runtime_nsecs_stats
[cpu
].n
!= 0) {
622 total
= avg_stats(&runtime_nsecs_stats
[cpu
]);
625 ratio
= 1000.0 * avg
/ total
;
627 fprintf(stderr
, " # %8.3f M/sec ", ratio
);
629 fprintf(stderr
, " ");
634 * Print out the results of a single counter:
635 * aggregated counts in system-wide mode
637 static void print_counter_aggr(struct perf_evsel
*counter
)
639 struct perf_stat
*ps
= counter
->priv
;
640 double avg
= avg_stats(&ps
->res_stats
[0]);
641 int scaled
= counter
->counts
->scaled
;
644 fprintf(stderr
, "%*s%s%*s",
648 csv_output
? 0 : -24,
649 event_name(counter
));
652 fprintf(stderr
, "%s%s", csv_sep
, counter
->cgrp
->name
);
658 if (nsec_counter(counter
))
659 nsec_printout(-1, counter
, avg
);
661 abs_printout(-1, counter
, avg
);
668 print_noise(counter
, avg
);
671 double avg_enabled
, avg_running
;
673 avg_enabled
= avg_stats(&ps
->res_stats
[1]);
674 avg_running
= avg_stats(&ps
->res_stats
[2]);
676 fprintf(stderr
, " (%.2f%%)", 100 * avg_running
/ avg_enabled
);
678 fprintf(stderr
, "\n");
682 * Print out the results of a single counter:
683 * does not use aggregated count in system-wide
685 static void print_counter(struct perf_evsel
*counter
)
690 for (cpu
= 0; cpu
< evsel_list
->cpus
->nr
; cpu
++) {
691 val
= counter
->counts
->cpu
[cpu
].val
;
692 ena
= counter
->counts
->cpu
[cpu
].ena
;
693 run
= counter
->counts
->cpu
[cpu
].run
;
694 if (run
== 0 || ena
== 0) {
695 fprintf(stderr
, "CPU%*d%s%*s%s%*s",
697 evsel_list
->cpus
->map
[cpu
], csv_sep
,
699 "<not counted>", csv_sep
,
700 csv_output
? 0 : -24,
701 event_name(counter
));
704 fprintf(stderr
, "%s%s", csv_sep
, counter
->cgrp
->name
);
710 if (nsec_counter(counter
))
711 nsec_printout(cpu
, counter
, val
);
713 abs_printout(cpu
, counter
, val
);
716 print_noise(counter
, 1.0);
719 fprintf(stderr
, " (%.2f%%)", 100.0 * run
/ ena
);
725 static void print_stat(int argc
, const char **argv
)
727 struct perf_evsel
*counter
;
733 fprintf(stderr
, "\n");
734 fprintf(stderr
, " Performance counter stats for ");
735 if(target_pid
== -1 && target_tid
== -1) {
736 fprintf(stderr
, "\'%s", argv
[0]);
737 for (i
= 1; i
< argc
; i
++)
738 fprintf(stderr
, " %s", argv
[i
]);
739 } else if (target_pid
!= -1)
740 fprintf(stderr
, "process id \'%d", target_pid
);
742 fprintf(stderr
, "thread id \'%d", target_tid
);
744 fprintf(stderr
, "\'");
746 fprintf(stderr
, " (%d runs)", run_count
);
747 fprintf(stderr
, ":\n\n");
751 list_for_each_entry(counter
, &evsel_list
->entries
, node
)
752 print_counter(counter
);
754 list_for_each_entry(counter
, &evsel_list
->entries
, node
)
755 print_counter_aggr(counter
);
759 fprintf(stderr
, "\n");
760 fprintf(stderr
, " %18.9f seconds time elapsed",
761 avg_stats(&walltime_nsecs_stats
)/1e9
);
763 print_noise_pct(stddev_stats(&walltime_nsecs_stats
),
764 avg_stats(&walltime_nsecs_stats
));
766 fprintf(stderr
, "\n\n");
770 static volatile int signr
= -1;
772 static void skip_signal(int signo
)
780 static void sig_atexit(void)
783 kill(child_pid
, SIGTERM
);
788 signal(signr
, SIG_DFL
);
789 kill(getpid(), signr
);
792 static const char * const stat_usage
[] = {
793 "perf stat [<options>] [<command>]",
797 static int stat__set_big_num(const struct option
*opt __used
,
798 const char *s __used
, int unset
)
800 big_num_opt
= unset
? 0 : 1;
804 static const struct option options
[] = {
805 OPT_CALLBACK('e', "event", &evsel_list
, "event",
806 "event selector. use 'perf list' to list available events",
808 OPT_CALLBACK(0, "filter", &evsel_list
, "filter",
809 "event filter", parse_filter
),
810 OPT_BOOLEAN('i', "no-inherit", &no_inherit
,
811 "child tasks do not inherit counters"),
812 OPT_INTEGER('p', "pid", &target_pid
,
813 "stat events on existing process id"),
814 OPT_INTEGER('t', "tid", &target_tid
,
815 "stat events on existing thread id"),
816 OPT_BOOLEAN('a', "all-cpus", &system_wide
,
817 "system-wide collection from all CPUs"),
818 OPT_BOOLEAN('c', "scale", &scale
,
819 "scale/normalize counters"),
820 OPT_INCR('v', "verbose", &verbose
,
821 "be more verbose (show counter open errors, etc)"),
822 OPT_INTEGER('r', "repeat", &run_count
,
823 "repeat command and print average + stddev (max: 100)"),
824 OPT_BOOLEAN('n', "null", &null_run
,
825 "null run - dont start any counters"),
826 OPT_BOOLEAN('d', "detailed", &detailed_run
,
827 "detailed run - start a lot of events"),
828 OPT_BOOLEAN('S', "sync", &sync_run
,
829 "call sync() before starting a run"),
830 OPT_CALLBACK_NOOPT('B', "big-num", NULL
, NULL
,
831 "print large numbers with thousands\' separators",
833 OPT_STRING('C', "cpu", &cpu_list
, "cpu",
834 "list of cpus to monitor in system-wide"),
835 OPT_BOOLEAN('A', "no-aggr", &no_aggr
,
836 "disable CPU count aggregation"),
837 OPT_STRING('x', "field-separator", &csv_sep
, "separator",
838 "print counts with custom separator"),
839 OPT_CALLBACK('G', "cgroup", &evsel_list
, "name",
840 "monitor event in cgroup name only",
845 int cmd_stat(int argc
, const char **argv
, const char *prefix __used
)
847 struct perf_evsel
*pos
;
848 int status
= -ENOMEM
;
850 setlocale(LC_ALL
, "");
852 evsel_list
= perf_evlist__new(NULL
, NULL
);
853 if (evsel_list
== NULL
)
856 argc
= parse_options(argc
, argv
, options
, stat_usage
,
857 PARSE_OPT_STOP_AT_NON_OPTION
);
862 csv_sep
= DEFAULT_SEPARATOR
;
865 * let the spreadsheet do the pretty-printing
868 /* User explicitely passed -B? */
869 if (big_num_opt
== 1) {
870 fprintf(stderr
, "-B option not supported with -x\n");
871 usage_with_options(stat_usage
, options
);
872 } else /* Nope, so disable big number formatting */
874 } else if (big_num_opt
== 0) /* User passed --no-big-num */
877 if (!argc
&& target_pid
== -1 && target_tid
== -1)
878 usage_with_options(stat_usage
, options
);
880 usage_with_options(stat_usage
, options
);
882 /* no_aggr, cgroup are for system-wide only */
883 if ((no_aggr
|| nr_cgroups
) && !system_wide
) {
884 fprintf(stderr
, "both cgroup and no-aggregation "
885 "modes only available in system-wide mode\n");
887 usage_with_options(stat_usage
, options
);
890 /* Set attrs and nr_counters if no event is selected and !null_run */
894 for (c
= 0; c
< ARRAY_SIZE(detailed_attrs
); ++c
) {
895 pos
= perf_evsel__new(&detailed_attrs
[c
], c
);
898 perf_evlist__add(evsel_list
, pos
);
901 /* Set attrs and nr_counters if no event is selected and !null_run */
902 if (!detailed_run
&& !null_run
&& !evsel_list
->nr_entries
) {
905 for (c
= 0; c
< ARRAY_SIZE(default_attrs
); ++c
) {
906 pos
= perf_evsel__new(&default_attrs
[c
], c
);
909 perf_evlist__add(evsel_list
, pos
);
913 if (target_pid
!= -1)
914 target_tid
= target_pid
;
916 evsel_list
->threads
= thread_map__new(target_pid
, target_tid
);
917 if (evsel_list
->threads
== NULL
) {
918 pr_err("Problems finding threads of monitor\n");
919 usage_with_options(stat_usage
, options
);
923 evsel_list
->cpus
= cpu_map__new(cpu_list
);
925 evsel_list
->cpus
= cpu_map__dummy_new();
927 if (evsel_list
->cpus
== NULL
) {
928 perror("failed to parse CPUs map");
929 usage_with_options(stat_usage
, options
);
933 list_for_each_entry(pos
, &evsel_list
->entries
, node
) {
934 if (perf_evsel__alloc_stat_priv(pos
) < 0 ||
935 perf_evsel__alloc_counts(pos
, evsel_list
->cpus
->nr
) < 0 ||
936 perf_evsel__alloc_fd(pos
, evsel_list
->cpus
->nr
, evsel_list
->threads
->nr
) < 0)
941 * We dont want to block the signals - that would cause
942 * child tasks to inherit that and Ctrl-C would not work.
943 * What we want is for Ctrl-C to work in the exec()-ed
944 * task, but being ignored by perf stat itself:
947 signal(SIGINT
, skip_signal
);
948 signal(SIGALRM
, skip_signal
);
949 signal(SIGABRT
, skip_signal
);
952 for (run_idx
= 0; run_idx
< run_count
; run_idx
++) {
953 if (run_count
!= 1 && verbose
)
954 fprintf(stderr
, "[ perf stat: executing run #%d ... ]\n", run_idx
+ 1);
959 status
= run_perf_stat(argc
, argv
);
963 print_stat(argc
, argv
);
965 list_for_each_entry(pos
, &evsel_list
->entries
, node
)
966 perf_evsel__free_stat_priv(pos
);
967 perf_evlist__delete_maps(evsel_list
);
969 perf_evlist__delete(evsel_list
);