blob: af2a3bf659f76e096ca53a09f922cc3e4cf77f5e [file] [log] [blame]
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * 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 Molnarddcacfa2009-04-20 15:37:32 +02008
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02009 $ perf stat ./hackbench 10
Ingo Molnarddcacfa2009-04-20 15:37:32 +020010
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020011 Time: 0.118
Ingo Molnarddcacfa2009-04-20 15:37:32 +020012
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020013 Performance counter stats for './hackbench 10':
Ingo Molnarddcacfa2009-04-20 15:37:32 +020014
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020015 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 Molnarddcacfa2009-04-20 15:37:32 +020028
Ingo Molnar52425192009-05-26 09:17:18 +020029 *
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020030 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
Ingo Molnar52425192009-05-26 09:17:18 +020031 *
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 Rajput6e750a8f2009-06-27 03:02:07 +053039 * Jaswinder Singh Rajput <jaswinder@kernel.org>
Ingo Molnar52425192009-05-26 09:17:18 +020040 *
41 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnarddcacfa2009-04-20 15:37:32 +020042 */
43
Peter Zijlstra1a482f32009-05-23 18:28:58 +020044#include "perf.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +020045#include "builtin.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030046#include "util/cgroup.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020047#include "util/util.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060048#include <subcmd/parse-options.h>
Ingo Molnar52425192009-05-26 09:17:18 +020049#include "util/parse-events.h"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070050#include "util/pmu.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020051#include "util/event.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020052#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020053#include "util/evsel.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020054#include "util/debug.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020055#include "util/color.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080056#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080057#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110058#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030059#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020060#include "util/thread_map.h"
Jiri Olsad8095602015-08-07 12:51:03 +020061#include "util/counts.h"
Jiri Olsa4979d0c2015-11-05 15:40:46 +010062#include "util/session.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020063
Peter Zijlstra1f16c572012-10-23 13:40:14 +020064#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020065#include <sys/prctl.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020066#include <locale.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020067
Stephane Eraniand7470b62010-12-01 18:49:05 +020068#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060069#define CNTR_NOT_SUPPORTED "<not supported>"
70#define CNTR_NOT_COUNTED "<not counted>"
Stephane Eraniand7470b62010-12-01 18:49:05 +020071
Jiri Olsad4f63a42015-06-26 11:29:26 +020072static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010073
Andi Kleen4cabc3d2013-08-21 16:47:26 -070074/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020075static const char *transaction_attrs = {
76 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070077 "{"
78 "instructions,"
79 "cycles,"
80 "cpu/cycles-t/,"
81 "cpu/tx-start/,"
82 "cpu/el-start/,"
83 "cpu/cycles-ct/"
84 "}"
85};
86
87/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +020088static const char * transaction_limited_attrs = {
89 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070090 "{"
91 "instructions,"
92 "cycles,"
93 "cpu/cycles-t/,"
94 "cpu/tx-start/"
95 "}"
96};
97
Robert Richter666e6d42012-04-05 18:26:27 +020098static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020099
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300100static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900101 .uid = UINT_MAX,
102};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530103
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100104typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
105
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530106static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200107static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200108static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000109static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200110static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700111static bool transaction_run;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200112static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200113static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200114static const char *csv_sep = NULL;
115static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800116static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200117static const char *pre_cmd = NULL;
118static const char *post_cmd = NULL;
119static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700120static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100121static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500122static bool forever = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100123static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100124static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100125static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100126static bool append_file;
127static const char *output_name;
128static int output_fd;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200129
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100130struct perf_stat {
131 bool record;
132 struct perf_data_file file;
133 struct perf_session *session;
134 u64 bytes_written;
135};
136
137static struct perf_stat perf_stat;
138#define STAT_RECORD perf_stat.record
139
Liming Wang60666c62009-12-31 16:05:50 +0800140static volatile int done = 0;
141
Jiri Olsa421a50f2015-07-21 14:31:22 +0200142static struct perf_stat_config stat_config = {
143 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200144 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200145};
146
Stephane Eranian13370a92013-01-29 12:47:44 +0100147static inline void diff_timespec(struct timespec *r, struct timespec *a,
148 struct timespec *b)
149{
150 r->tv_sec = a->tv_sec - b->tv_sec;
151 if (a->tv_nsec < b->tv_nsec) {
152 r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
153 r->tv_sec--;
154 } else {
155 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
156 }
157}
158
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200159static void perf_stat__reset_stats(void)
160{
161 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200162 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200163}
164
Jiri Olsacac21422012-11-12 18:34:00 +0100165static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200166{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200167 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200168
Jiri Olsa711a5722015-07-21 14:31:23 +0200169 if (stat_config.scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200170 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
171 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200172
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200173 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300174
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100175 /*
176 * Some events get initialized with sample_(period/type) set,
177 * like tracepoints. Clear it up for counting.
178 */
179 attr->sample_period = 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100180 /*
181 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
182 * while avoiding that older tools show confusing messages.
183 */
184 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100185
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100186 /*
187 * Disabling all counters initially, they will be enabled
188 * either manually by us or by kernel via enable_on_exec
189 * set later.
190 */
Jiri Olsac8280cec2015-12-03 10:06:45 +0100191 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100192 attr->disabled = 1;
193
Jiri Olsac8280cec2015-12-03 10:06:45 +0100194 /*
195 * In case of initial_delay we enable tracee
196 * events manually.
197 */
198 if (target__none(&target) && !initial_delay)
199 attr->enable_on_exec = 1;
200 }
201
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300202 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300203 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200204
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300205 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200206}
207
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200208/*
209 * Does the counter have nsecs as a unit?
210 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200211static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200212{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200213 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
214 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200215 return 1;
216
217 return 0;
218}
219
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100220static int perf_stat__write(struct perf_stat *stat, void *bf, size_t size)
221{
222 if (perf_data_file__write(stat->session->file, bf, size) < 0) {
223 pr_err("failed to write perf data, error: %m\n");
224 return -1;
225 }
226
227 stat->bytes_written += size;
228 return 0;
229}
230
231static int process_synthesized_event(struct perf_tool *tool,
232 union perf_event *event,
233 struct perf_sample *sample __maybe_unused,
234 struct machine *machine __maybe_unused)
235{
236 struct perf_stat *stat = (void *)tool;
237 return perf_stat__write(stat, event, event->header.size);
238}
239
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200240/*
241 * Read out the results of a single counter:
242 * do not aggregate counts across CPUs in system-wide mode
243 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200244static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200245{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100246 int nthreads = thread_map__nr(evsel_list->threads);
247 int ncpus = perf_evsel__nr_cpus(counter);
248 int cpu, thread;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200249
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000250 if (!counter->supported)
251 return -ENOENT;
252
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100253 if (counter->system_wide)
254 nthreads = 1;
255
256 for (thread = 0; thread < nthreads; thread++) {
257 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200258 struct perf_counts_values *count;
259
260 count = perf_counts(counter->counts, cpu, thread);
261 if (perf_evsel__read(counter, cpu, thread, count))
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100262 return -1;
263 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200264 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200265
266 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200267}
268
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200269static void read_counters(bool close_counters)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200270{
271 struct perf_evsel *counter;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200272
273 evlist__for_each(evsel_list, counter) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200274 if (read_counter(counter))
Andi Kleen245bad82015-09-01 15:52:46 -0700275 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200276
Jiri Olsaf80010e2015-07-21 14:31:27 +0200277 if (perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200278 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200279
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200280 if (close_counters) {
Jiri Olsa106a94a2015-06-26 11:29:19 +0200281 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
282 thread_map__nr(evsel_list->threads));
283 }
284 }
285}
286
Jiri Olsaba411a92015-06-26 11:29:24 +0200287static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100288{
Stephane Eranian13370a92013-01-29 12:47:44 +0100289 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100290
Jiri Olsa106a94a2015-06-26 11:29:19 +0200291 read_counters(false);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100292
Stephane Eranian13370a92013-01-29 12:47:44 +0100293 clock_gettime(CLOCK_MONOTONIC, &ts);
294 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100295
Jiri Olsad4f63a42015-06-26 11:29:26 +0200296 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100297}
298
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100299static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700300{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100301 if (initial_delay)
Andi Kleen41191682013-08-02 17:41:11 -0700302 usleep(initial_delay * 1000);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100303
304 /*
305 * We need to enable counters only if:
306 * - we don't have tracee (attaching to task or cpu)
307 * - we have initial delay configured
308 */
309 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100310 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700311}
312
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300313static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300314
315/*
316 * perf_evlist__prepare_workload will send a SIGUSR1
317 * if the fork fails, since we asked by setting its
318 * want_signal to true.
319 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300320static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
321 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300322{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300323 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300324}
325
Namhyung Kimacf28922013-03-11 16:43:18 +0900326static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200327{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200328 int interval = stat_config.interval;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300329 char msg[512];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200330 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100331 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100332 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100333 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200334 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300335 const bool forks = (argc > 0);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200336
Stephane Eranian13370a92013-01-29 12:47:44 +0100337 if (interval) {
338 ts.tv_sec = interval / 1000;
339 ts.tv_nsec = (interval % 1000) * 1000000;
340 } else {
341 ts.tv_sec = 1;
342 ts.tv_nsec = 0;
343 }
344
Liming Wang60666c62009-12-31 16:05:50 +0800345 if (forks) {
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300346 if (perf_evlist__prepare_workload(evsel_list, &target, argv, false,
347 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900348 perror("failed to prepare workload");
349 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800350 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900351 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000352 }
353
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200354 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300355 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200356
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300357 evlist__for_each(evsel_list, counter) {
Jiri Olsacac21422012-11-12 18:34:00 +0100358 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600359 /*
360 * PPC returns ENXIO for HW counters until 2.6.37
361 * (behavior changed with commit b0a873e).
362 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100363 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600364 errno == ENOENT || errno == EOPNOTSUPP ||
365 errno == ENXIO) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600366 if (verbose)
367 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300368 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600369 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400370
371 if ((counter->leader != counter) ||
372 !(counter->leader->nr_members > 1))
373 continue;
David Ahernc63ca0c2011-04-29 16:04:15 -0600374 }
Ingo Molnarede70292011-04-28 08:48:42 +0200375
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300376 perf_evsel__open_strerror(counter, &target,
377 errno, msg, sizeof(msg));
378 ui__error("%s\n", msg);
379
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200380 if (child_pid != -1)
381 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600382
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200383 return -1;
384 }
David Ahern2cee77c2011-05-30 08:55:59 -0600385 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100386
387 l = strlen(counter->unit);
388 if (l > unit_width)
389 unit_width = l;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300390 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200391
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300392 if (perf_evlist__apply_filters(evsel_list, &counter)) {
393 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
394 counter->filter, perf_evsel__name(counter), errno,
Masami Hiramatsu759e6122014-08-14 02:22:55 +0000395 strerror_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100396 return -1;
397 }
398
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100399 if (STAT_RECORD) {
400 int err, fd = perf_data_file__fd(&perf_stat.file);
401
402 err = perf_session__write_header(perf_stat.session, evsel_list,
403 fd, false);
404 if (err < 0)
405 return err;
406 }
407
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200408 /*
409 * Enable counters and exec the command:
410 */
411 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100412 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200413
Liming Wang60666c62009-12-31 16:05:50 +0800414 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900415 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100416 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900417
Stephane Eranian13370a92013-01-29 12:47:44 +0100418 if (interval) {
419 while (!waitpid(child_pid, &status, WNOHANG)) {
420 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200421 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100422 }
423 }
Liming Wang60666c62009-12-31 16:05:50 +0800424 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300425
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300426 if (workload_exec_errno) {
427 const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
428 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300429 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300430 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300431
Andi Kleen33e49ea2011-09-15 14:31:40 -0700432 if (WIFSIGNALED(status))
433 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800434 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100435 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100436 while (!done) {
437 nanosleep(&ts, NULL);
438 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200439 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100440 }
Liming Wang60666c62009-12-31 16:05:50 +0800441 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200442
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200443 t1 = rdclock();
444
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200445 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200446
Jiri Olsa106a94a2015-06-26 11:29:19 +0200447 read_counters(true);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200448
Ingo Molnar42202dd2009-06-13 14:57:28 +0200449 return WEXITSTATUS(status);
450}
451
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300452static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200453{
454 int ret;
455
456 if (pre_cmd) {
457 ret = system(pre_cmd);
458 if (ret)
459 return ret;
460 }
461
462 if (sync_run)
463 sync();
464
465 ret = __run_perf_stat(argc, argv);
466 if (ret)
467 return ret;
468
469 if (post_cmd) {
470 ret = system(post_cmd);
471 if (ret)
472 return ret;
473 }
474
475 return ret;
476}
477
Andi Kleend73515c2015-03-11 07:16:27 -0700478static void print_running(u64 run, u64 ena)
479{
480 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200481 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700482 csv_sep,
483 run,
484 csv_sep,
485 ena ? 100.0 * run / ena : 100.0);
486 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200487 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700488 }
489}
490
Ingo Molnarf99844c2011-04-27 05:35:39 +0200491static void print_noise_pct(double total, double avg)
492{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800493 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200494
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700495 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200496 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600497 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200498 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200499}
500
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200501static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200502{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200503 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200504
Peter Zijlstra849abde2009-09-04 18:23:38 +0200505 if (run_count == 1)
506 return;
507
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200508 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200509 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200510}
511
Stephane Eranian12c08a92013-02-14 13:57:29 +0100512static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200513{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200514 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100515 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200516 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100517 cpu_map__id_to_socket(id),
518 csv_output ? 0 : -8,
519 cpu_map__id_to_cpu(id),
520 csv_sep,
521 csv_output ? 0 : 4,
522 nr,
523 csv_sep);
524 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100525 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200526 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100527 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100528 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100529 csv_sep,
530 csv_output ? 0 : 4,
531 nr,
532 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100533 break;
534 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200535 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200536 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100537 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100538 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200539 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200540 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200541 csv_output ? 0 : 16,
542 thread_map__comm(evsel->threads, id),
543 csv_output ? 0 : -8,
544 thread_map__pid(evsel->threads, id),
545 csv_sep);
546 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100547 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200548 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100549 default:
550 break;
551 }
552}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200553
Andi Kleenda88c7f2014-09-24 13:50:46 -0700554static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100555{
Jiri Olsa58215222015-07-21 14:31:24 +0200556 FILE *output = stat_config.output;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100557 double msecs = avg / 1e6;
Stephane Eranian410136f2013-11-12 17:58:49 +0100558 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600559 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100560
Stephane Eranian410136f2013-11-12 17:58:49 +0100561 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
562 fmt_n = csv_output ? "%s" : "%-25s";
563
Andi Kleenda88c7f2014-09-24 13:50:46 -0700564 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100565
David Ahern4bbe5a62013-09-28 14:28:00 -0600566 scnprintf(name, sizeof(name), "%s%s",
567 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +0100568
569 fprintf(output, fmt_v, msecs, csv_sep);
570
571 if (csv_output)
572 fprintf(output, "%s%s", evsel->unit, csv_sep);
573 else
574 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
575
576 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200577
Stephane Eranian023695d2011-02-14 11:20:01 +0200578 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200579 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200580}
581
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200582static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
583{
Jiri Olsa58215222015-07-21 14:31:24 +0200584 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200585 double sc = evsel->scale;
586 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200587
588 if (csv_output) {
589 fmt = sc != 1.0 ? "%.2f%s" : "%.0f%s";
590 } else {
591 if (big_num)
592 fmt = sc != 1.0 ? "%'18.2f%s" : "%'18.0f%s";
593 else
594 fmt = sc != 1.0 ? "%18.2f%s" : "%18.0f%s";
595 }
596
597 aggr_printout(evsel, id, nr);
598
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200599 fprintf(output, fmt, avg, csv_sep);
600
601 if (evsel->unit)
602 fprintf(output, "%-*s%s",
603 csv_output ? 0 : unit_width,
604 evsel->unit, csv_sep);
605
606 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
607
608 if (evsel->cgrp)
609 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -0800610}
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200611
Andi Kleeneedfcb42015-11-02 17:50:21 -0800612static void printout(int id, int nr, struct perf_evsel *counter, double uval)
613{
614 int cpu = cpu_map__id_to_cpu(id);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200615
Andi Kleeneedfcb42015-11-02 17:50:21 -0800616 if (stat_config.aggr_mode == AGGR_GLOBAL)
617 cpu = 0;
618
619 if (nsec_counter(counter))
620 nsec_printout(id, nr, counter, uval);
621 else
622 abs_printout(id, nr, counter, uval);
623
624 if (!csv_output && !stat_config.interval)
625 perf_stat__print_shadow_stats(stat_config.output, counter,
626 uval, cpu,
627 stat_config.aggr_mode);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200628}
629
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100630static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100631{
Jiri Olsa58215222015-07-21 14:31:24 +0200632 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100633 struct perf_evsel *counter;
Kan Liang601083c2015-07-02 03:08:43 -0400634 int cpu, s, s2, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +0100635 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100636 u64 ena, run, val;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100637
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100638 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100639 return;
640
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100641 for (s = 0; s < aggr_map->nr; s++) {
642 id = aggr_map->map[s];
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300643 evlist__for_each(evsel_list, counter) {
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100644 val = ena = run = 0;
645 nr = 0;
646 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Kan Liang601083c2015-07-02 03:08:43 -0400647 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100648 if (s2 != id)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100649 continue;
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200650 val += perf_counts(counter->counts, cpu, 0)->val;
651 ena += perf_counts(counter->counts, cpu, 0)->ena;
652 run += perf_counts(counter->counts, cpu, 0)->run;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100653 nr++;
654 }
655 if (prefix)
656 fprintf(output, "%s", prefix);
657
658 if (run == 0 || ena == 0) {
Stephane Eranian582ec0822013-07-05 19:06:45 +0200659 aggr_printout(counter, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100660
Stephane Eranian410136f2013-11-12 17:58:49 +0100661 fprintf(output, "%*s%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100662 csv_output ? 0 : 18,
663 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100664 csv_sep);
665
666 fprintf(output, "%-*s%s",
667 csv_output ? 0 : unit_width,
668 counter->unit, csv_sep);
669
670 fprintf(output, "%*s",
671 csv_output ? 0 : -25,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100672 perf_evsel__name(counter));
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100673
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100674 if (counter->cgrp)
675 fprintf(output, "%s%s",
676 csv_sep, counter->cgrp->name);
677
Andi Kleend73515c2015-03-11 07:16:27 -0700678 print_running(run, ena);
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100679 fputc('\n', output);
680 continue;
681 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100682 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800683 printout(id, nr, counter, uval);
Andi Kleend73515c2015-03-11 07:16:27 -0700684 if (!csv_output)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100685 print_noise(counter, 1.0);
686
Andi Kleend73515c2015-03-11 07:16:27 -0700687 print_running(run, ena);
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100688 fputc('\n', output);
689 }
690 }
691}
692
Jiri Olsa32b8af82015-06-26 11:29:27 +0200693static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
694{
Jiri Olsa58215222015-07-21 14:31:24 +0200695 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200696 int nthreads = thread_map__nr(counter->threads);
697 int ncpus = cpu_map__nr(counter->cpus);
698 int cpu, thread;
699 double uval;
700
701 for (thread = 0; thread < nthreads; thread++) {
702 u64 ena = 0, run = 0, val = 0;
703
704 for (cpu = 0; cpu < ncpus; cpu++) {
705 val += perf_counts(counter->counts, cpu, thread)->val;
706 ena += perf_counts(counter->counts, cpu, thread)->ena;
707 run += perf_counts(counter->counts, cpu, thread)->run;
708 }
709
710 if (prefix)
711 fprintf(output, "%s", prefix);
712
713 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800714 printout(thread, 0, counter, uval);
Jiri Olsa32b8af82015-06-26 11:29:27 +0200715
716 if (!csv_output)
717 print_noise(counter, 1.0);
718
719 print_running(run, ena);
720 fputc('\n', output);
721 }
722}
723
Ingo Molnar42202dd2009-06-13 14:57:28 +0200724/*
725 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200726 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +0200727 */
Stephane Eranian13370a92013-01-29 12:47:44 +0100728static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200729{
Jiri Olsa58215222015-07-21 14:31:24 +0200730 FILE *output = stat_config.output;
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200731 struct perf_stat_evsel *ps = counter->priv;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200732 double avg = avg_stats(&ps->res_stats[0]);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200733 int scaled = counter->counts->scaled;
Stephane Eranian410136f2013-11-12 17:58:49 +0100734 double uval;
Andi Kleend73515c2015-03-11 07:16:27 -0700735 double avg_enabled, avg_running;
736
737 avg_enabled = avg_stats(&ps->res_stats[1]);
738 avg_running = avg_stats(&ps->res_stats[2]);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200739
Stephane Eranian13370a92013-01-29 12:47:44 +0100740 if (prefix)
741 fprintf(output, "%s", prefix);
742
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000743 if (scaled == -1 || !counter->supported) {
Stephane Eranian410136f2013-11-12 17:58:49 +0100744 fprintf(output, "%*s%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200745 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600746 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100747 csv_sep);
748 fprintf(output, "%-*s%s",
749 csv_output ? 0 : unit_width,
750 counter->unit, csv_sep);
751 fprintf(output, "%*s",
752 csv_output ? 0 : -25,
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300753 perf_evsel__name(counter));
Stephane Eranian023695d2011-02-14 11:20:01 +0200754
755 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200756 fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200757
Andi Kleend73515c2015-03-11 07:16:27 -0700758 print_running(avg_running, avg_enabled);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200759 fputc('\n', output);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200760 return;
761 }
762
Stephane Eranian410136f2013-11-12 17:58:49 +0100763 uval = avg * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800764 printout(-1, 0, counter, uval);
Peter Zijlstra849abde2009-09-04 18:23:38 +0200765
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700766 print_noise(counter, avg);
767
Andi Kleend73515c2015-03-11 07:16:27 -0700768 print_running(avg_running, avg_enabled);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200769 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200770}
771
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200772/*
773 * Print out the results of a single counter:
774 * does not use aggregated count in system-wide
775 */
Stephane Eranian13370a92013-01-29 12:47:44 +0100776static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200777{
Jiri Olsa58215222015-07-21 14:31:24 +0200778 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200779 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +0100780 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200781 int cpu;
782
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800783 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200784 val = perf_counts(counter->counts, cpu, 0)->val;
785 ena = perf_counts(counter->counts, cpu, 0)->ena;
786 run = perf_counts(counter->counts, cpu, 0)->run;
Stephane Eranian13370a92013-01-29 12:47:44 +0100787
788 if (prefix)
789 fprintf(output, "%s", prefix);
790
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200791 if (run == 0 || ena == 0) {
Stephane Eranian410136f2013-11-12 17:58:49 +0100792 fprintf(output, "CPU%*d%s%*s%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200793 csv_output ? 0 : -4,
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800794 perf_evsel__cpus(counter)->map[cpu], csv_sep,
Stephane Eraniand7470b62010-12-01 18:49:05 +0200795 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600796 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100797 csv_sep);
798
799 fprintf(output, "%-*s%s",
800 csv_output ? 0 : unit_width,
801 counter->unit, csv_sep);
802
803 fprintf(output, "%*s",
804 csv_output ? 0 : -25,
805 perf_evsel__name(counter));
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200806
Stephane Eranian023695d2011-02-14 11:20:01 +0200807 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200808 fprintf(output, "%s%s",
809 csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200810
Andi Kleend73515c2015-03-11 07:16:27 -0700811 print_running(run, ena);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200812 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200813 continue;
814 }
815
Stephane Eranian410136f2013-11-12 17:58:49 +0100816 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800817 printout(cpu, 0, counter, uval);
Andi Kleend73515c2015-03-11 07:16:27 -0700818 if (!csv_output)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200819 print_noise(counter, 1.0);
Andi Kleend73515c2015-03-11 07:16:27 -0700820 print_running(run, ena);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200821
Stephane Eranian4aa90152011-08-15 22:22:33 +0200822 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200823 }
824}
825
Jiri Olsad4f63a42015-06-26 11:29:26 +0200826static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200827{
Jiri Olsa58215222015-07-21 14:31:24 +0200828 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +0200829 static int num_print_interval;
830
831 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
832
833 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +0200834 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +0200835 case AGGR_SOCKET:
836 fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit");
837 break;
838 case AGGR_CORE:
839 fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit");
840 break;
841 case AGGR_NONE:
842 fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit");
843 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200844 case AGGR_THREAD:
845 fprintf(output, "# time comm-pid counts %*s events\n", unit_width, "unit");
846 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +0200847 case AGGR_GLOBAL:
848 default:
849 fprintf(output, "# time counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +0200850 case AGGR_UNSET:
851 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +0200852 }
853 }
854
855 if (++num_print_interval == 25)
856 num_print_interval = 0;
857}
858
859static void print_header(int argc, const char **argv)
860{
Jiri Olsa58215222015-07-21 14:31:24 +0200861 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200862 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200863
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200864 fflush(stdout);
865
Stephane Eraniand7470b62010-12-01 18:49:05 +0200866 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200867 fprintf(output, "\n");
868 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -0600869 if (target.system_wide)
870 fprintf(output, "\'system wide");
871 else if (target.cpu_list)
872 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300873 else if (!target__has_task(&target)) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200874 fprintf(output, "\'%s", argv[0]);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200875 for (i = 1; i < argc; i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200876 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +0900877 } else if (target.pid)
878 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200879 else
Namhyung Kim20f946b2012-04-26 14:15:16 +0900880 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +0200881
Stephane Eranian4aa90152011-08-15 22:22:33 +0200882 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +0200883 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200884 fprintf(output, " (%d runs)", run_count);
885 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +0200886 }
Jiri Olsad4f63a42015-06-26 11:29:26 +0200887}
888
889static void print_footer(void)
890{
Jiri Olsa58215222015-07-21 14:31:24 +0200891 FILE *output = stat_config.output;
892
Jiri Olsad4f63a42015-06-26 11:29:26 +0200893 if (!null_run)
894 fprintf(output, "\n");
895 fprintf(output, " %17.9f seconds time elapsed",
896 avg_stats(&walltime_nsecs_stats)/1e9);
897 if (run_count > 1) {
898 fprintf(output, " ");
899 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
900 avg_stats(&walltime_nsecs_stats));
901 }
902 fprintf(output, "\n\n");
903}
904
905static void print_counters(struct timespec *ts, int argc, const char **argv)
906{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200907 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +0200908 struct perf_evsel *counter;
909 char buf[64], *prefix = NULL;
910
911 if (interval)
912 print_interval(prefix = buf, ts);
913 else
914 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200915
Jiri Olsa421a50f2015-07-21 14:31:22 +0200916 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100917 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100918 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +0200919 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100920 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200921 case AGGR_THREAD:
922 evlist__for_each(evsel_list, counter)
923 print_aggr_thread(counter, prefix);
924 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100925 case AGGR_GLOBAL:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300926 evlist__for_each(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +0200927 print_counter_aggr(counter, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100928 break;
929 case AGGR_NONE:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300930 evlist__for_each(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +0200931 print_counter(counter, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100932 break;
Jiri Olsa208df992015-10-16 12:41:04 +0200933 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100934 default:
935 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200936 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200937
Jiri Olsad4f63a42015-06-26 11:29:26 +0200938 if (!interval && !csv_output)
939 print_footer();
940
Jiri Olsa58215222015-07-21 14:31:24 +0200941 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200942}
943
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200944static volatile int signr = -1;
945
Ingo Molnar52425192009-05-26 09:17:18 +0200946static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200947{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200948 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +0800949 done = 1;
950
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200951 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200952 /*
953 * render child_pid harmless
954 * won't send SIGTERM to a random
955 * process in case of race condition
956 * and fast PID recycling
957 */
958 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200959}
960
961static void sig_atexit(void)
962{
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200963 sigset_t set, oset;
964
965 /*
966 * avoid race condition with SIGCHLD handler
967 * in skip_signal() which is modifying child_pid
968 * goal is to avoid send SIGTERM to a random
969 * process
970 */
971 sigemptyset(&set);
972 sigaddset(&set, SIGCHLD);
973 sigprocmask(SIG_BLOCK, &set, &oset);
974
Chris Wilson933da832009-10-04 01:35:01 +0100975 if (child_pid != -1)
976 kill(child_pid, SIGTERM);
977
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200978 sigprocmask(SIG_SETMASK, &oset, NULL);
979
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200980 if (signr == -1)
981 return;
982
983 signal(signr, SIG_DFL);
984 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +0200985}
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200986
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300987static int stat__set_big_num(const struct option *opt __maybe_unused,
988 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200989{
990 big_num_opt = unset ? 0 : 1;
991 return 0;
992}
993
Jiri Olsae0547312015-11-05 15:40:45 +0100994static const struct option stat_options[] = {
995 OPT_BOOLEAN('T', "transaction", &transaction_run,
996 "hardware transaction statistics"),
997 OPT_CALLBACK('e', "event", &evsel_list, "event",
998 "event selector. use 'perf list' to list available events",
999 parse_events_option),
1000 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1001 "event filter", parse_filter),
1002 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1003 "child tasks do not inherit counters"),
1004 OPT_STRING('p', "pid", &target.pid, "pid",
1005 "stat events on existing process id"),
1006 OPT_STRING('t', "tid", &target.tid, "tid",
1007 "stat events on existing thread id"),
1008 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1009 "system-wide collection from all CPUs"),
1010 OPT_BOOLEAN('g', "group", &group,
1011 "put the counters into a counter group"),
1012 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1013 OPT_INCR('v', "verbose", &verbose,
1014 "be more verbose (show counter open errors, etc)"),
1015 OPT_INTEGER('r', "repeat", &run_count,
1016 "repeat command and print average + stddev (max: 100, forever: 0)"),
1017 OPT_BOOLEAN('n', "null", &null_run,
1018 "null run - dont start any counters"),
1019 OPT_INCR('d', "detailed", &detailed_run,
1020 "detailed run - start a lot of events"),
1021 OPT_BOOLEAN('S', "sync", &sync_run,
1022 "call sync() before starting a run"),
1023 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1024 "print large numbers with thousands\' separators",
1025 stat__set_big_num),
1026 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1027 "list of cpus to monitor in system-wide"),
1028 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1029 "disable CPU count aggregation", AGGR_NONE),
1030 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1031 "print counts with custom separator"),
1032 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1033 "monitor event in cgroup name only", parse_cgroups),
1034 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1035 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1036 OPT_INTEGER(0, "log-fd", &output_fd,
1037 "log output to fd, instead of stderr"),
1038 OPT_STRING(0, "pre", &pre_cmd, "command",
1039 "command to run prior to the measured command"),
1040 OPT_STRING(0, "post", &post_cmd, "command",
1041 "command to run after to the measured command"),
1042 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1043 "print counts at regular interval in ms (>= 10)"),
1044 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1045 "aggregate counts per processor socket", AGGR_SOCKET),
1046 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1047 "aggregate counts per physical processor core", AGGR_CORE),
1048 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1049 "aggregate counts per thread", AGGR_THREAD),
1050 OPT_UINTEGER('D', "delay", &initial_delay,
1051 "ms to wait before starting measurement after program start"),
1052 OPT_END()
1053};
1054
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001055static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1056{
1057 return cpu_map__get_socket(map, cpu, NULL);
1058}
1059
1060static int perf_stat__get_core(struct cpu_map *map, int cpu)
1061{
1062 return cpu_map__get_core(map, cpu, NULL);
1063}
1064
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001065static int cpu_map__get_max(struct cpu_map *map)
1066{
1067 int i, max = -1;
1068
1069 for (i = 0; i < map->nr; i++) {
1070 if (map->map[i] > max)
1071 max = map->map[i];
1072 }
1073
1074 return max;
1075}
1076
1077static struct cpu_map *cpus_aggr_map;
1078
1079static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1080{
1081 int cpu;
1082
1083 if (idx >= map->nr)
1084 return -1;
1085
1086 cpu = map->map[idx];
1087
1088 if (cpus_aggr_map->map[cpu] == -1)
1089 cpus_aggr_map->map[cpu] = get_id(map, idx);
1090
1091 return cpus_aggr_map->map[cpu];
1092}
1093
1094static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1095{
1096 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1097}
1098
1099static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1100{
1101 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1102}
1103
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001104static int perf_stat_init_aggr_mode(void)
1105{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001106 int nr;
1107
Jiri Olsa421a50f2015-07-21 14:31:22 +02001108 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001109 case AGGR_SOCKET:
1110 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1111 perror("cannot build socket map");
1112 return -1;
1113 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001114 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001115 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001116 case AGGR_CORE:
1117 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1118 perror("cannot build core map");
1119 return -1;
1120 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001121 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001122 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001123 case AGGR_NONE:
1124 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001125 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001126 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001127 default:
1128 break;
1129 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001130
1131 /*
1132 * The evsel_list->cpus is the base we operate on,
1133 * taking the highest cpu number to be the size of
1134 * the aggregation translate cpumap.
1135 */
1136 nr = cpu_map__get_max(evsel_list->cpus);
1137 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1138 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001139}
1140
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001141static void perf_stat__exit_aggr_mode(void)
1142{
1143 cpu_map__put(aggr_map);
1144 cpu_map__put(cpus_aggr_map);
1145 aggr_map = NULL;
1146 cpus_aggr_map = NULL;
1147}
1148
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001149/*
1150 * Add default attributes, if there were no attributes specified or
1151 * if -d/--detailed, -d -d or -d -d -d is used:
1152 */
1153static int add_default_attributes(void)
1154{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001155 struct perf_event_attr default_attrs[] = {
1156
1157 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1158 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1159 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1160 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1161
1162 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
1163 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
1164 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
1165 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1166 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1167 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1168
1169};
1170
1171/*
1172 * Detailed stats (-d), covering the L1 and last level data caches:
1173 */
1174 struct perf_event_attr detailed_attrs[] = {
1175
1176 { .type = PERF_TYPE_HW_CACHE,
1177 .config =
1178 PERF_COUNT_HW_CACHE_L1D << 0 |
1179 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1180 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1181
1182 { .type = PERF_TYPE_HW_CACHE,
1183 .config =
1184 PERF_COUNT_HW_CACHE_L1D << 0 |
1185 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1186 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1187
1188 { .type = PERF_TYPE_HW_CACHE,
1189 .config =
1190 PERF_COUNT_HW_CACHE_LL << 0 |
1191 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1192 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1193
1194 { .type = PERF_TYPE_HW_CACHE,
1195 .config =
1196 PERF_COUNT_HW_CACHE_LL << 0 |
1197 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1198 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1199};
1200
1201/*
1202 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1203 */
1204 struct perf_event_attr very_detailed_attrs[] = {
1205
1206 { .type = PERF_TYPE_HW_CACHE,
1207 .config =
1208 PERF_COUNT_HW_CACHE_L1I << 0 |
1209 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1210 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1211
1212 { .type = PERF_TYPE_HW_CACHE,
1213 .config =
1214 PERF_COUNT_HW_CACHE_L1I << 0 |
1215 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1216 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1217
1218 { .type = PERF_TYPE_HW_CACHE,
1219 .config =
1220 PERF_COUNT_HW_CACHE_DTLB << 0 |
1221 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1222 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1223
1224 { .type = PERF_TYPE_HW_CACHE,
1225 .config =
1226 PERF_COUNT_HW_CACHE_DTLB << 0 |
1227 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1228 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1229
1230 { .type = PERF_TYPE_HW_CACHE,
1231 .config =
1232 PERF_COUNT_HW_CACHE_ITLB << 0 |
1233 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1234 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1235
1236 { .type = PERF_TYPE_HW_CACHE,
1237 .config =
1238 PERF_COUNT_HW_CACHE_ITLB << 0 |
1239 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1240 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1241
1242};
1243
1244/*
1245 * Very, very detailed stats (-d -d -d), adding prefetch events:
1246 */
1247 struct perf_event_attr very_very_detailed_attrs[] = {
1248
1249 { .type = PERF_TYPE_HW_CACHE,
1250 .config =
1251 PERF_COUNT_HW_CACHE_L1D << 0 |
1252 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1253 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1254
1255 { .type = PERF_TYPE_HW_CACHE,
1256 .config =
1257 PERF_COUNT_HW_CACHE_L1D << 0 |
1258 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1259 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1260};
1261
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001262 /* Set attrs if no event is selected and !null_run: */
1263 if (null_run)
1264 return 0;
1265
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001266 if (transaction_run) {
1267 int err;
1268 if (pmu_have_event("cpu", "cycles-ct") &&
1269 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02001270 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001271 else
Jiri Olsaa4547422015-06-03 16:25:53 +02001272 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
1273 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001274 fprintf(stderr, "Cannot set up transaction events\n");
1275 return -1;
1276 }
1277 return 0;
1278 }
1279
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001280 if (!evsel_list->nr_entries) {
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001281 if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001282 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001283 }
1284
1285 /* Detailed events get appended to the event list: */
1286
1287 if (detailed_run < 1)
1288 return 0;
1289
1290 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001291 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001292 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001293
1294 if (detailed_run < 2)
1295 return 0;
1296
1297 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001298 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001299 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001300
1301 if (detailed_run < 3)
1302 return 0;
1303
1304 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001305 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001306}
1307
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001308static const char * const recort_usage[] = {
1309 "perf stat record [<options>]",
1310 NULL,
1311};
1312
1313static int __cmd_record(int argc, const char **argv)
1314{
1315 struct perf_session *session;
1316 struct perf_data_file *file = &perf_stat.file;
1317
1318 argc = parse_options(argc, argv, stat_options, record_usage,
1319 PARSE_OPT_STOP_AT_NON_OPTION);
1320
1321 if (output_name)
1322 file->path = output_name;
1323
1324 session = perf_session__new(file, false, NULL);
1325 if (session == NULL) {
1326 pr_err("Perf session creation failed.\n");
1327 return -1;
1328 }
1329
1330 /* No pipe support ATM */
1331 if (perf_stat.file.is_pipe)
1332 return -EINVAL;
1333
1334 session->evlist = evsel_list;
1335 perf_stat.session = session;
1336 perf_stat.record = true;
1337 return argc;
1338}
1339
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001340int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02001341{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001342 const char * const stat_usage[] = {
1343 "perf stat [<options>] [<command>]",
1344 NULL
1345 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09001346 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001347 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02001348 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001349 unsigned int interval;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001350 const char * const stat_subcommands[] = { "record" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02001351
Stephane Eranian5af52b52010-05-18 15:00:01 +02001352 setlocale(LC_ALL, "");
1353
Namhyung Kim334fe7a2013-03-11 16:43:12 +09001354 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001355 if (evsel_list == NULL)
1356 return -ENOMEM;
1357
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001358 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
1359 (const char **) stat_usage,
1360 PARSE_OPT_STOP_AT_NON_OPTION);
1361
1362 if (argc && !strncmp(argv[0], "rec", 3)) {
1363 argc = __cmd_record(argc, argv);
1364 if (argc < 0)
1365 return -1;
1366 }
Stephane Eraniand7470b62010-12-01 18:49:05 +02001367
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001368 interval = stat_config.interval;
1369
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001370 /*
1371 * For record command the -o is already taken care of.
1372 */
1373 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02001374 output = NULL;
1375
Jim Cromie56f3bae2011-09-07 17:14:00 -06001376 if (output_name && output_fd) {
1377 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001378 parse_options_usage(stat_usage, stat_options, "o", 1);
1379 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001380 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06001381 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001382
1383 if (output_fd < 0) {
1384 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001385 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001386 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001387 }
1388
Stephane Eranian4aa90152011-08-15 22:22:33 +02001389 if (!output) {
1390 struct timespec tm;
1391 mode = append_file ? "a" : "w";
1392
1393 output = fopen(output_name, mode);
1394 if (!output) {
1395 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06001396 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001397 }
1398 clock_gettime(CLOCK_REALTIME, &tm);
1399 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001400 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06001401 mode = append_file ? "a" : "w";
1402 output = fdopen(output_fd, mode);
1403 if (!output) {
1404 perror("Failed opening logfd");
1405 return -errno;
1406 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001407 }
1408
Jiri Olsa58215222015-07-21 14:31:24 +02001409 stat_config.output = output;
1410
Jim Cromied4ffd042011-09-07 17:14:03 -06001411 if (csv_sep) {
Stephane Eraniand7470b62010-12-01 18:49:05 +02001412 csv_output = true;
Jim Cromied4ffd042011-09-07 17:14:03 -06001413 if (!strcmp(csv_sep, "\\t"))
1414 csv_sep = "\t";
1415 } else
Stephane Eraniand7470b62010-12-01 18:49:05 +02001416 csv_sep = DEFAULT_SEPARATOR;
1417
1418 /*
1419 * let the spreadsheet do the pretty-printing
1420 */
1421 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06001422 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02001423 if (big_num_opt == 1) {
1424 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001425 parse_options_usage(stat_usage, stat_options, "B", 1);
1426 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001427 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02001428 } else /* Nope, so disable big number formatting */
1429 big_num = false;
1430 } else if (big_num_opt == 0) /* User passed --no-big-num */
1431 big_num = false;
1432
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001433 if (!argc && target__none(&target))
Jiri Olsae0547312015-11-05 15:40:45 +01001434 usage_with_options(stat_usage, stat_options);
David Ahernac3063b2013-09-30 07:37:37 -06001435
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001436 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09001437 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001438 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001439 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001440 } else if (run_count == 0) {
1441 forever = true;
1442 run_count = 1;
1443 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001444
Jiri Olsa421a50f2015-07-21 14:31:22 +02001445 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001446 fprintf(stderr, "The --per-thread option is only available "
1447 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001448 parse_options_usage(NULL, stat_options, "p", 1);
1449 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001450 goto out;
1451 }
1452
1453 /*
1454 * no_aggr, cgroup are for system-wide only
1455 * --per-thread is aggregated per thread, we dont mix it with cpu mode
1456 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02001457 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
1458 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001459 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02001460 fprintf(stderr, "both cgroup and no-aggregation "
1461 "modes only available in system-wide mode\n");
1462
Jiri Olsae0547312015-11-05 15:40:45 +01001463 parse_options_usage(stat_usage, stat_options, "G", 1);
1464 parse_options_usage(NULL, stat_options, "A", 1);
1465 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001466 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001467 }
1468
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001469 if (add_default_attributes())
1470 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001471
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001472 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02001473
Namhyung Kim77a6f012012-05-07 14:09:04 +09001474 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001475 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09001476 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001477 parse_options_usage(stat_usage, stat_options, "p", 1);
1478 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001479 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09001480 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01001481 parse_options_usage(stat_usage, stat_options, "C", 1);
1482 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001483 }
1484 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02001485 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001486
1487 /*
1488 * Initialize thread_map with comm names,
1489 * so we could print it out on output.
1490 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02001491 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001492 thread_map__read_comms(evsel_list->threads);
1493
Stephane Eranian13370a92013-01-29 12:47:44 +01001494 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04001495 if (interval < 10) {
1496 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001497 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04001498 goto out;
1499 } else
1500 pr_warning("print interval < 100ms. "
1501 "The overhead percentage could be high in some cases. "
1502 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01001503 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001504
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001505 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03001506 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001507
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001508 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03001509 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001510
Ingo Molnar58d7e992009-05-15 11:03:23 +02001511 /*
1512 * We dont want to block the signals - that would cause
1513 * child tasks to inherit that and Ctrl-C would not work.
1514 * What we want is for Ctrl-C to work in the exec()-ed
1515 * task, but being ignored by perf stat itself:
1516 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001517 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001518 if (!forever)
1519 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01001520 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02001521 signal(SIGALRM, skip_signal);
1522 signal(SIGABRT, skip_signal);
1523
Ingo Molnar42202dd2009-06-13 14:57:28 +02001524 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001525 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Ingo Molnar42202dd2009-06-13 14:57:28 +02001526 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001527 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
1528 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02001529
Ingo Molnar42202dd2009-06-13 14:57:28 +02001530 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001531 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001532 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02001533 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001534 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02001535 }
1536
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001537 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001538 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001539
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001540 if (STAT_RECORD) {
1541 /*
1542 * We synthesize the kernel mmap record just so that older tools
1543 * don't emit warnings about not being able to resolve symbols
1544 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
1545 * a saner message about no samples being in the perf.data file.
1546 *
1547 * This also serves to suppress a warning about f_header.data.size == 0
1548 * in header.c. -acme
1549 */
1550 int fd = perf_data_file__fd(&perf_stat.file);
1551 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
1552 process_synthesized_event,
1553 &perf_stat.session->machines.host);
1554 if (err) {
1555 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
1556 "older tools may produce warnings about this file\n.");
1557 }
1558
1559 perf_stat.session->header.data_size += perf_stat.bytes_written;
1560 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
1561
1562 perf_session__delete(perf_stat.session);
1563 }
1564
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001565 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001566 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02001567out:
1568 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001569 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001570}