4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
9 $ perf stat ./hackbench 10
13 Performance counter stats for './hackbench 10':
15 1708.761321 task-clock # 11.037 CPUs utilized
16 41,190 context-switches # 0.024 M/sec
17 6,735 CPU-migrations # 0.004 M/sec
18 17,318 page-faults # 0.010 M/sec
19 5,205,202,243 cycles # 3.046 GHz
20 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
21 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
22 2,603,501,247 instructions # 0.50 insns per cycle
23 # 1.48 stalled cycles per insn
24 484,357,498 branches # 283.455 M/sec
25 6,388,934 branch-misses # 1.32% of all branches
27 0.154822978 seconds time elapsed
30 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
32 * Improvements and fixes by:
34 * Arjan van de Ven <arjan@linux.intel.com>
35 * Yanmin Zhang <yanmin.zhang@intel.com>
36 * Wu Fengguang <fengguang.wu@intel.com>
37 * Mike Galbraith <efault@gmx.de>
38 * Paul Mackerras <paulus@samba.org>
39 * Jaswinder Singh Rajput <jaswinder@kernel.org>
41 * Released under the GPL v2. (and only v2, not any later version)
46 #include "util/cgroup.h"
47 #include "util/util.h"
48 #include <subcmd/parse-options.h>
49 #include "util/parse-events.h"
51 #include "util/event.h"
52 #include "util/evlist.h"
53 #include "util/evsel.h"
54 #include "util/debug.h"
55 #include "util/drv_configs.h"
56 #include "util/color.h"
57 #include "util/stat.h"
58 #include "util/header.h"
59 #include "util/cpumap.h"
60 #include "util/thread.h"
61 #include "util/thread_map.h"
62 #include "util/counts.h"
63 #include "util/group.h"
64 #include "util/session.h"
65 #include "util/tool.h"
66 #include "util/group.h"
67 #include "util/string2.h"
70 #include <linux/time64.h>
71 #include <api/fs/fs.h>
75 #include <sys/prctl.h>
79 #include <sys/types.h>
84 #include "sane_ctype.h"
86 #define DEFAULT_SEPARATOR " "
87 #define CNTR_NOT_SUPPORTED "<not supported>"
88 #define CNTR_NOT_COUNTED "<not counted>"
89 #define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
91 static void print_counters(struct timespec *ts, int argc, const char **argv);
93 /* Default events used for perf stat -T */
94 static const char *transaction_attrs = {
106 /* More limited version when the CPU does not have all events. */
107 static const char * transaction_limited_attrs = {
117 static const char * topdown_attrs[] = {
118 "topdown-total-slots",
119 "topdown-slots-retired",
120 "topdown-recovery-bubbles",
121 "topdown-fetch-bubbles",
122 "topdown-slots-issued",
126 static const char *smi_cost_attrs = {
134 static struct perf_evlist *evsel_list;
136 static struct target target = {
140 typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
142 static int run_count = 1;
143 static bool no_inherit = false;
144 static volatile pid_t child_pid = -1;
145 static bool null_run = false;
146 static int detailed_run = 0;
147 static bool transaction_run;
148 static bool topdown_run = false;
149 static bool smi_cost = false;
150 static bool smi_reset = false;
151 static bool big_num = true;
152 static int big_num_opt = -1;
153 static const char *csv_sep = NULL;
154 static bool csv_output = false;
155 static bool group = false;
156 static const char *pre_cmd = NULL;
157 static const char *post_cmd = NULL;
158 static bool sync_run = false;
159 static unsigned int initial_delay = 0;
160 static unsigned int unit_width = 4; /* strlen("unit") */
161 static bool forever = false;
162 static bool metric_only = false;
163 static bool force_metric_only = false;
164 static bool no_merge = false;
165 static struct timespec ref_time;
166 static struct cpu_map *aggr_map;
167 static aggr_get_id_t aggr_get_id;
168 static bool append_file;
169 static const char *output_name;
170 static int output_fd;
171 static int print_free_counters_hint;
175 struct perf_data_file file;
176 struct perf_session *session;
178 struct perf_tool tool;
180 struct cpu_map *cpus;
181 struct thread_map *threads;
182 enum aggr_mode aggr_mode;
185 static struct perf_stat perf_stat;
186 #define STAT_RECORD perf_stat.record
188 static volatile int done = 0;
190 static struct perf_stat_config stat_config = {
191 .aggr_mode = AGGR_GLOBAL,
195 static inline void diff_timespec(struct timespec *r, struct timespec *a,
198 r->tv_sec = a->tv_sec - b->tv_sec;
199 if (a->tv_nsec < b->tv_nsec) {
200 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
203 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
207 static void perf_stat__reset_stats(void)
209 perf_evlist__reset_stats(evsel_list);
210 perf_stat__reset_shadow_stats();
213 static int create_perf_stat_counter(struct perf_evsel *evsel)
215 struct perf_event_attr *attr = &evsel->attr;
217 if (stat_config.scale)
218 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
219 PERF_FORMAT_TOTAL_TIME_RUNNING;
221 attr->inherit = !no_inherit;
224 * Some events get initialized with sample_(period/type) set,
225 * like tracepoints. Clear it up for counting.
227 attr->sample_period = 0;
230 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
231 * while avoiding that older tools show confusing messages.
233 * However for pipe sessions we need to keep it zero,
234 * because script's perf_evsel__check_attr is triggered
235 * by attr->sample_type != 0, and we can't run it on
238 if (!(STAT_RECORD && perf_stat.file.is_pipe))
239 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
242 * Disabling all counters initially, they will be enabled
243 * either manually by us or by kernel via enable_on_exec
246 if (perf_evsel__is_group_leader(evsel)) {
250 * In case of initial_delay we enable tracee
253 if (target__none(&target) && !initial_delay)
254 attr->enable_on_exec = 1;
257 if (target__has_cpu(&target))
258 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
260 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
264 * Does the counter have nsecs as a unit?
266 static inline int nsec_counter(struct perf_evsel *evsel)
268 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
269 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
275 static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
276 union perf_event *event,
277 struct perf_sample *sample __maybe_unused,
278 struct machine *machine __maybe_unused)
280 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
281 pr_err("failed to write perf data, error: %m\n");
285 perf_stat.bytes_written += event->header.size;
289 static int write_stat_round_event(u64 tm, u64 type)
291 return perf_event__synthesize_stat_round(NULL, tm, type,
292 process_synthesized_event,
296 #define WRITE_STAT_ROUND_EVENT(time, interval) \
297 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
299 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
302 perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
303 struct perf_counts_values *count)
305 struct perf_sample_id *sid = SID(counter, cpu, thread);
307 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
308 process_synthesized_event, NULL);
312 * Read out the results of a single counter:
313 * do not aggregate counts across CPUs in system-wide mode
315 static int read_counter(struct perf_evsel *counter)
317 int nthreads = thread_map__nr(evsel_list->threads);
318 int ncpus, cpu, thread;
320 if (target__has_cpu(&target))
321 ncpus = perf_evsel__nr_cpus(counter);
325 if (!counter->supported)
328 if (counter->system_wide)
331 for (thread = 0; thread < nthreads; thread++) {
332 for (cpu = 0; cpu < ncpus; cpu++) {
333 struct perf_counts_values *count;
335 count = perf_counts(counter->counts, cpu, thread);
336 if (perf_evsel__read(counter, cpu, thread, count)) {
337 counter->counts->scaled = -1;
338 perf_counts(counter->counts, cpu, thread)->ena = 0;
339 perf_counts(counter->counts, cpu, thread)->run = 0;
344 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
345 pr_err("failed to write stat event\n");
351 fprintf(stat_config.output,
352 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
353 perf_evsel__name(counter),
355 count->val, count->ena, count->run);
363 static void read_counters(void)
365 struct perf_evsel *counter;
368 evlist__for_each_entry(evsel_list, counter) {
369 ret = read_counter(counter);
371 pr_debug("failed to read counter %s\n", counter->name);
373 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
374 pr_warning("failed to process counter %s\n", counter->name);
378 static void process_interval(void)
380 struct timespec ts, rs;
384 clock_gettime(CLOCK_MONOTONIC, &ts);
385 diff_timespec(&rs, &ts, &ref_time);
388 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
389 pr_err("failed to write stat round event\n");
392 print_counters(&rs, 0, NULL);
395 static void enable_counters(void)
398 usleep(initial_delay * USEC_PER_MSEC);
401 * We need to enable counters only if:
402 * - we don't have tracee (attaching to task or cpu)
403 * - we have initial delay configured
405 if (!target__none(&target) || initial_delay)
406 perf_evlist__enable(evsel_list);
409 static void disable_counters(void)
412 * If we don't have tracee (attaching to task or cpu), counters may
413 * still be running. To get accurate group ratios, we must stop groups
414 * from counting before reading their constituent counters.
416 if (!target__none(&target))
417 perf_evlist__disable(evsel_list);
420 static volatile int workload_exec_errno;
423 * perf_evlist__prepare_workload will send a SIGUSR1
424 * if the fork fails, since we asked by setting its
425 * want_signal to true.
427 static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
428 void *ucontext __maybe_unused)
430 workload_exec_errno = info->si_value.sival_int;
433 static bool has_unit(struct perf_evsel *counter)
435 return counter->unit && *counter->unit;
438 static bool has_scale(struct perf_evsel *counter)
440 return counter->scale != 1;
443 static int perf_stat_synthesize_config(bool is_pipe)
445 struct perf_evsel *counter;
449 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
450 process_synthesized_event);
452 pr_err("Couldn't synthesize attrs.\n");
458 * Synthesize other events stuff not carried within
459 * attr event - unit, scale, name
461 evlist__for_each_entry(evsel_list, counter) {
462 if (!counter->supported)
466 * Synthesize unit and scale only if it's defined.
468 if (has_unit(counter)) {
469 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
471 pr_err("Couldn't synthesize evsel unit.\n");
476 if (has_scale(counter)) {
477 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
479 pr_err("Couldn't synthesize evsel scale.\n");
484 if (counter->own_cpus) {
485 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
487 pr_err("Couldn't synthesize evsel scale.\n");
493 * Name is needed only for pipe output,
494 * perf.data carries event names.
497 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
499 pr_err("Couldn't synthesize evsel name.\n");
505 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
506 process_synthesized_event,
509 pr_err("Couldn't synthesize thread map.\n");
513 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
514 process_synthesized_event, NULL);
516 pr_err("Couldn't synthesize thread map.\n");
520 err = perf_event__synthesize_stat_config(NULL, &stat_config,
521 process_synthesized_event, NULL);
523 pr_err("Couldn't synthesize config.\n");
530 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
532 static int __store_counter_ids(struct perf_evsel *counter,
533 struct cpu_map *cpus,
534 struct thread_map *threads)
538 for (cpu = 0; cpu < cpus->nr; cpu++) {
539 for (thread = 0; thread < threads->nr; thread++) {
540 int fd = FD(counter, cpu, thread);
542 if (perf_evlist__id_add_fd(evsel_list, counter,
543 cpu, thread, fd) < 0)
551 static int store_counter_ids(struct perf_evsel *counter)
553 struct cpu_map *cpus = counter->cpus;
554 struct thread_map *threads = counter->threads;
556 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
559 return __store_counter_ids(counter, cpus, threads);
562 static int __run_perf_stat(int argc, const char **argv)
564 int interval = stat_config.interval;
566 unsigned long long t0, t1;
567 struct perf_evsel *counter;
571 const bool forks = (argc > 0);
572 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
573 struct perf_evsel_config_term *err_term;
576 ts.tv_sec = interval / USEC_PER_MSEC;
577 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
584 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
585 workload_exec_failed_signal) < 0) {
586 perror("failed to prepare workload");
589 child_pid = evsel_list->workload.pid;
593 perf_evlist__set_leader(evsel_list);
595 evlist__for_each_entry(evsel_list, counter) {
597 if (create_perf_stat_counter(counter) < 0) {
599 * PPC returns ENXIO for HW counters until 2.6.37
600 * (behavior changed with commit b0a873e).
602 if (errno == EINVAL || errno == ENOSYS ||
603 errno == ENOENT || errno == EOPNOTSUPP ||
606 ui__warning("%s event is not supported by the kernel.\n",
607 perf_evsel__name(counter));
608 counter->supported = false;
610 if ((counter->leader != counter) ||
611 !(counter->leader->nr_members > 1))
613 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
615 ui__warning("%s\n", msg);
619 perf_evsel__open_strerror(counter, &target,
620 errno, msg, sizeof(msg));
621 ui__error("%s\n", msg);
624 kill(child_pid, SIGTERM);
628 counter->supported = true;
630 l = strlen(counter->unit);
634 if (STAT_RECORD && store_counter_ids(counter))
638 if (perf_evlist__apply_filters(evsel_list, &counter)) {
639 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
640 counter->filter, perf_evsel__name(counter), errno,
641 str_error_r(errno, msg, sizeof(msg)));
645 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
646 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
647 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
648 str_error_r(errno, msg, sizeof(msg)));
653 int err, fd = perf_data_file__fd(&perf_stat.file);
656 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
658 err = perf_session__write_header(perf_stat.session, evsel_list,
665 err = perf_stat_synthesize_config(is_pipe);
671 * Enable counters and exec the command:
674 clock_gettime(CLOCK_MONOTONIC, &ref_time);
677 perf_evlist__start_workload(evsel_list);
681 while (!waitpid(child_pid, &status, WNOHANG)) {
682 nanosleep(&ts, NULL);
688 if (workload_exec_errno) {
689 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
690 pr_err("Workload failed: %s\n", emsg);
694 if (WIFSIGNALED(status))
695 psignal(WTERMSIG(status), argv[0]);
699 nanosleep(&ts, NULL);
709 update_stats(&walltime_nsecs_stats, t1 - t0);
712 * Closing a group leader splits the group, and as we only disable
713 * group leaders, results in remaining events becoming enabled. To
714 * avoid arbitrary skew, we must read all counters before closing any
718 perf_evlist__close(evsel_list);
720 return WEXITSTATUS(status);
723 static int run_perf_stat(int argc, const char **argv)
728 ret = system(pre_cmd);
736 ret = __run_perf_stat(argc, argv);
741 ret = system(post_cmd);
749 static void print_running(u64 run, u64 ena)
752 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
756 ena ? 100.0 * run / ena : 100.0);
757 } else if (run != ena) {
758 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
762 static void print_noise_pct(double total, double avg)
764 double pct = rel_stddev_stats(total, avg);
767 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
769 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
772 static void print_noise(struct perf_evsel *evsel, double avg)
774 struct perf_stat_evsel *ps;
780 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
783 static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
785 switch (stat_config.aggr_mode) {
787 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
788 cpu_map__id_to_socket(id),
790 cpu_map__id_to_cpu(id),
797 fprintf(stat_config.output, "S%*d%s%*d%s",
806 fprintf(stat_config.output, "CPU%*d%s",
808 perf_evsel__cpus(evsel)->map[id], csv_sep);
811 fprintf(stat_config.output, "%*s-%*d%s",
813 thread_map__comm(evsel->threads, id),
815 thread_map__pid(evsel->threads, id),
831 struct perf_evsel *evsel;
834 #define METRIC_LEN 35
836 static void new_line_std(void *ctx)
838 struct outstate *os = ctx;
843 static void do_new_line_std(struct outstate *os)
846 fputs(os->prefix, os->fh);
847 aggr_printout(os->evsel, os->id, os->nr);
848 if (stat_config.aggr_mode == AGGR_NONE)
849 fprintf(os->fh, " ");
850 fprintf(os->fh, " ");
853 static void print_metric_std(void *ctx, const char *color, const char *fmt,
854 const char *unit, double val)
856 struct outstate *os = ctx;
859 bool newline = os->newline;
863 if (unit == NULL || fmt == NULL) {
864 fprintf(out, "%-*s", METRIC_LEN, "");
871 n = fprintf(out, " # ");
873 n += color_fprintf(out, color, fmt, val);
875 n += fprintf(out, fmt, val);
876 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
879 static void new_line_csv(void *ctx)
881 struct outstate *os = ctx;
886 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
887 aggr_printout(os->evsel, os->id, os->nr);
888 for (i = 0; i < os->nfields; i++)
889 fputs(csv_sep, os->fh);
892 static void print_metric_csv(void *ctx,
893 const char *color __maybe_unused,
894 const char *fmt, const char *unit, double val)
896 struct outstate *os = ctx;
898 char buf[64], *vals, *ends;
900 if (unit == NULL || fmt == NULL) {
901 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
904 snprintf(buf, sizeof(buf), fmt, val);
905 ends = vals = ltrim(buf);
906 while (isdigit(*ends) || *ends == '.')
909 while (isspace(*unit))
911 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
914 #define METRIC_ONLY_LEN 20
916 /* Filter out some columns that don't work well in metrics only mode */
918 static bool valid_only_metric(const char *unit)
922 if (strstr(unit, "/sec") ||
923 strstr(unit, "hz") ||
924 strstr(unit, "Hz") ||
925 strstr(unit, "CPUs utilized"))
930 static const char *fixunit(char *buf, struct perf_evsel *evsel,
933 if (!strncmp(unit, "of all", 6)) {
934 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
941 static void print_metric_only(void *ctx, const char *color, const char *fmt,
942 const char *unit, double val)
944 struct outstate *os = ctx;
948 unsigned mlen = METRIC_ONLY_LEN;
950 if (!valid_only_metric(unit))
952 unit = fixunit(buf, os->evsel, unit);
954 n = color_fprintf(out, color, fmt, val);
956 n = fprintf(out, fmt, val);
957 if (n > METRIC_ONLY_LEN)
959 if (mlen < strlen(unit))
960 mlen = strlen(unit) + 1;
961 fprintf(out, "%*s", mlen - n, "");
964 static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
966 const char *unit, double val)
968 struct outstate *os = ctx;
970 char buf[64], *vals, *ends;
973 if (!valid_only_metric(unit))
975 unit = fixunit(tbuf, os->evsel, unit);
976 snprintf(buf, sizeof buf, fmt, val);
977 ends = vals = ltrim(buf);
978 while (isdigit(*ends) || *ends == '.')
981 fprintf(out, "%s%s", vals, csv_sep);
984 static void new_line_metric(void *ctx __maybe_unused)
988 static void print_metric_header(void *ctx, const char *color __maybe_unused,
989 const char *fmt __maybe_unused,
990 const char *unit, double val __maybe_unused)
992 struct outstate *os = ctx;
995 if (!valid_only_metric(unit))
997 unit = fixunit(tbuf, os->evsel, unit);
999 fprintf(os->fh, "%s%s", unit, csv_sep);
1001 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
1004 static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1006 FILE *output = stat_config.output;
1007 double msecs = avg / NSEC_PER_MSEC;
1008 const char *fmt_v, *fmt_n;
1011 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
1012 fmt_n = csv_output ? "%s" : "%-25s";
1014 aggr_printout(evsel, id, nr);
1016 scnprintf(name, sizeof(name), "%s%s",
1017 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
1019 fprintf(output, fmt_v, msecs, csv_sep);
1022 fprintf(output, "%s%s", evsel->unit, csv_sep);
1024 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1026 fprintf(output, fmt_n, name);
1029 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
1032 static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1039 if (stat_config.aggr_mode == AGGR_NONE)
1042 if (stat_config.aggr_mode == AGGR_GLOBAL)
1045 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1046 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1048 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1054 static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1056 FILE *output = stat_config.output;
1057 double sc = evsel->scale;
1061 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
1064 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
1066 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
1069 aggr_printout(evsel, id, nr);
1071 fprintf(output, fmt, avg, csv_sep);
1074 fprintf(output, "%-*s%s",
1075 csv_output ? 0 : unit_width,
1076 evsel->unit, csv_sep);
1078 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1081 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
1084 static void printout(int id, int nr, struct perf_evsel *counter, double uval,
1085 char *prefix, u64 run, u64 ena, double noise)
1087 struct perf_stat_output_ctx out;
1088 struct outstate os = {
1089 .fh = stat_config.output,
1090 .prefix = prefix ? prefix : "",
1095 print_metric_t pm = print_metric_std;
1099 nl = new_line_metric;
1101 pm = print_metric_only_csv;
1103 pm = print_metric_only;
1107 if (csv_output && !metric_only) {
1108 static int aggr_fields[] = {
1116 pm = print_metric_csv;
1119 os.nfields += aggr_fields[stat_config.aggr_mode];
1123 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
1125 pm(&os, NULL, "", "", 0);
1128 aggr_printout(counter, id, nr);
1130 fprintf(stat_config.output, "%*s%s",
1131 csv_output ? 0 : 18,
1132 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1135 if (counter->supported)
1136 print_free_counters_hint = 1;
1138 fprintf(stat_config.output, "%-*s%s",
1139 csv_output ? 0 : unit_width,
1140 counter->unit, csv_sep);
1142 fprintf(stat_config.output, "%*s",
1143 csv_output ? 0 : -25,
1144 perf_evsel__name(counter));
1147 fprintf(stat_config.output, "%s%s",
1148 csv_sep, counter->cgrp->name);
1151 pm(&os, NULL, NULL, "", 0);
1152 print_noise(counter, noise);
1153 print_running(run, ena);
1155 pm(&os, NULL, NULL, "", 0);
1161 else if (nsec_counter(counter))
1162 nsec_printout(id, nr, counter, uval);
1164 abs_printout(id, nr, counter, uval);
1166 out.print_metric = pm;
1169 out.force_header = false;
1171 if (csv_output && !metric_only) {
1172 print_noise(counter, noise);
1173 print_running(run, ena);
1176 perf_stat__print_shadow_stats(counter, uval,
1177 first_shadow_cpu(counter, id),
1179 if (!csv_output && !metric_only) {
1180 print_noise(counter, noise);
1181 print_running(run, ena);
1185 static void aggr_update_shadow(void)
1189 struct perf_evsel *counter;
1191 for (s = 0; s < aggr_map->nr; s++) {
1192 id = aggr_map->map[s];
1193 evlist__for_each_entry(evsel_list, counter) {
1195 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1196 s2 = aggr_get_id(evsel_list->cpus, cpu);
1199 val += perf_counts(counter->counts, cpu, 0)->val;
1201 val = val * counter->scale;
1202 perf_stat__update_shadow_stats(counter, &val,
1203 first_shadow_cpu(counter, id));
1208 static void collect_all_aliases(struct perf_evsel *counter,
1209 void (*cb)(struct perf_evsel *counter, void *data,
1213 struct perf_evsel *alias;
1215 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1216 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1217 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1218 alias->scale != counter->scale ||
1219 alias->cgrp != counter->cgrp ||
1220 strcmp(alias->unit, counter->unit) ||
1221 nsec_counter(alias) != nsec_counter(counter))
1223 alias->merged_stat = true;
1224 cb(alias, data, false);
1228 static bool collect_data(struct perf_evsel *counter,
1229 void (*cb)(struct perf_evsel *counter, void *data,
1233 if (counter->merged_stat)
1235 cb(counter, data, true);
1237 collect_all_aliases(counter, cb, data);
1248 static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1250 struct aggr_data *ad = data;
1253 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1254 struct perf_counts_values *counts;
1256 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1261 counts = perf_counts(counter->counts, cpu, 0);
1263 * When any result is bad, make them all to give
1264 * consistent output in interval mode.
1266 if (counts->ena == 0 || counts->run == 0 ||
1267 counter->counts->scaled == -1) {
1272 ad->val += counts->val;
1273 ad->ena += counts->ena;
1274 ad->run += counts->run;
1278 static void print_aggr(char *prefix)
1280 FILE *output = stat_config.output;
1281 struct perf_evsel *counter;
1287 if (!(aggr_map || aggr_get_id))
1290 aggr_update_shadow();
1293 * With metric_only everything is on a single line.
1294 * Without each counter has its own line.
1296 for (s = 0; s < aggr_map->nr; s++) {
1297 struct aggr_data ad;
1298 if (prefix && metric_only)
1299 fprintf(output, "%s", prefix);
1301 ad.id = id = aggr_map->map[s];
1303 evlist__for_each_entry(evsel_list, counter) {
1304 ad.val = ad.ena = ad.run = 0;
1306 if (!collect_data(counter, aggr_cb, &ad))
1312 if (first && metric_only) {
1314 aggr_printout(counter, id, nr);
1316 if (prefix && !metric_only)
1317 fprintf(output, "%s", prefix);
1319 uval = val * counter->scale;
1320 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
1322 fputc('\n', output);
1325 fputc('\n', output);
1329 static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1331 FILE *output = stat_config.output;
1332 int nthreads = thread_map__nr(counter->threads);
1333 int ncpus = cpu_map__nr(counter->cpus);
1337 for (thread = 0; thread < nthreads; thread++) {
1338 u64 ena = 0, run = 0, val = 0;
1340 for (cpu = 0; cpu < ncpus; cpu++) {
1341 val += perf_counts(counter->counts, cpu, thread)->val;
1342 ena += perf_counts(counter->counts, cpu, thread)->ena;
1343 run += perf_counts(counter->counts, cpu, thread)->run;
1347 fprintf(output, "%s", prefix);
1349 uval = val * counter->scale;
1350 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
1351 fputc('\n', output);
1356 double avg, avg_enabled, avg_running;
1359 static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1360 bool first __maybe_unused)
1362 struct caggr_data *cd = data;
1363 struct perf_stat_evsel *ps = counter->priv;
1365 cd->avg += avg_stats(&ps->res_stats[0]);
1366 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1367 cd->avg_running += avg_stats(&ps->res_stats[2]);
1371 * Print out the results of a single counter:
1372 * aggregated counts in system-wide mode
1374 static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
1376 FILE *output = stat_config.output;
1378 struct caggr_data cd = { .avg = 0.0 };
1380 if (!collect_data(counter, counter_aggr_cb, &cd))
1383 if (prefix && !metric_only)
1384 fprintf(output, "%s", prefix);
1386 uval = cd.avg * counter->scale;
1387 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, cd.avg);
1389 fprintf(output, "\n");
1392 static void counter_cb(struct perf_evsel *counter, void *data,
1393 bool first __maybe_unused)
1395 struct aggr_data *ad = data;
1397 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1398 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1399 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1403 * Print out the results of a single counter:
1404 * does not use aggregated count in system-wide
1406 static void print_counter(struct perf_evsel *counter, char *prefix)
1408 FILE *output = stat_config.output;
1413 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1414 struct aggr_data ad = { .cpu = cpu };
1416 if (!collect_data(counter, counter_cb, &ad))
1423 fprintf(output, "%s", prefix);
1425 uval = val * counter->scale;
1426 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1428 fputc('\n', output);
1432 static void print_no_aggr_metric(char *prefix)
1436 struct perf_evsel *counter;
1440 nrcpus = evsel_list->cpus->nr;
1441 for (cpu = 0; cpu < nrcpus; cpu++) {
1445 fputs(prefix, stat_config.output);
1446 evlist__for_each_entry(evsel_list, counter) {
1448 aggr_printout(counter, cpu, 0);
1451 val = perf_counts(counter->counts, cpu, 0)->val;
1452 ena = perf_counts(counter->counts, cpu, 0)->ena;
1453 run = perf_counts(counter->counts, cpu, 0)->run;
1455 uval = val * counter->scale;
1456 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1458 fputc('\n', stat_config.output);
1462 static int aggr_header_lens[] = {
1470 static const char *aggr_header_csv[] = {
1471 [AGGR_CORE] = "core,cpus,",
1472 [AGGR_SOCKET] = "socket,cpus",
1473 [AGGR_NONE] = "cpu,",
1474 [AGGR_THREAD] = "comm-pid,",
1478 static void print_metric_headers(const char *prefix, bool no_indent)
1480 struct perf_stat_output_ctx out;
1481 struct perf_evsel *counter;
1482 struct outstate os = {
1483 .fh = stat_config.output
1487 fprintf(stat_config.output, "%s", prefix);
1489 if (!csv_output && !no_indent)
1490 fprintf(stat_config.output, "%*s",
1491 aggr_header_lens[stat_config.aggr_mode], "");
1493 if (stat_config.interval)
1494 fputs("time,", stat_config.output);
1495 fputs(aggr_header_csv[stat_config.aggr_mode],
1496 stat_config.output);
1499 /* Print metrics headers only */
1500 evlist__for_each_entry(evsel_list, counter) {
1503 out.print_metric = print_metric_header;
1504 out.new_line = new_line_metric;
1505 out.force_header = true;
1507 perf_stat__print_shadow_stats(counter, 0,
1511 fputc('\n', stat_config.output);
1514 static void print_interval(char *prefix, struct timespec *ts)
1516 FILE *output = stat_config.output;
1517 static int num_print_interval;
1519 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1521 if (num_print_interval == 0 && !csv_output) {
1522 switch (stat_config.aggr_mode) {
1524 fprintf(output, "# time socket cpus");
1526 fprintf(output, " counts %*s events\n", unit_width, "unit");
1529 fprintf(output, "# time core cpus");
1531 fprintf(output, " counts %*s events\n", unit_width, "unit");
1534 fprintf(output, "# time CPU");
1536 fprintf(output, " counts %*s events\n", unit_width, "unit");
1539 fprintf(output, "# time comm-pid");
1541 fprintf(output, " counts %*s events\n", unit_width, "unit");
1545 fprintf(output, "# time");
1547 fprintf(output, " counts %*s events\n", unit_width, "unit");
1553 if (num_print_interval == 0 && metric_only)
1554 print_metric_headers(" ", true);
1555 if (++num_print_interval == 25)
1556 num_print_interval = 0;
1559 static void print_header(int argc, const char **argv)
1561 FILE *output = stat_config.output;
1567 fprintf(output, "\n");
1568 fprintf(output, " Performance counter stats for ");
1569 if (target.system_wide)
1570 fprintf(output, "\'system wide");
1571 else if (target.cpu_list)
1572 fprintf(output, "\'CPU(s) %s", target.cpu_list);
1573 else if (!target__has_task(&target)) {
1574 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1575 for (i = 1; argv && (i < argc); i++)
1576 fprintf(output, " %s", argv[i]);
1577 } else if (target.pid)
1578 fprintf(output, "process id \'%s", target.pid);
1580 fprintf(output, "thread id \'%s", target.tid);
1582 fprintf(output, "\'");
1584 fprintf(output, " (%d runs)", run_count);
1585 fprintf(output, ":\n\n");
1589 static void print_footer(void)
1591 FILE *output = stat_config.output;
1595 fprintf(output, "\n");
1596 fprintf(output, " %17.9f seconds time elapsed",
1597 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
1598 if (run_count > 1) {
1599 fprintf(output, " ");
1600 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1601 avg_stats(&walltime_nsecs_stats));
1603 fprintf(output, "\n\n");
1605 if (print_free_counters_hint &&
1606 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1609 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1610 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1612 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1615 static void print_counters(struct timespec *ts, int argc, const char **argv)
1617 int interval = stat_config.interval;
1618 struct perf_evsel *counter;
1619 char buf[64], *prefix = NULL;
1621 /* Do not print anything if we record to the pipe. */
1622 if (STAT_RECORD && perf_stat.file.is_pipe)
1626 print_interval(prefix = buf, ts);
1628 print_header(argc, argv);
1631 static int num_print_iv;
1633 if (num_print_iv == 0 && !interval)
1634 print_metric_headers(prefix, false);
1635 if (num_print_iv++ == 25)
1637 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1638 fprintf(stat_config.output, "%s", prefix);
1641 switch (stat_config.aggr_mode) {
1647 evlist__for_each_entry(evsel_list, counter)
1648 print_aggr_thread(counter, prefix);
1651 evlist__for_each_entry(evsel_list, counter)
1652 print_counter_aggr(counter, prefix);
1654 fputc('\n', stat_config.output);
1658 print_no_aggr_metric(prefix);
1660 evlist__for_each_entry(evsel_list, counter)
1661 print_counter(counter, prefix);
1669 if (!interval && !csv_output)
1672 fflush(stat_config.output);
1675 static volatile int signr = -1;
1677 static void skip_signal(int signo)
1679 if ((child_pid == -1) || stat_config.interval)
1684 * render child_pid harmless
1685 * won't send SIGTERM to a random
1686 * process in case of race condition
1687 * and fast PID recycling
1692 static void sig_atexit(void)
1697 * avoid race condition with SIGCHLD handler
1698 * in skip_signal() which is modifying child_pid
1699 * goal is to avoid send SIGTERM to a random
1703 sigaddset(&set, SIGCHLD);
1704 sigprocmask(SIG_BLOCK, &set, &oset);
1706 if (child_pid != -1)
1707 kill(child_pid, SIGTERM);
1709 sigprocmask(SIG_SETMASK, &oset, NULL);
1714 signal(signr, SIG_DFL);
1715 kill(getpid(), signr);
1718 static int stat__set_big_num(const struct option *opt __maybe_unused,
1719 const char *s __maybe_unused, int unset)
1721 big_num_opt = unset ? 0 : 1;
1725 static int enable_metric_only(const struct option *opt __maybe_unused,
1726 const char *s __maybe_unused, int unset)
1728 force_metric_only = true;
1729 metric_only = !unset;
1733 static const struct option stat_options[] = {
1734 OPT_BOOLEAN('T', "transaction", &transaction_run,
1735 "hardware transaction statistics"),
1736 OPT_CALLBACK('e', "event", &evsel_list, "event",
1737 "event selector. use 'perf list' to list available events",
1738 parse_events_option),
1739 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1740 "event filter", parse_filter),
1741 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1742 "child tasks do not inherit counters"),
1743 OPT_STRING('p', "pid", &target.pid, "pid",
1744 "stat events on existing process id"),
1745 OPT_STRING('t', "tid", &target.tid, "tid",
1746 "stat events on existing thread id"),
1747 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1748 "system-wide collection from all CPUs"),
1749 OPT_BOOLEAN('g', "group", &group,
1750 "put the counters into a counter group"),
1751 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1752 OPT_INCR('v', "verbose", &verbose,
1753 "be more verbose (show counter open errors, etc)"),
1754 OPT_INTEGER('r', "repeat", &run_count,
1755 "repeat command and print average + stddev (max: 100, forever: 0)"),
1756 OPT_BOOLEAN('n', "null", &null_run,
1757 "null run - dont start any counters"),
1758 OPT_INCR('d', "detailed", &detailed_run,
1759 "detailed run - start a lot of events"),
1760 OPT_BOOLEAN('S', "sync", &sync_run,
1761 "call sync() before starting a run"),
1762 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1763 "print large numbers with thousands\' separators",
1765 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1766 "list of cpus to monitor in system-wide"),
1767 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1768 "disable CPU count aggregation", AGGR_NONE),
1769 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
1770 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1771 "print counts with custom separator"),
1772 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1773 "monitor event in cgroup name only", parse_cgroups),
1774 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1775 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1776 OPT_INTEGER(0, "log-fd", &output_fd,
1777 "log output to fd, instead of stderr"),
1778 OPT_STRING(0, "pre", &pre_cmd, "command",
1779 "command to run prior to the measured command"),
1780 OPT_STRING(0, "post", &post_cmd, "command",
1781 "command to run after to the measured command"),
1782 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1783 "print counts at regular interval in ms (>= 10)"),
1784 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1785 "aggregate counts per processor socket", AGGR_SOCKET),
1786 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1787 "aggregate counts per physical processor core", AGGR_CORE),
1788 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1789 "aggregate counts per thread", AGGR_THREAD),
1790 OPT_UINTEGER('D', "delay", &initial_delay,
1791 "ms to wait before starting measurement after program start"),
1792 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1793 "Only print computed metrics. No raw values", enable_metric_only),
1794 OPT_BOOLEAN(0, "topdown", &topdown_run,
1795 "measure topdown level 1 statistics"),
1796 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1797 "measure SMI cost"),
1801 static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1803 return cpu_map__get_socket(map, cpu, NULL);
1806 static int perf_stat__get_core(struct cpu_map *map, int cpu)
1808 return cpu_map__get_core(map, cpu, NULL);
1811 static int cpu_map__get_max(struct cpu_map *map)
1815 for (i = 0; i < map->nr; i++) {
1816 if (map->map[i] > max)
1823 static struct cpu_map *cpus_aggr_map;
1825 static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1832 cpu = map->map[idx];
1834 if (cpus_aggr_map->map[cpu] == -1)
1835 cpus_aggr_map->map[cpu] = get_id(map, idx);
1837 return cpus_aggr_map->map[cpu];
1840 static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1842 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1845 static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1847 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1850 static int perf_stat_init_aggr_mode(void)
1854 switch (stat_config.aggr_mode) {
1856 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1857 perror("cannot build socket map");
1860 aggr_get_id = perf_stat__get_socket_cached;
1863 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1864 perror("cannot build core map");
1867 aggr_get_id = perf_stat__get_core_cached;
1878 * The evsel_list->cpus is the base we operate on,
1879 * taking the highest cpu number to be the size of
1880 * the aggregation translate cpumap.
1882 nr = cpu_map__get_max(evsel_list->cpus);
1883 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1884 return cpus_aggr_map ? 0 : -ENOMEM;
1887 static void perf_stat__exit_aggr_mode(void)
1889 cpu_map__put(aggr_map);
1890 cpu_map__put(cpus_aggr_map);
1892 cpus_aggr_map = NULL;
1895 static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1902 cpu = map->map[idx];
1904 if (cpu >= env->nr_cpus_avail)
1910 static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1912 struct perf_env *env = data;
1913 int cpu = perf_env__get_cpu(env, map, idx);
1915 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1918 static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1920 struct perf_env *env = data;
1921 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1924 int socket_id = env->cpu[cpu].socket_id;
1927 * Encode socket in upper 16 bits
1928 * core_id is relative to socket, and
1929 * we need a global id. So we combine
1932 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1938 static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1939 struct cpu_map **sockp)
1941 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1944 static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1945 struct cpu_map **corep)
1947 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1950 static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1952 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1955 static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1957 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1960 static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1962 struct perf_env *env = &st->session->header.env;
1964 switch (stat_config.aggr_mode) {
1966 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1967 perror("cannot build socket map");
1970 aggr_get_id = perf_stat__get_socket_file;
1973 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1974 perror("cannot build core map");
1977 aggr_get_id = perf_stat__get_core_file;
1990 static int topdown_filter_events(const char **attr, char **str, bool use_group)
1997 for (i = 0; attr[i]; i++) {
1998 if (pmu_have_event("cpu", attr[i])) {
1999 len += strlen(attr[i]) + 1;
2000 attr[i - off] = attr[i];
2004 attr[i - off] = NULL;
2006 *str = malloc(len + 1 + 2);
2016 for (i = 0; attr[i]; i++) {
2029 __weak bool arch_topdown_check_group(bool *warn)
2035 __weak void arch_topdown_group_warn(void)
2040 * Add default attributes, if there were no attributes specified or
2041 * if -d/--detailed, -d -d or -d -d -d is used:
2043 static int add_default_attributes(void)
2046 struct perf_event_attr default_attrs0[] = {
2048 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2049 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2050 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2051 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2053 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
2055 struct perf_event_attr frontend_attrs[] = {
2056 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
2058 struct perf_event_attr backend_attrs[] = {
2059 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
2061 struct perf_event_attr default_attrs1[] = {
2062 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2063 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2064 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2069 * Detailed stats (-d), covering the L1 and last level data caches:
2071 struct perf_event_attr detailed_attrs[] = {
2073 { .type = PERF_TYPE_HW_CACHE,
2075 PERF_COUNT_HW_CACHE_L1D << 0 |
2076 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2077 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2079 { .type = PERF_TYPE_HW_CACHE,
2081 PERF_COUNT_HW_CACHE_L1D << 0 |
2082 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2083 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2085 { .type = PERF_TYPE_HW_CACHE,
2087 PERF_COUNT_HW_CACHE_LL << 0 |
2088 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2089 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2091 { .type = PERF_TYPE_HW_CACHE,
2093 PERF_COUNT_HW_CACHE_LL << 0 |
2094 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2095 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2099 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2101 struct perf_event_attr very_detailed_attrs[] = {
2103 { .type = PERF_TYPE_HW_CACHE,
2105 PERF_COUNT_HW_CACHE_L1I << 0 |
2106 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2107 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2109 { .type = PERF_TYPE_HW_CACHE,
2111 PERF_COUNT_HW_CACHE_L1I << 0 |
2112 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2113 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2115 { .type = PERF_TYPE_HW_CACHE,
2117 PERF_COUNT_HW_CACHE_DTLB << 0 |
2118 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2119 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2121 { .type = PERF_TYPE_HW_CACHE,
2123 PERF_COUNT_HW_CACHE_DTLB << 0 |
2124 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2125 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2127 { .type = PERF_TYPE_HW_CACHE,
2129 PERF_COUNT_HW_CACHE_ITLB << 0 |
2130 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2131 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2133 { .type = PERF_TYPE_HW_CACHE,
2135 PERF_COUNT_HW_CACHE_ITLB << 0 |
2136 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2137 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2142 * Very, very detailed stats (-d -d -d), adding prefetch events:
2144 struct perf_event_attr very_very_detailed_attrs[] = {
2146 { .type = PERF_TYPE_HW_CACHE,
2148 PERF_COUNT_HW_CACHE_L1D << 0 |
2149 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2150 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2152 { .type = PERF_TYPE_HW_CACHE,
2154 PERF_COUNT_HW_CACHE_L1D << 0 |
2155 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2156 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2159 /* Set attrs if no event is selected and !null_run: */
2163 if (transaction_run) {
2164 if (pmu_have_event("cpu", "cycles-ct") &&
2165 pmu_have_event("cpu", "el-start"))
2166 err = parse_events(evsel_list, transaction_attrs, NULL);
2168 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2170 fprintf(stderr, "Cannot set up transaction events\n");
2179 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2180 fprintf(stderr, "freeze_on_smi is not supported.\n");
2185 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2186 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2192 if (pmu_have_event("msr", "aperf") &&
2193 pmu_have_event("msr", "smi")) {
2194 if (!force_metric_only)
2196 err = parse_events(evsel_list, smi_cost_attrs, NULL);
2198 fprintf(stderr, "To measure SMI cost, it needs "
2199 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2203 fprintf(stderr, "Cannot set up SMI cost events\n");
2213 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2214 stat_config.aggr_mode != AGGR_CORE) {
2215 pr_err("top down event configuration requires --per-core mode\n");
2218 stat_config.aggr_mode = AGGR_CORE;
2219 if (nr_cgroups || !target__has_cpu(&target)) {
2220 pr_err("top down event configuration requires system-wide mode (-a)\n");
2224 if (!force_metric_only)
2226 if (topdown_filter_events(topdown_attrs, &str,
2227 arch_topdown_check_group(&warn)) < 0) {
2228 pr_err("Out of memory\n");
2231 if (topdown_attrs[0] && str) {
2233 arch_topdown_group_warn();
2234 err = parse_events(evsel_list, str, NULL);
2237 "Cannot set up top down events %s: %d\n",
2243 fprintf(stderr, "System does not support topdown\n");
2249 if (!evsel_list->nr_entries) {
2250 if (target__has_cpu(&target))
2251 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2253 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2255 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2256 if (perf_evlist__add_default_attrs(evsel_list,
2257 frontend_attrs) < 0)
2260 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2261 if (perf_evlist__add_default_attrs(evsel_list,
2265 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
2269 /* Detailed events get appended to the event list: */
2271 if (detailed_run < 1)
2274 /* Append detailed run extra attributes: */
2275 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
2278 if (detailed_run < 2)
2281 /* Append very detailed run extra attributes: */
2282 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
2285 if (detailed_run < 3)
2288 /* Append very, very detailed run extra attributes: */
2289 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
2292 static const char * const stat_record_usage[] = {
2293 "perf stat record [<options>]",
2297 static void init_features(struct perf_session *session)
2301 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2302 perf_header__set_feat(&session->header, feat);
2304 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2305 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2306 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2307 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2310 static int __cmd_record(int argc, const char **argv)
2312 struct perf_session *session;
2313 struct perf_data_file *file = &perf_stat.file;
2315 argc = parse_options(argc, argv, stat_options, stat_record_usage,
2316 PARSE_OPT_STOP_AT_NON_OPTION);
2319 file->path = output_name;
2321 if (run_count != 1 || forever) {
2322 pr_err("Cannot use -r option with perf stat record.\n");
2326 session = perf_session__new(file, false, NULL);
2327 if (session == NULL) {
2328 pr_err("Perf session creation failed.\n");
2332 init_features(session);
2334 session->evlist = evsel_list;
2335 perf_stat.session = session;
2336 perf_stat.record = true;
2340 static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2341 union perf_event *event,
2342 struct perf_session *session)
2344 struct stat_round_event *stat_round = &event->stat_round;
2345 struct perf_evsel *counter;
2346 struct timespec tsh, *ts = NULL;
2347 const char **argv = session->header.env.cmdline_argv;
2348 int argc = session->header.env.nr_cmdline;
2350 evlist__for_each_entry(evsel_list, counter)
2351 perf_stat_process_counter(&stat_config, counter);
2353 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2354 update_stats(&walltime_nsecs_stats, stat_round->time);
2356 if (stat_config.interval && stat_round->time) {
2357 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2358 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
2362 print_counters(ts, argc, argv);
2367 int process_stat_config_event(struct perf_tool *tool,
2368 union perf_event *event,
2369 struct perf_session *session __maybe_unused)
2371 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2373 perf_event__read_stat_config(&stat_config, &event->stat_config);
2375 if (cpu_map__empty(st->cpus)) {
2376 if (st->aggr_mode != AGGR_UNSET)
2377 pr_warning("warning: processing task data, aggregation mode not set\n");
2381 if (st->aggr_mode != AGGR_UNSET)
2382 stat_config.aggr_mode = st->aggr_mode;
2384 if (perf_stat.file.is_pipe)
2385 perf_stat_init_aggr_mode();
2387 perf_stat_init_aggr_mode_file(st);
2392 static int set_maps(struct perf_stat *st)
2394 if (!st->cpus || !st->threads)
2397 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2400 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2402 if (perf_evlist__alloc_stats(evsel_list, true))
2405 st->maps_allocated = true;
2410 int process_thread_map_event(struct perf_tool *tool,
2411 union perf_event *event,
2412 struct perf_session *session __maybe_unused)
2414 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2417 pr_warning("Extra thread map event, ignoring.\n");
2421 st->threads = thread_map__new_event(&event->thread_map);
2425 return set_maps(st);
2429 int process_cpu_map_event(struct perf_tool *tool,
2430 union perf_event *event,
2431 struct perf_session *session __maybe_unused)
2433 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2434 struct cpu_map *cpus;
2437 pr_warning("Extra cpu map event, ignoring.\n");
2441 cpus = cpu_map__new_data(&event->cpu_map.data);
2446 return set_maps(st);
2449 static const char * const stat_report_usage[] = {
2450 "perf stat report [<options>]",
2454 static struct perf_stat perf_stat = {
2456 .attr = perf_event__process_attr,
2457 .event_update = perf_event__process_event_update,
2458 .thread_map = process_thread_map_event,
2459 .cpu_map = process_cpu_map_event,
2460 .stat_config = process_stat_config_event,
2461 .stat = perf_event__process_stat_event,
2462 .stat_round = process_stat_round_event,
2464 .aggr_mode = AGGR_UNSET,
2467 static int __cmd_report(int argc, const char **argv)
2469 struct perf_session *session;
2470 const struct option options[] = {
2471 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2472 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2473 "aggregate counts per processor socket", AGGR_SOCKET),
2474 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2475 "aggregate counts per physical processor core", AGGR_CORE),
2476 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2477 "disable CPU count aggregation", AGGR_NONE),
2483 argc = parse_options(argc, argv, options, stat_report_usage, 0);
2485 if (!input_name || !strlen(input_name)) {
2486 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2489 input_name = "perf.data";
2492 perf_stat.file.path = input_name;
2493 perf_stat.file.mode = PERF_DATA_MODE_READ;
2495 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2496 if (session == NULL)
2499 perf_stat.session = session;
2500 stat_config.output = stderr;
2501 evsel_list = session->evlist;
2503 ret = perf_session__process_events(session);
2507 perf_session__delete(session);
2511 static void setup_system_wide(int forks)
2514 * Make system wide (-a) the default target if
2515 * no target was specified and one of following
2516 * conditions is met:
2518 * - there's no workload specified
2519 * - there is workload specified but all requested
2520 * events are system wide events
2522 if (!target__none(&target))
2526 target.system_wide = true;
2528 struct perf_evsel *counter;
2530 evlist__for_each_entry(evsel_list, counter) {
2531 if (!counter->system_wide)
2535 if (evsel_list->nr_entries)
2536 target.system_wide = true;
2540 int cmd_stat(int argc, const char **argv)
2542 const char * const stat_usage[] = {
2543 "perf stat [<options>] [<command>]",
2546 int status = -EINVAL, run_idx;
2548 FILE *output = stderr;
2549 unsigned int interval;
2550 const char * const stat_subcommands[] = { "record", "report" };
2552 setlocale(LC_ALL, "");
2554 evsel_list = perf_evlist__new();
2555 if (evsel_list == NULL)
2558 parse_events__shrink_config_terms();
2559 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2560 (const char **) stat_usage,
2561 PARSE_OPT_STOP_AT_NON_OPTION);
2562 perf_stat__collect_metric_expr(evsel_list);
2563 perf_stat__init_shadow_stats();
2567 if (!strcmp(csv_sep, "\\t"))
2570 csv_sep = DEFAULT_SEPARATOR;
2572 if (argc && !strncmp(argv[0], "rec", 3)) {
2573 argc = __cmd_record(argc, argv);
2576 } else if (argc && !strncmp(argv[0], "rep", 3))
2577 return __cmd_report(argc, argv);
2579 interval = stat_config.interval;
2582 * For record command the -o is already taken care of.
2584 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
2587 if (output_name && output_fd) {
2588 fprintf(stderr, "cannot use both --output and --log-fd\n");
2589 parse_options_usage(stat_usage, stat_options, "o", 1);
2590 parse_options_usage(NULL, stat_options, "log-fd", 0);
2594 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2595 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2599 if (metric_only && run_count > 1) {
2600 fprintf(stderr, "--metric-only is not supported with -r\n");
2604 if (output_fd < 0) {
2605 fprintf(stderr, "argument to --log-fd must be a > 0\n");
2606 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
2612 mode = append_file ? "a" : "w";
2614 output = fopen(output_name, mode);
2616 perror("failed to create output file");
2619 clock_gettime(CLOCK_REALTIME, &tm);
2620 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
2621 } else if (output_fd > 0) {
2622 mode = append_file ? "a" : "w";
2623 output = fdopen(output_fd, mode);
2625 perror("Failed opening logfd");
2630 stat_config.output = output;
2633 * let the spreadsheet do the pretty-printing
2636 /* User explicitly passed -B? */
2637 if (big_num_opt == 1) {
2638 fprintf(stderr, "-B option not supported with -x\n");
2639 parse_options_usage(stat_usage, stat_options, "B", 1);
2640 parse_options_usage(NULL, stat_options, "x", 1);
2642 } else /* Nope, so disable big number formatting */
2644 } else if (big_num_opt == 0) /* User passed --no-big-num */
2647 setup_system_wide(argc);
2649 if (run_count < 0) {
2650 pr_err("Run count must be a positive number\n");
2651 parse_options_usage(stat_usage, stat_options, "r", 1);
2653 } else if (run_count == 0) {
2658 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
2659 fprintf(stderr, "The --per-thread option is only available "
2660 "when monitoring via -p -t options.\n");
2661 parse_options_usage(NULL, stat_options, "p", 1);
2662 parse_options_usage(NULL, stat_options, "t", 1);
2667 * no_aggr, cgroup are for system-wide only
2668 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2670 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2671 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
2672 !target__has_cpu(&target)) {
2673 fprintf(stderr, "both cgroup and no-aggregation "
2674 "modes only available in system-wide mode\n");
2676 parse_options_usage(stat_usage, stat_options, "G", 1);
2677 parse_options_usage(NULL, stat_options, "A", 1);
2678 parse_options_usage(NULL, stat_options, "a", 1);
2682 if (add_default_attributes())
2685 target__validate(&target);
2687 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
2688 if (target__has_task(&target)) {
2689 pr_err("Problems finding threads of monitor\n");
2690 parse_options_usage(stat_usage, stat_options, "p", 1);
2691 parse_options_usage(NULL, stat_options, "t", 1);
2692 } else if (target__has_cpu(&target)) {
2693 perror("failed to parse CPUs map");
2694 parse_options_usage(stat_usage, stat_options, "C", 1);
2695 parse_options_usage(NULL, stat_options, "a", 1);
2701 * Initialize thread_map with comm names,
2702 * so we could print it out on output.
2704 if (stat_config.aggr_mode == AGGR_THREAD)
2705 thread_map__read_comms(evsel_list->threads);
2707 if (interval && interval < 100) {
2708 if (interval < 10) {
2709 pr_err("print interval must be >= 10ms\n");
2710 parse_options_usage(stat_usage, stat_options, "I", 1);
2713 pr_warning("print interval < 100ms. "
2714 "The overhead percentage could be high in some cases. "
2715 "Please proceed with caution.\n");
2718 if (perf_evlist__alloc_stats(evsel_list, interval))
2721 if (perf_stat_init_aggr_mode())
2725 * We dont want to block the signals - that would cause
2726 * child tasks to inherit that and Ctrl-C would not work.
2727 * What we want is for Ctrl-C to work in the exec()-ed
2728 * task, but being ignored by perf stat itself:
2732 signal(SIGINT, skip_signal);
2733 signal(SIGCHLD, skip_signal);
2734 signal(SIGALRM, skip_signal);
2735 signal(SIGABRT, skip_signal);
2738 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
2739 if (run_count != 1 && verbose > 0)
2740 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2743 status = run_perf_stat(argc, argv);
2744 if (forever && status != -1) {
2745 print_counters(NULL, argc, argv);
2746 perf_stat__reset_stats();
2750 if (!forever && status != -1 && !interval)
2751 print_counters(NULL, argc, argv);
2755 * We synthesize the kernel mmap record just so that older tools
2756 * don't emit warnings about not being able to resolve symbols
2757 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2758 * a saner message about no samples being in the perf.data file.
2760 * This also serves to suppress a warning about f_header.data.size == 0
2761 * in header.c at the moment 'perf stat record' gets introduced, which
2762 * is not really needed once we start adding the stat specific PERF_RECORD_
2763 * records, but the need to suppress the kptr_restrict messages in older
2764 * tools remain -acme
2766 int fd = perf_data_file__fd(&perf_stat.file);
2767 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2768 process_synthesized_event,
2769 &perf_stat.session->machines.host);
2771 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2772 "older tools may produce warnings about this file\n.");
2776 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2777 pr_err("failed to write stat round event\n");
2780 if (!perf_stat.file.is_pipe) {
2781 perf_stat.session->header.data_size += perf_stat.bytes_written;
2782 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2785 perf_session__delete(perf_stat.session);
2788 perf_stat__exit_aggr_mode();
2789 perf_evlist__free_stats(evsel_list);
2791 if (smi_cost && smi_reset)
2792 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
2794 perf_evlist__delete(evsel_list);