blob: 2ffb8221917a8e2d47888eaedc9139a7515f3f34 [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 Olsa6db1a5c2016-01-05 22:09:05 +0100187
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100188 /*
189 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
190 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100191 *
192 * However for pipe sessions we need to keep it zero,
193 * because script's perf_evsel__check_attr is triggered
194 * by attr->sample_type != 0, and we can't run it on
195 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100196 */
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100197 if (!(STAT_RECORD && perf_stat.file.is_pipe))
198 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100199
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100200 /*
201 * Disabling all counters initially, they will be enabled
202 * either manually by us or by kernel via enable_on_exec
203 * set later.
204 */
Jiri Olsac8280cec2015-12-03 10:06:45 +0100205 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100206 attr->disabled = 1;
207
Jiri Olsac8280cec2015-12-03 10:06:45 +0100208 /*
209 * In case of initial_delay we enable tracee
210 * events manually.
211 */
212 if (target__none(&target) && !initial_delay)
213 attr->enable_on_exec = 1;
214 }
215
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300216 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300217 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200218
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300219 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200220}
221
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200222/*
223 * Does the counter have nsecs as a unit?
224 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200225static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200226{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200227 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
228 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200229 return 1;
230
231 return 0;
232}
233
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100234static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100235 union perf_event *event,
236 struct perf_sample *sample __maybe_unused,
237 struct machine *machine __maybe_unused)
238{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100239 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
240 pr_err("failed to write perf data, error: %m\n");
241 return -1;
242 }
243
244 perf_stat.bytes_written += event->header.size;
245 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100246}
247
Jiri Olsa1975d362015-11-05 15:40:56 +0100248static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100249{
Jiri Olsa1975d362015-11-05 15:40:56 +0100250 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100251 process_synthesized_event,
252 NULL);
253}
254
255#define WRITE_STAT_ROUND_EVENT(time, interval) \
256 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
257
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100258#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
259
260static int
261perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
262 struct perf_counts_values *count)
263{
264 struct perf_sample_id *sid = SID(counter, cpu, thread);
265
266 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
267 process_synthesized_event, NULL);
268}
269
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200270/*
271 * Read out the results of a single counter:
272 * do not aggregate counts across CPUs in system-wide mode
273 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200274static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200275{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100276 int nthreads = thread_map__nr(evsel_list->threads);
277 int ncpus = perf_evsel__nr_cpus(counter);
278 int cpu, thread;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200279
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000280 if (!counter->supported)
281 return -ENOENT;
282
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100283 if (counter->system_wide)
284 nthreads = 1;
285
286 for (thread = 0; thread < nthreads; thread++) {
287 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200288 struct perf_counts_values *count;
289
290 count = perf_counts(counter->counts, cpu, thread);
291 if (perf_evsel__read(counter, cpu, thread, count))
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100292 return -1;
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100293
294 if (STAT_RECORD) {
295 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
296 pr_err("failed to write stat event\n");
297 return -1;
298 }
299 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100300 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200301 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200302
303 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200304}
305
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200306static void read_counters(bool close_counters)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200307{
308 struct perf_evsel *counter;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200309
310 evlist__for_each(evsel_list, counter) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200311 if (read_counter(counter))
Andi Kleen245bad82015-09-01 15:52:46 -0700312 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200313
Jiri Olsaf80010e2015-07-21 14:31:27 +0200314 if (perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200315 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200316
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200317 if (close_counters) {
Jiri Olsa106a94a2015-06-26 11:29:19 +0200318 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
319 thread_map__nr(evsel_list->threads));
320 }
321 }
322}
323
Jiri Olsaba411a92015-06-26 11:29:24 +0200324static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100325{
Stephane Eranian13370a92013-01-29 12:47:44 +0100326 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100327
Jiri Olsa106a94a2015-06-26 11:29:19 +0200328 read_counters(false);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100329
Stephane Eranian13370a92013-01-29 12:47:44 +0100330 clock_gettime(CLOCK_MONOTONIC, &ts);
331 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100332
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100333 if (STAT_RECORD) {
334 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSECS_PER_SEC + rs.tv_nsec, INTERVAL))
335 pr_err("failed to write stat round event\n");
336 }
337
Jiri Olsad4f63a42015-06-26 11:29:26 +0200338 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100339}
340
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100341static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700342{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100343 if (initial_delay)
Andi Kleen41191682013-08-02 17:41:11 -0700344 usleep(initial_delay * 1000);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100345
346 /*
347 * We need to enable counters only if:
348 * - we don't have tracee (attaching to task or cpu)
349 * - we have initial delay configured
350 */
351 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100352 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700353}
354
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300355static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300356
357/*
358 * perf_evlist__prepare_workload will send a SIGUSR1
359 * if the fork fails, since we asked by setting its
360 * want_signal to true.
361 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300362static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
363 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300364{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300365 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300366}
367
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100368static bool has_unit(struct perf_evsel *counter)
369{
370 return counter->unit && *counter->unit;
371}
372
373static bool has_scale(struct perf_evsel *counter)
374{
375 return counter->scale != 1;
376}
377
Jiri Olsa664c98d2015-11-05 15:40:50 +0100378static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100379{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100380 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100381 int err;
382
Jiri Olsa664c98d2015-11-05 15:40:50 +0100383 if (is_pipe) {
384 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
385 process_synthesized_event);
386 if (err < 0) {
387 pr_err("Couldn't synthesize attrs.\n");
388 return err;
389 }
390 }
391
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100392 /*
393 * Synthesize other events stuff not carried within
394 * attr event - unit, scale, name
395 */
396 evlist__for_each(evsel_list, counter) {
397 if (!counter->supported)
398 continue;
399
400 /*
401 * Synthesize unit and scale only if it's defined.
402 */
403 if (has_unit(counter)) {
404 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
405 if (err < 0) {
406 pr_err("Couldn't synthesize evsel unit.\n");
407 return err;
408 }
409 }
410
411 if (has_scale(counter)) {
412 err = perf_event__synthesize_event_update_scale(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 if (counter->own_cpus) {
420 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
421 if (err < 0) {
422 pr_err("Couldn't synthesize evsel scale.\n");
423 return err;
424 }
425 }
426
427 /*
428 * Name is needed only for pipe output,
429 * perf.data carries event names.
430 */
431 if (is_pipe) {
432 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
433 if (err < 0) {
434 pr_err("Couldn't synthesize evsel name.\n");
435 return err;
436 }
437 }
438 }
439
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100440 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
441 process_synthesized_event,
442 NULL);
443 if (err < 0) {
444 pr_err("Couldn't synthesize thread map.\n");
445 return err;
446 }
447
448 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
449 process_synthesized_event, NULL);
450 if (err < 0) {
451 pr_err("Couldn't synthesize thread map.\n");
452 return err;
453 }
454
455 err = perf_event__synthesize_stat_config(NULL, &stat_config,
456 process_synthesized_event, NULL);
457 if (err < 0) {
458 pr_err("Couldn't synthesize config.\n");
459 return err;
460 }
461
462 return 0;
463}
464
Jiri Olsa2af46462015-11-05 15:40:49 +0100465#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
466
467static int __store_counter_ids(struct perf_evsel *counter,
468 struct cpu_map *cpus,
469 struct thread_map *threads)
470{
471 int cpu, thread;
472
473 for (cpu = 0; cpu < cpus->nr; cpu++) {
474 for (thread = 0; thread < threads->nr; thread++) {
475 int fd = FD(counter, cpu, thread);
476
477 if (perf_evlist__id_add_fd(evsel_list, counter,
478 cpu, thread, fd) < 0)
479 return -1;
480 }
481 }
482
483 return 0;
484}
485
486static int store_counter_ids(struct perf_evsel *counter)
487{
488 struct cpu_map *cpus = counter->cpus;
489 struct thread_map *threads = counter->threads;
490
491 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
492 return -ENOMEM;
493
494 return __store_counter_ids(counter, cpus, threads);
495}
496
Namhyung Kimacf28922013-03-11 16:43:18 +0900497static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200498{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200499 int interval = stat_config.interval;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300500 char msg[512];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200501 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100502 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100503 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100504 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200505 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300506 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100507 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200508
Stephane Eranian13370a92013-01-29 12:47:44 +0100509 if (interval) {
510 ts.tv_sec = interval / 1000;
511 ts.tv_nsec = (interval % 1000) * 1000000;
512 } else {
513 ts.tv_sec = 1;
514 ts.tv_nsec = 0;
515 }
516
Liming Wang60666c62009-12-31 16:05:50 +0800517 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100518 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300519 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900520 perror("failed to prepare workload");
521 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800522 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900523 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000524 }
525
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200526 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300527 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200528
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300529 evlist__for_each(evsel_list, counter) {
Jiri Olsacac21422012-11-12 18:34:00 +0100530 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600531 /*
532 * PPC returns ENXIO for HW counters until 2.6.37
533 * (behavior changed with commit b0a873e).
534 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100535 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600536 errno == ENOENT || errno == EOPNOTSUPP ||
537 errno == ENXIO) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600538 if (verbose)
539 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300540 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600541 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400542
543 if ((counter->leader != counter) ||
544 !(counter->leader->nr_members > 1))
545 continue;
David Ahernc63ca0c2011-04-29 16:04:15 -0600546 }
Ingo Molnarede70292011-04-28 08:48:42 +0200547
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300548 perf_evsel__open_strerror(counter, &target,
549 errno, msg, sizeof(msg));
550 ui__error("%s\n", msg);
551
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200552 if (child_pid != -1)
553 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600554
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200555 return -1;
556 }
David Ahern2cee77c2011-05-30 08:55:59 -0600557 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100558
559 l = strlen(counter->unit);
560 if (l > unit_width)
561 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100562
563 if (STAT_RECORD && store_counter_ids(counter))
564 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300565 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200566
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300567 if (perf_evlist__apply_filters(evsel_list, &counter)) {
568 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
569 counter->filter, perf_evsel__name(counter), errno,
Masami Hiramatsu759e6122014-08-14 02:22:55 +0000570 strerror_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100571 return -1;
572 }
573
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100574 if (STAT_RECORD) {
575 int err, fd = perf_data_file__fd(&perf_stat.file);
576
Jiri Olsa664c98d2015-11-05 15:40:50 +0100577 if (is_pipe) {
578 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
579 } else {
580 err = perf_session__write_header(perf_stat.session, evsel_list,
581 fd, false);
582 }
583
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100584 if (err < 0)
585 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100586
Jiri Olsa664c98d2015-11-05 15:40:50 +0100587 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100588 if (err < 0)
589 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100590 }
591
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200592 /*
593 * Enable counters and exec the command:
594 */
595 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100596 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200597
Liming Wang60666c62009-12-31 16:05:50 +0800598 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900599 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100600 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900601
Stephane Eranian13370a92013-01-29 12:47:44 +0100602 if (interval) {
603 while (!waitpid(child_pid, &status, WNOHANG)) {
604 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200605 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100606 }
607 }
Liming Wang60666c62009-12-31 16:05:50 +0800608 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300609
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300610 if (workload_exec_errno) {
611 const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
612 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300613 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300614 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300615
Andi Kleen33e49ea2011-09-15 14:31:40 -0700616 if (WIFSIGNALED(status))
617 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800618 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100619 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100620 while (!done) {
621 nanosleep(&ts, NULL);
622 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200623 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100624 }
Liming Wang60666c62009-12-31 16:05:50 +0800625 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200626
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200627 t1 = rdclock();
628
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200629 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200630
Jiri Olsa106a94a2015-06-26 11:29:19 +0200631 read_counters(true);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200632
Ingo Molnar42202dd2009-06-13 14:57:28 +0200633 return WEXITSTATUS(status);
634}
635
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300636static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200637{
638 int ret;
639
640 if (pre_cmd) {
641 ret = system(pre_cmd);
642 if (ret)
643 return ret;
644 }
645
646 if (sync_run)
647 sync();
648
649 ret = __run_perf_stat(argc, argv);
650 if (ret)
651 return ret;
652
653 if (post_cmd) {
654 ret = system(post_cmd);
655 if (ret)
656 return ret;
657 }
658
659 return ret;
660}
661
Andi Kleend73515c2015-03-11 07:16:27 -0700662static void print_running(u64 run, u64 ena)
663{
664 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200665 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700666 csv_sep,
667 run,
668 csv_sep,
669 ena ? 100.0 * run / ena : 100.0);
670 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200671 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700672 }
673}
674
Ingo Molnarf99844c2011-04-27 05:35:39 +0200675static void print_noise_pct(double total, double avg)
676{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800677 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200678
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700679 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200680 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600681 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200682 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200683}
684
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200685static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200686{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200687 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200688
Peter Zijlstra849abde2009-09-04 18:23:38 +0200689 if (run_count == 1)
690 return;
691
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200692 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200693 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200694}
695
Stephane Eranian12c08a92013-02-14 13:57:29 +0100696static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200697{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200698 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100699 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200700 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100701 cpu_map__id_to_socket(id),
702 csv_output ? 0 : -8,
703 cpu_map__id_to_cpu(id),
704 csv_sep,
705 csv_output ? 0 : 4,
706 nr,
707 csv_sep);
708 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100709 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200710 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100711 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100712 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100713 csv_sep,
714 csv_output ? 0 : 4,
715 nr,
716 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100717 break;
718 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200719 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200720 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100721 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100722 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200723 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200724 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200725 csv_output ? 0 : 16,
726 thread_map__comm(evsel->threads, id),
727 csv_output ? 0 : -8,
728 thread_map__pid(evsel->threads, id),
729 csv_sep);
730 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100731 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200732 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100733 default:
734 break;
735 }
736}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200737
Andi Kleen140aead2016-01-30 09:06:49 -0800738struct outstate {
739 FILE *fh;
740 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800741 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800742 int nfields;
Andi Kleen140aead2016-01-30 09:06:49 -0800743};
744
745#define METRIC_LEN 35
746
747static void new_line_std(void *ctx)
748{
749 struct outstate *os = ctx;
750
751 os->newline = true;
752}
753
754static void do_new_line_std(struct outstate *os)
755{
756 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800757 fputs(os->prefix, os->fh);
Andi Kleen140aead2016-01-30 09:06:49 -0800758 if (stat_config.aggr_mode == AGGR_NONE)
759 fprintf(os->fh, " ");
760 if (stat_config.aggr_mode == AGGR_CORE)
761 fprintf(os->fh, " ");
762 if (stat_config.aggr_mode == AGGR_SOCKET)
763 fprintf(os->fh, " ");
764 fprintf(os->fh, " ");
765}
766
767static void print_metric_std(void *ctx, const char *color, const char *fmt,
768 const char *unit, double val)
769{
770 struct outstate *os = ctx;
771 FILE *out = os->fh;
772 int n;
773 bool newline = os->newline;
774
775 os->newline = false;
776
777 if (unit == NULL || fmt == NULL) {
778 fprintf(out, "%-*s", METRIC_LEN, "");
779 return;
780 }
781
782 if (newline)
783 do_new_line_std(os);
784
785 n = fprintf(out, " # ");
786 if (color)
787 n += color_fprintf(out, color, fmt, val);
788 else
789 n += fprintf(out, fmt, val);
790 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
791}
792
Andi Kleen92a61f62016-02-29 14:36:21 -0800793static void new_line_csv(void *ctx)
794{
795 struct outstate *os = ctx;
796 int i;
797
798 fputc('\n', os->fh);
799 if (os->prefix)
800 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
801 for (i = 0; i < os->nfields; i++)
802 fputs(csv_sep, os->fh);
803}
804
805static void print_metric_csv(void *ctx,
806 const char *color __maybe_unused,
807 const char *fmt, const char *unit, double val)
808{
809 struct outstate *os = ctx;
810 FILE *out = os->fh;
811 char buf[64], *vals, *ends;
812
813 if (unit == NULL || fmt == NULL) {
814 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
815 return;
816 }
817 snprintf(buf, sizeof(buf), fmt, val);
818 vals = buf;
819 while (isspace(*vals))
820 vals++;
821 ends = vals;
822 while (isdigit(*ends) || *ends == '.')
823 ends++;
824 *ends = 0;
825 while (isspace(*unit))
826 unit++;
827 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
828}
829
Andi Kleenda88c7f2014-09-24 13:50:46 -0700830static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100831{
Jiri Olsa58215222015-07-21 14:31:24 +0200832 FILE *output = stat_config.output;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100833 double msecs = avg / 1e6;
Stephane Eranian410136f2013-11-12 17:58:49 +0100834 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600835 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100836
Stephane Eranian410136f2013-11-12 17:58:49 +0100837 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
838 fmt_n = csv_output ? "%s" : "%-25s";
839
Andi Kleenda88c7f2014-09-24 13:50:46 -0700840 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100841
David Ahern4bbe5a62013-09-28 14:28:00 -0600842 scnprintf(name, sizeof(name), "%s%s",
843 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +0100844
845 fprintf(output, fmt_v, msecs, csv_sep);
846
847 if (csv_output)
848 fprintf(output, "%s%s", evsel->unit, csv_sep);
849 else
850 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
851
852 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200853
Stephane Eranian023695d2011-02-14 11:20:01 +0200854 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200855 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200856}
857
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200858static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
859{
Jiri Olsa58215222015-07-21 14:31:24 +0200860 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200861 double sc = evsel->scale;
862 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200863
864 if (csv_output) {
865 fmt = sc != 1.0 ? "%.2f%s" : "%.0f%s";
866 } else {
867 if (big_num)
868 fmt = sc != 1.0 ? "%'18.2f%s" : "%'18.0f%s";
869 else
870 fmt = sc != 1.0 ? "%18.2f%s" : "%18.0f%s";
871 }
872
873 aggr_printout(evsel, id, nr);
874
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200875 fprintf(output, fmt, avg, csv_sep);
876
877 if (evsel->unit)
878 fprintf(output, "%-*s%s",
879 csv_output ? 0 : unit_width,
880 evsel->unit, csv_sep);
881
882 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
883
884 if (evsel->cgrp)
885 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -0800886}
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200887
Andi Kleenf9483392016-01-30 09:06:50 -0800888static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Andi Kleencb110f42016-01-30 09:06:51 -0800889 char *prefix, u64 run, u64 ena, double noise)
Andi Kleeneedfcb42015-11-02 17:50:21 -0800890{
Andi Kleen140aead2016-01-30 09:06:49 -0800891 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -0800892 struct outstate os = {
893 .fh = stat_config.output,
894 .prefix = prefix ? prefix : ""
895 };
Andi Kleen140aead2016-01-30 09:06:49 -0800896 print_metric_t pm = print_metric_std;
897 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200898
Andi Kleen140aead2016-01-30 09:06:49 -0800899 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800900
Andi Kleen92a61f62016-02-29 14:36:21 -0800901 if (csv_output) {
902 static int aggr_fields[] = {
903 [AGGR_GLOBAL] = 0,
904 [AGGR_THREAD] = 1,
905 [AGGR_NONE] = 1,
906 [AGGR_SOCKET] = 2,
907 [AGGR_CORE] = 2,
908 };
909
910 pm = print_metric_csv;
911 nl = new_line_csv;
912 os.nfields = 3;
913 os.nfields += aggr_fields[stat_config.aggr_mode];
914 if (counter->cgrp)
915 os.nfields++;
916 }
Andi Kleenb002f3b2016-02-17 14:44:00 -0800917 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleencb110f42016-01-30 09:06:51 -0800918 aggr_printout(counter, id, nr);
919
920 fprintf(stat_config.output, "%*s%s",
921 csv_output ? 0 : 18,
922 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
923 csv_sep);
924
925 fprintf(stat_config.output, "%-*s%s",
926 csv_output ? 0 : unit_width,
927 counter->unit, csv_sep);
928
929 fprintf(stat_config.output, "%*s",
930 csv_output ? 0 : -25,
931 perf_evsel__name(counter));
932
933 if (counter->cgrp)
934 fprintf(stat_config.output, "%s%s",
935 csv_sep, counter->cgrp->name);
936
Andi Kleen92a61f62016-02-29 14:36:21 -0800937 if (!csv_output)
938 pm(&os, NULL, NULL, "", 0);
939 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -0800940 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -0800941 if (csv_output)
942 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -0800943 return;
944 }
945
Andi Kleeneedfcb42015-11-02 17:50:21 -0800946 if (nsec_counter(counter))
947 nsec_printout(id, nr, counter, uval);
948 else
949 abs_printout(id, nr, counter, uval);
950
Andi Kleen140aead2016-01-30 09:06:49 -0800951 out.print_metric = pm;
952 out.new_line = nl;
953 out.ctx = &os;
954
Andi Kleen92a61f62016-02-29 14:36:21 -0800955 if (csv_output) {
956 print_noise(counter, noise);
957 print_running(run, ena);
958 }
959
960 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen140aead2016-01-30 09:06:49 -0800961 stat_config.aggr_mode == AGGR_GLOBAL ? 0 :
962 cpu_map__id_to_cpu(id),
963 &out);
Andi Kleencb110f42016-01-30 09:06:51 -0800964
Andi Kleen92a61f62016-02-29 14:36:21 -0800965 if (!csv_output) {
966 print_noise(counter, noise);
967 print_running(run, ena);
968 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200969}
970
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100971static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100972{
Jiri Olsa58215222015-07-21 14:31:24 +0200973 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100974 struct perf_evsel *counter;
Kan Liang601083c2015-07-02 03:08:43 -0400975 int cpu, s, s2, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +0100976 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100977 u64 ena, run, val;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100978
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100979 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100980 return;
981
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100982 for (s = 0; s < aggr_map->nr; s++) {
983 id = aggr_map->map[s];
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300984 evlist__for_each(evsel_list, counter) {
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100985 val = ena = run = 0;
986 nr = 0;
987 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Kan Liang601083c2015-07-02 03:08:43 -0400988 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100989 if (s2 != id)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100990 continue;
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200991 val += perf_counts(counter->counts, cpu, 0)->val;
992 ena += perf_counts(counter->counts, cpu, 0)->ena;
993 run += perf_counts(counter->counts, cpu, 0)->run;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100994 nr++;
995 }
996 if (prefix)
997 fprintf(output, "%s", prefix);
998
Stephane Eranian410136f2013-11-12 17:58:49 +0100999 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001000 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001001 fputc('\n', output);
1002 }
1003 }
1004}
1005
Jiri Olsa32b8af82015-06-26 11:29:27 +02001006static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1007{
Jiri Olsa58215222015-07-21 14:31:24 +02001008 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001009 int nthreads = thread_map__nr(counter->threads);
1010 int ncpus = cpu_map__nr(counter->cpus);
1011 int cpu, thread;
1012 double uval;
1013
1014 for (thread = 0; thread < nthreads; thread++) {
1015 u64 ena = 0, run = 0, val = 0;
1016
1017 for (cpu = 0; cpu < ncpus; cpu++) {
1018 val += perf_counts(counter->counts, cpu, thread)->val;
1019 ena += perf_counts(counter->counts, cpu, thread)->ena;
1020 run += perf_counts(counter->counts, cpu, thread)->run;
1021 }
1022
1023 if (prefix)
1024 fprintf(output, "%s", prefix);
1025
1026 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001027 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001028 fputc('\n', output);
1029 }
1030}
1031
Ingo Molnar42202dd2009-06-13 14:57:28 +02001032/*
1033 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001034 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001035 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001036static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001037{
Jiri Olsa58215222015-07-21 14:31:24 +02001038 FILE *output = stat_config.output;
Jiri Olsa581cc8a2015-10-16 12:41:03 +02001039 struct perf_stat_evsel *ps = counter->priv;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001040 double avg = avg_stats(&ps->res_stats[0]);
Stephane Eranian410136f2013-11-12 17:58:49 +01001041 double uval;
Andi Kleend73515c2015-03-11 07:16:27 -07001042 double avg_enabled, avg_running;
1043
1044 avg_enabled = avg_stats(&ps->res_stats[1]);
1045 avg_running = avg_stats(&ps->res_stats[2]);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001046
Stephane Eranian13370a92013-01-29 12:47:44 +01001047 if (prefix)
1048 fprintf(output, "%s", prefix);
1049
Stephane Eranian410136f2013-11-12 17:58:49 +01001050 uval = avg * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001051 printout(-1, 0, counter, uval, prefix, avg_running, avg_enabled, avg);
Stephane Eranian4aa90152011-08-15 22:22:33 +02001052 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001053}
1054
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001055/*
1056 * Print out the results of a single counter:
1057 * does not use aggregated count in system-wide
1058 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001059static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001060{
Jiri Olsa58215222015-07-21 14:31:24 +02001061 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001062 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001063 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001064 int cpu;
1065
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001066 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001067 val = perf_counts(counter->counts, cpu, 0)->val;
1068 ena = perf_counts(counter->counts, cpu, 0)->ena;
1069 run = perf_counts(counter->counts, cpu, 0)->run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001070
1071 if (prefix)
1072 fprintf(output, "%s", prefix);
1073
Stephane Eranian410136f2013-11-12 17:58:49 +01001074 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001075 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001076
Stephane Eranian4aa90152011-08-15 22:22:33 +02001077 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001078 }
1079}
1080
Jiri Olsad4f63a42015-06-26 11:29:26 +02001081static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001082{
Jiri Olsa58215222015-07-21 14:31:24 +02001083 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001084 static int num_print_interval;
1085
1086 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1087
1088 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001089 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001090 case AGGR_SOCKET:
1091 fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit");
1092 break;
1093 case AGGR_CORE:
1094 fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit");
1095 break;
1096 case AGGR_NONE:
1097 fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit");
1098 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001099 case AGGR_THREAD:
1100 fprintf(output, "# time comm-pid counts %*s events\n", unit_width, "unit");
1101 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001102 case AGGR_GLOBAL:
1103 default:
1104 fprintf(output, "# time counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001105 case AGGR_UNSET:
1106 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001107 }
1108 }
1109
1110 if (++num_print_interval == 25)
1111 num_print_interval = 0;
1112}
1113
1114static void print_header(int argc, const char **argv)
1115{
Jiri Olsa58215222015-07-21 14:31:24 +02001116 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001117 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001118
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001119 fflush(stdout);
1120
Stephane Eraniand7470b62010-12-01 18:49:05 +02001121 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001122 fprintf(output, "\n");
1123 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001124 if (target.system_wide)
1125 fprintf(output, "\'system wide");
1126 else if (target.cpu_list)
1127 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001128 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001129 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1130 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001131 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001132 } else if (target.pid)
1133 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001134 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001135 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001136
Stephane Eranian4aa90152011-08-15 22:22:33 +02001137 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001138 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001139 fprintf(output, " (%d runs)", run_count);
1140 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001141 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001142}
1143
1144static void print_footer(void)
1145{
Jiri Olsa58215222015-07-21 14:31:24 +02001146 FILE *output = stat_config.output;
1147
Jiri Olsad4f63a42015-06-26 11:29:26 +02001148 if (!null_run)
1149 fprintf(output, "\n");
1150 fprintf(output, " %17.9f seconds time elapsed",
1151 avg_stats(&walltime_nsecs_stats)/1e9);
1152 if (run_count > 1) {
1153 fprintf(output, " ");
1154 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1155 avg_stats(&walltime_nsecs_stats));
1156 }
1157 fprintf(output, "\n\n");
1158}
1159
1160static void print_counters(struct timespec *ts, int argc, const char **argv)
1161{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001162 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001163 struct perf_evsel *counter;
1164 char buf[64], *prefix = NULL;
1165
Jiri Olsa664c98d2015-11-05 15:40:50 +01001166 /* Do not print anything if we record to the pipe. */
1167 if (STAT_RECORD && perf_stat.file.is_pipe)
1168 return;
1169
Jiri Olsad4f63a42015-06-26 11:29:26 +02001170 if (interval)
1171 print_interval(prefix = buf, ts);
1172 else
1173 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001174
Jiri Olsa421a50f2015-07-21 14:31:22 +02001175 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001176 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001177 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001178 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001179 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001180 case AGGR_THREAD:
1181 evlist__for_each(evsel_list, counter)
1182 print_aggr_thread(counter, prefix);
1183 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001184 case AGGR_GLOBAL:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001185 evlist__for_each(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001186 print_counter_aggr(counter, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001187 break;
1188 case AGGR_NONE:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001189 evlist__for_each(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001190 print_counter(counter, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001191 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001192 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001193 default:
1194 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001195 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001196
Jiri Olsad4f63a42015-06-26 11:29:26 +02001197 if (!interval && !csv_output)
1198 print_footer();
1199
Jiri Olsa58215222015-07-21 14:31:24 +02001200 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001201}
1202
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001203static volatile int signr = -1;
1204
Ingo Molnar52425192009-05-26 09:17:18 +02001205static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001206{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001207 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001208 done = 1;
1209
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001210 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001211 /*
1212 * render child_pid harmless
1213 * won't send SIGTERM to a random
1214 * process in case of race condition
1215 * and fast PID recycling
1216 */
1217 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001218}
1219
1220static void sig_atexit(void)
1221{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001222 sigset_t set, oset;
1223
1224 /*
1225 * avoid race condition with SIGCHLD handler
1226 * in skip_signal() which is modifying child_pid
1227 * goal is to avoid send SIGTERM to a random
1228 * process
1229 */
1230 sigemptyset(&set);
1231 sigaddset(&set, SIGCHLD);
1232 sigprocmask(SIG_BLOCK, &set, &oset);
1233
Chris Wilson933da832009-10-04 01:35:01 +01001234 if (child_pid != -1)
1235 kill(child_pid, SIGTERM);
1236
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001237 sigprocmask(SIG_SETMASK, &oset, NULL);
1238
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001239 if (signr == -1)
1240 return;
1241
1242 signal(signr, SIG_DFL);
1243 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001244}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001245
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001246static int stat__set_big_num(const struct option *opt __maybe_unused,
1247 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001248{
1249 big_num_opt = unset ? 0 : 1;
1250 return 0;
1251}
1252
Jiri Olsae0547312015-11-05 15:40:45 +01001253static const struct option stat_options[] = {
1254 OPT_BOOLEAN('T', "transaction", &transaction_run,
1255 "hardware transaction statistics"),
1256 OPT_CALLBACK('e', "event", &evsel_list, "event",
1257 "event selector. use 'perf list' to list available events",
1258 parse_events_option),
1259 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1260 "event filter", parse_filter),
1261 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1262 "child tasks do not inherit counters"),
1263 OPT_STRING('p', "pid", &target.pid, "pid",
1264 "stat events on existing process id"),
1265 OPT_STRING('t', "tid", &target.tid, "tid",
1266 "stat events on existing thread id"),
1267 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1268 "system-wide collection from all CPUs"),
1269 OPT_BOOLEAN('g', "group", &group,
1270 "put the counters into a counter group"),
1271 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1272 OPT_INCR('v', "verbose", &verbose,
1273 "be more verbose (show counter open errors, etc)"),
1274 OPT_INTEGER('r', "repeat", &run_count,
1275 "repeat command and print average + stddev (max: 100, forever: 0)"),
1276 OPT_BOOLEAN('n', "null", &null_run,
1277 "null run - dont start any counters"),
1278 OPT_INCR('d', "detailed", &detailed_run,
1279 "detailed run - start a lot of events"),
1280 OPT_BOOLEAN('S', "sync", &sync_run,
1281 "call sync() before starting a run"),
1282 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1283 "print large numbers with thousands\' separators",
1284 stat__set_big_num),
1285 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1286 "list of cpus to monitor in system-wide"),
1287 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1288 "disable CPU count aggregation", AGGR_NONE),
1289 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1290 "print counts with custom separator"),
1291 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1292 "monitor event in cgroup name only", parse_cgroups),
1293 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1294 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1295 OPT_INTEGER(0, "log-fd", &output_fd,
1296 "log output to fd, instead of stderr"),
1297 OPT_STRING(0, "pre", &pre_cmd, "command",
1298 "command to run prior to the measured command"),
1299 OPT_STRING(0, "post", &post_cmd, "command",
1300 "command to run after to the measured command"),
1301 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1302 "print counts at regular interval in ms (>= 10)"),
1303 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1304 "aggregate counts per processor socket", AGGR_SOCKET),
1305 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1306 "aggregate counts per physical processor core", AGGR_CORE),
1307 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1308 "aggregate counts per thread", AGGR_THREAD),
1309 OPT_UINTEGER('D', "delay", &initial_delay,
1310 "ms to wait before starting measurement after program start"),
1311 OPT_END()
1312};
1313
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001314static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1315{
1316 return cpu_map__get_socket(map, cpu, NULL);
1317}
1318
1319static int perf_stat__get_core(struct cpu_map *map, int cpu)
1320{
1321 return cpu_map__get_core(map, cpu, NULL);
1322}
1323
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001324static int cpu_map__get_max(struct cpu_map *map)
1325{
1326 int i, max = -1;
1327
1328 for (i = 0; i < map->nr; i++) {
1329 if (map->map[i] > max)
1330 max = map->map[i];
1331 }
1332
1333 return max;
1334}
1335
1336static struct cpu_map *cpus_aggr_map;
1337
1338static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1339{
1340 int cpu;
1341
1342 if (idx >= map->nr)
1343 return -1;
1344
1345 cpu = map->map[idx];
1346
1347 if (cpus_aggr_map->map[cpu] == -1)
1348 cpus_aggr_map->map[cpu] = get_id(map, idx);
1349
1350 return cpus_aggr_map->map[cpu];
1351}
1352
1353static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1354{
1355 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1356}
1357
1358static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1359{
1360 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1361}
1362
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001363static int perf_stat_init_aggr_mode(void)
1364{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001365 int nr;
1366
Jiri Olsa421a50f2015-07-21 14:31:22 +02001367 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001368 case AGGR_SOCKET:
1369 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1370 perror("cannot build socket map");
1371 return -1;
1372 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001373 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001374 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001375 case AGGR_CORE:
1376 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1377 perror("cannot build core map");
1378 return -1;
1379 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001380 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001381 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001382 case AGGR_NONE:
1383 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001384 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001385 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001386 default:
1387 break;
1388 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001389
1390 /*
1391 * The evsel_list->cpus is the base we operate on,
1392 * taking the highest cpu number to be the size of
1393 * the aggregation translate cpumap.
1394 */
1395 nr = cpu_map__get_max(evsel_list->cpus);
1396 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1397 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001398}
1399
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001400static void perf_stat__exit_aggr_mode(void)
1401{
1402 cpu_map__put(aggr_map);
1403 cpu_map__put(cpus_aggr_map);
1404 aggr_map = NULL;
1405 cpus_aggr_map = NULL;
1406}
1407
Jiri Olsa68d702f2015-11-05 15:40:58 +01001408static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1409{
1410 int cpu;
1411
1412 if (idx > map->nr)
1413 return -1;
1414
1415 cpu = map->map[idx];
1416
1417 if (cpu >= env->nr_cpus_online)
1418 return -1;
1419
1420 return cpu;
1421}
1422
1423static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1424{
1425 struct perf_env *env = data;
1426 int cpu = perf_env__get_cpu(env, map, idx);
1427
1428 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1429}
1430
1431static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1432{
1433 struct perf_env *env = data;
1434 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1435
1436 if (cpu != -1) {
1437 int socket_id = env->cpu[cpu].socket_id;
1438
1439 /*
1440 * Encode socket in upper 16 bits
1441 * core_id is relative to socket, and
1442 * we need a global id. So we combine
1443 * socket + core id.
1444 */
1445 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1446 }
1447
1448 return core;
1449}
1450
1451static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1452 struct cpu_map **sockp)
1453{
1454 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1455}
1456
1457static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1458 struct cpu_map **corep)
1459{
1460 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1461}
1462
1463static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1464{
1465 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1466}
1467
1468static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1469{
1470 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1471}
1472
1473static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1474{
1475 struct perf_env *env = &st->session->header.env;
1476
1477 switch (stat_config.aggr_mode) {
1478 case AGGR_SOCKET:
1479 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1480 perror("cannot build socket map");
1481 return -1;
1482 }
1483 aggr_get_id = perf_stat__get_socket_file;
1484 break;
1485 case AGGR_CORE:
1486 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1487 perror("cannot build core map");
1488 return -1;
1489 }
1490 aggr_get_id = perf_stat__get_core_file;
1491 break;
1492 case AGGR_NONE:
1493 case AGGR_GLOBAL:
1494 case AGGR_THREAD:
1495 case AGGR_UNSET:
1496 default:
1497 break;
1498 }
1499
1500 return 0;
1501}
1502
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001503/*
1504 * Add default attributes, if there were no attributes specified or
1505 * if -d/--detailed, -d -d or -d -d -d is used:
1506 */
1507static int add_default_attributes(void)
1508{
Andi Kleen9dec4472016-02-26 16:27:56 -08001509 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001510
1511 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1512 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1513 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1514 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1515
1516 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08001517};
1518 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001519 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001520};
1521 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001522 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001523};
1524 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001525 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1526 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1527 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1528
1529};
1530
1531/*
1532 * Detailed stats (-d), covering the L1 and last level data caches:
1533 */
1534 struct perf_event_attr detailed_attrs[] = {
1535
1536 { .type = PERF_TYPE_HW_CACHE,
1537 .config =
1538 PERF_COUNT_HW_CACHE_L1D << 0 |
1539 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1540 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1541
1542 { .type = PERF_TYPE_HW_CACHE,
1543 .config =
1544 PERF_COUNT_HW_CACHE_L1D << 0 |
1545 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1546 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1547
1548 { .type = PERF_TYPE_HW_CACHE,
1549 .config =
1550 PERF_COUNT_HW_CACHE_LL << 0 |
1551 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1552 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1553
1554 { .type = PERF_TYPE_HW_CACHE,
1555 .config =
1556 PERF_COUNT_HW_CACHE_LL << 0 |
1557 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1558 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1559};
1560
1561/*
1562 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1563 */
1564 struct perf_event_attr very_detailed_attrs[] = {
1565
1566 { .type = PERF_TYPE_HW_CACHE,
1567 .config =
1568 PERF_COUNT_HW_CACHE_L1I << 0 |
1569 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1570 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1571
1572 { .type = PERF_TYPE_HW_CACHE,
1573 .config =
1574 PERF_COUNT_HW_CACHE_L1I << 0 |
1575 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1576 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1577
1578 { .type = PERF_TYPE_HW_CACHE,
1579 .config =
1580 PERF_COUNT_HW_CACHE_DTLB << 0 |
1581 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1582 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1583
1584 { .type = PERF_TYPE_HW_CACHE,
1585 .config =
1586 PERF_COUNT_HW_CACHE_DTLB << 0 |
1587 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1588 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1589
1590 { .type = PERF_TYPE_HW_CACHE,
1591 .config =
1592 PERF_COUNT_HW_CACHE_ITLB << 0 |
1593 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1594 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1595
1596 { .type = PERF_TYPE_HW_CACHE,
1597 .config =
1598 PERF_COUNT_HW_CACHE_ITLB << 0 |
1599 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1600 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1601
1602};
1603
1604/*
1605 * Very, very detailed stats (-d -d -d), adding prefetch events:
1606 */
1607 struct perf_event_attr very_very_detailed_attrs[] = {
1608
1609 { .type = PERF_TYPE_HW_CACHE,
1610 .config =
1611 PERF_COUNT_HW_CACHE_L1D << 0 |
1612 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1613 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1614
1615 { .type = PERF_TYPE_HW_CACHE,
1616 .config =
1617 PERF_COUNT_HW_CACHE_L1D << 0 |
1618 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1619 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1620};
1621
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001622 /* Set attrs if no event is selected and !null_run: */
1623 if (null_run)
1624 return 0;
1625
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001626 if (transaction_run) {
1627 int err;
1628 if (pmu_have_event("cpu", "cycles-ct") &&
1629 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02001630 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001631 else
Jiri Olsaa4547422015-06-03 16:25:53 +02001632 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
1633 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001634 fprintf(stderr, "Cannot set up transaction events\n");
1635 return -1;
1636 }
1637 return 0;
1638 }
1639
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001640 if (!evsel_list->nr_entries) {
Andi Kleen9dec4472016-02-26 16:27:56 -08001641 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
1642 return -1;
1643 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
1644 if (perf_evlist__add_default_attrs(evsel_list,
1645 frontend_attrs) < 0)
1646 return -1;
1647 }
1648 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
1649 if (perf_evlist__add_default_attrs(evsel_list,
1650 backend_attrs) < 0)
1651 return -1;
1652 }
1653 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001654 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001655 }
1656
1657 /* Detailed events get appended to the event list: */
1658
1659 if (detailed_run < 1)
1660 return 0;
1661
1662 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001663 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001664 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001665
1666 if (detailed_run < 2)
1667 return 0;
1668
1669 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001670 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001671 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001672
1673 if (detailed_run < 3)
1674 return 0;
1675
1676 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001677 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001678}
1679
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01001680static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001681 "perf stat record [<options>]",
1682 NULL,
1683};
1684
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001685static void init_features(struct perf_session *session)
1686{
1687 int feat;
1688
1689 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
1690 perf_header__set_feat(&session->header, feat);
1691
1692 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
1693 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
1694 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1695 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1696}
1697
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001698static int __cmd_record(int argc, const char **argv)
1699{
1700 struct perf_session *session;
1701 struct perf_data_file *file = &perf_stat.file;
1702
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01001703 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001704 PARSE_OPT_STOP_AT_NON_OPTION);
1705
1706 if (output_name)
1707 file->path = output_name;
1708
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01001709 if (run_count != 1 || forever) {
1710 pr_err("Cannot use -r option with perf stat record.\n");
1711 return -1;
1712 }
1713
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001714 session = perf_session__new(file, false, NULL);
1715 if (session == NULL) {
1716 pr_err("Perf session creation failed.\n");
1717 return -1;
1718 }
1719
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001720 init_features(session);
1721
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001722 session->evlist = evsel_list;
1723 perf_stat.session = session;
1724 perf_stat.record = true;
1725 return argc;
1726}
1727
Jiri Olsaa56f9392015-11-05 15:40:59 +01001728static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
1729 union perf_event *event,
1730 struct perf_session *session)
1731{
1732 struct stat_round_event *round = &event->stat_round;
1733 struct perf_evsel *counter;
1734 struct timespec tsh, *ts = NULL;
1735 const char **argv = session->header.env.cmdline_argv;
1736 int argc = session->header.env.nr_cmdline;
1737
1738 evlist__for_each(evsel_list, counter)
1739 perf_stat_process_counter(&stat_config, counter);
1740
1741 if (round->type == PERF_STAT_ROUND_TYPE__FINAL)
1742 update_stats(&walltime_nsecs_stats, round->time);
1743
1744 if (stat_config.interval && round->time) {
1745 tsh.tv_sec = round->time / NSECS_PER_SEC;
1746 tsh.tv_nsec = round->time % NSECS_PER_SEC;
1747 ts = &tsh;
1748 }
1749
1750 print_counters(ts, argc, argv);
1751 return 0;
1752}
1753
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01001754static
1755int process_stat_config_event(struct perf_tool *tool __maybe_unused,
1756 union perf_event *event,
1757 struct perf_session *session __maybe_unused)
1758{
Jiri Olsa68d702f2015-11-05 15:40:58 +01001759 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
1760
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01001761 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01001762
Jiri Olsa89af4e02015-11-05 15:41:02 +01001763 if (cpu_map__empty(st->cpus)) {
1764 if (st->aggr_mode != AGGR_UNSET)
1765 pr_warning("warning: processing task data, aggregation mode not set\n");
1766 return 0;
1767 }
1768
1769 if (st->aggr_mode != AGGR_UNSET)
1770 stat_config.aggr_mode = st->aggr_mode;
1771
Jiri Olsa68d702f2015-11-05 15:40:58 +01001772 if (perf_stat.file.is_pipe)
1773 perf_stat_init_aggr_mode();
1774 else
1775 perf_stat_init_aggr_mode_file(st);
1776
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01001777 return 0;
1778}
1779
Jiri Olsa1975d362015-11-05 15:40:56 +01001780static int set_maps(struct perf_stat *st)
1781{
1782 if (!st->cpus || !st->threads)
1783 return 0;
1784
1785 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
1786 return -EINVAL;
1787
1788 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
1789
1790 if (perf_evlist__alloc_stats(evsel_list, true))
1791 return -ENOMEM;
1792
1793 st->maps_allocated = true;
1794 return 0;
1795}
1796
1797static
1798int process_thread_map_event(struct perf_tool *tool __maybe_unused,
1799 union perf_event *event,
1800 struct perf_session *session __maybe_unused)
1801{
1802 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
1803
1804 if (st->threads) {
1805 pr_warning("Extra thread map event, ignoring.\n");
1806 return 0;
1807 }
1808
1809 st->threads = thread_map__new_event(&event->thread_map);
1810 if (!st->threads)
1811 return -ENOMEM;
1812
1813 return set_maps(st);
1814}
1815
1816static
1817int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
1818 union perf_event *event,
1819 struct perf_session *session __maybe_unused)
1820{
1821 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
1822 struct cpu_map *cpus;
1823
1824 if (st->cpus) {
1825 pr_warning("Extra cpu map event, ignoring.\n");
1826 return 0;
1827 }
1828
1829 cpus = cpu_map__new_data(&event->cpu_map.data);
1830 if (!cpus)
1831 return -ENOMEM;
1832
1833 st->cpus = cpus;
1834 return set_maps(st);
1835}
1836
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01001837static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001838 "perf stat report [<options>]",
1839 NULL,
1840};
1841
1842static struct perf_stat perf_stat = {
1843 .tool = {
1844 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01001845 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01001846 .thread_map = process_thread_map_event,
1847 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01001848 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01001849 .stat = perf_event__process_stat_event,
1850 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01001851 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01001852 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01001853};
1854
1855static int __cmd_report(int argc, const char **argv)
1856{
1857 struct perf_session *session;
1858 const struct option options[] = {
1859 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01001860 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
1861 "aggregate counts per processor socket", AGGR_SOCKET),
1862 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
1863 "aggregate counts per physical processor core", AGGR_CORE),
1864 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
1865 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01001866 OPT_END()
1867 };
1868 struct stat st;
1869 int ret;
1870
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01001871 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01001872
1873 if (!input_name || !strlen(input_name)) {
1874 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
1875 input_name = "-";
1876 else
1877 input_name = "perf.data";
1878 }
1879
1880 perf_stat.file.path = input_name;
1881 perf_stat.file.mode = PERF_DATA_MODE_READ;
1882
1883 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
1884 if (session == NULL)
1885 return -1;
1886
1887 perf_stat.session = session;
1888 stat_config.output = stderr;
1889 evsel_list = session->evlist;
1890
1891 ret = perf_session__process_events(session);
1892 if (ret)
1893 return ret;
1894
1895 perf_session__delete(session);
1896 return 0;
1897}
1898
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001899int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02001900{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001901 const char * const stat_usage[] = {
1902 "perf stat [<options>] [<command>]",
1903 NULL
1904 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09001905 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001906 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02001907 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001908 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01001909 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02001910
Stephane Eranian5af52b52010-05-18 15:00:01 +02001911 setlocale(LC_ALL, "");
1912
Namhyung Kim334fe7a2013-03-11 16:43:12 +09001913 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001914 if (evsel_list == NULL)
1915 return -ENOMEM;
1916
Wang Nan1669e502016-02-19 11:43:58 +00001917 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001918 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
1919 (const char **) stat_usage,
1920 PARSE_OPT_STOP_AT_NON_OPTION);
1921
Jiri Olsa6edb78a2015-11-05 15:41:01 +01001922 if (csv_sep) {
1923 csv_output = true;
1924 if (!strcmp(csv_sep, "\\t"))
1925 csv_sep = "\t";
1926 } else
1927 csv_sep = DEFAULT_SEPARATOR;
1928
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001929 if (argc && !strncmp(argv[0], "rec", 3)) {
1930 argc = __cmd_record(argc, argv);
1931 if (argc < 0)
1932 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01001933 } else if (argc && !strncmp(argv[0], "rep", 3))
1934 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001935
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001936 interval = stat_config.interval;
1937
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001938 /*
1939 * For record command the -o is already taken care of.
1940 */
1941 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02001942 output = NULL;
1943
Jim Cromie56f3bae2011-09-07 17:14:00 -06001944 if (output_name && output_fd) {
1945 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001946 parse_options_usage(stat_usage, stat_options, "o", 1);
1947 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001948 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06001949 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001950
1951 if (output_fd < 0) {
1952 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001953 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001954 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001955 }
1956
Stephane Eranian4aa90152011-08-15 22:22:33 +02001957 if (!output) {
1958 struct timespec tm;
1959 mode = append_file ? "a" : "w";
1960
1961 output = fopen(output_name, mode);
1962 if (!output) {
1963 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06001964 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001965 }
1966 clock_gettime(CLOCK_REALTIME, &tm);
1967 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001968 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06001969 mode = append_file ? "a" : "w";
1970 output = fdopen(output_fd, mode);
1971 if (!output) {
1972 perror("Failed opening logfd");
1973 return -errno;
1974 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001975 }
1976
Jiri Olsa58215222015-07-21 14:31:24 +02001977 stat_config.output = output;
1978
Stephane Eraniand7470b62010-12-01 18:49:05 +02001979 /*
1980 * let the spreadsheet do the pretty-printing
1981 */
1982 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06001983 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02001984 if (big_num_opt == 1) {
1985 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001986 parse_options_usage(stat_usage, stat_options, "B", 1);
1987 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001988 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02001989 } else /* Nope, so disable big number formatting */
1990 big_num = false;
1991 } else if (big_num_opt == 0) /* User passed --no-big-num */
1992 big_num = false;
1993
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001994 if (!argc && target__none(&target))
Jiri Olsae0547312015-11-05 15:40:45 +01001995 usage_with_options(stat_usage, stat_options);
David Ahernac3063b2013-09-30 07:37:37 -06001996
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001997 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09001998 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001999 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002000 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002001 } else if (run_count == 0) {
2002 forever = true;
2003 run_count = 1;
2004 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002005
Jiri Olsa421a50f2015-07-21 14:31:22 +02002006 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002007 fprintf(stderr, "The --per-thread option is only available "
2008 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002009 parse_options_usage(NULL, stat_options, "p", 1);
2010 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002011 goto out;
2012 }
2013
2014 /*
2015 * no_aggr, cgroup are for system-wide only
2016 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2017 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002018 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2019 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002020 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002021 fprintf(stderr, "both cgroup and no-aggregation "
2022 "modes only available in system-wide mode\n");
2023
Jiri Olsae0547312015-11-05 15:40:45 +01002024 parse_options_usage(stat_usage, stat_options, "G", 1);
2025 parse_options_usage(NULL, stat_options, "A", 1);
2026 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002027 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002028 }
2029
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002030 if (add_default_attributes())
2031 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002032
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002033 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002034
Namhyung Kim77a6f012012-05-07 14:09:04 +09002035 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002036 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002037 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002038 parse_options_usage(stat_usage, stat_options, "p", 1);
2039 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002040 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002041 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002042 parse_options_usage(stat_usage, stat_options, "C", 1);
2043 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002044 }
2045 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002046 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002047
2048 /*
2049 * Initialize thread_map with comm names,
2050 * so we could print it out on output.
2051 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002052 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02002053 thread_map__read_comms(evsel_list->threads);
2054
Stephane Eranian13370a92013-01-29 12:47:44 +01002055 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002056 if (interval < 10) {
2057 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002058 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002059 goto out;
2060 } else
2061 pr_warning("print interval < 100ms. "
2062 "The overhead percentage could be high in some cases. "
2063 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002064 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002065
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002066 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002067 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002068
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002069 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002070 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002071
Ingo Molnar58d7e992009-05-15 11:03:23 +02002072 /*
2073 * We dont want to block the signals - that would cause
2074 * child tasks to inherit that and Ctrl-C would not work.
2075 * What we want is for Ctrl-C to work in the exec()-ed
2076 * task, but being ignored by perf stat itself:
2077 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002078 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002079 if (!forever)
2080 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002081 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002082 signal(SIGALRM, skip_signal);
2083 signal(SIGABRT, skip_signal);
2084
Ingo Molnar42202dd2009-06-13 14:57:28 +02002085 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002086 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Ingo Molnar42202dd2009-06-13 14:57:28 +02002087 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002088 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2089 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002090
Ingo Molnar42202dd2009-06-13 14:57:28 +02002091 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002092 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002093 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002094 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002095 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002096 }
2097
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002098 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002099 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002100
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002101 if (STAT_RECORD) {
2102 /*
2103 * We synthesize the kernel mmap record just so that older tools
2104 * don't emit warnings about not being able to resolve symbols
2105 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2106 * a saner message about no samples being in the perf.data file.
2107 *
2108 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002109 * in header.c at the moment 'perf stat record' gets introduced, which
2110 * is not really needed once we start adding the stat specific PERF_RECORD_
2111 * records, but the need to suppress the kptr_restrict messages in older
2112 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002113 */
2114 int fd = perf_data_file__fd(&perf_stat.file);
2115 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2116 process_synthesized_event,
2117 &perf_stat.session->machines.host);
2118 if (err) {
2119 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2120 "older tools may produce warnings about this file\n.");
2121 }
2122
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002123 if (!interval) {
2124 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2125 pr_err("failed to write stat round event\n");
2126 }
2127
Jiri Olsa664c98d2015-11-05 15:40:50 +01002128 if (!perf_stat.file.is_pipe) {
2129 perf_stat.session->header.data_size += perf_stat.bytes_written;
2130 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2131 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002132
2133 perf_session__delete(perf_stat.session);
2134 }
2135
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002136 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002137 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002138out:
2139 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002140 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002141}