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