1 // SPDX-License-Identifier: GPL-2.0-only
5 * Builtin stat command: Give a precise performance counters summary
6 * overview about any workload, CPU or specific PID.
10 $ perf stat ./hackbench 10
14 Performance counter stats for './hackbench 10':
16 1708.761321 task-clock # 11.037 CPUs utilized
17 41,190 context-switches # 0.024 M/sec
18 6,735 CPU-migrations # 0.004 M/sec
19 17,318 page-faults # 0.010 M/sec
20 5,205,202,243 cycles # 3.046 GHz
21 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
22 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
23 2,603,501,247 instructions # 0.50 insns per cycle
24 # 1.48 stalled cycles per insn
25 484,357,498 branches # 283.455 M/sec
26 6,388,934 branch-misses # 1.32% of all branches
28 0.154822978 seconds time elapsed
31 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
33 * Improvements and fixes by:
35 * Arjan van de Ven <arjan@linux.intel.com>
36 * Yanmin Zhang <yanmin.zhang@intel.com>
37 * Wu Fengguang <fengguang.wu@intel.com>
38 * Mike Galbraith <efault@gmx.de>
39 * Paul Mackerras <paulus@samba.org>
40 * Jaswinder Singh Rajput <jaswinder@kernel.org>
45 #include "util/cgroup.h"
46 #include <subcmd/parse-options.h>
47 #include "util/parse-events.h"
49 #include "util/event.h"
50 #include "util/evlist.h"
51 #include "util/evlist-hybrid.h"
52 #include "util/evsel.h"
53 #include "util/debug.h"
54 #include "util/color.h"
55 #include "util/stat.h"
56 #include "util/header.h"
57 #include "util/cpumap.h"
58 #include "util/thread_map.h"
59 #include "util/counts.h"
60 #include "util/topdown.h"
61 #include "util/session.h"
62 #include "util/tool.h"
63 #include "util/string2.h"
64 #include "util/metricgroup.h"
65 #include "util/synthetic-events.h"
66 #include "util/target.h"
67 #include "util/time-utils.h"
69 #include "util/affinity.h"
71 #include "util/bpf_counter.h"
72 #include "util/iostat.h"
73 #include "util/pmu-hybrid.h"
76 #include <linux/time64.h>
77 #include <linux/zalloc.h>
78 #include <api/fs/fs.h>
82 #include <sys/prctl.h>
86 #include <sys/types.h>
91 #include <sys/resource.h>
92 #include <linux/err.h>
94 #include <linux/ctype.h>
95 #include <perf/evlist.h>
97 #define DEFAULT_SEPARATOR " "
98 #define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
100 static void print_counters(struct timespec *ts, int argc, const char **argv);
102 /* Default events used for perf stat -T */
103 static const char *transaction_attrs = {
115 /* More limited version when the CPU does not have all events. */
116 static const char * transaction_limited_attrs = {
126 static const char * topdown_attrs[] = {
127 "topdown-total-slots",
128 "topdown-slots-retired",
129 "topdown-recovery-bubbles",
130 "topdown-fetch-bubbles",
131 "topdown-slots-issued",
135 static const char *topdown_metric_attrs[] = {
144 static const char *topdown_metric_L2_attrs[] = {
151 "topdown-br-mispredict",
157 static const char *smi_cost_attrs = {
165 static struct evlist *evsel_list;
166 static bool all_counters_use_bpf = true;
168 static struct target target = {
172 #define METRIC_ONLY_LEN 20
174 static volatile pid_t child_pid = -1;
175 static int detailed_run = 0;
176 static bool transaction_run;
177 static bool topdown_run = false;
178 static bool smi_cost = false;
179 static bool smi_reset = false;
180 static int big_num_opt = -1;
181 static bool group = false;
182 static const char *pre_cmd = NULL;
183 static const char *post_cmd = NULL;
184 static bool sync_run = false;
185 static bool forever = false;
186 static bool force_metric_only = false;
187 static struct timespec ref_time;
188 static bool append_file;
189 static bool interval_count;
190 static const char *output_name;
191 static int output_fd;
195 struct perf_data data;
196 struct perf_session *session;
198 struct perf_tool tool;
200 struct perf_cpu_map *cpus;
201 struct perf_thread_map *threads;
202 enum aggr_mode aggr_mode;
205 static struct perf_stat perf_stat;
206 #define STAT_RECORD perf_stat.record
208 static volatile int done = 0;
210 static struct perf_stat_config stat_config = {
211 .aggr_mode = AGGR_GLOBAL,
213 .unit_width = 4, /* strlen("unit") */
215 .metric_only_len = METRIC_ONLY_LEN,
216 .walltime_nsecs_stats = &walltime_nsecs_stats,
223 static bool cpus_map_matched(struct evsel *a, struct evsel *b)
225 if (!a->core.cpus && !b->core.cpus)
228 if (!a->core.cpus || !b->core.cpus)
231 if (a->core.cpus->nr != b->core.cpus->nr)
234 for (int i = 0; i < a->core.cpus->nr; i++) {
235 if (a->core.cpus->map[i] != b->core.cpus->map[i])
242 static void evlist__check_cpu_maps(struct evlist *evlist)
244 struct evsel *evsel, *pos, *leader;
247 if (evlist__has_hybrid(evlist))
248 evlist__warn_hybrid_group(evlist);
250 evlist__for_each_entry(evlist, evsel) {
251 leader = evsel->leader;
253 /* Check that leader matches cpus with each member. */
256 if (cpus_map_matched(leader, evsel))
259 /* If there's mismatch disable the group and warn user. */
260 WARN_ONCE(1, "WARNING: grouped events cpus do not match, disabling group:\n");
261 evsel__group_desc(leader, buf, sizeof(buf));
262 pr_warning(" %s\n", buf);
265 cpu_map__snprint(leader->core.cpus, buf, sizeof(buf));
266 pr_warning(" %s: %s\n", leader->name, buf);
267 cpu_map__snprint(evsel->core.cpus, buf, sizeof(buf));
268 pr_warning(" %s: %s\n", evsel->name, buf);
271 for_each_group_evsel(pos, leader) {
273 pos->core.nr_members = 0;
275 evsel->leader->core.nr_members = 0;
279 static inline void diff_timespec(struct timespec *r, struct timespec *a,
282 r->tv_sec = a->tv_sec - b->tv_sec;
283 if (a->tv_nsec < b->tv_nsec) {
284 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
287 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
291 static void perf_stat__reset_stats(void)
295 evlist__reset_stats(evsel_list);
296 perf_stat__reset_shadow_stats();
298 for (i = 0; i < stat_config.stats_num; i++)
299 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
302 static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
303 union perf_event *event,
304 struct perf_sample *sample __maybe_unused,
305 struct machine *machine __maybe_unused)
307 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
308 pr_err("failed to write perf data, error: %m\n");
312 perf_stat.bytes_written += event->header.size;
316 static int write_stat_round_event(u64 tm, u64 type)
318 return perf_event__synthesize_stat_round(NULL, tm, type,
319 process_synthesized_event,
323 #define WRITE_STAT_ROUND_EVENT(time, interval) \
324 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
326 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
328 static int evsel__write_stat_event(struct evsel *counter, u32 cpu, u32 thread,
329 struct perf_counts_values *count)
331 struct perf_sample_id *sid = SID(counter, cpu, thread);
333 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
334 process_synthesized_event, NULL);
337 static int read_single_counter(struct evsel *counter, int cpu,
338 int thread, struct timespec *rs)
340 if (counter->tool_event == PERF_TOOL_DURATION_TIME) {
341 u64 val = rs->tv_nsec + rs->tv_sec*1000000000ULL;
342 struct perf_counts_values *count =
343 perf_counts(counter->counts, cpu, thread);
344 count->ena = count->run = val;
348 return evsel__read_counter(counter, cpu, thread);
352 * Read out the results of a single counter:
353 * do not aggregate counts across CPUs in system-wide mode
355 static int read_counter_cpu(struct evsel *counter, struct timespec *rs, int cpu)
357 int nthreads = perf_thread_map__nr(evsel_list->core.threads);
360 if (!counter->supported)
363 if (counter->core.system_wide)
366 for (thread = 0; thread < nthreads; thread++) {
367 struct perf_counts_values *count;
369 count = perf_counts(counter->counts, cpu, thread);
372 * The leader's group read loads data into its group members
373 * (via evsel__read_counter()) and sets their count->loaded.
375 if (!perf_counts__is_loaded(counter->counts, cpu, thread) &&
376 read_single_counter(counter, cpu, thread, rs)) {
377 counter->counts->scaled = -1;
378 perf_counts(counter->counts, cpu, thread)->ena = 0;
379 perf_counts(counter->counts, cpu, thread)->run = 0;
383 perf_counts__set_loaded(counter->counts, cpu, thread, false);
386 if (evsel__write_stat_event(counter, cpu, thread, count)) {
387 pr_err("failed to write stat event\n");
393 fprintf(stat_config.output,
394 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
395 evsel__name(counter),
397 count->val, count->ena, count->run);
404 static int read_affinity_counters(struct timespec *rs)
406 struct evsel *counter;
407 struct affinity affinity;
410 if (all_counters_use_bpf)
413 if (affinity__setup(&affinity) < 0)
416 ncpus = perf_cpu_map__nr(evsel_list->core.all_cpus);
417 if (!target__has_cpu(&target) || target__has_per_thread(&target))
419 evlist__for_each_cpu(evsel_list, i, cpu) {
422 affinity__set(&affinity, cpu);
424 evlist__for_each_entry(evsel_list, counter) {
425 if (evsel__cpu_iter_skip(counter, cpu))
427 if (evsel__is_bpf(counter))
430 counter->err = read_counter_cpu(counter, rs,
431 counter->cpu_iter - 1);
435 affinity__cleanup(&affinity);
439 static int read_bpf_map_counters(void)
441 struct evsel *counter;
444 evlist__for_each_entry(evsel_list, counter) {
445 if (!evsel__is_bpf(counter))
448 err = bpf_counter__read(counter);
455 static void read_counters(struct timespec *rs)
457 struct evsel *counter;
459 if (!stat_config.stop_read_counter) {
460 if (read_bpf_map_counters() ||
461 read_affinity_counters(rs))
465 evlist__for_each_entry(evsel_list, counter) {
467 pr_debug("failed to read counter %s\n", counter->name);
468 if (counter->err == 0 && perf_stat_process_counter(&stat_config, counter))
469 pr_warning("failed to process counter %s\n", counter->name);
474 static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
478 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
482 config->stats_num = nthreads;
484 for (i = 0; i < nthreads; i++)
485 runtime_stat__init(&config->stats[i]);
490 static void runtime_stat_delete(struct perf_stat_config *config)
497 for (i = 0; i < config->stats_num; i++)
498 runtime_stat__exit(&config->stats[i]);
500 zfree(&config->stats);
503 static void runtime_stat_reset(struct perf_stat_config *config)
510 for (i = 0; i < config->stats_num; i++)
511 perf_stat__reset_shadow_per_stat(&config->stats[i]);
514 static void process_interval(void)
516 struct timespec ts, rs;
518 clock_gettime(CLOCK_MONOTONIC, &ts);
519 diff_timespec(&rs, &ts, &ref_time);
521 perf_stat__reset_shadow_per_stat(&rt_stat);
522 runtime_stat_reset(&stat_config);
526 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
527 pr_err("failed to write stat round event\n");
530 init_stats(&walltime_nsecs_stats);
531 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000ULL);
532 print_counters(&rs, 0, NULL);
535 static bool handle_interval(unsigned int interval, int *times)
539 if (interval_count && !(--(*times)))
545 static int enable_counters(void)
550 evlist__for_each_entry(evsel_list, evsel) {
551 if (!evsel__is_bpf(evsel))
554 err = bpf_counter__enable(evsel);
559 if (stat_config.initial_delay < 0) {
560 pr_info(EVLIST_DISABLED_MSG);
564 if (stat_config.initial_delay > 0) {
565 pr_info(EVLIST_DISABLED_MSG);
566 usleep(stat_config.initial_delay * USEC_PER_MSEC);
570 * We need to enable counters only if:
571 * - we don't have tracee (attaching to task or cpu)
572 * - we have initial delay configured
574 if (!target__none(&target) || stat_config.initial_delay) {
575 evlist__enable(evsel_list);
576 if (stat_config.initial_delay > 0)
577 pr_info(EVLIST_ENABLED_MSG);
582 static void disable_counters(void)
585 * If we don't have tracee (attaching to task or cpu), counters may
586 * still be running. To get accurate group ratios, we must stop groups
587 * from counting before reading their constituent counters.
589 if (!target__none(&target))
590 evlist__disable(evsel_list);
593 static volatile int workload_exec_errno;
596 * evlist__prepare_workload will send a SIGUSR1
597 * if the fork fails, since we asked by setting its
598 * want_signal to true.
600 static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
601 void *ucontext __maybe_unused)
603 workload_exec_errno = info->si_value.sival_int;
606 static bool evsel__should_store_id(struct evsel *counter)
608 return STAT_RECORD || counter->core.attr.read_format & PERF_FORMAT_ID;
611 static bool is_target_alive(struct target *_target,
612 struct perf_thread_map *threads)
617 if (!target__has_task(_target))
620 for (i = 0; i < threads->nr; i++) {
623 scnprintf(path, PATH_MAX, "%s/%d", procfs__mountpoint(),
624 threads->map[i].pid);
626 if (!stat(path, &st))
633 static void process_evlist(struct evlist *evlist, unsigned int interval)
635 enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED;
637 if (evlist__ctlfd_process(evlist, &cmd) > 0) {
639 case EVLIST_CTL_CMD_ENABLE:
643 case EVLIST_CTL_CMD_DISABLE:
647 case EVLIST_CTL_CMD_SNAPSHOT:
648 case EVLIST_CTL_CMD_ACK:
649 case EVLIST_CTL_CMD_UNSUPPORTED:
650 case EVLIST_CTL_CMD_EVLIST:
651 case EVLIST_CTL_CMD_STOP:
652 case EVLIST_CTL_CMD_PING:
659 static void compute_tts(struct timespec *time_start, struct timespec *time_stop,
662 int tts = *time_to_sleep;
663 struct timespec time_diff;
665 diff_timespec(&time_diff, time_stop, time_start);
667 tts -= time_diff.tv_sec * MSEC_PER_SEC +
668 time_diff.tv_nsec / NSEC_PER_MSEC;
673 *time_to_sleep = tts;
676 static int dispatch_events(bool forks, int timeout, int interval, int *times)
678 int child_exited = 0, status = 0;
679 int time_to_sleep, sleep_time;
680 struct timespec time_start, time_stop;
683 sleep_time = interval;
685 sleep_time = timeout;
689 time_to_sleep = sleep_time;
693 child_exited = waitpid(child_pid, &status, WNOHANG);
695 child_exited = !is_target_alive(&target, evsel_list->core.threads) ? 1 : 0;
700 clock_gettime(CLOCK_MONOTONIC, &time_start);
701 if (!(evlist__poll(evsel_list, time_to_sleep) > 0)) { /* poll timeout or EINTR */
702 if (timeout || handle_interval(interval, times))
704 time_to_sleep = sleep_time;
705 } else { /* fd revent */
706 process_evlist(evsel_list, interval);
707 clock_gettime(CLOCK_MONOTONIC, &time_stop);
708 compute_tts(&time_start, &time_stop, &time_to_sleep);
715 enum counter_recovery {
721 static enum counter_recovery stat_handle_error(struct evsel *counter)
725 * PPC returns ENXIO for HW counters until 2.6.37
726 * (behavior changed with commit b0a873e).
728 if (errno == EINVAL || errno == ENOSYS ||
729 errno == ENOENT || errno == EOPNOTSUPP ||
732 ui__warning("%s event is not supported by the kernel.\n",
733 evsel__name(counter));
734 counter->supported = false;
736 * errored is a sticky flag that means one of the counter's
737 * cpu event had a problem and needs to be reexamined.
739 counter->errored = true;
741 if ((counter->leader != counter) ||
742 !(counter->leader->core.nr_members > 1))
744 } else if (evsel__fallback(counter, errno, msg, sizeof(msg))) {
746 ui__warning("%s\n", msg);
747 return COUNTER_RETRY;
748 } else if (target__has_per_thread(&target) &&
749 evsel_list->core.threads &&
750 evsel_list->core.threads->err_thread != -1) {
752 * For global --per-thread case, skip current
755 if (!thread_map__remove(evsel_list->core.threads,
756 evsel_list->core.threads->err_thread)) {
757 evsel_list->core.threads->err_thread = -1;
758 return COUNTER_RETRY;
762 evsel__open_strerror(counter, &target, errno, msg, sizeof(msg));
763 ui__error("%s\n", msg);
766 kill(child_pid, SIGTERM);
767 return COUNTER_FATAL;
770 static int __run_perf_stat(int argc, const char **argv, int run_idx)
772 int interval = stat_config.interval;
773 int times = stat_config.times;
774 int timeout = stat_config.timeout;
776 unsigned long long t0, t1;
777 struct evsel *counter;
780 const bool forks = (argc > 0);
781 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
782 struct affinity affinity;
784 bool second_pass = false;
787 if (evlist__prepare_workload(evsel_list, &target, argv, is_pipe, workload_exec_failed_signal) < 0) {
788 perror("failed to prepare workload");
791 child_pid = evsel_list->workload.pid;
795 evlist__set_leader(evsel_list);
797 if (affinity__setup(&affinity) < 0)
800 evlist__for_each_entry(evsel_list, counter) {
801 if (bpf_counter__load(counter, &target))
803 if (!evsel__is_bpf(counter))
804 all_counters_use_bpf = false;
807 evlist__for_each_cpu (evsel_list, i, cpu) {
809 * bperf calls evsel__open_per_cpu() in bperf__load(), so
810 * no need to call it again here.
814 affinity__set(&affinity, cpu);
816 evlist__for_each_entry(evsel_list, counter) {
817 if (evsel__cpu_iter_skip(counter, cpu))
819 if (counter->reset_group || counter->errored)
821 if (evsel__is_bpf(counter))
824 if (create_perf_stat_counter(counter, &stat_config, &target,
825 counter->cpu_iter - 1) < 0) {
828 * Weak group failed. We cannot just undo this here
829 * because earlier CPUs might be in group mode, and the kernel
830 * doesn't support mixing group and non group reads. Defer
832 * Don't close here because we're in the wrong affinity.
834 if ((errno == EINVAL || errno == EBADF) &&
835 counter->leader != counter &&
836 counter->weak_group) {
837 evlist__reset_weak_group(evsel_list, counter, false);
838 assert(counter->reset_group);
843 switch (stat_handle_error(counter)) {
855 counter->supported = true;
861 * Now redo all the weak group after closing them,
862 * and also close errored counters.
865 evlist__for_each_cpu(evsel_list, i, cpu) {
866 affinity__set(&affinity, cpu);
867 /* First close errored or weak retry */
868 evlist__for_each_entry(evsel_list, counter) {
869 if (!counter->reset_group && !counter->errored)
871 if (evsel__cpu_iter_skip_no_inc(counter, cpu))
873 perf_evsel__close_cpu(&counter->core, counter->cpu_iter);
875 /* Now reopen weak */
876 evlist__for_each_entry(evsel_list, counter) {
877 if (!counter->reset_group && !counter->errored)
879 if (evsel__cpu_iter_skip(counter, cpu))
881 if (!counter->reset_group)
884 pr_debug2("reopening weak %s\n", evsel__name(counter));
885 if (create_perf_stat_counter(counter, &stat_config, &target,
886 counter->cpu_iter - 1) < 0) {
888 switch (stat_handle_error(counter)) {
892 goto try_again_reset;
899 counter->supported = true;
903 affinity__cleanup(&affinity);
905 evlist__for_each_entry(evsel_list, counter) {
906 if (!counter->supported) {
907 perf_evsel__free_fd(&counter->core);
911 l = strlen(counter->unit);
912 if (l > stat_config.unit_width)
913 stat_config.unit_width = l;
915 if (evsel__should_store_id(counter) &&
916 evsel__store_ids(counter, evsel_list))
920 if (evlist__apply_filters(evsel_list, &counter)) {
921 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
922 counter->filter, evsel__name(counter), errno,
923 str_error_r(errno, msg, sizeof(msg)));
928 int fd = perf_data__fd(&perf_stat.data);
931 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
933 err = perf_session__write_header(perf_stat.session, evsel_list,
940 err = perf_event__synthesize_stat_events(&stat_config, NULL, evsel_list,
941 process_synthesized_event, is_pipe);
947 * Enable counters and exec the command:
950 evlist__start_workload(evsel_list);
951 err = enable_counters();
956 clock_gettime(CLOCK_MONOTONIC, &ref_time);
958 if (interval || timeout || evlist__ctlfd_initialized(evsel_list))
959 status = dispatch_events(forks, timeout, interval, ×);
960 if (child_pid != -1) {
962 kill(child_pid, SIGTERM);
963 wait4(child_pid, &status, 0, &stat_config.ru_data);
966 if (workload_exec_errno) {
967 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
968 pr_err("Workload failed: %s\n", emsg);
972 if (WIFSIGNALED(status))
973 psignal(WTERMSIG(status), argv[0]);
975 err = enable_counters();
980 clock_gettime(CLOCK_MONOTONIC, &ref_time);
982 status = dispatch_events(forks, timeout, interval, ×);
989 if (stat_config.walltime_run_table)
990 stat_config.walltime_run[run_idx] = t1 - t0;
992 if (interval && stat_config.summary) {
993 stat_config.interval = 0;
994 stat_config.stop_read_counter = true;
995 init_stats(&walltime_nsecs_stats);
996 update_stats(&walltime_nsecs_stats, t1 - t0);
998 if (stat_config.aggr_mode == AGGR_GLOBAL)
999 evlist__save_aggr_prev_raw_counts(evsel_list);
1001 evlist__copy_prev_raw_counts(evsel_list);
1002 evlist__reset_prev_raw_counts(evsel_list);
1003 runtime_stat_reset(&stat_config);
1004 perf_stat__reset_shadow_per_stat(&rt_stat);
1006 update_stats(&walltime_nsecs_stats, t1 - t0);
1009 * Closing a group leader splits the group, and as we only disable
1010 * group leaders, results in remaining events becoming enabled. To
1011 * avoid arbitrary skew, we must read all counters before closing any
1014 read_counters(&(struct timespec) { .tv_nsec = t1-t0 });
1017 * We need to keep evsel_list alive, because it's processed
1018 * later the evsel_list will be closed after.
1021 evlist__close(evsel_list);
1023 return WEXITSTATUS(status);
1026 static int run_perf_stat(int argc, const char **argv, int run_idx)
1031 ret = system(pre_cmd);
1039 ret = __run_perf_stat(argc, argv, run_idx);
1044 ret = system(post_cmd);
1052 static void print_counters(struct timespec *ts, int argc, const char **argv)
1054 /* Do not print anything if we record to the pipe. */
1055 if (STAT_RECORD && perf_stat.data.is_pipe)
1057 if (stat_config.quiet)
1060 evlist__print_counters(evsel_list, &stat_config, &target, ts, argc, argv);
1063 static volatile int signr = -1;
1065 static void skip_signal(int signo)
1067 if ((child_pid == -1) || stat_config.interval)
1072 * render child_pid harmless
1073 * won't send SIGTERM to a random
1074 * process in case of race condition
1075 * and fast PID recycling
1080 static void sig_atexit(void)
1085 * avoid race condition with SIGCHLD handler
1086 * in skip_signal() which is modifying child_pid
1087 * goal is to avoid send SIGTERM to a random
1091 sigaddset(&set, SIGCHLD);
1092 sigprocmask(SIG_BLOCK, &set, &oset);
1094 if (child_pid != -1)
1095 kill(child_pid, SIGTERM);
1097 sigprocmask(SIG_SETMASK, &oset, NULL);
1102 signal(signr, SIG_DFL);
1103 kill(getpid(), signr);
1106 void perf_stat__set_big_num(int set)
1108 stat_config.big_num = (set != 0);
1111 void perf_stat__set_no_csv_summary(int set)
1113 stat_config.no_csv_summary = (set != 0);
1116 static int stat__set_big_num(const struct option *opt __maybe_unused,
1117 const char *s __maybe_unused, int unset)
1119 big_num_opt = unset ? 0 : 1;
1120 perf_stat__set_big_num(!unset);
1124 static int enable_metric_only(const struct option *opt __maybe_unused,
1125 const char *s __maybe_unused, int unset)
1127 force_metric_only = true;
1128 stat_config.metric_only = !unset;
1132 static int parse_metric_groups(const struct option *opt,
1134 int unset __maybe_unused)
1136 return metricgroup__parse_groups(opt, str,
1137 stat_config.metric_no_group,
1138 stat_config.metric_no_merge,
1139 &stat_config.metric_events);
1142 static int parse_control_option(const struct option *opt,
1144 int unset __maybe_unused)
1146 struct perf_stat_config *config = opt->value;
1148 return evlist__parse_control(str, &config->ctl_fd, &config->ctl_fd_ack, &config->ctl_fd_close);
1151 static int parse_stat_cgroups(const struct option *opt,
1152 const char *str, int unset)
1154 if (stat_config.cgroup_list) {
1155 pr_err("--cgroup and --for-each-cgroup cannot be used together\n");
1159 return parse_cgroups(opt, str, unset);
1162 static struct option stat_options[] = {
1163 OPT_BOOLEAN('T', "transaction", &transaction_run,
1164 "hardware transaction statistics"),
1165 OPT_CALLBACK('e', "event", &evsel_list, "event",
1166 "event selector. use 'perf list' to list available events",
1167 parse_events_option),
1168 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1169 "event filter", parse_filter),
1170 OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
1171 "child tasks do not inherit counters"),
1172 OPT_STRING('p', "pid", &target.pid, "pid",
1173 "stat events on existing process id"),
1174 OPT_STRING('t', "tid", &target.tid, "tid",
1175 "stat events on existing thread id"),
1176 #ifdef HAVE_BPF_SKEL
1177 OPT_STRING('b', "bpf-prog", &target.bpf_str, "bpf-prog-id",
1178 "stat events on existing bpf program id"),
1179 OPT_BOOLEAN(0, "bpf-counters", &target.use_bpf,
1180 "use bpf program to count events"),
1181 OPT_STRING(0, "bpf-attr-map", &target.attr_map, "attr-map-path",
1182 "path to perf_event_attr map"),
1184 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1185 "system-wide collection from all CPUs"),
1186 OPT_BOOLEAN('g', "group", &group,
1187 "put the counters into a counter group"),
1188 OPT_BOOLEAN(0, "scale", &stat_config.scale,
1189 "Use --no-scale to disable counter scaling for multiplexing"),
1190 OPT_INCR('v', "verbose", &verbose,
1191 "be more verbose (show counter open errors, etc)"),
1192 OPT_INTEGER('r', "repeat", &stat_config.run_count,
1193 "repeat command and print average + stddev (max: 100, forever: 0)"),
1194 OPT_BOOLEAN(0, "table", &stat_config.walltime_run_table,
1195 "display details about each run (only with -r option)"),
1196 OPT_BOOLEAN('n', "null", &stat_config.null_run,
1197 "null run - dont start any counters"),
1198 OPT_INCR('d', "detailed", &detailed_run,
1199 "detailed run - start a lot of events"),
1200 OPT_BOOLEAN('S', "sync", &sync_run,
1201 "call sync() before starting a run"),
1202 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1203 "print large numbers with thousands\' separators",
1205 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1206 "list of cpus to monitor in system-wide"),
1207 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1208 "disable CPU count aggregation", AGGR_NONE),
1209 OPT_BOOLEAN(0, "no-merge", &stat_config.no_merge, "Do not merge identical named events"),
1210 OPT_STRING('x', "field-separator", &stat_config.csv_sep, "separator",
1211 "print counts with custom separator"),
1212 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1213 "monitor event in cgroup name only", parse_stat_cgroups),
1214 OPT_STRING(0, "for-each-cgroup", &stat_config.cgroup_list, "name",
1215 "expand events for each cgroup"),
1216 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1217 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1218 OPT_INTEGER(0, "log-fd", &output_fd,
1219 "log output to fd, instead of stderr"),
1220 OPT_STRING(0, "pre", &pre_cmd, "command",
1221 "command to run prior to the measured command"),
1222 OPT_STRING(0, "post", &post_cmd, "command",
1223 "command to run after to the measured command"),
1224 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1225 "print counts at regular interval in ms "
1226 "(overhead is possible for values <= 100ms)"),
1227 OPT_INTEGER(0, "interval-count", &stat_config.times,
1228 "print counts for fixed number of times"),
1229 OPT_BOOLEAN(0, "interval-clear", &stat_config.interval_clear,
1230 "clear screen in between new interval"),
1231 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1232 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
1233 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1234 "aggregate counts per processor socket", AGGR_SOCKET),
1235 OPT_SET_UINT(0, "per-die", &stat_config.aggr_mode,
1236 "aggregate counts per processor die", AGGR_DIE),
1237 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1238 "aggregate counts per physical processor core", AGGR_CORE),
1239 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1240 "aggregate counts per thread", AGGR_THREAD),
1241 OPT_SET_UINT(0, "per-node", &stat_config.aggr_mode,
1242 "aggregate counts per numa node", AGGR_NODE),
1243 OPT_INTEGER('D', "delay", &stat_config.initial_delay,
1244 "ms to wait before starting measurement after program start (-1: start with events disabled)"),
1245 OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL,
1246 "Only print computed metrics. No raw values", enable_metric_only),
1247 OPT_BOOLEAN(0, "metric-no-group", &stat_config.metric_no_group,
1248 "don't group metric events, impacts multiplexing"),
1249 OPT_BOOLEAN(0, "metric-no-merge", &stat_config.metric_no_merge,
1250 "don't try to share events between metrics in a group"),
1251 OPT_BOOLEAN(0, "topdown", &topdown_run,
1252 "measure top-down statistics"),
1253 OPT_UINTEGER(0, "td-level", &stat_config.topdown_level,
1254 "Set the metrics level for the top-down statistics (0: max level)"),
1255 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1256 "measure SMI cost"),
1257 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1258 "monitor specified metrics or metric groups (separated by ,)",
1259 parse_metric_groups),
1260 OPT_BOOLEAN_FLAG(0, "all-kernel", &stat_config.all_kernel,
1261 "Configure all used events to run in kernel space.",
1262 PARSE_OPT_EXCLUSIVE),
1263 OPT_BOOLEAN_FLAG(0, "all-user", &stat_config.all_user,
1264 "Configure all used events to run in user space.",
1265 PARSE_OPT_EXCLUSIVE),
1266 OPT_BOOLEAN(0, "percore-show-thread", &stat_config.percore_show_thread,
1267 "Use with 'percore' event qualifier to show the event "
1268 "counts of one hardware thread by sum up total hardware "
1269 "threads of same physical core"),
1270 OPT_BOOLEAN(0, "summary", &stat_config.summary,
1271 "print summary for interval mode"),
1272 OPT_BOOLEAN(0, "no-csv-summary", &stat_config.no_csv_summary,
1273 "don't print 'summary' for CSV summary output"),
1274 OPT_BOOLEAN(0, "quiet", &stat_config.quiet,
1275 "don't print output (useful with record)"),
1277 OPT_CALLBACK(0, "pfm-events", &evsel_list, "event",
1278 "libpfm4 event selector. use 'perf list' to list available events",
1279 parse_libpfm_events_option),
1281 OPT_CALLBACK(0, "control", &stat_config, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
1282 "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events).\n"
1283 "\t\t\t Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
1284 "\t\t\t Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
1285 parse_control_option),
1286 OPT_CALLBACK_OPTARG(0, "iostat", &evsel_list, &stat_config, "default",
1287 "measure I/O performance metrics provided by arch/platform",
1292 static struct aggr_cpu_id perf_stat__get_socket(struct perf_stat_config *config __maybe_unused,
1293 struct perf_cpu_map *map, int cpu)
1295 return cpu_map__get_socket(map, cpu, NULL);
1298 static struct aggr_cpu_id perf_stat__get_die(struct perf_stat_config *config __maybe_unused,
1299 struct perf_cpu_map *map, int cpu)
1301 return cpu_map__get_die(map, cpu, NULL);
1304 static struct aggr_cpu_id perf_stat__get_core(struct perf_stat_config *config __maybe_unused,
1305 struct perf_cpu_map *map, int cpu)
1307 return cpu_map__get_core(map, cpu, NULL);
1310 static struct aggr_cpu_id perf_stat__get_node(struct perf_stat_config *config __maybe_unused,
1311 struct perf_cpu_map *map, int cpu)
1313 return cpu_map__get_node(map, cpu, NULL);
1316 static struct aggr_cpu_id perf_stat__get_aggr(struct perf_stat_config *config,
1317 aggr_get_id_t get_id, struct perf_cpu_map *map, int idx)
1320 struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
1325 cpu = map->map[idx];
1327 if (cpu_map__aggr_cpu_id_is_empty(config->cpus_aggr_map->map[cpu]))
1328 config->cpus_aggr_map->map[cpu] = get_id(config, map, idx);
1330 id = config->cpus_aggr_map->map[cpu];
1334 static struct aggr_cpu_id perf_stat__get_socket_cached(struct perf_stat_config *config,
1335 struct perf_cpu_map *map, int idx)
1337 return perf_stat__get_aggr(config, perf_stat__get_socket, map, idx);
1340 static struct aggr_cpu_id perf_stat__get_die_cached(struct perf_stat_config *config,
1341 struct perf_cpu_map *map, int idx)
1343 return perf_stat__get_aggr(config, perf_stat__get_die, map, idx);
1346 static struct aggr_cpu_id perf_stat__get_core_cached(struct perf_stat_config *config,
1347 struct perf_cpu_map *map, int idx)
1349 return perf_stat__get_aggr(config, perf_stat__get_core, map, idx);
1352 static struct aggr_cpu_id perf_stat__get_node_cached(struct perf_stat_config *config,
1353 struct perf_cpu_map *map, int idx)
1355 return perf_stat__get_aggr(config, perf_stat__get_node, map, idx);
1358 static bool term_percore_set(void)
1360 struct evsel *counter;
1362 evlist__for_each_entry(evsel_list, counter) {
1363 if (counter->percore)
1370 static int perf_stat_init_aggr_mode(void)
1374 switch (stat_config.aggr_mode) {
1376 if (cpu_map__build_socket_map(evsel_list->core.cpus, &stat_config.aggr_map)) {
1377 perror("cannot build socket map");
1380 stat_config.aggr_get_id = perf_stat__get_socket_cached;
1383 if (cpu_map__build_die_map(evsel_list->core.cpus, &stat_config.aggr_map)) {
1384 perror("cannot build die map");
1387 stat_config.aggr_get_id = perf_stat__get_die_cached;
1390 if (cpu_map__build_core_map(evsel_list->core.cpus, &stat_config.aggr_map)) {
1391 perror("cannot build core map");
1394 stat_config.aggr_get_id = perf_stat__get_core_cached;
1397 if (cpu_map__build_node_map(evsel_list->core.cpus, &stat_config.aggr_map)) {
1398 perror("cannot build core map");
1401 stat_config.aggr_get_id = perf_stat__get_node_cached;
1404 if (term_percore_set()) {
1405 if (cpu_map__build_core_map(evsel_list->core.cpus,
1406 &stat_config.aggr_map)) {
1407 perror("cannot build core map");
1410 stat_config.aggr_get_id = perf_stat__get_core_cached;
1421 * The evsel_list->cpus is the base we operate on,
1422 * taking the highest cpu number to be the size of
1423 * the aggregation translate cpumap.
1425 nr = perf_cpu_map__max(evsel_list->core.cpus);
1426 stat_config.cpus_aggr_map = cpu_aggr_map__empty_new(nr + 1);
1427 return stat_config.cpus_aggr_map ? 0 : -ENOMEM;
1430 static void cpu_aggr_map__delete(struct cpu_aggr_map *map)
1433 WARN_ONCE(refcount_read(&map->refcnt) != 0,
1434 "cpu_aggr_map refcnt unbalanced\n");
1439 static void cpu_aggr_map__put(struct cpu_aggr_map *map)
1441 if (map && refcount_dec_and_test(&map->refcnt))
1442 cpu_aggr_map__delete(map);
1445 static void perf_stat__exit_aggr_mode(void)
1447 cpu_aggr_map__put(stat_config.aggr_map);
1448 cpu_aggr_map__put(stat_config.cpus_aggr_map);
1449 stat_config.aggr_map = NULL;
1450 stat_config.cpus_aggr_map = NULL;
1453 static inline int perf_env__get_cpu(struct perf_env *env, struct perf_cpu_map *map, int idx)
1460 cpu = map->map[idx];
1462 if (cpu >= env->nr_cpus_avail)
1468 static struct aggr_cpu_id perf_env__get_socket(struct perf_cpu_map *map, int idx, void *data)
1470 struct perf_env *env = data;
1471 int cpu = perf_env__get_cpu(env, map, idx);
1472 struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
1475 id.socket = env->cpu[cpu].socket_id;
1480 static struct aggr_cpu_id perf_env__get_die(struct perf_cpu_map *map, int idx, void *data)
1482 struct perf_env *env = data;
1483 struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
1484 int cpu = perf_env__get_cpu(env, map, idx);
1488 * die_id is relative to socket, so start
1489 * with the socket ID and then add die to
1492 id.socket = env->cpu[cpu].socket_id;
1493 id.die = env->cpu[cpu].die_id;
1499 static struct aggr_cpu_id perf_env__get_core(struct perf_cpu_map *map, int idx, void *data)
1501 struct perf_env *env = data;
1502 struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
1503 int cpu = perf_env__get_cpu(env, map, idx);
1507 * core_id is relative to socket and die,
1508 * we need a global id. So we set
1509 * socket, die id and core id
1511 id.socket = env->cpu[cpu].socket_id;
1512 id.die = env->cpu[cpu].die_id;
1513 id.core = env->cpu[cpu].core_id;
1519 static struct aggr_cpu_id perf_env__get_node(struct perf_cpu_map *map, int idx, void *data)
1521 int cpu = perf_env__get_cpu(data, map, idx);
1522 struct aggr_cpu_id id = cpu_map__empty_aggr_cpu_id();
1524 id.node = perf_env__numa_node(data, cpu);
1528 static int perf_env__build_socket_map(struct perf_env *env, struct perf_cpu_map *cpus,
1529 struct cpu_aggr_map **sockp)
1531 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1534 static int perf_env__build_die_map(struct perf_env *env, struct perf_cpu_map *cpus,
1535 struct cpu_aggr_map **diep)
1537 return cpu_map__build_map(cpus, diep, perf_env__get_die, env);
1540 static int perf_env__build_core_map(struct perf_env *env, struct perf_cpu_map *cpus,
1541 struct cpu_aggr_map **corep)
1543 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1546 static int perf_env__build_node_map(struct perf_env *env, struct perf_cpu_map *cpus,
1547 struct cpu_aggr_map **nodep)
1549 return cpu_map__build_map(cpus, nodep, perf_env__get_node, env);
1552 static struct aggr_cpu_id perf_stat__get_socket_file(struct perf_stat_config *config __maybe_unused,
1553 struct perf_cpu_map *map, int idx)
1555 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1557 static struct aggr_cpu_id perf_stat__get_die_file(struct perf_stat_config *config __maybe_unused,
1558 struct perf_cpu_map *map, int idx)
1560 return perf_env__get_die(map, idx, &perf_stat.session->header.env);
1563 static struct aggr_cpu_id perf_stat__get_core_file(struct perf_stat_config *config __maybe_unused,
1564 struct perf_cpu_map *map, int idx)
1566 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1569 static struct aggr_cpu_id perf_stat__get_node_file(struct perf_stat_config *config __maybe_unused,
1570 struct perf_cpu_map *map, int idx)
1572 return perf_env__get_node(map, idx, &perf_stat.session->header.env);
1575 static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1577 struct perf_env *env = &st->session->header.env;
1579 switch (stat_config.aggr_mode) {
1581 if (perf_env__build_socket_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) {
1582 perror("cannot build socket map");
1585 stat_config.aggr_get_id = perf_stat__get_socket_file;
1588 if (perf_env__build_die_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) {
1589 perror("cannot build die map");
1592 stat_config.aggr_get_id = perf_stat__get_die_file;
1595 if (perf_env__build_core_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) {
1596 perror("cannot build core map");
1599 stat_config.aggr_get_id = perf_stat__get_core_file;
1602 if (perf_env__build_node_map(env, evsel_list->core.cpus, &stat_config.aggr_map)) {
1603 perror("cannot build core map");
1606 stat_config.aggr_get_id = perf_stat__get_node_file;
1620 * Add default attributes, if there were no attributes specified or
1621 * if -d/--detailed, -d -d or -d -d -d is used:
1623 static int add_default_attributes(void)
1626 struct perf_event_attr default_attrs0[] = {
1628 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1629 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1630 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1631 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1633 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
1635 struct perf_event_attr frontend_attrs[] = {
1636 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
1638 struct perf_event_attr backend_attrs[] = {
1639 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
1641 struct perf_event_attr default_attrs1[] = {
1642 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1643 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1644 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1647 struct perf_event_attr default_sw_attrs[] = {
1648 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1649 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1650 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1651 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1655 * Detailed stats (-d), covering the L1 and last level data caches:
1657 struct perf_event_attr detailed_attrs[] = {
1659 { .type = PERF_TYPE_HW_CACHE,
1661 PERF_COUNT_HW_CACHE_L1D << 0 |
1662 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1663 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1665 { .type = PERF_TYPE_HW_CACHE,
1667 PERF_COUNT_HW_CACHE_L1D << 0 |
1668 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1669 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1671 { .type = PERF_TYPE_HW_CACHE,
1673 PERF_COUNT_HW_CACHE_LL << 0 |
1674 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1675 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1677 { .type = PERF_TYPE_HW_CACHE,
1679 PERF_COUNT_HW_CACHE_LL << 0 |
1680 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1681 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1685 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1687 struct perf_event_attr very_detailed_attrs[] = {
1689 { .type = PERF_TYPE_HW_CACHE,
1691 PERF_COUNT_HW_CACHE_L1I << 0 |
1692 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1693 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1695 { .type = PERF_TYPE_HW_CACHE,
1697 PERF_COUNT_HW_CACHE_L1I << 0 |
1698 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1699 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1701 { .type = PERF_TYPE_HW_CACHE,
1703 PERF_COUNT_HW_CACHE_DTLB << 0 |
1704 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1705 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1707 { .type = PERF_TYPE_HW_CACHE,
1709 PERF_COUNT_HW_CACHE_DTLB << 0 |
1710 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1711 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1713 { .type = PERF_TYPE_HW_CACHE,
1715 PERF_COUNT_HW_CACHE_ITLB << 0 |
1716 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1717 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1719 { .type = PERF_TYPE_HW_CACHE,
1721 PERF_COUNT_HW_CACHE_ITLB << 0 |
1722 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1723 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1728 * Very, very detailed stats (-d -d -d), adding prefetch events:
1730 struct perf_event_attr very_very_detailed_attrs[] = {
1732 { .type = PERF_TYPE_HW_CACHE,
1734 PERF_COUNT_HW_CACHE_L1D << 0 |
1735 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1736 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1738 { .type = PERF_TYPE_HW_CACHE,
1740 PERF_COUNT_HW_CACHE_L1D << 0 |
1741 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1742 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1744 struct parse_events_error errinfo;
1746 /* Set attrs if no event is selected and !null_run: */
1747 if (stat_config.null_run)
1750 bzero(&errinfo, sizeof(errinfo));
1751 if (transaction_run) {
1752 /* Handle -T as -M transaction. Once platform specific metrics
1753 * support has been added to the json files, all architectures
1754 * will use this approach. To determine transaction support
1755 * on an architecture test for such a metric name.
1757 if (metricgroup__has_metric("transaction")) {
1758 struct option opt = { .value = &evsel_list };
1760 return metricgroup__parse_groups(&opt, "transaction",
1761 stat_config.metric_no_group,
1762 stat_config.metric_no_merge,
1763 &stat_config.metric_events);
1766 if (pmu_have_event("cpu", "cycles-ct") &&
1767 pmu_have_event("cpu", "el-start"))
1768 err = parse_events(evsel_list, transaction_attrs,
1771 err = parse_events(evsel_list,
1772 transaction_limited_attrs,
1775 fprintf(stderr, "Cannot set up transaction events\n");
1776 parse_events_print_error(&errinfo, transaction_attrs);
1785 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
1786 fprintf(stderr, "freeze_on_smi is not supported.\n");
1791 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
1792 fprintf(stderr, "Failed to set freeze_on_smi.\n");
1798 if (pmu_have_event("msr", "aperf") &&
1799 pmu_have_event("msr", "smi")) {
1800 if (!force_metric_only)
1801 stat_config.metric_only = true;
1802 err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
1804 fprintf(stderr, "To measure SMI cost, it needs "
1805 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
1806 parse_events_print_error(&errinfo, smi_cost_attrs);
1810 parse_events_print_error(&errinfo, smi_cost_attrs);
1811 fprintf(stderr, "Cannot set up SMI cost events\n");
1818 const char **metric_attrs = topdown_metric_attrs;
1819 unsigned int max_level = 1;
1823 if (!force_metric_only)
1824 stat_config.metric_only = true;
1826 if (pmu_have_event("cpu", topdown_metric_L2_attrs[5])) {
1827 metric_attrs = topdown_metric_L2_attrs;
1831 if (stat_config.topdown_level > max_level) {
1832 pr_err("Invalid top-down metrics level. The max level is %u.\n", max_level);
1834 } else if (!stat_config.topdown_level)
1835 stat_config.topdown_level = max_level;
1837 if (topdown_filter_events(metric_attrs, &str, 1) < 0) {
1838 pr_err("Out of memory\n");
1841 if (metric_attrs[0] && str) {
1842 if (!stat_config.interval && !stat_config.metric_only) {
1843 fprintf(stat_config.output,
1844 "Topdown accuracy may decrease when measuring long periods.\n"
1845 "Please print the result regularly, e.g. -I1000\n");
1852 if (stat_config.aggr_mode != AGGR_GLOBAL &&
1853 stat_config.aggr_mode != AGGR_CORE) {
1854 pr_err("top down event configuration requires --per-core mode\n");
1857 stat_config.aggr_mode = AGGR_CORE;
1858 if (nr_cgroups || !target__has_cpu(&target)) {
1859 pr_err("top down event configuration requires system-wide mode (-a)\n");
1863 if (topdown_filter_events(topdown_attrs, &str,
1864 arch_topdown_check_group(&warn)) < 0) {
1865 pr_err("Out of memory\n");
1868 if (topdown_attrs[0] && str) {
1870 arch_topdown_group_warn();
1872 err = parse_events(evsel_list, str, &errinfo);
1875 "Cannot set up top down events %s: %d\n",
1877 parse_events_print_error(&errinfo, str);
1882 fprintf(stderr, "System does not support topdown\n");
1888 if (!evsel_list->core.nr_entries) {
1889 if (perf_pmu__has_hybrid()) {
1890 const char *hybrid_str = "cycles,instructions,branches,branch-misses";
1892 if (target__has_cpu(&target))
1893 default_sw_attrs[0].config = PERF_COUNT_SW_CPU_CLOCK;
1895 if (evlist__add_default_attrs(evsel_list,
1896 default_sw_attrs) < 0) {
1900 err = parse_events(evsel_list, hybrid_str, &errinfo);
1903 "Cannot set up hybrid events %s: %d\n",
1905 parse_events_print_error(&errinfo, hybrid_str);
1911 if (target__has_cpu(&target))
1912 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
1914 if (evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
1916 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
1917 if (evlist__add_default_attrs(evsel_list, frontend_attrs) < 0)
1920 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
1921 if (evlist__add_default_attrs(evsel_list, backend_attrs) < 0)
1924 if (evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
1927 if (arch_evlist__add_default_attrs(evsel_list) < 0)
1931 /* Detailed events get appended to the event list: */
1933 if (detailed_run < 1)
1936 /* Append detailed run extra attributes: */
1937 if (evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
1940 if (detailed_run < 2)
1943 /* Append very detailed run extra attributes: */
1944 if (evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
1947 if (detailed_run < 3)
1950 /* Append very, very detailed run extra attributes: */
1951 return evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
1954 static const char * const stat_record_usage[] = {
1955 "perf stat record [<options>]",
1959 static void init_features(struct perf_session *session)
1963 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
1964 perf_header__set_feat(&session->header, feat);
1966 perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT);
1967 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
1968 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
1969 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1970 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1973 static int __cmd_record(int argc, const char **argv)
1975 struct perf_session *session;
1976 struct perf_data *data = &perf_stat.data;
1978 argc = parse_options(argc, argv, stat_options, stat_record_usage,
1979 PARSE_OPT_STOP_AT_NON_OPTION);
1982 data->path = output_name;
1984 if (stat_config.run_count != 1 || forever) {
1985 pr_err("Cannot use -r option with perf stat record.\n");
1989 session = perf_session__new(data, false, NULL);
1990 if (IS_ERR(session)) {
1991 pr_err("Perf session creation failed\n");
1992 return PTR_ERR(session);
1995 init_features(session);
1997 session->evlist = evsel_list;
1998 perf_stat.session = session;
1999 perf_stat.record = true;
2003 static int process_stat_round_event(struct perf_session *session,
2004 union perf_event *event)
2006 struct perf_record_stat_round *stat_round = &event->stat_round;
2007 struct evsel *counter;
2008 struct timespec tsh, *ts = NULL;
2009 const char **argv = session->header.env.cmdline_argv;
2010 int argc = session->header.env.nr_cmdline;
2012 evlist__for_each_entry(evsel_list, counter)
2013 perf_stat_process_counter(&stat_config, counter);
2015 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2016 update_stats(&walltime_nsecs_stats, stat_round->time);
2018 if (stat_config.interval && stat_round->time) {
2019 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2020 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
2024 print_counters(ts, argc, argv);
2029 int process_stat_config_event(struct perf_session *session,
2030 union perf_event *event)
2032 struct perf_tool *tool = session->tool;
2033 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2035 perf_event__read_stat_config(&stat_config, &event->stat_config);
2037 if (perf_cpu_map__empty(st->cpus)) {
2038 if (st->aggr_mode != AGGR_UNSET)
2039 pr_warning("warning: processing task data, aggregation mode not set\n");
2043 if (st->aggr_mode != AGGR_UNSET)
2044 stat_config.aggr_mode = st->aggr_mode;
2046 if (perf_stat.data.is_pipe)
2047 perf_stat_init_aggr_mode();
2049 perf_stat_init_aggr_mode_file(st);
2054 static int set_maps(struct perf_stat *st)
2056 if (!st->cpus || !st->threads)
2059 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2062 perf_evlist__set_maps(&evsel_list->core, st->cpus, st->threads);
2064 if (evlist__alloc_stats(evsel_list, true))
2067 st->maps_allocated = true;
2072 int process_thread_map_event(struct perf_session *session,
2073 union perf_event *event)
2075 struct perf_tool *tool = session->tool;
2076 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2079 pr_warning("Extra thread map event, ignoring.\n");
2083 st->threads = thread_map__new_event(&event->thread_map);
2087 return set_maps(st);
2091 int process_cpu_map_event(struct perf_session *session,
2092 union perf_event *event)
2094 struct perf_tool *tool = session->tool;
2095 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2096 struct perf_cpu_map *cpus;
2099 pr_warning("Extra cpu map event, ignoring.\n");
2103 cpus = cpu_map__new_data(&event->cpu_map.data);
2108 return set_maps(st);
2111 static const char * const stat_report_usage[] = {
2112 "perf stat report [<options>]",
2116 static struct perf_stat perf_stat = {
2118 .attr = perf_event__process_attr,
2119 .event_update = perf_event__process_event_update,
2120 .thread_map = process_thread_map_event,
2121 .cpu_map = process_cpu_map_event,
2122 .stat_config = process_stat_config_event,
2123 .stat = perf_event__process_stat_event,
2124 .stat_round = process_stat_round_event,
2126 .aggr_mode = AGGR_UNSET,
2129 static int __cmd_report(int argc, const char **argv)
2131 struct perf_session *session;
2132 const struct option options[] = {
2133 OPT_STRING('i', "input", &input_name, "file", "input file name"),
2134 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2135 "aggregate counts per processor socket", AGGR_SOCKET),
2136 OPT_SET_UINT(0, "per-die", &perf_stat.aggr_mode,
2137 "aggregate counts per processor die", AGGR_DIE),
2138 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2139 "aggregate counts per physical processor core", AGGR_CORE),
2140 OPT_SET_UINT(0, "per-node", &perf_stat.aggr_mode,
2141 "aggregate counts per numa node", AGGR_NODE),
2142 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2143 "disable CPU count aggregation", AGGR_NONE),
2149 argc = parse_options(argc, argv, options, stat_report_usage, 0);
2151 if (!input_name || !strlen(input_name)) {
2152 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2155 input_name = "perf.data";
2158 perf_stat.data.path = input_name;
2159 perf_stat.data.mode = PERF_DATA_MODE_READ;
2161 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
2162 if (IS_ERR(session))
2163 return PTR_ERR(session);
2165 perf_stat.session = session;
2166 stat_config.output = stderr;
2167 evsel_list = session->evlist;
2169 ret = perf_session__process_events(session);
2173 perf_session__delete(session);
2177 static void setup_system_wide(int forks)
2180 * Make system wide (-a) the default target if
2181 * no target was specified and one of following
2182 * conditions is met:
2184 * - there's no workload specified
2185 * - there is workload specified but all requested
2186 * events are system wide events
2188 if (!target__none(&target))
2192 target.system_wide = true;
2194 struct evsel *counter;
2196 evlist__for_each_entry(evsel_list, counter) {
2197 if (!counter->core.system_wide &&
2198 strcmp(counter->name, "duration_time")) {
2203 if (evsel_list->core.nr_entries)
2204 target.system_wide = true;
2208 int cmd_stat(int argc, const char **argv)
2210 const char * const stat_usage[] = {
2211 "perf stat [<options>] [<command>]",
2214 int status = -EINVAL, run_idx, err;
2216 FILE *output = stderr;
2217 unsigned int interval, timeout;
2218 const char * const stat_subcommands[] = { "record", "report" };
2219 char errbuf[BUFSIZ];
2221 setlocale(LC_ALL, "");
2223 evsel_list = evlist__new();
2224 if (evsel_list == NULL)
2227 parse_events__shrink_config_terms();
2229 /* String-parsing callback-based options would segfault when negated */
2230 set_option_flag(stat_options, 'e', "event", PARSE_OPT_NONEG);
2231 set_option_flag(stat_options, 'M', "metrics", PARSE_OPT_NONEG);
2232 set_option_flag(stat_options, 'G', "cgroup", PARSE_OPT_NONEG);
2234 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2235 (const char **) stat_usage,
2236 PARSE_OPT_STOP_AT_NON_OPTION);
2237 perf_stat__collect_metric_expr(evsel_list);
2238 perf_stat__init_shadow_stats();
2240 if (stat_config.csv_sep) {
2241 stat_config.csv_output = true;
2242 if (!strcmp(stat_config.csv_sep, "\\t"))
2243 stat_config.csv_sep = "\t";
2245 stat_config.csv_sep = DEFAULT_SEPARATOR;
2247 if (argc && !strncmp(argv[0], "rec", 3)) {
2248 argc = __cmd_record(argc, argv);
2251 } else if (argc && !strncmp(argv[0], "rep", 3))
2252 return __cmd_report(argc, argv);
2254 interval = stat_config.interval;
2255 timeout = stat_config.timeout;
2258 * For record command the -o is already taken care of.
2260 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
2263 if (output_name && output_fd) {
2264 fprintf(stderr, "cannot use both --output and --log-fd\n");
2265 parse_options_usage(stat_usage, stat_options, "o", 1);
2266 parse_options_usage(NULL, stat_options, "log-fd", 0);
2270 if (stat_config.metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2271 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2275 if (stat_config.metric_only && stat_config.run_count > 1) {
2276 fprintf(stderr, "--metric-only is not supported with -r\n");
2280 if (stat_config.walltime_run_table && stat_config.run_count <= 1) {
2281 fprintf(stderr, "--table is only supported with -r\n");
2282 parse_options_usage(stat_usage, stat_options, "r", 1);
2283 parse_options_usage(NULL, stat_options, "table", 0);
2287 if (output_fd < 0) {
2288 fprintf(stderr, "argument to --log-fd must be a > 0\n");
2289 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
2293 if (!output && !stat_config.quiet) {
2295 mode = append_file ? "a" : "w";
2297 output = fopen(output_name, mode);
2299 perror("failed to create output file");
2302 clock_gettime(CLOCK_REALTIME, &tm);
2303 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
2304 } else if (output_fd > 0) {
2305 mode = append_file ? "a" : "w";
2306 output = fdopen(output_fd, mode);
2308 perror("Failed opening logfd");
2313 stat_config.output = output;
2316 * let the spreadsheet do the pretty-printing
2318 if (stat_config.csv_output) {
2319 /* User explicitly passed -B? */
2320 if (big_num_opt == 1) {
2321 fprintf(stderr, "-B option not supported with -x\n");
2322 parse_options_usage(stat_usage, stat_options, "B", 1);
2323 parse_options_usage(NULL, stat_options, "x", 1);
2325 } else /* Nope, so disable big number formatting */
2326 stat_config.big_num = false;
2327 } else if (big_num_opt == 0) /* User passed --no-big-num */
2328 stat_config.big_num = false;
2330 err = target__validate(&target);
2332 target__strerror(&target, err, errbuf, BUFSIZ);
2333 pr_warning("%s\n", errbuf);
2336 setup_system_wide(argc);
2339 * Display user/system times only for single
2340 * run and when there's specified tracee.
2342 if ((stat_config.run_count == 1) && target__none(&target))
2343 stat_config.ru_display = true;
2345 if (stat_config.run_count < 0) {
2346 pr_err("Run count must be a positive number\n");
2347 parse_options_usage(stat_usage, stat_options, "r", 1);
2349 } else if (stat_config.run_count == 0) {
2351 stat_config.run_count = 1;
2354 if (stat_config.walltime_run_table) {
2355 stat_config.walltime_run = zalloc(stat_config.run_count * sizeof(stat_config.walltime_run[0]));
2356 if (!stat_config.walltime_run) {
2357 pr_err("failed to setup -r option");
2362 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2363 !target__has_task(&target)) {
2364 if (!target.system_wide || target.cpu_list) {
2365 fprintf(stderr, "The --per-thread option is only "
2366 "available when monitoring via -p -t -a "
2367 "options or only --per-thread.\n");
2368 parse_options_usage(NULL, stat_options, "p", 1);
2369 parse_options_usage(NULL, stat_options, "t", 1);
2375 * no_aggr, cgroup are for system-wide only
2376 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2378 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2379 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
2380 !target__has_cpu(&target)) {
2381 fprintf(stderr, "both cgroup and no-aggregation "
2382 "modes only available in system-wide mode\n");
2384 parse_options_usage(stat_usage, stat_options, "G", 1);
2385 parse_options_usage(NULL, stat_options, "A", 1);
2386 parse_options_usage(NULL, stat_options, "a", 1);
2390 if (stat_config.iostat_run) {
2391 status = iostat_prepare(evsel_list, &stat_config);
2394 if (iostat_mode == IOSTAT_LIST) {
2395 iostat_list(evsel_list, &stat_config);
2398 iostat_list(evsel_list, &stat_config);
2401 if (add_default_attributes())
2404 if (stat_config.cgroup_list) {
2405 if (nr_cgroups > 0) {
2406 pr_err("--cgroup and --for-each-cgroup cannot be used together\n");
2407 parse_options_usage(stat_usage, stat_options, "G", 1);
2408 parse_options_usage(NULL, stat_options, "for-each-cgroup", 0);
2412 if (evlist__expand_cgroup(evsel_list, stat_config.cgroup_list,
2413 &stat_config.metric_events, true) < 0) {
2414 parse_options_usage(stat_usage, stat_options,
2415 "for-each-cgroup", 0);
2420 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2421 target.per_thread = true;
2423 if (evlist__create_maps(evsel_list, &target) < 0) {
2424 if (target__has_task(&target)) {
2425 pr_err("Problems finding threads of monitor\n");
2426 parse_options_usage(stat_usage, stat_options, "p", 1);
2427 parse_options_usage(NULL, stat_options, "t", 1);
2428 } else if (target__has_cpu(&target)) {
2429 perror("failed to parse CPUs map");
2430 parse_options_usage(stat_usage, stat_options, "C", 1);
2431 parse_options_usage(NULL, stat_options, "a", 1);
2436 evlist__check_cpu_maps(evsel_list);
2438 if (perf_pmu__has_hybrid())
2439 stat_config.no_merge = true;
2442 * Initialize thread_map with comm names,
2443 * so we could print it out on output.
2445 if (stat_config.aggr_mode == AGGR_THREAD) {
2446 thread_map__read_comms(evsel_list->core.threads);
2447 if (target.system_wide) {
2448 if (runtime_stat_new(&stat_config,
2449 perf_thread_map__nr(evsel_list->core.threads))) {
2455 if (stat_config.aggr_mode == AGGR_NODE)
2456 cpu__setup_cpunode_map();
2458 if (stat_config.times && interval)
2459 interval_count = true;
2460 else if (stat_config.times && !interval) {
2461 pr_err("interval-count option should be used together with "
2462 "interval-print.\n");
2463 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2464 parse_options_usage(stat_usage, stat_options, "I", 1);
2468 if (timeout && timeout < 100) {
2470 pr_err("timeout must be >= 10ms.\n");
2471 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2474 pr_warning("timeout < 100ms. "
2475 "The overhead percentage could be high in some cases. "
2476 "Please proceed with caution.\n");
2478 if (timeout && interval) {
2479 pr_err("timeout option is not supported with interval-print.\n");
2480 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2481 parse_options_usage(stat_usage, stat_options, "I", 1);
2485 if (evlist__alloc_stats(evsel_list, interval))
2488 if (perf_stat_init_aggr_mode())
2492 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
2493 * while avoiding that older tools show confusing messages.
2495 * However for pipe sessions we need to keep it zero,
2496 * because script's perf_evsel__check_attr is triggered
2497 * by attr->sample_type != 0, and we can't run it on
2500 stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
2503 * We dont want to block the signals - that would cause
2504 * child tasks to inherit that and Ctrl-C would not work.
2505 * What we want is for Ctrl-C to work in the exec()-ed
2506 * task, but being ignored by perf stat itself:
2510 signal(SIGINT, skip_signal);
2511 signal(SIGCHLD, skip_signal);
2512 signal(SIGALRM, skip_signal);
2513 signal(SIGABRT, skip_signal);
2515 if (evlist__initialize_ctlfd(evsel_list, stat_config.ctl_fd, stat_config.ctl_fd_ack))
2519 for (run_idx = 0; forever || run_idx < stat_config.run_count; run_idx++) {
2520 if (stat_config.run_count != 1 && verbose > 0)
2521 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2525 evlist__reset_prev_raw_counts(evsel_list);
2527 status = run_perf_stat(argc, argv, run_idx);
2528 if (forever && status != -1 && !interval) {
2529 print_counters(NULL, argc, argv);
2530 perf_stat__reset_stats();
2534 if (!forever && status != -1 && (!interval || stat_config.summary))
2535 print_counters(NULL, argc, argv);
2537 evlist__finalize_ctlfd(evsel_list);
2541 * We synthesize the kernel mmap record just so that older tools
2542 * don't emit warnings about not being able to resolve symbols
2543 * due to /proc/sys/kernel/kptr_restrict settings and instead provide
2544 * a saner message about no samples being in the perf.data file.
2546 * This also serves to suppress a warning about f_header.data.size == 0
2547 * in header.c at the moment 'perf stat record' gets introduced, which
2548 * is not really needed once we start adding the stat specific PERF_RECORD_
2549 * records, but the need to suppress the kptr_restrict messages in older
2550 * tools remain -acme
2552 int fd = perf_data__fd(&perf_stat.data);
2554 err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2555 process_synthesized_event,
2556 &perf_stat.session->machines.host);
2558 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2559 "older tools may produce warnings about this file\n.");
2563 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2564 pr_err("failed to write stat round event\n");
2567 if (!perf_stat.data.is_pipe) {
2568 perf_stat.session->header.data_size += perf_stat.bytes_written;
2569 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2572 evlist__close(evsel_list);
2573 perf_session__delete(perf_stat.session);
2576 perf_stat__exit_aggr_mode();
2577 evlist__free_stats(evsel_list);
2579 if (stat_config.iostat_run)
2580 iostat_release(evsel_list);
2582 zfree(&stat_config.walltime_run);
2584 if (smi_cost && smi_reset)
2585 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
2587 evlist__delete(evsel_list);
2589 metricgroup__rblist_exit(&stat_config.metric_events);
2590 runtime_stat_delete(&stat_config);
2591 evlist__close_control(stat_config.ctl_fd, stat_config.ctl_fd_ack, &stat_config.ctl_fd_close);