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