blob: 3ccf5a9dab335be1608c3e652909ae7ad841d161 [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 Weisbecker8f28827a2009-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 Weisbecker8f28827a2009-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 Olsa1975d362015-11-05 15:40:56 +0100138 bool maps_allocated;
139 struct cpu_map *cpus;
140 struct thread_map *threads;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100141};
142
143static struct perf_stat perf_stat;
144#define STAT_RECORD perf_stat.record
145
Liming Wang60666c62009-12-31 16:05:50 +0800146static volatile int done = 0;
147
Jiri Olsa421a50f2015-07-21 14:31:22 +0200148static struct perf_stat_config stat_config = {
149 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200150 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200151};
152
Stephane Eranian13370a92013-01-29 12:47:44 +0100153static inline void diff_timespec(struct timespec *r, struct timespec *a,
154 struct timespec *b)
155{
156 r->tv_sec = a->tv_sec - b->tv_sec;
157 if (a->tv_nsec < b->tv_nsec) {
158 r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
159 r->tv_sec--;
160 } else {
161 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
162 }
163}
164
Jiri Olsa254ecbc2015-06-26 11:29:13 +0200165static void perf_stat__reset_stats(void)
166{
167 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200168 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200169}
170
Jiri Olsacac21422012-11-12 18:34:00 +0100171static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200172{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200173 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200174
Jiri Olsa711a5722015-07-21 14:31:23 +0200175 if (stat_config.scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200176 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
177 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200178
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200179 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300180
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100181 /*
182 * Some events get initialized with sample_(period/type) set,
183 * like tracepoints. Clear it up for counting.
184 */
185 attr->sample_period = 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100186 /*
187 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
188 * while avoiding that older tools show confusing messages.
189 */
190 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100191
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100192 /*
193 * Disabling all counters initially, they will be enabled
194 * either manually by us or by kernel via enable_on_exec
195 * set later.
196 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100197 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100198 attr->disabled = 1;
199
Jiri Olsac8280ce2015-12-03 10:06:45 +0100200 /*
201 * In case of initial_delay we enable tracee
202 * events manually.
203 */
204 if (target__none(&target) && !initial_delay)
205 attr->enable_on_exec = 1;
206 }
207
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300208 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300209 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200210
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300211 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200212}
213
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200214/*
215 * Does the counter have nsecs as a unit?
216 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200217static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200218{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200219 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
220 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200221 return 1;
222
223 return 0;
224}
225
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100226static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100227 union perf_event *event,
228 struct perf_sample *sample __maybe_unused,
229 struct machine *machine __maybe_unused)
230{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100231 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
232 pr_err("failed to write perf data, error: %m\n");
233 return -1;
234 }
235
236 perf_stat.bytes_written += event->header.size;
237 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100238}
239
Jiri Olsa1975d362015-11-05 15:40:56 +0100240static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100241{
Jiri Olsa1975d362015-11-05 15:40:56 +0100242 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100243 process_synthesized_event,
244 NULL);
245}
246
247#define WRITE_STAT_ROUND_EVENT(time, interval) \
248 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
249
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100250#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
251
252static int
253perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
254 struct perf_counts_values *count)
255{
256 struct perf_sample_id *sid = SID(counter, cpu, thread);
257
258 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
259 process_synthesized_event, NULL);
260}
261
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200262/*
263 * Read out the results of a single counter:
264 * do not aggregate counts across CPUs in system-wide mode
265 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200266static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200267{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100268 int nthreads = thread_map__nr(evsel_list->threads);
269 int ncpus = perf_evsel__nr_cpus(counter);
270 int cpu, thread;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200271
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000272 if (!counter->supported)
273 return -ENOENT;
274
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100275 if (counter->system_wide)
276 nthreads = 1;
277
278 for (thread = 0; thread < nthreads; thread++) {
279 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200280 struct perf_counts_values *count;
281
282 count = perf_counts(counter->counts, cpu, thread);
283 if (perf_evsel__read(counter, cpu, thread, count))
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100284 return -1;
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100285
286 if (STAT_RECORD) {
287 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
288 pr_err("failed to write stat event\n");
289 return -1;
290 }
291 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100292 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200293 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200294
295 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200296}
297
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200298static void read_counters(bool close_counters)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200299{
300 struct perf_evsel *counter;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200301
302 evlist__for_each(evsel_list, counter) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200303 if (read_counter(counter))
Andi Kleen245bad82015-09-01 15:52:46 -0700304 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200305
Jiri Olsaf80010e2015-07-21 14:31:27 +0200306 if (perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200307 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200308
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200309 if (close_counters) {
Jiri Olsa106a94a2015-06-26 11:29:19 +0200310 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
311 thread_map__nr(evsel_list->threads));
312 }
313 }
314}
315
Jiri Olsaba411a92015-06-26 11:29:24 +0200316static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100317{
Stephane Eranian13370a92013-01-29 12:47:44 +0100318 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100319
Jiri Olsa106a94a2015-06-26 11:29:19 +0200320 read_counters(false);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100321
Stephane Eranian13370a92013-01-29 12:47:44 +0100322 clock_gettime(CLOCK_MONOTONIC, &ts);
323 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100324
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100325 if (STAT_RECORD) {
326 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSECS_PER_SEC + rs.tv_nsec, INTERVAL))
327 pr_err("failed to write stat round event\n");
328 }
329
Jiri Olsad4f63a42015-06-26 11:29:26 +0200330 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100331}
332
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100333static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700334{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100335 if (initial_delay)
Andi Kleen41191682013-08-02 17:41:11 -0700336 usleep(initial_delay * 1000);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100337
338 /*
339 * We need to enable counters only if:
340 * - we don't have tracee (attaching to task or cpu)
341 * - we have initial delay configured
342 */
343 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100344 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700345}
346
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300347static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300348
349/*
350 * perf_evlist__prepare_workload will send a SIGUSR1
351 * if the fork fails, since we asked by setting its
352 * want_signal to true.
353 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300354static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
355 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300356{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300357 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300358}
359
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100360static bool has_unit(struct perf_evsel *counter)
361{
362 return counter->unit && *counter->unit;
363}
364
365static bool has_scale(struct perf_evsel *counter)
366{
367 return counter->scale != 1;
368}
369
Jiri Olsa664c98d2015-11-05 15:40:50 +0100370static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100371{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100372 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100373 int err;
374
Jiri Olsa664c98d2015-11-05 15:40:50 +0100375 if (is_pipe) {
376 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
377 process_synthesized_event);
378 if (err < 0) {
379 pr_err("Couldn't synthesize attrs.\n");
380 return err;
381 }
382 }
383
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100384 /*
385 * Synthesize other events stuff not carried within
386 * attr event - unit, scale, name
387 */
388 evlist__for_each(evsel_list, counter) {
389 if (!counter->supported)
390 continue;
391
392 /*
393 * Synthesize unit and scale only if it's defined.
394 */
395 if (has_unit(counter)) {
396 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
397 if (err < 0) {
398 pr_err("Couldn't synthesize evsel unit.\n");
399 return err;
400 }
401 }
402
403 if (has_scale(counter)) {
404 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
405 if (err < 0) {
406 pr_err("Couldn't synthesize evsel scale.\n");
407 return err;
408 }
409 }
410
411 if (counter->own_cpus) {
412 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
413 if (err < 0) {
414 pr_err("Couldn't synthesize evsel scale.\n");
415 return err;
416 }
417 }
418
419 /*
420 * Name is needed only for pipe output,
421 * perf.data carries event names.
422 */
423 if (is_pipe) {
424 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
425 if (err < 0) {
426 pr_err("Couldn't synthesize evsel name.\n");
427 return err;
428 }
429 }
430 }
431
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100432 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
433 process_synthesized_event,
434 NULL);
435 if (err < 0) {
436 pr_err("Couldn't synthesize thread map.\n");
437 return err;
438 }
439
440 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
441 process_synthesized_event, NULL);
442 if (err < 0) {
443 pr_err("Couldn't synthesize thread map.\n");
444 return err;
445 }
446
447 err = perf_event__synthesize_stat_config(NULL, &stat_config,
448 process_synthesized_event, NULL);
449 if (err < 0) {
450 pr_err("Couldn't synthesize config.\n");
451 return err;
452 }
453
454 return 0;
455}
456
Jiri Olsa2af46462015-11-05 15:40:49 +0100457#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
458
459static int __store_counter_ids(struct perf_evsel *counter,
460 struct cpu_map *cpus,
461 struct thread_map *threads)
462{
463 int cpu, thread;
464
465 for (cpu = 0; cpu < cpus->nr; cpu++) {
466 for (thread = 0; thread < threads->nr; thread++) {
467 int fd = FD(counter, cpu, thread);
468
469 if (perf_evlist__id_add_fd(evsel_list, counter,
470 cpu, thread, fd) < 0)
471 return -1;
472 }
473 }
474
475 return 0;
476}
477
478static int store_counter_ids(struct perf_evsel *counter)
479{
480 struct cpu_map *cpus = counter->cpus;
481 struct thread_map *threads = counter->threads;
482
483 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
484 return -ENOMEM;
485
486 return __store_counter_ids(counter, cpus, threads);
487}
488
Namhyung Kimacf28922013-03-11 16:43:18 +0900489static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200490{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200491 int interval = stat_config.interval;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300492 char msg[512];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200493 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100494 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100495 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100496 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200497 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300498 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100499 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200500
Stephane Eranian13370a92013-01-29 12:47:44 +0100501 if (interval) {
502 ts.tv_sec = interval / 1000;
503 ts.tv_nsec = (interval % 1000) * 1000000;
504 } else {
505 ts.tv_sec = 1;
506 ts.tv_nsec = 0;
507 }
508
Liming Wang60666c62009-12-31 16:05:50 +0800509 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100510 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300511 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900512 perror("failed to prepare workload");
513 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800514 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900515 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000516 }
517
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200518 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300519 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200520
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300521 evlist__for_each(evsel_list, counter) {
Jiri Olsacac21422012-11-12 18:34:00 +0100522 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600523 /*
524 * PPC returns ENXIO for HW counters until 2.6.37
525 * (behavior changed with commit b0a873e).
526 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100527 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600528 errno == ENOENT || errno == EOPNOTSUPP ||
529 errno == ENXIO) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600530 if (verbose)
531 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300532 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600533 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400534
535 if ((counter->leader != counter) ||
536 !(counter->leader->nr_members > 1))
537 continue;
David Ahernc63ca0c2011-04-29 16:04:15 -0600538 }
Ingo Molnarede70292011-04-28 08:48:42 +0200539
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300540 perf_evsel__open_strerror(counter, &target,
541 errno, msg, sizeof(msg));
542 ui__error("%s\n", msg);
543
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200544 if (child_pid != -1)
545 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600546
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200547 return -1;
548 }
David Ahern2cee77c2011-05-30 08:55:59 -0600549 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100550
551 l = strlen(counter->unit);
552 if (l > unit_width)
553 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100554
555 if (STAT_RECORD && store_counter_ids(counter))
556 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300557 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200558
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300559 if (perf_evlist__apply_filters(evsel_list, &counter)) {
560 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
561 counter->filter, perf_evsel__name(counter), errno,
Masami Hiramatsu759e6122014-08-14 02:22:55 +0000562 strerror_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100563 return -1;
564 }
565
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100566 if (STAT_RECORD) {
567 int err, fd = perf_data_file__fd(&perf_stat.file);
568
Jiri Olsa664c98d2015-11-05 15:40:50 +0100569 if (is_pipe) {
570 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
571 } else {
572 err = perf_session__write_header(perf_stat.session, evsel_list,
573 fd, false);
574 }
575
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100576 if (err < 0)
577 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100578
Jiri Olsa664c98d2015-11-05 15:40:50 +0100579 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100580 if (err < 0)
581 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100582 }
583
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200584 /*
585 * Enable counters and exec the command:
586 */
587 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100588 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200589
Liming Wang60666c62009-12-31 16:05:50 +0800590 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900591 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100592 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900593
Stephane Eranian13370a92013-01-29 12:47:44 +0100594 if (interval) {
595 while (!waitpid(child_pid, &status, WNOHANG)) {
596 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200597 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100598 }
599 }
Liming Wang60666c62009-12-31 16:05:50 +0800600 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300601
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300602 if (workload_exec_errno) {
603 const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
604 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300605 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300606 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300607
Andi Kleen33e49ea2011-09-15 14:31:40 -0700608 if (WIFSIGNALED(status))
609 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800610 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100611 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100612 while (!done) {
613 nanosleep(&ts, NULL);
614 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200615 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100616 }
Liming Wang60666c62009-12-31 16:05:50 +0800617 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200618
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200619 t1 = rdclock();
620
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200621 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200622
Jiri Olsa106a94a2015-06-26 11:29:19 +0200623 read_counters(true);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200624
Ingo Molnar42202dd2009-06-13 14:57:28 +0200625 return WEXITSTATUS(status);
626}
627
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300628static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200629{
630 int ret;
631
632 if (pre_cmd) {
633 ret = system(pre_cmd);
634 if (ret)
635 return ret;
636 }
637
638 if (sync_run)
639 sync();
640
641 ret = __run_perf_stat(argc, argv);
642 if (ret)
643 return ret;
644
645 if (post_cmd) {
646 ret = system(post_cmd);
647 if (ret)
648 return ret;
649 }
650
651 return ret;
652}
653
Andi Kleend73515c2015-03-11 07:16:27 -0700654static void print_running(u64 run, u64 ena)
655{
656 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200657 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700658 csv_sep,
659 run,
660 csv_sep,
661 ena ? 100.0 * run / ena : 100.0);
662 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200663 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700664 }
665}
666
Ingo Molnarf99844c2011-04-27 05:35:39 +0200667static void print_noise_pct(double total, double avg)
668{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800669 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200670
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700671 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200672 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600673 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200674 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200675}
676
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200677static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200678{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200679 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200680
Peter Zijlstra849abde2009-09-04 18:23:38 +0200681 if (run_count == 1)
682 return;
683
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200684 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200685 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200686}
687
Stephane Eranian12c08a92013-02-14 13:57:29 +0100688static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200689{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200690 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100691 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200692 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100693 cpu_map__id_to_socket(id),
694 csv_output ? 0 : -8,
695 cpu_map__id_to_cpu(id),
696 csv_sep,
697 csv_output ? 0 : 4,
698 nr,
699 csv_sep);
700 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100701 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200702 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100703 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100704 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100705 csv_sep,
706 csv_output ? 0 : 4,
707 nr,
708 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100709 break;
710 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200711 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200712 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100713 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100714 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200715 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200716 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200717 csv_output ? 0 : 16,
718 thread_map__comm(evsel->threads, id),
719 csv_output ? 0 : -8,
720 thread_map__pid(evsel->threads, id),
721 csv_sep);
722 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100723 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200724 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100725 default:
726 break;
727 }
728}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200729
Andi Kleenda88c7f2014-09-24 13:50:46 -0700730static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100731{
Jiri Olsa58215222015-07-21 14:31:24 +0200732 FILE *output = stat_config.output;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100733 double msecs = avg / 1e6;
Stephane Eranian410136f2013-11-12 17:58:49 +0100734 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600735 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100736
Stephane Eranian410136f2013-11-12 17:58:49 +0100737 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
738 fmt_n = csv_output ? "%s" : "%-25s";
739
Andi Kleenda88c7f2014-09-24 13:50:46 -0700740 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100741
David Ahern4bbe5a62013-09-28 14:28:00 -0600742 scnprintf(name, sizeof(name), "%s%s",
743 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +0100744
745 fprintf(output, fmt_v, msecs, csv_sep);
746
747 if (csv_output)
748 fprintf(output, "%s%s", evsel->unit, csv_sep);
749 else
750 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
751
752 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200753
Stephane Eranian023695d2011-02-14 11:20:01 +0200754 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200755 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200756}
757
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200758static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
759{
Jiri Olsa58215222015-07-21 14:31:24 +0200760 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200761 double sc = evsel->scale;
762 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200763
764 if (csv_output) {
765 fmt = sc != 1.0 ? "%.2f%s" : "%.0f%s";
766 } else {
767 if (big_num)
768 fmt = sc != 1.0 ? "%'18.2f%s" : "%'18.0f%s";
769 else
770 fmt = sc != 1.0 ? "%18.2f%s" : "%18.0f%s";
771 }
772
773 aggr_printout(evsel, id, nr);
774
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200775 fprintf(output, fmt, avg, csv_sep);
776
777 if (evsel->unit)
778 fprintf(output, "%-*s%s",
779 csv_output ? 0 : unit_width,
780 evsel->unit, csv_sep);
781
782 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
783
784 if (evsel->cgrp)
785 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -0800786}
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200787
Andi Kleeneedfcb42015-11-02 17:50:21 -0800788static void printout(int id, int nr, struct perf_evsel *counter, double uval)
789{
790 int cpu = cpu_map__id_to_cpu(id);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200791
Andi Kleeneedfcb42015-11-02 17:50:21 -0800792 if (stat_config.aggr_mode == AGGR_GLOBAL)
793 cpu = 0;
794
795 if (nsec_counter(counter))
796 nsec_printout(id, nr, counter, uval);
797 else
798 abs_printout(id, nr, counter, uval);
799
800 if (!csv_output && !stat_config.interval)
801 perf_stat__print_shadow_stats(stat_config.output, counter,
802 uval, cpu,
803 stat_config.aggr_mode);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200804}
805
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100806static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100807{
Jiri Olsa58215222015-07-21 14:31:24 +0200808 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100809 struct perf_evsel *counter;
Kan Liang601083c2015-07-02 03:08:43 -0400810 int cpu, s, s2, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +0100811 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100812 u64 ena, run, val;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100813
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100814 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100815 return;
816
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100817 for (s = 0; s < aggr_map->nr; s++) {
818 id = aggr_map->map[s];
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300819 evlist__for_each(evsel_list, counter) {
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100820 val = ena = run = 0;
821 nr = 0;
822 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Kan Liang601083c2015-07-02 03:08:43 -0400823 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100824 if (s2 != id)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100825 continue;
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200826 val += perf_counts(counter->counts, cpu, 0)->val;
827 ena += perf_counts(counter->counts, cpu, 0)->ena;
828 run += perf_counts(counter->counts, cpu, 0)->run;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100829 nr++;
830 }
831 if (prefix)
832 fprintf(output, "%s", prefix);
833
834 if (run == 0 || ena == 0) {
Stephane Eranian582ec0822013-07-05 19:06:45 +0200835 aggr_printout(counter, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100836
Stephane Eranian410136f2013-11-12 17:58:49 +0100837 fprintf(output, "%*s%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100838 csv_output ? 0 : 18,
839 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100840 csv_sep);
841
842 fprintf(output, "%-*s%s",
843 csv_output ? 0 : unit_width,
844 counter->unit, csv_sep);
845
846 fprintf(output, "%*s",
847 csv_output ? 0 : -25,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100848 perf_evsel__name(counter));
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100849
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100850 if (counter->cgrp)
851 fprintf(output, "%s%s",
852 csv_sep, counter->cgrp->name);
853
Andi Kleend73515c2015-03-11 07:16:27 -0700854 print_running(run, ena);
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100855 fputc('\n', output);
856 continue;
857 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100858 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800859 printout(id, nr, counter, uval);
Andi Kleend73515c2015-03-11 07:16:27 -0700860 if (!csv_output)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100861 print_noise(counter, 1.0);
862
Andi Kleend73515c2015-03-11 07:16:27 -0700863 print_running(run, ena);
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100864 fputc('\n', output);
865 }
866 }
867}
868
Jiri Olsa32b8af82015-06-26 11:29:27 +0200869static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
870{
Jiri Olsa58215222015-07-21 14:31:24 +0200871 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200872 int nthreads = thread_map__nr(counter->threads);
873 int ncpus = cpu_map__nr(counter->cpus);
874 int cpu, thread;
875 double uval;
876
877 for (thread = 0; thread < nthreads; thread++) {
878 u64 ena = 0, run = 0, val = 0;
879
880 for (cpu = 0; cpu < ncpus; cpu++) {
881 val += perf_counts(counter->counts, cpu, thread)->val;
882 ena += perf_counts(counter->counts, cpu, thread)->ena;
883 run += perf_counts(counter->counts, cpu, thread)->run;
884 }
885
886 if (prefix)
887 fprintf(output, "%s", prefix);
888
889 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800890 printout(thread, 0, counter, uval);
Jiri Olsa32b8af82015-06-26 11:29:27 +0200891
892 if (!csv_output)
893 print_noise(counter, 1.0);
894
895 print_running(run, ena);
896 fputc('\n', output);
897 }
898}
899
Ingo Molnar42202dd2009-06-13 14:57:28 +0200900/*
901 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200902 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +0200903 */
Stephane Eranian13370a92013-01-29 12:47:44 +0100904static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200905{
Jiri Olsa58215222015-07-21 14:31:24 +0200906 FILE *output = stat_config.output;
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200907 struct perf_stat_evsel *ps = counter->priv;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200908 double avg = avg_stats(&ps->res_stats[0]);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200909 int scaled = counter->counts->scaled;
Stephane Eranian410136f2013-11-12 17:58:49 +0100910 double uval;
Andi Kleend73515c2015-03-11 07:16:27 -0700911 double avg_enabled, avg_running;
912
913 avg_enabled = avg_stats(&ps->res_stats[1]);
914 avg_running = avg_stats(&ps->res_stats[2]);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200915
Stephane Eranian13370a92013-01-29 12:47:44 +0100916 if (prefix)
917 fprintf(output, "%s", prefix);
918
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000919 if (scaled == -1 || !counter->supported) {
Stephane Eranian410136f2013-11-12 17:58:49 +0100920 fprintf(output, "%*s%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200921 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600922 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100923 csv_sep);
924 fprintf(output, "%-*s%s",
925 csv_output ? 0 : unit_width,
926 counter->unit, csv_sep);
927 fprintf(output, "%*s",
928 csv_output ? 0 : -25,
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300929 perf_evsel__name(counter));
Stephane Eranian023695d2011-02-14 11:20:01 +0200930
931 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200932 fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200933
Andi Kleend73515c2015-03-11 07:16:27 -0700934 print_running(avg_running, avg_enabled);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200935 fputc('\n', output);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200936 return;
937 }
938
Stephane Eranian410136f2013-11-12 17:58:49 +0100939 uval = avg * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800940 printout(-1, 0, counter, uval);
Peter Zijlstra849abde2009-09-04 18:23:38 +0200941
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700942 print_noise(counter, avg);
943
Andi Kleend73515c2015-03-11 07:16:27 -0700944 print_running(avg_running, avg_enabled);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200945 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200946}
947
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200948/*
949 * Print out the results of a single counter:
950 * does not use aggregated count in system-wide
951 */
Stephane Eranian13370a92013-01-29 12:47:44 +0100952static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200953{
Jiri Olsa58215222015-07-21 14:31:24 +0200954 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200955 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +0100956 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200957 int cpu;
958
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800959 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200960 val = perf_counts(counter->counts, cpu, 0)->val;
961 ena = perf_counts(counter->counts, cpu, 0)->ena;
962 run = perf_counts(counter->counts, cpu, 0)->run;
Stephane Eranian13370a92013-01-29 12:47:44 +0100963
964 if (prefix)
965 fprintf(output, "%s", prefix);
966
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200967 if (run == 0 || ena == 0) {
Stephane Eranian410136f2013-11-12 17:58:49 +0100968 fprintf(output, "CPU%*d%s%*s%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200969 csv_output ? 0 : -4,
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800970 perf_evsel__cpus(counter)->map[cpu], csv_sep,
Stephane Eraniand7470b62010-12-01 18:49:05 +0200971 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600972 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100973 csv_sep);
974
975 fprintf(output, "%-*s%s",
976 csv_output ? 0 : unit_width,
977 counter->unit, csv_sep);
978
979 fprintf(output, "%*s",
980 csv_output ? 0 : -25,
981 perf_evsel__name(counter));
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200982
Stephane Eranian023695d2011-02-14 11:20:01 +0200983 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200984 fprintf(output, "%s%s",
985 csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200986
Andi Kleend73515c2015-03-11 07:16:27 -0700987 print_running(run, ena);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200988 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200989 continue;
990 }
991
Stephane Eranian410136f2013-11-12 17:58:49 +0100992 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800993 printout(cpu, 0, counter, uval);
Andi Kleend73515c2015-03-11 07:16:27 -0700994 if (!csv_output)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200995 print_noise(counter, 1.0);
Andi Kleend73515c2015-03-11 07:16:27 -0700996 print_running(run, ena);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200997
Stephane Eranian4aa90152011-08-15 22:22:33 +0200998 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200999 }
1000}
1001
Jiri Olsad4f63a42015-06-26 11:29:26 +02001002static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001003{
Jiri Olsa58215222015-07-21 14:31:24 +02001004 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001005 static int num_print_interval;
1006
1007 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1008
1009 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001010 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001011 case AGGR_SOCKET:
1012 fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit");
1013 break;
1014 case AGGR_CORE:
1015 fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit");
1016 break;
1017 case AGGR_NONE:
1018 fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit");
1019 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001020 case AGGR_THREAD:
1021 fprintf(output, "# time comm-pid counts %*s events\n", unit_width, "unit");
1022 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001023 case AGGR_GLOBAL:
1024 default:
1025 fprintf(output, "# time counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001026 case AGGR_UNSET:
1027 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001028 }
1029 }
1030
1031 if (++num_print_interval == 25)
1032 num_print_interval = 0;
1033}
1034
1035static void print_header(int argc, const char **argv)
1036{
Jiri Olsa58215222015-07-21 14:31:24 +02001037 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001038 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001039
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001040 fflush(stdout);
1041
Stephane Eraniand7470b62010-12-01 18:49:05 +02001042 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001043 fprintf(output, "\n");
1044 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001045 if (target.system_wide)
1046 fprintf(output, "\'system wide");
1047 else if (target.cpu_list)
1048 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001049 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001050 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1051 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001052 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001053 } else if (target.pid)
1054 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001055 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001056 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001057
Stephane Eranian4aa90152011-08-15 22:22:33 +02001058 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001059 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001060 fprintf(output, " (%d runs)", run_count);
1061 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001062 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001063}
1064
1065static void print_footer(void)
1066{
Jiri Olsa58215222015-07-21 14:31:24 +02001067 FILE *output = stat_config.output;
1068
Jiri Olsad4f63a42015-06-26 11:29:26 +02001069 if (!null_run)
1070 fprintf(output, "\n");
1071 fprintf(output, " %17.9f seconds time elapsed",
1072 avg_stats(&walltime_nsecs_stats)/1e9);
1073 if (run_count > 1) {
1074 fprintf(output, " ");
1075 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1076 avg_stats(&walltime_nsecs_stats));
1077 }
1078 fprintf(output, "\n\n");
1079}
1080
1081static void print_counters(struct timespec *ts, int argc, const char **argv)
1082{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001083 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001084 struct perf_evsel *counter;
1085 char buf[64], *prefix = NULL;
1086
Jiri Olsa664c98d2015-11-05 15:40:50 +01001087 /* Do not print anything if we record to the pipe. */
1088 if (STAT_RECORD && perf_stat.file.is_pipe)
1089 return;
1090
Jiri Olsad4f63a42015-06-26 11:29:26 +02001091 if (interval)
1092 print_interval(prefix = buf, ts);
1093 else
1094 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001095
Jiri Olsa421a50f2015-07-21 14:31:22 +02001096 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001097 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001098 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001099 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001100 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001101 case AGGR_THREAD:
1102 evlist__for_each(evsel_list, counter)
1103 print_aggr_thread(counter, prefix);
1104 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001105 case AGGR_GLOBAL:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001106 evlist__for_each(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001107 print_counter_aggr(counter, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001108 break;
1109 case AGGR_NONE:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001110 evlist__for_each(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001111 print_counter(counter, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001112 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001113 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001114 default:
1115 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001116 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001117
Jiri Olsad4f63a42015-06-26 11:29:26 +02001118 if (!interval && !csv_output)
1119 print_footer();
1120
Jiri Olsa58215222015-07-21 14:31:24 +02001121 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001122}
1123
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001124static volatile int signr = -1;
1125
Ingo Molnar52425192009-05-26 09:17:18 +02001126static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001127{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001128 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001129 done = 1;
1130
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001131 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001132 /*
1133 * render child_pid harmless
1134 * won't send SIGTERM to a random
1135 * process in case of race condition
1136 * and fast PID recycling
1137 */
1138 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001139}
1140
1141static void sig_atexit(void)
1142{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001143 sigset_t set, oset;
1144
1145 /*
1146 * avoid race condition with SIGCHLD handler
1147 * in skip_signal() which is modifying child_pid
1148 * goal is to avoid send SIGTERM to a random
1149 * process
1150 */
1151 sigemptyset(&set);
1152 sigaddset(&set, SIGCHLD);
1153 sigprocmask(SIG_BLOCK, &set, &oset);
1154
Chris Wilson933da832009-10-04 01:35:01 +01001155 if (child_pid != -1)
1156 kill(child_pid, SIGTERM);
1157
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001158 sigprocmask(SIG_SETMASK, &oset, NULL);
1159
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001160 if (signr == -1)
1161 return;
1162
1163 signal(signr, SIG_DFL);
1164 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001165}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001166
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001167static int stat__set_big_num(const struct option *opt __maybe_unused,
1168 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001169{
1170 big_num_opt = unset ? 0 : 1;
1171 return 0;
1172}
1173
Jiri Olsae0547312015-11-05 15:40:45 +01001174static const struct option stat_options[] = {
1175 OPT_BOOLEAN('T', "transaction", &transaction_run,
1176 "hardware transaction statistics"),
1177 OPT_CALLBACK('e', "event", &evsel_list, "event",
1178 "event selector. use 'perf list' to list available events",
1179 parse_events_option),
1180 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1181 "event filter", parse_filter),
1182 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1183 "child tasks do not inherit counters"),
1184 OPT_STRING('p', "pid", &target.pid, "pid",
1185 "stat events on existing process id"),
1186 OPT_STRING('t', "tid", &target.tid, "tid",
1187 "stat events on existing thread id"),
1188 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1189 "system-wide collection from all CPUs"),
1190 OPT_BOOLEAN('g', "group", &group,
1191 "put the counters into a counter group"),
1192 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1193 OPT_INCR('v', "verbose", &verbose,
1194 "be more verbose (show counter open errors, etc)"),
1195 OPT_INTEGER('r', "repeat", &run_count,
1196 "repeat command and print average + stddev (max: 100, forever: 0)"),
1197 OPT_BOOLEAN('n', "null", &null_run,
1198 "null run - dont start any counters"),
1199 OPT_INCR('d', "detailed", &detailed_run,
1200 "detailed run - start a lot of events"),
1201 OPT_BOOLEAN('S', "sync", &sync_run,
1202 "call sync() before starting a run"),
1203 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1204 "print large numbers with thousands\' separators",
1205 stat__set_big_num),
1206 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1207 "list of cpus to monitor in system-wide"),
1208 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1209 "disable CPU count aggregation", AGGR_NONE),
1210 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1211 "print counts with custom separator"),
1212 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1213 "monitor event in cgroup name only", parse_cgroups),
1214 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1215 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1216 OPT_INTEGER(0, "log-fd", &output_fd,
1217 "log output to fd, instead of stderr"),
1218 OPT_STRING(0, "pre", &pre_cmd, "command",
1219 "command to run prior to the measured command"),
1220 OPT_STRING(0, "post", &post_cmd, "command",
1221 "command to run after to the measured command"),
1222 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1223 "print counts at regular interval in ms (>= 10)"),
1224 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1225 "aggregate counts per processor socket", AGGR_SOCKET),
1226 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1227 "aggregate counts per physical processor core", AGGR_CORE),
1228 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1229 "aggregate counts per thread", AGGR_THREAD),
1230 OPT_UINTEGER('D', "delay", &initial_delay,
1231 "ms to wait before starting measurement after program start"),
1232 OPT_END()
1233};
1234
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001235static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1236{
1237 return cpu_map__get_socket(map, cpu, NULL);
1238}
1239
1240static int perf_stat__get_core(struct cpu_map *map, int cpu)
1241{
1242 return cpu_map__get_core(map, cpu, NULL);
1243}
1244
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001245static int cpu_map__get_max(struct cpu_map *map)
1246{
1247 int i, max = -1;
1248
1249 for (i = 0; i < map->nr; i++) {
1250 if (map->map[i] > max)
1251 max = map->map[i];
1252 }
1253
1254 return max;
1255}
1256
1257static struct cpu_map *cpus_aggr_map;
1258
1259static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1260{
1261 int cpu;
1262
1263 if (idx >= map->nr)
1264 return -1;
1265
1266 cpu = map->map[idx];
1267
1268 if (cpus_aggr_map->map[cpu] == -1)
1269 cpus_aggr_map->map[cpu] = get_id(map, idx);
1270
1271 return cpus_aggr_map->map[cpu];
1272}
1273
1274static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1275{
1276 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1277}
1278
1279static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1280{
1281 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1282}
1283
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001284static int perf_stat_init_aggr_mode(void)
1285{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001286 int nr;
1287
Jiri Olsa421a50f2015-07-21 14:31:22 +02001288 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001289 case AGGR_SOCKET:
1290 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1291 perror("cannot build socket map");
1292 return -1;
1293 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001294 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001295 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001296 case AGGR_CORE:
1297 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1298 perror("cannot build core map");
1299 return -1;
1300 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001301 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001302 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001303 case AGGR_NONE:
1304 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001305 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001306 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001307 default:
1308 break;
1309 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001310
1311 /*
1312 * The evsel_list->cpus is the base we operate on,
1313 * taking the highest cpu number to be the size of
1314 * the aggregation translate cpumap.
1315 */
1316 nr = cpu_map__get_max(evsel_list->cpus);
1317 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1318 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001319}
1320
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001321static void perf_stat__exit_aggr_mode(void)
1322{
1323 cpu_map__put(aggr_map);
1324 cpu_map__put(cpus_aggr_map);
1325 aggr_map = NULL;
1326 cpus_aggr_map = NULL;
1327}
1328
Jiri Olsa68d702f2015-11-05 15:40:58 +01001329static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1330{
1331 int cpu;
1332
1333 if (idx > map->nr)
1334 return -1;
1335
1336 cpu = map->map[idx];
1337
1338 if (cpu >= env->nr_cpus_online)
1339 return -1;
1340
1341 return cpu;
1342}
1343
1344static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1345{
1346 struct perf_env *env = data;
1347 int cpu = perf_env__get_cpu(env, map, idx);
1348
1349 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1350}
1351
1352static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1353{
1354 struct perf_env *env = data;
1355 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1356
1357 if (cpu != -1) {
1358 int socket_id = env->cpu[cpu].socket_id;
1359
1360 /*
1361 * Encode socket in upper 16 bits
1362 * core_id is relative to socket, and
1363 * we need a global id. So we combine
1364 * socket + core id.
1365 */
1366 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1367 }
1368
1369 return core;
1370}
1371
1372static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1373 struct cpu_map **sockp)
1374{
1375 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1376}
1377
1378static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1379 struct cpu_map **corep)
1380{
1381 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1382}
1383
1384static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1385{
1386 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1387}
1388
1389static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1390{
1391 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1392}
1393
1394static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1395{
1396 struct perf_env *env = &st->session->header.env;
1397
1398 switch (stat_config.aggr_mode) {
1399 case AGGR_SOCKET:
1400 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1401 perror("cannot build socket map");
1402 return -1;
1403 }
1404 aggr_get_id = perf_stat__get_socket_file;
1405 break;
1406 case AGGR_CORE:
1407 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1408 perror("cannot build core map");
1409 return -1;
1410 }
1411 aggr_get_id = perf_stat__get_core_file;
1412 break;
1413 case AGGR_NONE:
1414 case AGGR_GLOBAL:
1415 case AGGR_THREAD:
1416 case AGGR_UNSET:
1417 default:
1418 break;
1419 }
1420
1421 return 0;
1422}
1423
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001424/*
1425 * Add default attributes, if there were no attributes specified or
1426 * if -d/--detailed, -d -d or -d -d -d is used:
1427 */
1428static int add_default_attributes(void)
1429{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001430 struct perf_event_attr default_attrs[] = {
1431
1432 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1433 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1434 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1435 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1436
1437 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
1438 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
1439 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
1440 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1441 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1442 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1443
1444};
1445
1446/*
1447 * Detailed stats (-d), covering the L1 and last level data caches:
1448 */
1449 struct perf_event_attr detailed_attrs[] = {
1450
1451 { .type = PERF_TYPE_HW_CACHE,
1452 .config =
1453 PERF_COUNT_HW_CACHE_L1D << 0 |
1454 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1455 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1456
1457 { .type = PERF_TYPE_HW_CACHE,
1458 .config =
1459 PERF_COUNT_HW_CACHE_L1D << 0 |
1460 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1461 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1462
1463 { .type = PERF_TYPE_HW_CACHE,
1464 .config =
1465 PERF_COUNT_HW_CACHE_LL << 0 |
1466 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1467 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1468
1469 { .type = PERF_TYPE_HW_CACHE,
1470 .config =
1471 PERF_COUNT_HW_CACHE_LL << 0 |
1472 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1473 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1474};
1475
1476/*
1477 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1478 */
1479 struct perf_event_attr very_detailed_attrs[] = {
1480
1481 { .type = PERF_TYPE_HW_CACHE,
1482 .config =
1483 PERF_COUNT_HW_CACHE_L1I << 0 |
1484 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1485 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1486
1487 { .type = PERF_TYPE_HW_CACHE,
1488 .config =
1489 PERF_COUNT_HW_CACHE_L1I << 0 |
1490 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1491 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1492
1493 { .type = PERF_TYPE_HW_CACHE,
1494 .config =
1495 PERF_COUNT_HW_CACHE_DTLB << 0 |
1496 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1497 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1498
1499 { .type = PERF_TYPE_HW_CACHE,
1500 .config =
1501 PERF_COUNT_HW_CACHE_DTLB << 0 |
1502 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1503 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1504
1505 { .type = PERF_TYPE_HW_CACHE,
1506 .config =
1507 PERF_COUNT_HW_CACHE_ITLB << 0 |
1508 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1509 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1510
1511 { .type = PERF_TYPE_HW_CACHE,
1512 .config =
1513 PERF_COUNT_HW_CACHE_ITLB << 0 |
1514 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1515 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1516
1517};
1518
1519/*
1520 * Very, very detailed stats (-d -d -d), adding prefetch events:
1521 */
1522 struct perf_event_attr very_very_detailed_attrs[] = {
1523
1524 { .type = PERF_TYPE_HW_CACHE,
1525 .config =
1526 PERF_COUNT_HW_CACHE_L1D << 0 |
1527 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1528 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1529
1530 { .type = PERF_TYPE_HW_CACHE,
1531 .config =
1532 PERF_COUNT_HW_CACHE_L1D << 0 |
1533 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1534 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1535};
1536
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001537 /* Set attrs if no event is selected and !null_run: */
1538 if (null_run)
1539 return 0;
1540
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001541 if (transaction_run) {
1542 int err;
1543 if (pmu_have_event("cpu", "cycles-ct") &&
1544 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02001545 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001546 else
Jiri Olsaa4547422015-06-03 16:25:53 +02001547 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
1548 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001549 fprintf(stderr, "Cannot set up transaction events\n");
1550 return -1;
1551 }
1552 return 0;
1553 }
1554
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001555 if (!evsel_list->nr_entries) {
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001556 if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001557 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001558 }
1559
1560 /* Detailed events get appended to the event list: */
1561
1562 if (detailed_run < 1)
1563 return 0;
1564
1565 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001566 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001567 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001568
1569 if (detailed_run < 2)
1570 return 0;
1571
1572 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001573 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001574 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001575
1576 if (detailed_run < 3)
1577 return 0;
1578
1579 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001580 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001581}
1582
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001583static const char * const recort_usage[] = {
1584 "perf stat record [<options>]",
1585 NULL,
1586};
1587
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001588static void init_features(struct perf_session *session)
1589{
1590 int feat;
1591
1592 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
1593 perf_header__set_feat(&session->header, feat);
1594
1595 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
1596 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
1597 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1598 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1599}
1600
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001601static int __cmd_record(int argc, const char **argv)
1602{
1603 struct perf_session *session;
1604 struct perf_data_file *file = &perf_stat.file;
1605
1606 argc = parse_options(argc, argv, stat_options, record_usage,
1607 PARSE_OPT_STOP_AT_NON_OPTION);
1608
1609 if (output_name)
1610 file->path = output_name;
1611
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01001612 if (run_count != 1 || forever) {
1613 pr_err("Cannot use -r option with perf stat record.\n");
1614 return -1;
1615 }
1616
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001617 session = perf_session__new(file, false, NULL);
1618 if (session == NULL) {
1619 pr_err("Perf session creation failed.\n");
1620 return -1;
1621 }
1622
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001623 init_features(session);
1624
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001625 session->evlist = evsel_list;
1626 perf_stat.session = session;
1627 perf_stat.record = true;
1628 return argc;
1629}
1630
Jiri Olsaa56f9392015-11-05 15:40:59 +01001631static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1632 union perf_event *event,
1633 struct perf_session *session)
1634{
1635 struct stat_round_event *round = &event->stat_round;
1636 struct perf_evsel *counter;
1637 struct timespec tsh, *ts = NULL;
1638 const char **argv = session->header.env.cmdline_argv;
1639 int argc = session->header.env.nr_cmdline;
1640
1641 evlist__for_each(evsel_list, counter)
1642 perf_stat_process_counter(&stat_config, counter);
1643
1644 if (round->type == PERF_STAT_ROUND_TYPE__FINAL)
1645 update_stats(&walltime_nsecs_stats, round->time);
1646
1647 if (stat_config.interval && round->time) {
1648 tsh.tv_sec = round->time / NSECS_PER_SEC;
1649 tsh.tv_nsec = round->time % NSECS_PER_SEC;
1650 ts = &tsh;
1651 }
1652
1653 print_counters(ts, argc, argv);
1654 return 0;
1655}
1656
Jiri Olsa62ba18b2015-11-05 15:40:57 +01001657static
1658int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1659 union perf_event *event,
1660 struct perf_session *session __maybe_unused)
1661{
Jiri Olsa68d702f2015-11-05 15:40:58 +01001662 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
1663
Jiri Olsa62ba18b2015-11-05 15:40:57 +01001664 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01001665
1666 if (perf_stat.file.is_pipe)
1667 perf_stat_init_aggr_mode();
1668 else
1669 perf_stat_init_aggr_mode_file(st);
1670
Jiri Olsa62ba18b2015-11-05 15:40:57 +01001671 return 0;
1672}
1673
Jiri Olsa1975d362015-11-05 15:40:56 +01001674static int set_maps(struct perf_stat *st)
1675{
1676 if (!st->cpus || !st->threads)
1677 return 0;
1678
1679 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
1680 return -EINVAL;
1681
1682 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
1683
1684 if (perf_evlist__alloc_stats(evsel_list, true))
1685 return -ENOMEM;
1686
1687 st->maps_allocated = true;
1688 return 0;
1689}
1690
1691static
1692int process_thread_map_event(struct perf_tool *tool __maybe_unused,
1693 union perf_event *event,
1694 struct perf_session *session __maybe_unused)
1695{
1696 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
1697
1698 if (st->threads) {
1699 pr_warning("Extra thread map event, ignoring.\n");
1700 return 0;
1701 }
1702
1703 st->threads = thread_map__new_event(&event->thread_map);
1704 if (!st->threads)
1705 return -ENOMEM;
1706
1707 return set_maps(st);
1708}
1709
1710static
1711int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
1712 union perf_event *event,
1713 struct perf_session *session __maybe_unused)
1714{
1715 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
1716 struct cpu_map *cpus;
1717
1718 if (st->cpus) {
1719 pr_warning("Extra cpu map event, ignoring.\n");
1720 return 0;
1721 }
1722
1723 cpus = cpu_map__new_data(&event->cpu_map.data);
1724 if (!cpus)
1725 return -ENOMEM;
1726
1727 st->cpus = cpus;
1728 return set_maps(st);
1729}
1730
Jiri Olsaba6039b62015-11-05 15:40:55 +01001731static const char * const report_usage[] = {
1732 "perf stat report [<options>]",
1733 NULL,
1734};
1735
1736static struct perf_stat perf_stat = {
1737 .tool = {
1738 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01001739 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01001740 .thread_map = process_thread_map_event,
1741 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01001742 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01001743 .stat = perf_event__process_stat_event,
1744 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01001745 },
1746};
1747
1748static int __cmd_report(int argc, const char **argv)
1749{
1750 struct perf_session *session;
1751 const struct option options[] = {
1752 OPT_STRING('i', "input", &input_name, "file", "input file name"),
1753 OPT_END()
1754 };
1755 struct stat st;
1756 int ret;
1757
1758 argc = parse_options(argc, argv, options, report_usage, 0);
1759
1760 if (!input_name || !strlen(input_name)) {
1761 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
1762 input_name = "-";
1763 else
1764 input_name = "perf.data";
1765 }
1766
1767 perf_stat.file.path = input_name;
1768 perf_stat.file.mode = PERF_DATA_MODE_READ;
1769
1770 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
1771 if (session == NULL)
1772 return -1;
1773
1774 perf_stat.session = session;
1775 stat_config.output = stderr;
1776 evsel_list = session->evlist;
1777
1778 ret = perf_session__process_events(session);
1779 if (ret)
1780 return ret;
1781
1782 perf_session__delete(session);
1783 return 0;
1784}
1785
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001786int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02001787{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001788 const char * const stat_usage[] = {
1789 "perf stat [<options>] [<command>]",
1790 NULL
1791 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09001792 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001793 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02001794 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001795 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01001796 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02001797
Stephane Eranian5af52b52010-05-18 15:00:01 +02001798 setlocale(LC_ALL, "");
1799
Namhyung Kim334fe7a2013-03-11 16:43:12 +09001800 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001801 if (evsel_list == NULL)
1802 return -ENOMEM;
1803
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001804 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
1805 (const char **) stat_usage,
1806 PARSE_OPT_STOP_AT_NON_OPTION);
1807
Jiri Olsa6edb78a2015-11-05 15:41:01 +01001808 if (csv_sep) {
1809 csv_output = true;
1810 if (!strcmp(csv_sep, "\\t"))
1811 csv_sep = "\t";
1812 } else
1813 csv_sep = DEFAULT_SEPARATOR;
1814
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001815 if (argc && !strncmp(argv[0], "rec", 3)) {
1816 argc = __cmd_record(argc, argv);
1817 if (argc < 0)
1818 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01001819 } else if (argc && !strncmp(argv[0], "rep", 3))
1820 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001821
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001822 interval = stat_config.interval;
1823
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001824 /*
1825 * For record command the -o is already taken care of.
1826 */
1827 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02001828 output = NULL;
1829
Jim Cromie56f3bae2011-09-07 17:14:00 -06001830 if (output_name && output_fd) {
1831 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001832 parse_options_usage(stat_usage, stat_options, "o", 1);
1833 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001834 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06001835 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001836
1837 if (output_fd < 0) {
1838 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001839 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001840 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001841 }
1842
Stephane Eranian4aa90152011-08-15 22:22:33 +02001843 if (!output) {
1844 struct timespec tm;
1845 mode = append_file ? "a" : "w";
1846
1847 output = fopen(output_name, mode);
1848 if (!output) {
1849 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06001850 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001851 }
1852 clock_gettime(CLOCK_REALTIME, &tm);
1853 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001854 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06001855 mode = append_file ? "a" : "w";
1856 output = fdopen(output_fd, mode);
1857 if (!output) {
1858 perror("Failed opening logfd");
1859 return -errno;
1860 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001861 }
1862
Jiri Olsa58215222015-07-21 14:31:24 +02001863 stat_config.output = output;
1864
Stephane Eraniand7470b62010-12-01 18:49:05 +02001865 /*
1866 * let the spreadsheet do the pretty-printing
1867 */
1868 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06001869 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02001870 if (big_num_opt == 1) {
1871 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001872 parse_options_usage(stat_usage, stat_options, "B", 1);
1873 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001874 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02001875 } else /* Nope, so disable big number formatting */
1876 big_num = false;
1877 } else if (big_num_opt == 0) /* User passed --no-big-num */
1878 big_num = false;
1879
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001880 if (!argc && target__none(&target))
Jiri Olsae0547312015-11-05 15:40:45 +01001881 usage_with_options(stat_usage, stat_options);
David Ahernac3063b2013-09-30 07:37:37 -06001882
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001883 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09001884 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001885 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001886 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001887 } else if (run_count == 0) {
1888 forever = true;
1889 run_count = 1;
1890 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001891
Jiri Olsa421a50f2015-07-21 14:31:22 +02001892 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001893 fprintf(stderr, "The --per-thread option is only available "
1894 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001895 parse_options_usage(NULL, stat_options, "p", 1);
1896 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001897 goto out;
1898 }
1899
1900 /*
1901 * no_aggr, cgroup are for system-wide only
1902 * --per-thread is aggregated per thread, we dont mix it with cpu mode
1903 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02001904 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
1905 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001906 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02001907 fprintf(stderr, "both cgroup and no-aggregation "
1908 "modes only available in system-wide mode\n");
1909
Jiri Olsae0547312015-11-05 15:40:45 +01001910 parse_options_usage(stat_usage, stat_options, "G", 1);
1911 parse_options_usage(NULL, stat_options, "A", 1);
1912 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001913 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001914 }
1915
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001916 if (add_default_attributes())
1917 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001918
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001919 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02001920
Namhyung Kim77a6f012012-05-07 14:09:04 +09001921 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001922 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09001923 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001924 parse_options_usage(stat_usage, stat_options, "p", 1);
1925 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001926 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09001927 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01001928 parse_options_usage(stat_usage, stat_options, "C", 1);
1929 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001930 }
1931 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02001932 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001933
1934 /*
1935 * Initialize thread_map with comm names,
1936 * so we could print it out on output.
1937 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02001938 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001939 thread_map__read_comms(evsel_list->threads);
1940
Stephane Eranian13370a92013-01-29 12:47:44 +01001941 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04001942 if (interval < 10) {
1943 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001944 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04001945 goto out;
1946 } else
1947 pr_warning("print interval < 100ms. "
1948 "The overhead percentage could be high in some cases. "
1949 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01001950 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001951
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001952 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03001953 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001954
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001955 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03001956 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001957
Ingo Molnar58d7e992009-05-15 11:03:23 +02001958 /*
1959 * We dont want to block the signals - that would cause
1960 * child tasks to inherit that and Ctrl-C would not work.
1961 * What we want is for Ctrl-C to work in the exec()-ed
1962 * task, but being ignored by perf stat itself:
1963 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001964 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001965 if (!forever)
1966 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01001967 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02001968 signal(SIGALRM, skip_signal);
1969 signal(SIGABRT, skip_signal);
1970
Ingo Molnar42202dd2009-06-13 14:57:28 +02001971 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001972 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Ingo Molnar42202dd2009-06-13 14:57:28 +02001973 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001974 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
1975 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02001976
Ingo Molnar42202dd2009-06-13 14:57:28 +02001977 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001978 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001979 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc2015-06-26 11:29:13 +02001980 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001981 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02001982 }
1983
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001984 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001985 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001986
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001987 if (STAT_RECORD) {
1988 /*
1989 * We synthesize the kernel mmap record just so that older tools
1990 * don't emit warnings about not being able to resolve symbols
1991 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
1992 * a saner message about no samples being in the perf.data file.
1993 *
1994 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01001995 * in header.c at the moment 'perf stat record' gets introduced, which
1996 * is not really needed once we start adding the stat specific PERF_RECORD_
1997 * records, but the need to suppress the kptr_restrict messages in older
1998 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001999 */
2000 int fd = perf_data_file__fd(&perf_stat.file);
2001 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2002 process_synthesized_event,
2003 &perf_stat.session->machines.host);
2004 if (err) {
2005 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2006 "older tools may produce warnings about this file\n.");
2007 }
2008
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002009 if (!interval) {
2010 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2011 pr_err("failed to write stat round event\n");
2012 }
2013
Jiri Olsa664c98d2015-11-05 15:40:50 +01002014 if (!perf_stat.file.is_pipe) {
2015 perf_stat.session->header.data_size += perf_stat.bytes_written;
2016 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2017 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002018
2019 perf_session__delete(perf_stat.session);
2020 }
2021
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002022 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002023 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002024out:
2025 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002026 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002027}