blob: abba49b847d2911258c89b6de5e7054ce39f191d [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"
Jiri Olsaba6039b62015-11-05 15:40:55 +010063#include "util/tool.h"
64#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020065
Peter Zijlstra1f16c572012-10-23 13:40:14 +020066#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020067#include <sys/prctl.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020068#include <locale.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020069
Stephane Eraniand7470b62010-12-01 18:49:05 +020070#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060071#define CNTR_NOT_SUPPORTED "<not supported>"
72#define CNTR_NOT_COUNTED "<not counted>"
Stephane Eraniand7470b62010-12-01 18:49:05 +020073
Jiri Olsad4f63a42015-06-26 11:29:26 +020074static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010075
Andi Kleen4cabc3d2013-08-21 16:47:26 -070076/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020077static const char *transaction_attrs = {
78 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070079 "{"
80 "instructions,"
81 "cycles,"
82 "cpu/cycles-t/,"
83 "cpu/tx-start/,"
84 "cpu/el-start/,"
85 "cpu/cycles-ct/"
86 "}"
87};
88
89/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +020090static const char * transaction_limited_attrs = {
91 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070092 "{"
93 "instructions,"
94 "cycles,"
95 "cpu/cycles-t/,"
96 "cpu/tx-start/"
97 "}"
98};
99
Robert Richter666e6d42012-04-05 18:26:27 +0200100static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200101
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300102static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900103 .uid = UINT_MAX,
104};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530105
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100106typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
107
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530108static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200109static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200110static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000111static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200112static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700113static bool transaction_run;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200114static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200115static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200116static const char *csv_sep = NULL;
117static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800118static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200119static const char *pre_cmd = NULL;
120static const char *post_cmd = NULL;
121static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700122static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100123static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500124static bool forever = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100125static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100126static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100127static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100128static bool append_file;
129static const char *output_name;
130static int output_fd;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200131
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100132struct perf_stat {
133 bool record;
134 struct perf_data_file file;
135 struct perf_session *session;
136 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100137 struct perf_tool tool;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100138};
139
140static struct perf_stat perf_stat;
141#define STAT_RECORD perf_stat.record
142
Liming Wang60666c62009-12-31 16:05:50 +0800143static volatile int done = 0;
144
Jiri Olsa421a50f2015-07-21 14:31:22 +0200145static struct perf_stat_config stat_config = {
146 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200147 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200148};
149
Stephane Eranian13370a92013-01-29 12:47:44 +0100150static inline void diff_timespec(struct timespec *r, struct timespec *a,
151 struct timespec *b)
152{
153 r->tv_sec = a->tv_sec - b->tv_sec;
154 if (a->tv_nsec < b->tv_nsec) {
155 r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
156 r->tv_sec--;
157 } else {
158 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
159 }
160}
161
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200162static void perf_stat__reset_stats(void)
163{
164 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200165 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200166}
167
Jiri Olsacac21422012-11-12 18:34:00 +0100168static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200169{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200170 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200171
Jiri Olsa711a5722015-07-21 14:31:23 +0200172 if (stat_config.scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200173 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
174 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200175
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200176 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300177
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100178 /*
179 * Some events get initialized with sample_(period/type) set,
180 * like tracepoints. Clear it up for counting.
181 */
182 attr->sample_period = 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100183 /*
184 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
185 * while avoiding that older tools show confusing messages.
186 */
187 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100188
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100189 /*
190 * Disabling all counters initially, they will be enabled
191 * either manually by us or by kernel via enable_on_exec
192 * set later.
193 */
Jiri Olsac8280cec2015-12-03 10:06:45 +0100194 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100195 attr->disabled = 1;
196
Jiri Olsac8280cec2015-12-03 10:06:45 +0100197 /*
198 * In case of initial_delay we enable tracee
199 * events manually.
200 */
201 if (target__none(&target) && !initial_delay)
202 attr->enable_on_exec = 1;
203 }
204
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300205 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300206 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200207
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300208 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200209}
210
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200211/*
212 * Does the counter have nsecs as a unit?
213 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200214static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200215{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200216 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
217 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200218 return 1;
219
220 return 0;
221}
222
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100223static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100224 union perf_event *event,
225 struct perf_sample *sample __maybe_unused,
226 struct machine *machine __maybe_unused)
227{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100228 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
229 pr_err("failed to write perf data, error: %m\n");
230 return -1;
231 }
232
233 perf_stat.bytes_written += event->header.size;
234 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100235}
236
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100237static int write_stat_round_event(u64 time, u64 type)
238{
239 return perf_event__synthesize_stat_round(NULL, time, type,
240 process_synthesized_event,
241 NULL);
242}
243
244#define WRITE_STAT_ROUND_EVENT(time, interval) \
245 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
246
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100247#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
248
249static int
250perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
251 struct perf_counts_values *count)
252{
253 struct perf_sample_id *sid = SID(counter, cpu, thread);
254
255 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
256 process_synthesized_event, NULL);
257}
258
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200259/*
260 * Read out the results of a single counter:
261 * do not aggregate counts across CPUs in system-wide mode
262 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200263static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200264{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100265 int nthreads = thread_map__nr(evsel_list->threads);
266 int ncpus = perf_evsel__nr_cpus(counter);
267 int cpu, thread;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200268
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000269 if (!counter->supported)
270 return -ENOENT;
271
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100272 if (counter->system_wide)
273 nthreads = 1;
274
275 for (thread = 0; thread < nthreads; thread++) {
276 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200277 struct perf_counts_values *count;
278
279 count = perf_counts(counter->counts, cpu, thread);
280 if (perf_evsel__read(counter, cpu, thread, count))
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100281 return -1;
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100282
283 if (STAT_RECORD) {
284 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
285 pr_err("failed to write stat event\n");
286 return -1;
287 }
288 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100289 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200290 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200291
292 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200293}
294
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200295static void read_counters(bool close_counters)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200296{
297 struct perf_evsel *counter;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200298
299 evlist__for_each(evsel_list, counter) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200300 if (read_counter(counter))
Andi Kleen245bad82015-09-01 15:52:46 -0700301 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200302
Jiri Olsaf80010e2015-07-21 14:31:27 +0200303 if (perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200304 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200305
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200306 if (close_counters) {
Jiri Olsa106a94a2015-06-26 11:29:19 +0200307 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
308 thread_map__nr(evsel_list->threads));
309 }
310 }
311}
312
Jiri Olsaba411a92015-06-26 11:29:24 +0200313static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100314{
Stephane Eranian13370a92013-01-29 12:47:44 +0100315 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100316
Jiri Olsa106a94a2015-06-26 11:29:19 +0200317 read_counters(false);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100318
Stephane Eranian13370a92013-01-29 12:47:44 +0100319 clock_gettime(CLOCK_MONOTONIC, &ts);
320 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100321
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100322 if (STAT_RECORD) {
323 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSECS_PER_SEC + rs.tv_nsec, INTERVAL))
324 pr_err("failed to write stat round event\n");
325 }
326
Jiri Olsad4f63a42015-06-26 11:29:26 +0200327 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100328}
329
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100330static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700331{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100332 if (initial_delay)
Andi Kleen41191682013-08-02 17:41:11 -0700333 usleep(initial_delay * 1000);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100334
335 /*
336 * We need to enable counters only if:
337 * - we don't have tracee (attaching to task or cpu)
338 * - we have initial delay configured
339 */
340 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100341 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700342}
343
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300344static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300345
346/*
347 * perf_evlist__prepare_workload will send a SIGUSR1
348 * if the fork fails, since we asked by setting its
349 * want_signal to true.
350 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300351static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
352 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300353{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300354 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300355}
356
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100357static bool has_unit(struct perf_evsel *counter)
358{
359 return counter->unit && *counter->unit;
360}
361
362static bool has_scale(struct perf_evsel *counter)
363{
364 return counter->scale != 1;
365}
366
Jiri Olsa664c98d2015-11-05 15:40:50 +0100367static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100368{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100369 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100370 int err;
371
Jiri Olsa664c98d2015-11-05 15:40:50 +0100372 if (is_pipe) {
373 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
374 process_synthesized_event);
375 if (err < 0) {
376 pr_err("Couldn't synthesize attrs.\n");
377 return err;
378 }
379 }
380
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100381 /*
382 * Synthesize other events stuff not carried within
383 * attr event - unit, scale, name
384 */
385 evlist__for_each(evsel_list, counter) {
386 if (!counter->supported)
387 continue;
388
389 /*
390 * Synthesize unit and scale only if it's defined.
391 */
392 if (has_unit(counter)) {
393 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
394 if (err < 0) {
395 pr_err("Couldn't synthesize evsel unit.\n");
396 return err;
397 }
398 }
399
400 if (has_scale(counter)) {
401 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
402 if (err < 0) {
403 pr_err("Couldn't synthesize evsel scale.\n");
404 return err;
405 }
406 }
407
408 if (counter->own_cpus) {
409 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
410 if (err < 0) {
411 pr_err("Couldn't synthesize evsel scale.\n");
412 return err;
413 }
414 }
415
416 /*
417 * Name is needed only for pipe output,
418 * perf.data carries event names.
419 */
420 if (is_pipe) {
421 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
422 if (err < 0) {
423 pr_err("Couldn't synthesize evsel name.\n");
424 return err;
425 }
426 }
427 }
428
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100429 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
430 process_synthesized_event,
431 NULL);
432 if (err < 0) {
433 pr_err("Couldn't synthesize thread map.\n");
434 return err;
435 }
436
437 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
438 process_synthesized_event, NULL);
439 if (err < 0) {
440 pr_err("Couldn't synthesize thread map.\n");
441 return err;
442 }
443
444 err = perf_event__synthesize_stat_config(NULL, &stat_config,
445 process_synthesized_event, NULL);
446 if (err < 0) {
447 pr_err("Couldn't synthesize config.\n");
448 return err;
449 }
450
451 return 0;
452}
453
Jiri Olsa2af46462015-11-05 15:40:49 +0100454#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
455
456static int __store_counter_ids(struct perf_evsel *counter,
457 struct cpu_map *cpus,
458 struct thread_map *threads)
459{
460 int cpu, thread;
461
462 for (cpu = 0; cpu < cpus->nr; cpu++) {
463 for (thread = 0; thread < threads->nr; thread++) {
464 int fd = FD(counter, cpu, thread);
465
466 if (perf_evlist__id_add_fd(evsel_list, counter,
467 cpu, thread, fd) < 0)
468 return -1;
469 }
470 }
471
472 return 0;
473}
474
475static int store_counter_ids(struct perf_evsel *counter)
476{
477 struct cpu_map *cpus = counter->cpus;
478 struct thread_map *threads = counter->threads;
479
480 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
481 return -ENOMEM;
482
483 return __store_counter_ids(counter, cpus, threads);
484}
485
Namhyung Kimacf28922013-03-11 16:43:18 +0900486static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200487{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200488 int interval = stat_config.interval;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300489 char msg[512];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200490 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100491 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100492 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100493 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200494 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300495 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100496 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200497
Stephane Eranian13370a92013-01-29 12:47:44 +0100498 if (interval) {
499 ts.tv_sec = interval / 1000;
500 ts.tv_nsec = (interval % 1000) * 1000000;
501 } else {
502 ts.tv_sec = 1;
503 ts.tv_nsec = 0;
504 }
505
Liming Wang60666c62009-12-31 16:05:50 +0800506 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100507 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300508 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900509 perror("failed to prepare workload");
510 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800511 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900512 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000513 }
514
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200515 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300516 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200517
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300518 evlist__for_each(evsel_list, counter) {
Jiri Olsacac21422012-11-12 18:34:00 +0100519 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600520 /*
521 * PPC returns ENXIO for HW counters until 2.6.37
522 * (behavior changed with commit b0a873e).
523 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100524 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600525 errno == ENOENT || errno == EOPNOTSUPP ||
526 errno == ENXIO) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600527 if (verbose)
528 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300529 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600530 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400531
532 if ((counter->leader != counter) ||
533 !(counter->leader->nr_members > 1))
534 continue;
David Ahernc63ca0c2011-04-29 16:04:15 -0600535 }
Ingo Molnarede70292011-04-28 08:48:42 +0200536
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300537 perf_evsel__open_strerror(counter, &target,
538 errno, msg, sizeof(msg));
539 ui__error("%s\n", msg);
540
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200541 if (child_pid != -1)
542 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600543
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200544 return -1;
545 }
David Ahern2cee77c2011-05-30 08:55:59 -0600546 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100547
548 l = strlen(counter->unit);
549 if (l > unit_width)
550 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100551
552 if (STAT_RECORD && store_counter_ids(counter))
553 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300554 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200555
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300556 if (perf_evlist__apply_filters(evsel_list, &counter)) {
557 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
558 counter->filter, perf_evsel__name(counter), errno,
Masami Hiramatsu759e6122014-08-14 02:22:55 +0000559 strerror_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100560 return -1;
561 }
562
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100563 if (STAT_RECORD) {
564 int err, fd = perf_data_file__fd(&perf_stat.file);
565
Jiri Olsa664c98d2015-11-05 15:40:50 +0100566 if (is_pipe) {
567 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
568 } else {
569 err = perf_session__write_header(perf_stat.session, evsel_list,
570 fd, false);
571 }
572
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100573 if (err < 0)
574 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100575
Jiri Olsa664c98d2015-11-05 15:40:50 +0100576 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100577 if (err < 0)
578 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100579 }
580
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200581 /*
582 * Enable counters and exec the command:
583 */
584 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100585 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200586
Liming Wang60666c62009-12-31 16:05:50 +0800587 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900588 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100589 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900590
Stephane Eranian13370a92013-01-29 12:47:44 +0100591 if (interval) {
592 while (!waitpid(child_pid, &status, WNOHANG)) {
593 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200594 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100595 }
596 }
Liming Wang60666c62009-12-31 16:05:50 +0800597 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300598
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300599 if (workload_exec_errno) {
600 const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
601 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300602 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300603 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300604
Andi Kleen33e49ea2011-09-15 14:31:40 -0700605 if (WIFSIGNALED(status))
606 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800607 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100608 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100609 while (!done) {
610 nanosleep(&ts, NULL);
611 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200612 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100613 }
Liming Wang60666c62009-12-31 16:05:50 +0800614 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200615
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200616 t1 = rdclock();
617
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200618 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200619
Jiri Olsa106a94a2015-06-26 11:29:19 +0200620 read_counters(true);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200621
Ingo Molnar42202dd2009-06-13 14:57:28 +0200622 return WEXITSTATUS(status);
623}
624
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300625static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200626{
627 int ret;
628
629 if (pre_cmd) {
630 ret = system(pre_cmd);
631 if (ret)
632 return ret;
633 }
634
635 if (sync_run)
636 sync();
637
638 ret = __run_perf_stat(argc, argv);
639 if (ret)
640 return ret;
641
642 if (post_cmd) {
643 ret = system(post_cmd);
644 if (ret)
645 return ret;
646 }
647
648 return ret;
649}
650
Andi Kleend73515c2015-03-11 07:16:27 -0700651static void print_running(u64 run, u64 ena)
652{
653 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200654 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700655 csv_sep,
656 run,
657 csv_sep,
658 ena ? 100.0 * run / ena : 100.0);
659 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200660 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700661 }
662}
663
Ingo Molnarf99844c2011-04-27 05:35:39 +0200664static void print_noise_pct(double total, double avg)
665{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800666 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200667
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700668 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200669 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600670 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200671 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200672}
673
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200674static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200675{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200676 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200677
Peter Zijlstra849abde2009-09-04 18:23:38 +0200678 if (run_count == 1)
679 return;
680
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200681 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200682 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200683}
684
Stephane Eranian12c08a92013-02-14 13:57:29 +0100685static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200686{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200687 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100688 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200689 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100690 cpu_map__id_to_socket(id),
691 csv_output ? 0 : -8,
692 cpu_map__id_to_cpu(id),
693 csv_sep,
694 csv_output ? 0 : 4,
695 nr,
696 csv_sep);
697 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100698 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200699 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100700 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100701 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100702 csv_sep,
703 csv_output ? 0 : 4,
704 nr,
705 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100706 break;
707 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200708 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200709 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100710 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100711 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200712 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200713 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200714 csv_output ? 0 : 16,
715 thread_map__comm(evsel->threads, id),
716 csv_output ? 0 : -8,
717 thread_map__pid(evsel->threads, id),
718 csv_sep);
719 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100720 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200721 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100722 default:
723 break;
724 }
725}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200726
Andi Kleenda88c7f2014-09-24 13:50:46 -0700727static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100728{
Jiri Olsa58215222015-07-21 14:31:24 +0200729 FILE *output = stat_config.output;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100730 double msecs = avg / 1e6;
Stephane Eranian410136f2013-11-12 17:58:49 +0100731 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600732 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100733
Stephane Eranian410136f2013-11-12 17:58:49 +0100734 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
735 fmt_n = csv_output ? "%s" : "%-25s";
736
Andi Kleenda88c7f2014-09-24 13:50:46 -0700737 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100738
David Ahern4bbe5a62013-09-28 14:28:00 -0600739 scnprintf(name, sizeof(name), "%s%s",
740 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +0100741
742 fprintf(output, fmt_v, msecs, csv_sep);
743
744 if (csv_output)
745 fprintf(output, "%s%s", evsel->unit, csv_sep);
746 else
747 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
748
749 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200750
Stephane Eranian023695d2011-02-14 11:20:01 +0200751 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200752 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200753}
754
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200755static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
756{
Jiri Olsa58215222015-07-21 14:31:24 +0200757 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200758 double sc = evsel->scale;
759 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200760
761 if (csv_output) {
762 fmt = sc != 1.0 ? "%.2f%s" : "%.0f%s";
763 } else {
764 if (big_num)
765 fmt = sc != 1.0 ? "%'18.2f%s" : "%'18.0f%s";
766 else
767 fmt = sc != 1.0 ? "%18.2f%s" : "%18.0f%s";
768 }
769
770 aggr_printout(evsel, id, nr);
771
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200772 fprintf(output, fmt, avg, csv_sep);
773
774 if (evsel->unit)
775 fprintf(output, "%-*s%s",
776 csv_output ? 0 : unit_width,
777 evsel->unit, csv_sep);
778
779 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
780
781 if (evsel->cgrp)
782 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -0800783}
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200784
Andi Kleeneedfcb42015-11-02 17:50:21 -0800785static void printout(int id, int nr, struct perf_evsel *counter, double uval)
786{
787 int cpu = cpu_map__id_to_cpu(id);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200788
Andi Kleeneedfcb42015-11-02 17:50:21 -0800789 if (stat_config.aggr_mode == AGGR_GLOBAL)
790 cpu = 0;
791
792 if (nsec_counter(counter))
793 nsec_printout(id, nr, counter, uval);
794 else
795 abs_printout(id, nr, counter, uval);
796
797 if (!csv_output && !stat_config.interval)
798 perf_stat__print_shadow_stats(stat_config.output, counter,
799 uval, cpu,
800 stat_config.aggr_mode);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200801}
802
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100803static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100804{
Jiri Olsa58215222015-07-21 14:31:24 +0200805 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100806 struct perf_evsel *counter;
Kan Liang601083c2015-07-02 03:08:43 -0400807 int cpu, s, s2, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +0100808 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100809 u64 ena, run, val;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100810
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100811 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100812 return;
813
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100814 for (s = 0; s < aggr_map->nr; s++) {
815 id = aggr_map->map[s];
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300816 evlist__for_each(evsel_list, counter) {
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100817 val = ena = run = 0;
818 nr = 0;
819 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Kan Liang601083c2015-07-02 03:08:43 -0400820 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100821 if (s2 != id)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100822 continue;
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200823 val += perf_counts(counter->counts, cpu, 0)->val;
824 ena += perf_counts(counter->counts, cpu, 0)->ena;
825 run += perf_counts(counter->counts, cpu, 0)->run;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100826 nr++;
827 }
828 if (prefix)
829 fprintf(output, "%s", prefix);
830
831 if (run == 0 || ena == 0) {
Stephane Eranian582ec0822013-07-05 19:06:45 +0200832 aggr_printout(counter, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100833
Stephane Eranian410136f2013-11-12 17:58:49 +0100834 fprintf(output, "%*s%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100835 csv_output ? 0 : 18,
836 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100837 csv_sep);
838
839 fprintf(output, "%-*s%s",
840 csv_output ? 0 : unit_width,
841 counter->unit, csv_sep);
842
843 fprintf(output, "%*s",
844 csv_output ? 0 : -25,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100845 perf_evsel__name(counter));
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100846
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100847 if (counter->cgrp)
848 fprintf(output, "%s%s",
849 csv_sep, counter->cgrp->name);
850
Andi Kleend73515c2015-03-11 07:16:27 -0700851 print_running(run, ena);
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100852 fputc('\n', output);
853 continue;
854 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100855 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800856 printout(id, nr, counter, uval);
Andi Kleend73515c2015-03-11 07:16:27 -0700857 if (!csv_output)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100858 print_noise(counter, 1.0);
859
Andi Kleend73515c2015-03-11 07:16:27 -0700860 print_running(run, ena);
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100861 fputc('\n', output);
862 }
863 }
864}
865
Jiri Olsa32b8af82015-06-26 11:29:27 +0200866static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
867{
Jiri Olsa58215222015-07-21 14:31:24 +0200868 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200869 int nthreads = thread_map__nr(counter->threads);
870 int ncpus = cpu_map__nr(counter->cpus);
871 int cpu, thread;
872 double uval;
873
874 for (thread = 0; thread < nthreads; thread++) {
875 u64 ena = 0, run = 0, val = 0;
876
877 for (cpu = 0; cpu < ncpus; cpu++) {
878 val += perf_counts(counter->counts, cpu, thread)->val;
879 ena += perf_counts(counter->counts, cpu, thread)->ena;
880 run += perf_counts(counter->counts, cpu, thread)->run;
881 }
882
883 if (prefix)
884 fprintf(output, "%s", prefix);
885
886 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800887 printout(thread, 0, counter, uval);
Jiri Olsa32b8af82015-06-26 11:29:27 +0200888
889 if (!csv_output)
890 print_noise(counter, 1.0);
891
892 print_running(run, ena);
893 fputc('\n', output);
894 }
895}
896
Ingo Molnar42202dd2009-06-13 14:57:28 +0200897/*
898 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200899 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +0200900 */
Stephane Eranian13370a92013-01-29 12:47:44 +0100901static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200902{
Jiri Olsa58215222015-07-21 14:31:24 +0200903 FILE *output = stat_config.output;
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200904 struct perf_stat_evsel *ps = counter->priv;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200905 double avg = avg_stats(&ps->res_stats[0]);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200906 int scaled = counter->counts->scaled;
Stephane Eranian410136f2013-11-12 17:58:49 +0100907 double uval;
Andi Kleend73515c2015-03-11 07:16:27 -0700908 double avg_enabled, avg_running;
909
910 avg_enabled = avg_stats(&ps->res_stats[1]);
911 avg_running = avg_stats(&ps->res_stats[2]);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200912
Stephane Eranian13370a92013-01-29 12:47:44 +0100913 if (prefix)
914 fprintf(output, "%s", prefix);
915
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000916 if (scaled == -1 || !counter->supported) {
Stephane Eranian410136f2013-11-12 17:58:49 +0100917 fprintf(output, "%*s%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200918 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600919 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100920 csv_sep);
921 fprintf(output, "%-*s%s",
922 csv_output ? 0 : unit_width,
923 counter->unit, csv_sep);
924 fprintf(output, "%*s",
925 csv_output ? 0 : -25,
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300926 perf_evsel__name(counter));
Stephane Eranian023695d2011-02-14 11:20:01 +0200927
928 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200929 fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200930
Andi Kleend73515c2015-03-11 07:16:27 -0700931 print_running(avg_running, avg_enabled);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200932 fputc('\n', output);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200933 return;
934 }
935
Stephane Eranian410136f2013-11-12 17:58:49 +0100936 uval = avg * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800937 printout(-1, 0, counter, uval);
Peter Zijlstra849abde2009-09-04 18:23:38 +0200938
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700939 print_noise(counter, avg);
940
Andi Kleend73515c2015-03-11 07:16:27 -0700941 print_running(avg_running, avg_enabled);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200942 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200943}
944
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200945/*
946 * Print out the results of a single counter:
947 * does not use aggregated count in system-wide
948 */
Stephane Eranian13370a92013-01-29 12:47:44 +0100949static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200950{
Jiri Olsa58215222015-07-21 14:31:24 +0200951 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200952 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +0100953 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200954 int cpu;
955
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800956 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200957 val = perf_counts(counter->counts, cpu, 0)->val;
958 ena = perf_counts(counter->counts, cpu, 0)->ena;
959 run = perf_counts(counter->counts, cpu, 0)->run;
Stephane Eranian13370a92013-01-29 12:47:44 +0100960
961 if (prefix)
962 fprintf(output, "%s", prefix);
963
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200964 if (run == 0 || ena == 0) {
Stephane Eranian410136f2013-11-12 17:58:49 +0100965 fprintf(output, "CPU%*d%s%*s%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200966 csv_output ? 0 : -4,
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800967 perf_evsel__cpus(counter)->map[cpu], csv_sep,
Stephane Eraniand7470b62010-12-01 18:49:05 +0200968 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600969 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100970 csv_sep);
971
972 fprintf(output, "%-*s%s",
973 csv_output ? 0 : unit_width,
974 counter->unit, csv_sep);
975
976 fprintf(output, "%*s",
977 csv_output ? 0 : -25,
978 perf_evsel__name(counter));
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200979
Stephane Eranian023695d2011-02-14 11:20:01 +0200980 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200981 fprintf(output, "%s%s",
982 csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200983
Andi Kleend73515c2015-03-11 07:16:27 -0700984 print_running(run, ena);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200985 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200986 continue;
987 }
988
Stephane Eranian410136f2013-11-12 17:58:49 +0100989 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800990 printout(cpu, 0, counter, uval);
Andi Kleend73515c2015-03-11 07:16:27 -0700991 if (!csv_output)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200992 print_noise(counter, 1.0);
Andi Kleend73515c2015-03-11 07:16:27 -0700993 print_running(run, ena);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200994
Stephane Eranian4aa90152011-08-15 22:22:33 +0200995 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200996 }
997}
998
Jiri Olsad4f63a42015-06-26 11:29:26 +0200999static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001000{
Jiri Olsa58215222015-07-21 14:31:24 +02001001 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001002 static int num_print_interval;
1003
1004 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1005
1006 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001007 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001008 case AGGR_SOCKET:
1009 fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit");
1010 break;
1011 case AGGR_CORE:
1012 fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit");
1013 break;
1014 case AGGR_NONE:
1015 fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit");
1016 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001017 case AGGR_THREAD:
1018 fprintf(output, "# time comm-pid counts %*s events\n", unit_width, "unit");
1019 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001020 case AGGR_GLOBAL:
1021 default:
1022 fprintf(output, "# time counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001023 case AGGR_UNSET:
1024 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001025 }
1026 }
1027
1028 if (++num_print_interval == 25)
1029 num_print_interval = 0;
1030}
1031
1032static void print_header(int argc, const char **argv)
1033{
Jiri Olsa58215222015-07-21 14:31:24 +02001034 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001035 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001036
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001037 fflush(stdout);
1038
Stephane Eraniand7470b62010-12-01 18:49:05 +02001039 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001040 fprintf(output, "\n");
1041 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001042 if (target.system_wide)
1043 fprintf(output, "\'system wide");
1044 else if (target.cpu_list)
1045 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001046 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001047 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1048 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001049 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001050 } else if (target.pid)
1051 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001052 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001053 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001054
Stephane Eranian4aa90152011-08-15 22:22:33 +02001055 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001056 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001057 fprintf(output, " (%d runs)", run_count);
1058 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001059 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001060}
1061
1062static void print_footer(void)
1063{
Jiri Olsa58215222015-07-21 14:31:24 +02001064 FILE *output = stat_config.output;
1065
Jiri Olsad4f63a42015-06-26 11:29:26 +02001066 if (!null_run)
1067 fprintf(output, "\n");
1068 fprintf(output, " %17.9f seconds time elapsed",
1069 avg_stats(&walltime_nsecs_stats)/1e9);
1070 if (run_count > 1) {
1071 fprintf(output, " ");
1072 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1073 avg_stats(&walltime_nsecs_stats));
1074 }
1075 fprintf(output, "\n\n");
1076}
1077
1078static void print_counters(struct timespec *ts, int argc, const char **argv)
1079{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001080 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001081 struct perf_evsel *counter;
1082 char buf[64], *prefix = NULL;
1083
Jiri Olsa664c98d2015-11-05 15:40:50 +01001084 /* Do not print anything if we record to the pipe. */
1085 if (STAT_RECORD && perf_stat.file.is_pipe)
1086 return;
1087
Jiri Olsad4f63a42015-06-26 11:29:26 +02001088 if (interval)
1089 print_interval(prefix = buf, ts);
1090 else
1091 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001092
Jiri Olsa421a50f2015-07-21 14:31:22 +02001093 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001094 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001095 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001096 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001097 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001098 case AGGR_THREAD:
1099 evlist__for_each(evsel_list, counter)
1100 print_aggr_thread(counter, prefix);
1101 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001102 case AGGR_GLOBAL:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001103 evlist__for_each(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001104 print_counter_aggr(counter, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001105 break;
1106 case AGGR_NONE:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001107 evlist__for_each(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001108 print_counter(counter, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001109 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001110 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001111 default:
1112 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001113 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001114
Jiri Olsad4f63a42015-06-26 11:29:26 +02001115 if (!interval && !csv_output)
1116 print_footer();
1117
Jiri Olsa58215222015-07-21 14:31:24 +02001118 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001119}
1120
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001121static volatile int signr = -1;
1122
Ingo Molnar52425192009-05-26 09:17:18 +02001123static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001124{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001125 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001126 done = 1;
1127
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001128 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001129 /*
1130 * render child_pid harmless
1131 * won't send SIGTERM to a random
1132 * process in case of race condition
1133 * and fast PID recycling
1134 */
1135 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001136}
1137
1138static void sig_atexit(void)
1139{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001140 sigset_t set, oset;
1141
1142 /*
1143 * avoid race condition with SIGCHLD handler
1144 * in skip_signal() which is modifying child_pid
1145 * goal is to avoid send SIGTERM to a random
1146 * process
1147 */
1148 sigemptyset(&set);
1149 sigaddset(&set, SIGCHLD);
1150 sigprocmask(SIG_BLOCK, &set, &oset);
1151
Chris Wilson933da832009-10-04 01:35:01 +01001152 if (child_pid != -1)
1153 kill(child_pid, SIGTERM);
1154
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001155 sigprocmask(SIG_SETMASK, &oset, NULL);
1156
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001157 if (signr == -1)
1158 return;
1159
1160 signal(signr, SIG_DFL);
1161 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001162}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001163
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001164static int stat__set_big_num(const struct option *opt __maybe_unused,
1165 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001166{
1167 big_num_opt = unset ? 0 : 1;
1168 return 0;
1169}
1170
Jiri Olsae0547312015-11-05 15:40:45 +01001171static const struct option stat_options[] = {
1172 OPT_BOOLEAN('T', "transaction", &transaction_run,
1173 "hardware transaction statistics"),
1174 OPT_CALLBACK('e', "event", &evsel_list, "event",
1175 "event selector. use 'perf list' to list available events",
1176 parse_events_option),
1177 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1178 "event filter", parse_filter),
1179 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1180 "child tasks do not inherit counters"),
1181 OPT_STRING('p', "pid", &target.pid, "pid",
1182 "stat events on existing process id"),
1183 OPT_STRING('t', "tid", &target.tid, "tid",
1184 "stat events on existing thread id"),
1185 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1186 "system-wide collection from all CPUs"),
1187 OPT_BOOLEAN('g', "group", &group,
1188 "put the counters into a counter group"),
1189 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1190 OPT_INCR('v', "verbose", &verbose,
1191 "be more verbose (show counter open errors, etc)"),
1192 OPT_INTEGER('r', "repeat", &run_count,
1193 "repeat command and print average + stddev (max: 100, forever: 0)"),
1194 OPT_BOOLEAN('n', "null", &null_run,
1195 "null run - dont start any counters"),
1196 OPT_INCR('d', "detailed", &detailed_run,
1197 "detailed run - start a lot of events"),
1198 OPT_BOOLEAN('S', "sync", &sync_run,
1199 "call sync() before starting a run"),
1200 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1201 "print large numbers with thousands\' separators",
1202 stat__set_big_num),
1203 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1204 "list of cpus to monitor in system-wide"),
1205 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1206 "disable CPU count aggregation", AGGR_NONE),
1207 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1208 "print counts with custom separator"),
1209 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1210 "monitor event in cgroup name only", parse_cgroups),
1211 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1212 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1213 OPT_INTEGER(0, "log-fd", &output_fd,
1214 "log output to fd, instead of stderr"),
1215 OPT_STRING(0, "pre", &pre_cmd, "command",
1216 "command to run prior to the measured command"),
1217 OPT_STRING(0, "post", &post_cmd, "command",
1218 "command to run after to the measured command"),
1219 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1220 "print counts at regular interval in ms (>= 10)"),
1221 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1222 "aggregate counts per processor socket", AGGR_SOCKET),
1223 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1224 "aggregate counts per physical processor core", AGGR_CORE),
1225 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1226 "aggregate counts per thread", AGGR_THREAD),
1227 OPT_UINTEGER('D', "delay", &initial_delay,
1228 "ms to wait before starting measurement after program start"),
1229 OPT_END()
1230};
1231
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001232static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1233{
1234 return cpu_map__get_socket(map, cpu, NULL);
1235}
1236
1237static int perf_stat__get_core(struct cpu_map *map, int cpu)
1238{
1239 return cpu_map__get_core(map, cpu, NULL);
1240}
1241
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001242static int cpu_map__get_max(struct cpu_map *map)
1243{
1244 int i, max = -1;
1245
1246 for (i = 0; i < map->nr; i++) {
1247 if (map->map[i] > max)
1248 max = map->map[i];
1249 }
1250
1251 return max;
1252}
1253
1254static struct cpu_map *cpus_aggr_map;
1255
1256static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1257{
1258 int cpu;
1259
1260 if (idx >= map->nr)
1261 return -1;
1262
1263 cpu = map->map[idx];
1264
1265 if (cpus_aggr_map->map[cpu] == -1)
1266 cpus_aggr_map->map[cpu] = get_id(map, idx);
1267
1268 return cpus_aggr_map->map[cpu];
1269}
1270
1271static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1272{
1273 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1274}
1275
1276static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1277{
1278 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1279}
1280
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001281static int perf_stat_init_aggr_mode(void)
1282{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001283 int nr;
1284
Jiri Olsa421a50f2015-07-21 14:31:22 +02001285 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001286 case AGGR_SOCKET:
1287 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1288 perror("cannot build socket map");
1289 return -1;
1290 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001291 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001292 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001293 case AGGR_CORE:
1294 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1295 perror("cannot build core map");
1296 return -1;
1297 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001298 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001299 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001300 case AGGR_NONE:
1301 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001302 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001303 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001304 default:
1305 break;
1306 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001307
1308 /*
1309 * The evsel_list->cpus is the base we operate on,
1310 * taking the highest cpu number to be the size of
1311 * the aggregation translate cpumap.
1312 */
1313 nr = cpu_map__get_max(evsel_list->cpus);
1314 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1315 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001316}
1317
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001318static void perf_stat__exit_aggr_mode(void)
1319{
1320 cpu_map__put(aggr_map);
1321 cpu_map__put(cpus_aggr_map);
1322 aggr_map = NULL;
1323 cpus_aggr_map = NULL;
1324}
1325
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001326/*
1327 * Add default attributes, if there were no attributes specified or
1328 * if -d/--detailed, -d -d or -d -d -d is used:
1329 */
1330static int add_default_attributes(void)
1331{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001332 struct perf_event_attr default_attrs[] = {
1333
1334 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1335 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1336 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1337 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1338
1339 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
1340 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
1341 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
1342 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1343 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1344 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1345
1346};
1347
1348/*
1349 * Detailed stats (-d), covering the L1 and last level data caches:
1350 */
1351 struct perf_event_attr detailed_attrs[] = {
1352
1353 { .type = PERF_TYPE_HW_CACHE,
1354 .config =
1355 PERF_COUNT_HW_CACHE_L1D << 0 |
1356 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1357 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1358
1359 { .type = PERF_TYPE_HW_CACHE,
1360 .config =
1361 PERF_COUNT_HW_CACHE_L1D << 0 |
1362 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1363 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1364
1365 { .type = PERF_TYPE_HW_CACHE,
1366 .config =
1367 PERF_COUNT_HW_CACHE_LL << 0 |
1368 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1369 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1370
1371 { .type = PERF_TYPE_HW_CACHE,
1372 .config =
1373 PERF_COUNT_HW_CACHE_LL << 0 |
1374 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1375 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1376};
1377
1378/*
1379 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1380 */
1381 struct perf_event_attr very_detailed_attrs[] = {
1382
1383 { .type = PERF_TYPE_HW_CACHE,
1384 .config =
1385 PERF_COUNT_HW_CACHE_L1I << 0 |
1386 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1387 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1388
1389 { .type = PERF_TYPE_HW_CACHE,
1390 .config =
1391 PERF_COUNT_HW_CACHE_L1I << 0 |
1392 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1393 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1394
1395 { .type = PERF_TYPE_HW_CACHE,
1396 .config =
1397 PERF_COUNT_HW_CACHE_DTLB << 0 |
1398 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1399 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1400
1401 { .type = PERF_TYPE_HW_CACHE,
1402 .config =
1403 PERF_COUNT_HW_CACHE_DTLB << 0 |
1404 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1405 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1406
1407 { .type = PERF_TYPE_HW_CACHE,
1408 .config =
1409 PERF_COUNT_HW_CACHE_ITLB << 0 |
1410 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1411 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1412
1413 { .type = PERF_TYPE_HW_CACHE,
1414 .config =
1415 PERF_COUNT_HW_CACHE_ITLB << 0 |
1416 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1417 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1418
1419};
1420
1421/*
1422 * Very, very detailed stats (-d -d -d), adding prefetch events:
1423 */
1424 struct perf_event_attr very_very_detailed_attrs[] = {
1425
1426 { .type = PERF_TYPE_HW_CACHE,
1427 .config =
1428 PERF_COUNT_HW_CACHE_L1D << 0 |
1429 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1430 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1431
1432 { .type = PERF_TYPE_HW_CACHE,
1433 .config =
1434 PERF_COUNT_HW_CACHE_L1D << 0 |
1435 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1436 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1437};
1438
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001439 /* Set attrs if no event is selected and !null_run: */
1440 if (null_run)
1441 return 0;
1442
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001443 if (transaction_run) {
1444 int err;
1445 if (pmu_have_event("cpu", "cycles-ct") &&
1446 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02001447 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001448 else
Jiri Olsaa4547422015-06-03 16:25:53 +02001449 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
1450 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001451 fprintf(stderr, "Cannot set up transaction events\n");
1452 return -1;
1453 }
1454 return 0;
1455 }
1456
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001457 if (!evsel_list->nr_entries) {
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001458 if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001459 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001460 }
1461
1462 /* Detailed events get appended to the event list: */
1463
1464 if (detailed_run < 1)
1465 return 0;
1466
1467 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001468 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001469 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001470
1471 if (detailed_run < 2)
1472 return 0;
1473
1474 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001475 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001476 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001477
1478 if (detailed_run < 3)
1479 return 0;
1480
1481 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001482 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001483}
1484
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001485static const char * const recort_usage[] = {
1486 "perf stat record [<options>]",
1487 NULL,
1488};
1489
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001490static void init_features(struct perf_session *session)
1491{
1492 int feat;
1493
1494 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
1495 perf_header__set_feat(&session->header, feat);
1496
1497 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
1498 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
1499 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1500 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1501}
1502
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001503static int __cmd_record(int argc, const char **argv)
1504{
1505 struct perf_session *session;
1506 struct perf_data_file *file = &perf_stat.file;
1507
1508 argc = parse_options(argc, argv, stat_options, record_usage,
1509 PARSE_OPT_STOP_AT_NON_OPTION);
1510
1511 if (output_name)
1512 file->path = output_name;
1513
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01001514 if (run_count != 1 || forever) {
1515 pr_err("Cannot use -r option with perf stat record.\n");
1516 return -1;
1517 }
1518
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001519 session = perf_session__new(file, false, NULL);
1520 if (session == NULL) {
1521 pr_err("Perf session creation failed.\n");
1522 return -1;
1523 }
1524
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001525 init_features(session);
1526
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001527 session->evlist = evsel_list;
1528 perf_stat.session = session;
1529 perf_stat.record = true;
1530 return argc;
1531}
1532
Jiri Olsaba6039b62015-11-05 15:40:55 +01001533static const char * const report_usage[] = {
1534 "perf stat report [<options>]",
1535 NULL,
1536};
1537
1538static struct perf_stat perf_stat = {
1539 .tool = {
1540 .attr = perf_event__process_attr,
1541 },
1542};
1543
1544static int __cmd_report(int argc, const char **argv)
1545{
1546 struct perf_session *session;
1547 const struct option options[] = {
1548 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1549 OPT_END()
1550 };
1551 struct stat st;
1552 int ret;
1553
1554 argc = parse_options(argc, argv, options, report_usage, 0);
1555
1556 if (!input_name || !strlen(input_name)) {
1557 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
1558 input_name = "-";
1559 else
1560 input_name = "perf.data";
1561 }
1562
1563 perf_stat.file.path = input_name;
1564 perf_stat.file.mode = PERF_DATA_MODE_READ;
1565
1566 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
1567 if (session == NULL)
1568 return -1;
1569
1570 perf_stat.session = session;
1571 stat_config.output = stderr;
1572 evsel_list = session->evlist;
1573
1574 ret = perf_session__process_events(session);
1575 if (ret)
1576 return ret;
1577
1578 perf_session__delete(session);
1579 return 0;
1580}
1581
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001582int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02001583{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001584 const char * const stat_usage[] = {
1585 "perf stat [<options>] [<command>]",
1586 NULL
1587 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09001588 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001589 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02001590 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001591 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01001592 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02001593
Stephane Eranian5af52b52010-05-18 15:00:01 +02001594 setlocale(LC_ALL, "");
1595
Namhyung Kim334fe7a2013-03-11 16:43:12 +09001596 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001597 if (evsel_list == NULL)
1598 return -ENOMEM;
1599
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001600 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
1601 (const char **) stat_usage,
1602 PARSE_OPT_STOP_AT_NON_OPTION);
1603
1604 if (argc && !strncmp(argv[0], "rec", 3)) {
1605 argc = __cmd_record(argc, argv);
1606 if (argc < 0)
1607 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01001608 } else if (argc && !strncmp(argv[0], "rep", 3))
1609 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001610
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001611 interval = stat_config.interval;
1612
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001613 /*
1614 * For record command the -o is already taken care of.
1615 */
1616 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02001617 output = NULL;
1618
Jim Cromie56f3bae2011-09-07 17:14:00 -06001619 if (output_name && output_fd) {
1620 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001621 parse_options_usage(stat_usage, stat_options, "o", 1);
1622 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001623 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06001624 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001625
1626 if (output_fd < 0) {
1627 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001628 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001629 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001630 }
1631
Stephane Eranian4aa90152011-08-15 22:22:33 +02001632 if (!output) {
1633 struct timespec tm;
1634 mode = append_file ? "a" : "w";
1635
1636 output = fopen(output_name, mode);
1637 if (!output) {
1638 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06001639 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001640 }
1641 clock_gettime(CLOCK_REALTIME, &tm);
1642 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001643 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06001644 mode = append_file ? "a" : "w";
1645 output = fdopen(output_fd, mode);
1646 if (!output) {
1647 perror("Failed opening logfd");
1648 return -errno;
1649 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001650 }
1651
Jiri Olsa58215222015-07-21 14:31:24 +02001652 stat_config.output = output;
1653
Jim Cromied4ffd042011-09-07 17:14:03 -06001654 if (csv_sep) {
Stephane Eraniand7470b62010-12-01 18:49:05 +02001655 csv_output = true;
Jim Cromied4ffd042011-09-07 17:14:03 -06001656 if (!strcmp(csv_sep, "\\t"))
1657 csv_sep = "\t";
1658 } else
Stephane Eraniand7470b62010-12-01 18:49:05 +02001659 csv_sep = DEFAULT_SEPARATOR;
1660
1661 /*
1662 * let the spreadsheet do the pretty-printing
1663 */
1664 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06001665 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02001666 if (big_num_opt == 1) {
1667 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001668 parse_options_usage(stat_usage, stat_options, "B", 1);
1669 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001670 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02001671 } else /* Nope, so disable big number formatting */
1672 big_num = false;
1673 } else if (big_num_opt == 0) /* User passed --no-big-num */
1674 big_num = false;
1675
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001676 if (!argc && target__none(&target))
Jiri Olsae0547312015-11-05 15:40:45 +01001677 usage_with_options(stat_usage, stat_options);
David Ahernac3063b2013-09-30 07:37:37 -06001678
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001679 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09001680 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001681 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001682 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001683 } else if (run_count == 0) {
1684 forever = true;
1685 run_count = 1;
1686 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001687
Jiri Olsa421a50f2015-07-21 14:31:22 +02001688 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001689 fprintf(stderr, "The --per-thread option is only available "
1690 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001691 parse_options_usage(NULL, stat_options, "p", 1);
1692 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001693 goto out;
1694 }
1695
1696 /*
1697 * no_aggr, cgroup are for system-wide only
1698 * --per-thread is aggregated per thread, we dont mix it with cpu mode
1699 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02001700 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
1701 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001702 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02001703 fprintf(stderr, "both cgroup and no-aggregation "
1704 "modes only available in system-wide mode\n");
1705
Jiri Olsae0547312015-11-05 15:40:45 +01001706 parse_options_usage(stat_usage, stat_options, "G", 1);
1707 parse_options_usage(NULL, stat_options, "A", 1);
1708 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001709 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001710 }
1711
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001712 if (add_default_attributes())
1713 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001714
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001715 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02001716
Namhyung Kim77a6f012012-05-07 14:09:04 +09001717 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001718 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09001719 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001720 parse_options_usage(stat_usage, stat_options, "p", 1);
1721 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001722 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09001723 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01001724 parse_options_usage(stat_usage, stat_options, "C", 1);
1725 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001726 }
1727 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02001728 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001729
1730 /*
1731 * Initialize thread_map with comm names,
1732 * so we could print it out on output.
1733 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02001734 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001735 thread_map__read_comms(evsel_list->threads);
1736
Stephane Eranian13370a92013-01-29 12:47:44 +01001737 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04001738 if (interval < 10) {
1739 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001740 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04001741 goto out;
1742 } else
1743 pr_warning("print interval < 100ms. "
1744 "The overhead percentage could be high in some cases. "
1745 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01001746 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001747
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001748 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03001749 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001750
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001751 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03001752 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001753
Ingo Molnar58d7e992009-05-15 11:03:23 +02001754 /*
1755 * We dont want to block the signals - that would cause
1756 * child tasks to inherit that and Ctrl-C would not work.
1757 * What we want is for Ctrl-C to work in the exec()-ed
1758 * task, but being ignored by perf stat itself:
1759 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001760 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001761 if (!forever)
1762 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01001763 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02001764 signal(SIGALRM, skip_signal);
1765 signal(SIGABRT, skip_signal);
1766
Ingo Molnar42202dd2009-06-13 14:57:28 +02001767 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001768 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Ingo Molnar42202dd2009-06-13 14:57:28 +02001769 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001770 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
1771 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02001772
Ingo Molnar42202dd2009-06-13 14:57:28 +02001773 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001774 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001775 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02001776 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001777 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02001778 }
1779
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001780 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001781 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001782
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001783 if (STAT_RECORD) {
1784 /*
1785 * We synthesize the kernel mmap record just so that older tools
1786 * don't emit warnings about not being able to resolve symbols
1787 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
1788 * a saner message about no samples being in the perf.data file.
1789 *
1790 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01001791 * in header.c at the moment 'perf stat record' gets introduced, which
1792 * is not really needed once we start adding the stat specific PERF_RECORD_
1793 * records, but the need to suppress the kptr_restrict messages in older
1794 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001795 */
1796 int fd = perf_data_file__fd(&perf_stat.file);
1797 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
1798 process_synthesized_event,
1799 &perf_stat.session->machines.host);
1800 if (err) {
1801 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
1802 "older tools may produce warnings about this file\n.");
1803 }
1804
Jiri Olsa7aad0c32015-11-05 15:40:52 +01001805 if (!interval) {
1806 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
1807 pr_err("failed to write stat round event\n");
1808 }
1809
Jiri Olsa664c98d2015-11-05 15:40:50 +01001810 if (!perf_stat.file.is_pipe) {
1811 perf_stat.session->header.data_size += perf_stat.bytes_written;
1812 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
1813 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001814
1815 perf_session__delete(perf_stat.session);
1816 }
1817
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001818 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001819 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02001820out:
1821 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001822 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001823}