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