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 },
70 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
71 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
72 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
76 struct perf_evlist *evsel_list;
78 static bool system_wide = false;
79 static int run_idx = 0;
81 static int run_count = 1;
82 static bool no_inherit = false;
83 static bool scale = true;
84 static bool no_aggr = false;
85 static pid_t target_pid = -1;
86 static pid_t target_tid = -1;
87 static pid_t child_pid = -1;
88 static bool null_run = false;
89 static bool big_num = true;
90 static int big_num_opt = -1;
91 static const char *cpu_list;
92 static const char *csv_sep = NULL;
93 static bool csv_output = false;
95 static volatile int done = 0;
103 struct stats res_stats[3];
106 static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
108 evsel->priv = zalloc(sizeof(struct perf_stat));
109 return evsel->priv == NULL ? -ENOMEM : 0;
112 static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
118 static void update_stats(struct stats *stats, u64 val)
123 delta = val - stats->mean;
124 stats->mean += delta / stats->n;
125 stats->M2 += delta*(val - stats->mean);
128 static double avg_stats(struct stats *stats)
134 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
136 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
137 * s^2 = -------------------------------
140 * http://en.wikipedia.org/wiki/Stddev
142 * The std dev of the mean is related to the std dev by:
149 static double stddev_stats(struct stats *stats)
151 double variance = stats->M2 / (stats->n - 1);
152 double variance_mean = variance / stats->n;
154 return sqrt(variance_mean);
157 struct stats runtime_nsecs_stats[MAX_NR_CPUS];
158 struct stats runtime_cycles_stats[MAX_NR_CPUS];
159 struct stats runtime_stalled_cycles_stats[MAX_NR_CPUS];
160 struct stats runtime_branches_stats[MAX_NR_CPUS];
161 struct stats runtime_cacherefs_stats[MAX_NR_CPUS];
162 struct stats walltime_nsecs_stats;
164 static int create_perf_stat_counter(struct perf_evsel *evsel)
166 struct perf_event_attr *attr = &evsel->attr;
169 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
170 PERF_FORMAT_TOTAL_TIME_RUNNING;
172 attr->inherit = !no_inherit;
175 return perf_evsel__open_per_cpu(evsel, evsel_list->cpus, false);
177 if (target_pid == -1 && target_tid == -1) {
179 attr->enable_on_exec = 1;
182 return perf_evsel__open_per_thread(evsel, evsel_list->threads, false);
186 * Does the counter have nsecs as a unit?
188 static inline int nsec_counter(struct perf_evsel *evsel)
190 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
191 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
198 * Update various tracking values we maintain to print
199 * more semantic information such as miss/hit ratios,
200 * instruction rates, etc:
202 static void update_shadow_stats(struct perf_evsel *counter, u64 *count)
204 if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK))
205 update_stats(&runtime_nsecs_stats[0], count[0]);
206 else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
207 update_stats(&runtime_cycles_stats[0], count[0]);
208 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES))
209 update_stats(&runtime_stalled_cycles_stats[0], count[0]);
210 else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
211 update_stats(&runtime_branches_stats[0], count[0]);
212 else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
213 update_stats(&runtime_cacherefs_stats[0], count[0]);
217 * Read out the results of a single counter:
218 * aggregate counts across CPUs in system-wide mode
220 static int read_counter_aggr(struct perf_evsel *counter)
222 struct perf_stat *ps = counter->priv;
223 u64 *count = counter->counts->aggr.values;
226 if (__perf_evsel__read(counter, evsel_list->cpus->nr,
227 evsel_list->threads->nr, scale) < 0)
230 for (i = 0; i < 3; i++)
231 update_stats(&ps->res_stats[i], count[i]);
234 fprintf(stderr, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
235 event_name(counter), count[0], count[1], count[2]);
239 * Save the full runtime - to allow normalization during printout:
241 update_shadow_stats(counter, count);
247 * Read out the results of a single counter:
248 * do not aggregate counts across CPUs in system-wide mode
250 static int read_counter(struct perf_evsel *counter)
255 for (cpu = 0; cpu < evsel_list->cpus->nr; cpu++) {
256 if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0)
259 count = counter->counts->cpu[cpu].values;
261 update_shadow_stats(counter, count);
267 static int run_perf_stat(int argc __used, const char **argv)
269 unsigned long long t0, t1;
270 struct perf_evsel *counter;
272 int child_ready_pipe[2], go_pipe[2];
273 const bool forks = (argc > 0);
276 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
277 perror("failed to create pipes");
282 if ((child_pid = fork()) < 0)
283 perror("failed to fork");
286 close(child_ready_pipe[0]);
288 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
291 * Do a dummy execvp to get the PLT entry resolved,
292 * so we avoid the resolver overhead on the real
295 execvp("", (char **)argv);
298 * Tell the parent we're ready to go
300 close(child_ready_pipe[1]);
303 * Wait until the parent tells us to go.
305 if (read(go_pipe[0], &buf, 1) == -1)
306 perror("unable to read pipe");
308 execvp(argv[0], (char **)argv);
314 if (target_tid == -1 && target_pid == -1 && !system_wide)
315 evsel_list->threads->map[0] = child_pid;
318 * Wait for the child to be ready to exec.
320 close(child_ready_pipe[1]);
322 if (read(child_ready_pipe[0], &buf, 1) == -1)
323 perror("unable to read pipe");
324 close(child_ready_pipe[0]);
327 list_for_each_entry(counter, &evsel_list->entries, node) {
328 if (create_perf_stat_counter(counter) < 0) {
329 if (errno == -EPERM || errno == -EACCES) {
330 error("You may not have permission to collect %sstats.\n"
331 "\t Consider tweaking"
332 " /proc/sys/kernel/perf_event_paranoid or running as root.",
333 system_wide ? "system-wide " : "");
334 } else if (errno == ENOENT) {
335 error("%s event is not supported. ", event_name(counter));
337 error("open_counter returned with %d (%s). "
338 "/bin/dmesg may provide additional information.\n",
339 errno, strerror(errno));
342 kill(child_pid, SIGTERM);
343 die("Not all events could be opened.\n");
348 if (perf_evlist__set_filters(evsel_list)) {
349 error("failed to set filter with %d (%s)\n", errno,
355 * Enable counters and exec the command:
363 while(!done) sleep(1);
368 update_stats(&walltime_nsecs_stats, t1 - t0);
371 list_for_each_entry(counter, &evsel_list->entries, node) {
372 read_counter(counter);
373 perf_evsel__close_fd(counter, evsel_list->cpus->nr, 1);
376 list_for_each_entry(counter, &evsel_list->entries, node) {
377 read_counter_aggr(counter);
378 perf_evsel__close_fd(counter, evsel_list->cpus->nr,
379 evsel_list->threads->nr);
383 return WEXITSTATUS(status);
386 static void print_noise_pct(double total, double avg)
391 pct = 100.0*total/avg;
393 fprintf(stderr, " ( +-%6.2f%% )", pct);
396 static void print_noise(struct perf_evsel *evsel, double avg)
398 struct perf_stat *ps;
404 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
407 static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
409 double msecs = avg / 1e6;
410 char cpustr[16] = { '\0', };
411 const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-24s";
414 sprintf(cpustr, "CPU%*d%s",
416 evsel_list->cpus->map[cpu], csv_sep);
418 fprintf(stderr, fmt, cpustr, msecs, csv_sep, event_name(evsel));
421 fprintf(stderr, "%s%s", csv_sep, evsel->cgrp->name);
426 if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
427 fprintf(stderr, " # %8.3f CPUs utilized ", avg / avg_stats(&walltime_nsecs_stats));
430 static void print_stalled_cycles(int cpu, struct perf_evsel *evsel __used, double avg)
432 double total, ratio = 0.0;
435 total = avg_stats(&runtime_cycles_stats[cpu]);
438 ratio = avg / total * 100.0;
440 color = PERF_COLOR_NORMAL;
442 color = PERF_COLOR_RED;
443 else if (ratio > 50.0)
444 color = PERF_COLOR_MAGENTA;
445 else if (ratio > 25.0)
446 color = PERF_COLOR_YELLOW;
448 fprintf(stderr, " # ");
449 color_fprintf(stderr, color, "%5.2f%%", ratio);
450 fprintf(stderr, " of all cycles are idle ");
453 static void print_branch_misses(int cpu, struct perf_evsel *evsel __used, double avg)
455 double total, ratio = 0.0;
458 total = avg_stats(&runtime_branches_stats[cpu]);
461 ratio = avg / total * 100.0;
463 color = PERF_COLOR_NORMAL;
465 color = PERF_COLOR_RED;
466 else if (ratio > 10.0)
467 color = PERF_COLOR_MAGENTA;
468 else if (ratio > 5.0)
469 color = PERF_COLOR_YELLOW;
471 fprintf(stderr, " # ");
472 color_fprintf(stderr, color, "%5.2f%%", ratio);
473 fprintf(stderr, " of all branches ");
476 static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
478 double total, ratio = 0.0;
479 char cpustr[16] = { '\0', };
485 fmt = "%s%'18.0f%s%-24s";
487 fmt = "%s%18.0f%s%-24s";
490 sprintf(cpustr, "CPU%*d%s",
492 evsel_list->cpus->map[cpu], csv_sep);
496 fprintf(stderr, fmt, cpustr, avg, csv_sep, event_name(evsel));
499 fprintf(stderr, "%s%s", csv_sep, evsel->cgrp->name);
504 if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
505 total = avg_stats(&runtime_cycles_stats[cpu]);
510 fprintf(stderr, " # %4.2f insns per cycle", ratio);
512 total = avg_stats(&runtime_stalled_cycles_stats[cpu]);
516 fprintf(stderr, "\n # %4.2f stalled cycles per insn", ratio);
519 } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
520 runtime_branches_stats[cpu].n != 0) {
521 print_branch_misses(cpu, evsel, avg);
522 } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES) &&
523 runtime_cacherefs_stats[cpu].n != 0) {
524 total = avg_stats(&runtime_cacherefs_stats[cpu]);
527 ratio = avg * 100 / total;
529 fprintf(stderr, " # %8.3f %% of all cache refs ", ratio);
531 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES)) {
532 print_stalled_cycles(cpu, evsel, avg);
533 } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
534 total = avg_stats(&runtime_nsecs_stats[cpu]);
537 ratio = 1.0 * avg / total;
539 fprintf(stderr, " # %8.3f GHz ", ratio);
540 } else if (runtime_nsecs_stats[cpu].n != 0) {
541 total = avg_stats(&runtime_nsecs_stats[cpu]);
544 ratio = 1000.0 * avg / total;
546 fprintf(stderr, " # %8.3f M/sec ", ratio);
548 fprintf(stderr, " ");
553 * Print out the results of a single counter:
554 * aggregated counts in system-wide mode
556 static void print_counter_aggr(struct perf_evsel *counter)
558 struct perf_stat *ps = counter->priv;
559 double avg = avg_stats(&ps->res_stats[0]);
560 int scaled = counter->counts->scaled;
563 fprintf(stderr, "%*s%s%*s",
567 csv_output ? 0 : -24,
568 event_name(counter));
571 fprintf(stderr, "%s%s", csv_sep, counter->cgrp->name);
577 if (nsec_counter(counter))
578 nsec_printout(-1, counter, avg);
580 abs_printout(-1, counter, avg);
587 print_noise(counter, avg);
590 double avg_enabled, avg_running;
592 avg_enabled = avg_stats(&ps->res_stats[1]);
593 avg_running = avg_stats(&ps->res_stats[2]);
595 fprintf(stderr, " (scaled from %.2f%%)",
596 100 * avg_running / avg_enabled);
598 fprintf(stderr, "\n");
602 * Print out the results of a single counter:
603 * does not use aggregated count in system-wide
605 static void print_counter(struct perf_evsel *counter)
610 for (cpu = 0; cpu < evsel_list->cpus->nr; cpu++) {
611 val = counter->counts->cpu[cpu].val;
612 ena = counter->counts->cpu[cpu].ena;
613 run = counter->counts->cpu[cpu].run;
614 if (run == 0 || ena == 0) {
615 fprintf(stderr, "CPU%*d%s%*s%s%*s",
617 evsel_list->cpus->map[cpu], csv_sep,
619 "<not counted>", csv_sep,
620 csv_output ? 0 : -24,
621 event_name(counter));
624 fprintf(stderr, "%s%s", csv_sep, counter->cgrp->name);
630 if (nsec_counter(counter))
631 nsec_printout(cpu, counter, val);
633 abs_printout(cpu, counter, val);
636 print_noise(counter, 1.0);
639 fprintf(stderr, " (scaled from %.2f%%)",
647 static void print_stat(int argc, const char **argv)
649 struct perf_evsel *counter;
655 fprintf(stderr, "\n");
656 fprintf(stderr, " Performance counter stats for ");
657 if(target_pid == -1 && target_tid == -1) {
658 fprintf(stderr, "\'%s", argv[0]);
659 for (i = 1; i < argc; i++)
660 fprintf(stderr, " %s", argv[i]);
661 } else if (target_pid != -1)
662 fprintf(stderr, "process id \'%d", target_pid);
664 fprintf(stderr, "thread id \'%d", target_tid);
666 fprintf(stderr, "\'");
668 fprintf(stderr, " (%d runs)", run_count);
669 fprintf(stderr, ":\n\n");
673 list_for_each_entry(counter, &evsel_list->entries, node)
674 print_counter(counter);
676 list_for_each_entry(counter, &evsel_list->entries, node)
677 print_counter_aggr(counter);
681 fprintf(stderr, "\n");
682 fprintf(stderr, " %18.9f seconds time elapsed",
683 avg_stats(&walltime_nsecs_stats)/1e9);
685 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
686 avg_stats(&walltime_nsecs_stats));
688 fprintf(stderr, "\n\n");
692 static volatile int signr = -1;
694 static void skip_signal(int signo)
702 static void sig_atexit(void)
705 kill(child_pid, SIGTERM);
710 signal(signr, SIG_DFL);
711 kill(getpid(), signr);
714 static const char * const stat_usage[] = {
715 "perf stat [<options>] [<command>]",
719 static int stat__set_big_num(const struct option *opt __used,
720 const char *s __used, int unset)
722 big_num_opt = unset ? 0 : 1;
726 static const struct option options[] = {
727 OPT_CALLBACK('e', "event", &evsel_list, "event",
728 "event selector. use 'perf list' to list available events",
730 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
731 "event filter", parse_filter),
732 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
733 "child tasks do not inherit counters"),
734 OPT_INTEGER('p', "pid", &target_pid,
735 "stat events on existing process id"),
736 OPT_INTEGER('t', "tid", &target_tid,
737 "stat events on existing thread id"),
738 OPT_BOOLEAN('a', "all-cpus", &system_wide,
739 "system-wide collection from all CPUs"),
740 OPT_BOOLEAN('c', "scale", &scale,
741 "scale/normalize counters"),
742 OPT_INCR('v', "verbose", &verbose,
743 "be more verbose (show counter open errors, etc)"),
744 OPT_INTEGER('r', "repeat", &run_count,
745 "repeat command and print average + stddev (max: 100)"),
746 OPT_BOOLEAN('n', "null", &null_run,
747 "null run - dont start any counters"),
748 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
749 "print large numbers with thousands\' separators",
751 OPT_STRING('C', "cpu", &cpu_list, "cpu",
752 "list of cpus to monitor in system-wide"),
753 OPT_BOOLEAN('A', "no-aggr", &no_aggr,
754 "disable CPU count aggregation"),
755 OPT_STRING('x', "field-separator", &csv_sep, "separator",
756 "print counts with custom separator"),
757 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
758 "monitor event in cgroup name only",
763 int cmd_stat(int argc, const char **argv, const char *prefix __used)
765 struct perf_evsel *pos;
766 int status = -ENOMEM;
768 setlocale(LC_ALL, "");
770 evsel_list = perf_evlist__new(NULL, NULL);
771 if (evsel_list == NULL)
774 argc = parse_options(argc, argv, options, stat_usage,
775 PARSE_OPT_STOP_AT_NON_OPTION);
780 csv_sep = DEFAULT_SEPARATOR;
783 * let the spreadsheet do the pretty-printing
786 /* User explicitely passed -B? */
787 if (big_num_opt == 1) {
788 fprintf(stderr, "-B option not supported with -x\n");
789 usage_with_options(stat_usage, options);
790 } else /* Nope, so disable big number formatting */
792 } else if (big_num_opt == 0) /* User passed --no-big-num */
795 if (!argc && target_pid == -1 && target_tid == -1)
796 usage_with_options(stat_usage, options);
798 usage_with_options(stat_usage, options);
800 /* no_aggr, cgroup are for system-wide only */
801 if ((no_aggr || nr_cgroups) && !system_wide) {
802 fprintf(stderr, "both cgroup and no-aggregation "
803 "modes only available in system-wide mode\n");
805 usage_with_options(stat_usage, options);
808 /* Set attrs and nr_counters if no event is selected and !null_run */
809 if (!null_run && !evsel_list->nr_entries) {
812 for (c = 0; c < ARRAY_SIZE(default_attrs); ++c) {
813 pos = perf_evsel__new(&default_attrs[c], c);
816 perf_evlist__add(evsel_list, pos);
820 if (target_pid != -1)
821 target_tid = target_pid;
823 evsel_list->threads = thread_map__new(target_pid, target_tid);
824 if (evsel_list->threads == NULL) {
825 pr_err("Problems finding threads of monitor\n");
826 usage_with_options(stat_usage, options);
830 evsel_list->cpus = cpu_map__new(cpu_list);
832 evsel_list->cpus = cpu_map__dummy_new();
834 if (evsel_list->cpus == NULL) {
835 perror("failed to parse CPUs map");
836 usage_with_options(stat_usage, options);
840 list_for_each_entry(pos, &evsel_list->entries, node) {
841 if (perf_evsel__alloc_stat_priv(pos) < 0 ||
842 perf_evsel__alloc_counts(pos, evsel_list->cpus->nr) < 0 ||
843 perf_evsel__alloc_fd(pos, evsel_list->cpus->nr, evsel_list->threads->nr) < 0)
848 * We dont want to block the signals - that would cause
849 * child tasks to inherit that and Ctrl-C would not work.
850 * What we want is for Ctrl-C to work in the exec()-ed
851 * task, but being ignored by perf stat itself:
854 signal(SIGINT, skip_signal);
855 signal(SIGALRM, skip_signal);
856 signal(SIGABRT, skip_signal);
859 for (run_idx = 0; run_idx < run_count; run_idx++) {
860 if (run_count != 1 && verbose)
861 fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1);
862 status = run_perf_stat(argc, argv);
866 print_stat(argc, argv);
868 list_for_each_entry(pos, &evsel_list->entries, node)
869 perf_evsel__free_stat_priv(pos);
870 perf_evlist__delete_maps(evsel_list);
872 perf_evlist__delete(evsel_list);