4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
9 $ perf stat ./hackbench 10
13 Performance counter stats for './hackbench 10':
15 1708.761321 task-clock # 11.037 CPUs utilized
16 41,190 context-switches # 0.024 M/sec
17 6,735 CPU-migrations # 0.004 M/sec
18 17,318 page-faults # 0.010 M/sec
19 5,205,202,243 cycles # 3.046 GHz
20 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
21 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
22 2,603,501,247 instructions # 0.50 insns per cycle
23 # 1.48 stalled cycles per insn
24 484,357,498 branches # 283.455 M/sec
25 6,388,934 branch-misses # 1.32% of all branches
27 0.154822978 seconds time elapsed
30 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
32 * Improvements and fixes by:
34 * Arjan van de Ven <arjan@linux.intel.com>
35 * Yanmin Zhang <yanmin.zhang@intel.com>
36 * Wu Fengguang <fengguang.wu@intel.com>
37 * Mike Galbraith <efault@gmx.de>
38 * Paul Mackerras <paulus@samba.org>
39 * Jaswinder Singh Rajput <jaswinder@kernel.org>
41 * Released under the GPL v2. (and only v2, not any later version)
46 #include "util/cgroup.h"
47 #include "util/util.h"
48 #include <subcmd/parse-options.h>
49 #include "util/parse-events.h"
51 #include "util/event.h"
52 #include "util/evlist.h"
53 #include "util/evsel.h"
54 #include "util/debug.h"
55 #include "util/drv_configs.h"
56 #include "util/color.h"
57 #include "util/stat.h"
58 #include "util/header.h"
59 #include "util/cpumap.h"
60 #include "util/thread.h"
61 #include "util/thread_map.h"
62 #include "util/counts.h"
63 #include "util/group.h"
64 #include "util/session.h"
65 #include "util/tool.h"
66 #include "util/group.h"
69 #include <linux/time64.h>
70 #include <api/fs/fs.h>
72 #include <sys/prctl.h>
76 #define DEFAULT_SEPARATOR " "
77 #define CNTR_NOT_SUPPORTED "<not supported>"
78 #define CNTR_NOT_COUNTED "<not counted>"
80 static void print_counters(struct timespec *ts, int argc, const char **argv);
82 /* Default events used for perf stat -T */
83 static const char *transaction_attrs = {
95 /* More limited version when the CPU does not have all events. */
96 static const char * transaction_limited_attrs = {
106 static const char * topdown_attrs[] = {
107 "topdown-total-slots",
108 "topdown-slots-retired",
109 "topdown-recovery-bubbles",
110 "topdown-fetch-bubbles",
111 "topdown-slots-issued",
115 static struct perf_evlist *evsel_list;
117 static struct target target = {
121 typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
123 static int run_count = 1;
124 static bool no_inherit = false;
125 static volatile pid_t child_pid = -1;
126 static bool null_run = false;
127 static int detailed_run = 0;
128 static bool transaction_run;
129 static bool topdown_run = false;
130 static bool big_num = true;
131 static int big_num_opt = -1;
132 static const char *csv_sep = NULL;
133 static bool csv_output = false;
134 static bool group = false;
135 static const char *pre_cmd = NULL;
136 static const char *post_cmd = NULL;
137 static bool sync_run = false;
138 static unsigned int initial_delay = 0;
139 static unsigned int unit_width = 4; /* strlen("unit") */
140 static bool forever = false;
141 static bool metric_only = false;
142 static bool force_metric_only = false;
143 static struct timespec ref_time;
144 static struct cpu_map *aggr_map;
145 static aggr_get_id_t aggr_get_id;
146 static bool append_file;
147 static const char *output_name;
148 static int output_fd;
149 static int print_free_counters_hint;
153 struct perf_data_file file;
154 struct perf_session *session;
156 struct perf_tool tool;
158 struct cpu_map *cpus;
159 struct thread_map *threads;
160 enum aggr_mode aggr_mode;
163 static struct perf_stat perf_stat;
164 #define STAT_RECORD perf_stat.record
166 static volatile int done = 0;
168 static struct perf_stat_config stat_config = {
169 .aggr_mode = AGGR_GLOBAL,
173 static inline void diff_timespec(struct timespec *r, struct timespec *a,
176 r->tv_sec = a->tv_sec - b->tv_sec;
177 if (a->tv_nsec < b->tv_nsec) {
178 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
181 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
185 static void perf_stat__reset_stats(void)
187 perf_evlist__reset_stats(evsel_list);
188 perf_stat__reset_shadow_stats();
191 static int create_perf_stat_counter(struct perf_evsel *evsel)
193 struct perf_event_attr *attr = &evsel->attr;
195 if (stat_config.scale)
196 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
197 PERF_FORMAT_TOTAL_TIME_RUNNING;
199 attr->inherit = !no_inherit;
202 * Some events get initialized with sample_(period/type) set,
203 * like tracepoints. Clear it up for counting.
205 attr->sample_period = 0;
208 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
209 * while avoiding that older tools show confusing messages.
211 * However for pipe sessions we need to keep it zero,
212 * because script's perf_evsel__check_attr is triggered
213 * by attr->sample_type != 0, and we can't run it on
216 if (!(STAT_RECORD && perf_stat.file.is_pipe))
217 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
220 * Disabling all counters initially, they will be enabled
221 * either manually by us or by kernel via enable_on_exec
224 if (perf_evsel__is_group_leader(evsel)) {
228 * In case of initial_delay we enable tracee
231 if (target__none(&target) && !initial_delay)
232 attr->enable_on_exec = 1;
235 if (target__has_cpu(&target))
236 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
238 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
242 * Does the counter have nsecs as a unit?
244 static inline int nsec_counter(struct perf_evsel *evsel)
246 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
247 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
253 static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
254 union perf_event *event,
255 struct perf_sample *sample __maybe_unused,
256 struct machine *machine __maybe_unused)
258 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
259 pr_err("failed to write perf data, error: %m\n");
263 perf_stat.bytes_written += event->header.size;
267 static int write_stat_round_event(u64 tm, u64 type)
269 return perf_event__synthesize_stat_round(NULL, tm, type,
270 process_synthesized_event,
274 #define WRITE_STAT_ROUND_EVENT(time, interval) \
275 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
277 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
280 perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
281 struct perf_counts_values *count)
283 struct perf_sample_id *sid = SID(counter, cpu, thread);
285 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
286 process_synthesized_event, NULL);
290 * Read out the results of a single counter:
291 * do not aggregate counts across CPUs in system-wide mode
293 static int read_counter(struct perf_evsel *counter)
295 int nthreads = thread_map__nr(evsel_list->threads);
296 int ncpus, cpu, thread;
298 if (target__has_cpu(&target))
299 ncpus = perf_evsel__nr_cpus(counter);
303 if (!counter->supported)
306 if (counter->system_wide)
309 for (thread = 0; thread < nthreads; thread++) {
310 for (cpu = 0; cpu < ncpus; cpu++) {
311 struct perf_counts_values *count;
313 count = perf_counts(counter->counts, cpu, thread);
314 if (perf_evsel__read(counter, cpu, thread, count))
318 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
319 pr_err("failed to write stat event\n");
325 fprintf(stat_config.output,
326 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
327 perf_evsel__name(counter),
329 count->val, count->ena, count->run);
337 static void read_counters(void)
339 struct perf_evsel *counter;
341 evlist__for_each_entry(evsel_list, counter) {
342 if (read_counter(counter))
343 pr_debug("failed to read counter %s\n", counter->name);
345 if (perf_stat_process_counter(&stat_config, counter))
346 pr_warning("failed to process counter %s\n", counter->name);
350 static void process_interval(void)
352 struct timespec ts, rs;
356 clock_gettime(CLOCK_MONOTONIC, &ts);
357 diff_timespec(&rs, &ts, &ref_time);
360 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
361 pr_err("failed to write stat round event\n");
364 print_counters(&rs, 0, NULL);
367 static void enable_counters(void)
370 usleep(initial_delay * USEC_PER_MSEC);
373 * We need to enable counters only if:
374 * - we don't have tracee (attaching to task or cpu)
375 * - we have initial delay configured
377 if (!target__none(&target) || initial_delay)
378 perf_evlist__enable(evsel_list);
381 static void disable_counters(void)
384 * If we don't have tracee (attaching to task or cpu), counters may
385 * still be running. To get accurate group ratios, we must stop groups
386 * from counting before reading their constituent counters.
388 if (!target__none(&target))
389 perf_evlist__disable(evsel_list);
392 static volatile int workload_exec_errno;
395 * perf_evlist__prepare_workload will send a SIGUSR1
396 * if the fork fails, since we asked by setting its
397 * want_signal to true.
399 static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
400 void *ucontext __maybe_unused)
402 workload_exec_errno = info->si_value.sival_int;
405 static bool has_unit(struct perf_evsel *counter)
407 return counter->unit && *counter->unit;
410 static bool has_scale(struct perf_evsel *counter)
412 return counter->scale != 1;
415 static int perf_stat_synthesize_config(bool is_pipe)
417 struct perf_evsel *counter;
421 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
422 process_synthesized_event);
424 pr_err("Couldn't synthesize attrs.\n");
430 * Synthesize other events stuff not carried within
431 * attr event - unit, scale, name
433 evlist__for_each_entry(evsel_list, counter) {
434 if (!counter->supported)
438 * Synthesize unit and scale only if it's defined.
440 if (has_unit(counter)) {
441 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
443 pr_err("Couldn't synthesize evsel unit.\n");
448 if (has_scale(counter)) {
449 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
451 pr_err("Couldn't synthesize evsel scale.\n");
456 if (counter->own_cpus) {
457 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
459 pr_err("Couldn't synthesize evsel scale.\n");
465 * Name is needed only for pipe output,
466 * perf.data carries event names.
469 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
471 pr_err("Couldn't synthesize evsel name.\n");
477 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
478 process_synthesized_event,
481 pr_err("Couldn't synthesize thread map.\n");
485 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
486 process_synthesized_event, NULL);
488 pr_err("Couldn't synthesize thread map.\n");
492 err = perf_event__synthesize_stat_config(NULL, &stat_config,
493 process_synthesized_event, NULL);
495 pr_err("Couldn't synthesize config.\n");
502 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
504 static int __store_counter_ids(struct perf_evsel *counter,
505 struct cpu_map *cpus,
506 struct thread_map *threads)
510 for (cpu = 0; cpu < cpus->nr; cpu++) {
511 for (thread = 0; thread < threads->nr; thread++) {
512 int fd = FD(counter, cpu, thread);
514 if (perf_evlist__id_add_fd(evsel_list, counter,
515 cpu, thread, fd) < 0)
523 static int store_counter_ids(struct perf_evsel *counter)
525 struct cpu_map *cpus = counter->cpus;
526 struct thread_map *threads = counter->threads;
528 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
531 return __store_counter_ids(counter, cpus, threads);
534 static int __run_perf_stat(int argc, const char **argv)
536 int interval = stat_config.interval;
538 unsigned long long t0, t1;
539 struct perf_evsel *counter;
543 const bool forks = (argc > 0);
544 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
545 struct perf_evsel_config_term *err_term;
548 ts.tv_sec = interval / USEC_PER_MSEC;
549 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
556 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
557 workload_exec_failed_signal) < 0) {
558 perror("failed to prepare workload");
561 child_pid = evsel_list->workload.pid;
565 perf_evlist__set_leader(evsel_list);
567 evlist__for_each_entry(evsel_list, counter) {
569 if (create_perf_stat_counter(counter) < 0) {
571 * PPC returns ENXIO for HW counters until 2.6.37
572 * (behavior changed with commit b0a873e).
574 if (errno == EINVAL || errno == ENOSYS ||
575 errno == ENOENT || errno == EOPNOTSUPP ||
578 ui__warning("%s event is not supported by the kernel.\n",
579 perf_evsel__name(counter));
580 counter->supported = false;
582 if ((counter->leader != counter) ||
583 !(counter->leader->nr_members > 1))
585 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
587 ui__warning("%s\n", msg);
591 perf_evsel__open_strerror(counter, &target,
592 errno, msg, sizeof(msg));
593 ui__error("%s\n", msg);
596 kill(child_pid, SIGTERM);
600 counter->supported = true;
602 l = strlen(counter->unit);
606 if (STAT_RECORD && store_counter_ids(counter))
610 if (perf_evlist__apply_filters(evsel_list, &counter)) {
611 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
612 counter->filter, perf_evsel__name(counter), errno,
613 str_error_r(errno, msg, sizeof(msg)));
617 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
618 error("failed to set config \"%s\" on event %s with %d (%s)\n",
619 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
620 str_error_r(errno, msg, sizeof(msg)));
625 int err, fd = perf_data_file__fd(&perf_stat.file);
628 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
630 err = perf_session__write_header(perf_stat.session, evsel_list,
637 err = perf_stat_synthesize_config(is_pipe);
643 * Enable counters and exec the command:
646 clock_gettime(CLOCK_MONOTONIC, &ref_time);
649 perf_evlist__start_workload(evsel_list);
653 while (!waitpid(child_pid, &status, WNOHANG)) {
654 nanosleep(&ts, NULL);
660 if (workload_exec_errno) {
661 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
662 pr_err("Workload failed: %s\n", emsg);
666 if (WIFSIGNALED(status))
667 psignal(WTERMSIG(status), argv[0]);
671 nanosleep(&ts, NULL);
681 update_stats(&walltime_nsecs_stats, t1 - t0);
684 * Closing a group leader splits the group, and as we only disable
685 * group leaders, results in remaining events becoming enabled. To
686 * avoid arbitrary skew, we must read all counters before closing any
690 perf_evlist__close(evsel_list);
692 return WEXITSTATUS(status);
695 static int run_perf_stat(int argc, const char **argv)
700 ret = system(pre_cmd);
708 ret = __run_perf_stat(argc, argv);
713 ret = system(post_cmd);
721 static void print_running(u64 run, u64 ena)
724 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
728 ena ? 100.0 * run / ena : 100.0);
729 } else if (run != ena) {
730 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
734 static void print_noise_pct(double total, double avg)
736 double pct = rel_stddev_stats(total, avg);
739 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
741 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
744 static void print_noise(struct perf_evsel *evsel, double avg)
746 struct perf_stat_evsel *ps;
752 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
755 static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
757 switch (stat_config.aggr_mode) {
759 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
760 cpu_map__id_to_socket(id),
762 cpu_map__id_to_cpu(id),
769 fprintf(stat_config.output, "S%*d%s%*d%s",
778 fprintf(stat_config.output, "CPU%*d%s",
780 perf_evsel__cpus(evsel)->map[id], csv_sep);
783 fprintf(stat_config.output, "%*s-%*d%s",
785 thread_map__comm(evsel->threads, id),
787 thread_map__pid(evsel->threads, id),
803 struct perf_evsel *evsel;
806 #define METRIC_LEN 35
808 static void new_line_std(void *ctx)
810 struct outstate *os = ctx;
815 static void do_new_line_std(struct outstate *os)
818 fputs(os->prefix, os->fh);
819 aggr_printout(os->evsel, os->id, os->nr);
820 if (stat_config.aggr_mode == AGGR_NONE)
821 fprintf(os->fh, " ");
822 fprintf(os->fh, " ");
825 static void print_metric_std(void *ctx, const char *color, const char *fmt,
826 const char *unit, double val)
828 struct outstate *os = ctx;
831 bool newline = os->newline;
835 if (unit == NULL || fmt == NULL) {
836 fprintf(out, "%-*s", METRIC_LEN, "");
843 n = fprintf(out, " # ");
845 n += color_fprintf(out, color, fmt, val);
847 n += fprintf(out, fmt, val);
848 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
851 static void new_line_csv(void *ctx)
853 struct outstate *os = ctx;
858 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
859 aggr_printout(os->evsel, os->id, os->nr);
860 for (i = 0; i < os->nfields; i++)
861 fputs(csv_sep, os->fh);
864 static void print_metric_csv(void *ctx,
865 const char *color __maybe_unused,
866 const char *fmt, const char *unit, double val)
868 struct outstate *os = ctx;
870 char buf[64], *vals, *ends;
872 if (unit == NULL || fmt == NULL) {
873 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
876 snprintf(buf, sizeof(buf), fmt, val);
878 while (isspace(*vals))
881 while (isdigit(*ends) || *ends == '.')
884 while (isspace(*unit))
886 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
889 #define METRIC_ONLY_LEN 20
891 /* Filter out some columns that don't work well in metrics only mode */
893 static bool valid_only_metric(const char *unit)
897 if (strstr(unit, "/sec") ||
898 strstr(unit, "hz") ||
899 strstr(unit, "Hz") ||
900 strstr(unit, "CPUs utilized"))
905 static const char *fixunit(char *buf, struct perf_evsel *evsel,
908 if (!strncmp(unit, "of all", 6)) {
909 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
916 static void print_metric_only(void *ctx, const char *color, const char *fmt,
917 const char *unit, double val)
919 struct outstate *os = ctx;
923 unsigned mlen = METRIC_ONLY_LEN;
925 if (!valid_only_metric(unit))
927 unit = fixunit(buf, os->evsel, unit);
929 n = color_fprintf(out, color, fmt, val);
931 n = fprintf(out, fmt, val);
932 if (n > METRIC_ONLY_LEN)
934 if (mlen < strlen(unit))
935 mlen = strlen(unit) + 1;
936 fprintf(out, "%*s", mlen - n, "");
939 static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
941 const char *unit, double val)
943 struct outstate *os = ctx;
945 char buf[64], *vals, *ends;
948 if (!valid_only_metric(unit))
950 unit = fixunit(tbuf, os->evsel, unit);
951 snprintf(buf, sizeof buf, fmt, val);
953 while (isspace(*vals))
956 while (isdigit(*ends) || *ends == '.')
959 fprintf(out, "%s%s", vals, csv_sep);
962 static void new_line_metric(void *ctx __maybe_unused)
966 static void print_metric_header(void *ctx, const char *color __maybe_unused,
967 const char *fmt __maybe_unused,
968 const char *unit, double val __maybe_unused)
970 struct outstate *os = ctx;
973 if (!valid_only_metric(unit))
975 unit = fixunit(tbuf, os->evsel, unit);
977 fprintf(os->fh, "%s%s", unit, csv_sep);
979 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
982 static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
984 FILE *output = stat_config.output;
985 double msecs = avg / NSEC_PER_MSEC;
986 const char *fmt_v, *fmt_n;
989 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
990 fmt_n = csv_output ? "%s" : "%-25s";
992 aggr_printout(evsel, id, nr);
994 scnprintf(name, sizeof(name), "%s%s",
995 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
997 fprintf(output, fmt_v, msecs, csv_sep);
1000 fprintf(output, "%s%s", evsel->unit, csv_sep);
1002 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1004 fprintf(output, fmt_n, name);
1007 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
1010 static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1017 if (stat_config.aggr_mode == AGGR_NONE)
1020 if (stat_config.aggr_mode == AGGR_GLOBAL)
1023 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1024 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1026 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1032 static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1034 FILE *output = stat_config.output;
1035 double sc = evsel->scale;
1039 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
1042 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
1044 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
1047 aggr_printout(evsel, id, nr);
1049 fprintf(output, fmt, avg, csv_sep);
1052 fprintf(output, "%-*s%s",
1053 csv_output ? 0 : unit_width,
1054 evsel->unit, csv_sep);
1056 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1059 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
1062 static void printout(int id, int nr, struct perf_evsel *counter, double uval,
1063 char *prefix, u64 run, u64 ena, double noise)
1065 struct perf_stat_output_ctx out;
1066 struct outstate os = {
1067 .fh = stat_config.output,
1068 .prefix = prefix ? prefix : "",
1073 print_metric_t pm = print_metric_std;
1077 nl = new_line_metric;
1079 pm = print_metric_only_csv;
1081 pm = print_metric_only;
1085 if (csv_output && !metric_only) {
1086 static int aggr_fields[] = {
1094 pm = print_metric_csv;
1097 os.nfields += aggr_fields[stat_config.aggr_mode];
1101 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
1103 pm(&os, NULL, "", "", 0);
1106 aggr_printout(counter, id, nr);
1108 fprintf(stat_config.output, "%*s%s",
1109 csv_output ? 0 : 18,
1110 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1113 if (counter->supported)
1114 print_free_counters_hint = 1;
1116 fprintf(stat_config.output, "%-*s%s",
1117 csv_output ? 0 : unit_width,
1118 counter->unit, csv_sep);
1120 fprintf(stat_config.output, "%*s",
1121 csv_output ? 0 : -25,
1122 perf_evsel__name(counter));
1125 fprintf(stat_config.output, "%s%s",
1126 csv_sep, counter->cgrp->name);
1129 pm(&os, NULL, NULL, "", 0);
1130 print_noise(counter, noise);
1131 print_running(run, ena);
1133 pm(&os, NULL, NULL, "", 0);
1139 else if (nsec_counter(counter))
1140 nsec_printout(id, nr, counter, uval);
1142 abs_printout(id, nr, counter, uval);
1144 out.print_metric = pm;
1148 if (csv_output && !metric_only) {
1149 print_noise(counter, noise);
1150 print_running(run, ena);
1153 perf_stat__print_shadow_stats(counter, uval,
1154 first_shadow_cpu(counter, id),
1156 if (!csv_output && !metric_only) {
1157 print_noise(counter, noise);
1158 print_running(run, ena);
1162 static void aggr_update_shadow(void)
1166 struct perf_evsel *counter;
1168 for (s = 0; s < aggr_map->nr; s++) {
1169 id = aggr_map->map[s];
1170 evlist__for_each_entry(evsel_list, counter) {
1172 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1173 s2 = aggr_get_id(evsel_list->cpus, cpu);
1176 val += perf_counts(counter->counts, cpu, 0)->val;
1178 val = val * counter->scale;
1179 perf_stat__update_shadow_stats(counter, &val,
1180 first_shadow_cpu(counter, id));
1185 static void collect_data(struct perf_evsel *counter,
1186 void (*cb)(struct perf_evsel *counter, void *data,
1190 cb(counter, data, true);
1200 static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1202 struct aggr_data *ad = data;
1205 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1206 struct perf_counts_values *counts;
1208 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1213 counts = perf_counts(counter->counts, cpu, 0);
1214 ad->val += counts->val;
1215 ad->ena += counts->ena;
1216 ad->run += counts->run;
1220 static void print_aggr(char *prefix)
1222 FILE *output = stat_config.output;
1223 struct perf_evsel *counter;
1229 if (!(aggr_map || aggr_get_id))
1232 aggr_update_shadow();
1235 * With metric_only everything is on a single line.
1236 * Without each counter has its own line.
1238 for (s = 0; s < aggr_map->nr; s++) {
1239 struct aggr_data ad;
1240 if (prefix && metric_only)
1241 fprintf(output, "%s", prefix);
1243 ad.id = id = aggr_map->map[s];
1245 evlist__for_each_entry(evsel_list, counter) {
1246 ad.val = ad.ena = ad.run = 0;
1248 collect_data(counter, aggr_cb, &ad);
1253 if (first && metric_only) {
1255 aggr_printout(counter, id, nr);
1257 if (prefix && !metric_only)
1258 fprintf(output, "%s", prefix);
1260 uval = val * counter->scale;
1261 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
1263 fputc('\n', output);
1266 fputc('\n', output);
1270 static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1272 FILE *output = stat_config.output;
1273 int nthreads = thread_map__nr(counter->threads);
1274 int ncpus = cpu_map__nr(counter->cpus);
1278 for (thread = 0; thread < nthreads; thread++) {
1279 u64 ena = 0, run = 0, val = 0;
1281 for (cpu = 0; cpu < ncpus; cpu++) {
1282 val += perf_counts(counter->counts, cpu, thread)->val;
1283 ena += perf_counts(counter->counts, cpu, thread)->ena;
1284 run += perf_counts(counter->counts, cpu, thread)->run;
1288 fprintf(output, "%s", prefix);
1290 uval = val * counter->scale;
1291 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
1292 fputc('\n', output);
1297 double avg, avg_enabled, avg_running;
1300 static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1301 bool first __maybe_unused)
1303 struct caggr_data *cd = data;
1304 struct perf_stat_evsel *ps = counter->priv;
1306 cd->avg += avg_stats(&ps->res_stats[0]);
1307 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1308 cd->avg_running += avg_stats(&ps->res_stats[2]);
1312 * Print out the results of a single counter:
1313 * aggregated counts in system-wide mode
1315 static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
1317 FILE *output = stat_config.output;
1319 struct caggr_data cd = { .avg = 0.0 };
1321 collect_data(counter, counter_aggr_cb, &cd);
1323 if (prefix && !metric_only)
1324 fprintf(output, "%s", prefix);
1326 uval = cd.avg * counter->scale;
1327 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, cd.avg);
1329 fprintf(output, "\n");
1332 static void counter_cb(struct perf_evsel *counter, void *data,
1333 bool first __maybe_unused)
1335 struct aggr_data *ad = data;
1337 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1338 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1339 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1343 * Print out the results of a single counter:
1344 * does not use aggregated count in system-wide
1346 static void print_counter(struct perf_evsel *counter, char *prefix)
1348 FILE *output = stat_config.output;
1353 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1354 struct aggr_data ad = { .cpu = cpu };
1356 collect_data(counter, counter_cb, &ad);
1362 fprintf(output, "%s", prefix);
1364 uval = val * counter->scale;
1365 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1367 fputc('\n', output);
1371 static void print_no_aggr_metric(char *prefix)
1375 struct perf_evsel *counter;
1379 nrcpus = evsel_list->cpus->nr;
1380 for (cpu = 0; cpu < nrcpus; cpu++) {
1384 fputs(prefix, stat_config.output);
1385 evlist__for_each_entry(evsel_list, counter) {
1387 aggr_printout(counter, cpu, 0);
1390 val = perf_counts(counter->counts, cpu, 0)->val;
1391 ena = perf_counts(counter->counts, cpu, 0)->ena;
1392 run = perf_counts(counter->counts, cpu, 0)->run;
1394 uval = val * counter->scale;
1395 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1397 fputc('\n', stat_config.output);
1401 static int aggr_header_lens[] = {
1409 static const char *aggr_header_csv[] = {
1410 [AGGR_CORE] = "core,cpus,",
1411 [AGGR_SOCKET] = "socket,cpus",
1412 [AGGR_NONE] = "cpu,",
1413 [AGGR_THREAD] = "comm-pid,",
1417 static void print_metric_headers(const char *prefix, bool no_indent)
1419 struct perf_stat_output_ctx out;
1420 struct perf_evsel *counter;
1421 struct outstate os = {
1422 .fh = stat_config.output
1426 fprintf(stat_config.output, "%s", prefix);
1428 if (!csv_output && !no_indent)
1429 fprintf(stat_config.output, "%*s",
1430 aggr_header_lens[stat_config.aggr_mode], "");
1432 if (stat_config.interval)
1433 fputs("time,", stat_config.output);
1434 fputs(aggr_header_csv[stat_config.aggr_mode],
1435 stat_config.output);
1438 /* Print metrics headers only */
1439 evlist__for_each_entry(evsel_list, counter) {
1442 out.print_metric = print_metric_header;
1443 out.new_line = new_line_metric;
1445 perf_stat__print_shadow_stats(counter, 0,
1449 fputc('\n', stat_config.output);
1452 static void print_interval(char *prefix, struct timespec *ts)
1454 FILE *output = stat_config.output;
1455 static int num_print_interval;
1457 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1459 if (num_print_interval == 0 && !csv_output) {
1460 switch (stat_config.aggr_mode) {
1462 fprintf(output, "# time socket cpus");
1464 fprintf(output, " counts %*s events\n", unit_width, "unit");
1467 fprintf(output, "# time core cpus");
1469 fprintf(output, " counts %*s events\n", unit_width, "unit");
1472 fprintf(output, "# time CPU");
1474 fprintf(output, " counts %*s events\n", unit_width, "unit");
1477 fprintf(output, "# time comm-pid");
1479 fprintf(output, " counts %*s events\n", unit_width, "unit");
1483 fprintf(output, "# time");
1485 fprintf(output, " counts %*s events\n", unit_width, "unit");
1491 if (num_print_interval == 0 && metric_only)
1492 print_metric_headers(" ", true);
1493 if (++num_print_interval == 25)
1494 num_print_interval = 0;
1497 static void print_header(int argc, const char **argv)
1499 FILE *output = stat_config.output;
1505 fprintf(output, "\n");
1506 fprintf(output, " Performance counter stats for ");
1507 if (target.system_wide)
1508 fprintf(output, "\'system wide");
1509 else if (target.cpu_list)
1510 fprintf(output, "\'CPU(s) %s", target.cpu_list);
1511 else if (!target__has_task(&target)) {
1512 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1513 for (i = 1; argv && (i < argc); i++)
1514 fprintf(output, " %s", argv[i]);
1515 } else if (target.pid)
1516 fprintf(output, "process id \'%s", target.pid);
1518 fprintf(output, "thread id \'%s", target.tid);
1520 fprintf(output, "\'");
1522 fprintf(output, " (%d runs)", run_count);
1523 fprintf(output, ":\n\n");
1527 static void print_footer(void)
1529 FILE *output = stat_config.output;
1532 fprintf(output, "\n");
1533 fprintf(output, " %17.9f seconds time elapsed",
1534 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
1535 if (run_count > 1) {
1536 fprintf(output, " ");
1537 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1538 avg_stats(&walltime_nsecs_stats));
1540 fprintf(output, "\n\n");
1542 if (print_free_counters_hint)
1544 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1545 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1547 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1550 static void print_counters(struct timespec *ts, int argc, const char **argv)
1552 int interval = stat_config.interval;
1553 struct perf_evsel *counter;
1554 char buf[64], *prefix = NULL;
1556 /* Do not print anything if we record to the pipe. */
1557 if (STAT_RECORD && perf_stat.file.is_pipe)
1561 print_interval(prefix = buf, ts);
1563 print_header(argc, argv);
1566 static int num_print_iv;
1568 if (num_print_iv == 0 && !interval)
1569 print_metric_headers(prefix, false);
1570 if (num_print_iv++ == 25)
1572 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1573 fprintf(stat_config.output, "%s", prefix);
1576 switch (stat_config.aggr_mode) {
1582 evlist__for_each_entry(evsel_list, counter)
1583 print_aggr_thread(counter, prefix);
1586 evlist__for_each_entry(evsel_list, counter)
1587 print_counter_aggr(counter, prefix);
1589 fputc('\n', stat_config.output);
1593 print_no_aggr_metric(prefix);
1595 evlist__for_each_entry(evsel_list, counter)
1596 print_counter(counter, prefix);
1604 if (!interval && !csv_output)
1607 fflush(stat_config.output);
1610 static volatile int signr = -1;
1612 static void skip_signal(int signo)
1614 if ((child_pid == -1) || stat_config.interval)
1619 * render child_pid harmless
1620 * won't send SIGTERM to a random
1621 * process in case of race condition
1622 * and fast PID recycling
1627 static void sig_atexit(void)
1632 * avoid race condition with SIGCHLD handler
1633 * in skip_signal() which is modifying child_pid
1634 * goal is to avoid send SIGTERM to a random
1638 sigaddset(&set, SIGCHLD);
1639 sigprocmask(SIG_BLOCK, &set, &oset);
1641 if (child_pid != -1)
1642 kill(child_pid, SIGTERM);
1644 sigprocmask(SIG_SETMASK, &oset, NULL);
1649 signal(signr, SIG_DFL);
1650 kill(getpid(), signr);
1653 static int stat__set_big_num(const struct option *opt __maybe_unused,
1654 const char *s __maybe_unused, int unset)
1656 big_num_opt = unset ? 0 : 1;
1660 static int enable_metric_only(const struct option *opt __maybe_unused,
1661 const char *s __maybe_unused, int unset)
1663 force_metric_only = true;
1664 metric_only = !unset;
1668 static const struct option stat_options[] = {
1669 OPT_BOOLEAN('T', "transaction", &transaction_run,
1670 "hardware transaction statistics"),
1671 OPT_CALLBACK('e', "event", &evsel_list, "event",
1672 "event selector. use 'perf list' to list available events",
1673 parse_events_option),
1674 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1675 "event filter", parse_filter),
1676 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1677 "child tasks do not inherit counters"),
1678 OPT_STRING('p', "pid", &target.pid, "pid",
1679 "stat events on existing process id"),
1680 OPT_STRING('t', "tid", &target.tid, "tid",
1681 "stat events on existing thread id"),
1682 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1683 "system-wide collection from all CPUs"),
1684 OPT_BOOLEAN('g', "group", &group,
1685 "put the counters into a counter group"),
1686 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1687 OPT_INCR('v', "verbose", &verbose,
1688 "be more verbose (show counter open errors, etc)"),
1689 OPT_INTEGER('r', "repeat", &run_count,
1690 "repeat command and print average + stddev (max: 100, forever: 0)"),
1691 OPT_BOOLEAN('n', "null", &null_run,
1692 "null run - dont start any counters"),
1693 OPT_INCR('d', "detailed", &detailed_run,
1694 "detailed run - start a lot of events"),
1695 OPT_BOOLEAN('S', "sync", &sync_run,
1696 "call sync() before starting a run"),
1697 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1698 "print large numbers with thousands\' separators",
1700 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1701 "list of cpus to monitor in system-wide"),
1702 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1703 "disable CPU count aggregation", AGGR_NONE),
1704 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1705 "print counts with custom separator"),
1706 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1707 "monitor event in cgroup name only", parse_cgroups),
1708 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1709 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1710 OPT_INTEGER(0, "log-fd", &output_fd,
1711 "log output to fd, instead of stderr"),
1712 OPT_STRING(0, "pre", &pre_cmd, "command",
1713 "command to run prior to the measured command"),
1714 OPT_STRING(0, "post", &post_cmd, "command",
1715 "command to run after to the measured command"),
1716 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1717 "print counts at regular interval in ms (>= 10)"),
1718 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1719 "aggregate counts per processor socket", AGGR_SOCKET),
1720 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1721 "aggregate counts per physical processor core", AGGR_CORE),
1722 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1723 "aggregate counts per thread", AGGR_THREAD),
1724 OPT_UINTEGER('D', "delay", &initial_delay,
1725 "ms to wait before starting measurement after program start"),
1726 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1727 "Only print computed metrics. No raw values", enable_metric_only),
1728 OPT_BOOLEAN(0, "topdown", &topdown_run,
1729 "measure topdown level 1 statistics"),
1733 static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1735 return cpu_map__get_socket(map, cpu, NULL);
1738 static int perf_stat__get_core(struct cpu_map *map, int cpu)
1740 return cpu_map__get_core(map, cpu, NULL);
1743 static int cpu_map__get_max(struct cpu_map *map)
1747 for (i = 0; i < map->nr; i++) {
1748 if (map->map[i] > max)
1755 static struct cpu_map *cpus_aggr_map;
1757 static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1764 cpu = map->map[idx];
1766 if (cpus_aggr_map->map[cpu] == -1)
1767 cpus_aggr_map->map[cpu] = get_id(map, idx);
1769 return cpus_aggr_map->map[cpu];
1772 static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1774 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1777 static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1779 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1782 static int perf_stat_init_aggr_mode(void)
1786 switch (stat_config.aggr_mode) {
1788 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1789 perror("cannot build socket map");
1792 aggr_get_id = perf_stat__get_socket_cached;
1795 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1796 perror("cannot build core map");
1799 aggr_get_id = perf_stat__get_core_cached;
1810 * The evsel_list->cpus is the base we operate on,
1811 * taking the highest cpu number to be the size of
1812 * the aggregation translate cpumap.
1814 nr = cpu_map__get_max(evsel_list->cpus);
1815 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1816 return cpus_aggr_map ? 0 : -ENOMEM;
1819 static void perf_stat__exit_aggr_mode(void)
1821 cpu_map__put(aggr_map);
1822 cpu_map__put(cpus_aggr_map);
1824 cpus_aggr_map = NULL;
1827 static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1834 cpu = map->map[idx];
1836 if (cpu >= env->nr_cpus_avail)
1842 static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1844 struct perf_env *env = data;
1845 int cpu = perf_env__get_cpu(env, map, idx);
1847 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1850 static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1852 struct perf_env *env = data;
1853 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1856 int socket_id = env->cpu[cpu].socket_id;
1859 * Encode socket in upper 16 bits
1860 * core_id is relative to socket, and
1861 * we need a global id. So we combine
1864 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1870 static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1871 struct cpu_map **sockp)
1873 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1876 static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1877 struct cpu_map **corep)
1879 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1882 static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1884 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1887 static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1889 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1892 static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1894 struct perf_env *env = &st->session->header.env;
1896 switch (stat_config.aggr_mode) {
1898 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1899 perror("cannot build socket map");
1902 aggr_get_id = perf_stat__get_socket_file;
1905 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1906 perror("cannot build core map");
1909 aggr_get_id = perf_stat__get_core_file;
1922 static int topdown_filter_events(const char **attr, char **str, bool use_group)
1929 for (i = 0; attr[i]; i++) {
1930 if (pmu_have_event("cpu", attr[i])) {
1931 len += strlen(attr[i]) + 1;
1932 attr[i - off] = attr[i];
1936 attr[i - off] = NULL;
1938 *str = malloc(len + 1 + 2);
1948 for (i = 0; attr[i]; i++) {
1961 __weak bool arch_topdown_check_group(bool *warn)
1967 __weak void arch_topdown_group_warn(void)
1972 * Add default attributes, if there were no attributes specified or
1973 * if -d/--detailed, -d -d or -d -d -d is used:
1975 static int add_default_attributes(void)
1978 struct perf_event_attr default_attrs0[] = {
1980 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1981 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1982 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1983 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1985 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
1987 struct perf_event_attr frontend_attrs[] = {
1988 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
1990 struct perf_event_attr backend_attrs[] = {
1991 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
1993 struct perf_event_attr default_attrs1[] = {
1994 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1995 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1996 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2001 * Detailed stats (-d), covering the L1 and last level data caches:
2003 struct perf_event_attr detailed_attrs[] = {
2005 { .type = PERF_TYPE_HW_CACHE,
2007 PERF_COUNT_HW_CACHE_L1D << 0 |
2008 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2009 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2011 { .type = PERF_TYPE_HW_CACHE,
2013 PERF_COUNT_HW_CACHE_L1D << 0 |
2014 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2015 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2017 { .type = PERF_TYPE_HW_CACHE,
2019 PERF_COUNT_HW_CACHE_LL << 0 |
2020 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2021 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2023 { .type = PERF_TYPE_HW_CACHE,
2025 PERF_COUNT_HW_CACHE_LL << 0 |
2026 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2027 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2031 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2033 struct perf_event_attr very_detailed_attrs[] = {
2035 { .type = PERF_TYPE_HW_CACHE,
2037 PERF_COUNT_HW_CACHE_L1I << 0 |
2038 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2039 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2041 { .type = PERF_TYPE_HW_CACHE,
2043 PERF_COUNT_HW_CACHE_L1I << 0 |
2044 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2045 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2047 { .type = PERF_TYPE_HW_CACHE,
2049 PERF_COUNT_HW_CACHE_DTLB << 0 |
2050 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2051 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2053 { .type = PERF_TYPE_HW_CACHE,
2055 PERF_COUNT_HW_CACHE_DTLB << 0 |
2056 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2057 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2059 { .type = PERF_TYPE_HW_CACHE,
2061 PERF_COUNT_HW_CACHE_ITLB << 0 |
2062 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2063 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2065 { .type = PERF_TYPE_HW_CACHE,
2067 PERF_COUNT_HW_CACHE_ITLB << 0 |
2068 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2069 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2074 * Very, very detailed stats (-d -d -d), adding prefetch events:
2076 struct perf_event_attr very_very_detailed_attrs[] = {
2078 { .type = PERF_TYPE_HW_CACHE,
2080 PERF_COUNT_HW_CACHE_L1D << 0 |
2081 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2082 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2084 { .type = PERF_TYPE_HW_CACHE,
2086 PERF_COUNT_HW_CACHE_L1D << 0 |
2087 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2088 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2091 /* Set attrs if no event is selected and !null_run: */
2095 if (transaction_run) {
2096 if (pmu_have_event("cpu", "cycles-ct") &&
2097 pmu_have_event("cpu", "el-start"))
2098 err = parse_events(evsel_list, transaction_attrs, NULL);
2100 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2102 fprintf(stderr, "Cannot set up transaction events\n");
2112 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2113 stat_config.aggr_mode != AGGR_CORE) {
2114 pr_err("top down event configuration requires --per-core mode\n");
2117 stat_config.aggr_mode = AGGR_CORE;
2118 if (nr_cgroups || !target__has_cpu(&target)) {
2119 pr_err("top down event configuration requires system-wide mode (-a)\n");
2123 if (!force_metric_only)
2125 if (topdown_filter_events(topdown_attrs, &str,
2126 arch_topdown_check_group(&warn)) < 0) {
2127 pr_err("Out of memory\n");
2130 if (topdown_attrs[0] && str) {
2132 arch_topdown_group_warn();
2133 err = parse_events(evsel_list, str, NULL);
2136 "Cannot set up top down events %s: %d\n",
2142 fprintf(stderr, "System does not support topdown\n");
2148 if (!evsel_list->nr_entries) {
2149 if (target__has_cpu(&target))
2150 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2152 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2154 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2155 if (perf_evlist__add_default_attrs(evsel_list,
2156 frontend_attrs) < 0)
2159 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2160 if (perf_evlist__add_default_attrs(evsel_list,
2164 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
2168 /* Detailed events get appended to the event list: */
2170 if (detailed_run < 1)
2173 /* Append detailed run extra attributes: */
2174 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
2177 if (detailed_run < 2)
2180 /* Append very detailed run extra attributes: */
2181 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
2184 if (detailed_run < 3)
2187 /* Append very, very detailed run extra attributes: */
2188 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
2191 static const char * const stat_record_usage[] = {
2192 "perf stat record [<options>]",
2196 static void init_features(struct perf_session *session)
2200 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2201 perf_header__set_feat(&session->header, feat);
2203 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2204 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2205 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2206 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2209 static int __cmd_record(int argc, const char **argv)
2211 struct perf_session *session;
2212 struct perf_data_file *file = &perf_stat.file;
2214 argc = parse_options(argc, argv, stat_options, stat_record_usage,
2215 PARSE_OPT_STOP_AT_NON_OPTION);
2218 file->path = output_name;
2220 if (run_count != 1 || forever) {
2221 pr_err("Cannot use -r option with perf stat record.\n");
2225 session = perf_session__new(file, false, NULL);
2226 if (session == NULL) {
2227 pr_err("Perf session creation failed.\n");
2231 init_features(session);
2233 session->evlist = evsel_list;
2234 perf_stat.session = session;
2235 perf_stat.record = true;
2239 static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2240 union perf_event *event,
2241 struct perf_session *session)
2243 struct stat_round_event *stat_round = &event->stat_round;
2244 struct perf_evsel *counter;
2245 struct timespec tsh, *ts = NULL;
2246 const char **argv = session->header.env.cmdline_argv;
2247 int argc = session->header.env.nr_cmdline;
2249 evlist__for_each_entry(evsel_list, counter)
2250 perf_stat_process_counter(&stat_config, counter);
2252 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2253 update_stats(&walltime_nsecs_stats, stat_round->time);
2255 if (stat_config.interval && stat_round->time) {
2256 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2257 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
2261 print_counters(ts, argc, argv);
2266 int process_stat_config_event(struct perf_tool *tool,
2267 union perf_event *event,
2268 struct perf_session *session __maybe_unused)
2270 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2272 perf_event__read_stat_config(&stat_config, &event->stat_config);
2274 if (cpu_map__empty(st->cpus)) {
2275 if (st->aggr_mode != AGGR_UNSET)
2276 pr_warning("warning: processing task data, aggregation mode not set\n");
2280 if (st->aggr_mode != AGGR_UNSET)
2281 stat_config.aggr_mode = st->aggr_mode;
2283 if (perf_stat.file.is_pipe)
2284 perf_stat_init_aggr_mode();
2286 perf_stat_init_aggr_mode_file(st);
2291 static int set_maps(struct perf_stat *st)
2293 if (!st->cpus || !st->threads)
2296 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2299 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2301 if (perf_evlist__alloc_stats(evsel_list, true))
2304 st->maps_allocated = true;
2309 int process_thread_map_event(struct perf_tool *tool,
2310 union perf_event *event,
2311 struct perf_session *session __maybe_unused)
2313 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2316 pr_warning("Extra thread map event, ignoring.\n");
2320 st->threads = thread_map__new_event(&event->thread_map);
2324 return set_maps(st);
2328 int process_cpu_map_event(struct perf_tool *tool,
2329 union perf_event *event,
2330 struct perf_session *session __maybe_unused)
2332 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2333 struct cpu_map *cpus;
2336 pr_warning("Extra cpu map event, ignoring.\n");
2340 cpus = cpu_map__new_data(&event->cpu_map.data);
2345 return set_maps(st);
2348 static const char * const stat_report_usage[] = {
2349 "perf stat report [<options>]",
2353 static struct perf_stat perf_stat = {
2355 .attr = perf_event__process_attr,
2356 .event_update = perf_event__process_event_update,
2357 .thread_map = process_thread_map_event,
2358 .cpu_map = process_cpu_map_event,
2359 .stat_config = process_stat_config_event,
2360 .stat = perf_event__process_stat_event,
2361 .stat_round = process_stat_round_event,
2363 .aggr_mode = AGGR_UNSET,
2366 static int __cmd_report(int argc, const char **argv)
2368 struct perf_session *session;
2369 const struct option options[] = {
2370 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2371 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2372 "aggregate counts per processor socket", AGGR_SOCKET),
2373 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2374 "aggregate counts per physical processor core", AGGR_CORE),
2375 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2376 "disable CPU count aggregation", AGGR_NONE),
2382 argc = parse_options(argc, argv, options, stat_report_usage, 0);
2384 if (!input_name || !strlen(input_name)) {
2385 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2388 input_name = "perf.data";
2391 perf_stat.file.path = input_name;
2392 perf_stat.file.mode = PERF_DATA_MODE_READ;
2394 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2395 if (session == NULL)
2398 perf_stat.session = session;
2399 stat_config.output = stderr;
2400 evsel_list = session->evlist;
2402 ret = perf_session__process_events(session);
2406 perf_session__delete(session);
2410 static void setup_system_wide(int forks)
2413 * Make system wide (-a) the default target if
2414 * no target was specified and one of following
2415 * conditions is met:
2417 * - there's no workload specified
2418 * - there is workload specified but all requested
2419 * events are system wide events
2421 if (!target__none(&target))
2425 target.system_wide = true;
2427 struct perf_evsel *counter;
2429 evlist__for_each_entry(evsel_list, counter) {
2430 if (!counter->system_wide)
2434 if (evsel_list->nr_entries)
2435 target.system_wide = true;
2439 int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
2441 const char * const stat_usage[] = {
2442 "perf stat [<options>] [<command>]",
2445 int status = -EINVAL, run_idx;
2447 FILE *output = stderr;
2448 unsigned int interval;
2449 const char * const stat_subcommands[] = { "record", "report" };
2451 setlocale(LC_ALL, "");
2453 evsel_list = perf_evlist__new();
2454 if (evsel_list == NULL)
2457 parse_events__shrink_config_terms();
2458 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2459 (const char **) stat_usage,
2460 PARSE_OPT_STOP_AT_NON_OPTION);
2461 perf_stat__init_shadow_stats();
2465 if (!strcmp(csv_sep, "\\t"))
2468 csv_sep = DEFAULT_SEPARATOR;
2470 if (argc && !strncmp(argv[0], "rec", 3)) {
2471 argc = __cmd_record(argc, argv);
2474 } else if (argc && !strncmp(argv[0], "rep", 3))
2475 return __cmd_report(argc, argv);
2477 interval = stat_config.interval;
2480 * For record command the -o is already taken care of.
2482 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
2485 if (output_name && output_fd) {
2486 fprintf(stderr, "cannot use both --output and --log-fd\n");
2487 parse_options_usage(stat_usage, stat_options, "o", 1);
2488 parse_options_usage(NULL, stat_options, "log-fd", 0);
2492 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2493 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2497 if (metric_only && run_count > 1) {
2498 fprintf(stderr, "--metric-only is not supported with -r\n");
2502 if (output_fd < 0) {
2503 fprintf(stderr, "argument to --log-fd must be a > 0\n");
2504 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
2510 mode = append_file ? "a" : "w";
2512 output = fopen(output_name, mode);
2514 perror("failed to create output file");
2517 clock_gettime(CLOCK_REALTIME, &tm);
2518 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
2519 } else if (output_fd > 0) {
2520 mode = append_file ? "a" : "w";
2521 output = fdopen(output_fd, mode);
2523 perror("Failed opening logfd");
2528 stat_config.output = output;
2531 * let the spreadsheet do the pretty-printing
2534 /* User explicitly passed -B? */
2535 if (big_num_opt == 1) {
2536 fprintf(stderr, "-B option not supported with -x\n");
2537 parse_options_usage(stat_usage, stat_options, "B", 1);
2538 parse_options_usage(NULL, stat_options, "x", 1);
2540 } else /* Nope, so disable big number formatting */
2542 } else if (big_num_opt == 0) /* User passed --no-big-num */
2545 setup_system_wide(argc);
2547 if (run_count < 0) {
2548 pr_err("Run count must be a positive number\n");
2549 parse_options_usage(stat_usage, stat_options, "r", 1);
2551 } else if (run_count == 0) {
2556 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
2557 fprintf(stderr, "The --per-thread option is only available "
2558 "when monitoring via -p -t options.\n");
2559 parse_options_usage(NULL, stat_options, "p", 1);
2560 parse_options_usage(NULL, stat_options, "t", 1);
2565 * no_aggr, cgroup are for system-wide only
2566 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2568 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2569 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
2570 !target__has_cpu(&target)) {
2571 fprintf(stderr, "both cgroup and no-aggregation "
2572 "modes only available in system-wide mode\n");
2574 parse_options_usage(stat_usage, stat_options, "G", 1);
2575 parse_options_usage(NULL, stat_options, "A", 1);
2576 parse_options_usage(NULL, stat_options, "a", 1);
2580 if (add_default_attributes())
2583 target__validate(&target);
2585 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
2586 if (target__has_task(&target)) {
2587 pr_err("Problems finding threads of monitor\n");
2588 parse_options_usage(stat_usage, stat_options, "p", 1);
2589 parse_options_usage(NULL, stat_options, "t", 1);
2590 } else if (target__has_cpu(&target)) {
2591 perror("failed to parse CPUs map");
2592 parse_options_usage(stat_usage, stat_options, "C", 1);
2593 parse_options_usage(NULL, stat_options, "a", 1);
2599 * Initialize thread_map with comm names,
2600 * so we could print it out on output.
2602 if (stat_config.aggr_mode == AGGR_THREAD)
2603 thread_map__read_comms(evsel_list->threads);
2605 if (interval && interval < 100) {
2606 if (interval < 10) {
2607 pr_err("print interval must be >= 10ms\n");
2608 parse_options_usage(stat_usage, stat_options, "I", 1);
2611 pr_warning("print interval < 100ms. "
2612 "The overhead percentage could be high in some cases. "
2613 "Please proceed with caution.\n");
2616 if (perf_evlist__alloc_stats(evsel_list, interval))
2619 if (perf_stat_init_aggr_mode())
2623 * We dont want to block the signals - that would cause
2624 * child tasks to inherit that and Ctrl-C would not work.
2625 * What we want is for Ctrl-C to work in the exec()-ed
2626 * task, but being ignored by perf stat itself:
2630 signal(SIGINT, skip_signal);
2631 signal(SIGCHLD, skip_signal);
2632 signal(SIGALRM, skip_signal);
2633 signal(SIGABRT, skip_signal);
2636 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
2637 if (run_count != 1 && verbose > 0)
2638 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2641 status = run_perf_stat(argc, argv);
2642 if (forever && status != -1) {
2643 print_counters(NULL, argc, argv);
2644 perf_stat__reset_stats();
2648 if (!forever && status != -1 && !interval)
2649 print_counters(NULL, argc, argv);
2653 * We synthesize the kernel mmap record just so that older tools
2654 * don't emit warnings about not being able to resolve symbols
2655 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2656 * a saner message about no samples being in the perf.data file.
2658 * This also serves to suppress a warning about f_header.data.size == 0
2659 * in header.c at the moment 'perf stat record' gets introduced, which
2660 * is not really needed once we start adding the stat specific PERF_RECORD_
2661 * records, but the need to suppress the kptr_restrict messages in older
2662 * tools remain -acme
2664 int fd = perf_data_file__fd(&perf_stat.file);
2665 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2666 process_synthesized_event,
2667 &perf_stat.session->machines.host);
2669 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2670 "older tools may produce warnings about this file\n.");
2674 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2675 pr_err("failed to write stat round event\n");
2678 if (!perf_stat.file.is_pipe) {
2679 perf_stat.session->header.data_size += perf_stat.bytes_written;
2680 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2683 perf_session__delete(perf_stat.session);
2686 perf_stat__exit_aggr_mode();
2687 perf_evlist__free_stats(evsel_list);
2689 perf_evlist__delete(evsel_list);