blob: 45bf4d2caebe981a741b39f24b5906e43950d71f [file] [log] [blame]
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-stat.c
3 *
4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
6 *
7 * Sample output:
Ingo Molnarddcacfa2009-04-20 15:37:32 +02008
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02009 $ perf stat ./hackbench 10
Ingo Molnarddcacfa2009-04-20 15:37:32 +020010
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020011 Time: 0.118
Ingo Molnarddcacfa2009-04-20 15:37:32 +020012
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020013 Performance counter stats for './hackbench 10':
Ingo Molnarddcacfa2009-04-20 15:37:32 +020014
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020015 1708.761321 task-clock # 11.037 CPUs utilized
16 41,190 context-switches # 0.024 M/sec
17 6,735 CPU-migrations # 0.004 M/sec
18 17,318 page-faults # 0.010 M/sec
19 5,205,202,243 cycles # 3.046 GHz
20 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
21 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
22 2,603,501,247 instructions # 0.50 insns per cycle
23 # 1.48 stalled cycles per insn
24 484,357,498 branches # 283.455 M/sec
25 6,388,934 branch-misses # 1.32% of all branches
26
27 0.154822978 seconds time elapsed
Ingo Molnarddcacfa2009-04-20 15:37:32 +020028
Ingo Molnar52425192009-05-26 09:17:18 +020029 *
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020030 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
Ingo Molnar52425192009-05-26 09:17:18 +020031 *
32 * Improvements and fixes by:
33 *
34 * Arjan van de Ven <arjan@linux.intel.com>
35 * Yanmin Zhang <yanmin.zhang@intel.com>
36 * Wu Fengguang <fengguang.wu@intel.com>
37 * Mike Galbraith <efault@gmx.de>
38 * Paul Mackerras <paulus@samba.org>
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +053039 * Jaswinder Singh Rajput <jaswinder@kernel.org>
Ingo Molnar52425192009-05-26 09:17:18 +020040 *
41 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnarddcacfa2009-04-20 15:37:32 +020042 */
43
Peter Zijlstra1a482f32009-05-23 18:28:58 +020044#include "perf.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +020045#include "builtin.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030046#include "util/cgroup.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020047#include "util/util.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060048#include <subcmd/parse-options.h>
Ingo Molnar52425192009-05-26 09:17:18 +020049#include "util/parse-events.h"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070050#include "util/pmu.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020051#include "util/event.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020052#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020053#include "util/evsel.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020054#include "util/debug.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020055#include "util/color.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080056#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080057#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110058#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030059#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020060#include "util/thread_map.h"
Jiri Olsad8095602015-08-07 12:51:03 +020061#include "util/counts.h"
Jiri Olsa4979d0c2015-11-05 15:40:46 +010062#include "util/session.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020063
Peter Zijlstra1f16c572012-10-23 13:40:14 +020064#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020065#include <sys/prctl.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020066#include <locale.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020067
Stephane Eraniand7470b62010-12-01 18:49:05 +020068#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060069#define CNTR_NOT_SUPPORTED "<not supported>"
70#define CNTR_NOT_COUNTED "<not counted>"
Stephane Eraniand7470b62010-12-01 18:49:05 +020071
Jiri Olsad4f63a42015-06-26 11:29:26 +020072static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010073
Andi Kleen4cabc3d2013-08-21 16:47:26 -070074/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020075static const char *transaction_attrs = {
76 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070077 "{"
78 "instructions,"
79 "cycles,"
80 "cpu/cycles-t/,"
81 "cpu/tx-start/,"
82 "cpu/el-start/,"
83 "cpu/cycles-ct/"
84 "}"
85};
86
87/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +020088static const char * transaction_limited_attrs = {
89 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070090 "{"
91 "instructions,"
92 "cycles,"
93 "cpu/cycles-t/,"
94 "cpu/tx-start/"
95 "}"
96};
97
Robert Richter666e6d42012-04-05 18:26:27 +020098static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020099
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300100static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900101 .uid = UINT_MAX,
102};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530103
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100104typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
105
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530106static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200107static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200108static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000109static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200110static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700111static bool transaction_run;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200112static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200113static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200114static const char *csv_sep = NULL;
115static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800116static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200117static const char *pre_cmd = NULL;
118static const char *post_cmd = NULL;
119static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700120static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100121static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500122static bool forever = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100123static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100124static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100125static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100126static bool append_file;
127static const char *output_name;
128static int output_fd;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200129
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100130struct perf_stat {
131 bool record;
132 struct perf_data_file file;
133 struct perf_session *session;
134 u64 bytes_written;
135};
136
137static struct perf_stat perf_stat;
138#define STAT_RECORD perf_stat.record
139
Liming Wang60666c62009-12-31 16:05:50 +0800140static volatile int done = 0;
141
Jiri Olsa421a50f2015-07-21 14:31:22 +0200142static struct perf_stat_config stat_config = {
143 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200144 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200145};
146
Stephane Eranian13370a92013-01-29 12:47:44 +0100147static inline void diff_timespec(struct timespec *r, struct timespec *a,
148 struct timespec *b)
149{
150 r->tv_sec = a->tv_sec - b->tv_sec;
151 if (a->tv_nsec < b->tv_nsec) {
152 r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
153 r->tv_sec--;
154 } else {
155 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
156 }
157}
158
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200159static void perf_stat__reset_stats(void)
160{
161 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200162 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200163}
164
Jiri Olsacac21422012-11-12 18:34:00 +0100165static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200166{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200167 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200168
Jiri Olsa711a5722015-07-21 14:31:23 +0200169 if (stat_config.scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200170 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
171 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200172
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200173 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300174
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100175 /*
176 * Some events get initialized with sample_(period/type) set,
177 * like tracepoints. Clear it up for counting.
178 */
179 attr->sample_period = 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100180 /*
181 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
182 * while avoiding that older tools show confusing messages.
183 */
184 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100185
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100186 /*
187 * Disabling all counters initially, they will be enabled
188 * either manually by us or by kernel via enable_on_exec
189 * set later.
190 */
Jiri Olsac8280cec2015-12-03 10:06:45 +0100191 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100192 attr->disabled = 1;
193
Jiri Olsac8280cec2015-12-03 10:06:45 +0100194 /*
195 * In case of initial_delay we enable tracee
196 * events manually.
197 */
198 if (target__none(&target) && !initial_delay)
199 attr->enable_on_exec = 1;
200 }
201
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300202 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300203 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200204
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300205 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200206}
207
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200208/*
209 * Does the counter have nsecs as a unit?
210 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200211static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200212{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200213 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
214 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200215 return 1;
216
217 return 0;
218}
219
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100220static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100221 union perf_event *event,
222 struct perf_sample *sample __maybe_unused,
223 struct machine *machine __maybe_unused)
224{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100225 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
226 pr_err("failed to write perf data, error: %m\n");
227 return -1;
228 }
229
230 perf_stat.bytes_written += event->header.size;
231 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100232}
233
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200234/*
235 * Read out the results of a single counter:
236 * do not aggregate counts across CPUs in system-wide mode
237 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200238static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200239{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100240 int nthreads = thread_map__nr(evsel_list->threads);
241 int ncpus = perf_evsel__nr_cpus(counter);
242 int cpu, thread;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200243
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000244 if (!counter->supported)
245 return -ENOENT;
246
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100247 if (counter->system_wide)
248 nthreads = 1;
249
250 for (thread = 0; thread < nthreads; thread++) {
251 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200252 struct perf_counts_values *count;
253
254 count = perf_counts(counter->counts, cpu, thread);
255 if (perf_evsel__read(counter, cpu, thread, count))
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100256 return -1;
257 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200258 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200259
260 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200261}
262
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200263static void read_counters(bool close_counters)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200264{
265 struct perf_evsel *counter;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200266
267 evlist__for_each(evsel_list, counter) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200268 if (read_counter(counter))
Andi Kleen245bad82015-09-01 15:52:46 -0700269 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200270
Jiri Olsaf80010e2015-07-21 14:31:27 +0200271 if (perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200272 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200273
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200274 if (close_counters) {
Jiri Olsa106a94a2015-06-26 11:29:19 +0200275 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
276 thread_map__nr(evsel_list->threads));
277 }
278 }
279}
280
Jiri Olsaba411a92015-06-26 11:29:24 +0200281static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100282{
Stephane Eranian13370a92013-01-29 12:47:44 +0100283 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100284
Jiri Olsa106a94a2015-06-26 11:29:19 +0200285 read_counters(false);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100286
Stephane Eranian13370a92013-01-29 12:47:44 +0100287 clock_gettime(CLOCK_MONOTONIC, &ts);
288 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100289
Jiri Olsad4f63a42015-06-26 11:29:26 +0200290 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100291}
292
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100293static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700294{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100295 if (initial_delay)
Andi Kleen41191682013-08-02 17:41:11 -0700296 usleep(initial_delay * 1000);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100297
298 /*
299 * We need to enable counters only if:
300 * - we don't have tracee (attaching to task or cpu)
301 * - we have initial delay configured
302 */
303 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100304 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700305}
306
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300307static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300308
309/*
310 * perf_evlist__prepare_workload will send a SIGUSR1
311 * if the fork fails, since we asked by setting its
312 * want_signal to true.
313 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300314static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
315 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300316{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300317 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300318}
319
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100320static int perf_stat_synthesize_config(void)
321{
322 int err;
323
324 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
325 process_synthesized_event,
326 NULL);
327 if (err < 0) {
328 pr_err("Couldn't synthesize thread map.\n");
329 return err;
330 }
331
332 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
333 process_synthesized_event, NULL);
334 if (err < 0) {
335 pr_err("Couldn't synthesize thread map.\n");
336 return err;
337 }
338
339 err = perf_event__synthesize_stat_config(NULL, &stat_config,
340 process_synthesized_event, NULL);
341 if (err < 0) {
342 pr_err("Couldn't synthesize config.\n");
343 return err;
344 }
345
346 return 0;
347}
348
Namhyung Kimacf28922013-03-11 16:43:18 +0900349static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200350{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200351 int interval = stat_config.interval;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300352 char msg[512];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200353 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100354 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100355 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100356 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200357 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300358 const bool forks = (argc > 0);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200359
Stephane Eranian13370a92013-01-29 12:47:44 +0100360 if (interval) {
361 ts.tv_sec = interval / 1000;
362 ts.tv_nsec = (interval % 1000) * 1000000;
363 } else {
364 ts.tv_sec = 1;
365 ts.tv_nsec = 0;
366 }
367
Liming Wang60666c62009-12-31 16:05:50 +0800368 if (forks) {
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300369 if (perf_evlist__prepare_workload(evsel_list, &target, argv, false,
370 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900371 perror("failed to prepare workload");
372 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800373 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900374 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000375 }
376
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200377 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300378 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200379
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300380 evlist__for_each(evsel_list, counter) {
Jiri Olsacac21422012-11-12 18:34:00 +0100381 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600382 /*
383 * PPC returns ENXIO for HW counters until 2.6.37
384 * (behavior changed with commit b0a873e).
385 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100386 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600387 errno == ENOENT || errno == EOPNOTSUPP ||
388 errno == ENXIO) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600389 if (verbose)
390 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300391 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600392 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400393
394 if ((counter->leader != counter) ||
395 !(counter->leader->nr_members > 1))
396 continue;
David Ahernc63ca0c2011-04-29 16:04:15 -0600397 }
Ingo Molnarede70292011-04-28 08:48:42 +0200398
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300399 perf_evsel__open_strerror(counter, &target,
400 errno, msg, sizeof(msg));
401 ui__error("%s\n", msg);
402
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200403 if (child_pid != -1)
404 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600405
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200406 return -1;
407 }
David Ahern2cee77c2011-05-30 08:55:59 -0600408 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100409
410 l = strlen(counter->unit);
411 if (l > unit_width)
412 unit_width = l;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300413 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200414
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300415 if (perf_evlist__apply_filters(evsel_list, &counter)) {
416 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
417 counter->filter, perf_evsel__name(counter), errno,
Masami Hiramatsu759e6122014-08-14 02:22:55 +0000418 strerror_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100419 return -1;
420 }
421
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100422 if (STAT_RECORD) {
423 int err, fd = perf_data_file__fd(&perf_stat.file);
424
425 err = perf_session__write_header(perf_stat.session, evsel_list,
426 fd, false);
427 if (err < 0)
428 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100429
430 err = perf_stat_synthesize_config();
431 if (err < 0)
432 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100433 }
434
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200435 /*
436 * Enable counters and exec the command:
437 */
438 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100439 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200440
Liming Wang60666c62009-12-31 16:05:50 +0800441 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900442 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100443 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900444
Stephane Eranian13370a92013-01-29 12:47:44 +0100445 if (interval) {
446 while (!waitpid(child_pid, &status, WNOHANG)) {
447 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200448 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100449 }
450 }
Liming Wang60666c62009-12-31 16:05:50 +0800451 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300452
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300453 if (workload_exec_errno) {
454 const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
455 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300456 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300457 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300458
Andi Kleen33e49ea2011-09-15 14:31:40 -0700459 if (WIFSIGNALED(status))
460 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800461 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100462 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100463 while (!done) {
464 nanosleep(&ts, NULL);
465 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200466 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100467 }
Liming Wang60666c62009-12-31 16:05:50 +0800468 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200469
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200470 t1 = rdclock();
471
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200472 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200473
Jiri Olsa106a94a2015-06-26 11:29:19 +0200474 read_counters(true);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200475
Ingo Molnar42202dd2009-06-13 14:57:28 +0200476 return WEXITSTATUS(status);
477}
478
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300479static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200480{
481 int ret;
482
483 if (pre_cmd) {
484 ret = system(pre_cmd);
485 if (ret)
486 return ret;
487 }
488
489 if (sync_run)
490 sync();
491
492 ret = __run_perf_stat(argc, argv);
493 if (ret)
494 return ret;
495
496 if (post_cmd) {
497 ret = system(post_cmd);
498 if (ret)
499 return ret;
500 }
501
502 return ret;
503}
504
Andi Kleend73515c2015-03-11 07:16:27 -0700505static void print_running(u64 run, u64 ena)
506{
507 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200508 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700509 csv_sep,
510 run,
511 csv_sep,
512 ena ? 100.0 * run / ena : 100.0);
513 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200514 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700515 }
516}
517
Ingo Molnarf99844c2011-04-27 05:35:39 +0200518static void print_noise_pct(double total, double avg)
519{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800520 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200521
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700522 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200523 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600524 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200525 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200526}
527
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200528static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200529{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200530 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200531
Peter Zijlstra849abde2009-09-04 18:23:38 +0200532 if (run_count == 1)
533 return;
534
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200535 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200536 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200537}
538
Stephane Eranian12c08a92013-02-14 13:57:29 +0100539static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200540{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200541 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100542 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200543 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100544 cpu_map__id_to_socket(id),
545 csv_output ? 0 : -8,
546 cpu_map__id_to_cpu(id),
547 csv_sep,
548 csv_output ? 0 : 4,
549 nr,
550 csv_sep);
551 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100552 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200553 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100554 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100555 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100556 csv_sep,
557 csv_output ? 0 : 4,
558 nr,
559 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100560 break;
561 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200562 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200563 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100564 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100565 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200566 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200567 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200568 csv_output ? 0 : 16,
569 thread_map__comm(evsel->threads, id),
570 csv_output ? 0 : -8,
571 thread_map__pid(evsel->threads, id),
572 csv_sep);
573 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100574 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200575 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100576 default:
577 break;
578 }
579}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200580
Andi Kleenda88c7f2014-09-24 13:50:46 -0700581static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100582{
Jiri Olsa58215222015-07-21 14:31:24 +0200583 FILE *output = stat_config.output;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100584 double msecs = avg / 1e6;
Stephane Eranian410136f2013-11-12 17:58:49 +0100585 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600586 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100587
Stephane Eranian410136f2013-11-12 17:58:49 +0100588 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
589 fmt_n = csv_output ? "%s" : "%-25s";
590
Andi Kleenda88c7f2014-09-24 13:50:46 -0700591 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100592
David Ahern4bbe5a62013-09-28 14:28:00 -0600593 scnprintf(name, sizeof(name), "%s%s",
594 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +0100595
596 fprintf(output, fmt_v, msecs, csv_sep);
597
598 if (csv_output)
599 fprintf(output, "%s%s", evsel->unit, csv_sep);
600 else
601 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
602
603 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200604
Stephane Eranian023695d2011-02-14 11:20:01 +0200605 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200606 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200607}
608
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200609static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
610{
Jiri Olsa58215222015-07-21 14:31:24 +0200611 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200612 double sc = evsel->scale;
613 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200614
615 if (csv_output) {
616 fmt = sc != 1.0 ? "%.2f%s" : "%.0f%s";
617 } else {
618 if (big_num)
619 fmt = sc != 1.0 ? "%'18.2f%s" : "%'18.0f%s";
620 else
621 fmt = sc != 1.0 ? "%18.2f%s" : "%18.0f%s";
622 }
623
624 aggr_printout(evsel, id, nr);
625
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200626 fprintf(output, fmt, avg, csv_sep);
627
628 if (evsel->unit)
629 fprintf(output, "%-*s%s",
630 csv_output ? 0 : unit_width,
631 evsel->unit, csv_sep);
632
633 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
634
635 if (evsel->cgrp)
636 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -0800637}
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200638
Andi Kleeneedfcb42015-11-02 17:50:21 -0800639static void printout(int id, int nr, struct perf_evsel *counter, double uval)
640{
641 int cpu = cpu_map__id_to_cpu(id);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200642
Andi Kleeneedfcb42015-11-02 17:50:21 -0800643 if (stat_config.aggr_mode == AGGR_GLOBAL)
644 cpu = 0;
645
646 if (nsec_counter(counter))
647 nsec_printout(id, nr, counter, uval);
648 else
649 abs_printout(id, nr, counter, uval);
650
651 if (!csv_output && !stat_config.interval)
652 perf_stat__print_shadow_stats(stat_config.output, counter,
653 uval, cpu,
654 stat_config.aggr_mode);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200655}
656
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100657static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100658{
Jiri Olsa58215222015-07-21 14:31:24 +0200659 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100660 struct perf_evsel *counter;
Kan Liang601083c2015-07-02 03:08:43 -0400661 int cpu, s, s2, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +0100662 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100663 u64 ena, run, val;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100664
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100665 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100666 return;
667
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100668 for (s = 0; s < aggr_map->nr; s++) {
669 id = aggr_map->map[s];
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300670 evlist__for_each(evsel_list, counter) {
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100671 val = ena = run = 0;
672 nr = 0;
673 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Kan Liang601083c2015-07-02 03:08:43 -0400674 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100675 if (s2 != id)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100676 continue;
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200677 val += perf_counts(counter->counts, cpu, 0)->val;
678 ena += perf_counts(counter->counts, cpu, 0)->ena;
679 run += perf_counts(counter->counts, cpu, 0)->run;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100680 nr++;
681 }
682 if (prefix)
683 fprintf(output, "%s", prefix);
684
685 if (run == 0 || ena == 0) {
Stephane Eranian582ec0822013-07-05 19:06:45 +0200686 aggr_printout(counter, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100687
Stephane Eranian410136f2013-11-12 17:58:49 +0100688 fprintf(output, "%*s%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100689 csv_output ? 0 : 18,
690 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100691 csv_sep);
692
693 fprintf(output, "%-*s%s",
694 csv_output ? 0 : unit_width,
695 counter->unit, csv_sep);
696
697 fprintf(output, "%*s",
698 csv_output ? 0 : -25,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100699 perf_evsel__name(counter));
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100700
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100701 if (counter->cgrp)
702 fprintf(output, "%s%s",
703 csv_sep, counter->cgrp->name);
704
Andi Kleend73515c2015-03-11 07:16:27 -0700705 print_running(run, ena);
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100706 fputc('\n', output);
707 continue;
708 }
Stephane Eranian410136f2013-11-12 17:58:49 +0100709 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800710 printout(id, nr, counter, uval);
Andi Kleend73515c2015-03-11 07:16:27 -0700711 if (!csv_output)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100712 print_noise(counter, 1.0);
713
Andi Kleend73515c2015-03-11 07:16:27 -0700714 print_running(run, ena);
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100715 fputc('\n', output);
716 }
717 }
718}
719
Jiri Olsa32b8af82015-06-26 11:29:27 +0200720static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
721{
Jiri Olsa58215222015-07-21 14:31:24 +0200722 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200723 int nthreads = thread_map__nr(counter->threads);
724 int ncpus = cpu_map__nr(counter->cpus);
725 int cpu, thread;
726 double uval;
727
728 for (thread = 0; thread < nthreads; thread++) {
729 u64 ena = 0, run = 0, val = 0;
730
731 for (cpu = 0; cpu < ncpus; cpu++) {
732 val += perf_counts(counter->counts, cpu, thread)->val;
733 ena += perf_counts(counter->counts, cpu, thread)->ena;
734 run += perf_counts(counter->counts, cpu, thread)->run;
735 }
736
737 if (prefix)
738 fprintf(output, "%s", prefix);
739
740 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800741 printout(thread, 0, counter, uval);
Jiri Olsa32b8af82015-06-26 11:29:27 +0200742
743 if (!csv_output)
744 print_noise(counter, 1.0);
745
746 print_running(run, ena);
747 fputc('\n', output);
748 }
749}
750
Ingo Molnar42202dd2009-06-13 14:57:28 +0200751/*
752 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200753 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +0200754 */
Stephane Eranian13370a92013-01-29 12:47:44 +0100755static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200756{
Jiri Olsa58215222015-07-21 14:31:24 +0200757 FILE *output = stat_config.output;
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200758 struct perf_stat_evsel *ps = counter->priv;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200759 double avg = avg_stats(&ps->res_stats[0]);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200760 int scaled = counter->counts->scaled;
Stephane Eranian410136f2013-11-12 17:58:49 +0100761 double uval;
Andi Kleend73515c2015-03-11 07:16:27 -0700762 double avg_enabled, avg_running;
763
764 avg_enabled = avg_stats(&ps->res_stats[1]);
765 avg_running = avg_stats(&ps->res_stats[2]);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200766
Stephane Eranian13370a92013-01-29 12:47:44 +0100767 if (prefix)
768 fprintf(output, "%s", prefix);
769
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000770 if (scaled == -1 || !counter->supported) {
Stephane Eranian410136f2013-11-12 17:58:49 +0100771 fprintf(output, "%*s%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200772 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600773 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100774 csv_sep);
775 fprintf(output, "%-*s%s",
776 csv_output ? 0 : unit_width,
777 counter->unit, csv_sep);
778 fprintf(output, "%*s",
779 csv_output ? 0 : -25,
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300780 perf_evsel__name(counter));
Stephane Eranian023695d2011-02-14 11:20:01 +0200781
782 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200783 fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200784
Andi Kleend73515c2015-03-11 07:16:27 -0700785 print_running(avg_running, avg_enabled);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200786 fputc('\n', output);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200787 return;
788 }
789
Stephane Eranian410136f2013-11-12 17:58:49 +0100790 uval = avg * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800791 printout(-1, 0, counter, uval);
Peter Zijlstra849abde2009-09-04 18:23:38 +0200792
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700793 print_noise(counter, avg);
794
Andi Kleend73515c2015-03-11 07:16:27 -0700795 print_running(avg_running, avg_enabled);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200796 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200797}
798
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200799/*
800 * Print out the results of a single counter:
801 * does not use aggregated count in system-wide
802 */
Stephane Eranian13370a92013-01-29 12:47:44 +0100803static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200804{
Jiri Olsa58215222015-07-21 14:31:24 +0200805 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200806 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +0100807 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200808 int cpu;
809
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800810 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200811 val = perf_counts(counter->counts, cpu, 0)->val;
812 ena = perf_counts(counter->counts, cpu, 0)->ena;
813 run = perf_counts(counter->counts, cpu, 0)->run;
Stephane Eranian13370a92013-01-29 12:47:44 +0100814
815 if (prefix)
816 fprintf(output, "%s", prefix);
817
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200818 if (run == 0 || ena == 0) {
Stephane Eranian410136f2013-11-12 17:58:49 +0100819 fprintf(output, "CPU%*d%s%*s%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200820 csv_output ? 0 : -4,
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800821 perf_evsel__cpus(counter)->map[cpu], csv_sep,
Stephane Eraniand7470b62010-12-01 18:49:05 +0200822 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600823 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian410136f2013-11-12 17:58:49 +0100824 csv_sep);
825
826 fprintf(output, "%-*s%s",
827 csv_output ? 0 : unit_width,
828 counter->unit, csv_sep);
829
830 fprintf(output, "%*s",
831 csv_output ? 0 : -25,
832 perf_evsel__name(counter));
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200833
Stephane Eranian023695d2011-02-14 11:20:01 +0200834 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200835 fprintf(output, "%s%s",
836 csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200837
Andi Kleend73515c2015-03-11 07:16:27 -0700838 print_running(run, ena);
Stephane Eranian4aa90152011-08-15 22:22:33 +0200839 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200840 continue;
841 }
842
Stephane Eranian410136f2013-11-12 17:58:49 +0100843 uval = val * counter->scale;
Andi Kleeneedfcb42015-11-02 17:50:21 -0800844 printout(cpu, 0, counter, uval);
Andi Kleend73515c2015-03-11 07:16:27 -0700845 if (!csv_output)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200846 print_noise(counter, 1.0);
Andi Kleend73515c2015-03-11 07:16:27 -0700847 print_running(run, ena);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200848
Stephane Eranian4aa90152011-08-15 22:22:33 +0200849 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200850 }
851}
852
Jiri Olsad4f63a42015-06-26 11:29:26 +0200853static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200854{
Jiri Olsa58215222015-07-21 14:31:24 +0200855 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +0200856 static int num_print_interval;
857
858 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
859
860 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +0200861 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +0200862 case AGGR_SOCKET:
863 fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit");
864 break;
865 case AGGR_CORE:
866 fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit");
867 break;
868 case AGGR_NONE:
869 fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit");
870 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200871 case AGGR_THREAD:
872 fprintf(output, "# time comm-pid counts %*s events\n", unit_width, "unit");
873 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +0200874 case AGGR_GLOBAL:
875 default:
876 fprintf(output, "# time counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +0200877 case AGGR_UNSET:
878 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +0200879 }
880 }
881
882 if (++num_print_interval == 25)
883 num_print_interval = 0;
884}
885
886static void print_header(int argc, const char **argv)
887{
Jiri Olsa58215222015-07-21 14:31:24 +0200888 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200889 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200890
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200891 fflush(stdout);
892
Stephane Eraniand7470b62010-12-01 18:49:05 +0200893 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200894 fprintf(output, "\n");
895 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -0600896 if (target.system_wide)
897 fprintf(output, "\'system wide");
898 else if (target.cpu_list)
899 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300900 else if (!target__has_task(&target)) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200901 fprintf(output, "\'%s", argv[0]);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200902 for (i = 1; i < argc; i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200903 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +0900904 } else if (target.pid)
905 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200906 else
Namhyung Kim20f946b2012-04-26 14:15:16 +0900907 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +0200908
Stephane Eranian4aa90152011-08-15 22:22:33 +0200909 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +0200910 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200911 fprintf(output, " (%d runs)", run_count);
912 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +0200913 }
Jiri Olsad4f63a42015-06-26 11:29:26 +0200914}
915
916static void print_footer(void)
917{
Jiri Olsa58215222015-07-21 14:31:24 +0200918 FILE *output = stat_config.output;
919
Jiri Olsad4f63a42015-06-26 11:29:26 +0200920 if (!null_run)
921 fprintf(output, "\n");
922 fprintf(output, " %17.9f seconds time elapsed",
923 avg_stats(&walltime_nsecs_stats)/1e9);
924 if (run_count > 1) {
925 fprintf(output, " ");
926 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
927 avg_stats(&walltime_nsecs_stats));
928 }
929 fprintf(output, "\n\n");
930}
931
932static void print_counters(struct timespec *ts, int argc, const char **argv)
933{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200934 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +0200935 struct perf_evsel *counter;
936 char buf[64], *prefix = NULL;
937
938 if (interval)
939 print_interval(prefix = buf, ts);
940 else
941 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200942
Jiri Olsa421a50f2015-07-21 14:31:22 +0200943 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100944 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100945 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +0200946 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100947 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200948 case AGGR_THREAD:
949 evlist__for_each(evsel_list, counter)
950 print_aggr_thread(counter, prefix);
951 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100952 case AGGR_GLOBAL:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300953 evlist__for_each(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +0200954 print_counter_aggr(counter, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100955 break;
956 case AGGR_NONE:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300957 evlist__for_each(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +0200958 print_counter(counter, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100959 break;
Jiri Olsa208df992015-10-16 12:41:04 +0200960 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100961 default:
962 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200963 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200964
Jiri Olsad4f63a42015-06-26 11:29:26 +0200965 if (!interval && !csv_output)
966 print_footer();
967
Jiri Olsa58215222015-07-21 14:31:24 +0200968 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200969}
970
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200971static volatile int signr = -1;
972
Ingo Molnar52425192009-05-26 09:17:18 +0200973static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200974{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200975 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +0800976 done = 1;
977
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200978 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200979 /*
980 * render child_pid harmless
981 * won't send SIGTERM to a random
982 * process in case of race condition
983 * and fast PID recycling
984 */
985 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200986}
987
988static void sig_atexit(void)
989{
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200990 sigset_t set, oset;
991
992 /*
993 * avoid race condition with SIGCHLD handler
994 * in skip_signal() which is modifying child_pid
995 * goal is to avoid send SIGTERM to a random
996 * process
997 */
998 sigemptyset(&set);
999 sigaddset(&set, SIGCHLD);
1000 sigprocmask(SIG_BLOCK, &set, &oset);
1001
Chris Wilson933da832009-10-04 01:35:01 +01001002 if (child_pid != -1)
1003 kill(child_pid, SIGTERM);
1004
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001005 sigprocmask(SIG_SETMASK, &oset, NULL);
1006
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001007 if (signr == -1)
1008 return;
1009
1010 signal(signr, SIG_DFL);
1011 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001012}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001013
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001014static int stat__set_big_num(const struct option *opt __maybe_unused,
1015 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001016{
1017 big_num_opt = unset ? 0 : 1;
1018 return 0;
1019}
1020
Jiri Olsae0547312015-11-05 15:40:45 +01001021static const struct option stat_options[] = {
1022 OPT_BOOLEAN('T', "transaction", &transaction_run,
1023 "hardware transaction statistics"),
1024 OPT_CALLBACK('e', "event", &evsel_list, "event",
1025 "event selector. use 'perf list' to list available events",
1026 parse_events_option),
1027 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1028 "event filter", parse_filter),
1029 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1030 "child tasks do not inherit counters"),
1031 OPT_STRING('p', "pid", &target.pid, "pid",
1032 "stat events on existing process id"),
1033 OPT_STRING('t', "tid", &target.tid, "tid",
1034 "stat events on existing thread id"),
1035 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1036 "system-wide collection from all CPUs"),
1037 OPT_BOOLEAN('g', "group", &group,
1038 "put the counters into a counter group"),
1039 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1040 OPT_INCR('v', "verbose", &verbose,
1041 "be more verbose (show counter open errors, etc)"),
1042 OPT_INTEGER('r', "repeat", &run_count,
1043 "repeat command and print average + stddev (max: 100, forever: 0)"),
1044 OPT_BOOLEAN('n', "null", &null_run,
1045 "null run - dont start any counters"),
1046 OPT_INCR('d', "detailed", &detailed_run,
1047 "detailed run - start a lot of events"),
1048 OPT_BOOLEAN('S', "sync", &sync_run,
1049 "call sync() before starting a run"),
1050 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1051 "print large numbers with thousands\' separators",
1052 stat__set_big_num),
1053 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1054 "list of cpus to monitor in system-wide"),
1055 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1056 "disable CPU count aggregation", AGGR_NONE),
1057 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1058 "print counts with custom separator"),
1059 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1060 "monitor event in cgroup name only", parse_cgroups),
1061 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1062 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1063 OPT_INTEGER(0, "log-fd", &output_fd,
1064 "log output to fd, instead of stderr"),
1065 OPT_STRING(0, "pre", &pre_cmd, "command",
1066 "command to run prior to the measured command"),
1067 OPT_STRING(0, "post", &post_cmd, "command",
1068 "command to run after to the measured command"),
1069 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1070 "print counts at regular interval in ms (>= 10)"),
1071 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1072 "aggregate counts per processor socket", AGGR_SOCKET),
1073 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1074 "aggregate counts per physical processor core", AGGR_CORE),
1075 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1076 "aggregate counts per thread", AGGR_THREAD),
1077 OPT_UINTEGER('D', "delay", &initial_delay,
1078 "ms to wait before starting measurement after program start"),
1079 OPT_END()
1080};
1081
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001082static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1083{
1084 return cpu_map__get_socket(map, cpu, NULL);
1085}
1086
1087static int perf_stat__get_core(struct cpu_map *map, int cpu)
1088{
1089 return cpu_map__get_core(map, cpu, NULL);
1090}
1091
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001092static int cpu_map__get_max(struct cpu_map *map)
1093{
1094 int i, max = -1;
1095
1096 for (i = 0; i < map->nr; i++) {
1097 if (map->map[i] > max)
1098 max = map->map[i];
1099 }
1100
1101 return max;
1102}
1103
1104static struct cpu_map *cpus_aggr_map;
1105
1106static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1107{
1108 int cpu;
1109
1110 if (idx >= map->nr)
1111 return -1;
1112
1113 cpu = map->map[idx];
1114
1115 if (cpus_aggr_map->map[cpu] == -1)
1116 cpus_aggr_map->map[cpu] = get_id(map, idx);
1117
1118 return cpus_aggr_map->map[cpu];
1119}
1120
1121static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1122{
1123 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1124}
1125
1126static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1127{
1128 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1129}
1130
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001131static int perf_stat_init_aggr_mode(void)
1132{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001133 int nr;
1134
Jiri Olsa421a50f2015-07-21 14:31:22 +02001135 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001136 case AGGR_SOCKET:
1137 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1138 perror("cannot build socket map");
1139 return -1;
1140 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001141 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001142 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001143 case AGGR_CORE:
1144 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1145 perror("cannot build core map");
1146 return -1;
1147 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001148 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001149 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001150 case AGGR_NONE:
1151 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001152 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001153 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001154 default:
1155 break;
1156 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001157
1158 /*
1159 * The evsel_list->cpus is the base we operate on,
1160 * taking the highest cpu number to be the size of
1161 * the aggregation translate cpumap.
1162 */
1163 nr = cpu_map__get_max(evsel_list->cpus);
1164 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1165 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001166}
1167
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001168static void perf_stat__exit_aggr_mode(void)
1169{
1170 cpu_map__put(aggr_map);
1171 cpu_map__put(cpus_aggr_map);
1172 aggr_map = NULL;
1173 cpus_aggr_map = NULL;
1174}
1175
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001176/*
1177 * Add default attributes, if there were no attributes specified or
1178 * if -d/--detailed, -d -d or -d -d -d is used:
1179 */
1180static int add_default_attributes(void)
1181{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001182 struct perf_event_attr default_attrs[] = {
1183
1184 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1185 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1186 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1187 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1188
1189 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
1190 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
1191 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
1192 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1193 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1194 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1195
1196};
1197
1198/*
1199 * Detailed stats (-d), covering the L1 and last level data caches:
1200 */
1201 struct perf_event_attr detailed_attrs[] = {
1202
1203 { .type = PERF_TYPE_HW_CACHE,
1204 .config =
1205 PERF_COUNT_HW_CACHE_L1D << 0 |
1206 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1207 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1208
1209 { .type = PERF_TYPE_HW_CACHE,
1210 .config =
1211 PERF_COUNT_HW_CACHE_L1D << 0 |
1212 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1213 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1214
1215 { .type = PERF_TYPE_HW_CACHE,
1216 .config =
1217 PERF_COUNT_HW_CACHE_LL << 0 |
1218 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1219 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1220
1221 { .type = PERF_TYPE_HW_CACHE,
1222 .config =
1223 PERF_COUNT_HW_CACHE_LL << 0 |
1224 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1225 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1226};
1227
1228/*
1229 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1230 */
1231 struct perf_event_attr very_detailed_attrs[] = {
1232
1233 { .type = PERF_TYPE_HW_CACHE,
1234 .config =
1235 PERF_COUNT_HW_CACHE_L1I << 0 |
1236 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1237 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1238
1239 { .type = PERF_TYPE_HW_CACHE,
1240 .config =
1241 PERF_COUNT_HW_CACHE_L1I << 0 |
1242 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1243 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1244
1245 { .type = PERF_TYPE_HW_CACHE,
1246 .config =
1247 PERF_COUNT_HW_CACHE_DTLB << 0 |
1248 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1249 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1250
1251 { .type = PERF_TYPE_HW_CACHE,
1252 .config =
1253 PERF_COUNT_HW_CACHE_DTLB << 0 |
1254 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1255 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1256
1257 { .type = PERF_TYPE_HW_CACHE,
1258 .config =
1259 PERF_COUNT_HW_CACHE_ITLB << 0 |
1260 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1261 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1262
1263 { .type = PERF_TYPE_HW_CACHE,
1264 .config =
1265 PERF_COUNT_HW_CACHE_ITLB << 0 |
1266 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1267 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1268
1269};
1270
1271/*
1272 * Very, very detailed stats (-d -d -d), adding prefetch events:
1273 */
1274 struct perf_event_attr very_very_detailed_attrs[] = {
1275
1276 { .type = PERF_TYPE_HW_CACHE,
1277 .config =
1278 PERF_COUNT_HW_CACHE_L1D << 0 |
1279 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1280 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1281
1282 { .type = PERF_TYPE_HW_CACHE,
1283 .config =
1284 PERF_COUNT_HW_CACHE_L1D << 0 |
1285 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1286 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1287};
1288
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001289 /* Set attrs if no event is selected and !null_run: */
1290 if (null_run)
1291 return 0;
1292
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001293 if (transaction_run) {
1294 int err;
1295 if (pmu_have_event("cpu", "cycles-ct") &&
1296 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02001297 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001298 else
Jiri Olsaa4547422015-06-03 16:25:53 +02001299 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
1300 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001301 fprintf(stderr, "Cannot set up transaction events\n");
1302 return -1;
1303 }
1304 return 0;
1305 }
1306
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001307 if (!evsel_list->nr_entries) {
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001308 if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001309 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001310 }
1311
1312 /* Detailed events get appended to the event list: */
1313
1314 if (detailed_run < 1)
1315 return 0;
1316
1317 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001318 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001319 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001320
1321 if (detailed_run < 2)
1322 return 0;
1323
1324 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001325 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001326 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001327
1328 if (detailed_run < 3)
1329 return 0;
1330
1331 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001332 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001333}
1334
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001335static const char * const recort_usage[] = {
1336 "perf stat record [<options>]",
1337 NULL,
1338};
1339
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001340static void init_features(struct perf_session *session)
1341{
1342 int feat;
1343
1344 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
1345 perf_header__set_feat(&session->header, feat);
1346
1347 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
1348 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
1349 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1350 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1351}
1352
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001353static int __cmd_record(int argc, const char **argv)
1354{
1355 struct perf_session *session;
1356 struct perf_data_file *file = &perf_stat.file;
1357
1358 argc = parse_options(argc, argv, stat_options, record_usage,
1359 PARSE_OPT_STOP_AT_NON_OPTION);
1360
1361 if (output_name)
1362 file->path = output_name;
1363
1364 session = perf_session__new(file, false, NULL);
1365 if (session == NULL) {
1366 pr_err("Perf session creation failed.\n");
1367 return -1;
1368 }
1369
1370 /* No pipe support ATM */
1371 if (perf_stat.file.is_pipe)
1372 return -EINVAL;
1373
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001374 init_features(session);
1375
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001376 session->evlist = evsel_list;
1377 perf_stat.session = session;
1378 perf_stat.record = true;
1379 return argc;
1380}
1381
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001382int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02001383{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001384 const char * const stat_usage[] = {
1385 "perf stat [<options>] [<command>]",
1386 NULL
1387 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09001388 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001389 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02001390 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001391 unsigned int interval;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001392 const char * const stat_subcommands[] = { "record" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02001393
Stephane Eranian5af52b52010-05-18 15:00:01 +02001394 setlocale(LC_ALL, "");
1395
Namhyung Kim334fe7a2013-03-11 16:43:12 +09001396 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001397 if (evsel_list == NULL)
1398 return -ENOMEM;
1399
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001400 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
1401 (const char **) stat_usage,
1402 PARSE_OPT_STOP_AT_NON_OPTION);
1403
1404 if (argc && !strncmp(argv[0], "rec", 3)) {
1405 argc = __cmd_record(argc, argv);
1406 if (argc < 0)
1407 return -1;
1408 }
Stephane Eraniand7470b62010-12-01 18:49:05 +02001409
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001410 interval = stat_config.interval;
1411
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001412 /*
1413 * For record command the -o is already taken care of.
1414 */
1415 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02001416 output = NULL;
1417
Jim Cromie56f3bae2011-09-07 17:14:00 -06001418 if (output_name && output_fd) {
1419 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001420 parse_options_usage(stat_usage, stat_options, "o", 1);
1421 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001422 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06001423 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001424
1425 if (output_fd < 0) {
1426 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001427 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001428 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001429 }
1430
Stephane Eranian4aa90152011-08-15 22:22:33 +02001431 if (!output) {
1432 struct timespec tm;
1433 mode = append_file ? "a" : "w";
1434
1435 output = fopen(output_name, mode);
1436 if (!output) {
1437 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06001438 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001439 }
1440 clock_gettime(CLOCK_REALTIME, &tm);
1441 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001442 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06001443 mode = append_file ? "a" : "w";
1444 output = fdopen(output_fd, mode);
1445 if (!output) {
1446 perror("Failed opening logfd");
1447 return -errno;
1448 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001449 }
1450
Jiri Olsa58215222015-07-21 14:31:24 +02001451 stat_config.output = output;
1452
Jim Cromied4ffd042011-09-07 17:14:03 -06001453 if (csv_sep) {
Stephane Eraniand7470b62010-12-01 18:49:05 +02001454 csv_output = true;
Jim Cromied4ffd042011-09-07 17:14:03 -06001455 if (!strcmp(csv_sep, "\\t"))
1456 csv_sep = "\t";
1457 } else
Stephane Eraniand7470b62010-12-01 18:49:05 +02001458 csv_sep = DEFAULT_SEPARATOR;
1459
1460 /*
1461 * let the spreadsheet do the pretty-printing
1462 */
1463 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06001464 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02001465 if (big_num_opt == 1) {
1466 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001467 parse_options_usage(stat_usage, stat_options, "B", 1);
1468 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001469 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02001470 } else /* Nope, so disable big number formatting */
1471 big_num = false;
1472 } else if (big_num_opt == 0) /* User passed --no-big-num */
1473 big_num = false;
1474
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001475 if (!argc && target__none(&target))
Jiri Olsae0547312015-11-05 15:40:45 +01001476 usage_with_options(stat_usage, stat_options);
David Ahernac3063b2013-09-30 07:37:37 -06001477
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001478 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09001479 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001480 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001481 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001482 } else if (run_count == 0) {
1483 forever = true;
1484 run_count = 1;
1485 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001486
Jiri Olsa421a50f2015-07-21 14:31:22 +02001487 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001488 fprintf(stderr, "The --per-thread option is only available "
1489 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001490 parse_options_usage(NULL, stat_options, "p", 1);
1491 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001492 goto out;
1493 }
1494
1495 /*
1496 * no_aggr, cgroup are for system-wide only
1497 * --per-thread is aggregated per thread, we dont mix it with cpu mode
1498 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02001499 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
1500 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001501 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02001502 fprintf(stderr, "both cgroup and no-aggregation "
1503 "modes only available in system-wide mode\n");
1504
Jiri Olsae0547312015-11-05 15:40:45 +01001505 parse_options_usage(stat_usage, stat_options, "G", 1);
1506 parse_options_usage(NULL, stat_options, "A", 1);
1507 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001508 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001509 }
1510
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001511 if (add_default_attributes())
1512 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001513
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001514 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02001515
Namhyung Kim77a6f012012-05-07 14:09:04 +09001516 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001517 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09001518 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001519 parse_options_usage(stat_usage, stat_options, "p", 1);
1520 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001521 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09001522 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01001523 parse_options_usage(stat_usage, stat_options, "C", 1);
1524 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09001525 }
1526 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02001527 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001528
1529 /*
1530 * Initialize thread_map with comm names,
1531 * so we could print it out on output.
1532 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02001533 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001534 thread_map__read_comms(evsel_list->threads);
1535
Stephane Eranian13370a92013-01-29 12:47:44 +01001536 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04001537 if (interval < 10) {
1538 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01001539 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04001540 goto out;
1541 } else
1542 pr_warning("print interval < 100ms. "
1543 "The overhead percentage could be high in some cases. "
1544 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01001545 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001546
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001547 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03001548 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001549
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001550 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03001551 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001552
Ingo Molnar58d7e992009-05-15 11:03:23 +02001553 /*
1554 * We dont want to block the signals - that would cause
1555 * child tasks to inherit that and Ctrl-C would not work.
1556 * What we want is for Ctrl-C to work in the exec()-ed
1557 * task, but being ignored by perf stat itself:
1558 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001559 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001560 if (!forever)
1561 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01001562 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02001563 signal(SIGALRM, skip_signal);
1564 signal(SIGABRT, skip_signal);
1565
Ingo Molnar42202dd2009-06-13 14:57:28 +02001566 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001567 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Ingo Molnar42202dd2009-06-13 14:57:28 +02001568 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001569 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
1570 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02001571
Ingo Molnar42202dd2009-06-13 14:57:28 +02001572 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001573 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001574 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02001575 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001576 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02001577 }
1578
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001579 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001580 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001581
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001582 if (STAT_RECORD) {
1583 /*
1584 * We synthesize the kernel mmap record just so that older tools
1585 * don't emit warnings about not being able to resolve symbols
1586 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
1587 * a saner message about no samples being in the perf.data file.
1588 *
1589 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01001590 * in header.c at the moment 'perf stat record' gets introduced, which
1591 * is not really needed once we start adding the stat specific PERF_RECORD_
1592 * records, but the need to suppress the kptr_restrict messages in older
1593 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001594 */
1595 int fd = perf_data_file__fd(&perf_stat.file);
1596 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
1597 process_synthesized_event,
1598 &perf_stat.session->machines.host);
1599 if (err) {
1600 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
1601 "older tools may produce warnings about this file\n.");
1602 }
1603
1604 perf_stat.session->header.data_size += perf_stat.bytes_written;
1605 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
1606
1607 perf_session__delete(perf_stat.session);
1608 }
1609
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001610 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001611 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02001612out:
1613 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001614 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001615}