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/string2.h"
67 #include "util/metricgroup.h"
71 #include <linux/time64.h>
72 #include <api/fs/fs.h>
76 #include <sys/prctl.h>
80 #include <sys/types.h>
85 #include <sys/resource.h>
88 #include "sane_ctype.h"
90 #define DEFAULT_SEPARATOR " "
91 #define CNTR_NOT_SUPPORTED "<not supported>"
92 #define CNTR_NOT_COUNTED "<not counted>"
93 #define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
95 static void print_counters(struct timespec *ts, int argc, const char **argv);
97 /* Default events used for perf stat -T */
98 static const char *transaction_attrs = {
110 /* More limited version when the CPU does not have all events. */
111 static const char * transaction_limited_attrs = {
121 static const char * topdown_attrs[] = {
122 "topdown-total-slots",
123 "topdown-slots-retired",
124 "topdown-recovery-bubbles",
125 "topdown-fetch-bubbles",
126 "topdown-slots-issued",
130 static const char *smi_cost_attrs = {
138 static struct perf_evlist *evsel_list;
140 static struct rblist metric_events;
142 static struct target target = {
146 #define METRIC_ONLY_LEN 20
148 static volatile pid_t child_pid = -1;
149 static int detailed_run = 0;
150 static bool transaction_run;
151 static bool topdown_run = false;
152 static bool smi_cost = false;
153 static bool smi_reset = false;
154 static int big_num_opt = -1;
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 bool forever = false;
160 static bool force_metric_only = false;
161 static struct timespec ref_time;
162 static bool append_file;
163 static bool interval_count;
164 static const char *output_name;
165 static int output_fd;
169 struct perf_data data;
170 struct perf_session *session;
172 struct perf_tool tool;
174 struct cpu_map *cpus;
175 struct thread_map *threads;
176 enum aggr_mode aggr_mode;
179 static struct perf_stat perf_stat;
180 #define STAT_RECORD perf_stat.record
182 static volatile int done = 0;
184 static struct perf_stat_config stat_config = {
185 .aggr_mode = AGGR_GLOBAL,
187 .unit_width = 4, /* strlen("unit") */
189 .metric_only_len = METRIC_ONLY_LEN,
190 .walltime_nsecs_stats = &walltime_nsecs_stats,
194 static bool is_duration_time(struct perf_evsel *evsel)
196 return !strcmp(evsel->name, "duration_time");
199 static inline void diff_timespec(struct timespec *r, struct timespec *a,
202 r->tv_sec = a->tv_sec - b->tv_sec;
203 if (a->tv_nsec < b->tv_nsec) {
204 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
207 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
211 static void perf_stat__reset_stats(void)
215 perf_evlist__reset_stats(evsel_list);
216 perf_stat__reset_shadow_stats();
218 for (i = 0; i < stat_config.stats_num; i++)
219 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
222 static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
223 union perf_event *event,
224 struct perf_sample *sample __maybe_unused,
225 struct machine *machine __maybe_unused)
227 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
228 pr_err("failed to write perf data, error: %m\n");
232 perf_stat.bytes_written += event->header.size;
236 static int write_stat_round_event(u64 tm, u64 type)
238 return perf_event__synthesize_stat_round(NULL, tm, type,
239 process_synthesized_event,
243 #define WRITE_STAT_ROUND_EVENT(time, interval) \
244 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
246 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
249 perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
250 struct perf_counts_values *count)
252 struct perf_sample_id *sid = SID(counter, cpu, thread);
254 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
255 process_synthesized_event, NULL);
259 * Read out the results of a single counter:
260 * do not aggregate counts across CPUs in system-wide mode
262 static int read_counter(struct perf_evsel *counter)
264 int nthreads = thread_map__nr(evsel_list->threads);
265 int ncpus, cpu, thread;
267 if (target__has_cpu(&target) && !target__has_per_thread(&target))
268 ncpus = perf_evsel__nr_cpus(counter);
272 if (!counter->supported)
275 if (counter->system_wide)
278 for (thread = 0; thread < nthreads; thread++) {
279 for (cpu = 0; cpu < ncpus; cpu++) {
280 struct perf_counts_values *count;
282 count = perf_counts(counter->counts, cpu, thread);
285 * The leader's group read loads data into its group members
286 * (via perf_evsel__read_counter) and sets threir count->loaded.
288 if (!count->loaded &&
289 perf_evsel__read_counter(counter, cpu, thread)) {
290 counter->counts->scaled = -1;
291 perf_counts(counter->counts, cpu, thread)->ena = 0;
292 perf_counts(counter->counts, cpu, thread)->run = 0;
296 count->loaded = false;
299 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
300 pr_err("failed to write stat event\n");
306 fprintf(stat_config.output,
307 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
308 perf_evsel__name(counter),
310 count->val, count->ena, count->run);
318 static void read_counters(void)
320 struct perf_evsel *counter;
323 evlist__for_each_entry(evsel_list, counter) {
324 ret = read_counter(counter);
326 pr_debug("failed to read counter %s\n", counter->name);
328 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
329 pr_warning("failed to process counter %s\n", counter->name);
333 static void process_interval(void)
335 struct timespec ts, rs;
339 clock_gettime(CLOCK_MONOTONIC, &ts);
340 diff_timespec(&rs, &ts, &ref_time);
343 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
344 pr_err("failed to write stat round event\n");
347 init_stats(&walltime_nsecs_stats);
348 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
349 print_counters(&rs, 0, NULL);
352 static void enable_counters(void)
354 if (stat_config.initial_delay)
355 usleep(stat_config.initial_delay * USEC_PER_MSEC);
358 * We need to enable counters only if:
359 * - we don't have tracee (attaching to task or cpu)
360 * - we have initial delay configured
362 if (!target__none(&target) || stat_config.initial_delay)
363 perf_evlist__enable(evsel_list);
366 static void disable_counters(void)
369 * If we don't have tracee (attaching to task or cpu), counters may
370 * still be running. To get accurate group ratios, we must stop groups
371 * from counting before reading their constituent counters.
373 if (!target__none(&target))
374 perf_evlist__disable(evsel_list);
377 static volatile int workload_exec_errno;
380 * perf_evlist__prepare_workload will send a SIGUSR1
381 * if the fork fails, since we asked by setting its
382 * want_signal to true.
384 static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
385 void *ucontext __maybe_unused)
387 workload_exec_errno = info->si_value.sival_int;
390 static bool perf_evsel__should_store_id(struct perf_evsel *counter)
392 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
395 static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
397 struct perf_evsel *c2, *leader;
400 leader = evsel->leader;
401 pr_debug("Weak group for %s/%d failed\n",
402 leader->name, leader->nr_members);
405 * for_each_group_member doesn't work here because it doesn't
406 * include the first entry.
408 evlist__for_each_entry(evsel_list, c2) {
411 if (c2->leader == leader) {
413 perf_evsel__close(c2);
421 static int __run_perf_stat(int argc, const char **argv, int run_idx)
423 int interval = stat_config.interval;
424 int times = stat_config.times;
425 int timeout = stat_config.timeout;
427 unsigned long long t0, t1;
428 struct perf_evsel *counter;
432 const bool forks = (argc > 0);
433 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
434 struct perf_evsel_config_term *err_term;
437 ts.tv_sec = interval / USEC_PER_MSEC;
438 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
439 } else if (timeout) {
440 ts.tv_sec = timeout / USEC_PER_MSEC;
441 ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
448 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
449 workload_exec_failed_signal) < 0) {
450 perror("failed to prepare workload");
453 child_pid = evsel_list->workload.pid;
457 perf_evlist__set_leader(evsel_list);
459 evlist__for_each_entry(evsel_list, counter) {
461 if (create_perf_stat_counter(counter, &stat_config, &target) < 0) {
463 /* Weak group failed. Reset the group. */
464 if ((errno == EINVAL || errno == EBADF) &&
465 counter->leader != counter &&
466 counter->weak_group) {
467 counter = perf_evsel__reset_weak_group(counter);
472 * PPC returns ENXIO for HW counters until 2.6.37
473 * (behavior changed with commit b0a873e).
475 if (errno == EINVAL || errno == ENOSYS ||
476 errno == ENOENT || errno == EOPNOTSUPP ||
479 ui__warning("%s event is not supported by the kernel.\n",
480 perf_evsel__name(counter));
481 counter->supported = false;
483 if ((counter->leader != counter) ||
484 !(counter->leader->nr_members > 1))
486 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
488 ui__warning("%s\n", msg);
490 } else if (target__has_per_thread(&target) &&
491 evsel_list->threads &&
492 evsel_list->threads->err_thread != -1) {
494 * For global --per-thread case, skip current
497 if (!thread_map__remove(evsel_list->threads,
498 evsel_list->threads->err_thread)) {
499 evsel_list->threads->err_thread = -1;
504 perf_evsel__open_strerror(counter, &target,
505 errno, msg, sizeof(msg));
506 ui__error("%s\n", msg);
509 kill(child_pid, SIGTERM);
513 counter->supported = true;
515 l = strlen(counter->unit);
516 if (l > stat_config.unit_width)
517 stat_config.unit_width = l;
519 if (perf_evsel__should_store_id(counter) &&
520 perf_evsel__store_ids(counter, evsel_list))
524 if (perf_evlist__apply_filters(evsel_list, &counter)) {
525 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
526 counter->filter, perf_evsel__name(counter), errno,
527 str_error_r(errno, msg, sizeof(msg)));
531 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
532 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
533 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
534 str_error_r(errno, msg, sizeof(msg)));
539 int err, fd = perf_data__fd(&perf_stat.data);
542 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
544 err = perf_session__write_header(perf_stat.session, evsel_list,
551 err = perf_stat_synthesize_config(&stat_config, NULL, evsel_list,
552 process_synthesized_event, is_pipe);
558 * Enable counters and exec the command:
561 clock_gettime(CLOCK_MONOTONIC, &ref_time);
564 perf_evlist__start_workload(evsel_list);
567 if (interval || timeout) {
568 while (!waitpid(child_pid, &status, WNOHANG)) {
569 nanosleep(&ts, NULL);
573 if (interval_count && !(--times))
577 wait4(child_pid, &status, 0, &stat_config.ru_data);
579 if (workload_exec_errno) {
580 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
581 pr_err("Workload failed: %s\n", emsg);
585 if (WIFSIGNALED(status))
586 psignal(WTERMSIG(status), argv[0]);
590 nanosleep(&ts, NULL);
595 if (interval_count && !(--times))
605 if (stat_config.walltime_run_table)
606 stat_config.walltime_run[run_idx] = t1 - t0;
608 update_stats(&walltime_nsecs_stats, t1 - t0);
611 * Closing a group leader splits the group, and as we only disable
612 * group leaders, results in remaining events becoming enabled. To
613 * avoid arbitrary skew, we must read all counters before closing any
617 perf_evlist__close(evsel_list);
619 return WEXITSTATUS(status);
622 static int run_perf_stat(int argc, const char **argv, int run_idx)
627 ret = system(pre_cmd);
635 ret = __run_perf_stat(argc, argv, run_idx);
640 ret = system(post_cmd);
648 static void print_running(struct perf_stat_config *config,
651 if (config->csv_output) {
652 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
656 ena ? 100.0 * run / ena : 100.0);
657 } else if (run != ena) {
658 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
662 static void print_noise_pct(struct perf_stat_config *config,
663 double total, double avg)
665 double pct = rel_stddev_stats(total, avg);
667 if (config->csv_output)
668 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
670 fprintf(config->output, " ( +-%6.2f%% )", pct);
673 static void print_noise(struct perf_stat_config *config,
674 struct perf_evsel *evsel, double avg)
676 struct perf_stat_evsel *ps;
678 if (config->run_count == 1)
682 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
685 static void aggr_printout(struct perf_stat_config *config,
686 struct perf_evsel *evsel, int id, int nr)
688 switch (config->aggr_mode) {
690 fprintf(config->output, "S%d-C%*d%s%*d%s",
691 cpu_map__id_to_socket(id),
692 config->csv_output ? 0 : -8,
693 cpu_map__id_to_cpu(id),
695 config->csv_output ? 0 : 4,
700 fprintf(config->output, "S%*d%s%*d%s",
701 config->csv_output ? 0 : -5,
704 config->csv_output ? 0 : 4,
709 fprintf(config->output, "CPU%*d%s",
710 config->csv_output ? 0 : -4,
711 perf_evsel__cpus(evsel)->map[id], config->csv_sep);
714 fprintf(config->output, "%*s-%*d%s",
715 config->csv_output ? 0 : 16,
716 thread_map__comm(evsel->threads, id),
717 config->csv_output ? 0 : -8,
718 thread_map__pid(evsel->threads, id),
734 struct perf_evsel *evsel;
737 #define METRIC_LEN 35
739 static void new_line_std(struct perf_stat_config *config __maybe_unused,
742 struct outstate *os = ctx;
747 static void do_new_line_std(struct perf_stat_config *config,
751 fputs(os->prefix, os->fh);
752 aggr_printout(config, os->evsel, os->id, os->nr);
753 if (config->aggr_mode == AGGR_NONE)
754 fprintf(os->fh, " ");
755 fprintf(os->fh, " ");
758 static void print_metric_std(struct perf_stat_config *config,
759 void *ctx, const char *color, const char *fmt,
760 const char *unit, double val)
762 struct outstate *os = ctx;
765 bool newline = os->newline;
769 if (unit == NULL || fmt == NULL) {
770 fprintf(out, "%-*s", METRIC_LEN, "");
775 do_new_line_std(config, os);
777 n = fprintf(out, " # ");
779 n += color_fprintf(out, color, fmt, val);
781 n += fprintf(out, fmt, val);
782 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
785 static void new_line_csv(struct perf_stat_config *config, void *ctx)
787 struct outstate *os = ctx;
792 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
793 aggr_printout(config, os->evsel, os->id, os->nr);
794 for (i = 0; i < os->nfields; i++)
795 fputs(config->csv_sep, os->fh);
798 static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
800 const char *color __maybe_unused,
801 const char *fmt, const char *unit, double val)
803 struct outstate *os = ctx;
805 char buf[64], *vals, *ends;
807 if (unit == NULL || fmt == NULL) {
808 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
811 snprintf(buf, sizeof(buf), fmt, val);
812 ends = vals = ltrim(buf);
813 while (isdigit(*ends) || *ends == '.')
816 while (isspace(*unit))
818 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, unit);
821 /* Filter out some columns that don't work well in metrics only mode */
823 static bool valid_only_metric(const char *unit)
827 if (strstr(unit, "/sec") ||
828 strstr(unit, "hz") ||
829 strstr(unit, "Hz") ||
830 strstr(unit, "CPUs utilized"))
835 static const char *fixunit(char *buf, struct perf_evsel *evsel,
838 if (!strncmp(unit, "of all", 6)) {
839 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
846 static void print_metric_only(struct perf_stat_config *config,
847 void *ctx, const char *color, const char *fmt,
848 const char *unit, double val)
850 struct outstate *os = ctx;
852 char buf[1024], str[1024];
853 unsigned mlen = config->metric_only_len;
855 if (!valid_only_metric(unit))
857 unit = fixunit(buf, os->evsel, unit);
858 if (mlen < strlen(unit))
859 mlen = strlen(unit) + 1;
862 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
864 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
865 fprintf(out, "%*s ", mlen, str);
868 static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
869 void *ctx, const char *color __maybe_unused,
871 const char *unit, double val)
873 struct outstate *os = ctx;
875 char buf[64], *vals, *ends;
878 if (!valid_only_metric(unit))
880 unit = fixunit(tbuf, os->evsel, unit);
881 snprintf(buf, sizeof buf, fmt, val);
882 ends = vals = ltrim(buf);
883 while (isdigit(*ends) || *ends == '.')
886 fprintf(out, "%s%s", vals, config->csv_sep);
889 static void new_line_metric(struct perf_stat_config *config __maybe_unused,
890 void *ctx __maybe_unused)
894 static void print_metric_header(struct perf_stat_config *config,
895 void *ctx, const char *color __maybe_unused,
896 const char *fmt __maybe_unused,
897 const char *unit, double val __maybe_unused)
899 struct outstate *os = ctx;
902 if (!valid_only_metric(unit))
904 unit = fixunit(tbuf, os->evsel, unit);
905 if (config->csv_output)
906 fprintf(os->fh, "%s%s", unit, config->csv_sep);
908 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
911 static int first_shadow_cpu(struct perf_stat_config *config,
912 struct perf_evsel *evsel, int id)
914 struct perf_evlist *evlist = evsel->evlist;
917 if (!config->aggr_get_id)
920 if (config->aggr_mode == AGGR_NONE)
923 if (config->aggr_mode == AGGR_GLOBAL)
926 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
927 int cpu2 = perf_evsel__cpus(evsel)->map[i];
929 if (config->aggr_get_id(config, evlist->cpus, cpu2) == id)
935 static void abs_printout(struct perf_stat_config *config,
936 int id, int nr, struct perf_evsel *evsel, double avg)
938 FILE *output = config->output;
939 double sc = evsel->scale;
942 if (config->csv_output) {
943 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
946 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
948 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
951 aggr_printout(config, evsel, id, nr);
953 fprintf(output, fmt, avg, config->csv_sep);
956 fprintf(output, "%-*s%s",
957 config->csv_output ? 0 : config->unit_width,
958 evsel->unit, config->csv_sep);
960 fprintf(output, "%-*s", config->csv_output ? 0 : 25, perf_evsel__name(evsel));
963 fprintf(output, "%s%s", config->csv_sep, evsel->cgrp->name);
966 static bool is_mixed_hw_group(struct perf_evsel *counter)
968 struct perf_evlist *evlist = counter->evlist;
969 u32 pmu_type = counter->attr.type;
970 struct perf_evsel *pos;
972 if (counter->nr_members < 2)
975 evlist__for_each_entry(evlist, pos) {
976 /* software events can be part of any hardware group */
977 if (pos->attr.type == PERF_TYPE_SOFTWARE)
979 if (pmu_type == PERF_TYPE_SOFTWARE) {
980 pmu_type = pos->attr.type;
983 if (pmu_type != pos->attr.type)
990 static void printout(struct perf_stat_config *config, int id, int nr,
991 struct perf_evsel *counter, double uval,
992 char *prefix, u64 run, u64 ena, double noise,
993 struct runtime_stat *st)
995 struct perf_stat_output_ctx out;
996 struct outstate os = {
997 .fh = config->output,
998 .prefix = prefix ? prefix : "",
1003 print_metric_t pm = print_metric_std;
1006 if (config->metric_only) {
1007 nl = new_line_metric;
1008 if (config->csv_output)
1009 pm = print_metric_only_csv;
1011 pm = print_metric_only;
1015 if (config->csv_output && !config->metric_only) {
1016 static int aggr_fields[] = {
1024 pm = print_metric_csv;
1027 os.nfields += aggr_fields[config->aggr_mode];
1031 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
1032 if (config->metric_only) {
1033 pm(config, &os, NULL, "", "", 0);
1036 aggr_printout(config, counter, id, nr);
1038 fprintf(config->output, "%*s%s",
1039 config->csv_output ? 0 : 18,
1040 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1043 if (counter->supported) {
1044 config->print_free_counters_hint = 1;
1045 if (is_mixed_hw_group(counter))
1046 config->print_mixed_hw_group_error = 1;
1049 fprintf(config->output, "%-*s%s",
1050 config->csv_output ? 0 : config->unit_width,
1051 counter->unit, config->csv_sep);
1053 fprintf(config->output, "%*s",
1054 config->csv_output ? 0 : -25,
1055 perf_evsel__name(counter));
1058 fprintf(config->output, "%s%s",
1059 config->csv_sep, counter->cgrp->name);
1061 if (!config->csv_output)
1062 pm(config, &os, NULL, NULL, "", 0);
1063 print_noise(config, counter, noise);
1064 print_running(config, run, ena);
1065 if (config->csv_output)
1066 pm(config, &os, NULL, NULL, "", 0);
1070 if (!config->metric_only)
1071 abs_printout(config, id, nr, counter, uval);
1073 out.print_metric = pm;
1076 out.force_header = false;
1078 if (config->csv_output && !config->metric_only) {
1079 print_noise(config, counter, noise);
1080 print_running(config, run, ena);
1083 perf_stat__print_shadow_stats(config, counter, uval,
1084 first_shadow_cpu(config, counter, id),
1085 &out, &metric_events, st);
1086 if (!config->csv_output && !config->metric_only) {
1087 print_noise(config, counter, noise);
1088 print_running(config, run, ena);
1092 static void aggr_update_shadow(struct perf_stat_config *config,
1093 struct perf_evlist *evlist)
1097 struct perf_evsel *counter;
1099 for (s = 0; s < config->aggr_map->nr; s++) {
1100 id = config->aggr_map->map[s];
1101 evlist__for_each_entry(evlist, counter) {
1103 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1104 s2 = config->aggr_get_id(config, evlist->cpus, cpu);
1107 val += perf_counts(counter->counts, cpu, 0)->val;
1109 perf_stat__update_shadow_stats(counter, val,
1110 first_shadow_cpu(config, counter, id),
1116 static void uniquify_event_name(struct perf_evsel *counter)
1121 if (counter->uniquified_name ||
1122 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
1123 strlen(counter->pmu_name)))
1126 config = strchr(counter->name, '/');
1128 if (asprintf(&new_name,
1129 "%s%s", counter->pmu_name, config) > 0) {
1130 free(counter->name);
1131 counter->name = new_name;
1134 if (asprintf(&new_name,
1135 "%s [%s]", counter->name, counter->pmu_name) > 0) {
1136 free(counter->name);
1137 counter->name = new_name;
1141 counter->uniquified_name = true;
1144 static void collect_all_aliases(struct perf_stat_config *config, struct perf_evsel *counter,
1145 void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data,
1149 struct perf_evlist *evlist = counter->evlist;
1150 struct perf_evsel *alias;
1152 alias = list_prepare_entry(counter, &(evlist->entries), node);
1153 list_for_each_entry_continue (alias, &evlist->entries, node) {
1154 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1155 alias->scale != counter->scale ||
1156 alias->cgrp != counter->cgrp ||
1157 strcmp(alias->unit, counter->unit) ||
1158 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter))
1160 alias->merged_stat = true;
1161 cb(config, alias, data, false);
1165 static bool collect_data(struct perf_stat_config *config, struct perf_evsel *counter,
1166 void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data,
1170 if (counter->merged_stat)
1172 cb(config, counter, data, true);
1173 if (config->no_merge)
1174 uniquify_event_name(counter);
1175 else if (counter->auto_merge_stats)
1176 collect_all_aliases(config, counter, cb, data);
1187 static void aggr_cb(struct perf_stat_config *config,
1188 struct perf_evsel *counter, void *data, bool first)
1190 struct aggr_data *ad = data;
1193 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1194 struct perf_counts_values *counts;
1196 s2 = config->aggr_get_id(config, perf_evsel__cpus(counter), cpu);
1201 counts = perf_counts(counter->counts, cpu, 0);
1203 * When any result is bad, make them all to give
1204 * consistent output in interval mode.
1206 if (counts->ena == 0 || counts->run == 0 ||
1207 counter->counts->scaled == -1) {
1212 ad->val += counts->val;
1213 ad->ena += counts->ena;
1214 ad->run += counts->run;
1218 static void print_aggr(struct perf_stat_config *config,
1219 struct perf_evlist *evlist,
1222 bool metric_only = config->metric_only;
1223 FILE *output = config->output;
1224 struct perf_evsel *counter;
1230 if (!(config->aggr_map || config->aggr_get_id))
1233 aggr_update_shadow(config, evlist);
1236 * With metric_only everything is on a single line.
1237 * Without each counter has its own line.
1239 for (s = 0; s < config->aggr_map->nr; s++) {
1240 struct aggr_data ad;
1241 if (prefix && metric_only)
1242 fprintf(output, "%s", prefix);
1244 ad.id = id = config->aggr_map->map[s];
1246 evlist__for_each_entry(evlist, counter) {
1247 if (is_duration_time(counter))
1250 ad.val = ad.ena = ad.run = 0;
1252 if (!collect_data(config, counter, aggr_cb, &ad))
1258 if (first && metric_only) {
1260 aggr_printout(config, counter, id, nr);
1262 if (prefix && !metric_only)
1263 fprintf(output, "%s", prefix);
1265 uval = val * counter->scale;
1266 printout(config, id, nr, counter, uval, prefix,
1267 run, ena, 1.0, &rt_stat);
1269 fputc('\n', output);
1272 fputc('\n', output);
1276 static int cmp_val(const void *a, const void *b)
1278 return ((struct perf_aggr_thread_value *)b)->val -
1279 ((struct perf_aggr_thread_value *)a)->val;
1282 static struct perf_aggr_thread_value *sort_aggr_thread(
1283 struct perf_evsel *counter,
1284 int nthreads, int ncpus,
1286 struct target *_target)
1288 int cpu, thread, i = 0;
1290 struct perf_aggr_thread_value *buf;
1292 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1296 for (thread = 0; thread < nthreads; thread++) {
1297 u64 ena = 0, run = 0, val = 0;
1299 for (cpu = 0; cpu < ncpus; cpu++) {
1300 val += perf_counts(counter->counts, cpu, thread)->val;
1301 ena += perf_counts(counter->counts, cpu, thread)->ena;
1302 run += perf_counts(counter->counts, cpu, thread)->run;
1305 uval = val * counter->scale;
1308 * Skip value 0 when enabling --per-thread globally,
1309 * otherwise too many 0 output.
1311 if (uval == 0.0 && target__has_per_thread(_target))
1314 buf[i].counter = counter;
1323 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1331 static void print_aggr_thread(struct perf_stat_config *config,
1332 struct target *_target,
1333 struct perf_evsel *counter, char *prefix)
1335 FILE *output = config->output;
1336 int nthreads = thread_map__nr(counter->threads);
1337 int ncpus = cpu_map__nr(counter->cpus);
1338 int thread, sorted_threads, id;
1339 struct perf_aggr_thread_value *buf;
1341 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
1343 perror("cannot sort aggr thread");
1347 for (thread = 0; thread < sorted_threads; thread++) {
1349 fprintf(output, "%s", prefix);
1351 id = buf[thread].id;
1353 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
1354 prefix, buf[thread].run, buf[thread].ena, 1.0,
1355 &config->stats[id]);
1357 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
1358 prefix, buf[thread].run, buf[thread].ena, 1.0,
1360 fputc('\n', output);
1367 double avg, avg_enabled, avg_running;
1370 static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
1371 struct perf_evsel *counter, void *data,
1372 bool first __maybe_unused)
1374 struct caggr_data *cd = data;
1375 struct perf_stat_evsel *ps = counter->stats;
1377 cd->avg += avg_stats(&ps->res_stats[0]);
1378 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1379 cd->avg_running += avg_stats(&ps->res_stats[2]);
1383 * Print out the results of a single counter:
1384 * aggregated counts in system-wide mode
1386 static void print_counter_aggr(struct perf_stat_config *config,
1387 struct perf_evsel *counter, char *prefix)
1389 bool metric_only = config->metric_only;
1390 FILE *output = config->output;
1392 struct caggr_data cd = { .avg = 0.0 };
1394 if (!collect_data(config, counter, counter_aggr_cb, &cd))
1397 if (prefix && !metric_only)
1398 fprintf(output, "%s", prefix);
1400 uval = cd.avg * counter->scale;
1401 printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
1404 fprintf(output, "\n");
1407 static void counter_cb(struct perf_stat_config *config __maybe_unused,
1408 struct perf_evsel *counter, void *data,
1409 bool first __maybe_unused)
1411 struct aggr_data *ad = data;
1413 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1414 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1415 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1419 * Print out the results of a single counter:
1420 * does not use aggregated count in system-wide
1422 static void print_counter(struct perf_stat_config *config,
1423 struct perf_evsel *counter, char *prefix)
1425 FILE *output = config->output;
1430 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1431 struct aggr_data ad = { .cpu = cpu };
1433 if (!collect_data(config, counter, counter_cb, &ad))
1440 fprintf(output, "%s", prefix);
1442 uval = val * counter->scale;
1443 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
1446 fputc('\n', output);
1450 static void print_no_aggr_metric(struct perf_stat_config *config,
1451 struct perf_evlist *evlist,
1456 struct perf_evsel *counter;
1460 nrcpus = evlist->cpus->nr;
1461 for (cpu = 0; cpu < nrcpus; cpu++) {
1465 fputs(prefix, config->output);
1466 evlist__for_each_entry(evlist, counter) {
1467 if (is_duration_time(counter))
1470 aggr_printout(config, counter, cpu, 0);
1473 val = perf_counts(counter->counts, cpu, 0)->val;
1474 ena = perf_counts(counter->counts, cpu, 0)->ena;
1475 run = perf_counts(counter->counts, cpu, 0)->run;
1477 uval = val * counter->scale;
1478 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
1481 fputc('\n', config->output);
1485 static int aggr_header_lens[] = {
1493 static const char *aggr_header_csv[] = {
1494 [AGGR_CORE] = "core,cpus,",
1495 [AGGR_SOCKET] = "socket,cpus",
1496 [AGGR_NONE] = "cpu,",
1497 [AGGR_THREAD] = "comm-pid,",
1501 static void print_metric_headers(struct perf_stat_config *config,
1502 struct perf_evlist *evlist,
1503 const char *prefix, bool no_indent)
1505 struct perf_stat_output_ctx out;
1506 struct perf_evsel *counter;
1507 struct outstate os = {
1508 .fh = config->output
1512 fprintf(config->output, "%s", prefix);
1514 if (!config->csv_output && !no_indent)
1515 fprintf(config->output, "%*s",
1516 aggr_header_lens[config->aggr_mode], "");
1517 if (config->csv_output) {
1518 if (config->interval)
1519 fputs("time,", config->output);
1520 fputs(aggr_header_csv[config->aggr_mode], config->output);
1523 /* Print metrics headers only */
1524 evlist__for_each_entry(evlist, counter) {
1525 if (is_duration_time(counter))
1529 out.print_metric = print_metric_header;
1530 out.new_line = new_line_metric;
1531 out.force_header = true;
1533 perf_stat__print_shadow_stats(config, counter, 0,
1539 fputc('\n', config->output);
1542 static void print_interval(struct perf_stat_config *config,
1543 struct perf_evlist *evlist,
1544 char *prefix, struct timespec *ts)
1546 bool metric_only = config->metric_only;
1547 unsigned int unit_width = config->unit_width;
1548 FILE *output = config->output;
1549 static int num_print_interval;
1551 if (config->interval_clear)
1552 puts(CONSOLE_CLEAR);
1554 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
1556 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
1557 switch (config->aggr_mode) {
1559 fprintf(output, "# time socket cpus");
1561 fprintf(output, " counts %*s events\n", unit_width, "unit");
1564 fprintf(output, "# time core cpus");
1566 fprintf(output, " counts %*s events\n", unit_width, "unit");
1569 fprintf(output, "# time CPU ");
1571 fprintf(output, " counts %*s events\n", unit_width, "unit");
1574 fprintf(output, "# time comm-pid");
1576 fprintf(output, " counts %*s events\n", unit_width, "unit");
1580 fprintf(output, "# time");
1582 fprintf(output, " counts %*s events\n", unit_width, "unit");
1588 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1589 print_metric_headers(config, evlist, " ", true);
1590 if (++num_print_interval == 25)
1591 num_print_interval = 0;
1594 static void print_header(struct perf_stat_config *config,
1595 struct target *_target,
1596 int argc, const char **argv)
1598 FILE *output = config->output;
1603 if (!config->csv_output) {
1604 fprintf(output, "\n");
1605 fprintf(output, " Performance counter stats for ");
1606 if (_target->system_wide)
1607 fprintf(output, "\'system wide");
1608 else if (_target->cpu_list)
1609 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1610 else if (!target__has_task(_target)) {
1611 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1612 for (i = 1; argv && (i < argc); i++)
1613 fprintf(output, " %s", argv[i]);
1614 } else if (_target->pid)
1615 fprintf(output, "process id \'%s", _target->pid);
1617 fprintf(output, "thread id \'%s", _target->tid);
1619 fprintf(output, "\'");
1620 if (config->run_count > 1)
1621 fprintf(output, " (%d runs)", config->run_count);
1622 fprintf(output, ":\n\n");
1626 static int get_precision(double num)
1631 return lround(ceil(-log10(num)));
1634 static void print_table(struct perf_stat_config *config,
1635 FILE *output, int precision, double avg)
1638 int idx, indent = 0;
1640 scnprintf(tmp, 64, " %17.*f", precision, avg);
1641 while (tmp[indent] == ' ')
1644 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1646 for (idx = 0; idx < config->run_count; idx++) {
1647 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1648 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1650 fprintf(output, " %17.*f (%+.*f) ",
1651 precision, run, precision, run - avg);
1653 for (h = 0; h < n; h++)
1654 fprintf(output, "#");
1656 fprintf(output, "\n");
1659 fprintf(output, "\n%*s# Final result:\n", indent, "");
1662 static double timeval2double(struct timeval *t)
1664 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1667 static void print_footer(struct perf_stat_config *config)
1669 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1670 FILE *output = config->output;
1673 if (!config->null_run)
1674 fprintf(output, "\n");
1676 if (config->run_count == 1) {
1677 fprintf(output, " %17.9f seconds time elapsed", avg);
1679 if (config->ru_display) {
1680 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1681 double ru_stime = timeval2double(&config->ru_data.ru_stime);
1683 fprintf(output, "\n\n");
1684 fprintf(output, " %17.9f seconds user\n", ru_utime);
1685 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1688 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1690 * Display at most 2 more significant
1691 * digits than the stddev inaccuracy.
1693 int precision = get_precision(sd) + 2;
1695 if (config->walltime_run_table)
1696 print_table(config, output, precision, avg);
1698 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1699 precision, avg, precision, sd);
1701 print_noise_pct(config, sd, avg);
1703 fprintf(output, "\n\n");
1705 if (config->print_free_counters_hint &&
1706 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1709 "Some events weren't counted. Try disabling the NMI watchdog:\n"
1710 " echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1712 " echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1714 if (config->print_mixed_hw_group_error)
1716 "The events in group usually have to be from "
1717 "the same PMU. Try reorganizing the group.\n");
1721 perf_evlist__print_counters(struct perf_evlist *evlist,
1722 struct perf_stat_config *config,
1723 struct target *_target,
1724 struct timespec *ts,
1725 int argc, const char **argv)
1727 bool metric_only = config->metric_only;
1728 int interval = config->interval;
1729 struct perf_evsel *counter;
1730 char buf[64], *prefix = NULL;
1733 print_interval(config, evlist, prefix = buf, ts);
1735 print_header(config, _target, argc, argv);
1738 static int num_print_iv;
1740 if (num_print_iv == 0 && !interval)
1741 print_metric_headers(config, evlist, prefix, false);
1742 if (num_print_iv++ == 25)
1744 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1745 fprintf(config->output, "%s", prefix);
1748 switch (config->aggr_mode) {
1751 print_aggr(config, evlist, prefix);
1754 evlist__for_each_entry(evlist, counter) {
1755 if (is_duration_time(counter))
1757 print_aggr_thread(config, _target, counter, prefix);
1761 evlist__for_each_entry(evlist, counter) {
1762 if (is_duration_time(counter))
1764 print_counter_aggr(config, counter, prefix);
1767 fputc('\n', config->output);
1771 print_no_aggr_metric(config, evlist, prefix);
1773 evlist__for_each_entry(evlist, counter) {
1774 if (is_duration_time(counter))
1776 print_counter(config, counter, prefix);
1785 if (!interval && !config->csv_output)
1786 print_footer(config);
1788 fflush(config->output);
1791 static void print_counters(struct timespec *ts, int argc, const char **argv)
1793 /* Do not print anything if we record to the pipe. */
1794 if (STAT_RECORD && perf_stat.data.is_pipe)
1797 perf_evlist__print_counters(evsel_list, &stat_config, &target,
1801 static volatile int signr = -1;
1803 static void skip_signal(int signo)
1805 if ((child_pid == -1) || stat_config.interval)
1810 * render child_pid harmless
1811 * won't send SIGTERM to a random
1812 * process in case of race condition
1813 * and fast PID recycling
1818 static void sig_atexit(void)
1823 * avoid race condition with SIGCHLD handler
1824 * in skip_signal() which is modifying child_pid
1825 * goal is to avoid send SIGTERM to a random
1829 sigaddset(&set, SIGCHLD);
1830 sigprocmask(SIG_BLOCK, &set, &oset);
1832 if (child_pid != -1)
1833 kill(child_pid, SIGTERM);
1835 sigprocmask(SIG_SETMASK, &oset, NULL);
1840 signal(signr, SIG_DFL);
1841 kill(getpid(), signr);
1844 static int stat__set_big_num(const struct option *opt __maybe_unused,
1845 const char *s __maybe_unused, int unset)
1847 big_num_opt = unset ? 0 : 1;
1851 static int enable_metric_only(const struct option *opt __maybe_unused,
1852 const char *s __maybe_unused, int unset)
1854 force_metric_only = true;
1855 stat_config.metric_only = !unset;
1859 static int parse_metric_groups(const struct option *opt,
1861 int unset __maybe_unused)
1863 return metricgroup__parse_groups(opt, str, &metric_events);
1866 static const struct option stat_options[] = {
1867 OPT_BOOLEAN('T', "transaction", &transaction_run,
1868 "hardware transaction statistics"),
1869 OPT_CALLBACK('e', "event", &evsel_list, "event",
1870 "event selector. use 'perf list' to list available events",
1871 parse_events_option),
1872 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1873 "event filter", parse_filter),
1874 OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
1875 "child tasks do not inherit counters"),
1876 OPT_STRING('p', "pid", &target.pid, "pid",
1877 "stat events on existing process id"),
1878 OPT_STRING('t', "tid", &target.tid, "tid",
1879 "stat events on existing thread id"),
1880 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1881 "system-wide collection from all CPUs"),
1882 OPT_BOOLEAN('g', "group", &group,
1883 "put the counters into a counter group"),
1884 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1885 OPT_INCR('v', "verbose", &verbose,
1886 "be more verbose (show counter open errors, etc)"),
1887 OPT_INTEGER('r', "repeat", &stat_config.run_count,
1888 "repeat command and print average + stddev (max: 100, forever: 0)"),
1889 OPT_BOOLEAN(0, "table", &stat_config.walltime_run_table,
1890 "display details about each run (only with -r option)"),
1891 OPT_BOOLEAN('n', "null", &stat_config.null_run,
1892 "null run - dont start any counters"),
1893 OPT_INCR('d', "detailed", &detailed_run,
1894 "detailed run - start a lot of events"),
1895 OPT_BOOLEAN('S', "sync", &sync_run,
1896 "call sync() before starting a run"),
1897 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1898 "print large numbers with thousands\' separators",
1900 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1901 "list of cpus to monitor in system-wide"),
1902 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1903 "disable CPU count aggregation", AGGR_NONE),
1904 OPT_BOOLEAN(0, "no-merge", &stat_config.no_merge, "Do not merge identical named events"),
1905 OPT_STRING('x', "field-separator", &stat_config.csv_sep, "separator",
1906 "print counts with custom separator"),
1907 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1908 "monitor event in cgroup name only", parse_cgroups),
1909 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1910 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1911 OPT_INTEGER(0, "log-fd", &output_fd,
1912 "log output to fd, instead of stderr"),
1913 OPT_STRING(0, "pre", &pre_cmd, "command",
1914 "command to run prior to the measured command"),
1915 OPT_STRING(0, "post", &post_cmd, "command",
1916 "command to run after to the measured command"),
1917 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1918 "print counts at regular interval in ms "
1919 "(overhead is possible for values <= 100ms)"),
1920 OPT_INTEGER(0, "interval-count", &stat_config.times,
1921 "print counts for fixed number of times"),
1922 OPT_BOOLEAN(0, "interval-clear", &stat_config.interval_clear,
1923 "clear screen in between new interval"),
1924 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1925 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
1926 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1927 "aggregate counts per processor socket", AGGR_SOCKET),
1928 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1929 "aggregate counts per physical processor core", AGGR_CORE),
1930 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1931 "aggregate counts per thread", AGGR_THREAD),
1932 OPT_UINTEGER('D', "delay", &stat_config.initial_delay,
1933 "ms to wait before starting measurement after program start"),
1934 OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL,
1935 "Only print computed metrics. No raw values", enable_metric_only),
1936 OPT_BOOLEAN(0, "topdown", &topdown_run,
1937 "measure topdown level 1 statistics"),
1938 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1939 "measure SMI cost"),
1940 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1941 "monitor specified metrics or metric groups (separated by ,)",
1942 parse_metric_groups),
1946 static int perf_stat__get_socket(struct perf_stat_config *config __maybe_unused,
1947 struct cpu_map *map, int cpu)
1949 return cpu_map__get_socket(map, cpu, NULL);
1952 static int perf_stat__get_core(struct perf_stat_config *config __maybe_unused,
1953 struct cpu_map *map, int cpu)
1955 return cpu_map__get_core(map, cpu, NULL);
1958 static int cpu_map__get_max(struct cpu_map *map)
1962 for (i = 0; i < map->nr; i++) {
1963 if (map->map[i] > max)
1970 static int perf_stat__get_aggr(struct perf_stat_config *config,
1971 aggr_get_id_t get_id, struct cpu_map *map, int idx)
1978 cpu = map->map[idx];
1980 if (config->cpus_aggr_map->map[cpu] == -1)
1981 config->cpus_aggr_map->map[cpu] = get_id(config, map, idx);
1983 return config->cpus_aggr_map->map[cpu];
1986 static int perf_stat__get_socket_cached(struct perf_stat_config *config,
1987 struct cpu_map *map, int idx)
1989 return perf_stat__get_aggr(config, perf_stat__get_socket, map, idx);
1992 static int perf_stat__get_core_cached(struct perf_stat_config *config,
1993 struct cpu_map *map, int idx)
1995 return perf_stat__get_aggr(config, perf_stat__get_core, map, idx);
1998 static int perf_stat_init_aggr_mode(void)
2002 switch (stat_config.aggr_mode) {
2004 if (cpu_map__build_socket_map(evsel_list->cpus, &stat_config.aggr_map)) {
2005 perror("cannot build socket map");
2008 stat_config.aggr_get_id = perf_stat__get_socket_cached;
2011 if (cpu_map__build_core_map(evsel_list->cpus, &stat_config.aggr_map)) {
2012 perror("cannot build core map");
2015 stat_config.aggr_get_id = perf_stat__get_core_cached;
2026 * The evsel_list->cpus is the base we operate on,
2027 * taking the highest cpu number to be the size of
2028 * the aggregation translate cpumap.
2030 nr = cpu_map__get_max(evsel_list->cpus);
2031 stat_config.cpus_aggr_map = cpu_map__empty_new(nr + 1);
2032 return stat_config.cpus_aggr_map ? 0 : -ENOMEM;
2035 static void perf_stat__exit_aggr_mode(void)
2037 cpu_map__put(stat_config.aggr_map);
2038 cpu_map__put(stat_config.cpus_aggr_map);
2039 stat_config.aggr_map = NULL;
2040 stat_config.cpus_aggr_map = NULL;
2043 static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2050 cpu = map->map[idx];
2052 if (cpu >= env->nr_cpus_avail)
2058 static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2060 struct perf_env *env = data;
2061 int cpu = perf_env__get_cpu(env, map, idx);
2063 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2066 static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2068 struct perf_env *env = data;
2069 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2072 int socket_id = env->cpu[cpu].socket_id;
2075 * Encode socket in upper 16 bits
2076 * core_id is relative to socket, and
2077 * we need a global id. So we combine
2080 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2086 static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2087 struct cpu_map **sockp)
2089 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2092 static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2093 struct cpu_map **corep)
2095 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2098 static int perf_stat__get_socket_file(struct perf_stat_config *config __maybe_unused,
2099 struct cpu_map *map, int idx)
2101 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2104 static int perf_stat__get_core_file(struct perf_stat_config *config __maybe_unused,
2105 struct cpu_map *map, int idx)
2107 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2110 static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2112 struct perf_env *env = &st->session->header.env;
2114 switch (stat_config.aggr_mode) {
2116 if (perf_env__build_socket_map(env, evsel_list->cpus, &stat_config.aggr_map)) {
2117 perror("cannot build socket map");
2120 stat_config.aggr_get_id = perf_stat__get_socket_file;
2123 if (perf_env__build_core_map(env, evsel_list->cpus, &stat_config.aggr_map)) {
2124 perror("cannot build core map");
2127 stat_config.aggr_get_id = perf_stat__get_core_file;
2140 static int topdown_filter_events(const char **attr, char **str, bool use_group)
2147 for (i = 0; attr[i]; i++) {
2148 if (pmu_have_event("cpu", attr[i])) {
2149 len += strlen(attr[i]) + 1;
2150 attr[i - off] = attr[i];
2154 attr[i - off] = NULL;
2156 *str = malloc(len + 1 + 2);
2166 for (i = 0; attr[i]; i++) {
2179 __weak bool arch_topdown_check_group(bool *warn)
2185 __weak void arch_topdown_group_warn(void)
2190 * Add default attributes, if there were no attributes specified or
2191 * if -d/--detailed, -d -d or -d -d -d is used:
2193 static int add_default_attributes(void)
2196 struct perf_event_attr default_attrs0[] = {
2198 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2199 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2200 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2201 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2203 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
2205 struct perf_event_attr frontend_attrs[] = {
2206 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
2208 struct perf_event_attr backend_attrs[] = {
2209 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
2211 struct perf_event_attr default_attrs1[] = {
2212 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2213 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2214 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2219 * Detailed stats (-d), covering the L1 and last level data caches:
2221 struct perf_event_attr detailed_attrs[] = {
2223 { .type = PERF_TYPE_HW_CACHE,
2225 PERF_COUNT_HW_CACHE_L1D << 0 |
2226 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2227 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2229 { .type = PERF_TYPE_HW_CACHE,
2231 PERF_COUNT_HW_CACHE_L1D << 0 |
2232 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2233 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2235 { .type = PERF_TYPE_HW_CACHE,
2237 PERF_COUNT_HW_CACHE_LL << 0 |
2238 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2239 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2241 { .type = PERF_TYPE_HW_CACHE,
2243 PERF_COUNT_HW_CACHE_LL << 0 |
2244 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2245 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2249 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2251 struct perf_event_attr very_detailed_attrs[] = {
2253 { .type = PERF_TYPE_HW_CACHE,
2255 PERF_COUNT_HW_CACHE_L1I << 0 |
2256 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2257 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2259 { .type = PERF_TYPE_HW_CACHE,
2261 PERF_COUNT_HW_CACHE_L1I << 0 |
2262 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2263 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2265 { .type = PERF_TYPE_HW_CACHE,
2267 PERF_COUNT_HW_CACHE_DTLB << 0 |
2268 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2269 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2271 { .type = PERF_TYPE_HW_CACHE,
2273 PERF_COUNT_HW_CACHE_DTLB << 0 |
2274 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2275 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2277 { .type = PERF_TYPE_HW_CACHE,
2279 PERF_COUNT_HW_CACHE_ITLB << 0 |
2280 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2281 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2283 { .type = PERF_TYPE_HW_CACHE,
2285 PERF_COUNT_HW_CACHE_ITLB << 0 |
2286 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2287 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2292 * Very, very detailed stats (-d -d -d), adding prefetch events:
2294 struct perf_event_attr very_very_detailed_attrs[] = {
2296 { .type = PERF_TYPE_HW_CACHE,
2298 PERF_COUNT_HW_CACHE_L1D << 0 |
2299 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2300 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2302 { .type = PERF_TYPE_HW_CACHE,
2304 PERF_COUNT_HW_CACHE_L1D << 0 |
2305 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2306 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2308 struct parse_events_error errinfo;
2310 /* Set attrs if no event is selected and !null_run: */
2311 if (stat_config.null_run)
2314 if (transaction_run) {
2315 /* Handle -T as -M transaction. Once platform specific metrics
2316 * support has been added to the json files, all archictures
2317 * will use this approach. To determine transaction support
2318 * on an architecture test for such a metric name.
2320 if (metricgroup__has_metric("transaction")) {
2321 struct option opt = { .value = &evsel_list };
2323 return metricgroup__parse_groups(&opt, "transaction",
2327 if (pmu_have_event("cpu", "cycles-ct") &&
2328 pmu_have_event("cpu", "el-start"))
2329 err = parse_events(evsel_list, transaction_attrs,
2332 err = parse_events(evsel_list,
2333 transaction_limited_attrs,
2336 fprintf(stderr, "Cannot set up transaction events\n");
2337 parse_events_print_error(&errinfo, transaction_attrs);
2346 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2347 fprintf(stderr, "freeze_on_smi is not supported.\n");
2352 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2353 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2359 if (pmu_have_event("msr", "aperf") &&
2360 pmu_have_event("msr", "smi")) {
2361 if (!force_metric_only)
2362 stat_config.metric_only = true;
2363 err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
2365 fprintf(stderr, "To measure SMI cost, it needs "
2366 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2367 parse_events_print_error(&errinfo, smi_cost_attrs);
2371 fprintf(stderr, "Cannot set up SMI cost events\n");
2381 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2382 stat_config.aggr_mode != AGGR_CORE) {
2383 pr_err("top down event configuration requires --per-core mode\n");
2386 stat_config.aggr_mode = AGGR_CORE;
2387 if (nr_cgroups || !target__has_cpu(&target)) {
2388 pr_err("top down event configuration requires system-wide mode (-a)\n");
2392 if (!force_metric_only)
2393 stat_config.metric_only = true;
2394 if (topdown_filter_events(topdown_attrs, &str,
2395 arch_topdown_check_group(&warn)) < 0) {
2396 pr_err("Out of memory\n");
2399 if (topdown_attrs[0] && str) {
2401 arch_topdown_group_warn();
2402 err = parse_events(evsel_list, str, &errinfo);
2405 "Cannot set up top down events %s: %d\n",
2408 parse_events_print_error(&errinfo, str);
2412 fprintf(stderr, "System does not support topdown\n");
2418 if (!evsel_list->nr_entries) {
2419 if (target__has_cpu(&target))
2420 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2422 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2424 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2425 if (perf_evlist__add_default_attrs(evsel_list,
2426 frontend_attrs) < 0)
2429 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2430 if (perf_evlist__add_default_attrs(evsel_list,
2434 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
2438 /* Detailed events get appended to the event list: */
2440 if (detailed_run < 1)
2443 /* Append detailed run extra attributes: */
2444 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
2447 if (detailed_run < 2)
2450 /* Append very detailed run extra attributes: */
2451 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
2454 if (detailed_run < 3)
2457 /* Append very, very detailed run extra attributes: */
2458 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
2461 static const char * const stat_record_usage[] = {
2462 "perf stat record [<options>]",
2466 static void init_features(struct perf_session *session)
2470 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2471 perf_header__set_feat(&session->header, feat);
2473 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2474 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2475 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2476 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2479 static int __cmd_record(int argc, const char **argv)
2481 struct perf_session *session;
2482 struct perf_data *data = &perf_stat.data;
2484 argc = parse_options(argc, argv, stat_options, stat_record_usage,
2485 PARSE_OPT_STOP_AT_NON_OPTION);
2488 data->file.path = output_name;
2490 if (stat_config.run_count != 1 || forever) {
2491 pr_err("Cannot use -r option with perf stat record.\n");
2495 session = perf_session__new(data, false, NULL);
2496 if (session == NULL) {
2497 pr_err("Perf session creation failed.\n");
2501 init_features(session);
2503 session->evlist = evsel_list;
2504 perf_stat.session = session;
2505 perf_stat.record = true;
2509 static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2510 union perf_event *event,
2511 struct perf_session *session)
2513 struct stat_round_event *stat_round = &event->stat_round;
2514 struct perf_evsel *counter;
2515 struct timespec tsh, *ts = NULL;
2516 const char **argv = session->header.env.cmdline_argv;
2517 int argc = session->header.env.nr_cmdline;
2519 evlist__for_each_entry(evsel_list, counter)
2520 perf_stat_process_counter(&stat_config, counter);
2522 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2523 update_stats(&walltime_nsecs_stats, stat_round->time);
2525 if (stat_config.interval && stat_round->time) {
2526 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2527 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
2531 print_counters(ts, argc, argv);
2536 int process_stat_config_event(struct perf_tool *tool,
2537 union perf_event *event,
2538 struct perf_session *session __maybe_unused)
2540 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2542 perf_event__read_stat_config(&stat_config, &event->stat_config);
2544 if (cpu_map__empty(st->cpus)) {
2545 if (st->aggr_mode != AGGR_UNSET)
2546 pr_warning("warning: processing task data, aggregation mode not set\n");
2550 if (st->aggr_mode != AGGR_UNSET)
2551 stat_config.aggr_mode = st->aggr_mode;
2553 if (perf_stat.data.is_pipe)
2554 perf_stat_init_aggr_mode();
2556 perf_stat_init_aggr_mode_file(st);
2561 static int set_maps(struct perf_stat *st)
2563 if (!st->cpus || !st->threads)
2566 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2569 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2571 if (perf_evlist__alloc_stats(evsel_list, true))
2574 st->maps_allocated = true;
2579 int process_thread_map_event(struct perf_tool *tool,
2580 union perf_event *event,
2581 struct perf_session *session __maybe_unused)
2583 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2586 pr_warning("Extra thread map event, ignoring.\n");
2590 st->threads = thread_map__new_event(&event->thread_map);
2594 return set_maps(st);
2598 int process_cpu_map_event(struct perf_tool *tool,
2599 union perf_event *event,
2600 struct perf_session *session __maybe_unused)
2602 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2603 struct cpu_map *cpus;
2606 pr_warning("Extra cpu map event, ignoring.\n");
2610 cpus = cpu_map__new_data(&event->cpu_map.data);
2615 return set_maps(st);
2618 static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2622 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2626 config->stats_num = nthreads;
2628 for (i = 0; i < nthreads; i++)
2629 runtime_stat__init(&config->stats[i]);
2634 static void runtime_stat_delete(struct perf_stat_config *config)
2641 for (i = 0; i < config->stats_num; i++)
2642 runtime_stat__exit(&config->stats[i]);
2644 free(config->stats);
2647 static const char * const stat_report_usage[] = {
2648 "perf stat report [<options>]",
2652 static struct perf_stat perf_stat = {
2654 .attr = perf_event__process_attr,
2655 .event_update = perf_event__process_event_update,
2656 .thread_map = process_thread_map_event,
2657 .cpu_map = process_cpu_map_event,
2658 .stat_config = process_stat_config_event,
2659 .stat = perf_event__process_stat_event,
2660 .stat_round = process_stat_round_event,
2662 .aggr_mode = AGGR_UNSET,
2665 static int __cmd_report(int argc, const char **argv)
2667 struct perf_session *session;
2668 const struct option options[] = {
2669 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2670 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2671 "aggregate counts per processor socket", AGGR_SOCKET),
2672 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2673 "aggregate counts per physical processor core", AGGR_CORE),
2674 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2675 "disable CPU count aggregation", AGGR_NONE),
2681 argc = parse_options(argc, argv, options, stat_report_usage, 0);
2683 if (!input_name || !strlen(input_name)) {
2684 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2687 input_name = "perf.data";
2690 perf_stat.data.file.path = input_name;
2691 perf_stat.data.mode = PERF_DATA_MODE_READ;
2693 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
2694 if (session == NULL)
2697 perf_stat.session = session;
2698 stat_config.output = stderr;
2699 evsel_list = session->evlist;
2701 ret = perf_session__process_events(session);
2705 perf_session__delete(session);
2709 static void setup_system_wide(int forks)
2712 * Make system wide (-a) the default target if
2713 * no target was specified and one of following
2714 * conditions is met:
2716 * - there's no workload specified
2717 * - there is workload specified but all requested
2718 * events are system wide events
2720 if (!target__none(&target))
2724 target.system_wide = true;
2726 struct perf_evsel *counter;
2728 evlist__for_each_entry(evsel_list, counter) {
2729 if (!counter->system_wide)
2733 if (evsel_list->nr_entries)
2734 target.system_wide = true;
2738 int cmd_stat(int argc, const char **argv)
2740 const char * const stat_usage[] = {
2741 "perf stat [<options>] [<command>]",
2744 int status = -EINVAL, run_idx;
2746 FILE *output = stderr;
2747 unsigned int interval, timeout;
2748 const char * const stat_subcommands[] = { "record", "report" };
2750 setlocale(LC_ALL, "");
2752 evsel_list = perf_evlist__new();
2753 if (evsel_list == NULL)
2756 parse_events__shrink_config_terms();
2757 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2758 (const char **) stat_usage,
2759 PARSE_OPT_STOP_AT_NON_OPTION);
2760 perf_stat__collect_metric_expr(evsel_list);
2761 perf_stat__init_shadow_stats();
2763 if (stat_config.csv_sep) {
2764 stat_config.csv_output = true;
2765 if (!strcmp(stat_config.csv_sep, "\\t"))
2766 stat_config.csv_sep = "\t";
2768 stat_config.csv_sep = DEFAULT_SEPARATOR;
2770 if (argc && !strncmp(argv[0], "rec", 3)) {
2771 argc = __cmd_record(argc, argv);
2774 } else if (argc && !strncmp(argv[0], "rep", 3))
2775 return __cmd_report(argc, argv);
2777 interval = stat_config.interval;
2778 timeout = stat_config.timeout;
2781 * For record command the -o is already taken care of.
2783 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
2786 if (output_name && output_fd) {
2787 fprintf(stderr, "cannot use both --output and --log-fd\n");
2788 parse_options_usage(stat_usage, stat_options, "o", 1);
2789 parse_options_usage(NULL, stat_options, "log-fd", 0);
2793 if (stat_config.metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2794 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2798 if (stat_config.metric_only && stat_config.run_count > 1) {
2799 fprintf(stderr, "--metric-only is not supported with -r\n");
2803 if (stat_config.walltime_run_table && stat_config.run_count <= 1) {
2804 fprintf(stderr, "--table is only supported with -r\n");
2805 parse_options_usage(stat_usage, stat_options, "r", 1);
2806 parse_options_usage(NULL, stat_options, "table", 0);
2810 if (output_fd < 0) {
2811 fprintf(stderr, "argument to --log-fd must be a > 0\n");
2812 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
2818 mode = append_file ? "a" : "w";
2820 output = fopen(output_name, mode);
2822 perror("failed to create output file");
2825 clock_gettime(CLOCK_REALTIME, &tm);
2826 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
2827 } else if (output_fd > 0) {
2828 mode = append_file ? "a" : "w";
2829 output = fdopen(output_fd, mode);
2831 perror("Failed opening logfd");
2836 stat_config.output = output;
2839 * let the spreadsheet do the pretty-printing
2841 if (stat_config.csv_output) {
2842 /* User explicitly passed -B? */
2843 if (big_num_opt == 1) {
2844 fprintf(stderr, "-B option not supported with -x\n");
2845 parse_options_usage(stat_usage, stat_options, "B", 1);
2846 parse_options_usage(NULL, stat_options, "x", 1);
2848 } else /* Nope, so disable big number formatting */
2849 stat_config.big_num = false;
2850 } else if (big_num_opt == 0) /* User passed --no-big-num */
2851 stat_config.big_num = false;
2853 setup_system_wide(argc);
2856 * Display user/system times only for single
2857 * run and when there's specified tracee.
2859 if ((stat_config.run_count == 1) && target__none(&target))
2860 stat_config.ru_display = true;
2862 if (stat_config.run_count < 0) {
2863 pr_err("Run count must be a positive number\n");
2864 parse_options_usage(stat_usage, stat_options, "r", 1);
2866 } else if (stat_config.run_count == 0) {
2868 stat_config.run_count = 1;
2871 if (stat_config.walltime_run_table) {
2872 stat_config.walltime_run = zalloc(stat_config.run_count * sizeof(stat_config.walltime_run[0]));
2873 if (!stat_config.walltime_run) {
2874 pr_err("failed to setup -r option");
2879 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2880 !target__has_task(&target)) {
2881 if (!target.system_wide || target.cpu_list) {
2882 fprintf(stderr, "The --per-thread option is only "
2883 "available when monitoring via -p -t -a "
2884 "options or only --per-thread.\n");
2885 parse_options_usage(NULL, stat_options, "p", 1);
2886 parse_options_usage(NULL, stat_options, "t", 1);
2892 * no_aggr, cgroup are for system-wide only
2893 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2895 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2896 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
2897 !target__has_cpu(&target)) {
2898 fprintf(stderr, "both cgroup and no-aggregation "
2899 "modes only available in system-wide mode\n");
2901 parse_options_usage(stat_usage, stat_options, "G", 1);
2902 parse_options_usage(NULL, stat_options, "A", 1);
2903 parse_options_usage(NULL, stat_options, "a", 1);
2907 if (add_default_attributes())
2910 target__validate(&target);
2912 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2913 target.per_thread = true;
2915 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
2916 if (target__has_task(&target)) {
2917 pr_err("Problems finding threads of monitor\n");
2918 parse_options_usage(stat_usage, stat_options, "p", 1);
2919 parse_options_usage(NULL, stat_options, "t", 1);
2920 } else if (target__has_cpu(&target)) {
2921 perror("failed to parse CPUs map");
2922 parse_options_usage(stat_usage, stat_options, "C", 1);
2923 parse_options_usage(NULL, stat_options, "a", 1);
2929 * Initialize thread_map with comm names,
2930 * so we could print it out on output.
2932 if (stat_config.aggr_mode == AGGR_THREAD) {
2933 thread_map__read_comms(evsel_list->threads);
2934 if (target.system_wide) {
2935 if (runtime_stat_new(&stat_config,
2936 thread_map__nr(evsel_list->threads))) {
2942 if (stat_config.times && interval)
2943 interval_count = true;
2944 else if (stat_config.times && !interval) {
2945 pr_err("interval-count option should be used together with "
2946 "interval-print.\n");
2947 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2948 parse_options_usage(stat_usage, stat_options, "I", 1);
2952 if (timeout && timeout < 100) {
2954 pr_err("timeout must be >= 10ms.\n");
2955 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2958 pr_warning("timeout < 100ms. "
2959 "The overhead percentage could be high in some cases. "
2960 "Please proceed with caution.\n");
2962 if (timeout && interval) {
2963 pr_err("timeout option is not supported with interval-print.\n");
2964 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2965 parse_options_usage(stat_usage, stat_options, "I", 1);
2969 if (perf_evlist__alloc_stats(evsel_list, interval))
2972 if (perf_stat_init_aggr_mode())
2976 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
2977 * while avoiding that older tools show confusing messages.
2979 * However for pipe sessions we need to keep it zero,
2980 * because script's perf_evsel__check_attr is triggered
2981 * by attr->sample_type != 0, and we can't run it on
2984 stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
2987 * We dont want to block the signals - that would cause
2988 * child tasks to inherit that and Ctrl-C would not work.
2989 * What we want is for Ctrl-C to work in the exec()-ed
2990 * task, but being ignored by perf stat itself:
2994 signal(SIGINT, skip_signal);
2995 signal(SIGCHLD, skip_signal);
2996 signal(SIGALRM, skip_signal);
2997 signal(SIGABRT, skip_signal);
3000 for (run_idx = 0; forever || run_idx < stat_config.run_count; run_idx++) {
3001 if (stat_config.run_count != 1 && verbose > 0)
3002 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
3005 status = run_perf_stat(argc, argv, run_idx);
3006 if (forever && status != -1) {
3007 print_counters(NULL, argc, argv);
3008 perf_stat__reset_stats();
3012 if (!forever && status != -1 && !interval)
3013 print_counters(NULL, argc, argv);
3017 * We synthesize the kernel mmap record just so that older tools
3018 * don't emit warnings about not being able to resolve symbols
3019 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3020 * a saner message about no samples being in the perf.data file.
3022 * This also serves to suppress a warning about f_header.data.size == 0
3023 * in header.c at the moment 'perf stat record' gets introduced, which
3024 * is not really needed once we start adding the stat specific PERF_RECORD_
3025 * records, but the need to suppress the kptr_restrict messages in older
3026 * tools remain -acme
3028 int fd = perf_data__fd(&perf_stat.data);
3029 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3030 process_synthesized_event,
3031 &perf_stat.session->machines.host);
3033 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3034 "older tools may produce warnings about this file\n.");
3038 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3039 pr_err("failed to write stat round event\n");
3042 if (!perf_stat.data.is_pipe) {
3043 perf_stat.session->header.data_size += perf_stat.bytes_written;
3044 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3047 perf_session__delete(perf_stat.session);
3050 perf_stat__exit_aggr_mode();
3051 perf_evlist__free_stats(evsel_list);
3053 free(stat_config.walltime_run);
3055 if (smi_cost && smi_reset)
3056 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3058 perf_evlist__delete(evsel_list);
3060 runtime_stat_delete(&stat_config);