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