4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
9 $ perf stat ./hackbench 10
13 Performance counter stats for './hackbench 10':
15 1708.761321 task-clock # 11.037 CPUs utilized
16 41,190 context-switches # 0.024 M/sec
17 6,735 CPU-migrations # 0.004 M/sec
18 17,318 page-faults # 0.010 M/sec
19 5,205,202,243 cycles # 3.046 GHz
20 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
21 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
22 2,603,501,247 instructions # 0.50 insns per cycle
23 # 1.48 stalled cycles per insn
24 484,357,498 branches # 283.455 M/sec
25 6,388,934 branch-misses # 1.32% of all branches
27 0.154822978 seconds time elapsed
30 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
32 * Improvements and fixes by:
34 * Arjan van de Ven <arjan@linux.intel.com>
35 * Yanmin Zhang <yanmin.zhang@intel.com>
36 * Wu Fengguang <fengguang.wu@intel.com>
37 * Mike Galbraith <efault@gmx.de>
38 * Paul Mackerras <paulus@samba.org>
39 * Jaswinder Singh Rajput <jaswinder@kernel.org>
41 * Released under the GPL v2. (and only v2, not any later version)
46 #include "util/cgroup.h"
47 #include "util/util.h"
48 #include <subcmd/parse-options.h>
49 #include "util/parse-events.h"
51 #include "util/event.h"
52 #include "util/evlist.h"
53 #include "util/evsel.h"
54 #include "util/debug.h"
55 #include "util/color.h"
56 #include "util/stat.h"
57 #include "util/header.h"
58 #include "util/cpumap.h"
59 #include "util/thread.h"
60 #include "util/thread_map.h"
61 #include "util/counts.h"
62 #include "util/group.h"
63 #include "util/session.h"
64 #include "util/tool.h"
65 #include "util/group.h"
68 #include <api/fs/fs.h>
70 #include <sys/prctl.h>
74 #define DEFAULT_SEPARATOR " "
75 #define CNTR_NOT_SUPPORTED "<not supported>"
76 #define CNTR_NOT_COUNTED "<not counted>"
78 static void print_counters(struct timespec *ts, int argc, const char **argv);
80 /* Default events used for perf stat -T */
81 static const char *transaction_attrs = {
93 /* More limited version when the CPU does not have all events. */
94 static const char * transaction_limited_attrs = {
104 static const char * topdown_attrs[] = {
105 "topdown-total-slots",
106 "topdown-slots-retired",
107 "topdown-recovery-bubbles",
108 "topdown-fetch-bubbles",
109 "topdown-slots-issued",
113 static struct perf_evlist *evsel_list;
115 static struct target target = {
119 typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
121 static int run_count = 1;
122 static bool no_inherit = false;
123 static volatile pid_t child_pid = -1;
124 static bool null_run = false;
125 static int detailed_run = 0;
126 static bool transaction_run;
127 static bool topdown_run = false;
128 static bool big_num = true;
129 static int big_num_opt = -1;
130 static const char *csv_sep = NULL;
131 static bool csv_output = false;
132 static bool group = false;
133 static const char *pre_cmd = NULL;
134 static const char *post_cmd = NULL;
135 static bool sync_run = false;
136 static unsigned int initial_delay = 0;
137 static unsigned int unit_width = 4; /* strlen("unit") */
138 static bool forever = false;
139 static bool metric_only = false;
140 static bool force_metric_only = false;
141 static struct timespec ref_time;
142 static struct cpu_map *aggr_map;
143 static aggr_get_id_t aggr_get_id;
144 static bool append_file;
145 static const char *output_name;
146 static int output_fd;
150 struct perf_data_file file;
151 struct perf_session *session;
153 struct perf_tool tool;
155 struct cpu_map *cpus;
156 struct thread_map *threads;
157 enum aggr_mode aggr_mode;
160 static struct perf_stat perf_stat;
161 #define STAT_RECORD perf_stat.record
163 static volatile int done = 0;
165 static struct perf_stat_config stat_config = {
166 .aggr_mode = AGGR_GLOBAL,
170 static inline void diff_timespec(struct timespec *r, struct timespec *a,
173 r->tv_sec = a->tv_sec - b->tv_sec;
174 if (a->tv_nsec < b->tv_nsec) {
175 r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
178 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
182 static void perf_stat__reset_stats(void)
184 perf_evlist__reset_stats(evsel_list);
185 perf_stat__reset_shadow_stats();
188 static int create_perf_stat_counter(struct perf_evsel *evsel)
190 struct perf_event_attr *attr = &evsel->attr;
192 if (stat_config.scale)
193 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
194 PERF_FORMAT_TOTAL_TIME_RUNNING;
196 attr->inherit = !no_inherit;
199 * Some events get initialized with sample_(period/type) set,
200 * like tracepoints. Clear it up for counting.
202 attr->sample_period = 0;
205 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
206 * while avoiding that older tools show confusing messages.
208 * However for pipe sessions we need to keep it zero,
209 * because script's perf_evsel__check_attr is triggered
210 * by attr->sample_type != 0, and we can't run it on
213 if (!(STAT_RECORD && perf_stat.file.is_pipe))
214 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
217 * Disabling all counters initially, they will be enabled
218 * either manually by us or by kernel via enable_on_exec
221 if (perf_evsel__is_group_leader(evsel)) {
225 * In case of initial_delay we enable tracee
228 if (target__none(&target) && !initial_delay)
229 attr->enable_on_exec = 1;
232 if (target__has_cpu(&target))
233 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
235 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
239 * Does the counter have nsecs as a unit?
241 static inline int nsec_counter(struct perf_evsel *evsel)
243 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
244 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
250 static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
251 union perf_event *event,
252 struct perf_sample *sample __maybe_unused,
253 struct machine *machine __maybe_unused)
255 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
256 pr_err("failed to write perf data, error: %m\n");
260 perf_stat.bytes_written += event->header.size;
264 static int write_stat_round_event(u64 tm, u64 type)
266 return perf_event__synthesize_stat_round(NULL, tm, type,
267 process_synthesized_event,
271 #define WRITE_STAT_ROUND_EVENT(time, interval) \
272 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
274 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
277 perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
278 struct perf_counts_values *count)
280 struct perf_sample_id *sid = SID(counter, cpu, thread);
282 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
283 process_synthesized_event, NULL);
287 * Read out the results of a single counter:
288 * do not aggregate counts across CPUs in system-wide mode
290 static int read_counter(struct perf_evsel *counter)
292 int nthreads = thread_map__nr(evsel_list->threads);
293 int ncpus, cpu, thread;
295 if (target__has_cpu(&target))
296 ncpus = perf_evsel__nr_cpus(counter);
300 if (!counter->supported)
303 if (counter->system_wide)
306 for (thread = 0; thread < nthreads; thread++) {
307 for (cpu = 0; cpu < ncpus; cpu++) {
308 struct perf_counts_values *count;
310 count = perf_counts(counter->counts, cpu, thread);
311 if (perf_evsel__read(counter, cpu, thread, count))
315 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
316 pr_err("failed to write stat event\n");
322 fprintf(stat_config.output,
323 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
324 perf_evsel__name(counter),
326 count->val, count->ena, count->run);
334 static void read_counters(bool close_counters)
336 struct perf_evsel *counter;
338 evlist__for_each_entry(evsel_list, counter) {
339 if (read_counter(counter))
340 pr_debug("failed to read counter %s\n", counter->name);
342 if (perf_stat_process_counter(&stat_config, counter))
343 pr_warning("failed to process counter %s\n", counter->name);
345 if (close_counters) {
346 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
347 thread_map__nr(evsel_list->threads));
352 static void process_interval(void)
354 struct timespec ts, rs;
356 read_counters(false);
358 clock_gettime(CLOCK_MONOTONIC, &ts);
359 diff_timespec(&rs, &ts, &ref_time);
362 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSECS_PER_SEC + rs.tv_nsec, INTERVAL))
363 pr_err("failed to write stat round event\n");
366 print_counters(&rs, 0, NULL);
369 static void enable_counters(void)
372 usleep(initial_delay * 1000);
375 * We need to enable counters only if:
376 * - we don't have tracee (attaching to task or cpu)
377 * - we have initial delay configured
379 if (!target__none(&target) || initial_delay)
380 perf_evlist__enable(evsel_list);
383 static volatile int workload_exec_errno;
386 * perf_evlist__prepare_workload will send a SIGUSR1
387 * if the fork fails, since we asked by setting its
388 * want_signal to true.
390 static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
391 void *ucontext __maybe_unused)
393 workload_exec_errno = info->si_value.sival_int;
396 static bool has_unit(struct perf_evsel *counter)
398 return counter->unit && *counter->unit;
401 static bool has_scale(struct perf_evsel *counter)
403 return counter->scale != 1;
406 static int perf_stat_synthesize_config(bool is_pipe)
408 struct perf_evsel *counter;
412 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
413 process_synthesized_event);
415 pr_err("Couldn't synthesize attrs.\n");
421 * Synthesize other events stuff not carried within
422 * attr event - unit, scale, name
424 evlist__for_each_entry(evsel_list, counter) {
425 if (!counter->supported)
429 * Synthesize unit and scale only if it's defined.
431 if (has_unit(counter)) {
432 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
434 pr_err("Couldn't synthesize evsel unit.\n");
439 if (has_scale(counter)) {
440 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
442 pr_err("Couldn't synthesize evsel scale.\n");
447 if (counter->own_cpus) {
448 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
450 pr_err("Couldn't synthesize evsel scale.\n");
456 * Name is needed only for pipe output,
457 * perf.data carries event names.
460 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
462 pr_err("Couldn't synthesize evsel name.\n");
468 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
469 process_synthesized_event,
472 pr_err("Couldn't synthesize thread map.\n");
476 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
477 process_synthesized_event, NULL);
479 pr_err("Couldn't synthesize thread map.\n");
483 err = perf_event__synthesize_stat_config(NULL, &stat_config,
484 process_synthesized_event, NULL);
486 pr_err("Couldn't synthesize config.\n");
493 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
495 static int __store_counter_ids(struct perf_evsel *counter,
496 struct cpu_map *cpus,
497 struct thread_map *threads)
501 for (cpu = 0; cpu < cpus->nr; cpu++) {
502 for (thread = 0; thread < threads->nr; thread++) {
503 int fd = FD(counter, cpu, thread);
505 if (perf_evlist__id_add_fd(evsel_list, counter,
506 cpu, thread, fd) < 0)
514 static int store_counter_ids(struct perf_evsel *counter)
516 struct cpu_map *cpus = counter->cpus;
517 struct thread_map *threads = counter->threads;
519 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
522 return __store_counter_ids(counter, cpus, threads);
525 static int __run_perf_stat(int argc, const char **argv)
527 int interval = stat_config.interval;
529 unsigned long long t0, t1;
530 struct perf_evsel *counter;
534 const bool forks = (argc > 0);
535 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
538 ts.tv_sec = interval / 1000;
539 ts.tv_nsec = (interval % 1000) * 1000000;
546 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
547 workload_exec_failed_signal) < 0) {
548 perror("failed to prepare workload");
551 child_pid = evsel_list->workload.pid;
555 perf_evlist__set_leader(evsel_list);
557 evlist__for_each_entry(evsel_list, counter) {
559 if (create_perf_stat_counter(counter) < 0) {
561 * PPC returns ENXIO for HW counters until 2.6.37
562 * (behavior changed with commit b0a873e).
564 if (errno == EINVAL || errno == ENOSYS ||
565 errno == ENOENT || errno == EOPNOTSUPP ||
568 ui__warning("%s event is not supported by the kernel.\n",
569 perf_evsel__name(counter));
570 counter->supported = false;
572 if ((counter->leader != counter) ||
573 !(counter->leader->nr_members > 1))
575 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
577 ui__warning("%s\n", msg);
581 perf_evsel__open_strerror(counter, &target,
582 errno, msg, sizeof(msg));
583 ui__error("%s\n", msg);
586 kill(child_pid, SIGTERM);
590 counter->supported = true;
592 l = strlen(counter->unit);
596 if (STAT_RECORD && store_counter_ids(counter))
600 if (perf_evlist__apply_filters(evsel_list, &counter)) {
601 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
602 counter->filter, perf_evsel__name(counter), errno,
603 str_error_r(errno, msg, sizeof(msg)));
608 int err, fd = perf_data_file__fd(&perf_stat.file);
611 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
613 err = perf_session__write_header(perf_stat.session, evsel_list,
620 err = perf_stat_synthesize_config(is_pipe);
626 * Enable counters and exec the command:
629 clock_gettime(CLOCK_MONOTONIC, &ref_time);
632 perf_evlist__start_workload(evsel_list);
636 while (!waitpid(child_pid, &status, WNOHANG)) {
637 nanosleep(&ts, NULL);
643 if (workload_exec_errno) {
644 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
645 pr_err("Workload failed: %s\n", emsg);
649 if (WIFSIGNALED(status))
650 psignal(WTERMSIG(status), argv[0]);
654 nanosleep(&ts, NULL);
662 update_stats(&walltime_nsecs_stats, t1 - t0);
666 return WEXITSTATUS(status);
669 static int run_perf_stat(int argc, const char **argv)
674 ret = system(pre_cmd);
682 ret = __run_perf_stat(argc, argv);
687 ret = system(post_cmd);
695 static void print_running(u64 run, u64 ena)
698 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
702 ena ? 100.0 * run / ena : 100.0);
703 } else if (run != ena) {
704 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
708 static void print_noise_pct(double total, double avg)
710 double pct = rel_stddev_stats(total, avg);
713 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
715 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
718 static void print_noise(struct perf_evsel *evsel, double avg)
720 struct perf_stat_evsel *ps;
726 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
729 static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
731 switch (stat_config.aggr_mode) {
733 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
734 cpu_map__id_to_socket(id),
736 cpu_map__id_to_cpu(id),
743 fprintf(stat_config.output, "S%*d%s%*d%s",
752 fprintf(stat_config.output, "CPU%*d%s",
754 perf_evsel__cpus(evsel)->map[id], csv_sep);
757 fprintf(stat_config.output, "%*s-%*d%s",
759 thread_map__comm(evsel->threads, id),
761 thread_map__pid(evsel->threads, id),
777 struct perf_evsel *evsel;
780 #define METRIC_LEN 35
782 static void new_line_std(void *ctx)
784 struct outstate *os = ctx;
789 static void do_new_line_std(struct outstate *os)
792 fputs(os->prefix, os->fh);
793 aggr_printout(os->evsel, os->id, os->nr);
794 if (stat_config.aggr_mode == AGGR_NONE)
795 fprintf(os->fh, " ");
796 fprintf(os->fh, " ");
799 static void print_metric_std(void *ctx, const char *color, const char *fmt,
800 const char *unit, double val)
802 struct outstate *os = ctx;
805 bool newline = os->newline;
809 if (unit == NULL || fmt == NULL) {
810 fprintf(out, "%-*s", METRIC_LEN, "");
817 n = fprintf(out, " # ");
819 n += color_fprintf(out, color, fmt, val);
821 n += fprintf(out, fmt, val);
822 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
825 static void new_line_csv(void *ctx)
827 struct outstate *os = ctx;
832 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
833 aggr_printout(os->evsel, os->id, os->nr);
834 for (i = 0; i < os->nfields; i++)
835 fputs(csv_sep, os->fh);
838 static void print_metric_csv(void *ctx,
839 const char *color __maybe_unused,
840 const char *fmt, const char *unit, double val)
842 struct outstate *os = ctx;
844 char buf[64], *vals, *ends;
846 if (unit == NULL || fmt == NULL) {
847 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
850 snprintf(buf, sizeof(buf), fmt, val);
852 while (isspace(*vals))
855 while (isdigit(*ends) || *ends == '.')
858 while (isspace(*unit))
860 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
863 #define METRIC_ONLY_LEN 20
865 /* Filter out some columns that don't work well in metrics only mode */
867 static bool valid_only_metric(const char *unit)
871 if (strstr(unit, "/sec") ||
872 strstr(unit, "hz") ||
873 strstr(unit, "Hz") ||
874 strstr(unit, "CPUs utilized"))
879 static const char *fixunit(char *buf, struct perf_evsel *evsel,
882 if (!strncmp(unit, "of all", 6)) {
883 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
890 static void print_metric_only(void *ctx, const char *color, const char *fmt,
891 const char *unit, double val)
893 struct outstate *os = ctx;
897 unsigned mlen = METRIC_ONLY_LEN;
899 if (!valid_only_metric(unit))
901 unit = fixunit(buf, os->evsel, unit);
903 n = color_fprintf(out, color, fmt, val);
905 n = fprintf(out, fmt, val);
906 if (n > METRIC_ONLY_LEN)
908 if (mlen < strlen(unit))
909 mlen = strlen(unit) + 1;
910 fprintf(out, "%*s", mlen - n, "");
913 static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
915 const char *unit, double val)
917 struct outstate *os = ctx;
919 char buf[64], *vals, *ends;
922 if (!valid_only_metric(unit))
924 unit = fixunit(tbuf, os->evsel, unit);
925 snprintf(buf, sizeof buf, fmt, val);
927 while (isspace(*vals))
930 while (isdigit(*ends) || *ends == '.')
933 fprintf(out, "%s%s", vals, csv_sep);
936 static void new_line_metric(void *ctx __maybe_unused)
940 static void print_metric_header(void *ctx, const char *color __maybe_unused,
941 const char *fmt __maybe_unused,
942 const char *unit, double val __maybe_unused)
944 struct outstate *os = ctx;
947 if (!valid_only_metric(unit))
949 unit = fixunit(tbuf, os->evsel, unit);
951 fprintf(os->fh, "%s%s", unit, csv_sep);
953 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
956 static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
958 FILE *output = stat_config.output;
959 double msecs = avg / 1e6;
960 const char *fmt_v, *fmt_n;
963 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
964 fmt_n = csv_output ? "%s" : "%-25s";
966 aggr_printout(evsel, id, nr);
968 scnprintf(name, sizeof(name), "%s%s",
969 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
971 fprintf(output, fmt_v, msecs, csv_sep);
974 fprintf(output, "%s%s", evsel->unit, csv_sep);
976 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
978 fprintf(output, fmt_n, name);
981 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
984 static int first_shadow_cpu(struct perf_evsel *evsel, int id)
991 if (stat_config.aggr_mode == AGGR_NONE)
994 if (stat_config.aggr_mode == AGGR_GLOBAL)
997 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
998 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1000 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1006 static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1008 FILE *output = stat_config.output;
1009 double sc = evsel->scale;
1013 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
1016 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
1018 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
1021 aggr_printout(evsel, id, nr);
1023 fprintf(output, fmt, avg, csv_sep);
1026 fprintf(output, "%-*s%s",
1027 csv_output ? 0 : unit_width,
1028 evsel->unit, csv_sep);
1030 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1033 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
1036 static void printout(int id, int nr, struct perf_evsel *counter, double uval,
1037 char *prefix, u64 run, u64 ena, double noise)
1039 struct perf_stat_output_ctx out;
1040 struct outstate os = {
1041 .fh = stat_config.output,
1042 .prefix = prefix ? prefix : "",
1047 print_metric_t pm = print_metric_std;
1051 nl = new_line_metric;
1053 pm = print_metric_only_csv;
1055 pm = print_metric_only;
1059 if (csv_output && !metric_only) {
1060 static int aggr_fields[] = {
1068 pm = print_metric_csv;
1071 os.nfields += aggr_fields[stat_config.aggr_mode];
1075 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
1077 pm(&os, NULL, "", "", 0);
1080 aggr_printout(counter, id, nr);
1082 fprintf(stat_config.output, "%*s%s",
1083 csv_output ? 0 : 18,
1084 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1087 fprintf(stat_config.output, "%-*s%s",
1088 csv_output ? 0 : unit_width,
1089 counter->unit, csv_sep);
1091 fprintf(stat_config.output, "%*s",
1092 csv_output ? 0 : -25,
1093 perf_evsel__name(counter));
1096 fprintf(stat_config.output, "%s%s",
1097 csv_sep, counter->cgrp->name);
1100 pm(&os, NULL, NULL, "", 0);
1101 print_noise(counter, noise);
1102 print_running(run, ena);
1104 pm(&os, NULL, NULL, "", 0);
1110 else if (nsec_counter(counter))
1111 nsec_printout(id, nr, counter, uval);
1113 abs_printout(id, nr, counter, uval);
1115 out.print_metric = pm;
1119 if (csv_output && !metric_only) {
1120 print_noise(counter, noise);
1121 print_running(run, ena);
1124 perf_stat__print_shadow_stats(counter, uval,
1125 first_shadow_cpu(counter, id),
1127 if (!csv_output && !metric_only) {
1128 print_noise(counter, noise);
1129 print_running(run, ena);
1133 static void aggr_update_shadow(void)
1137 struct perf_evsel *counter;
1139 for (s = 0; s < aggr_map->nr; s++) {
1140 id = aggr_map->map[s];
1141 evlist__for_each_entry(evsel_list, counter) {
1143 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1144 s2 = aggr_get_id(evsel_list->cpus, cpu);
1147 val += perf_counts(counter->counts, cpu, 0)->val;
1149 val = val * counter->scale;
1150 perf_stat__update_shadow_stats(counter, &val,
1151 first_shadow_cpu(counter, id));
1156 static void print_aggr(char *prefix)
1158 FILE *output = stat_config.output;
1159 struct perf_evsel *counter;
1160 int cpu, s, s2, id, nr;
1165 if (!(aggr_map || aggr_get_id))
1168 aggr_update_shadow();
1171 * With metric_only everything is on a single line.
1172 * Without each counter has its own line.
1174 for (s = 0; s < aggr_map->nr; s++) {
1175 if (prefix && metric_only)
1176 fprintf(output, "%s", prefix);
1178 id = aggr_map->map[s];
1180 evlist__for_each_entry(evsel_list, counter) {
1181 val = ena = run = 0;
1183 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1184 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1187 val += perf_counts(counter->counts, cpu, 0)->val;
1188 ena += perf_counts(counter->counts, cpu, 0)->ena;
1189 run += perf_counts(counter->counts, cpu, 0)->run;
1192 if (first && metric_only) {
1194 aggr_printout(counter, id, nr);
1196 if (prefix && !metric_only)
1197 fprintf(output, "%s", prefix);
1199 uval = val * counter->scale;
1200 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
1202 fputc('\n', output);
1205 fputc('\n', output);
1209 static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1211 FILE *output = stat_config.output;
1212 int nthreads = thread_map__nr(counter->threads);
1213 int ncpus = cpu_map__nr(counter->cpus);
1217 for (thread = 0; thread < nthreads; thread++) {
1218 u64 ena = 0, run = 0, val = 0;
1220 for (cpu = 0; cpu < ncpus; cpu++) {
1221 val += perf_counts(counter->counts, cpu, thread)->val;
1222 ena += perf_counts(counter->counts, cpu, thread)->ena;
1223 run += perf_counts(counter->counts, cpu, thread)->run;
1227 fprintf(output, "%s", prefix);
1229 uval = val * counter->scale;
1230 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
1231 fputc('\n', output);
1236 * Print out the results of a single counter:
1237 * aggregated counts in system-wide mode
1239 static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
1241 FILE *output = stat_config.output;
1242 struct perf_stat_evsel *ps = counter->priv;
1243 double avg = avg_stats(&ps->res_stats[0]);
1245 double avg_enabled, avg_running;
1247 avg_enabled = avg_stats(&ps->res_stats[1]);
1248 avg_running = avg_stats(&ps->res_stats[2]);
1250 if (prefix && !metric_only)
1251 fprintf(output, "%s", prefix);
1253 uval = avg * counter->scale;
1254 printout(-1, 0, counter, uval, prefix, avg_running, avg_enabled, avg);
1256 fprintf(output, "\n");
1260 * Print out the results of a single counter:
1261 * does not use aggregated count in system-wide
1263 static void print_counter(struct perf_evsel *counter, char *prefix)
1265 FILE *output = stat_config.output;
1270 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1271 val = perf_counts(counter->counts, cpu, 0)->val;
1272 ena = perf_counts(counter->counts, cpu, 0)->ena;
1273 run = perf_counts(counter->counts, cpu, 0)->run;
1276 fprintf(output, "%s", prefix);
1278 uval = val * counter->scale;
1279 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1281 fputc('\n', output);
1285 static void print_no_aggr_metric(char *prefix)
1289 struct perf_evsel *counter;
1293 nrcpus = evsel_list->cpus->nr;
1294 for (cpu = 0; cpu < nrcpus; cpu++) {
1298 fputs(prefix, stat_config.output);
1299 evlist__for_each_entry(evsel_list, counter) {
1301 aggr_printout(counter, cpu, 0);
1304 val = perf_counts(counter->counts, cpu, 0)->val;
1305 ena = perf_counts(counter->counts, cpu, 0)->ena;
1306 run = perf_counts(counter->counts, cpu, 0)->run;
1308 uval = val * counter->scale;
1309 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1311 fputc('\n', stat_config.output);
1315 static int aggr_header_lens[] = {
1323 static const char *aggr_header_csv[] = {
1324 [AGGR_CORE] = "core,cpus,",
1325 [AGGR_SOCKET] = "socket,cpus",
1326 [AGGR_NONE] = "cpu,",
1327 [AGGR_THREAD] = "comm-pid,",
1331 static void print_metric_headers(const char *prefix, bool no_indent)
1333 struct perf_stat_output_ctx out;
1334 struct perf_evsel *counter;
1335 struct outstate os = {
1336 .fh = stat_config.output
1340 fprintf(stat_config.output, "%s", prefix);
1342 if (!csv_output && !no_indent)
1343 fprintf(stat_config.output, "%*s",
1344 aggr_header_lens[stat_config.aggr_mode], "");
1346 if (stat_config.interval)
1347 fputs("time,", stat_config.output);
1348 fputs(aggr_header_csv[stat_config.aggr_mode],
1349 stat_config.output);
1352 /* Print metrics headers only */
1353 evlist__for_each_entry(evsel_list, counter) {
1356 out.print_metric = print_metric_header;
1357 out.new_line = new_line_metric;
1359 perf_stat__print_shadow_stats(counter, 0,
1363 fputc('\n', stat_config.output);
1366 static void print_interval(char *prefix, struct timespec *ts)
1368 FILE *output = stat_config.output;
1369 static int num_print_interval;
1371 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1373 if (num_print_interval == 0 && !csv_output) {
1374 switch (stat_config.aggr_mode) {
1376 fprintf(output, "# time socket cpus");
1378 fprintf(output, " counts %*s events\n", unit_width, "unit");
1381 fprintf(output, "# time core cpus");
1383 fprintf(output, " counts %*s events\n", unit_width, "unit");
1386 fprintf(output, "# time CPU");
1388 fprintf(output, " counts %*s events\n", unit_width, "unit");
1391 fprintf(output, "# time comm-pid");
1393 fprintf(output, " counts %*s events\n", unit_width, "unit");
1397 fprintf(output, "# time");
1399 fprintf(output, " counts %*s events\n", unit_width, "unit");
1405 if (num_print_interval == 0 && metric_only)
1406 print_metric_headers(" ", true);
1407 if (++num_print_interval == 25)
1408 num_print_interval = 0;
1411 static void print_header(int argc, const char **argv)
1413 FILE *output = stat_config.output;
1419 fprintf(output, "\n");
1420 fprintf(output, " Performance counter stats for ");
1421 if (target.system_wide)
1422 fprintf(output, "\'system wide");
1423 else if (target.cpu_list)
1424 fprintf(output, "\'CPU(s) %s", target.cpu_list);
1425 else if (!target__has_task(&target)) {
1426 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1427 for (i = 1; argv && (i < argc); i++)
1428 fprintf(output, " %s", argv[i]);
1429 } else if (target.pid)
1430 fprintf(output, "process id \'%s", target.pid);
1432 fprintf(output, "thread id \'%s", target.tid);
1434 fprintf(output, "\'");
1436 fprintf(output, " (%d runs)", run_count);
1437 fprintf(output, ":\n\n");
1441 static void print_footer(void)
1443 FILE *output = stat_config.output;
1446 fprintf(output, "\n");
1447 fprintf(output, " %17.9f seconds time elapsed",
1448 avg_stats(&walltime_nsecs_stats)/1e9);
1449 if (run_count > 1) {
1450 fprintf(output, " ");
1451 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1452 avg_stats(&walltime_nsecs_stats));
1454 fprintf(output, "\n\n");
1457 static void print_counters(struct timespec *ts, int argc, const char **argv)
1459 int interval = stat_config.interval;
1460 struct perf_evsel *counter;
1461 char buf[64], *prefix = NULL;
1463 /* Do not print anything if we record to the pipe. */
1464 if (STAT_RECORD && perf_stat.file.is_pipe)
1468 print_interval(prefix = buf, ts);
1470 print_header(argc, argv);
1473 static int num_print_iv;
1475 if (num_print_iv == 0 && !interval)
1476 print_metric_headers(prefix, false);
1477 if (num_print_iv++ == 25)
1479 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1480 fprintf(stat_config.output, "%s", prefix);
1483 switch (stat_config.aggr_mode) {
1489 evlist__for_each_entry(evsel_list, counter)
1490 print_aggr_thread(counter, prefix);
1493 evlist__for_each_entry(evsel_list, counter)
1494 print_counter_aggr(counter, prefix);
1496 fputc('\n', stat_config.output);
1500 print_no_aggr_metric(prefix);
1502 evlist__for_each_entry(evsel_list, counter)
1503 print_counter(counter, prefix);
1511 if (!interval && !csv_output)
1514 fflush(stat_config.output);
1517 static volatile int signr = -1;
1519 static void skip_signal(int signo)
1521 if ((child_pid == -1) || stat_config.interval)
1526 * render child_pid harmless
1527 * won't send SIGTERM to a random
1528 * process in case of race condition
1529 * and fast PID recycling
1534 static void sig_atexit(void)
1539 * avoid race condition with SIGCHLD handler
1540 * in skip_signal() which is modifying child_pid
1541 * goal is to avoid send SIGTERM to a random
1545 sigaddset(&set, SIGCHLD);
1546 sigprocmask(SIG_BLOCK, &set, &oset);
1548 if (child_pid != -1)
1549 kill(child_pid, SIGTERM);
1551 sigprocmask(SIG_SETMASK, &oset, NULL);
1556 signal(signr, SIG_DFL);
1557 kill(getpid(), signr);
1560 static int stat__set_big_num(const struct option *opt __maybe_unused,
1561 const char *s __maybe_unused, int unset)
1563 big_num_opt = unset ? 0 : 1;
1567 static int enable_metric_only(const struct option *opt __maybe_unused,
1568 const char *s __maybe_unused, int unset)
1570 force_metric_only = true;
1571 metric_only = !unset;
1575 static const struct option stat_options[] = {
1576 OPT_BOOLEAN('T', "transaction", &transaction_run,
1577 "hardware transaction statistics"),
1578 OPT_CALLBACK('e', "event", &evsel_list, "event",
1579 "event selector. use 'perf list' to list available events",
1580 parse_events_option),
1581 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1582 "event filter", parse_filter),
1583 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1584 "child tasks do not inherit counters"),
1585 OPT_STRING('p', "pid", &target.pid, "pid",
1586 "stat events on existing process id"),
1587 OPT_STRING('t', "tid", &target.tid, "tid",
1588 "stat events on existing thread id"),
1589 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1590 "system-wide collection from all CPUs"),
1591 OPT_BOOLEAN('g', "group", &group,
1592 "put the counters into a counter group"),
1593 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1594 OPT_INCR('v', "verbose", &verbose,
1595 "be more verbose (show counter open errors, etc)"),
1596 OPT_INTEGER('r', "repeat", &run_count,
1597 "repeat command and print average + stddev (max: 100, forever: 0)"),
1598 OPT_BOOLEAN('n', "null", &null_run,
1599 "null run - dont start any counters"),
1600 OPT_INCR('d', "detailed", &detailed_run,
1601 "detailed run - start a lot of events"),
1602 OPT_BOOLEAN('S', "sync", &sync_run,
1603 "call sync() before starting a run"),
1604 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1605 "print large numbers with thousands\' separators",
1607 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1608 "list of cpus to monitor in system-wide"),
1609 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1610 "disable CPU count aggregation", AGGR_NONE),
1611 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1612 "print counts with custom separator"),
1613 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1614 "monitor event in cgroup name only", parse_cgroups),
1615 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1616 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1617 OPT_INTEGER(0, "log-fd", &output_fd,
1618 "log output to fd, instead of stderr"),
1619 OPT_STRING(0, "pre", &pre_cmd, "command",
1620 "command to run prior to the measured command"),
1621 OPT_STRING(0, "post", &post_cmd, "command",
1622 "command to run after to the measured command"),
1623 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1624 "print counts at regular interval in ms (>= 10)"),
1625 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1626 "aggregate counts per processor socket", AGGR_SOCKET),
1627 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1628 "aggregate counts per physical processor core", AGGR_CORE),
1629 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1630 "aggregate counts per thread", AGGR_THREAD),
1631 OPT_UINTEGER('D', "delay", &initial_delay,
1632 "ms to wait before starting measurement after program start"),
1633 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1634 "Only print computed metrics. No raw values", enable_metric_only),
1635 OPT_BOOLEAN(0, "topdown", &topdown_run,
1636 "measure topdown level 1 statistics"),
1640 static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1642 return cpu_map__get_socket(map, cpu, NULL);
1645 static int perf_stat__get_core(struct cpu_map *map, int cpu)
1647 return cpu_map__get_core(map, cpu, NULL);
1650 static int cpu_map__get_max(struct cpu_map *map)
1654 for (i = 0; i < map->nr; i++) {
1655 if (map->map[i] > max)
1662 static struct cpu_map *cpus_aggr_map;
1664 static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1671 cpu = map->map[idx];
1673 if (cpus_aggr_map->map[cpu] == -1)
1674 cpus_aggr_map->map[cpu] = get_id(map, idx);
1676 return cpus_aggr_map->map[cpu];
1679 static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1681 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1684 static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1686 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1689 static int perf_stat_init_aggr_mode(void)
1693 switch (stat_config.aggr_mode) {
1695 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1696 perror("cannot build socket map");
1699 aggr_get_id = perf_stat__get_socket_cached;
1702 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1703 perror("cannot build core map");
1706 aggr_get_id = perf_stat__get_core_cached;
1717 * The evsel_list->cpus is the base we operate on,
1718 * taking the highest cpu number to be the size of
1719 * the aggregation translate cpumap.
1721 nr = cpu_map__get_max(evsel_list->cpus);
1722 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1723 return cpus_aggr_map ? 0 : -ENOMEM;
1726 static void perf_stat__exit_aggr_mode(void)
1728 cpu_map__put(aggr_map);
1729 cpu_map__put(cpus_aggr_map);
1731 cpus_aggr_map = NULL;
1734 static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1741 cpu = map->map[idx];
1743 if (cpu >= env->nr_cpus_online)
1749 static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1751 struct perf_env *env = data;
1752 int cpu = perf_env__get_cpu(env, map, idx);
1754 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1757 static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1759 struct perf_env *env = data;
1760 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1763 int socket_id = env->cpu[cpu].socket_id;
1766 * Encode socket in upper 16 bits
1767 * core_id is relative to socket, and
1768 * we need a global id. So we combine
1771 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1777 static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1778 struct cpu_map **sockp)
1780 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1783 static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1784 struct cpu_map **corep)
1786 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1789 static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1791 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1794 static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1796 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1799 static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1801 struct perf_env *env = &st->session->header.env;
1803 switch (stat_config.aggr_mode) {
1805 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1806 perror("cannot build socket map");
1809 aggr_get_id = perf_stat__get_socket_file;
1812 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1813 perror("cannot build core map");
1816 aggr_get_id = perf_stat__get_core_file;
1829 static int topdown_filter_events(const char **attr, char **str, bool use_group)
1836 for (i = 0; attr[i]; i++) {
1837 if (pmu_have_event("cpu", attr[i])) {
1838 len += strlen(attr[i]) + 1;
1839 attr[i - off] = attr[i];
1843 attr[i - off] = NULL;
1845 *str = malloc(len + 1 + 2);
1855 for (i = 0; attr[i]; i++) {
1868 __weak bool arch_topdown_check_group(bool *warn)
1874 __weak void arch_topdown_group_warn(void)
1879 * Add default attributes, if there were no attributes specified or
1880 * if -d/--detailed, -d -d or -d -d -d is used:
1882 static int add_default_attributes(void)
1885 struct perf_event_attr default_attrs0[] = {
1887 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1888 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1889 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1890 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1892 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
1894 struct perf_event_attr frontend_attrs[] = {
1895 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
1897 struct perf_event_attr backend_attrs[] = {
1898 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
1900 struct perf_event_attr default_attrs1[] = {
1901 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1902 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1903 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1908 * Detailed stats (-d), covering the L1 and last level data caches:
1910 struct perf_event_attr detailed_attrs[] = {
1912 { .type = PERF_TYPE_HW_CACHE,
1914 PERF_COUNT_HW_CACHE_L1D << 0 |
1915 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1916 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1918 { .type = PERF_TYPE_HW_CACHE,
1920 PERF_COUNT_HW_CACHE_L1D << 0 |
1921 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1922 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1924 { .type = PERF_TYPE_HW_CACHE,
1926 PERF_COUNT_HW_CACHE_LL << 0 |
1927 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1928 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1930 { .type = PERF_TYPE_HW_CACHE,
1932 PERF_COUNT_HW_CACHE_LL << 0 |
1933 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1934 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1938 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1940 struct perf_event_attr very_detailed_attrs[] = {
1942 { .type = PERF_TYPE_HW_CACHE,
1944 PERF_COUNT_HW_CACHE_L1I << 0 |
1945 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1946 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1948 { .type = PERF_TYPE_HW_CACHE,
1950 PERF_COUNT_HW_CACHE_L1I << 0 |
1951 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1952 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1954 { .type = PERF_TYPE_HW_CACHE,
1956 PERF_COUNT_HW_CACHE_DTLB << 0 |
1957 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1958 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1960 { .type = PERF_TYPE_HW_CACHE,
1962 PERF_COUNT_HW_CACHE_DTLB << 0 |
1963 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1964 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1966 { .type = PERF_TYPE_HW_CACHE,
1968 PERF_COUNT_HW_CACHE_ITLB << 0 |
1969 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1970 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1972 { .type = PERF_TYPE_HW_CACHE,
1974 PERF_COUNT_HW_CACHE_ITLB << 0 |
1975 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1976 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1981 * Very, very detailed stats (-d -d -d), adding prefetch events:
1983 struct perf_event_attr very_very_detailed_attrs[] = {
1985 { .type = PERF_TYPE_HW_CACHE,
1987 PERF_COUNT_HW_CACHE_L1D << 0 |
1988 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1989 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1991 { .type = PERF_TYPE_HW_CACHE,
1993 PERF_COUNT_HW_CACHE_L1D << 0 |
1994 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1995 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1998 /* Set attrs if no event is selected and !null_run: */
2002 if (transaction_run) {
2003 if (pmu_have_event("cpu", "cycles-ct") &&
2004 pmu_have_event("cpu", "el-start"))
2005 err = parse_events(evsel_list, transaction_attrs, NULL);
2007 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2009 fprintf(stderr, "Cannot set up transaction events\n");
2019 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2020 stat_config.aggr_mode != AGGR_CORE) {
2021 pr_err("top down event configuration requires --per-core mode\n");
2024 stat_config.aggr_mode = AGGR_CORE;
2025 if (nr_cgroups || !target__has_cpu(&target)) {
2026 pr_err("top down event configuration requires system-wide mode (-a)\n");
2030 if (!force_metric_only)
2032 if (topdown_filter_events(topdown_attrs, &str,
2033 arch_topdown_check_group(&warn)) < 0) {
2034 pr_err("Out of memory\n");
2037 if (topdown_attrs[0] && str) {
2039 arch_topdown_group_warn();
2040 err = parse_events(evsel_list, str, NULL);
2043 "Cannot set up top down events %s: %d\n",
2049 fprintf(stderr, "System does not support topdown\n");
2055 if (!evsel_list->nr_entries) {
2056 if (target__has_cpu(&target))
2057 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2059 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2061 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2062 if (perf_evlist__add_default_attrs(evsel_list,
2063 frontend_attrs) < 0)
2066 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2067 if (perf_evlist__add_default_attrs(evsel_list,
2071 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
2075 /* Detailed events get appended to the event list: */
2077 if (detailed_run < 1)
2080 /* Append detailed run extra attributes: */
2081 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
2084 if (detailed_run < 2)
2087 /* Append very detailed run extra attributes: */
2088 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
2091 if (detailed_run < 3)
2094 /* Append very, very detailed run extra attributes: */
2095 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
2098 static const char * const stat_record_usage[] = {
2099 "perf stat record [<options>]",
2103 static void init_features(struct perf_session *session)
2107 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2108 perf_header__set_feat(&session->header, feat);
2110 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2111 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2112 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2113 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2116 static int __cmd_record(int argc, const char **argv)
2118 struct perf_session *session;
2119 struct perf_data_file *file = &perf_stat.file;
2121 argc = parse_options(argc, argv, stat_options, stat_record_usage,
2122 PARSE_OPT_STOP_AT_NON_OPTION);
2125 file->path = output_name;
2127 if (run_count != 1 || forever) {
2128 pr_err("Cannot use -r option with perf stat record.\n");
2132 session = perf_session__new(file, false, NULL);
2133 if (session == NULL) {
2134 pr_err("Perf session creation failed.\n");
2138 init_features(session);
2140 session->evlist = evsel_list;
2141 perf_stat.session = session;
2142 perf_stat.record = true;
2146 static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2147 union perf_event *event,
2148 struct perf_session *session)
2150 struct stat_round_event *stat_round = &event->stat_round;
2151 struct perf_evsel *counter;
2152 struct timespec tsh, *ts = NULL;
2153 const char **argv = session->header.env.cmdline_argv;
2154 int argc = session->header.env.nr_cmdline;
2156 evlist__for_each_entry(evsel_list, counter)
2157 perf_stat_process_counter(&stat_config, counter);
2159 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2160 update_stats(&walltime_nsecs_stats, stat_round->time);
2162 if (stat_config.interval && stat_round->time) {
2163 tsh.tv_sec = stat_round->time / NSECS_PER_SEC;
2164 tsh.tv_nsec = stat_round->time % NSECS_PER_SEC;
2168 print_counters(ts, argc, argv);
2173 int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2174 union perf_event *event,
2175 struct perf_session *session __maybe_unused)
2177 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2179 perf_event__read_stat_config(&stat_config, &event->stat_config);
2181 if (cpu_map__empty(st->cpus)) {
2182 if (st->aggr_mode != AGGR_UNSET)
2183 pr_warning("warning: processing task data, aggregation mode not set\n");
2187 if (st->aggr_mode != AGGR_UNSET)
2188 stat_config.aggr_mode = st->aggr_mode;
2190 if (perf_stat.file.is_pipe)
2191 perf_stat_init_aggr_mode();
2193 perf_stat_init_aggr_mode_file(st);
2198 static int set_maps(struct perf_stat *st)
2200 if (!st->cpus || !st->threads)
2203 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2206 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2208 if (perf_evlist__alloc_stats(evsel_list, true))
2211 st->maps_allocated = true;
2216 int process_thread_map_event(struct perf_tool *tool __maybe_unused,
2217 union perf_event *event,
2218 struct perf_session *session __maybe_unused)
2220 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2223 pr_warning("Extra thread map event, ignoring.\n");
2227 st->threads = thread_map__new_event(&event->thread_map);
2231 return set_maps(st);
2235 int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2236 union perf_event *event,
2237 struct perf_session *session __maybe_unused)
2239 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2240 struct cpu_map *cpus;
2243 pr_warning("Extra cpu map event, ignoring.\n");
2247 cpus = cpu_map__new_data(&event->cpu_map.data);
2252 return set_maps(st);
2255 static const char * const stat_report_usage[] = {
2256 "perf stat report [<options>]",
2260 static struct perf_stat perf_stat = {
2262 .attr = perf_event__process_attr,
2263 .event_update = perf_event__process_event_update,
2264 .thread_map = process_thread_map_event,
2265 .cpu_map = process_cpu_map_event,
2266 .stat_config = process_stat_config_event,
2267 .stat = perf_event__process_stat_event,
2268 .stat_round = process_stat_round_event,
2270 .aggr_mode = AGGR_UNSET,
2273 static int __cmd_report(int argc, const char **argv)
2275 struct perf_session *session;
2276 const struct option options[] = {
2277 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2278 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2279 "aggregate counts per processor socket", AGGR_SOCKET),
2280 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2281 "aggregate counts per physical processor core", AGGR_CORE),
2282 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2283 "disable CPU count aggregation", AGGR_NONE),
2289 argc = parse_options(argc, argv, options, stat_report_usage, 0);
2291 if (!input_name || !strlen(input_name)) {
2292 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2295 input_name = "perf.data";
2298 perf_stat.file.path = input_name;
2299 perf_stat.file.mode = PERF_DATA_MODE_READ;
2301 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2302 if (session == NULL)
2305 perf_stat.session = session;
2306 stat_config.output = stderr;
2307 evsel_list = session->evlist;
2309 ret = perf_session__process_events(session);
2313 perf_session__delete(session);
2317 int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
2319 const char * const stat_usage[] = {
2320 "perf stat [<options>] [<command>]",
2323 int status = -EINVAL, run_idx;
2325 FILE *output = stderr;
2326 unsigned int interval;
2327 const char * const stat_subcommands[] = { "record", "report" };
2329 setlocale(LC_ALL, "");
2331 evsel_list = perf_evlist__new();
2332 if (evsel_list == NULL)
2335 parse_events__shrink_config_terms();
2336 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2337 (const char **) stat_usage,
2338 PARSE_OPT_STOP_AT_NON_OPTION);
2339 perf_stat__init_shadow_stats();
2343 if (!strcmp(csv_sep, "\\t"))
2346 csv_sep = DEFAULT_SEPARATOR;
2348 if (argc && !strncmp(argv[0], "rec", 3)) {
2349 argc = __cmd_record(argc, argv);
2352 } else if (argc && !strncmp(argv[0], "rep", 3))
2353 return __cmd_report(argc, argv);
2355 interval = stat_config.interval;
2358 * For record command the -o is already taken care of.
2360 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
2363 if (output_name && output_fd) {
2364 fprintf(stderr, "cannot use both --output and --log-fd\n");
2365 parse_options_usage(stat_usage, stat_options, "o", 1);
2366 parse_options_usage(NULL, stat_options, "log-fd", 0);
2370 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2371 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2375 if (metric_only && run_count > 1) {
2376 fprintf(stderr, "--metric-only is not supported with -r\n");
2380 if (output_fd < 0) {
2381 fprintf(stderr, "argument to --log-fd must be a > 0\n");
2382 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
2388 mode = append_file ? "a" : "w";
2390 output = fopen(output_name, mode);
2392 perror("failed to create output file");
2395 clock_gettime(CLOCK_REALTIME, &tm);
2396 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
2397 } else if (output_fd > 0) {
2398 mode = append_file ? "a" : "w";
2399 output = fdopen(output_fd, mode);
2401 perror("Failed opening logfd");
2406 stat_config.output = output;
2409 * let the spreadsheet do the pretty-printing
2412 /* User explicitly passed -B? */
2413 if (big_num_opt == 1) {
2414 fprintf(stderr, "-B option not supported with -x\n");
2415 parse_options_usage(stat_usage, stat_options, "B", 1);
2416 parse_options_usage(NULL, stat_options, "x", 1);
2418 } else /* Nope, so disable big number formatting */
2420 } else if (big_num_opt == 0) /* User passed --no-big-num */
2423 if (!argc && target__none(&target))
2424 usage_with_options(stat_usage, stat_options);
2426 if (run_count < 0) {
2427 pr_err("Run count must be a positive number\n");
2428 parse_options_usage(stat_usage, stat_options, "r", 1);
2430 } else if (run_count == 0) {
2435 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
2436 fprintf(stderr, "The --per-thread option is only available "
2437 "when monitoring via -p -t options.\n");
2438 parse_options_usage(NULL, stat_options, "p", 1);
2439 parse_options_usage(NULL, stat_options, "t", 1);
2444 * no_aggr, cgroup are for system-wide only
2445 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2447 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2448 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
2449 !target__has_cpu(&target)) {
2450 fprintf(stderr, "both cgroup and no-aggregation "
2451 "modes only available in system-wide mode\n");
2453 parse_options_usage(stat_usage, stat_options, "G", 1);
2454 parse_options_usage(NULL, stat_options, "A", 1);
2455 parse_options_usage(NULL, stat_options, "a", 1);
2459 if (add_default_attributes())
2462 target__validate(&target);
2464 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
2465 if (target__has_task(&target)) {
2466 pr_err("Problems finding threads of monitor\n");
2467 parse_options_usage(stat_usage, stat_options, "p", 1);
2468 parse_options_usage(NULL, stat_options, "t", 1);
2469 } else if (target__has_cpu(&target)) {
2470 perror("failed to parse CPUs map");
2471 parse_options_usage(stat_usage, stat_options, "C", 1);
2472 parse_options_usage(NULL, stat_options, "a", 1);
2478 * Initialize thread_map with comm names,
2479 * so we could print it out on output.
2481 if (stat_config.aggr_mode == AGGR_THREAD)
2482 thread_map__read_comms(evsel_list->threads);
2484 if (interval && interval < 100) {
2485 if (interval < 10) {
2486 pr_err("print interval must be >= 10ms\n");
2487 parse_options_usage(stat_usage, stat_options, "I", 1);
2490 pr_warning("print interval < 100ms. "
2491 "The overhead percentage could be high in some cases. "
2492 "Please proceed with caution.\n");
2495 if (perf_evlist__alloc_stats(evsel_list, interval))
2498 if (perf_stat_init_aggr_mode())
2502 * We dont want to block the signals - that would cause
2503 * child tasks to inherit that and Ctrl-C would not work.
2504 * What we want is for Ctrl-C to work in the exec()-ed
2505 * task, but being ignored by perf stat itself:
2509 signal(SIGINT, skip_signal);
2510 signal(SIGCHLD, skip_signal);
2511 signal(SIGALRM, skip_signal);
2512 signal(SIGABRT, skip_signal);
2515 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
2516 if (run_count != 1 && verbose)
2517 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2520 status = run_perf_stat(argc, argv);
2521 if (forever && status != -1) {
2522 print_counters(NULL, argc, argv);
2523 perf_stat__reset_stats();
2527 if (!forever && status != -1 && !interval)
2528 print_counters(NULL, argc, argv);
2532 * We synthesize the kernel mmap record just so that older tools
2533 * don't emit warnings about not being able to resolve symbols
2534 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2535 * a saner message about no samples being in the perf.data file.
2537 * This also serves to suppress a warning about f_header.data.size == 0
2538 * in header.c at the moment 'perf stat record' gets introduced, which
2539 * is not really needed once we start adding the stat specific PERF_RECORD_
2540 * records, but the need to suppress the kptr_restrict messages in older
2541 * tools remain -acme
2543 int fd = perf_data_file__fd(&perf_stat.file);
2544 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2545 process_synthesized_event,
2546 &perf_stat.session->machines.host);
2548 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2549 "older tools may produce warnings about this file\n.");
2553 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2554 pr_err("failed to write stat round event\n");
2557 if (!perf_stat.file.is_pipe) {
2558 perf_stat.session->header.data_size += perf_stat.bytes_written;
2559 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2562 perf_session__delete(perf_stat.session);
2565 perf_stat__exit_aggr_mode();
2566 perf_evlist__free_stats(evsel_list);
2568 perf_evlist__delete(evsel_list);