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