blob: 9805e03ab1638c782efa5626f41ed65b2497fc4f [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 Olsa1975d362015-11-05 15:40:56 +0100138 bool maps_allocated;
139 struct cpu_map *cpus;
140 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100141 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100142};
143
144static struct perf_stat perf_stat;
145#define STAT_RECORD perf_stat.record
146
Liming Wang60666c62009-12-31 16:05:50 +0800147static volatile int done = 0;
148
Jiri Olsa421a50f2015-07-21 14:31:22 +0200149static struct perf_stat_config stat_config = {
150 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200151 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200152};
153
Stephane Eranian13370a92013-01-29 12:47:44 +0100154static inline void diff_timespec(struct timespec *r, struct timespec *a,
155 struct timespec *b)
156{
157 r->tv_sec = a->tv_sec - b->tv_sec;
158 if (a->tv_nsec < b->tv_nsec) {
159 r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
160 r->tv_sec--;
161 } else {
162 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
163 }
164}
165
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200166static void perf_stat__reset_stats(void)
167{
168 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200169 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200170}
171
Jiri Olsacac21422012-11-12 18:34:00 +0100172static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200173{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200174 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200175
Jiri Olsa711a5722015-07-21 14:31:23 +0200176 if (stat_config.scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200177 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
178 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200179
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200180 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300181
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100182 /*
183 * Some events get initialized with sample_(period/type) set,
184 * like tracepoints. Clear it up for counting.
185 */
186 attr->sample_period = 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100187 /*
188 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
189 * while avoiding that older tools show confusing messages.
190 */
191 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100192
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100193 /*
194 * Disabling all counters initially, they will be enabled
195 * either manually by us or by kernel via enable_on_exec
196 * set later.
197 */
Jiri Olsac8280cec2015-12-03 10:06:45 +0100198 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100199 attr->disabled = 1;
200
Jiri Olsac8280cec2015-12-03 10:06:45 +0100201 /*
202 * In case of initial_delay we enable tracee
203 * events manually.
204 */
205 if (target__none(&target) && !initial_delay)
206 attr->enable_on_exec = 1;
207 }
208
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300209 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300210 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200211
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300212 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200213}
214
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200215/*
216 * Does the counter have nsecs as a unit?
217 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200218static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200219{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200220 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
221 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200222 return 1;
223
224 return 0;
225}
226
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100227static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100228 union perf_event *event,
229 struct perf_sample *sample __maybe_unused,
230 struct machine *machine __maybe_unused)
231{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100232 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
233 pr_err("failed to write perf data, error: %m\n");
234 return -1;
235 }
236
237 perf_stat.bytes_written += event->header.size;
238 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100239}
240
Jiri Olsa1975d362015-11-05 15:40:56 +0100241static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100242{
Jiri Olsa1975d362015-11-05 15:40:56 +0100243 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100244 process_synthesized_event,
245 NULL);
246}
247
248#define WRITE_STAT_ROUND_EVENT(time, interval) \
249 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
250
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100251#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
252
253static int
254perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
255 struct perf_counts_values *count)
256{
257 struct perf_sample_id *sid = SID(counter, cpu, thread);
258
259 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
260 process_synthesized_event, NULL);
261}
262
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200263/*
264 * Read out the results of a single counter:
265 * do not aggregate counts across CPUs in system-wide mode
266 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200267static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200268{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100269 int nthreads = thread_map__nr(evsel_list->threads);
270 int ncpus = perf_evsel__nr_cpus(counter);
271 int cpu, thread;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200272
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000273 if (!counter->supported)
274 return -ENOENT;
275
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100276 if (counter->system_wide)
277 nthreads = 1;
278
279 for (thread = 0; thread < nthreads; thread++) {
280 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200281 struct perf_counts_values *count;
282
283 count = perf_counts(counter->counts, cpu, thread);
284 if (perf_evsel__read(counter, cpu, thread, count))
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100285 return -1;
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100286
287 if (STAT_RECORD) {
288 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
289 pr_err("failed to write stat event\n");
290 return -1;
291 }
292 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100293 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200294 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200295
296 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200297}
298
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200299static void read_counters(bool close_counters)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200300{
301 struct perf_evsel *counter;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200302
303 evlist__for_each(evsel_list, counter) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200304 if (read_counter(counter))
Andi Kleen245bad82015-09-01 15:52:46 -0700305 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200306
Jiri Olsaf80010e2015-07-21 14:31:27 +0200307 if (perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200308 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200309
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200310 if (close_counters) {
Jiri Olsa106a94a2015-06-26 11:29:19 +0200311 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
312 thread_map__nr(evsel_list->threads));
313 }
314 }
315}
316
Jiri Olsaba411a92015-06-26 11:29:24 +0200317static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100318{
Stephane Eranian13370a92013-01-29 12:47:44 +0100319 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100320
Jiri Olsa106a94a2015-06-26 11:29:19 +0200321 read_counters(false);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100322
Stephane Eranian13370a92013-01-29 12:47:44 +0100323 clock_gettime(CLOCK_MONOTONIC, &ts);
324 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100325
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100326 if (STAT_RECORD) {
327 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSECS_PER_SEC + rs.tv_nsec, INTERVAL))
328 pr_err("failed to write stat round event\n");
329 }
330
Jiri Olsad4f63a42015-06-26 11:29:26 +0200331 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100332}
333
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100334static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700335{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100336 if (initial_delay)
Andi Kleen41191682013-08-02 17:41:11 -0700337 usleep(initial_delay * 1000);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100338
339 /*
340 * We need to enable counters only if:
341 * - we don't have tracee (attaching to task or cpu)
342 * - we have initial delay configured
343 */
344 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100345 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700346}
347
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300348static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300349
350/*
351 * perf_evlist__prepare_workload will send a SIGUSR1
352 * if the fork fails, since we asked by setting its
353 * want_signal to true.
354 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300355static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
356 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300357{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300358 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300359}
360
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100361static bool has_unit(struct perf_evsel *counter)
362{
363 return counter->unit && *counter->unit;
364}
365
366static bool has_scale(struct perf_evsel *counter)
367{
368 return counter->scale != 1;
369}
370
Jiri Olsa664c98d2015-11-05 15:40:50 +0100371static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100372{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100373 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100374 int err;
375
Jiri Olsa664c98d2015-11-05 15:40:50 +0100376 if (is_pipe) {
377 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
378 process_synthesized_event);
379 if (err < 0) {
380 pr_err("Couldn't synthesize attrs.\n");
381 return err;
382 }
383 }
384
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100385 /*
386 * Synthesize other events stuff not carried within
387 * attr event - unit, scale, name
388 */
389 evlist__for_each(evsel_list, counter) {
390 if (!counter->supported)
391 continue;
392
393 /*
394 * Synthesize unit and scale only if it's defined.
395 */
396 if (has_unit(counter)) {
397 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
398 if (err < 0) {
399 pr_err("Couldn't synthesize evsel unit.\n");
400 return err;
401 }
402 }
403
404 if (has_scale(counter)) {
405 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
406 if (err < 0) {
407 pr_err("Couldn't synthesize evsel scale.\n");
408 return err;
409 }
410 }
411
412 if (counter->own_cpus) {
413 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
414 if (err < 0) {
415 pr_err("Couldn't synthesize evsel scale.\n");
416 return err;
417 }
418 }
419
420 /*
421 * Name is needed only for pipe output,
422 * perf.data carries event names.
423 */
424 if (is_pipe) {
425 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
426 if (err < 0) {
427 pr_err("Couldn't synthesize evsel name.\n");
428 return err;
429 }
430 }
431 }
432
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100433 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
434 process_synthesized_event,
435 NULL);
436 if (err < 0) {
437 pr_err("Couldn't synthesize thread map.\n");
438 return err;
439 }
440
441 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
442 process_synthesized_event, NULL);
443 if (err < 0) {
444 pr_err("Couldn't synthesize thread map.\n");
445 return err;
446 }
447
448 err = perf_event__synthesize_stat_config(NULL, &stat_config,
449 process_synthesized_event, NULL);
450 if (err < 0) {
451 pr_err("Couldn't synthesize config.\n");
452 return err;
453 }
454
455 return 0;
456}
457
Jiri Olsa2af46462015-11-05 15:40:49 +0100458#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
459
460static int __store_counter_ids(struct perf_evsel *counter,
461 struct cpu_map *cpus,
462 struct thread_map *threads)
463{
464 int cpu, thread;
465
466 for (cpu = 0; cpu < cpus->nr; cpu++) {
467 for (thread = 0; thread < threads->nr; thread++) {
468 int fd = FD(counter, cpu, thread);
469
470 if (perf_evlist__id_add_fd(evsel_list, counter,
471 cpu, thread, fd) < 0)
472 return -1;
473 }
474 }
475
476 return 0;
477}
478
479static int store_counter_ids(struct perf_evsel *counter)
480{
481 struct cpu_map *cpus = counter->cpus;
482 struct thread_map *threads = counter->threads;
483
484 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
485 return -ENOMEM;
486
487 return __store_counter_ids(counter, cpus, threads);
488}
489
Namhyung Kimacf28922013-03-11 16:43:18 +0900490static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200491{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200492 int interval = stat_config.interval;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300493 char msg[512];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200494 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100495 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100496 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100497 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200498 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300499 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100500 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200501
Stephane Eranian13370a92013-01-29 12:47:44 +0100502 if (interval) {
503 ts.tv_sec = interval / 1000;
504 ts.tv_nsec = (interval % 1000) * 1000000;
505 } else {
506 ts.tv_sec = 1;
507 ts.tv_nsec = 0;
508 }
509
Liming Wang60666c62009-12-31 16:05:50 +0800510 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100511 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300512 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900513 perror("failed to prepare workload");
514 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800515 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900516 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000517 }
518
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200519 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300520 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200521
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300522 evlist__for_each(evsel_list, counter) {
Jiri Olsacac21422012-11-12 18:34:00 +0100523 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600524 /*
525 * PPC returns ENXIO for HW counters until 2.6.37
526 * (behavior changed with commit b0a873e).
527 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100528 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600529 errno == ENOENT || errno == EOPNOTSUPP ||
530 errno == ENXIO) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600531 if (verbose)
532 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300533 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600534 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400535
536 if ((counter->leader != counter) ||
537 !(counter->leader->nr_members > 1))
538 continue;
David Ahernc63ca0c2011-04-29 16:04:15 -0600539 }
Ingo Molnarede70292011-04-28 08:48:42 +0200540
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300541 perf_evsel__open_strerror(counter, &target,
542 errno, msg, sizeof(msg));
543 ui__error("%s\n", msg);
544
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200545 if (child_pid != -1)
546 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600547
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200548 return -1;
549 }
David Ahern2cee77c2011-05-30 08:55:59 -0600550 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100551
552 l = strlen(counter->unit);
553 if (l > unit_width)
554 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100555
556 if (STAT_RECORD && store_counter_ids(counter))
557 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300558 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200559
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300560 if (perf_evlist__apply_filters(evsel_list, &counter)) {
561 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
562 counter->filter, perf_evsel__name(counter), errno,
Masami Hiramatsu759e6122014-08-14 02:22:55 +0000563 strerror_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100564 return -1;
565 }
566
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100567 if (STAT_RECORD) {
568 int err, fd = perf_data_file__fd(&perf_stat.file);
569
Jiri Olsa664c98d2015-11-05 15:40:50 +0100570 if (is_pipe) {
571 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
572 } else {
573 err = perf_session__write_header(perf_stat.session, evsel_list,
574 fd, false);
575 }
576
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100577 if (err < 0)
578 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100579
Jiri Olsa664c98d2015-11-05 15:40:50 +0100580 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100581 if (err < 0)
582 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100583 }
584
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200585 /*
586 * Enable counters and exec the command:
587 */
588 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100589 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200590
Liming Wang60666c62009-12-31 16:05:50 +0800591 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900592 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100593 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900594
Stephane Eranian13370a92013-01-29 12:47:44 +0100595 if (interval) {
596 while (!waitpid(child_pid, &status, WNOHANG)) {
597 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200598 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100599 }
600 }
Liming Wang60666c62009-12-31 16:05:50 +0800601 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300602
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300603 if (workload_exec_errno) {
604 const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
605 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300606 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300607 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300608
Andi Kleen33e49ea2011-09-15 14:31:40 -0700609 if (WIFSIGNALED(status))
610 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800611 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100612 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100613 while (!done) {
614 nanosleep(&ts, NULL);
615 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200616 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100617 }
Liming Wang60666c62009-12-31 16:05:50 +0800618 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200619
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200620 t1 = rdclock();
621
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200622 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200623
Jiri Olsa106a94a2015-06-26 11:29:19 +0200624 read_counters(true);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200625
Ingo Molnar42202dd2009-06-13 14:57:28 +0200626 return WEXITSTATUS(status);
627}
628
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300629static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200630{
631 int ret;
632
633 if (pre_cmd) {
634 ret = system(pre_cmd);
635 if (ret)
636 return ret;
637 }
638
639 if (sync_run)
640 sync();
641
642 ret = __run_perf_stat(argc, argv);
643 if (ret)
644 return ret;
645
646 if (post_cmd) {
647 ret = system(post_cmd);
648 if (ret)
649 return ret;
650 }
651
652 return ret;
653}
654
Andi Kleend73515c2015-03-11 07:16:27 -0700655static void print_running(u64 run, u64 ena)
656{
657 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200658 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700659 csv_sep,
660 run,
661 csv_sep,
662 ena ? 100.0 * run / ena : 100.0);
663 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200664 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700665 }
666}
667
Ingo Molnarf99844c2011-04-27 05:35:39 +0200668static void print_noise_pct(double total, double avg)
669{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800670 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200671
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700672 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200673 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600674 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200675 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200676}
677
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200678static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200679{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200680 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200681
Peter Zijlstra849abde2009-09-04 18:23:38 +0200682 if (run_count == 1)
683 return;
684
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200685 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200686 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200687}
688
Stephane Eranian12c08a92013-02-14 13:57:29 +0100689static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200690{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200691 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100692 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200693 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100694 cpu_map__id_to_socket(id),
695 csv_output ? 0 : -8,
696 cpu_map__id_to_cpu(id),
697 csv_sep,
698 csv_output ? 0 : 4,
699 nr,
700 csv_sep);
701 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100702 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200703 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100704 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100705 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100706 csv_sep,
707 csv_output ? 0 : 4,
708 nr,
709 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100710 break;
711 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200712 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200713 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100714 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100715 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200716 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200717 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200718 csv_output ? 0 : 16,
719 thread_map__comm(evsel->threads, id),
720 csv_output ? 0 : -8,
721 thread_map__pid(evsel->threads, id),
722 csv_sep);
723 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100724 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200725 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100726 default:
727 break;
728 }
729}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200730
Andi Kleenda88c7f2014-09-24 13:50:46 -0700731static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100732{
Jiri Olsa58215222015-07-21 14:31:24 +0200733 FILE *output = stat_config.output;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100734 double msecs = avg / 1e6;
Stephane Eranian410136f2013-11-12 17:58:49 +0100735 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600736 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100737
Stephane Eranian410136f2013-11-12 17:58:49 +0100738 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
739 fmt_n = csv_output ? "%s" : "%-25s";
740
Andi Kleenda88c7f2014-09-24 13:50:46 -0700741 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100742
David Ahern4bbe5a62013-09-28 14:28:00 -0600743 scnprintf(name, sizeof(name), "%s%s",
744 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +0100745
746 fprintf(output, fmt_v, msecs, csv_sep);
747
748 if (csv_output)
749 fprintf(output, "%s%s", evsel->unit, csv_sep);
750 else
751 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
752
753 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200754
Stephane Eranian023695d2011-02-14 11:20:01 +0200755 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200756 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200757}
758
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200759static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
760{
Jiri Olsa58215222015-07-21 14:31:24 +0200761 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200762 double sc = evsel->scale;
763 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200764
765 if (csv_output) {
766 fmt = sc != 1.0 ? "%.2f%s" : "%.0f%s";
767 } else {
768 if (big_num)
769 fmt = sc != 1.0 ? "%'18.2f%s" : "%'18.0f%s";
770 else
771 fmt = sc != 1.0 ? "%18.2f%s" : "%18.0f%s";
772 }
773
774 aggr_printout(evsel, id, nr);
775
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200776 fprintf(output, fmt, avg, csv_sep);
777
778 if (evsel->unit)
779 fprintf(output, "%-*s%s",
780 csv_output ? 0 : unit_width,
781 evsel->unit, csv_sep);
782
783 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
784
785 if (evsel->cgrp)
786 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -0800787}
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200788
Andi Kleeneedfcb42015-11-02 17:50:21 -0800789static void printout(int id, int nr, struct perf_evsel *counter, double uval)
790{
791 int cpu = cpu_map__id_to_cpu(id);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200792
Andi Kleeneedfcb42015-11-02 17:50:21 -0800793 if (stat_config.aggr_mode == AGGR_GLOBAL)
794 cpu = 0;
795
796 if (nsec_counter(counter))
797 nsec_printout(id, nr, counter, uval);
798 else
799 abs_printout(id, nr, counter, uval);
800
801 if (!csv_output && !stat_config.interval)
802 perf_stat__print_shadow_stats(stat_config.output, counter,
803 uval, cpu,
804 stat_config.aggr_mode);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200805}
806
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100807static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100808{
Jiri Olsa58215222015-07-21 14:31:24 +0200809 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100810 struct perf_evsel *counter;
Kan Liang601083c2015-07-02 03:08:43 -0400811 int cpu, s, s2, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +0100812 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100813 u64 ena, run, val;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100814
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100815 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100816 return;
817
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100818 for (s = 0; s < aggr_map->nr; s++) {
819 id = aggr_map->map[s];
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300820 evlist__for_each(evsel_list, counter) {
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100821 val = ena = run = 0;
822 nr = 0;
823 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Kan Liang601083c2015-07-02 03:08:43 -0400824 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100825 if (s2 != id)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100826 continue;
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200827 val += perf_counts(counter->counts, cpu, 0)->val;
828 ena += perf_counts(counter->counts, cpu, 0)->ena;
829 run += perf_counts(counter->counts, cpu, 0)->run;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100830 nr++;
831 }
832 if (prefix)
833 fprintf(output, "%s", prefix);
834
835 if (run == 0 || ena == 0) {
Stephane Eranian582ec0822013-07-05 19:06:45 +0200836 aggr_printout(counter, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100837
Stephane Eranian410136f2013-11-12 17:58:49 +0100838 fprintf(output, "%*s%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100839 csv_output ? 0 : 18,
840 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100841 csv_sep);
842
843 fprintf(output, "%-*s%s",
844 csv_output ? 0 : unit_width,
845 counter->unit, csv_sep);
846
847 fprintf(output, "%*s",
848 csv_output ? 0 : -25,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100849 perf_evsel__name(counter));
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100850
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100851 if (counter->cgrp)
852 fprintf(output, "%s%s",
853 csv_sep, counter->cgrp->name);
854
Andi Kleend73515c2015-03-11 07:16:27 -0700855 print_running(run, ena);
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100856 fputc('\n', output);
857 continue;
858 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100859 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800860 printout(id, nr, counter, uval);
Andi Kleend73515c2015-03-11 07:16:27 -0700861 if (!csv_output)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100862 print_noise(counter, 1.0);
863
Andi Kleend73515c2015-03-11 07:16:27 -0700864 print_running(run, ena);
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100865 fputc('\n', output);
866 }
867 }
868}
869
Jiri Olsa32b8af82015-06-26 11:29:27 +0200870static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
871{
Jiri Olsa58215222015-07-21 14:31:24 +0200872 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200873 int nthreads = thread_map__nr(counter->threads);
874 int ncpus = cpu_map__nr(counter->cpus);
875 int cpu, thread;
876 double uval;
877
878 for (thread = 0; thread < nthreads; thread++) {
879 u64 ena = 0, run = 0, val = 0;
880
881 for (cpu = 0; cpu < ncpus; cpu++) {
882 val += perf_counts(counter->counts, cpu, thread)->val;
883 ena += perf_counts(counter->counts, cpu, thread)->ena;
884 run += perf_counts(counter->counts, cpu, thread)->run;
885 }
886
887 if (prefix)
888 fprintf(output, "%s", prefix);
889
890 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800891 printout(thread, 0, counter, uval);
Jiri Olsa32b8af82015-06-26 11:29:27 +0200892
893 if (!csv_output)
894 print_noise(counter, 1.0);
895
896 print_running(run, ena);
897 fputc('\n', output);
898 }
899}
900
Ingo Molnar42202dd2009-06-13 14:57:28 +0200901/*
902 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200903 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +0200904 */
Stephane Eranian13370a92013-01-29 12:47:44 +0100905static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200906{
Jiri Olsa58215222015-07-21 14:31:24 +0200907 FILE *output = stat_config.output;
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200908 struct perf_stat_evsel *ps = counter->priv;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200909 double avg = avg_stats(&ps->res_stats[0]);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200910 int scaled = counter->counts->scaled;
Stephane Eranian410136f2013-11-12 17:58:49 +0100911 double uval;
Andi Kleend73515c2015-03-11 07:16:27 -0700912 double avg_enabled, avg_running;
913
914 avg_enabled = avg_stats(&ps->res_stats[1]);
915 avg_running = avg_stats(&ps->res_stats[2]);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200916
Stephane Eranian13370a92013-01-29 12:47:44 +0100917 if (prefix)
918 fprintf(output, "%s", prefix);
919
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000920 if (scaled == -1 || !counter->supported) {
Stephane Eranian410136f2013-11-12 17:58:49 +0100921 fprintf(output, "%*s%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200922 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600923 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100924 csv_sep);
925 fprintf(output, "%-*s%s",
926 csv_output ? 0 : unit_width,
927 counter->unit, csv_sep);
928 fprintf(output, "%*s",
929 csv_output ? 0 : -25,
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300930 perf_evsel__name(counter));
Stephane Eranian023695d2011-02-14 11:20:01 +0200931
932 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200933 fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200934
Andi Kleend73515c2015-03-11 07:16:27 -0700935 print_running(avg_running, avg_enabled);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200936 fputc('\n', output);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200937 return;
938 }
939
Stephane Eranian410136f2013-11-12 17:58:49 +0100940 uval = avg * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800941 printout(-1, 0, counter, uval);
Peter Zijlstra849abde2009-09-04 18:23:38 +0200942
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700943 print_noise(counter, avg);
944
Andi Kleend73515c2015-03-11 07:16:27 -0700945 print_running(avg_running, avg_enabled);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200946 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200947}
948
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200949/*
950 * Print out the results of a single counter:
951 * does not use aggregated count in system-wide
952 */
Stephane Eranian13370a92013-01-29 12:47:44 +0100953static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200954{
Jiri Olsa58215222015-07-21 14:31:24 +0200955 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200956 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +0100957 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200958 int cpu;
959
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800960 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200961 val = perf_counts(counter->counts, cpu, 0)->val;
962 ena = perf_counts(counter->counts, cpu, 0)->ena;
963 run = perf_counts(counter->counts, cpu, 0)->run;
Stephane Eranian13370a92013-01-29 12:47:44 +0100964
965 if (prefix)
966 fprintf(output, "%s", prefix);
967
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200968 if (run == 0 || ena == 0) {
Stephane Eranian410136f2013-11-12 17:58:49 +0100969 fprintf(output, "CPU%*d%s%*s%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200970 csv_output ? 0 : -4,
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800971 perf_evsel__cpus(counter)->map[cpu], csv_sep,
Stephane Eraniand7470b62010-12-01 18:49:05 +0200972 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600973 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100974 csv_sep);
975
976 fprintf(output, "%-*s%s",
977 csv_output ? 0 : unit_width,
978 counter->unit, csv_sep);
979
980 fprintf(output, "%*s",
981 csv_output ? 0 : -25,
982 perf_evsel__name(counter));
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200983
Stephane Eranian023695d2011-02-14 11:20:01 +0200984 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200985 fprintf(output, "%s%s",
986 csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200987
Andi Kleend73515c2015-03-11 07:16:27 -0700988 print_running(run, ena);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200989 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200990 continue;
991 }
992
Stephane Eranian410136f2013-11-12 17:58:49 +0100993 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800994 printout(cpu, 0, counter, uval);
Andi Kleend73515c2015-03-11 07:16:27 -0700995 if (!csv_output)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200996 print_noise(counter, 1.0);
Andi Kleend73515c2015-03-11 07:16:27 -0700997 print_running(run, ena);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200998
Stephane Eranian4aa90152011-08-15 22:22:33 +0200999 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001000 }
1001}
1002
Jiri Olsad4f63a42015-06-26 11:29:26 +02001003static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001004{
Jiri Olsa58215222015-07-21 14:31:24 +02001005 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001006 static int num_print_interval;
1007
1008 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1009
1010 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001011 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001012 case AGGR_SOCKET:
1013 fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit");
1014 break;
1015 case AGGR_CORE:
1016 fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit");
1017 break;
1018 case AGGR_NONE:
1019 fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit");
1020 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001021 case AGGR_THREAD:
1022 fprintf(output, "# time comm-pid counts %*s events\n", unit_width, "unit");
1023 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001024 case AGGR_GLOBAL:
1025 default:
1026 fprintf(output, "# time counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001027 case AGGR_UNSET:
1028 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001029 }
1030 }
1031
1032 if (++num_print_interval == 25)
1033 num_print_interval = 0;
1034}
1035
1036static void print_header(int argc, const char **argv)
1037{
Jiri Olsa58215222015-07-21 14:31:24 +02001038 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001039 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001040
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001041 fflush(stdout);
1042
Stephane Eraniand7470b62010-12-01 18:49:05 +02001043 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001044 fprintf(output, "\n");
1045 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001046 if (target.system_wide)
1047 fprintf(output, "\'system wide");
1048 else if (target.cpu_list)
1049 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001050 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001051 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1052 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001053 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001054 } else if (target.pid)
1055 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001056 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001057 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001058
Stephane Eranian4aa90152011-08-15 22:22:33 +02001059 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001060 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001061 fprintf(output, " (%d runs)", run_count);
1062 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001063 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001064}
1065
1066static void print_footer(void)
1067{
Jiri Olsa58215222015-07-21 14:31:24 +02001068 FILE *output = stat_config.output;
1069
Jiri Olsad4f63a42015-06-26 11:29:26 +02001070 if (!null_run)
1071 fprintf(output, "\n");
1072 fprintf(output, " %17.9f seconds time elapsed",
1073 avg_stats(&walltime_nsecs_stats)/1e9);
1074 if (run_count > 1) {
1075 fprintf(output, " ");
1076 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1077 avg_stats(&walltime_nsecs_stats));
1078 }
1079 fprintf(output, "\n\n");
1080}
1081
1082static void print_counters(struct timespec *ts, int argc, const char **argv)
1083{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001084 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001085 struct perf_evsel *counter;
1086 char buf[64], *prefix = NULL;
1087
Jiri Olsa664c98d2015-11-05 15:40:50 +01001088 /* Do not print anything if we record to the pipe. */
1089 if (STAT_RECORD && perf_stat.file.is_pipe)
1090 return;
1091
Jiri Olsad4f63a42015-06-26 11:29:26 +02001092 if (interval)
1093 print_interval(prefix = buf, ts);
1094 else
1095 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001096
Jiri Olsa421a50f2015-07-21 14:31:22 +02001097 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001098 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001099 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001100 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001101 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001102 case AGGR_THREAD:
1103 evlist__for_each(evsel_list, counter)
1104 print_aggr_thread(counter, prefix);
1105 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001106 case AGGR_GLOBAL:
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_aggr(counter, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001109 break;
1110 case AGGR_NONE:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001111 evlist__for_each(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001112 print_counter(counter, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001113 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001114 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001115 default:
1116 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001117 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001118
Jiri Olsad4f63a42015-06-26 11:29:26 +02001119 if (!interval && !csv_output)
1120 print_footer();
1121
Jiri Olsa58215222015-07-21 14:31:24 +02001122 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001123}
1124
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001125static volatile int signr = -1;
1126
Ingo Molnar52425192009-05-26 09:17:18 +02001127static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001128{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001129 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001130 done = 1;
1131
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001132 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001133 /*
1134 * render child_pid harmless
1135 * won't send SIGTERM to a random
1136 * process in case of race condition
1137 * and fast PID recycling
1138 */
1139 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001140}
1141
1142static void sig_atexit(void)
1143{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001144 sigset_t set, oset;
1145
1146 /*
1147 * avoid race condition with SIGCHLD handler
1148 * in skip_signal() which is modifying child_pid
1149 * goal is to avoid send SIGTERM to a random
1150 * process
1151 */
1152 sigemptyset(&set);
1153 sigaddset(&set, SIGCHLD);
1154 sigprocmask(SIG_BLOCK, &set, &oset);
1155
Chris Wilson933da832009-10-04 01:35:01 +01001156 if (child_pid != -1)
1157 kill(child_pid, SIGTERM);
1158
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001159 sigprocmask(SIG_SETMASK, &oset, NULL);
1160
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001161 if (signr == -1)
1162 return;
1163
1164 signal(signr, SIG_DFL);
1165 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001166}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001167
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001168static int stat__set_big_num(const struct option *opt __maybe_unused,
1169 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001170{
1171 big_num_opt = unset ? 0 : 1;
1172 return 0;
1173}
1174
Jiri Olsae0547312015-11-05 15:40:45 +01001175static const struct option stat_options[] = {
1176 OPT_BOOLEAN('T', "transaction", &transaction_run,
1177 "hardware transaction statistics"),
1178 OPT_CALLBACK('e', "event", &evsel_list, "event",
1179 "event selector. use 'perf list' to list available events",
1180 parse_events_option),
1181 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1182 "event filter", parse_filter),
1183 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1184 "child tasks do not inherit counters"),
1185 OPT_STRING('p', "pid", &target.pid, "pid",
1186 "stat events on existing process id"),
1187 OPT_STRING('t', "tid", &target.tid, "tid",
1188 "stat events on existing thread id"),
1189 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1190 "system-wide collection from all CPUs"),
1191 OPT_BOOLEAN('g', "group", &group,
1192 "put the counters into a counter group"),
1193 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1194 OPT_INCR('v', "verbose", &verbose,
1195 "be more verbose (show counter open errors, etc)"),
1196 OPT_INTEGER('r', "repeat", &run_count,
1197 "repeat command and print average + stddev (max: 100, forever: 0)"),
1198 OPT_BOOLEAN('n', "null", &null_run,
1199 "null run - dont start any counters"),
1200 OPT_INCR('d', "detailed", &detailed_run,
1201 "detailed run - start a lot of events"),
1202 OPT_BOOLEAN('S', "sync", &sync_run,
1203 "call sync() before starting a run"),
1204 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1205 "print large numbers with thousands\' separators",
1206 stat__set_big_num),
1207 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1208 "list of cpus to monitor in system-wide"),
1209 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1210 "disable CPU count aggregation", AGGR_NONE),
1211 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1212 "print counts with custom separator"),
1213 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1214 "monitor event in cgroup name only", parse_cgroups),
1215 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1216 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1217 OPT_INTEGER(0, "log-fd", &output_fd,
1218 "log output to fd, instead of stderr"),
1219 OPT_STRING(0, "pre", &pre_cmd, "command",
1220 "command to run prior to the measured command"),
1221 OPT_STRING(0, "post", &post_cmd, "command",
1222 "command to run after to the measured command"),
1223 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1224 "print counts at regular interval in ms (>= 10)"),
1225 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1226 "aggregate counts per processor socket", AGGR_SOCKET),
1227 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1228 "aggregate counts per physical processor core", AGGR_CORE),
1229 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1230 "aggregate counts per thread", AGGR_THREAD),
1231 OPT_UINTEGER('D', "delay", &initial_delay,
1232 "ms to wait before starting measurement after program start"),
1233 OPT_END()
1234};
1235
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001236static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1237{
1238 return cpu_map__get_socket(map, cpu, NULL);
1239}
1240
1241static int perf_stat__get_core(struct cpu_map *map, int cpu)
1242{
1243 return cpu_map__get_core(map, cpu, NULL);
1244}
1245
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001246static int cpu_map__get_max(struct cpu_map *map)
1247{
1248 int i, max = -1;
1249
1250 for (i = 0; i < map->nr; i++) {
1251 if (map->map[i] > max)
1252 max = map->map[i];
1253 }
1254
1255 return max;
1256}
1257
1258static struct cpu_map *cpus_aggr_map;
1259
1260static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1261{
1262 int cpu;
1263
1264 if (idx >= map->nr)
1265 return -1;
1266
1267 cpu = map->map[idx];
1268
1269 if (cpus_aggr_map->map[cpu] == -1)
1270 cpus_aggr_map->map[cpu] = get_id(map, idx);
1271
1272 return cpus_aggr_map->map[cpu];
1273}
1274
1275static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1276{
1277 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1278}
1279
1280static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1281{
1282 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1283}
1284
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001285static int perf_stat_init_aggr_mode(void)
1286{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001287 int nr;
1288
Jiri Olsa421a50f2015-07-21 14:31:22 +02001289 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001290 case AGGR_SOCKET:
1291 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1292 perror("cannot build socket map");
1293 return -1;
1294 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001295 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001296 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001297 case AGGR_CORE:
1298 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1299 perror("cannot build core map");
1300 return -1;
1301 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001302 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001303 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001304 case AGGR_NONE:
1305 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001306 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001307 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001308 default:
1309 break;
1310 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001311
1312 /*
1313 * The evsel_list->cpus is the base we operate on,
1314 * taking the highest cpu number to be the size of
1315 * the aggregation translate cpumap.
1316 */
1317 nr = cpu_map__get_max(evsel_list->cpus);
1318 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1319 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001320}
1321
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001322static void perf_stat__exit_aggr_mode(void)
1323{
1324 cpu_map__put(aggr_map);
1325 cpu_map__put(cpus_aggr_map);
1326 aggr_map = NULL;
1327 cpus_aggr_map = NULL;
1328}
1329
Jiri Olsa68d702f2015-11-05 15:40:58 +01001330static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1331{
1332 int cpu;
1333
1334 if (idx > map->nr)
1335 return -1;
1336
1337 cpu = map->map[idx];
1338
1339 if (cpu >= env->nr_cpus_online)
1340 return -1;
1341
1342 return cpu;
1343}
1344
1345static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1346{
1347 struct perf_env *env = data;
1348 int cpu = perf_env__get_cpu(env, map, idx);
1349
1350 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1351}
1352
1353static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1354{
1355 struct perf_env *env = data;
1356 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1357
1358 if (cpu != -1) {
1359 int socket_id = env->cpu[cpu].socket_id;
1360
1361 /*
1362 * Encode socket in upper 16 bits
1363 * core_id is relative to socket, and
1364 * we need a global id. So we combine
1365 * socket + core id.
1366 */
1367 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1368 }
1369
1370 return core;
1371}
1372
1373static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1374 struct cpu_map **sockp)
1375{
1376 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1377}
1378
1379static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1380 struct cpu_map **corep)
1381{
1382 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1383}
1384
1385static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1386{
1387 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1388}
1389
1390static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1391{
1392 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1393}
1394
1395static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1396{
1397 struct perf_env *env = &st->session->header.env;
1398
1399 switch (stat_config.aggr_mode) {
1400 case AGGR_SOCKET:
1401 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1402 perror("cannot build socket map");
1403 return -1;
1404 }
1405 aggr_get_id = perf_stat__get_socket_file;
1406 break;
1407 case AGGR_CORE:
1408 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1409 perror("cannot build core map");
1410 return -1;
1411 }
1412 aggr_get_id = perf_stat__get_core_file;
1413 break;
1414 case AGGR_NONE:
1415 case AGGR_GLOBAL:
1416 case AGGR_THREAD:
1417 case AGGR_UNSET:
1418 default:
1419 break;
1420 }
1421
1422 return 0;
1423}
1424
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001425/*
1426 * Add default attributes, if there were no attributes specified or
1427 * if -d/--detailed, -d -d or -d -d -d is used:
1428 */
1429static int add_default_attributes(void)
1430{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001431 struct perf_event_attr default_attrs[] = {
1432
1433 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1434 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1435 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1436 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1437
1438 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
1439 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
1440 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
1441 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1442 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1443 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1444
1445};
1446
1447/*
1448 * Detailed stats (-d), covering the L1 and last level data caches:
1449 */
1450 struct perf_event_attr detailed_attrs[] = {
1451
1452 { .type = PERF_TYPE_HW_CACHE,
1453 .config =
1454 PERF_COUNT_HW_CACHE_L1D << 0 |
1455 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1456 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1457
1458 { .type = PERF_TYPE_HW_CACHE,
1459 .config =
1460 PERF_COUNT_HW_CACHE_L1D << 0 |
1461 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1462 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1463
1464 { .type = PERF_TYPE_HW_CACHE,
1465 .config =
1466 PERF_COUNT_HW_CACHE_LL << 0 |
1467 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1468 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1469
1470 { .type = PERF_TYPE_HW_CACHE,
1471 .config =
1472 PERF_COUNT_HW_CACHE_LL << 0 |
1473 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1474 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1475};
1476
1477/*
1478 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1479 */
1480 struct perf_event_attr very_detailed_attrs[] = {
1481
1482 { .type = PERF_TYPE_HW_CACHE,
1483 .config =
1484 PERF_COUNT_HW_CACHE_L1I << 0 |
1485 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1486 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1487
1488 { .type = PERF_TYPE_HW_CACHE,
1489 .config =
1490 PERF_COUNT_HW_CACHE_L1I << 0 |
1491 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1492 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1493
1494 { .type = PERF_TYPE_HW_CACHE,
1495 .config =
1496 PERF_COUNT_HW_CACHE_DTLB << 0 |
1497 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1498 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1499
1500 { .type = PERF_TYPE_HW_CACHE,
1501 .config =
1502 PERF_COUNT_HW_CACHE_DTLB << 0 |
1503 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1504 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1505
1506 { .type = PERF_TYPE_HW_CACHE,
1507 .config =
1508 PERF_COUNT_HW_CACHE_ITLB << 0 |
1509 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1510 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1511
1512 { .type = PERF_TYPE_HW_CACHE,
1513 .config =
1514 PERF_COUNT_HW_CACHE_ITLB << 0 |
1515 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1516 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1517
1518};
1519
1520/*
1521 * Very, very detailed stats (-d -d -d), adding prefetch events:
1522 */
1523 struct perf_event_attr very_very_detailed_attrs[] = {
1524
1525 { .type = PERF_TYPE_HW_CACHE,
1526 .config =
1527 PERF_COUNT_HW_CACHE_L1D << 0 |
1528 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1529 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1530
1531 { .type = PERF_TYPE_HW_CACHE,
1532 .config =
1533 PERF_COUNT_HW_CACHE_L1D << 0 |
1534 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1535 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1536};
1537
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001538 /* Set attrs if no event is selected and !null_run: */
1539 if (null_run)
1540 return 0;
1541
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001542 if (transaction_run) {
1543 int err;
1544 if (pmu_have_event("cpu", "cycles-ct") &&
1545 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02001546 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001547 else
Jiri Olsaa4547422015-06-03 16:25:53 +02001548 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
1549 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001550 fprintf(stderr, "Cannot set up transaction events\n");
1551 return -1;
1552 }
1553 return 0;
1554 }
1555
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001556 if (!evsel_list->nr_entries) {
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001557 if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001558 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001559 }
1560
1561 /* Detailed events get appended to the event list: */
1562
1563 if (detailed_run < 1)
1564 return 0;
1565
1566 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001567 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001568 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001569
1570 if (detailed_run < 2)
1571 return 0;
1572
1573 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001574 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001575 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001576
1577 if (detailed_run < 3)
1578 return 0;
1579
1580 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001581 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001582}
1583
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001584static const char * const recort_usage[] = {
1585 "perf stat record [<options>]",
1586 NULL,
1587};
1588
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001589static void init_features(struct perf_session *session)
1590{
1591 int feat;
1592
1593 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
1594 perf_header__set_feat(&session->header, feat);
1595
1596 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
1597 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
1598 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1599 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1600}
1601
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001602static int __cmd_record(int argc, const char **argv)
1603{
1604 struct perf_session *session;
1605 struct perf_data_file *file = &perf_stat.file;
1606
1607 argc = parse_options(argc, argv, stat_options, record_usage,
1608 PARSE_OPT_STOP_AT_NON_OPTION);
1609
1610 if (output_name)
1611 file->path = output_name;
1612
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01001613 if (run_count != 1 || forever) {
1614 pr_err("Cannot use -r option with perf stat record.\n");
1615 return -1;
1616 }
1617
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001618 session = perf_session__new(file, false, NULL);
1619 if (session == NULL) {
1620 pr_err("Perf session creation failed.\n");
1621 return -1;
1622 }
1623
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001624 init_features(session);
1625
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001626 session->evlist = evsel_list;
1627 perf_stat.session = session;
1628 perf_stat.record = true;
1629 return argc;
1630}
1631
Jiri Olsaa56f9392015-11-05 15:40:59 +01001632static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1633 union perf_event *event,
1634 struct perf_session *session)
1635{
1636 struct stat_round_event *round = &event->stat_round;
1637 struct perf_evsel *counter;
1638 struct timespec tsh, *ts = NULL;
1639 const char **argv = session->header.env.cmdline_argv;
1640 int argc = session->header.env.nr_cmdline;
1641
1642 evlist__for_each(evsel_list, counter)
1643 perf_stat_process_counter(&stat_config, counter);
1644
1645 if (round->type == PERF_STAT_ROUND_TYPE__FINAL)
1646 update_stats(&walltime_nsecs_stats, round->time);
1647
1648 if (stat_config.interval && round->time) {
1649 tsh.tv_sec = round->time / NSECS_PER_SEC;
1650 tsh.tv_nsec = round->time % NSECS_PER_SEC;
1651 ts = &tsh;
1652 }
1653
1654 print_counters(ts, argc, argv);
1655 return 0;
1656}
1657
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01001658static
1659int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1660 union perf_event *event,
1661 struct perf_session *session __maybe_unused)
1662{
Jiri Olsa68d702f2015-11-05 15:40:58 +01001663 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
1664
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01001665 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01001666
Jiri Olsa89af4e02015-11-05 15:41:02 +01001667 if (cpu_map__empty(st->cpus)) {
1668 if (st->aggr_mode != AGGR_UNSET)
1669 pr_warning("warning: processing task data, aggregation mode not set\n");
1670 return 0;
1671 }
1672
1673 if (st->aggr_mode != AGGR_UNSET)
1674 stat_config.aggr_mode = st->aggr_mode;
1675
Jiri Olsa68d702f2015-11-05 15:40:58 +01001676 if (perf_stat.file.is_pipe)
1677 perf_stat_init_aggr_mode();
1678 else
1679 perf_stat_init_aggr_mode_file(st);
1680
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01001681 return 0;
1682}
1683
Jiri Olsa1975d362015-11-05 15:40:56 +01001684static int set_maps(struct perf_stat *st)
1685{
1686 if (!st->cpus || !st->threads)
1687 return 0;
1688
1689 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
1690 return -EINVAL;
1691
1692 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
1693
1694 if (perf_evlist__alloc_stats(evsel_list, true))
1695 return -ENOMEM;
1696
1697 st->maps_allocated = true;
1698 return 0;
1699}
1700
1701static
1702int process_thread_map_event(struct perf_tool *tool __maybe_unused,
1703 union perf_event *event,
1704 struct perf_session *session __maybe_unused)
1705{
1706 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
1707
1708 if (st->threads) {
1709 pr_warning("Extra thread map event, ignoring.\n");
1710 return 0;
1711 }
1712
1713 st->threads = thread_map__new_event(&event->thread_map);
1714 if (!st->threads)
1715 return -ENOMEM;
1716
1717 return set_maps(st);
1718}
1719
1720static
1721int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
1722 union perf_event *event,
1723 struct perf_session *session __maybe_unused)
1724{
1725 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
1726 struct cpu_map *cpus;
1727
1728 if (st->cpus) {
1729 pr_warning("Extra cpu map event, ignoring.\n");
1730 return 0;
1731 }
1732
1733 cpus = cpu_map__new_data(&event->cpu_map.data);
1734 if (!cpus)
1735 return -ENOMEM;
1736
1737 st->cpus = cpus;
1738 return set_maps(st);
1739}
1740
Jiri Olsaba6039b62015-11-05 15:40:55 +01001741static const char * const report_usage[] = {
1742 "perf stat report [<options>]",
1743 NULL,
1744};
1745
1746static struct perf_stat perf_stat = {
1747 .tool = {
1748 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01001749 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01001750 .thread_map = process_thread_map_event,
1751 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01001752 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01001753 .stat = perf_event__process_stat_event,
1754 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01001755 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01001756 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01001757};
1758
1759static int __cmd_report(int argc, const char **argv)
1760{
1761 struct perf_session *session;
1762 const struct option options[] = {
1763 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01001764 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
1765 "aggregate counts per processor socket", AGGR_SOCKET),
1766 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
1767 "aggregate counts per physical processor core", AGGR_CORE),
1768 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
1769 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01001770 OPT_END()
1771 };
1772 struct stat st;
1773 int ret;
1774
1775 argc = parse_options(argc, argv, options, report_usage, 0);
1776
1777 if (!input_name || !strlen(input_name)) {
1778 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
1779 input_name = "-";
1780 else
1781 input_name = "perf.data";
1782 }
1783
1784 perf_stat.file.path = input_name;
1785 perf_stat.file.mode = PERF_DATA_MODE_READ;
1786
1787 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
1788 if (session == NULL)
1789 return -1;
1790
1791 perf_stat.session = session;
1792 stat_config.output = stderr;
1793 evsel_list = session->evlist;
1794
1795 ret = perf_session__process_events(session);
1796 if (ret)
1797 return ret;
1798
1799 perf_session__delete(session);
1800 return 0;
1801}
1802
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001803int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02001804{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001805 const char * const stat_usage[] = {
1806 "perf stat [<options>] [<command>]",
1807 NULL
1808 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09001809 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001810 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02001811 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001812 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01001813 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02001814
Stephane Eranian5af52b52010-05-18 15:00:01 +02001815 setlocale(LC_ALL, "");
1816
Namhyung Kim334fe7a2013-03-11 16:43:12 +09001817 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001818 if (evsel_list == NULL)
1819 return -ENOMEM;
1820
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001821 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
1822 (const char **) stat_usage,
1823 PARSE_OPT_STOP_AT_NON_OPTION);
1824
Jiri Olsa6edb78a2015-11-05 15:41:01 +01001825 if (csv_sep) {
1826 csv_output = true;
1827 if (!strcmp(csv_sep, "\\t"))
1828 csv_sep = "\t";
1829 } else
1830 csv_sep = DEFAULT_SEPARATOR;
1831
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001832 if (argc && !strncmp(argv[0], "rec", 3)) {
1833 argc = __cmd_record(argc, argv);
1834 if (argc < 0)
1835 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01001836 } else if (argc && !strncmp(argv[0], "rep", 3))
1837 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001838
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001839 interval = stat_config.interval;
1840
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001841 /*
1842 * For record command the -o is already taken care of.
1843 */
1844 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02001845 output = NULL;
1846
Jim Cromie56f3bae2011-09-07 17:14:00 -06001847 if (output_name && output_fd) {
1848 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001849 parse_options_usage(stat_usage, stat_options, "o", 1);
1850 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001851 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06001852 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001853
1854 if (output_fd < 0) {
1855 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001856 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001857 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001858 }
1859
Stephane Eranian4aa90152011-08-15 22:22:33 +02001860 if (!output) {
1861 struct timespec tm;
1862 mode = append_file ? "a" : "w";
1863
1864 output = fopen(output_name, mode);
1865 if (!output) {
1866 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06001867 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001868 }
1869 clock_gettime(CLOCK_REALTIME, &tm);
1870 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001871 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06001872 mode = append_file ? "a" : "w";
1873 output = fdopen(output_fd, mode);
1874 if (!output) {
1875 perror("Failed opening logfd");
1876 return -errno;
1877 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001878 }
1879
Jiri Olsa58215222015-07-21 14:31:24 +02001880 stat_config.output = output;
1881
Stephane Eraniand7470b62010-12-01 18:49:05 +02001882 /*
1883 * let the spreadsheet do the pretty-printing
1884 */
1885 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06001886 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02001887 if (big_num_opt == 1) {
1888 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001889 parse_options_usage(stat_usage, stat_options, "B", 1);
1890 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001891 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02001892 } else /* Nope, so disable big number formatting */
1893 big_num = false;
1894 } else if (big_num_opt == 0) /* User passed --no-big-num */
1895 big_num = false;
1896
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001897 if (!argc && target__none(&target))
Jiri Olsae0547312015-11-05 15:40:45 +01001898 usage_with_options(stat_usage, stat_options);
David Ahernac3063b2013-09-30 07:37:37 -06001899
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001900 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09001901 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001902 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001903 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001904 } else if (run_count == 0) {
1905 forever = true;
1906 run_count = 1;
1907 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001908
Jiri Olsa421a50f2015-07-21 14:31:22 +02001909 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001910 fprintf(stderr, "The --per-thread option is only available "
1911 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001912 parse_options_usage(NULL, stat_options, "p", 1);
1913 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001914 goto out;
1915 }
1916
1917 /*
1918 * no_aggr, cgroup are for system-wide only
1919 * --per-thread is aggregated per thread, we dont mix it with cpu mode
1920 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02001921 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
1922 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001923 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02001924 fprintf(stderr, "both cgroup and no-aggregation "
1925 "modes only available in system-wide mode\n");
1926
Jiri Olsae0547312015-11-05 15:40:45 +01001927 parse_options_usage(stat_usage, stat_options, "G", 1);
1928 parse_options_usage(NULL, stat_options, "A", 1);
1929 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001930 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001931 }
1932
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001933 if (add_default_attributes())
1934 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001935
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001936 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02001937
Namhyung Kim77a6f012012-05-07 14:09:04 +09001938 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001939 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09001940 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001941 parse_options_usage(stat_usage, stat_options, "p", 1);
1942 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001943 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09001944 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01001945 parse_options_usage(stat_usage, stat_options, "C", 1);
1946 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001947 }
1948 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02001949 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001950
1951 /*
1952 * Initialize thread_map with comm names,
1953 * so we could print it out on output.
1954 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02001955 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001956 thread_map__read_comms(evsel_list->threads);
1957
Stephane Eranian13370a92013-01-29 12:47:44 +01001958 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04001959 if (interval < 10) {
1960 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001961 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04001962 goto out;
1963 } else
1964 pr_warning("print interval < 100ms. "
1965 "The overhead percentage could be high in some cases. "
1966 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01001967 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001968
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001969 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03001970 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001971
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001972 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03001973 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001974
Ingo Molnar58d7e992009-05-15 11:03:23 +02001975 /*
1976 * We dont want to block the signals - that would cause
1977 * child tasks to inherit that and Ctrl-C would not work.
1978 * What we want is for Ctrl-C to work in the exec()-ed
1979 * task, but being ignored by perf stat itself:
1980 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001981 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001982 if (!forever)
1983 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01001984 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02001985 signal(SIGALRM, skip_signal);
1986 signal(SIGABRT, skip_signal);
1987
Ingo Molnar42202dd2009-06-13 14:57:28 +02001988 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001989 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Ingo Molnar42202dd2009-06-13 14:57:28 +02001990 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001991 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
1992 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02001993
Ingo Molnar42202dd2009-06-13 14:57:28 +02001994 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001995 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001996 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02001997 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001998 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02001999 }
2000
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002001 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002002 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002003
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002004 if (STAT_RECORD) {
2005 /*
2006 * We synthesize the kernel mmap record just so that older tools
2007 * don't emit warnings about not being able to resolve symbols
2008 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2009 * a saner message about no samples being in the perf.data file.
2010 *
2011 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002012 * in header.c at the moment 'perf stat record' gets introduced, which
2013 * is not really needed once we start adding the stat specific PERF_RECORD_
2014 * records, but the need to suppress the kptr_restrict messages in older
2015 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002016 */
2017 int fd = perf_data_file__fd(&perf_stat.file);
2018 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2019 process_synthesized_event,
2020 &perf_stat.session->machines.host);
2021 if (err) {
2022 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2023 "older tools may produce warnings about this file\n.");
2024 }
2025
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002026 if (!interval) {
2027 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2028 pr_err("failed to write stat round event\n");
2029 }
2030
Jiri Olsa664c98d2015-11-05 15:40:50 +01002031 if (!perf_stat.file.is_pipe) {
2032 perf_stat.session->header.data_size += perf_stat.bytes_written;
2033 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2034 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002035
2036 perf_session__delete(perf_stat.session);
2037 }
2038
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002039 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002040 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002041out:
2042 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002043 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002044}