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