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