Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1 | /* |
Ingo Molnar | bf9e187 | 2009-06-02 23:37:05 +0200 | [diff] [blame] | 2 | * builtin-stat.c |
| 3 | * |
| 4 | * Builtin stat command: Give a precise performance counters summary |
| 5 | * overview about any workload, CPU or specific PID. |
| 6 | * |
| 7 | * Sample output: |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 8 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 9 | $ perf stat ./hackbench 10 |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 10 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 11 | Time: 0.118 |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 12 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 13 | Performance counter stats for './hackbench 10': |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 14 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 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 |
| 26 | |
| 27 | 0.154822978 seconds time elapsed |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 28 | |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 29 | * |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 30 | * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com> |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 31 | * |
| 32 | * Improvements and fixes by: |
| 33 | * |
| 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> |
Jaswinder Singh Rajput | 6e750a8f | 2009-06-27 03:02:07 +0530 | [diff] [blame] | 39 | * Jaswinder Singh Rajput <jaswinder@kernel.org> |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 40 | * |
| 41 | * Released under the GPL v2. (and only v2, not any later version) |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 42 | */ |
| 43 | |
Peter Zijlstra | 1a482f3 | 2009-05-23 18:28:58 +0200 | [diff] [blame] | 44 | #include "perf.h" |
Ingo Molnar | 16f762a | 2009-05-27 09:10:38 +0200 | [diff] [blame] | 45 | #include "builtin.h" |
Arnaldo Carvalho de Melo | f14d570 | 2014-10-17 12:17:40 -0300 | [diff] [blame] | 46 | #include "util/cgroup.h" |
Ingo Molnar | 148be2c | 2009-04-27 08:02:14 +0200 | [diff] [blame] | 47 | #include "util/util.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 48 | #include <subcmd/parse-options.h> |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 49 | #include "util/parse-events.h" |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 50 | #include "util/pmu.h" |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 51 | #include "util/event.h" |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 52 | #include "util/evlist.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 53 | #include "util/evsel.h" |
Frederic Weisbecker | 8f28827 | 2009-08-16 22:05:48 +0200 | [diff] [blame] | 54 | #include "util/debug.h" |
Mathieu Poirier | 5d8bb1e | 2016-09-16 09:50:03 -0600 | [diff] [blame] | 55 | #include "util/drv_configs.h" |
Ingo Molnar | a5d243d | 2011-04-27 05:39:24 +0200 | [diff] [blame] | 56 | #include "util/color.h" |
Xiao Guangrong | 0007ece | 2012-09-17 16:31:14 +0800 | [diff] [blame] | 57 | #include "util/stat.h" |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 58 | #include "util/header.h" |
Paul Mackerras | a12b51c | 2010-03-10 20:36:09 +1100 | [diff] [blame] | 59 | #include "util/cpumap.h" |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 60 | #include "util/thread.h" |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 61 | #include "util/thread_map.h" |
Jiri Olsa | d809560 | 2015-08-07 12:51:03 +0200 | [diff] [blame] | 62 | #include "util/counts.h" |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 63 | #include "util/group.h" |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 64 | #include "util/session.h" |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 65 | #include "util/tool.h" |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 66 | #include "util/group.h" |
Arnaldo Carvalho de Melo | a067558 | 2017-04-17 16:51:59 -0300 | [diff] [blame] | 67 | #include "util/string2.h" |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 68 | #include "util/metricgroup.h" |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 69 | #include "asm/bug.h" |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 70 | |
Arnaldo Carvalho de Melo | bd48c63 | 2016-08-05 15:40:30 -0300 | [diff] [blame] | 71 | #include <linux/time64.h> |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 72 | #include <api/fs/fs.h> |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 73 | #include <errno.h> |
Arnaldo Carvalho de Melo | 9607ad3 | 2017-04-19 15:49:18 -0300 | [diff] [blame] | 74 | #include <signal.h> |
Peter Zijlstra | 1f16c57 | 2012-10-23 13:40:14 +0200 | [diff] [blame] | 75 | #include <stdlib.h> |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 76 | #include <sys/prctl.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 77 | #include <inttypes.h> |
Stephane Eranian | 5af52b5 | 2010-05-18 15:00:01 +0200 | [diff] [blame] | 78 | #include <locale.h> |
Andi Kleen | e3b03b6 | 2016-05-05 16:04:03 -0700 | [diff] [blame] | 79 | #include <math.h> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 80 | #include <sys/types.h> |
| 81 | #include <sys/stat.h> |
Arnaldo Carvalho de Melo | 4208735 | 2017-04-19 19:06:30 -0300 | [diff] [blame] | 82 | #include <sys/wait.h> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 83 | #include <unistd.h> |
Peter Zijlstra | 16c8a10 | 2009-05-05 17:50:27 +0200 | [diff] [blame] | 84 | |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 85 | #include "sane_ctype.h" |
| 86 | |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 87 | #define DEFAULT_SEPARATOR " " |
David Ahern | 2cee77c | 2011-05-30 08:55:59 -0600 | [diff] [blame] | 88 | #define CNTR_NOT_SUPPORTED "<not supported>" |
| 89 | #define CNTR_NOT_COUNTED "<not counted>" |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 90 | #define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi" |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 91 | |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 92 | static void print_counters(struct timespec *ts, int argc, const char **argv); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 93 | |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 94 | /* Default events used for perf stat -T */ |
Jiri Olsa | a454742 | 2015-06-03 16:25:53 +0200 | [diff] [blame] | 95 | static const char *transaction_attrs = { |
| 96 | "task-clock," |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 97 | "{" |
| 98 | "instructions," |
| 99 | "cycles," |
| 100 | "cpu/cycles-t/," |
| 101 | "cpu/tx-start/," |
| 102 | "cpu/el-start/," |
| 103 | "cpu/cycles-ct/" |
| 104 | "}" |
| 105 | }; |
| 106 | |
| 107 | /* More limited version when the CPU does not have all events. */ |
Jiri Olsa | a454742 | 2015-06-03 16:25:53 +0200 | [diff] [blame] | 108 | static const char * transaction_limited_attrs = { |
| 109 | "task-clock," |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 110 | "{" |
| 111 | "instructions," |
| 112 | "cycles," |
| 113 | "cpu/cycles-t/," |
| 114 | "cpu/tx-start/" |
| 115 | "}" |
| 116 | }; |
| 117 | |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 118 | static const char * topdown_attrs[] = { |
| 119 | "topdown-total-slots", |
| 120 | "topdown-slots-retired", |
| 121 | "topdown-recovery-bubbles", |
| 122 | "topdown-fetch-bubbles", |
| 123 | "topdown-slots-issued", |
| 124 | NULL, |
| 125 | }; |
| 126 | |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 127 | static const char *smi_cost_attrs = { |
| 128 | "{" |
| 129 | "msr/aperf/," |
| 130 | "msr/smi/," |
| 131 | "cycles" |
| 132 | "}" |
| 133 | }; |
| 134 | |
Robert Richter | 666e6d4 | 2012-04-05 18:26:27 +0200 | [diff] [blame] | 135 | static struct perf_evlist *evsel_list; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 136 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 137 | static struct rblist metric_events; |
| 138 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 139 | static struct target target = { |
Namhyung Kim | 77a6f01 | 2012-05-07 14:09:04 +0900 | [diff] [blame] | 140 | .uid = UINT_MAX, |
| 141 | }; |
Jaswinder Singh Rajput | 3d63259 | 2009-06-24 18:19:34 +0530 | [diff] [blame] | 142 | |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 143 | typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu); |
| 144 | |
Jaswinder Singh Rajput | 3d63259 | 2009-06-24 18:19:34 +0530 | [diff] [blame] | 145 | static int run_count = 1; |
Stephane Eranian | 2e6cdf9 | 2010-05-12 10:40:01 +0200 | [diff] [blame] | 146 | static bool no_inherit = false; |
Stephane Eranian | d07f0b1 | 2013-06-04 17:44:26 +0200 | [diff] [blame] | 147 | static volatile pid_t child_pid = -1; |
Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 148 | static bool null_run = false; |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 149 | static int detailed_run = 0; |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 150 | static bool transaction_run; |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 151 | static bool topdown_run = false; |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 152 | static bool smi_cost = false; |
| 153 | static bool smi_reset = false; |
Arnaldo Carvalho de Melo | 201e0b0 | 2010-12-01 17:53:27 -0200 | [diff] [blame] | 154 | static bool big_num = true; |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 155 | static int big_num_opt = -1; |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 156 | static const char *csv_sep = NULL; |
| 157 | static bool csv_output = false; |
Lin Ming | 43bece7 | 2011-08-17 18:42:07 +0800 | [diff] [blame] | 158 | static bool group = false; |
Peter Zijlstra | 1f16c57 | 2012-10-23 13:40:14 +0200 | [diff] [blame] | 159 | static const char *pre_cmd = NULL; |
| 160 | static const char *post_cmd = NULL; |
| 161 | static bool sync_run = false; |
Andi Kleen | 4119168 | 2013-08-02 17:41:11 -0700 | [diff] [blame] | 162 | static unsigned int initial_delay = 0; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 163 | static unsigned int unit_width = 4; /* strlen("unit") */ |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 164 | static bool forever = false; |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 165 | static bool metric_only = false; |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 166 | static bool force_metric_only = false; |
Andi Kleen | 430daf2 | 2017-03-20 13:17:00 -0700 | [diff] [blame] | 167 | static bool no_merge = false; |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 168 | static struct timespec ref_time; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 169 | static struct cpu_map *aggr_map; |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 170 | static aggr_get_id_t aggr_get_id; |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 171 | static bool append_file; |
| 172 | static const char *output_name; |
| 173 | static int output_fd; |
Borislav Petkov | 02d492e | 2017-02-07 01:40:05 +0100 | [diff] [blame] | 174 | static int print_free_counters_hint; |
Stephane Eranian | 5af52b5 | 2010-05-18 15:00:01 +0200 | [diff] [blame] | 175 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 176 | struct perf_stat { |
| 177 | bool record; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 178 | struct perf_data data; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 179 | struct perf_session *session; |
| 180 | u64 bytes_written; |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 181 | struct perf_tool tool; |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 182 | bool maps_allocated; |
| 183 | struct cpu_map *cpus; |
| 184 | struct thread_map *threads; |
Jiri Olsa | 89af4e0 | 2015-11-05 15:41:02 +0100 | [diff] [blame] | 185 | enum aggr_mode aggr_mode; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | static struct perf_stat perf_stat; |
| 189 | #define STAT_RECORD perf_stat.record |
| 190 | |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 191 | static volatile int done = 0; |
| 192 | |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 193 | static struct perf_stat_config stat_config = { |
| 194 | .aggr_mode = AGGR_GLOBAL, |
Jiri Olsa | 711a572 | 2015-07-21 14:31:23 +0200 | [diff] [blame] | 195 | .scale = true, |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 196 | }; |
| 197 | |
Andi Kleen | e864c5c | 2017-08-31 12:40:35 -0700 | [diff] [blame] | 198 | static bool is_duration_time(struct perf_evsel *evsel) |
| 199 | { |
| 200 | return !strcmp(evsel->name, "duration_time"); |
| 201 | } |
| 202 | |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 203 | static inline void diff_timespec(struct timespec *r, struct timespec *a, |
| 204 | struct timespec *b) |
| 205 | { |
| 206 | r->tv_sec = a->tv_sec - b->tv_sec; |
| 207 | if (a->tv_nsec < b->tv_nsec) { |
Arnaldo Carvalho de Melo | 310ebb9 | 2016-08-08 14:57:04 -0300 | [diff] [blame] | 208 | r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec; |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 209 | r->tv_sec--; |
| 210 | } else { |
| 211 | r->tv_nsec = a->tv_nsec - b->tv_nsec ; |
| 212 | } |
| 213 | } |
| 214 | |
Jiri Olsa | 254ecbc7 | 2015-06-26 11:29:13 +0200 | [diff] [blame] | 215 | static void perf_stat__reset_stats(void) |
| 216 | { |
| 217 | perf_evlist__reset_stats(evsel_list); |
Jiri Olsa | f87027b | 2015-06-03 16:25:59 +0200 | [diff] [blame] | 218 | perf_stat__reset_shadow_stats(); |
Jiri Olsa | 1eda3b2 | 2015-06-03 16:25:55 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Jiri Olsa | cac2142 | 2012-11-12 18:34:00 +0100 | [diff] [blame] | 221 | static int create_perf_stat_counter(struct perf_evsel *evsel) |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 222 | { |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 223 | struct perf_event_attr *attr = &evsel->attr; |
Jiri Olsa | 82bf311 | 2017-07-26 14:02:06 +0200 | [diff] [blame] | 224 | struct perf_evsel *leader = evsel->leader; |
Arnaldo Carvalho de Melo | 727ab04 | 2011-10-25 10:42:19 -0200 | [diff] [blame] | 225 | |
Jiri Olsa | 82bf311 | 2017-07-26 14:02:06 +0200 | [diff] [blame] | 226 | if (stat_config.scale) { |
Ingo Molnar | a21ca2c | 2009-06-06 09:58:57 +0200 | [diff] [blame] | 227 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | |
| 228 | PERF_FORMAT_TOTAL_TIME_RUNNING; |
Jiri Olsa | 82bf311 | 2017-07-26 14:02:06 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* |
| 232 | * The event is part of non trivial group, let's enable |
| 233 | * the group read (for leader) and ID retrieval for all |
| 234 | * members. |
| 235 | */ |
| 236 | if (leader->nr_members > 1) |
| 237 | attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 238 | |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 239 | attr->inherit = !no_inherit; |
Arnaldo Carvalho de Melo | 5d2cd90 | 2011-04-14 11:20:14 -0300 | [diff] [blame] | 240 | |
Jiri Olsa | 6acd8e9 | 2015-11-25 16:36:54 +0100 | [diff] [blame] | 241 | /* |
| 242 | * Some events get initialized with sample_(period/type) set, |
| 243 | * like tracepoints. Clear it up for counting. |
| 244 | */ |
| 245 | attr->sample_period = 0; |
Jiri Olsa | 6db1a5c | 2016-01-05 22:09:05 +0100 | [diff] [blame] | 246 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 247 | /* |
| 248 | * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless |
| 249 | * while avoiding that older tools show confusing messages. |
Jiri Olsa | 6db1a5c | 2016-01-05 22:09:05 +0100 | [diff] [blame] | 250 | * |
| 251 | * However for pipe sessions we need to keep it zero, |
| 252 | * because script's perf_evsel__check_attr is triggered |
| 253 | * by attr->sample_type != 0, and we can't run it on |
| 254 | * stat sessions. |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 255 | */ |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 256 | if (!(STAT_RECORD && perf_stat.data.is_pipe)) |
Jiri Olsa | 6db1a5c | 2016-01-05 22:09:05 +0100 | [diff] [blame] | 257 | attr->sample_type = PERF_SAMPLE_IDENTIFIER; |
Jiri Olsa | 6acd8e9 | 2015-11-25 16:36:54 +0100 | [diff] [blame] | 258 | |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 259 | /* |
| 260 | * Disabling all counters initially, they will be enabled |
| 261 | * either manually by us or by kernel via enable_on_exec |
| 262 | * set later. |
| 263 | */ |
Jiri Olsa | c8280ce | 2015-12-03 10:06:45 +0100 | [diff] [blame] | 264 | if (perf_evsel__is_group_leader(evsel)) { |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 265 | attr->disabled = 1; |
| 266 | |
Jiri Olsa | c8280ce | 2015-12-03 10:06:45 +0100 | [diff] [blame] | 267 | /* |
| 268 | * In case of initial_delay we enable tracee |
| 269 | * events manually. |
| 270 | */ |
| 271 | if (target__none(&target) && !initial_delay) |
| 272 | attr->enable_on_exec = 1; |
| 273 | } |
| 274 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 275 | if (target__has_cpu(&target)) |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 276 | return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel)); |
Stephane Eranian | 5622c07 | 2012-04-27 14:45:38 +0200 | [diff] [blame] | 277 | |
Arnaldo Carvalho de Melo | 594ac61 | 2012-12-13 13:13:07 -0300 | [diff] [blame] | 278 | return perf_evsel__open_per_thread(evsel, evsel_list->threads); |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Ingo Molnar | c04f5e5 | 2009-05-29 09:10:54 +0200 | [diff] [blame] | 281 | /* |
| 282 | * Does the counter have nsecs as a unit? |
| 283 | */ |
Arnaldo Carvalho de Melo | daec78a | 2011-01-03 16:49:44 -0200 | [diff] [blame] | 284 | static inline int nsec_counter(struct perf_evsel *evsel) |
Ingo Molnar | c04f5e5 | 2009-05-29 09:10:54 +0200 | [diff] [blame] | 285 | { |
Arnaldo Carvalho de Melo | daec78a | 2011-01-03 16:49:44 -0200 | [diff] [blame] | 286 | if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) || |
| 287 | perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK)) |
Ingo Molnar | c04f5e5 | 2009-05-29 09:10:54 +0200 | [diff] [blame] | 288 | return 1; |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 293 | static int process_synthesized_event(struct perf_tool *tool __maybe_unused, |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 294 | union perf_event *event, |
| 295 | struct perf_sample *sample __maybe_unused, |
| 296 | struct machine *machine __maybe_unused) |
| 297 | { |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 298 | if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) { |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 299 | pr_err("failed to write perf data, error: %m\n"); |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | perf_stat.bytes_written += event->header.size; |
| 304 | return 0; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 307 | static int write_stat_round_event(u64 tm, u64 type) |
Jiri Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 308 | { |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 309 | return perf_event__synthesize_stat_round(NULL, tm, type, |
Jiri Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 310 | process_synthesized_event, |
| 311 | NULL); |
| 312 | } |
| 313 | |
| 314 | #define WRITE_STAT_ROUND_EVENT(time, interval) \ |
| 315 | write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval) |
| 316 | |
Jiri Olsa | 5a6ea81 | 2015-11-05 15:40:51 +0100 | [diff] [blame] | 317 | #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) |
| 318 | |
| 319 | static int |
| 320 | perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread, |
| 321 | struct perf_counts_values *count) |
| 322 | { |
| 323 | struct perf_sample_id *sid = SID(counter, cpu, thread); |
| 324 | |
| 325 | return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count, |
| 326 | process_synthesized_event, NULL); |
| 327 | } |
| 328 | |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 329 | /* |
| 330 | * Read out the results of a single counter: |
| 331 | * do not aggregate counts across CPUs in system-wide mode |
| 332 | */ |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 333 | static int read_counter(struct perf_evsel *counter) |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 334 | { |
Jiri Olsa | 9bf1a52 | 2014-11-21 10:31:09 +0100 | [diff] [blame] | 335 | int nthreads = thread_map__nr(evsel_list->threads); |
Mark Rutland | 00e727b | 2016-07-15 11:08:10 +0100 | [diff] [blame] | 336 | int ncpus, cpu, thread; |
| 337 | |
| 338 | if (target__has_cpu(&target)) |
| 339 | ncpus = perf_evsel__nr_cpus(counter); |
| 340 | else |
| 341 | ncpus = 1; |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 342 | |
Suzuki K. Poulose | 3b4331d | 2015-02-13 18:40:58 +0000 | [diff] [blame] | 343 | if (!counter->supported) |
| 344 | return -ENOENT; |
| 345 | |
Jiri Olsa | 9bf1a52 | 2014-11-21 10:31:09 +0100 | [diff] [blame] | 346 | if (counter->system_wide) |
| 347 | nthreads = 1; |
| 348 | |
| 349 | for (thread = 0; thread < nthreads; thread++) { |
| 350 | for (cpu = 0; cpu < ncpus; cpu++) { |
Jiri Olsa | 3b3eb04 | 2015-06-26 11:29:20 +0200 | [diff] [blame] | 351 | struct perf_counts_values *count; |
| 352 | |
| 353 | count = perf_counts(counter->counts, cpu, thread); |
Jiri Olsa | 82bf311 | 2017-07-26 14:02:06 +0200 | [diff] [blame] | 354 | |
| 355 | /* |
| 356 | * The leader's group read loads data into its group members |
| 357 | * (via perf_evsel__read_counter) and sets threir count->loaded. |
| 358 | */ |
| 359 | if (!count->loaded && |
| 360 | perf_evsel__read_counter(counter, cpu, thread)) { |
Stephane Eranian | db49a71 | 2017-04-12 11:23:01 -0700 | [diff] [blame] | 361 | counter->counts->scaled = -1; |
| 362 | perf_counts(counter->counts, cpu, thread)->ena = 0; |
| 363 | perf_counts(counter->counts, cpu, thread)->run = 0; |
Jiri Olsa | 9bf1a52 | 2014-11-21 10:31:09 +0100 | [diff] [blame] | 364 | return -1; |
Stephane Eranian | db49a71 | 2017-04-12 11:23:01 -0700 | [diff] [blame] | 365 | } |
Jiri Olsa | 5a6ea81 | 2015-11-05 15:40:51 +0100 | [diff] [blame] | 366 | |
Jiri Olsa | 82bf311 | 2017-07-26 14:02:06 +0200 | [diff] [blame] | 367 | count->loaded = false; |
| 368 | |
Jiri Olsa | 5a6ea81 | 2015-11-05 15:40:51 +0100 | [diff] [blame] | 369 | if (STAT_RECORD) { |
| 370 | if (perf_evsel__write_stat_event(counter, cpu, thread, count)) { |
| 371 | pr_err("failed to write stat event\n"); |
| 372 | return -1; |
| 373 | } |
| 374 | } |
Andi Kleen | 0b1abbf | 2016-04-27 13:00:51 -0700 | [diff] [blame] | 375 | |
| 376 | if (verbose > 1) { |
| 377 | fprintf(stat_config.output, |
| 378 | "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", |
| 379 | perf_evsel__name(counter), |
| 380 | cpu, |
| 381 | count->val, count->ena, count->run); |
| 382 | } |
Jiri Olsa | 9bf1a52 | 2014-11-21 10:31:09 +0100 | [diff] [blame] | 383 | } |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 384 | } |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 385 | |
| 386 | return 0; |
Ingo Molnar | 2996f5d | 2009-05-29 09:10:54 +0200 | [diff] [blame] | 387 | } |
| 388 | |
Mark Rutland | 3df33ef | 2016-08-09 14:04:29 +0100 | [diff] [blame] | 389 | static void read_counters(void) |
Jiri Olsa | 106a94a | 2015-06-26 11:29:19 +0200 | [diff] [blame] | 390 | { |
| 391 | struct perf_evsel *counter; |
Stephane Eranian | db49a71 | 2017-04-12 11:23:01 -0700 | [diff] [blame] | 392 | int ret; |
Jiri Olsa | 106a94a | 2015-06-26 11:29:19 +0200 | [diff] [blame] | 393 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 394 | evlist__for_each_entry(evsel_list, counter) { |
Stephane Eranian | db49a71 | 2017-04-12 11:23:01 -0700 | [diff] [blame] | 395 | ret = read_counter(counter); |
| 396 | if (ret) |
Andi Kleen | 245bad8 | 2015-09-01 15:52:46 -0700 | [diff] [blame] | 397 | pr_debug("failed to read counter %s\n", counter->name); |
Jiri Olsa | 3b3eb04 | 2015-06-26 11:29:20 +0200 | [diff] [blame] | 398 | |
Stephane Eranian | db49a71 | 2017-04-12 11:23:01 -0700 | [diff] [blame] | 399 | if (ret == 0 && perf_stat_process_counter(&stat_config, counter)) |
Jiri Olsa | 3b3eb04 | 2015-06-26 11:29:20 +0200 | [diff] [blame] | 400 | pr_warning("failed to process counter %s\n", counter->name); |
Jiri Olsa | 106a94a | 2015-06-26 11:29:19 +0200 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
Jiri Olsa | ba411a9 | 2015-06-26 11:29:24 +0200 | [diff] [blame] | 404 | static void process_interval(void) |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 405 | { |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 406 | struct timespec ts, rs; |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 407 | |
Mark Rutland | 3df33ef | 2016-08-09 14:04:29 +0100 | [diff] [blame] | 408 | read_counters(); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 409 | |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 410 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 411 | diff_timespec(&rs, &ts, &ref_time); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 412 | |
Jiri Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 413 | if (STAT_RECORD) { |
Arnaldo Carvalho de Melo | bd48c63 | 2016-08-05 15:40:30 -0300 | [diff] [blame] | 414 | if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL)) |
Jiri Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 415 | pr_err("failed to write stat round event\n"); |
| 416 | } |
| 417 | |
Andi Kleen | b90f133 | 2017-08-31 12:40:36 -0700 | [diff] [blame] | 418 | init_stats(&walltime_nsecs_stats); |
| 419 | update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000); |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 420 | print_counters(&rs, 0, NULL); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 423 | static void enable_counters(void) |
Andi Kleen | 4119168 | 2013-08-02 17:41:11 -0700 | [diff] [blame] | 424 | { |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 425 | if (initial_delay) |
Arnaldo Carvalho de Melo | 310ebb9 | 2016-08-08 14:57:04 -0300 | [diff] [blame] | 426 | usleep(initial_delay * USEC_PER_MSEC); |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 427 | |
| 428 | /* |
| 429 | * We need to enable counters only if: |
| 430 | * - we don't have tracee (attaching to task or cpu) |
| 431 | * - we have initial delay configured |
| 432 | */ |
| 433 | if (!target__none(&target) || initial_delay) |
Jiri Olsa | ab46db0 | 2015-12-03 10:06:43 +0100 | [diff] [blame] | 434 | perf_evlist__enable(evsel_list); |
Andi Kleen | 4119168 | 2013-08-02 17:41:11 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Mark Rutland | 3df33ef | 2016-08-09 14:04:29 +0100 | [diff] [blame] | 437 | static void disable_counters(void) |
| 438 | { |
| 439 | /* |
| 440 | * If we don't have tracee (attaching to task or cpu), counters may |
| 441 | * still be running. To get accurate group ratios, we must stop groups |
| 442 | * from counting before reading their constituent counters. |
| 443 | */ |
| 444 | if (!target__none(&target)) |
| 445 | perf_evlist__disable(evsel_list); |
| 446 | } |
| 447 | |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 448 | static volatile int workload_exec_errno; |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 449 | |
| 450 | /* |
| 451 | * perf_evlist__prepare_workload will send a SIGUSR1 |
| 452 | * if the fork fails, since we asked by setting its |
| 453 | * want_signal to true. |
| 454 | */ |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 455 | static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info, |
| 456 | void *ucontext __maybe_unused) |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 457 | { |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 458 | workload_exec_errno = info->si_value.sival_int; |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 459 | } |
| 460 | |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 461 | static int perf_stat_synthesize_config(bool is_pipe) |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 462 | { |
| 463 | int err; |
| 464 | |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 465 | if (is_pipe) { |
| 466 | err = perf_event__synthesize_attrs(NULL, perf_stat.session, |
| 467 | process_synthesized_event); |
| 468 | if (err < 0) { |
| 469 | pr_err("Couldn't synthesize attrs.\n"); |
| 470 | return err; |
| 471 | } |
| 472 | } |
| 473 | |
Andi Kleen | bfd8f72 | 2017-11-17 13:42:58 -0800 | [diff] [blame^] | 474 | err = perf_event__synthesize_extra_attr(NULL, |
| 475 | evsel_list, |
| 476 | process_synthesized_event, |
| 477 | is_pipe); |
Jiri Olsa | 7b60a7e | 2015-11-05 15:40:54 +0100 | [diff] [blame] | 478 | |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 479 | err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads, |
| 480 | process_synthesized_event, |
| 481 | NULL); |
| 482 | if (err < 0) { |
| 483 | pr_err("Couldn't synthesize thread map.\n"); |
| 484 | return err; |
| 485 | } |
| 486 | |
| 487 | err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus, |
| 488 | process_synthesized_event, NULL); |
| 489 | if (err < 0) { |
| 490 | pr_err("Couldn't synthesize thread map.\n"); |
| 491 | return err; |
| 492 | } |
| 493 | |
| 494 | err = perf_event__synthesize_stat_config(NULL, &stat_config, |
| 495 | process_synthesized_event, NULL); |
| 496 | if (err < 0) { |
| 497 | pr_err("Couldn't synthesize config.\n"); |
| 498 | return err; |
| 499 | } |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
Jiri Olsa | 2af4646 | 2015-11-05 15:40:49 +0100 | [diff] [blame] | 504 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
| 505 | |
| 506 | static int __store_counter_ids(struct perf_evsel *counter, |
| 507 | struct cpu_map *cpus, |
| 508 | struct thread_map *threads) |
| 509 | { |
| 510 | int cpu, thread; |
| 511 | |
| 512 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
| 513 | for (thread = 0; thread < threads->nr; thread++) { |
| 514 | int fd = FD(counter, cpu, thread); |
| 515 | |
| 516 | if (perf_evlist__id_add_fd(evsel_list, counter, |
| 517 | cpu, thread, fd) < 0) |
| 518 | return -1; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static int store_counter_ids(struct perf_evsel *counter) |
| 526 | { |
| 527 | struct cpu_map *cpus = counter->cpus; |
| 528 | struct thread_map *threads = counter->threads; |
| 529 | |
| 530 | if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr)) |
| 531 | return -ENOMEM; |
| 532 | |
| 533 | return __store_counter_ids(counter, cpus, threads); |
| 534 | } |
| 535 | |
Jiri Olsa | 82bf311 | 2017-07-26 14:02:06 +0200 | [diff] [blame] | 536 | static bool perf_evsel__should_store_id(struct perf_evsel *counter) |
| 537 | { |
| 538 | return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID; |
| 539 | } |
| 540 | |
Andi Kleen | 5a5dfe4 | 2017-08-31 12:40:26 -0700 | [diff] [blame] | 541 | static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel) |
| 542 | { |
| 543 | struct perf_evsel *c2, *leader; |
| 544 | bool is_open = true; |
| 545 | |
| 546 | leader = evsel->leader; |
| 547 | pr_debug("Weak group for %s/%d failed\n", |
| 548 | leader->name, leader->nr_members); |
| 549 | |
| 550 | /* |
| 551 | * for_each_group_member doesn't work here because it doesn't |
| 552 | * include the first entry. |
| 553 | */ |
| 554 | evlist__for_each_entry(evsel_list, c2) { |
| 555 | if (c2 == evsel) |
| 556 | is_open = false; |
| 557 | if (c2->leader == leader) { |
| 558 | if (is_open) |
| 559 | perf_evsel__close(c2); |
| 560 | c2->leader = c2; |
| 561 | c2->nr_members = 0; |
| 562 | } |
| 563 | } |
| 564 | return leader; |
| 565 | } |
| 566 | |
Namhyung Kim | acf2892 | 2013-03-11 16:43:18 +0900 | [diff] [blame] | 567 | static int __run_perf_stat(int argc, const char **argv) |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 568 | { |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 569 | int interval = stat_config.interval; |
Arnaldo Carvalho de Melo | d6195a6 | 2017-02-13 16:45:24 -0300 | [diff] [blame] | 570 | char msg[BUFSIZ]; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 571 | unsigned long long t0, t1; |
Jiri Olsa | cac2142 | 2012-11-12 18:34:00 +0100 | [diff] [blame] | 572 | struct perf_evsel *counter; |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 573 | struct timespec ts; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 574 | size_t l; |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 575 | int status = 0; |
Zhang, Yanmin | 6be2850 | 2010-03-18 11:36:03 -0300 | [diff] [blame] | 576 | const bool forks = (argc > 0); |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 577 | bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false; |
Mathieu Poirier | 5d8bb1e | 2016-09-16 09:50:03 -0600 | [diff] [blame] | 578 | struct perf_evsel_config_term *err_term; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 579 | |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 580 | if (interval) { |
Arnaldo Carvalho de Melo | 310ebb9 | 2016-08-08 14:57:04 -0300 | [diff] [blame] | 581 | ts.tv_sec = interval / USEC_PER_MSEC; |
| 582 | ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC; |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 583 | } else { |
| 584 | ts.tv_sec = 1; |
| 585 | ts.tv_nsec = 0; |
| 586 | } |
| 587 | |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 588 | if (forks) { |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 589 | if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe, |
Arnaldo Carvalho de Melo | 735f7e0 | 2014-01-03 14:56:49 -0300 | [diff] [blame] | 590 | workload_exec_failed_signal) < 0) { |
Namhyung Kim | acf2892 | 2013-03-11 16:43:18 +0900 | [diff] [blame] | 591 | perror("failed to prepare workload"); |
| 592 | return -1; |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 593 | } |
Namhyung Kim | d20a47e | 2013-09-30 18:01:11 +0900 | [diff] [blame] | 594 | child_pid = evsel_list->workload.pid; |
Paul Mackerras | 051ae7f | 2009-06-29 21:13:21 +1000 | [diff] [blame] | 595 | } |
| 596 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 597 | if (group) |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 598 | perf_evlist__set_leader(evsel_list); |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 599 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 600 | evlist__for_each_entry(evsel_list, counter) { |
Arnaldo Carvalho de Melo | 42ef8a7 | 2016-05-12 16:25:18 -0300 | [diff] [blame] | 601 | try_again: |
Jiri Olsa | cac2142 | 2012-11-12 18:34:00 +0100 | [diff] [blame] | 602 | if (create_perf_stat_counter(counter) < 0) { |
Andi Kleen | 5a5dfe4 | 2017-08-31 12:40:26 -0700 | [diff] [blame] | 603 | |
| 604 | /* Weak group failed. Reset the group. */ |
Andi Kleen | 35c1980 | 2017-09-05 14:13:24 -0700 | [diff] [blame] | 605 | if ((errno == EINVAL || errno == EBADF) && |
Andi Kleen | 5a5dfe4 | 2017-08-31 12:40:26 -0700 | [diff] [blame] | 606 | counter->leader != counter && |
| 607 | counter->weak_group) { |
| 608 | counter = perf_evsel__reset_weak_group(counter); |
| 609 | goto try_again; |
| 610 | } |
| 611 | |
David Ahern | 979987a | 2012-05-08 09:29:16 -0600 | [diff] [blame] | 612 | /* |
| 613 | * PPC returns ENXIO for HW counters until 2.6.37 |
| 614 | * (behavior changed with commit b0a873e). |
| 615 | */ |
Anton Blanchard | 38f6ae1 | 2011-12-02 09:38:33 +1100 | [diff] [blame] | 616 | if (errno == EINVAL || errno == ENOSYS || |
David Ahern | 979987a | 2012-05-08 09:29:16 -0600 | [diff] [blame] | 617 | errno == ENOENT || errno == EOPNOTSUPP || |
| 618 | errno == ENXIO) { |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 619 | if (verbose > 0) |
David Ahern | c63ca0c | 2011-04-29 16:04:15 -0600 | [diff] [blame] | 620 | ui__warning("%s event is not supported by the kernel.\n", |
Arnaldo Carvalho de Melo | 7289f83 | 2012-06-12 12:34:58 -0300 | [diff] [blame] | 621 | perf_evsel__name(counter)); |
David Ahern | 2cee77c | 2011-05-30 08:55:59 -0600 | [diff] [blame] | 622 | counter->supported = false; |
Kan Liang | cb5ef60 | 2015-06-11 02:32:40 -0400 | [diff] [blame] | 623 | |
| 624 | if ((counter->leader != counter) || |
| 625 | !(counter->leader->nr_members > 1)) |
| 626 | continue; |
Arnaldo Carvalho de Melo | 42ef8a7 | 2016-05-12 16:25:18 -0300 | [diff] [blame] | 627 | } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) { |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 628 | if (verbose > 0) |
Arnaldo Carvalho de Melo | 42ef8a7 | 2016-05-12 16:25:18 -0300 | [diff] [blame] | 629 | ui__warning("%s\n", msg); |
| 630 | goto try_again; |
| 631 | } |
Ingo Molnar | ede7029 | 2011-04-28 08:48:42 +0200 | [diff] [blame] | 632 | |
Arnaldo Carvalho de Melo | 56e52e8 | 2012-12-13 15:10:58 -0300 | [diff] [blame] | 633 | perf_evsel__open_strerror(counter, &target, |
| 634 | errno, msg, sizeof(msg)); |
| 635 | ui__error("%s\n", msg); |
| 636 | |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 637 | if (child_pid != -1) |
| 638 | kill(child_pid, SIGTERM); |
David Ahern | fceda7f | 2012-08-26 12:24:44 -0600 | [diff] [blame] | 639 | |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 640 | return -1; |
| 641 | } |
David Ahern | 2cee77c | 2011-05-30 08:55:59 -0600 | [diff] [blame] | 642 | counter->supported = true; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 643 | |
| 644 | l = strlen(counter->unit); |
| 645 | if (l > unit_width) |
| 646 | unit_width = l; |
Jiri Olsa | 2af4646 | 2015-11-05 15:40:49 +0100 | [diff] [blame] | 647 | |
Jiri Olsa | 82bf311 | 2017-07-26 14:02:06 +0200 | [diff] [blame] | 648 | if (perf_evsel__should_store_id(counter) && |
| 649 | store_counter_ids(counter)) |
Jiri Olsa | 2af4646 | 2015-11-05 15:40:49 +0100 | [diff] [blame] | 650 | return -1; |
Arnaldo Carvalho de Melo | 084ab9f | 2010-03-22 13:10:28 -0300 | [diff] [blame] | 651 | } |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 652 | |
Arnaldo Carvalho de Melo | 23d4aad | 2015-03-24 19:23:47 -0300 | [diff] [blame] | 653 | if (perf_evlist__apply_filters(evsel_list, &counter)) { |
Arnaldo Carvalho de Melo | 62d94b0 | 2017-06-27 11:22:31 -0300 | [diff] [blame] | 654 | pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n", |
Arnaldo Carvalho de Melo | 23d4aad | 2015-03-24 19:23:47 -0300 | [diff] [blame] | 655 | counter->filter, perf_evsel__name(counter), errno, |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 656 | str_error_r(errno, msg, sizeof(msg))); |
Frederic Weisbecker | cfd748a | 2011-03-14 16:40:30 +0100 | [diff] [blame] | 657 | return -1; |
| 658 | } |
| 659 | |
Mathieu Poirier | 5d8bb1e | 2016-09-16 09:50:03 -0600 | [diff] [blame] | 660 | if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) { |
Arnaldo Carvalho de Melo | 62d94b0 | 2017-06-27 11:22:31 -0300 | [diff] [blame] | 661 | pr_err("failed to set config \"%s\" on event %s with %d (%s)\n", |
Mathieu Poirier | 5d8bb1e | 2016-09-16 09:50:03 -0600 | [diff] [blame] | 662 | err_term->val.drv_cfg, perf_evsel__name(counter), errno, |
| 663 | str_error_r(errno, msg, sizeof(msg))); |
| 664 | return -1; |
| 665 | } |
| 666 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 667 | if (STAT_RECORD) { |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 668 | int err, fd = perf_data__fd(&perf_stat.data); |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 669 | |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 670 | if (is_pipe) { |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 671 | err = perf_header__write_pipe(perf_data__fd(&perf_stat.data)); |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 672 | } else { |
| 673 | err = perf_session__write_header(perf_stat.session, evsel_list, |
| 674 | fd, false); |
| 675 | } |
| 676 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 677 | if (err < 0) |
| 678 | return err; |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 679 | |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 680 | err = perf_stat_synthesize_config(is_pipe); |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 681 | if (err < 0) |
| 682 | return err; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 683 | } |
| 684 | |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 685 | /* |
| 686 | * Enable counters and exec the command: |
| 687 | */ |
| 688 | t0 = rdclock(); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 689 | clock_gettime(CLOCK_MONOTONIC, &ref_time); |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 690 | |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 691 | if (forks) { |
Namhyung Kim | acf2892 | 2013-03-11 16:43:18 +0900 | [diff] [blame] | 692 | perf_evlist__start_workload(evsel_list); |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 693 | enable_counters(); |
Namhyung Kim | acf2892 | 2013-03-11 16:43:18 +0900 | [diff] [blame] | 694 | |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 695 | if (interval) { |
| 696 | while (!waitpid(child_pid, &status, WNOHANG)) { |
| 697 | nanosleep(&ts, NULL); |
Jiri Olsa | ba411a9 | 2015-06-26 11:29:24 +0200 | [diff] [blame] | 698 | process_interval(); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 699 | } |
| 700 | } |
Milian Wolff | dfc9eec | 2017-09-12 17:25:23 +0200 | [diff] [blame] | 701 | waitpid(child_pid, &status, 0); |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 702 | |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 703 | if (workload_exec_errno) { |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 704 | const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 705 | pr_err("Workload failed: %s\n", emsg); |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 706 | return -1; |
Arnaldo Carvalho de Melo | f33cbe7 | 2014-01-02 15:11:25 -0300 | [diff] [blame] | 707 | } |
Arnaldo Carvalho de Melo | 6af206f | 2013-12-28 15:45:08 -0300 | [diff] [blame] | 708 | |
Andi Kleen | 33e49ea | 2011-09-15 14:31:40 -0700 | [diff] [blame] | 709 | if (WIFSIGNALED(status)) |
| 710 | psignal(WTERMSIG(status), argv[0]); |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 711 | } else { |
Jiri Olsa | 67ccdec | 2015-12-03 10:06:44 +0100 | [diff] [blame] | 712 | enable_counters(); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 713 | while (!done) { |
| 714 | nanosleep(&ts, NULL); |
| 715 | if (interval) |
Jiri Olsa | ba411a9 | 2015-06-26 11:29:24 +0200 | [diff] [blame] | 716 | process_interval(); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 717 | } |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 718 | } |
Ingo Molnar | 44db76c | 2009-06-03 19:36:07 +0200 | [diff] [blame] | 719 | |
Mark Rutland | 3df33ef | 2016-08-09 14:04:29 +0100 | [diff] [blame] | 720 | disable_counters(); |
| 721 | |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 722 | t1 = rdclock(); |
| 723 | |
Peter Zijlstra | 9e9772c | 2009-09-04 15:36:08 +0200 | [diff] [blame] | 724 | update_stats(&walltime_nsecs_stats, t1 - t0); |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 725 | |
Mark Rutland | 3df33ef | 2016-08-09 14:04:29 +0100 | [diff] [blame] | 726 | /* |
| 727 | * Closing a group leader splits the group, and as we only disable |
| 728 | * group leaders, results in remaining events becoming enabled. To |
| 729 | * avoid arbitrary skew, we must read all counters before closing any |
| 730 | * group leaders. |
| 731 | */ |
| 732 | read_counters(); |
| 733 | perf_evlist__close(evsel_list); |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 734 | |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 735 | return WEXITSTATUS(status); |
| 736 | } |
| 737 | |
Arnaldo Carvalho de Melo | 41cde47 | 2014-01-03 17:34:42 -0300 | [diff] [blame] | 738 | static int run_perf_stat(int argc, const char **argv) |
Peter Zijlstra | 1f16c57 | 2012-10-23 13:40:14 +0200 | [diff] [blame] | 739 | { |
| 740 | int ret; |
| 741 | |
| 742 | if (pre_cmd) { |
| 743 | ret = system(pre_cmd); |
| 744 | if (ret) |
| 745 | return ret; |
| 746 | } |
| 747 | |
| 748 | if (sync_run) |
| 749 | sync(); |
| 750 | |
| 751 | ret = __run_perf_stat(argc, argv); |
| 752 | if (ret) |
| 753 | return ret; |
| 754 | |
| 755 | if (post_cmd) { |
| 756 | ret = system(post_cmd); |
| 757 | if (ret) |
| 758 | return ret; |
| 759 | } |
| 760 | |
| 761 | return ret; |
| 762 | } |
| 763 | |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 764 | static void print_running(u64 run, u64 ena) |
| 765 | { |
| 766 | if (csv_output) { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 767 | fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f", |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 768 | csv_sep, |
| 769 | run, |
| 770 | csv_sep, |
| 771 | ena ? 100.0 * run / ena : 100.0); |
| 772 | } else if (run != ena) { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 773 | fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena); |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
Ingo Molnar | f99844c | 2011-04-27 05:35:39 +0200 | [diff] [blame] | 777 | static void print_noise_pct(double total, double avg) |
| 778 | { |
Xiao Guangrong | 0007ece | 2012-09-17 16:31:14 +0800 | [diff] [blame] | 779 | double pct = rel_stddev_stats(total, avg); |
Ingo Molnar | f99844c | 2011-04-27 05:35:39 +0200 | [diff] [blame] | 780 | |
Zhengyu He | 3ae9a34d | 2011-06-23 13:45:42 -0700 | [diff] [blame] | 781 | if (csv_output) |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 782 | fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct); |
Jim Cromie | a1bca6c | 2011-09-07 17:14:02 -0600 | [diff] [blame] | 783 | else if (pct) |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 784 | fprintf(stat_config.output, " ( +-%6.2f%% )", pct); |
Ingo Molnar | f99844c | 2011-04-27 05:35:39 +0200 | [diff] [blame] | 785 | } |
| 786 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 787 | static void print_noise(struct perf_evsel *evsel, double avg) |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 788 | { |
Jiri Olsa | 581cc8a | 2015-10-16 12:41:03 +0200 | [diff] [blame] | 789 | struct perf_stat_evsel *ps; |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 790 | |
Peter Zijlstra | 849abde | 2009-09-04 18:23:38 +0200 | [diff] [blame] | 791 | if (run_count == 1) |
| 792 | return; |
| 793 | |
Arnaldo Carvalho de Melo | e669e83 | 2017-10-26 14:22:34 -0300 | [diff] [blame] | 794 | ps = evsel->stats; |
Ingo Molnar | f99844c | 2011-04-27 05:35:39 +0200 | [diff] [blame] | 795 | print_noise_pct(stddev_stats(&ps->res_stats[0]), avg); |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 796 | } |
| 797 | |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 798 | static void aggr_printout(struct perf_evsel *evsel, int id, int nr) |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 799 | { |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 800 | switch (stat_config.aggr_mode) { |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 801 | case AGGR_CORE: |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 802 | fprintf(stat_config.output, "S%d-C%*d%s%*d%s", |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 803 | cpu_map__id_to_socket(id), |
| 804 | csv_output ? 0 : -8, |
| 805 | cpu_map__id_to_cpu(id), |
| 806 | csv_sep, |
| 807 | csv_output ? 0 : 4, |
| 808 | nr, |
| 809 | csv_sep); |
| 810 | break; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 811 | case AGGR_SOCKET: |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 812 | fprintf(stat_config.output, "S%*d%s%*d%s", |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 813 | csv_output ? 0 : -5, |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 814 | id, |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 815 | csv_sep, |
| 816 | csv_output ? 0 : 4, |
| 817 | nr, |
| 818 | csv_sep); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 819 | break; |
| 820 | case AGGR_NONE: |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 821 | fprintf(stat_config.output, "CPU%*d%s", |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 822 | csv_output ? 0 : -4, |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 823 | perf_evsel__cpus(evsel)->map[id], csv_sep); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 824 | break; |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 825 | case AGGR_THREAD: |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 826 | fprintf(stat_config.output, "%*s-%*d%s", |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 827 | csv_output ? 0 : 16, |
| 828 | thread_map__comm(evsel->threads, id), |
| 829 | csv_output ? 0 : -8, |
| 830 | thread_map__pid(evsel->threads, id), |
| 831 | csv_sep); |
| 832 | break; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 833 | case AGGR_GLOBAL: |
Jiri Olsa | 208df99 | 2015-10-16 12:41:04 +0200 | [diff] [blame] | 834 | case AGGR_UNSET: |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 835 | default: |
| 836 | break; |
| 837 | } |
| 838 | } |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 839 | |
Andi Kleen | 140aead | 2016-01-30 09:06:49 -0800 | [diff] [blame] | 840 | struct outstate { |
| 841 | FILE *fh; |
| 842 | bool newline; |
Andi Kleen | f948339 | 2016-01-30 09:06:50 -0800 | [diff] [blame] | 843 | const char *prefix; |
Andi Kleen | 92a61f6 | 2016-02-29 14:36:21 -0800 | [diff] [blame] | 844 | int nfields; |
Andi Kleen | 44d49a6 | 2016-02-29 14:36:22 -0800 | [diff] [blame] | 845 | int id, nr; |
| 846 | struct perf_evsel *evsel; |
Andi Kleen | 140aead | 2016-01-30 09:06:49 -0800 | [diff] [blame] | 847 | }; |
| 848 | |
| 849 | #define METRIC_LEN 35 |
| 850 | |
| 851 | static void new_line_std(void *ctx) |
| 852 | { |
| 853 | struct outstate *os = ctx; |
| 854 | |
| 855 | os->newline = true; |
| 856 | } |
| 857 | |
| 858 | static void do_new_line_std(struct outstate *os) |
| 859 | { |
| 860 | fputc('\n', os->fh); |
Andi Kleen | f948339 | 2016-01-30 09:06:50 -0800 | [diff] [blame] | 861 | fputs(os->prefix, os->fh); |
Andi Kleen | 44d49a6 | 2016-02-29 14:36:22 -0800 | [diff] [blame] | 862 | aggr_printout(os->evsel, os->id, os->nr); |
Andi Kleen | 140aead | 2016-01-30 09:06:49 -0800 | [diff] [blame] | 863 | if (stat_config.aggr_mode == AGGR_NONE) |
| 864 | fprintf(os->fh, " "); |
Andi Kleen | 140aead | 2016-01-30 09:06:49 -0800 | [diff] [blame] | 865 | fprintf(os->fh, " "); |
| 866 | } |
| 867 | |
| 868 | static void print_metric_std(void *ctx, const char *color, const char *fmt, |
| 869 | const char *unit, double val) |
| 870 | { |
| 871 | struct outstate *os = ctx; |
| 872 | FILE *out = os->fh; |
| 873 | int n; |
| 874 | bool newline = os->newline; |
| 875 | |
| 876 | os->newline = false; |
| 877 | |
| 878 | if (unit == NULL || fmt == NULL) { |
| 879 | fprintf(out, "%-*s", METRIC_LEN, ""); |
| 880 | return; |
| 881 | } |
| 882 | |
| 883 | if (newline) |
| 884 | do_new_line_std(os); |
| 885 | |
| 886 | n = fprintf(out, " # "); |
| 887 | if (color) |
| 888 | n += color_fprintf(out, color, fmt, val); |
| 889 | else |
| 890 | n += fprintf(out, fmt, val); |
| 891 | fprintf(out, " %-*s", METRIC_LEN - n - 1, unit); |
| 892 | } |
| 893 | |
Andi Kleen | 92a61f6 | 2016-02-29 14:36:21 -0800 | [diff] [blame] | 894 | static void new_line_csv(void *ctx) |
| 895 | { |
| 896 | struct outstate *os = ctx; |
| 897 | int i; |
| 898 | |
| 899 | fputc('\n', os->fh); |
| 900 | if (os->prefix) |
| 901 | fprintf(os->fh, "%s%s", os->prefix, csv_sep); |
Andi Kleen | 44d49a6 | 2016-02-29 14:36:22 -0800 | [diff] [blame] | 902 | aggr_printout(os->evsel, os->id, os->nr); |
Andi Kleen | 92a61f6 | 2016-02-29 14:36:21 -0800 | [diff] [blame] | 903 | for (i = 0; i < os->nfields; i++) |
| 904 | fputs(csv_sep, os->fh); |
| 905 | } |
| 906 | |
| 907 | static void print_metric_csv(void *ctx, |
| 908 | const char *color __maybe_unused, |
| 909 | const char *fmt, const char *unit, double val) |
| 910 | { |
| 911 | struct outstate *os = ctx; |
| 912 | FILE *out = os->fh; |
| 913 | char buf[64], *vals, *ends; |
| 914 | |
| 915 | if (unit == NULL || fmt == NULL) { |
| 916 | fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep); |
| 917 | return; |
| 918 | } |
| 919 | snprintf(buf, sizeof(buf), fmt, val); |
Taeung Song | b07c40d | 2017-04-07 23:24:18 +0900 | [diff] [blame] | 920 | ends = vals = ltrim(buf); |
Andi Kleen | 92a61f6 | 2016-02-29 14:36:21 -0800 | [diff] [blame] | 921 | while (isdigit(*ends) || *ends == '.') |
| 922 | ends++; |
| 923 | *ends = 0; |
| 924 | while (isspace(*unit)) |
| 925 | unit++; |
| 926 | fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit); |
| 927 | } |
| 928 | |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 929 | #define METRIC_ONLY_LEN 20 |
| 930 | |
| 931 | /* Filter out some columns that don't work well in metrics only mode */ |
| 932 | |
| 933 | static bool valid_only_metric(const char *unit) |
| 934 | { |
| 935 | if (!unit) |
| 936 | return false; |
| 937 | if (strstr(unit, "/sec") || |
| 938 | strstr(unit, "hz") || |
| 939 | strstr(unit, "Hz") || |
| 940 | strstr(unit, "CPUs utilized")) |
| 941 | return false; |
| 942 | return true; |
| 943 | } |
| 944 | |
| 945 | static const char *fixunit(char *buf, struct perf_evsel *evsel, |
| 946 | const char *unit) |
| 947 | { |
| 948 | if (!strncmp(unit, "of all", 6)) { |
| 949 | snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel), |
| 950 | unit); |
| 951 | return buf; |
| 952 | } |
| 953 | return unit; |
| 954 | } |
| 955 | |
| 956 | static void print_metric_only(void *ctx, const char *color, const char *fmt, |
| 957 | const char *unit, double val) |
| 958 | { |
| 959 | struct outstate *os = ctx; |
| 960 | FILE *out = os->fh; |
| 961 | int n; |
| 962 | char buf[1024]; |
| 963 | unsigned mlen = METRIC_ONLY_LEN; |
| 964 | |
| 965 | if (!valid_only_metric(unit)) |
| 966 | return; |
| 967 | unit = fixunit(buf, os->evsel, unit); |
| 968 | if (color) |
| 969 | n = color_fprintf(out, color, fmt, val); |
| 970 | else |
| 971 | n = fprintf(out, fmt, val); |
| 972 | if (n > METRIC_ONLY_LEN) |
| 973 | n = METRIC_ONLY_LEN; |
| 974 | if (mlen < strlen(unit)) |
| 975 | mlen = strlen(unit) + 1; |
| 976 | fprintf(out, "%*s", mlen - n, ""); |
| 977 | } |
| 978 | |
| 979 | static void print_metric_only_csv(void *ctx, const char *color __maybe_unused, |
| 980 | const char *fmt, |
| 981 | const char *unit, double val) |
| 982 | { |
| 983 | struct outstate *os = ctx; |
| 984 | FILE *out = os->fh; |
| 985 | char buf[64], *vals, *ends; |
| 986 | char tbuf[1024]; |
| 987 | |
| 988 | if (!valid_only_metric(unit)) |
| 989 | return; |
| 990 | unit = fixunit(tbuf, os->evsel, unit); |
| 991 | snprintf(buf, sizeof buf, fmt, val); |
Taeung Song | b07c40d | 2017-04-07 23:24:18 +0900 | [diff] [blame] | 992 | ends = vals = ltrim(buf); |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 993 | while (isdigit(*ends) || *ends == '.') |
| 994 | ends++; |
| 995 | *ends = 0; |
| 996 | fprintf(out, "%s%s", vals, csv_sep); |
| 997 | } |
| 998 | |
| 999 | static void new_line_metric(void *ctx __maybe_unused) |
| 1000 | { |
| 1001 | } |
| 1002 | |
| 1003 | static void print_metric_header(void *ctx, const char *color __maybe_unused, |
| 1004 | const char *fmt __maybe_unused, |
| 1005 | const char *unit, double val __maybe_unused) |
| 1006 | { |
| 1007 | struct outstate *os = ctx; |
| 1008 | char tbuf[1024]; |
| 1009 | |
| 1010 | if (!valid_only_metric(unit)) |
| 1011 | return; |
| 1012 | unit = fixunit(tbuf, os->evsel, unit); |
| 1013 | if (csv_output) |
| 1014 | fprintf(os->fh, "%s%s", unit, csv_sep); |
| 1015 | else |
| 1016 | fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit); |
| 1017 | } |
| 1018 | |
Andi Kleen | da88c7f | 2014-09-24 13:50:46 -0700 | [diff] [blame] | 1019 | static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg) |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1020 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1021 | FILE *output = stat_config.output; |
Arnaldo Carvalho de Melo | 310ebb9 | 2016-08-08 14:57:04 -0300 | [diff] [blame] | 1022 | double msecs = avg / NSEC_PER_MSEC; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1023 | const char *fmt_v, *fmt_n; |
David Ahern | 4bbe5a6 | 2013-09-28 14:28:00 -0600 | [diff] [blame] | 1024 | char name[25]; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1025 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1026 | fmt_v = csv_output ? "%.6f%s" : "%18.6f%s"; |
| 1027 | fmt_n = csv_output ? "%s" : "%-25s"; |
| 1028 | |
Andi Kleen | da88c7f | 2014-09-24 13:50:46 -0700 | [diff] [blame] | 1029 | aggr_printout(evsel, id, nr); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1030 | |
David Ahern | 4bbe5a6 | 2013-09-28 14:28:00 -0600 | [diff] [blame] | 1031 | scnprintf(name, sizeof(name), "%s%s", |
| 1032 | perf_evsel__name(evsel), csv_output ? "" : " (msec)"); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1033 | |
| 1034 | fprintf(output, fmt_v, msecs, csv_sep); |
| 1035 | |
| 1036 | if (csv_output) |
| 1037 | fprintf(output, "%s%s", evsel->unit, csv_sep); |
| 1038 | else |
| 1039 | fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep); |
| 1040 | |
| 1041 | fprintf(output, fmt_n, name); |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1042 | |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 1043 | if (evsel->cgrp) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1044 | fprintf(output, "%s%s", csv_sep, evsel->cgrp->name); |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1045 | } |
| 1046 | |
Andi Kleen | 44d49a6 | 2016-02-29 14:36:22 -0800 | [diff] [blame] | 1047 | static int first_shadow_cpu(struct perf_evsel *evsel, int id) |
| 1048 | { |
| 1049 | int i; |
| 1050 | |
| 1051 | if (!aggr_get_id) |
| 1052 | return 0; |
| 1053 | |
| 1054 | if (stat_config.aggr_mode == AGGR_NONE) |
| 1055 | return id; |
| 1056 | |
| 1057 | if (stat_config.aggr_mode == AGGR_GLOBAL) |
| 1058 | return 0; |
| 1059 | |
| 1060 | for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) { |
| 1061 | int cpu2 = perf_evsel__cpus(evsel)->map[i]; |
| 1062 | |
| 1063 | if (aggr_get_id(evsel_list->cpus, cpu2) == id) |
| 1064 | return cpu2; |
| 1065 | } |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 1069 | static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg) |
| 1070 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1071 | FILE *output = stat_config.output; |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 1072 | double sc = evsel->scale; |
| 1073 | const char *fmt; |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 1074 | |
| 1075 | if (csv_output) { |
Andi Kleen | e3b03b6 | 2016-05-05 16:04:03 -0700 | [diff] [blame] | 1076 | fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s"; |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 1077 | } else { |
| 1078 | if (big_num) |
Andi Kleen | e3b03b6 | 2016-05-05 16:04:03 -0700 | [diff] [blame] | 1079 | fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s"; |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 1080 | else |
Andi Kleen | e3b03b6 | 2016-05-05 16:04:03 -0700 | [diff] [blame] | 1081 | fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s"; |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | aggr_printout(evsel, id, nr); |
| 1085 | |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 1086 | fprintf(output, fmt, avg, csv_sep); |
| 1087 | |
| 1088 | if (evsel->unit) |
| 1089 | fprintf(output, "%-*s%s", |
| 1090 | csv_output ? 0 : unit_width, |
| 1091 | evsel->unit, csv_sep); |
| 1092 | |
| 1093 | fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel)); |
| 1094 | |
| 1095 | if (evsel->cgrp) |
| 1096 | fprintf(output, "%s%s", csv_sep, evsel->cgrp->name); |
Andi Kleen | eedfcb4 | 2015-11-02 17:50:21 -0800 | [diff] [blame] | 1097 | } |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 1098 | |
Andi Kleen | f948339 | 2016-01-30 09:06:50 -0800 | [diff] [blame] | 1099 | static void printout(int id, int nr, struct perf_evsel *counter, double uval, |
Andi Kleen | cb110f4 | 2016-01-30 09:06:51 -0800 | [diff] [blame] | 1100 | char *prefix, u64 run, u64 ena, double noise) |
Andi Kleen | eedfcb4 | 2015-11-02 17:50:21 -0800 | [diff] [blame] | 1101 | { |
Andi Kleen | 140aead | 2016-01-30 09:06:49 -0800 | [diff] [blame] | 1102 | struct perf_stat_output_ctx out; |
Andi Kleen | f948339 | 2016-01-30 09:06:50 -0800 | [diff] [blame] | 1103 | struct outstate os = { |
| 1104 | .fh = stat_config.output, |
Andi Kleen | 44d49a6 | 2016-02-29 14:36:22 -0800 | [diff] [blame] | 1105 | .prefix = prefix ? prefix : "", |
| 1106 | .id = id, |
| 1107 | .nr = nr, |
| 1108 | .evsel = counter, |
Andi Kleen | f948339 | 2016-01-30 09:06:50 -0800 | [diff] [blame] | 1109 | }; |
Andi Kleen | 140aead | 2016-01-30 09:06:49 -0800 | [diff] [blame] | 1110 | print_metric_t pm = print_metric_std; |
| 1111 | void (*nl)(void *); |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 1112 | |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1113 | if (metric_only) { |
| 1114 | nl = new_line_metric; |
| 1115 | if (csv_output) |
| 1116 | pm = print_metric_only_csv; |
| 1117 | else |
| 1118 | pm = print_metric_only; |
| 1119 | } else |
| 1120 | nl = new_line_std; |
Andi Kleen | eedfcb4 | 2015-11-02 17:50:21 -0800 | [diff] [blame] | 1121 | |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1122 | if (csv_output && !metric_only) { |
Andi Kleen | 92a61f6 | 2016-02-29 14:36:21 -0800 | [diff] [blame] | 1123 | static int aggr_fields[] = { |
| 1124 | [AGGR_GLOBAL] = 0, |
| 1125 | [AGGR_THREAD] = 1, |
| 1126 | [AGGR_NONE] = 1, |
| 1127 | [AGGR_SOCKET] = 2, |
| 1128 | [AGGR_CORE] = 2, |
| 1129 | }; |
| 1130 | |
| 1131 | pm = print_metric_csv; |
| 1132 | nl = new_line_csv; |
| 1133 | os.nfields = 3; |
| 1134 | os.nfields += aggr_fields[stat_config.aggr_mode]; |
| 1135 | if (counter->cgrp) |
| 1136 | os.nfields++; |
| 1137 | } |
Andi Kleen | b002f3b | 2016-02-17 14:44:00 -0800 | [diff] [blame] | 1138 | if (run == 0 || ena == 0 || counter->counts->scaled == -1) { |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1139 | if (metric_only) { |
| 1140 | pm(&os, NULL, "", "", 0); |
| 1141 | return; |
| 1142 | } |
Andi Kleen | cb110f4 | 2016-01-30 09:06:51 -0800 | [diff] [blame] | 1143 | aggr_printout(counter, id, nr); |
| 1144 | |
| 1145 | fprintf(stat_config.output, "%*s%s", |
| 1146 | csv_output ? 0 : 18, |
| 1147 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, |
| 1148 | csv_sep); |
| 1149 | |
Borislav Petkov | 02d492e | 2017-02-07 01:40:05 +0100 | [diff] [blame] | 1150 | if (counter->supported) |
| 1151 | print_free_counters_hint = 1; |
| 1152 | |
Andi Kleen | cb110f4 | 2016-01-30 09:06:51 -0800 | [diff] [blame] | 1153 | fprintf(stat_config.output, "%-*s%s", |
| 1154 | csv_output ? 0 : unit_width, |
| 1155 | counter->unit, csv_sep); |
| 1156 | |
| 1157 | fprintf(stat_config.output, "%*s", |
| 1158 | csv_output ? 0 : -25, |
| 1159 | perf_evsel__name(counter)); |
| 1160 | |
| 1161 | if (counter->cgrp) |
| 1162 | fprintf(stat_config.output, "%s%s", |
| 1163 | csv_sep, counter->cgrp->name); |
| 1164 | |
Andi Kleen | 92a61f6 | 2016-02-29 14:36:21 -0800 | [diff] [blame] | 1165 | if (!csv_output) |
| 1166 | pm(&os, NULL, NULL, "", 0); |
| 1167 | print_noise(counter, noise); |
Andi Kleen | cb110f4 | 2016-01-30 09:06:51 -0800 | [diff] [blame] | 1168 | print_running(run, ena); |
Andi Kleen | 92a61f6 | 2016-02-29 14:36:21 -0800 | [diff] [blame] | 1169 | if (csv_output) |
| 1170 | pm(&os, NULL, NULL, "", 0); |
Andi Kleen | cb110f4 | 2016-01-30 09:06:51 -0800 | [diff] [blame] | 1171 | return; |
| 1172 | } |
| 1173 | |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1174 | if (metric_only) |
| 1175 | /* nothing */; |
| 1176 | else if (nsec_counter(counter)) |
Andi Kleen | eedfcb4 | 2015-11-02 17:50:21 -0800 | [diff] [blame] | 1177 | nsec_printout(id, nr, counter, uval); |
| 1178 | else |
| 1179 | abs_printout(id, nr, counter, uval); |
| 1180 | |
Andi Kleen | 140aead | 2016-01-30 09:06:49 -0800 | [diff] [blame] | 1181 | out.print_metric = pm; |
| 1182 | out.new_line = nl; |
| 1183 | out.ctx = &os; |
Andi Kleen | 37932c1 | 2017-03-20 13:17:08 -0700 | [diff] [blame] | 1184 | out.force_header = false; |
Andi Kleen | 140aead | 2016-01-30 09:06:49 -0800 | [diff] [blame] | 1185 | |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1186 | if (csv_output && !metric_only) { |
Andi Kleen | 92a61f6 | 2016-02-29 14:36:21 -0800 | [diff] [blame] | 1187 | print_noise(counter, noise); |
| 1188 | print_running(run, ena); |
| 1189 | } |
| 1190 | |
| 1191 | perf_stat__print_shadow_stats(counter, uval, |
Andi Kleen | 44d49a6 | 2016-02-29 14:36:22 -0800 | [diff] [blame] | 1192 | first_shadow_cpu(counter, id), |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1193 | &out, &metric_events); |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1194 | if (!csv_output && !metric_only) { |
Andi Kleen | 92a61f6 | 2016-02-29 14:36:21 -0800 | [diff] [blame] | 1195 | print_noise(counter, noise); |
| 1196 | print_running(run, ena); |
| 1197 | } |
Jiri Olsa | 556b1fb | 2015-06-03 16:25:56 +0200 | [diff] [blame] | 1198 | } |
| 1199 | |
Andi Kleen | 44d49a6 | 2016-02-29 14:36:22 -0800 | [diff] [blame] | 1200 | static void aggr_update_shadow(void) |
| 1201 | { |
| 1202 | int cpu, s2, id, s; |
| 1203 | u64 val; |
| 1204 | struct perf_evsel *counter; |
| 1205 | |
| 1206 | for (s = 0; s < aggr_map->nr; s++) { |
| 1207 | id = aggr_map->map[s]; |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1208 | evlist__for_each_entry(evsel_list, counter) { |
Andi Kleen | 44d49a6 | 2016-02-29 14:36:22 -0800 | [diff] [blame] | 1209 | val = 0; |
| 1210 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { |
| 1211 | s2 = aggr_get_id(evsel_list->cpus, cpu); |
| 1212 | if (s2 != id) |
| 1213 | continue; |
| 1214 | val += perf_counts(counter->counts, cpu, 0)->val; |
| 1215 | } |
Jiri Olsa | 54830dd | 2017-01-23 22:42:56 +0100 | [diff] [blame] | 1216 | perf_stat__update_shadow_stats(counter, val, |
Andi Kleen | 44d49a6 | 2016-02-29 14:36:22 -0800 | [diff] [blame] | 1217 | first_shadow_cpu(counter, id)); |
| 1218 | } |
| 1219 | } |
| 1220 | } |
| 1221 | |
Andi Kleen | 430daf2 | 2017-03-20 13:17:00 -0700 | [diff] [blame] | 1222 | static void collect_all_aliases(struct perf_evsel *counter, |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1223 | void (*cb)(struct perf_evsel *counter, void *data, |
| 1224 | bool first), |
| 1225 | void *data) |
| 1226 | { |
Andi Kleen | 430daf2 | 2017-03-20 13:17:00 -0700 | [diff] [blame] | 1227 | struct perf_evsel *alias; |
| 1228 | |
| 1229 | alias = list_prepare_entry(counter, &(evsel_list->entries), node); |
| 1230 | list_for_each_entry_continue (alias, &evsel_list->entries, node) { |
| 1231 | if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) || |
| 1232 | alias->scale != counter->scale || |
| 1233 | alias->cgrp != counter->cgrp || |
| 1234 | strcmp(alias->unit, counter->unit) || |
| 1235 | nsec_counter(alias) != nsec_counter(counter)) |
| 1236 | break; |
| 1237 | alias->merged_stat = true; |
| 1238 | cb(alias, data, false); |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | static bool collect_data(struct perf_evsel *counter, |
| 1243 | void (*cb)(struct perf_evsel *counter, void *data, |
| 1244 | bool first), |
| 1245 | void *data) |
| 1246 | { |
| 1247 | if (counter->merged_stat) |
| 1248 | return false; |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1249 | cb(counter, data, true); |
Arnaldo Carvalho de Melo | 63ce844 | 2017-08-31 15:32:18 -0300 | [diff] [blame] | 1250 | if (!no_merge && counter->auto_merge_stats) |
Andi Kleen | 430daf2 | 2017-03-20 13:17:00 -0700 | [diff] [blame] | 1251 | collect_all_aliases(counter, cb, data); |
| 1252 | return true; |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | struct aggr_data { |
| 1256 | u64 ena, run, val; |
| 1257 | int id; |
| 1258 | int nr; |
| 1259 | int cpu; |
| 1260 | }; |
| 1261 | |
| 1262 | static void aggr_cb(struct perf_evsel *counter, void *data, bool first) |
| 1263 | { |
| 1264 | struct aggr_data *ad = data; |
| 1265 | int cpu, s2; |
| 1266 | |
| 1267 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { |
| 1268 | struct perf_counts_values *counts; |
| 1269 | |
| 1270 | s2 = aggr_get_id(perf_evsel__cpus(counter), cpu); |
| 1271 | if (s2 != ad->id) |
| 1272 | continue; |
| 1273 | if (first) |
| 1274 | ad->nr++; |
| 1275 | counts = perf_counts(counter->counts, cpu, 0); |
Andi Kleen | b4229e9 | 2017-03-20 13:17:01 -0700 | [diff] [blame] | 1276 | /* |
| 1277 | * When any result is bad, make them all to give |
| 1278 | * consistent output in interval mode. |
| 1279 | */ |
| 1280 | if (counts->ena == 0 || counts->run == 0 || |
| 1281 | counter->counts->scaled == -1) { |
| 1282 | ad->ena = 0; |
| 1283 | ad->run = 0; |
| 1284 | break; |
| 1285 | } |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1286 | ad->val += counts->val; |
| 1287 | ad->ena += counts->ena; |
| 1288 | ad->run += counts->run; |
| 1289 | } |
| 1290 | } |
| 1291 | |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1292 | static void print_aggr(char *prefix) |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 1293 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1294 | FILE *output = stat_config.output; |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 1295 | struct perf_evsel *counter; |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1296 | int s, id, nr; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1297 | double uval; |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 1298 | u64 ena, run, val; |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1299 | bool first; |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 1300 | |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1301 | if (!(aggr_map || aggr_get_id)) |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 1302 | return; |
| 1303 | |
Andi Kleen | 44d49a6 | 2016-02-29 14:36:22 -0800 | [diff] [blame] | 1304 | aggr_update_shadow(); |
| 1305 | |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1306 | /* |
| 1307 | * With metric_only everything is on a single line. |
| 1308 | * Without each counter has its own line. |
| 1309 | */ |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1310 | for (s = 0; s < aggr_map->nr; s++) { |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1311 | struct aggr_data ad; |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1312 | if (prefix && metric_only) |
| 1313 | fprintf(output, "%s", prefix); |
| 1314 | |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1315 | ad.id = id = aggr_map->map[s]; |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1316 | first = true; |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1317 | evlist__for_each_entry(evsel_list, counter) { |
Andi Kleen | e864c5c | 2017-08-31 12:40:35 -0700 | [diff] [blame] | 1318 | if (is_duration_time(counter)) |
| 1319 | continue; |
| 1320 | |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1321 | ad.val = ad.ena = ad.run = 0; |
| 1322 | ad.nr = 0; |
Andi Kleen | 430daf2 | 2017-03-20 13:17:00 -0700 | [diff] [blame] | 1323 | if (!collect_data(counter, aggr_cb, &ad)) |
| 1324 | continue; |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1325 | nr = ad.nr; |
| 1326 | ena = ad.ena; |
| 1327 | run = ad.run; |
| 1328 | val = ad.val; |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1329 | if (first && metric_only) { |
| 1330 | first = false; |
| 1331 | aggr_printout(counter, id, nr); |
| 1332 | } |
| 1333 | if (prefix && !metric_only) |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 1334 | fprintf(output, "%s", prefix); |
| 1335 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1336 | uval = val * counter->scale; |
Andi Kleen | cb110f4 | 2016-01-30 09:06:51 -0800 | [diff] [blame] | 1337 | printout(id, nr, counter, uval, prefix, run, ena, 1.0); |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1338 | if (!metric_only) |
| 1339 | fputc('\n', output); |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 1340 | } |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1341 | if (metric_only) |
| 1342 | fputc('\n', output); |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1346 | static void print_aggr_thread(struct perf_evsel *counter, char *prefix) |
| 1347 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1348 | FILE *output = stat_config.output; |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1349 | int nthreads = thread_map__nr(counter->threads); |
| 1350 | int ncpus = cpu_map__nr(counter->cpus); |
| 1351 | int cpu, thread; |
| 1352 | double uval; |
| 1353 | |
| 1354 | for (thread = 0; thread < nthreads; thread++) { |
| 1355 | u64 ena = 0, run = 0, val = 0; |
| 1356 | |
| 1357 | for (cpu = 0; cpu < ncpus; cpu++) { |
| 1358 | val += perf_counts(counter->counts, cpu, thread)->val; |
| 1359 | ena += perf_counts(counter->counts, cpu, thread)->ena; |
| 1360 | run += perf_counts(counter->counts, cpu, thread)->run; |
| 1361 | } |
| 1362 | |
| 1363 | if (prefix) |
| 1364 | fprintf(output, "%s", prefix); |
| 1365 | |
| 1366 | uval = val * counter->scale; |
Andi Kleen | cb110f4 | 2016-01-30 09:06:51 -0800 | [diff] [blame] | 1367 | printout(thread, 0, counter, uval, prefix, run, ena, 1.0); |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1368 | fputc('\n', output); |
| 1369 | } |
| 1370 | } |
| 1371 | |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1372 | struct caggr_data { |
| 1373 | double avg, avg_enabled, avg_running; |
| 1374 | }; |
| 1375 | |
| 1376 | static void counter_aggr_cb(struct perf_evsel *counter, void *data, |
| 1377 | bool first __maybe_unused) |
| 1378 | { |
| 1379 | struct caggr_data *cd = data; |
Arnaldo Carvalho de Melo | e669e83 | 2017-10-26 14:22:34 -0300 | [diff] [blame] | 1380 | struct perf_stat_evsel *ps = counter->stats; |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1381 | |
| 1382 | cd->avg += avg_stats(&ps->res_stats[0]); |
| 1383 | cd->avg_enabled += avg_stats(&ps->res_stats[1]); |
| 1384 | cd->avg_running += avg_stats(&ps->res_stats[2]); |
| 1385 | } |
| 1386 | |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1387 | /* |
| 1388 | * Print out the results of a single counter: |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 1389 | * aggregated counts in system-wide mode |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1390 | */ |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 1391 | static void print_counter_aggr(struct perf_evsel *counter, char *prefix) |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1392 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1393 | FILE *output = stat_config.output; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1394 | double uval; |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1395 | struct caggr_data cd = { .avg = 0.0 }; |
Andi Kleen | d73515c | 2015-03-11 07:16:27 -0700 | [diff] [blame] | 1396 | |
Andi Kleen | 430daf2 | 2017-03-20 13:17:00 -0700 | [diff] [blame] | 1397 | if (!collect_data(counter, counter_aggr_cb, &cd)) |
| 1398 | return; |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1399 | |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1400 | if (prefix && !metric_only) |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 1401 | fprintf(output, "%s", prefix); |
| 1402 | |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1403 | uval = cd.avg * counter->scale; |
| 1404 | printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, cd.avg); |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1405 | if (!metric_only) |
| 1406 | fprintf(output, "\n"); |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1407 | } |
| 1408 | |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1409 | static void counter_cb(struct perf_evsel *counter, void *data, |
| 1410 | bool first __maybe_unused) |
| 1411 | { |
| 1412 | struct aggr_data *ad = data; |
| 1413 | |
| 1414 | ad->val += perf_counts(counter->counts, ad->cpu, 0)->val; |
| 1415 | ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena; |
| 1416 | ad->run += perf_counts(counter->counts, ad->cpu, 0)->run; |
| 1417 | } |
| 1418 | |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 1419 | /* |
| 1420 | * Print out the results of a single counter: |
| 1421 | * does not use aggregated count in system-wide |
| 1422 | */ |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 1423 | static void print_counter(struct perf_evsel *counter, char *prefix) |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 1424 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1425 | FILE *output = stat_config.output; |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 1426 | u64 ena, run, val; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1427 | double uval; |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 1428 | int cpu; |
| 1429 | |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 1430 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1431 | struct aggr_data ad = { .cpu = cpu }; |
| 1432 | |
Andi Kleen | 430daf2 | 2017-03-20 13:17:00 -0700 | [diff] [blame] | 1433 | if (!collect_data(counter, counter_cb, &ad)) |
| 1434 | return; |
Andi Kleen | fbe51fb | 2017-03-20 13:16:59 -0700 | [diff] [blame] | 1435 | val = ad.val; |
| 1436 | ena = ad.ena; |
| 1437 | run = ad.run; |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 1438 | |
| 1439 | if (prefix) |
| 1440 | fprintf(output, "%s", prefix); |
| 1441 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1442 | uval = val * counter->scale; |
Andi Kleen | cb110f4 | 2016-01-30 09:06:51 -0800 | [diff] [blame] | 1443 | printout(cpu, 0, counter, uval, prefix, run, ena, 1.0); |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 1444 | |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1445 | fputc('\n', output); |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | |
Andi Kleen | 206cab6 | 2016-03-03 15:57:37 -0800 | [diff] [blame] | 1449 | static void print_no_aggr_metric(char *prefix) |
| 1450 | { |
| 1451 | int cpu; |
| 1452 | int nrcpus = 0; |
| 1453 | struct perf_evsel *counter; |
| 1454 | u64 ena, run, val; |
| 1455 | double uval; |
| 1456 | |
| 1457 | nrcpus = evsel_list->cpus->nr; |
| 1458 | for (cpu = 0; cpu < nrcpus; cpu++) { |
| 1459 | bool first = true; |
| 1460 | |
| 1461 | if (prefix) |
| 1462 | fputs(prefix, stat_config.output); |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1463 | evlist__for_each_entry(evsel_list, counter) { |
Andi Kleen | e864c5c | 2017-08-31 12:40:35 -0700 | [diff] [blame] | 1464 | if (is_duration_time(counter)) |
| 1465 | continue; |
Andi Kleen | 206cab6 | 2016-03-03 15:57:37 -0800 | [diff] [blame] | 1466 | if (first) { |
| 1467 | aggr_printout(counter, cpu, 0); |
| 1468 | first = false; |
| 1469 | } |
| 1470 | val = perf_counts(counter->counts, cpu, 0)->val; |
| 1471 | ena = perf_counts(counter->counts, cpu, 0)->ena; |
| 1472 | run = perf_counts(counter->counts, cpu, 0)->run; |
| 1473 | |
| 1474 | uval = val * counter->scale; |
| 1475 | printout(cpu, 0, counter, uval, prefix, run, ena, 1.0); |
| 1476 | } |
| 1477 | fputc('\n', stat_config.output); |
| 1478 | } |
| 1479 | } |
| 1480 | |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1481 | static int aggr_header_lens[] = { |
| 1482 | [AGGR_CORE] = 18, |
| 1483 | [AGGR_SOCKET] = 12, |
Andi Kleen | 206cab6 | 2016-03-03 15:57:37 -0800 | [diff] [blame] | 1484 | [AGGR_NONE] = 6, |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1485 | [AGGR_THREAD] = 24, |
| 1486 | [AGGR_GLOBAL] = 0, |
| 1487 | }; |
| 1488 | |
Andi Kleen | c51fd63 | 2016-05-24 12:52:39 -0700 | [diff] [blame] | 1489 | static const char *aggr_header_csv[] = { |
| 1490 | [AGGR_CORE] = "core,cpus,", |
| 1491 | [AGGR_SOCKET] = "socket,cpus", |
| 1492 | [AGGR_NONE] = "cpu,", |
| 1493 | [AGGR_THREAD] = "comm-pid,", |
| 1494 | [AGGR_GLOBAL] = "" |
| 1495 | }; |
| 1496 | |
Andi Kleen | 41c8ca2 | 2016-05-24 12:52:38 -0700 | [diff] [blame] | 1497 | static void print_metric_headers(const char *prefix, bool no_indent) |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1498 | { |
| 1499 | struct perf_stat_output_ctx out; |
| 1500 | struct perf_evsel *counter; |
| 1501 | struct outstate os = { |
| 1502 | .fh = stat_config.output |
| 1503 | }; |
| 1504 | |
| 1505 | if (prefix) |
| 1506 | fprintf(stat_config.output, "%s", prefix); |
| 1507 | |
Andi Kleen | 41c8ca2 | 2016-05-24 12:52:38 -0700 | [diff] [blame] | 1508 | if (!csv_output && !no_indent) |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1509 | fprintf(stat_config.output, "%*s", |
| 1510 | aggr_header_lens[stat_config.aggr_mode], ""); |
Andi Kleen | c51fd63 | 2016-05-24 12:52:39 -0700 | [diff] [blame] | 1511 | if (csv_output) { |
| 1512 | if (stat_config.interval) |
| 1513 | fputs("time,", stat_config.output); |
| 1514 | fputs(aggr_header_csv[stat_config.aggr_mode], |
| 1515 | stat_config.output); |
| 1516 | } |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1517 | |
| 1518 | /* Print metrics headers only */ |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 1519 | evlist__for_each_entry(evsel_list, counter) { |
Andi Kleen | e864c5c | 2017-08-31 12:40:35 -0700 | [diff] [blame] | 1520 | if (is_duration_time(counter)) |
| 1521 | continue; |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1522 | os.evsel = counter; |
| 1523 | out.ctx = &os; |
| 1524 | out.print_metric = print_metric_header; |
| 1525 | out.new_line = new_line_metric; |
Andi Kleen | 37932c1 | 2017-03-20 13:17:08 -0700 | [diff] [blame] | 1526 | out.force_header = true; |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1527 | os.evsel = counter; |
| 1528 | perf_stat__print_shadow_stats(counter, 0, |
| 1529 | 0, |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1530 | &out, |
| 1531 | &metric_events); |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1532 | } |
| 1533 | fputc('\n', stat_config.output); |
| 1534 | } |
| 1535 | |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1536 | static void print_interval(char *prefix, struct timespec *ts) |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1537 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1538 | FILE *output = stat_config.output; |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1539 | static int num_print_interval; |
| 1540 | |
| 1541 | sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep); |
| 1542 | |
Andi Kleen | 41c8ca2 | 2016-05-24 12:52:38 -0700 | [diff] [blame] | 1543 | if (num_print_interval == 0 && !csv_output) { |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 1544 | switch (stat_config.aggr_mode) { |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1545 | case AGGR_SOCKET: |
Andi Kleen | 41c8ca2 | 2016-05-24 12:52:38 -0700 | [diff] [blame] | 1546 | fprintf(output, "# time socket cpus"); |
| 1547 | if (!metric_only) |
| 1548 | fprintf(output, " counts %*s events\n", unit_width, "unit"); |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1549 | break; |
| 1550 | case AGGR_CORE: |
Andi Kleen | 41c8ca2 | 2016-05-24 12:52:38 -0700 | [diff] [blame] | 1551 | fprintf(output, "# time core cpus"); |
| 1552 | if (!metric_only) |
| 1553 | fprintf(output, " counts %*s events\n", unit_width, "unit"); |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1554 | break; |
| 1555 | case AGGR_NONE: |
Andi Kleen | 41c8ca2 | 2016-05-24 12:52:38 -0700 | [diff] [blame] | 1556 | fprintf(output, "# time CPU"); |
| 1557 | if (!metric_only) |
| 1558 | fprintf(output, " counts %*s events\n", unit_width, "unit"); |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1559 | break; |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1560 | case AGGR_THREAD: |
Andi Kleen | 41c8ca2 | 2016-05-24 12:52:38 -0700 | [diff] [blame] | 1561 | fprintf(output, "# time comm-pid"); |
| 1562 | if (!metric_only) |
| 1563 | fprintf(output, " counts %*s events\n", unit_width, "unit"); |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1564 | break; |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1565 | case AGGR_GLOBAL: |
| 1566 | default: |
Andi Kleen | 41c8ca2 | 2016-05-24 12:52:38 -0700 | [diff] [blame] | 1567 | fprintf(output, "# time"); |
| 1568 | if (!metric_only) |
| 1569 | fprintf(output, " counts %*s events\n", unit_width, "unit"); |
Jiri Olsa | 208df99 | 2015-10-16 12:41:04 +0200 | [diff] [blame] | 1570 | case AGGR_UNSET: |
| 1571 | break; |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | |
Andi Kleen | 41c8ca2 | 2016-05-24 12:52:38 -0700 | [diff] [blame] | 1575 | if (num_print_interval == 0 && metric_only) |
| 1576 | print_metric_headers(" ", true); |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1577 | if (++num_print_interval == 25) |
| 1578 | num_print_interval = 0; |
| 1579 | } |
| 1580 | |
| 1581 | static void print_header(int argc, const char **argv) |
| 1582 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1583 | FILE *output = stat_config.output; |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1584 | int i; |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 1585 | |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1586 | fflush(stdout); |
| 1587 | |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1588 | if (!csv_output) { |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1589 | fprintf(output, "\n"); |
| 1590 | fprintf(output, " Performance counter stats for "); |
David Ahern | 62d3b61 | 2013-09-28 14:27:58 -0600 | [diff] [blame] | 1591 | if (target.system_wide) |
| 1592 | fprintf(output, "\'system wide"); |
| 1593 | else if (target.cpu_list) |
| 1594 | fprintf(output, "\'CPU(s) %s", target.cpu_list); |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 1595 | else if (!target__has_task(&target)) { |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 1596 | fprintf(output, "\'%s", argv ? argv[0] : "pipe"); |
| 1597 | for (i = 1; argv && (i < argc); i++) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1598 | fprintf(output, " %s", argv[i]); |
Namhyung Kim | 20f946b | 2012-04-26 14:15:16 +0900 | [diff] [blame] | 1599 | } else if (target.pid) |
| 1600 | fprintf(output, "process id \'%s", target.pid); |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1601 | else |
Namhyung Kim | 20f946b | 2012-04-26 14:15:16 +0900 | [diff] [blame] | 1602 | fprintf(output, "thread id \'%s", target.tid); |
Ingo Molnar | 44db76c | 2009-06-03 19:36:07 +0200 | [diff] [blame] | 1603 | |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1604 | fprintf(output, "\'"); |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1605 | if (run_count > 1) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 1606 | fprintf(output, " (%d runs)", run_count); |
| 1607 | fprintf(output, ":\n\n"); |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1608 | } |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | static void print_footer(void) |
| 1612 | { |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1613 | FILE *output = stat_config.output; |
Andi Kleen | 918c7b06 | 2017-05-22 18:00:16 -0700 | [diff] [blame] | 1614 | int n; |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1615 | |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1616 | if (!null_run) |
| 1617 | fprintf(output, "\n"); |
| 1618 | fprintf(output, " %17.9f seconds time elapsed", |
Arnaldo Carvalho de Melo | 310ebb9 | 2016-08-08 14:57:04 -0300 | [diff] [blame] | 1619 | avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC); |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1620 | if (run_count > 1) { |
| 1621 | fprintf(output, " "); |
| 1622 | print_noise_pct(stddev_stats(&walltime_nsecs_stats), |
| 1623 | avg_stats(&walltime_nsecs_stats)); |
| 1624 | } |
| 1625 | fprintf(output, "\n\n"); |
Borislav Petkov | 02d492e | 2017-02-07 01:40:05 +0100 | [diff] [blame] | 1626 | |
Andi Kleen | 918c7b06 | 2017-05-22 18:00:16 -0700 | [diff] [blame] | 1627 | if (print_free_counters_hint && |
| 1628 | sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 && |
| 1629 | n > 0) |
Borislav Petkov | 02d492e | 2017-02-07 01:40:05 +0100 | [diff] [blame] | 1630 | fprintf(output, |
| 1631 | "Some events weren't counted. Try disabling the NMI watchdog:\n" |
| 1632 | " echo 0 > /proc/sys/kernel/nmi_watchdog\n" |
| 1633 | " perf stat ...\n" |
| 1634 | " echo 1 > /proc/sys/kernel/nmi_watchdog\n"); |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1635 | } |
| 1636 | |
| 1637 | static void print_counters(struct timespec *ts, int argc, const char **argv) |
| 1638 | { |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 1639 | int interval = stat_config.interval; |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1640 | struct perf_evsel *counter; |
| 1641 | char buf[64], *prefix = NULL; |
| 1642 | |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 1643 | /* Do not print anything if we record to the pipe. */ |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 1644 | if (STAT_RECORD && perf_stat.data.is_pipe) |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 1645 | return; |
| 1646 | |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1647 | if (interval) |
| 1648 | print_interval(prefix = buf, ts); |
| 1649 | else |
| 1650 | print_header(argc, argv); |
Ingo Molnar | 2996f5d | 2009-05-29 09:10:54 +0200 | [diff] [blame] | 1651 | |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1652 | if (metric_only) { |
| 1653 | static int num_print_iv; |
| 1654 | |
Andi Kleen | 41c8ca2 | 2016-05-24 12:52:38 -0700 | [diff] [blame] | 1655 | if (num_print_iv == 0 && !interval) |
| 1656 | print_metric_headers(prefix, false); |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1657 | if (num_print_iv++ == 25) |
| 1658 | num_print_iv = 0; |
| 1659 | if (stat_config.aggr_mode == AGGR_GLOBAL && prefix) |
| 1660 | fprintf(stat_config.output, "%s", prefix); |
| 1661 | } |
| 1662 | |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 1663 | switch (stat_config.aggr_mode) { |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 1664 | case AGGR_CORE: |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1665 | case AGGR_SOCKET: |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1666 | print_aggr(prefix); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1667 | break; |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1668 | case AGGR_THREAD: |
Andi Kleen | e864c5c | 2017-08-31 12:40:35 -0700 | [diff] [blame] | 1669 | evlist__for_each_entry(evsel_list, counter) { |
| 1670 | if (is_duration_time(counter)) |
| 1671 | continue; |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1672 | print_aggr_thread(counter, prefix); |
Andi Kleen | e864c5c | 2017-08-31 12:40:35 -0700 | [diff] [blame] | 1673 | } |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1674 | break; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1675 | case AGGR_GLOBAL: |
Andi Kleen | e864c5c | 2017-08-31 12:40:35 -0700 | [diff] [blame] | 1676 | evlist__for_each_entry(evsel_list, counter) { |
| 1677 | if (is_duration_time(counter)) |
| 1678 | continue; |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1679 | print_counter_aggr(counter, prefix); |
Andi Kleen | e864c5c | 2017-08-31 12:40:35 -0700 | [diff] [blame] | 1680 | } |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 1681 | if (metric_only) |
| 1682 | fputc('\n', stat_config.output); |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1683 | break; |
| 1684 | case AGGR_NONE: |
Andi Kleen | 206cab6 | 2016-03-03 15:57:37 -0800 | [diff] [blame] | 1685 | if (metric_only) |
| 1686 | print_no_aggr_metric(prefix); |
| 1687 | else { |
Andi Kleen | e864c5c | 2017-08-31 12:40:35 -0700 | [diff] [blame] | 1688 | evlist__for_each_entry(evsel_list, counter) { |
| 1689 | if (is_duration_time(counter)) |
| 1690 | continue; |
Andi Kleen | 206cab6 | 2016-03-03 15:57:37 -0800 | [diff] [blame] | 1691 | print_counter(counter, prefix); |
Andi Kleen | e864c5c | 2017-08-31 12:40:35 -0700 | [diff] [blame] | 1692 | } |
Andi Kleen | 206cab6 | 2016-03-03 15:57:37 -0800 | [diff] [blame] | 1693 | } |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1694 | break; |
Jiri Olsa | 208df99 | 2015-10-16 12:41:04 +0200 | [diff] [blame] | 1695 | case AGGR_UNSET: |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1696 | default: |
| 1697 | break; |
Stephane Eranian | f5b4a9c3 | 2010-11-16 11:05:01 +0200 | [diff] [blame] | 1698 | } |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1699 | |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 1700 | if (!interval && !csv_output) |
| 1701 | print_footer(); |
| 1702 | |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 1703 | fflush(stat_config.output); |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1704 | } |
| 1705 | |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 1706 | static volatile int signr = -1; |
| 1707 | |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1708 | static void skip_signal(int signo) |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1709 | { |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 1710 | if ((child_pid == -1) || stat_config.interval) |
Liming Wang | 60666c6 | 2009-12-31 16:05:50 +0800 | [diff] [blame] | 1711 | done = 1; |
| 1712 | |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 1713 | signr = signo; |
Stephane Eranian | d07f0b1 | 2013-06-04 17:44:26 +0200 | [diff] [blame] | 1714 | /* |
| 1715 | * render child_pid harmless |
| 1716 | * won't send SIGTERM to a random |
| 1717 | * process in case of race condition |
| 1718 | * and fast PID recycling |
| 1719 | */ |
| 1720 | child_pid = -1; |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | static void sig_atexit(void) |
| 1724 | { |
Stephane Eranian | d07f0b1 | 2013-06-04 17:44:26 +0200 | [diff] [blame] | 1725 | sigset_t set, oset; |
| 1726 | |
| 1727 | /* |
| 1728 | * avoid race condition with SIGCHLD handler |
| 1729 | * in skip_signal() which is modifying child_pid |
| 1730 | * goal is to avoid send SIGTERM to a random |
| 1731 | * process |
| 1732 | */ |
| 1733 | sigemptyset(&set); |
| 1734 | sigaddset(&set, SIGCHLD); |
| 1735 | sigprocmask(SIG_BLOCK, &set, &oset); |
| 1736 | |
Chris Wilson | 933da83 | 2009-10-04 01:35:01 +0100 | [diff] [blame] | 1737 | if (child_pid != -1) |
| 1738 | kill(child_pid, SIGTERM); |
| 1739 | |
Stephane Eranian | d07f0b1 | 2013-06-04 17:44:26 +0200 | [diff] [blame] | 1740 | sigprocmask(SIG_SETMASK, &oset, NULL); |
| 1741 | |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 1742 | if (signr == -1) |
| 1743 | return; |
| 1744 | |
| 1745 | signal(signr, SIG_DFL); |
| 1746 | kill(getpid(), signr); |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 1747 | } |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 1748 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1749 | static int stat__set_big_num(const struct option *opt __maybe_unused, |
| 1750 | const char *s __maybe_unused, int unset) |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 1751 | { |
| 1752 | big_num_opt = unset ? 0 : 1; |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 1756 | static int enable_metric_only(const struct option *opt __maybe_unused, |
| 1757 | const char *s __maybe_unused, int unset) |
| 1758 | { |
| 1759 | force_metric_only = true; |
| 1760 | metric_only = !unset; |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1764 | static int parse_metric_groups(const struct option *opt, |
| 1765 | const char *str, |
| 1766 | int unset __maybe_unused) |
| 1767 | { |
| 1768 | return metricgroup__parse_groups(opt, str, &metric_events); |
| 1769 | } |
| 1770 | |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1771 | static const struct option stat_options[] = { |
| 1772 | OPT_BOOLEAN('T', "transaction", &transaction_run, |
| 1773 | "hardware transaction statistics"), |
| 1774 | OPT_CALLBACK('e', "event", &evsel_list, "event", |
| 1775 | "event selector. use 'perf list' to list available events", |
| 1776 | parse_events_option), |
| 1777 | OPT_CALLBACK(0, "filter", &evsel_list, "filter", |
| 1778 | "event filter", parse_filter), |
| 1779 | OPT_BOOLEAN('i', "no-inherit", &no_inherit, |
| 1780 | "child tasks do not inherit counters"), |
| 1781 | OPT_STRING('p', "pid", &target.pid, "pid", |
| 1782 | "stat events on existing process id"), |
| 1783 | OPT_STRING('t', "tid", &target.tid, "tid", |
| 1784 | "stat events on existing thread id"), |
| 1785 | OPT_BOOLEAN('a', "all-cpus", &target.system_wide, |
| 1786 | "system-wide collection from all CPUs"), |
| 1787 | OPT_BOOLEAN('g', "group", &group, |
| 1788 | "put the counters into a counter group"), |
| 1789 | OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"), |
| 1790 | OPT_INCR('v', "verbose", &verbose, |
| 1791 | "be more verbose (show counter open errors, etc)"), |
| 1792 | OPT_INTEGER('r', "repeat", &run_count, |
| 1793 | "repeat command and print average + stddev (max: 100, forever: 0)"), |
| 1794 | OPT_BOOLEAN('n', "null", &null_run, |
| 1795 | "null run - dont start any counters"), |
| 1796 | OPT_INCR('d', "detailed", &detailed_run, |
| 1797 | "detailed run - start a lot of events"), |
| 1798 | OPT_BOOLEAN('S', "sync", &sync_run, |
| 1799 | "call sync() before starting a run"), |
| 1800 | OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL, |
| 1801 | "print large numbers with thousands\' separators", |
| 1802 | stat__set_big_num), |
| 1803 | OPT_STRING('C', "cpu", &target.cpu_list, "cpu", |
| 1804 | "list of cpus to monitor in system-wide"), |
| 1805 | OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode, |
| 1806 | "disable CPU count aggregation", AGGR_NONE), |
Andi Kleen | 430daf2 | 2017-03-20 13:17:00 -0700 | [diff] [blame] | 1807 | OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"), |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1808 | OPT_STRING('x', "field-separator", &csv_sep, "separator", |
| 1809 | "print counts with custom separator"), |
| 1810 | OPT_CALLBACK('G', "cgroup", &evsel_list, "name", |
| 1811 | "monitor event in cgroup name only", parse_cgroups), |
| 1812 | OPT_STRING('o', "output", &output_name, "file", "output file name"), |
| 1813 | OPT_BOOLEAN(0, "append", &append_file, "append to the output file"), |
| 1814 | OPT_INTEGER(0, "log-fd", &output_fd, |
| 1815 | "log output to fd, instead of stderr"), |
| 1816 | OPT_STRING(0, "pre", &pre_cmd, "command", |
| 1817 | "command to run prior to the measured command"), |
| 1818 | OPT_STRING(0, "post", &post_cmd, "command", |
| 1819 | "command to run after to the measured command"), |
| 1820 | OPT_UINTEGER('I', "interval-print", &stat_config.interval, |
| 1821 | "print counts at regular interval in ms (>= 10)"), |
| 1822 | OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode, |
| 1823 | "aggregate counts per processor socket", AGGR_SOCKET), |
| 1824 | OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode, |
| 1825 | "aggregate counts per physical processor core", AGGR_CORE), |
| 1826 | OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode, |
| 1827 | "aggregate counts per thread", AGGR_THREAD), |
| 1828 | OPT_UINTEGER('D', "delay", &initial_delay, |
| 1829 | "ms to wait before starting measurement after program start"), |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 1830 | OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL, |
| 1831 | "Only print computed metrics. No raw values", enable_metric_only), |
| 1832 | OPT_BOOLEAN(0, "topdown", &topdown_run, |
| 1833 | "measure topdown level 1 statistics"), |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 1834 | OPT_BOOLEAN(0, "smi-cost", &smi_cost, |
| 1835 | "measure SMI cost"), |
Andi Kleen | b18f3e3 | 2017-08-31 12:40:31 -0700 | [diff] [blame] | 1836 | OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list", |
| 1837 | "monitor specified metrics or metric groups (separated by ,)", |
| 1838 | parse_metric_groups), |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 1839 | OPT_END() |
| 1840 | }; |
| 1841 | |
Jiri Olsa | 1fe7a30 | 2015-10-16 12:41:15 +0200 | [diff] [blame] | 1842 | static int perf_stat__get_socket(struct cpu_map *map, int cpu) |
| 1843 | { |
| 1844 | return cpu_map__get_socket(map, cpu, NULL); |
| 1845 | } |
| 1846 | |
| 1847 | static int perf_stat__get_core(struct cpu_map *map, int cpu) |
| 1848 | { |
| 1849 | return cpu_map__get_core(map, cpu, NULL); |
| 1850 | } |
| 1851 | |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1852 | static int cpu_map__get_max(struct cpu_map *map) |
| 1853 | { |
| 1854 | int i, max = -1; |
| 1855 | |
| 1856 | for (i = 0; i < map->nr; i++) { |
| 1857 | if (map->map[i] > max) |
| 1858 | max = map->map[i]; |
| 1859 | } |
| 1860 | |
| 1861 | return max; |
| 1862 | } |
| 1863 | |
| 1864 | static struct cpu_map *cpus_aggr_map; |
| 1865 | |
| 1866 | static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx) |
| 1867 | { |
| 1868 | int cpu; |
| 1869 | |
| 1870 | if (idx >= map->nr) |
| 1871 | return -1; |
| 1872 | |
| 1873 | cpu = map->map[idx]; |
| 1874 | |
| 1875 | if (cpus_aggr_map->map[cpu] == -1) |
| 1876 | cpus_aggr_map->map[cpu] = get_id(map, idx); |
| 1877 | |
| 1878 | return cpus_aggr_map->map[cpu]; |
| 1879 | } |
| 1880 | |
| 1881 | static int perf_stat__get_socket_cached(struct cpu_map *map, int idx) |
| 1882 | { |
| 1883 | return perf_stat__get_aggr(perf_stat__get_socket, map, idx); |
| 1884 | } |
| 1885 | |
| 1886 | static int perf_stat__get_core_cached(struct cpu_map *map, int idx) |
| 1887 | { |
| 1888 | return perf_stat__get_aggr(perf_stat__get_core, map, idx); |
| 1889 | } |
| 1890 | |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1891 | static int perf_stat_init_aggr_mode(void) |
| 1892 | { |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1893 | int nr; |
| 1894 | |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 1895 | switch (stat_config.aggr_mode) { |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1896 | case AGGR_SOCKET: |
| 1897 | if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) { |
| 1898 | perror("cannot build socket map"); |
| 1899 | return -1; |
| 1900 | } |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1901 | aggr_get_id = perf_stat__get_socket_cached; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1902 | break; |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 1903 | case AGGR_CORE: |
| 1904 | if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) { |
| 1905 | perror("cannot build core map"); |
| 1906 | return -1; |
| 1907 | } |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1908 | aggr_get_id = perf_stat__get_core_cached; |
Stephane Eranian | 12c08a9 | 2013-02-14 13:57:29 +0100 | [diff] [blame] | 1909 | break; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1910 | case AGGR_NONE: |
| 1911 | case AGGR_GLOBAL: |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 1912 | case AGGR_THREAD: |
Jiri Olsa | 208df99 | 2015-10-16 12:41:04 +0200 | [diff] [blame] | 1913 | case AGGR_UNSET: |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1914 | default: |
| 1915 | break; |
| 1916 | } |
Jiri Olsa | 1e5a293 | 2015-10-25 15:51:18 +0100 | [diff] [blame] | 1917 | |
| 1918 | /* |
| 1919 | * The evsel_list->cpus is the base we operate on, |
| 1920 | * taking the highest cpu number to be the size of |
| 1921 | * the aggregation translate cpumap. |
| 1922 | */ |
| 1923 | nr = cpu_map__get_max(evsel_list->cpus); |
| 1924 | cpus_aggr_map = cpu_map__empty_new(nr + 1); |
| 1925 | return cpus_aggr_map ? 0 : -ENOMEM; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 1926 | } |
| 1927 | |
Masami Hiramatsu | 544c2ae | 2015-12-09 11:11:27 +0900 | [diff] [blame] | 1928 | static void perf_stat__exit_aggr_mode(void) |
| 1929 | { |
| 1930 | cpu_map__put(aggr_map); |
| 1931 | cpu_map__put(cpus_aggr_map); |
| 1932 | aggr_map = NULL; |
| 1933 | cpus_aggr_map = NULL; |
| 1934 | } |
| 1935 | |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1936 | static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx) |
| 1937 | { |
| 1938 | int cpu; |
| 1939 | |
| 1940 | if (idx > map->nr) |
| 1941 | return -1; |
| 1942 | |
| 1943 | cpu = map->map[idx]; |
| 1944 | |
Jan Stancek | da8a58b | 2017-02-17 12:10:26 +0100 | [diff] [blame] | 1945 | if (cpu >= env->nr_cpus_avail) |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 1946 | return -1; |
| 1947 | |
| 1948 | return cpu; |
| 1949 | } |
| 1950 | |
| 1951 | static int perf_env__get_socket(struct cpu_map *map, int idx, void *data) |
| 1952 | { |
| 1953 | struct perf_env *env = data; |
| 1954 | int cpu = perf_env__get_cpu(env, map, idx); |
| 1955 | |
| 1956 | return cpu == -1 ? -1 : env->cpu[cpu].socket_id; |
| 1957 | } |
| 1958 | |
| 1959 | static int perf_env__get_core(struct cpu_map *map, int idx, void *data) |
| 1960 | { |
| 1961 | struct perf_env *env = data; |
| 1962 | int core = -1, cpu = perf_env__get_cpu(env, map, idx); |
| 1963 | |
| 1964 | if (cpu != -1) { |
| 1965 | int socket_id = env->cpu[cpu].socket_id; |
| 1966 | |
| 1967 | /* |
| 1968 | * Encode socket in upper 16 bits |
| 1969 | * core_id is relative to socket, and |
| 1970 | * we need a global id. So we combine |
| 1971 | * socket + core id. |
| 1972 | */ |
| 1973 | core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff); |
| 1974 | } |
| 1975 | |
| 1976 | return core; |
| 1977 | } |
| 1978 | |
| 1979 | static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus, |
| 1980 | struct cpu_map **sockp) |
| 1981 | { |
| 1982 | return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env); |
| 1983 | } |
| 1984 | |
| 1985 | static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus, |
| 1986 | struct cpu_map **corep) |
| 1987 | { |
| 1988 | return cpu_map__build_map(cpus, corep, perf_env__get_core, env); |
| 1989 | } |
| 1990 | |
| 1991 | static int perf_stat__get_socket_file(struct cpu_map *map, int idx) |
| 1992 | { |
| 1993 | return perf_env__get_socket(map, idx, &perf_stat.session->header.env); |
| 1994 | } |
| 1995 | |
| 1996 | static int perf_stat__get_core_file(struct cpu_map *map, int idx) |
| 1997 | { |
| 1998 | return perf_env__get_core(map, idx, &perf_stat.session->header.env); |
| 1999 | } |
| 2000 | |
| 2001 | static int perf_stat_init_aggr_mode_file(struct perf_stat *st) |
| 2002 | { |
| 2003 | struct perf_env *env = &st->session->header.env; |
| 2004 | |
| 2005 | switch (stat_config.aggr_mode) { |
| 2006 | case AGGR_SOCKET: |
| 2007 | if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) { |
| 2008 | perror("cannot build socket map"); |
| 2009 | return -1; |
| 2010 | } |
| 2011 | aggr_get_id = perf_stat__get_socket_file; |
| 2012 | break; |
| 2013 | case AGGR_CORE: |
| 2014 | if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) { |
| 2015 | perror("cannot build core map"); |
| 2016 | return -1; |
| 2017 | } |
| 2018 | aggr_get_id = perf_stat__get_core_file; |
| 2019 | break; |
| 2020 | case AGGR_NONE: |
| 2021 | case AGGR_GLOBAL: |
| 2022 | case AGGR_THREAD: |
| 2023 | case AGGR_UNSET: |
| 2024 | default: |
| 2025 | break; |
| 2026 | } |
| 2027 | |
| 2028 | return 0; |
| 2029 | } |
| 2030 | |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 2031 | static int topdown_filter_events(const char **attr, char **str, bool use_group) |
| 2032 | { |
| 2033 | int off = 0; |
| 2034 | int i; |
| 2035 | int len = 0; |
| 2036 | char *s; |
| 2037 | |
| 2038 | for (i = 0; attr[i]; i++) { |
| 2039 | if (pmu_have_event("cpu", attr[i])) { |
| 2040 | len += strlen(attr[i]) + 1; |
| 2041 | attr[i - off] = attr[i]; |
| 2042 | } else |
| 2043 | off++; |
| 2044 | } |
| 2045 | attr[i - off] = NULL; |
| 2046 | |
| 2047 | *str = malloc(len + 1 + 2); |
| 2048 | if (!*str) |
| 2049 | return -1; |
| 2050 | s = *str; |
| 2051 | if (i - off == 0) { |
| 2052 | *s = 0; |
| 2053 | return 0; |
| 2054 | } |
| 2055 | if (use_group) |
| 2056 | *s++ = '{'; |
| 2057 | for (i = 0; attr[i]; i++) { |
| 2058 | strcpy(s, attr[i]); |
| 2059 | s += strlen(s); |
| 2060 | *s++ = ','; |
| 2061 | } |
| 2062 | if (use_group) { |
| 2063 | s[-1] = '}'; |
| 2064 | *s = 0; |
| 2065 | } else |
| 2066 | s[-1] = 0; |
| 2067 | return 0; |
| 2068 | } |
| 2069 | |
| 2070 | __weak bool arch_topdown_check_group(bool *warn) |
| 2071 | { |
| 2072 | *warn = false; |
| 2073 | return false; |
| 2074 | } |
| 2075 | |
| 2076 | __weak void arch_topdown_group_warn(void) |
| 2077 | { |
| 2078 | } |
| 2079 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 2080 | /* |
| 2081 | * Add default attributes, if there were no attributes specified or |
| 2082 | * if -d/--detailed, -d -d or -d -d -d is used: |
| 2083 | */ |
| 2084 | static int add_default_attributes(void) |
| 2085 | { |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 2086 | int err; |
Andi Kleen | 9dec447 | 2016-02-26 16:27:56 -0800 | [diff] [blame] | 2087 | struct perf_event_attr default_attrs0[] = { |
Arnaldo Carvalho de Melo | b070a547 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 2088 | |
| 2089 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK }, |
| 2090 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES }, |
| 2091 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS }, |
| 2092 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS }, |
| 2093 | |
| 2094 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES }, |
Andi Kleen | 9dec447 | 2016-02-26 16:27:56 -0800 | [diff] [blame] | 2095 | }; |
| 2096 | struct perf_event_attr frontend_attrs[] = { |
Arnaldo Carvalho de Melo | b070a547 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 2097 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND }, |
Andi Kleen | 9dec447 | 2016-02-26 16:27:56 -0800 | [diff] [blame] | 2098 | }; |
| 2099 | struct perf_event_attr backend_attrs[] = { |
Arnaldo Carvalho de Melo | b070a547 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 2100 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND }, |
Andi Kleen | 9dec447 | 2016-02-26 16:27:56 -0800 | [diff] [blame] | 2101 | }; |
| 2102 | struct perf_event_attr default_attrs1[] = { |
Arnaldo Carvalho de Melo | b070a547 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 2103 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS }, |
| 2104 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, |
| 2105 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES }, |
| 2106 | |
| 2107 | }; |
| 2108 | |
| 2109 | /* |
| 2110 | * Detailed stats (-d), covering the L1 and last level data caches: |
| 2111 | */ |
| 2112 | struct perf_event_attr detailed_attrs[] = { |
| 2113 | |
| 2114 | { .type = PERF_TYPE_HW_CACHE, |
| 2115 | .config = |
| 2116 | PERF_COUNT_HW_CACHE_L1D << 0 | |
| 2117 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 2118 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, |
| 2119 | |
| 2120 | { .type = PERF_TYPE_HW_CACHE, |
| 2121 | .config = |
| 2122 | PERF_COUNT_HW_CACHE_L1D << 0 | |
| 2123 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 2124 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, |
| 2125 | |
| 2126 | { .type = PERF_TYPE_HW_CACHE, |
| 2127 | .config = |
| 2128 | PERF_COUNT_HW_CACHE_LL << 0 | |
| 2129 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 2130 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, |
| 2131 | |
| 2132 | { .type = PERF_TYPE_HW_CACHE, |
| 2133 | .config = |
| 2134 | PERF_COUNT_HW_CACHE_LL << 0 | |
| 2135 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 2136 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, |
| 2137 | }; |
| 2138 | |
| 2139 | /* |
| 2140 | * Very detailed stats (-d -d), covering the instruction cache and the TLB caches: |
| 2141 | */ |
| 2142 | struct perf_event_attr very_detailed_attrs[] = { |
| 2143 | |
| 2144 | { .type = PERF_TYPE_HW_CACHE, |
| 2145 | .config = |
| 2146 | PERF_COUNT_HW_CACHE_L1I << 0 | |
| 2147 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 2148 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, |
| 2149 | |
| 2150 | { .type = PERF_TYPE_HW_CACHE, |
| 2151 | .config = |
| 2152 | PERF_COUNT_HW_CACHE_L1I << 0 | |
| 2153 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 2154 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, |
| 2155 | |
| 2156 | { .type = PERF_TYPE_HW_CACHE, |
| 2157 | .config = |
| 2158 | PERF_COUNT_HW_CACHE_DTLB << 0 | |
| 2159 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 2160 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, |
| 2161 | |
| 2162 | { .type = PERF_TYPE_HW_CACHE, |
| 2163 | .config = |
| 2164 | PERF_COUNT_HW_CACHE_DTLB << 0 | |
| 2165 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 2166 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, |
| 2167 | |
| 2168 | { .type = PERF_TYPE_HW_CACHE, |
| 2169 | .config = |
| 2170 | PERF_COUNT_HW_CACHE_ITLB << 0 | |
| 2171 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 2172 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, |
| 2173 | |
| 2174 | { .type = PERF_TYPE_HW_CACHE, |
| 2175 | .config = |
| 2176 | PERF_COUNT_HW_CACHE_ITLB << 0 | |
| 2177 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | |
| 2178 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, |
| 2179 | |
| 2180 | }; |
| 2181 | |
| 2182 | /* |
| 2183 | * Very, very detailed stats (-d -d -d), adding prefetch events: |
| 2184 | */ |
| 2185 | struct perf_event_attr very_very_detailed_attrs[] = { |
| 2186 | |
| 2187 | { .type = PERF_TYPE_HW_CACHE, |
| 2188 | .config = |
| 2189 | PERF_COUNT_HW_CACHE_L1D << 0 | |
| 2190 | (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) | |
| 2191 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, |
| 2192 | |
| 2193 | { .type = PERF_TYPE_HW_CACHE, |
| 2194 | .config = |
| 2195 | PERF_COUNT_HW_CACHE_L1D << 0 | |
| 2196 | (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) | |
| 2197 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, |
| 2198 | }; |
| 2199 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 2200 | /* Set attrs if no event is selected and !null_run: */ |
| 2201 | if (null_run) |
| 2202 | return 0; |
| 2203 | |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 2204 | if (transaction_run) { |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 2205 | if (pmu_have_event("cpu", "cycles-ct") && |
| 2206 | pmu_have_event("cpu", "el-start")) |
Jiri Olsa | a454742 | 2015-06-03 16:25:53 +0200 | [diff] [blame] | 2207 | err = parse_events(evsel_list, transaction_attrs, NULL); |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 2208 | else |
Jiri Olsa | a454742 | 2015-06-03 16:25:53 +0200 | [diff] [blame] | 2209 | err = parse_events(evsel_list, transaction_limited_attrs, NULL); |
| 2210 | if (err) { |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 2211 | fprintf(stderr, "Cannot set up transaction events\n"); |
| 2212 | return -1; |
| 2213 | } |
| 2214 | return 0; |
| 2215 | } |
| 2216 | |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 2217 | if (smi_cost) { |
| 2218 | int smi; |
| 2219 | |
| 2220 | if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) { |
| 2221 | fprintf(stderr, "freeze_on_smi is not supported.\n"); |
| 2222 | return -1; |
| 2223 | } |
| 2224 | |
| 2225 | if (!smi) { |
| 2226 | if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) { |
| 2227 | fprintf(stderr, "Failed to set freeze_on_smi.\n"); |
| 2228 | return -1; |
| 2229 | } |
| 2230 | smi_reset = true; |
| 2231 | } |
| 2232 | |
| 2233 | if (pmu_have_event("msr", "aperf") && |
| 2234 | pmu_have_event("msr", "smi")) { |
| 2235 | if (!force_metric_only) |
| 2236 | metric_only = true; |
| 2237 | err = parse_events(evsel_list, smi_cost_attrs, NULL); |
| 2238 | } else { |
| 2239 | fprintf(stderr, "To measure SMI cost, it needs " |
| 2240 | "msr/aperf/, msr/smi/ and cpu/cycles/ support\n"); |
| 2241 | return -1; |
| 2242 | } |
| 2243 | if (err) { |
| 2244 | fprintf(stderr, "Cannot set up SMI cost events\n"); |
| 2245 | return -1; |
| 2246 | } |
| 2247 | return 0; |
| 2248 | } |
| 2249 | |
Andi Kleen | 44b1e60 | 2016-05-30 12:49:42 -0300 | [diff] [blame] | 2250 | if (topdown_run) { |
| 2251 | char *str = NULL; |
| 2252 | bool warn = false; |
| 2253 | |
| 2254 | if (stat_config.aggr_mode != AGGR_GLOBAL && |
| 2255 | stat_config.aggr_mode != AGGR_CORE) { |
| 2256 | pr_err("top down event configuration requires --per-core mode\n"); |
| 2257 | return -1; |
| 2258 | } |
| 2259 | stat_config.aggr_mode = AGGR_CORE; |
| 2260 | if (nr_cgroups || !target__has_cpu(&target)) { |
| 2261 | pr_err("top down event configuration requires system-wide mode (-a)\n"); |
| 2262 | return -1; |
| 2263 | } |
| 2264 | |
| 2265 | if (!force_metric_only) |
| 2266 | metric_only = true; |
| 2267 | if (topdown_filter_events(topdown_attrs, &str, |
| 2268 | arch_topdown_check_group(&warn)) < 0) { |
| 2269 | pr_err("Out of memory\n"); |
| 2270 | return -1; |
| 2271 | } |
| 2272 | if (topdown_attrs[0] && str) { |
| 2273 | if (warn) |
| 2274 | arch_topdown_group_warn(); |
| 2275 | err = parse_events(evsel_list, str, NULL); |
| 2276 | if (err) { |
| 2277 | fprintf(stderr, |
| 2278 | "Cannot set up top down events %s: %d\n", |
| 2279 | str, err); |
| 2280 | free(str); |
| 2281 | return -1; |
| 2282 | } |
| 2283 | } else { |
| 2284 | fprintf(stderr, "System does not support topdown\n"); |
| 2285 | return -1; |
| 2286 | } |
| 2287 | free(str); |
| 2288 | } |
| 2289 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 2290 | if (!evsel_list->nr_entries) { |
Namhyung Kim | a1f3d56 | 2016-05-13 15:01:03 +0900 | [diff] [blame] | 2291 | if (target__has_cpu(&target)) |
| 2292 | default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK; |
| 2293 | |
Andi Kleen | 9dec447 | 2016-02-26 16:27:56 -0800 | [diff] [blame] | 2294 | if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0) |
| 2295 | return -1; |
| 2296 | if (pmu_have_event("cpu", "stalled-cycles-frontend")) { |
| 2297 | if (perf_evlist__add_default_attrs(evsel_list, |
| 2298 | frontend_attrs) < 0) |
| 2299 | return -1; |
| 2300 | } |
| 2301 | if (pmu_have_event("cpu", "stalled-cycles-backend")) { |
| 2302 | if (perf_evlist__add_default_attrs(evsel_list, |
| 2303 | backend_attrs) < 0) |
| 2304 | return -1; |
| 2305 | } |
| 2306 | if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 2307 | return -1; |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | /* Detailed events get appended to the event list: */ |
| 2311 | |
| 2312 | if (detailed_run < 1) |
| 2313 | return 0; |
| 2314 | |
| 2315 | /* Append detailed run extra attributes: */ |
Arnaldo Carvalho de Melo | 79695e1 | 2012-05-30 13:53:54 -0300 | [diff] [blame] | 2316 | if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 2317 | return -1; |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 2318 | |
| 2319 | if (detailed_run < 2) |
| 2320 | return 0; |
| 2321 | |
| 2322 | /* Append very detailed run extra attributes: */ |
Arnaldo Carvalho de Melo | 79695e1 | 2012-05-30 13:53:54 -0300 | [diff] [blame] | 2323 | if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0) |
Arnaldo Carvalho de Melo | 50d08e4 | 2011-11-04 09:10:59 -0200 | [diff] [blame] | 2324 | return -1; |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 2325 | |
| 2326 | if (detailed_run < 3) |
| 2327 | return 0; |
| 2328 | |
| 2329 | /* Append very, very detailed run extra attributes: */ |
Arnaldo Carvalho de Melo | 79695e1 | 2012-05-30 13:53:54 -0300 | [diff] [blame] | 2330 | return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs); |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 2331 | } |
| 2332 | |
Jiri Olsa | 8a59f3c | 2016-01-12 10:35:29 +0100 | [diff] [blame] | 2333 | static const char * const stat_record_usage[] = { |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2334 | "perf stat record [<options>]", |
| 2335 | NULL, |
| 2336 | }; |
| 2337 | |
Jiri Olsa | 3ba78bd | 2015-11-05 15:40:47 +0100 | [diff] [blame] | 2338 | static void init_features(struct perf_session *session) |
| 2339 | { |
| 2340 | int feat; |
| 2341 | |
| 2342 | for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) |
| 2343 | perf_header__set_feat(&session->header, feat); |
| 2344 | |
| 2345 | perf_header__clear_feat(&session->header, HEADER_BUILD_ID); |
| 2346 | perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); |
| 2347 | perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); |
| 2348 | perf_header__clear_feat(&session->header, HEADER_AUXTRACE); |
| 2349 | } |
| 2350 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2351 | static int __cmd_record(int argc, const char **argv) |
| 2352 | { |
| 2353 | struct perf_session *session; |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2354 | struct perf_data *data = &perf_stat.data; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2355 | |
Jiri Olsa | 8a59f3c | 2016-01-12 10:35:29 +0100 | [diff] [blame] | 2356 | argc = parse_options(argc, argv, stat_options, stat_record_usage, |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2357 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 2358 | |
| 2359 | if (output_name) |
Jiri Olsa | eae8ad8 | 2017-01-23 22:25:41 +0100 | [diff] [blame] | 2360 | data->file.path = output_name; |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2361 | |
Jiri Olsa | e9d6db8e8 | 2015-11-05 15:40:53 +0100 | [diff] [blame] | 2362 | if (run_count != 1 || forever) { |
| 2363 | pr_err("Cannot use -r option with perf stat record.\n"); |
| 2364 | return -1; |
| 2365 | } |
| 2366 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2367 | session = perf_session__new(data, false, NULL); |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2368 | if (session == NULL) { |
| 2369 | pr_err("Perf session creation failed.\n"); |
| 2370 | return -1; |
| 2371 | } |
| 2372 | |
Jiri Olsa | 3ba78bd | 2015-11-05 15:40:47 +0100 | [diff] [blame] | 2373 | init_features(session); |
| 2374 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2375 | session->evlist = evsel_list; |
| 2376 | perf_stat.session = session; |
| 2377 | perf_stat.record = true; |
| 2378 | return argc; |
| 2379 | } |
| 2380 | |
Jiri Olsa | a56f939 | 2015-11-05 15:40:59 +0100 | [diff] [blame] | 2381 | static int process_stat_round_event(struct perf_tool *tool __maybe_unused, |
| 2382 | union perf_event *event, |
| 2383 | struct perf_session *session) |
| 2384 | { |
Andi Kleen | e3b03b6 | 2016-05-05 16:04:03 -0700 | [diff] [blame] | 2385 | struct stat_round_event *stat_round = &event->stat_round; |
Jiri Olsa | a56f939 | 2015-11-05 15:40:59 +0100 | [diff] [blame] | 2386 | struct perf_evsel *counter; |
| 2387 | struct timespec tsh, *ts = NULL; |
| 2388 | const char **argv = session->header.env.cmdline_argv; |
| 2389 | int argc = session->header.env.nr_cmdline; |
| 2390 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 2391 | evlist__for_each_entry(evsel_list, counter) |
Jiri Olsa | a56f939 | 2015-11-05 15:40:59 +0100 | [diff] [blame] | 2392 | perf_stat_process_counter(&stat_config, counter); |
| 2393 | |
Andi Kleen | e3b03b6 | 2016-05-05 16:04:03 -0700 | [diff] [blame] | 2394 | if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL) |
| 2395 | update_stats(&walltime_nsecs_stats, stat_round->time); |
Jiri Olsa | a56f939 | 2015-11-05 15:40:59 +0100 | [diff] [blame] | 2396 | |
Andi Kleen | e3b03b6 | 2016-05-05 16:04:03 -0700 | [diff] [blame] | 2397 | if (stat_config.interval && stat_round->time) { |
Arnaldo Carvalho de Melo | bd48c63 | 2016-08-05 15:40:30 -0300 | [diff] [blame] | 2398 | tsh.tv_sec = stat_round->time / NSEC_PER_SEC; |
| 2399 | tsh.tv_nsec = stat_round->time % NSEC_PER_SEC; |
Jiri Olsa | a56f939 | 2015-11-05 15:40:59 +0100 | [diff] [blame] | 2400 | ts = &tsh; |
| 2401 | } |
| 2402 | |
| 2403 | print_counters(ts, argc, argv); |
| 2404 | return 0; |
| 2405 | } |
| 2406 | |
Jiri Olsa | 62ba18b | 2015-11-05 15:40:57 +0100 | [diff] [blame] | 2407 | static |
Arnaldo Carvalho de Melo | 7e6a799 | 2016-12-12 10:52:10 -0300 | [diff] [blame] | 2408 | int process_stat_config_event(struct perf_tool *tool, |
Jiri Olsa | 62ba18b | 2015-11-05 15:40:57 +0100 | [diff] [blame] | 2409 | union perf_event *event, |
| 2410 | struct perf_session *session __maybe_unused) |
| 2411 | { |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 2412 | struct perf_stat *st = container_of(tool, struct perf_stat, tool); |
| 2413 | |
Jiri Olsa | 62ba18b | 2015-11-05 15:40:57 +0100 | [diff] [blame] | 2414 | perf_event__read_stat_config(&stat_config, &event->stat_config); |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 2415 | |
Jiri Olsa | 89af4e0 | 2015-11-05 15:41:02 +0100 | [diff] [blame] | 2416 | if (cpu_map__empty(st->cpus)) { |
| 2417 | if (st->aggr_mode != AGGR_UNSET) |
| 2418 | pr_warning("warning: processing task data, aggregation mode not set\n"); |
| 2419 | return 0; |
| 2420 | } |
| 2421 | |
| 2422 | if (st->aggr_mode != AGGR_UNSET) |
| 2423 | stat_config.aggr_mode = st->aggr_mode; |
| 2424 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2425 | if (perf_stat.data.is_pipe) |
Jiri Olsa | 68d702f | 2015-11-05 15:40:58 +0100 | [diff] [blame] | 2426 | perf_stat_init_aggr_mode(); |
| 2427 | else |
| 2428 | perf_stat_init_aggr_mode_file(st); |
| 2429 | |
Jiri Olsa | 62ba18b | 2015-11-05 15:40:57 +0100 | [diff] [blame] | 2430 | return 0; |
| 2431 | } |
| 2432 | |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 2433 | static int set_maps(struct perf_stat *st) |
| 2434 | { |
| 2435 | if (!st->cpus || !st->threads) |
| 2436 | return 0; |
| 2437 | |
| 2438 | if (WARN_ONCE(st->maps_allocated, "stats double allocation\n")) |
| 2439 | return -EINVAL; |
| 2440 | |
| 2441 | perf_evlist__set_maps(evsel_list, st->cpus, st->threads); |
| 2442 | |
| 2443 | if (perf_evlist__alloc_stats(evsel_list, true)) |
| 2444 | return -ENOMEM; |
| 2445 | |
| 2446 | st->maps_allocated = true; |
| 2447 | return 0; |
| 2448 | } |
| 2449 | |
| 2450 | static |
Arnaldo Carvalho de Melo | 7e6a799 | 2016-12-12 10:52:10 -0300 | [diff] [blame] | 2451 | int process_thread_map_event(struct perf_tool *tool, |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 2452 | union perf_event *event, |
| 2453 | struct perf_session *session __maybe_unused) |
| 2454 | { |
| 2455 | struct perf_stat *st = container_of(tool, struct perf_stat, tool); |
| 2456 | |
| 2457 | if (st->threads) { |
| 2458 | pr_warning("Extra thread map event, ignoring.\n"); |
| 2459 | return 0; |
| 2460 | } |
| 2461 | |
| 2462 | st->threads = thread_map__new_event(&event->thread_map); |
| 2463 | if (!st->threads) |
| 2464 | return -ENOMEM; |
| 2465 | |
| 2466 | return set_maps(st); |
| 2467 | } |
| 2468 | |
| 2469 | static |
Arnaldo Carvalho de Melo | 7e6a799 | 2016-12-12 10:52:10 -0300 | [diff] [blame] | 2470 | int process_cpu_map_event(struct perf_tool *tool, |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 2471 | union perf_event *event, |
| 2472 | struct perf_session *session __maybe_unused) |
| 2473 | { |
| 2474 | struct perf_stat *st = container_of(tool, struct perf_stat, tool); |
| 2475 | struct cpu_map *cpus; |
| 2476 | |
| 2477 | if (st->cpus) { |
| 2478 | pr_warning("Extra cpu map event, ignoring.\n"); |
| 2479 | return 0; |
| 2480 | } |
| 2481 | |
| 2482 | cpus = cpu_map__new_data(&event->cpu_map.data); |
| 2483 | if (!cpus) |
| 2484 | return -ENOMEM; |
| 2485 | |
| 2486 | st->cpus = cpus; |
| 2487 | return set_maps(st); |
| 2488 | } |
| 2489 | |
Jiri Olsa | 8a59f3c | 2016-01-12 10:35:29 +0100 | [diff] [blame] | 2490 | static const char * const stat_report_usage[] = { |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2491 | "perf stat report [<options>]", |
| 2492 | NULL, |
| 2493 | }; |
| 2494 | |
| 2495 | static struct perf_stat perf_stat = { |
| 2496 | .tool = { |
| 2497 | .attr = perf_event__process_attr, |
Jiri Olsa | fa6ea78 | 2015-11-05 15:41:00 +0100 | [diff] [blame] | 2498 | .event_update = perf_event__process_event_update, |
Jiri Olsa | 1975d36 | 2015-11-05 15:40:56 +0100 | [diff] [blame] | 2499 | .thread_map = process_thread_map_event, |
| 2500 | .cpu_map = process_cpu_map_event, |
Jiri Olsa | 62ba18b | 2015-11-05 15:40:57 +0100 | [diff] [blame] | 2501 | .stat_config = process_stat_config_event, |
Jiri Olsa | a56f939 | 2015-11-05 15:40:59 +0100 | [diff] [blame] | 2502 | .stat = perf_event__process_stat_event, |
| 2503 | .stat_round = process_stat_round_event, |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2504 | }, |
Jiri Olsa | 89af4e0 | 2015-11-05 15:41:02 +0100 | [diff] [blame] | 2505 | .aggr_mode = AGGR_UNSET, |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2506 | }; |
| 2507 | |
| 2508 | static int __cmd_report(int argc, const char **argv) |
| 2509 | { |
| 2510 | struct perf_session *session; |
| 2511 | const struct option options[] = { |
| 2512 | OPT_STRING('i', "input", &input_name, "file", "input file name"), |
Jiri Olsa | 89af4e0 | 2015-11-05 15:41:02 +0100 | [diff] [blame] | 2513 | OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode, |
| 2514 | "aggregate counts per processor socket", AGGR_SOCKET), |
| 2515 | OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode, |
| 2516 | "aggregate counts per physical processor core", AGGR_CORE), |
| 2517 | OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode, |
| 2518 | "disable CPU count aggregation", AGGR_NONE), |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2519 | OPT_END() |
| 2520 | }; |
| 2521 | struct stat st; |
| 2522 | int ret; |
| 2523 | |
Jiri Olsa | 8a59f3c | 2016-01-12 10:35:29 +0100 | [diff] [blame] | 2524 | argc = parse_options(argc, argv, options, stat_report_usage, 0); |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2525 | |
| 2526 | if (!input_name || !strlen(input_name)) { |
| 2527 | if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode)) |
| 2528 | input_name = "-"; |
| 2529 | else |
| 2530 | input_name = "perf.data"; |
| 2531 | } |
| 2532 | |
Jiri Olsa | eae8ad8 | 2017-01-23 22:25:41 +0100 | [diff] [blame] | 2533 | perf_stat.data.file.path = input_name; |
| 2534 | perf_stat.data.mode = PERF_DATA_MODE_READ; |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2535 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2536 | session = perf_session__new(&perf_stat.data, false, &perf_stat.tool); |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2537 | if (session == NULL) |
| 2538 | return -1; |
| 2539 | |
| 2540 | perf_stat.session = session; |
| 2541 | stat_config.output = stderr; |
| 2542 | evsel_list = session->evlist; |
| 2543 | |
| 2544 | ret = perf_session__process_events(session); |
| 2545 | if (ret) |
| 2546 | return ret; |
| 2547 | |
| 2548 | perf_session__delete(session); |
| 2549 | return 0; |
| 2550 | } |
| 2551 | |
Jiri Olsa | e3ba76d | 2017-02-27 10:48:18 +0100 | [diff] [blame] | 2552 | static void setup_system_wide(int forks) |
| 2553 | { |
| 2554 | /* |
| 2555 | * Make system wide (-a) the default target if |
| 2556 | * no target was specified and one of following |
| 2557 | * conditions is met: |
| 2558 | * |
| 2559 | * - there's no workload specified |
| 2560 | * - there is workload specified but all requested |
| 2561 | * events are system wide events |
| 2562 | */ |
| 2563 | if (!target__none(&target)) |
| 2564 | return; |
| 2565 | |
| 2566 | if (!forks) |
| 2567 | target.system_wide = true; |
| 2568 | else { |
| 2569 | struct perf_evsel *counter; |
| 2570 | |
| 2571 | evlist__for_each_entry(evsel_list, counter) { |
| 2572 | if (!counter->system_wide) |
| 2573 | return; |
| 2574 | } |
| 2575 | |
| 2576 | if (evsel_list->nr_entries) |
| 2577 | target.system_wide = true; |
| 2578 | } |
| 2579 | } |
| 2580 | |
Arnaldo Carvalho de Melo | b0ad8ea | 2017-03-27 11:47:20 -0300 | [diff] [blame] | 2581 | int cmd_stat(int argc, const char **argv) |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 2582 | { |
Arnaldo Carvalho de Melo | b070a547 | 2012-10-01 15:20:58 -0300 | [diff] [blame] | 2583 | const char * const stat_usage[] = { |
| 2584 | "perf stat [<options>] [<command>]", |
| 2585 | NULL |
| 2586 | }; |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2587 | int status = -EINVAL, run_idx; |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 2588 | const char *mode; |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 2589 | FILE *output = stderr; |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 2590 | unsigned int interval; |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2591 | const char * const stat_subcommands[] = { "record", "report" }; |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 2592 | |
Stephane Eranian | 5af52b5 | 2010-05-18 15:00:01 +0200 | [diff] [blame] | 2593 | setlocale(LC_ALL, ""); |
| 2594 | |
Namhyung Kim | 334fe7a | 2013-03-11 16:43:12 +0900 | [diff] [blame] | 2595 | evsel_list = perf_evlist__new(); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 2596 | if (evsel_list == NULL) |
| 2597 | return -ENOMEM; |
| 2598 | |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 2599 | parse_events__shrink_config_terms(); |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2600 | argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands, |
| 2601 | (const char **) stat_usage, |
| 2602 | PARSE_OPT_STOP_AT_NON_OPTION); |
Andi Kleen | 37932c1 | 2017-03-20 13:17:08 -0700 | [diff] [blame] | 2603 | perf_stat__collect_metric_expr(evsel_list); |
Andi Kleen | fb4605b | 2016-03-01 10:57:52 -0800 | [diff] [blame] | 2604 | perf_stat__init_shadow_stats(); |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2605 | |
Jiri Olsa | 6edb78a | 2015-11-05 15:41:01 +0100 | [diff] [blame] | 2606 | if (csv_sep) { |
| 2607 | csv_output = true; |
| 2608 | if (!strcmp(csv_sep, "\\t")) |
| 2609 | csv_sep = "\t"; |
| 2610 | } else |
| 2611 | csv_sep = DEFAULT_SEPARATOR; |
| 2612 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2613 | if (argc && !strncmp(argv[0], "rec", 3)) { |
| 2614 | argc = __cmd_record(argc, argv); |
| 2615 | if (argc < 0) |
| 2616 | return -1; |
Jiri Olsa | ba6039b6 | 2015-11-05 15:40:55 +0100 | [diff] [blame] | 2617 | } else if (argc && !strncmp(argv[0], "rep", 3)) |
| 2618 | return __cmd_report(argc, argv); |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 2619 | |
Jiri Olsa | ec0d3d1 | 2015-07-21 14:31:25 +0200 | [diff] [blame] | 2620 | interval = stat_config.interval; |
| 2621 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2622 | /* |
| 2623 | * For record command the -o is already taken care of. |
| 2624 | */ |
| 2625 | if (!STAT_RECORD && output_name && strcmp(output_name, "-")) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 2626 | output = NULL; |
| 2627 | |
Jim Cromie | 56f3bae | 2011-09-07 17:14:00 -0600 | [diff] [blame] | 2628 | if (output_name && output_fd) { |
| 2629 | fprintf(stderr, "cannot use both --output and --log-fd\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2630 | parse_options_usage(stat_usage, stat_options, "o", 1); |
| 2631 | parse_options_usage(NULL, stat_options, "log-fd", 0); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2632 | goto out; |
Jim Cromie | 56f3bae | 2011-09-07 17:14:00 -0600 | [diff] [blame] | 2633 | } |
Stephane Eranian | fc3e4d0 | 2012-05-15 13:11:11 +0200 | [diff] [blame] | 2634 | |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 2635 | if (metric_only && stat_config.aggr_mode == AGGR_THREAD) { |
| 2636 | fprintf(stderr, "--metric-only is not supported with --per-thread\n"); |
| 2637 | goto out; |
| 2638 | } |
| 2639 | |
Andi Kleen | 54b5091 | 2016-03-03 15:57:36 -0800 | [diff] [blame] | 2640 | if (metric_only && run_count > 1) { |
| 2641 | fprintf(stderr, "--metric-only is not supported with -r\n"); |
| 2642 | goto out; |
| 2643 | } |
| 2644 | |
Stephane Eranian | fc3e4d0 | 2012-05-15 13:11:11 +0200 | [diff] [blame] | 2645 | if (output_fd < 0) { |
| 2646 | fprintf(stderr, "argument to --log-fd must be a > 0\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2647 | parse_options_usage(stat_usage, stat_options, "log-fd", 0); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2648 | goto out; |
Stephane Eranian | fc3e4d0 | 2012-05-15 13:11:11 +0200 | [diff] [blame] | 2649 | } |
| 2650 | |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 2651 | if (!output) { |
| 2652 | struct timespec tm; |
| 2653 | mode = append_file ? "a" : "w"; |
| 2654 | |
| 2655 | output = fopen(output_name, mode); |
| 2656 | if (!output) { |
| 2657 | perror("failed to create output file"); |
David Ahern | fceda7f | 2012-08-26 12:24:44 -0600 | [diff] [blame] | 2658 | return -1; |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 2659 | } |
| 2660 | clock_gettime(CLOCK_REALTIME, &tm); |
| 2661 | fprintf(output, "# started on %s\n", ctime(&tm.tv_sec)); |
Stephane Eranian | fc3e4d0 | 2012-05-15 13:11:11 +0200 | [diff] [blame] | 2662 | } else if (output_fd > 0) { |
Jim Cromie | 56f3bae | 2011-09-07 17:14:00 -0600 | [diff] [blame] | 2663 | mode = append_file ? "a" : "w"; |
| 2664 | output = fdopen(output_fd, mode); |
| 2665 | if (!output) { |
| 2666 | perror("Failed opening logfd"); |
| 2667 | return -errno; |
| 2668 | } |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 2669 | } |
| 2670 | |
Jiri Olsa | 5821522 | 2015-07-21 14:31:24 +0200 | [diff] [blame] | 2671 | stat_config.output = output; |
| 2672 | |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 2673 | /* |
| 2674 | * let the spreadsheet do the pretty-printing |
| 2675 | */ |
| 2676 | if (csv_output) { |
Jim Cromie | 61a9f32 | 2011-09-07 17:14:04 -0600 | [diff] [blame] | 2677 | /* User explicitly passed -B? */ |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 2678 | if (big_num_opt == 1) { |
| 2679 | fprintf(stderr, "-B option not supported with -x\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2680 | parse_options_usage(stat_usage, stat_options, "B", 1); |
| 2681 | parse_options_usage(NULL, stat_options, "x", 1); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2682 | goto out; |
Stephane Eranian | d7470b6 | 2010-12-01 18:49:05 +0200 | [diff] [blame] | 2683 | } else /* Nope, so disable big number formatting */ |
| 2684 | big_num = false; |
| 2685 | } else if (big_num_opt == 0) /* User passed --no-big-num */ |
| 2686 | big_num = false; |
| 2687 | |
Jiri Olsa | e3ba76d | 2017-02-27 10:48:18 +0100 | [diff] [blame] | 2688 | setup_system_wide(argc); |
David Ahern | ac3063b | 2013-09-30 07:37:37 -0600 | [diff] [blame] | 2689 | |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 2690 | if (run_count < 0) { |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2691 | pr_err("Run count must be a positive number\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2692 | parse_options_usage(stat_usage, stat_options, "r", 1); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2693 | goto out; |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 2694 | } else if (run_count == 0) { |
| 2695 | forever = true; |
| 2696 | run_count = 1; |
| 2697 | } |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 2698 | |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 2699 | if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) { |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 2700 | fprintf(stderr, "The --per-thread option is only available " |
| 2701 | "when monitoring via -p -t options.\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2702 | parse_options_usage(NULL, stat_options, "p", 1); |
| 2703 | parse_options_usage(NULL, stat_options, "t", 1); |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 2704 | goto out; |
| 2705 | } |
| 2706 | |
| 2707 | /* |
| 2708 | * no_aggr, cgroup are for system-wide only |
| 2709 | * --per-thread is aggregated per thread, we dont mix it with cpu mode |
| 2710 | */ |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 2711 | if (((stat_config.aggr_mode != AGGR_GLOBAL && |
| 2712 | stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) && |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 2713 | !target__has_cpu(&target)) { |
Stephane Eranian | 023695d | 2011-02-14 11:20:01 +0200 | [diff] [blame] | 2714 | fprintf(stderr, "both cgroup and no-aggregation " |
| 2715 | "modes only available in system-wide mode\n"); |
| 2716 | |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2717 | parse_options_usage(stat_usage, stat_options, "G", 1); |
| 2718 | parse_options_usage(NULL, stat_options, "A", 1); |
| 2719 | parse_options_usage(NULL, stat_options, "a", 1); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2720 | goto out; |
Stephane Eranian | d7e7a45 | 2013-02-06 15:46:02 +0100 | [diff] [blame] | 2721 | } |
| 2722 | |
Ingo Molnar | 2cba3ff | 2011-05-19 13:30:56 +0200 | [diff] [blame] | 2723 | if (add_default_attributes()) |
| 2724 | goto out; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 2725 | |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 2726 | target__validate(&target); |
Arnaldo Carvalho de Melo | 5c98d466 | 2011-01-03 17:53:33 -0200 | [diff] [blame] | 2727 | |
Namhyung Kim | 77a6f01 | 2012-05-07 14:09:04 +0900 | [diff] [blame] | 2728 | if (perf_evlist__create_maps(evsel_list, &target) < 0) { |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 2729 | if (target__has_task(&target)) { |
Namhyung Kim | 77a6f01 | 2012-05-07 14:09:04 +0900 | [diff] [blame] | 2730 | pr_err("Problems finding threads of monitor\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2731 | parse_options_usage(stat_usage, stat_options, "p", 1); |
| 2732 | parse_options_usage(NULL, stat_options, "t", 1); |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 2733 | } else if (target__has_cpu(&target)) { |
Namhyung Kim | 77a6f01 | 2012-05-07 14:09:04 +0900 | [diff] [blame] | 2734 | perror("failed to parse CPUs map"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2735 | parse_options_usage(stat_usage, stat_options, "C", 1); |
| 2736 | parse_options_usage(NULL, stat_options, "a", 1); |
Namhyung Kim | cc03c54 | 2013-11-01 16:33:15 +0900 | [diff] [blame] | 2737 | } |
| 2738 | goto out; |
Arnaldo Carvalho de Melo | 60d567e | 2011-01-03 17:49:48 -0200 | [diff] [blame] | 2739 | } |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 2740 | |
| 2741 | /* |
| 2742 | * Initialize thread_map with comm names, |
| 2743 | * so we could print it out on output. |
| 2744 | */ |
Jiri Olsa | 421a50f | 2015-07-21 14:31:22 +0200 | [diff] [blame] | 2745 | if (stat_config.aggr_mode == AGGR_THREAD) |
Jiri Olsa | 32b8af8 | 2015-06-26 11:29:27 +0200 | [diff] [blame] | 2746 | thread_map__read_comms(evsel_list->threads); |
| 2747 | |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 2748 | if (interval && interval < 100) { |
Kan Liang | 19afd10 | 2015-10-02 05:04:34 -0400 | [diff] [blame] | 2749 | if (interval < 10) { |
| 2750 | pr_err("print interval must be >= 10ms\n"); |
Jiri Olsa | e054731 | 2015-11-05 15:40:45 +0100 | [diff] [blame] | 2751 | parse_options_usage(stat_usage, stat_options, "I", 1); |
Kan Liang | 19afd10 | 2015-10-02 05:04:34 -0400 | [diff] [blame] | 2752 | goto out; |
| 2753 | } else |
| 2754 | pr_warning("print interval < 100ms. " |
| 2755 | "The overhead percentage could be high in some cases. " |
| 2756 | "Please proceed with caution.\n"); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 2757 | } |
Stephane Eranian | c45c6ea | 2010-05-28 12:00:01 +0200 | [diff] [blame] | 2758 | |
Arnaldo Carvalho de Melo | d134ffb | 2013-03-18 11:24:21 -0300 | [diff] [blame] | 2759 | if (perf_evlist__alloc_stats(evsel_list, interval)) |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 2760 | goto out; |
Zhang, Yanmin | d6d901c | 2010-03-18 11:36:05 -0300 | [diff] [blame] | 2761 | |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 2762 | if (perf_stat_init_aggr_mode()) |
Arnaldo Carvalho de Melo | 03ad974 | 2014-01-03 15:56:06 -0300 | [diff] [blame] | 2763 | goto out; |
Stephane Eranian | 86ee6e1 | 2013-02-14 13:57:27 +0100 | [diff] [blame] | 2764 | |
Ingo Molnar | 58d7e99 | 2009-05-15 11:03:23 +0200 | [diff] [blame] | 2765 | /* |
| 2766 | * We dont want to block the signals - that would cause |
| 2767 | * child tasks to inherit that and Ctrl-C would not work. |
| 2768 | * What we want is for Ctrl-C to work in the exec()-ed |
| 2769 | * task, but being ignored by perf stat itself: |
| 2770 | */ |
Peter Zijlstra | f7b7c26 | 2009-06-10 15:55:59 +0200 | [diff] [blame] | 2771 | atexit(sig_atexit); |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 2772 | if (!forever) |
| 2773 | signal(SIGINT, skip_signal); |
Stephane Eranian | 13370a9 | 2013-01-29 12:47:44 +0100 | [diff] [blame] | 2774 | signal(SIGCHLD, skip_signal); |
Ingo Molnar | 58d7e99 | 2009-05-15 11:03:23 +0200 | [diff] [blame] | 2775 | signal(SIGALRM, skip_signal); |
| 2776 | signal(SIGABRT, skip_signal); |
| 2777 | |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 2778 | status = 0; |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 2779 | for (run_idx = 0; forever || run_idx < run_count; run_idx++) { |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 2780 | if (run_count != 1 && verbose > 0) |
Stephane Eranian | 4aa9015 | 2011-08-15 22:22:33 +0200 | [diff] [blame] | 2781 | fprintf(output, "[ perf stat: executing run #%d ... ]\n", |
| 2782 | run_idx + 1); |
Ingo Molnar | f9cef0a | 2011-04-28 18:17:11 +0200 | [diff] [blame] | 2783 | |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 2784 | status = run_perf_stat(argc, argv); |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 2785 | if (forever && status != -1) { |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 2786 | print_counters(NULL, argc, argv); |
Jiri Olsa | 254ecbc7 | 2015-06-26 11:29:13 +0200 | [diff] [blame] | 2787 | perf_stat__reset_stats(); |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 2788 | } |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 2789 | } |
| 2790 | |
Frederik Deweerdt | a7e191c | 2013-03-01 13:02:27 -0500 | [diff] [blame] | 2791 | if (!forever && status != -1 && !interval) |
Jiri Olsa | d4f63a4 | 2015-06-26 11:29:26 +0200 | [diff] [blame] | 2792 | print_counters(NULL, argc, argv); |
Arnaldo Carvalho de Melo | d134ffb | 2013-03-18 11:24:21 -0300 | [diff] [blame] | 2793 | |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2794 | if (STAT_RECORD) { |
| 2795 | /* |
| 2796 | * We synthesize the kernel mmap record just so that older tools |
| 2797 | * don't emit warnings about not being able to resolve symbols |
| 2798 | * due to /proc/sys/kernel/kptr_restrict settings and instear provide |
| 2799 | * a saner message about no samples being in the perf.data file. |
| 2800 | * |
| 2801 | * This also serves to suppress a warning about f_header.data.size == 0 |
Jiri Olsa | 8b99b1a | 2015-11-05 15:40:48 +0100 | [diff] [blame] | 2802 | * in header.c at the moment 'perf stat record' gets introduced, which |
| 2803 | * is not really needed once we start adding the stat specific PERF_RECORD_ |
| 2804 | * records, but the need to suppress the kptr_restrict messages in older |
| 2805 | * tools remain -acme |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2806 | */ |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2807 | int fd = perf_data__fd(&perf_stat.data); |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2808 | int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat, |
| 2809 | process_synthesized_event, |
| 2810 | &perf_stat.session->machines.host); |
| 2811 | if (err) { |
| 2812 | pr_warning("Couldn't synthesize the kernel mmap record, harmless, " |
| 2813 | "older tools may produce warnings about this file\n."); |
| 2814 | } |
| 2815 | |
Jiri Olsa | 7aad0c3 | 2015-11-05 15:40:52 +0100 | [diff] [blame] | 2816 | if (!interval) { |
| 2817 | if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL)) |
| 2818 | pr_err("failed to write stat round event\n"); |
| 2819 | } |
| 2820 | |
Jiri Olsa | 8ceb41d | 2017-01-23 22:07:59 +0100 | [diff] [blame] | 2821 | if (!perf_stat.data.is_pipe) { |
Jiri Olsa | 664c98d | 2015-11-05 15:40:50 +0100 | [diff] [blame] | 2822 | perf_stat.session->header.data_size += perf_stat.bytes_written; |
| 2823 | perf_session__write_header(perf_stat.session, evsel_list, fd, true); |
| 2824 | } |
Jiri Olsa | 4979d0c | 2015-11-05 15:40:46 +0100 | [diff] [blame] | 2825 | |
| 2826 | perf_session__delete(perf_stat.session); |
| 2827 | } |
| 2828 | |
Masami Hiramatsu | 544c2ae | 2015-12-09 11:11:27 +0900 | [diff] [blame] | 2829 | perf_stat__exit_aggr_mode(); |
Arnaldo Carvalho de Melo | d134ffb | 2013-03-18 11:24:21 -0300 | [diff] [blame] | 2830 | perf_evlist__free_stats(evsel_list); |
Arnaldo Carvalho de Melo | 0015e2e | 2011-02-01 16:18:10 -0200 | [diff] [blame] | 2831 | out: |
Kan Liang | daefd0b | 2017-05-26 12:05:38 -0700 | [diff] [blame] | 2832 | if (smi_cost && smi_reset) |
| 2833 | sysfs__write_int(FREEZE_ON_SMI_PATH, 0); |
| 2834 | |
Arnaldo Carvalho de Melo | 0015e2e | 2011-02-01 16:18:10 -0200 | [diff] [blame] | 2835 | perf_evlist__delete(evsel_list); |
Ingo Molnar | 42202dd | 2009-06-13 14:57:28 +0200 | [diff] [blame] | 2836 | return status; |
Ingo Molnar | ddcacfa | 2009-04-20 15:37:32 +0200 | [diff] [blame] | 2837 | } |