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