blob: 5abef25f998344d80ce3ddabfdebbf40b12bdc5c [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"
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -060055#include "util/drv_configs.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020056#include "util/color.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080057#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080058#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110059#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030060#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020061#include "util/thread_map.h"
Jiri Olsad8095602015-08-07 12:51:03 +020062#include "util/counts.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030063#include "util/group.h"
Jiri Olsa4979d0c2015-11-05 15:40:46 +010064#include "util/session.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010065#include "util/tool.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030066#include "util/group.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010067#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020068
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030069#include <linux/time64.h>
Andi Kleen44b1e602016-05-30 12:49:42 -030070#include <api/fs/fs.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020071#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020072#include <sys/prctl.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030073#include <inttypes.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020074#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070075#include <math.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020076
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030077#include "sane_ctype.h"
78
Stephane Eraniand7470b62010-12-01 18:49:05 +020079#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060080#define CNTR_NOT_SUPPORTED "<not supported>"
81#define CNTR_NOT_COUNTED "<not counted>"
Stephane Eraniand7470b62010-12-01 18:49:05 +020082
Jiri Olsad4f63a42015-06-26 11:29:26 +020083static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010084
Andi Kleen4cabc3d2013-08-21 16:47:26 -070085/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020086static const char *transaction_attrs = {
87 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070088 "{"
89 "instructions,"
90 "cycles,"
91 "cpu/cycles-t/,"
92 "cpu/tx-start/,"
93 "cpu/el-start/,"
94 "cpu/cycles-ct/"
95 "}"
96};
97
98/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +020099static const char * transaction_limited_attrs = {
100 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700101 "{"
102 "instructions,"
103 "cycles,"
104 "cpu/cycles-t/,"
105 "cpu/tx-start/"
106 "}"
107};
108
Andi Kleen44b1e602016-05-30 12:49:42 -0300109static const char * topdown_attrs[] = {
110 "topdown-total-slots",
111 "topdown-slots-retired",
112 "topdown-recovery-bubbles",
113 "topdown-fetch-bubbles",
114 "topdown-slots-issued",
115 NULL,
116};
117
Robert Richter666e6d42012-04-05 18:26:27 +0200118static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200119
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300120static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900121 .uid = UINT_MAX,
122};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530123
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100124typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
125
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530126static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200127static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200128static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000129static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200130static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700131static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300132static bool topdown_run = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200133static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200134static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200135static const char *csv_sep = NULL;
136static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800137static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200138static const char *pre_cmd = NULL;
139static const char *post_cmd = NULL;
140static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700141static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100142static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500143static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800144static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300145static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700146static bool no_merge = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100147static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100148static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100149static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100150static bool append_file;
151static const char *output_name;
152static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100153static int print_free_counters_hint;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200154
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100155struct perf_stat {
156 bool record;
157 struct perf_data_file file;
158 struct perf_session *session;
159 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100160 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100161 bool maps_allocated;
162 struct cpu_map *cpus;
163 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100164 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100165};
166
167static struct perf_stat perf_stat;
168#define STAT_RECORD perf_stat.record
169
Liming Wang60666c62009-12-31 16:05:50 +0800170static volatile int done = 0;
171
Jiri Olsa421a50f2015-07-21 14:31:22 +0200172static struct perf_stat_config stat_config = {
173 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200174 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200175};
176
Stephane Eranian13370a92013-01-29 12:47:44 +0100177static inline void diff_timespec(struct timespec *r, struct timespec *a,
178 struct timespec *b)
179{
180 r->tv_sec = a->tv_sec - b->tv_sec;
181 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300182 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100183 r->tv_sec--;
184 } else {
185 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
186 }
187}
188
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200189static void perf_stat__reset_stats(void)
190{
191 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200192 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200193}
194
Jiri Olsacac21422012-11-12 18:34:00 +0100195static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200196{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200197 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200198
Jiri Olsa711a5722015-07-21 14:31:23 +0200199 if (stat_config.scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200200 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
201 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200202
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200203 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300204
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100205 /*
206 * Some events get initialized with sample_(period/type) set,
207 * like tracepoints. Clear it up for counting.
208 */
209 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100210
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100211 /*
212 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
213 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100214 *
215 * However for pipe sessions we need to keep it zero,
216 * because script's perf_evsel__check_attr is triggered
217 * by attr->sample_type != 0, and we can't run it on
218 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100219 */
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100220 if (!(STAT_RECORD && perf_stat.file.is_pipe))
221 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100222
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100223 /*
224 * Disabling all counters initially, they will be enabled
225 * either manually by us or by kernel via enable_on_exec
226 * set later.
227 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100228 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100229 attr->disabled = 1;
230
Jiri Olsac8280ce2015-12-03 10:06:45 +0100231 /*
232 * In case of initial_delay we enable tracee
233 * events manually.
234 */
235 if (target__none(&target) && !initial_delay)
236 attr->enable_on_exec = 1;
237 }
238
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300239 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300240 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200241
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300242 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200243}
244
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200245/*
246 * Does the counter have nsecs as a unit?
247 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200248static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200249{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200250 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
251 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200252 return 1;
253
254 return 0;
255}
256
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100257static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100258 union perf_event *event,
259 struct perf_sample *sample __maybe_unused,
260 struct machine *machine __maybe_unused)
261{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100262 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
263 pr_err("failed to write perf data, error: %m\n");
264 return -1;
265 }
266
267 perf_stat.bytes_written += event->header.size;
268 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100269}
270
Jiri Olsa1975d362015-11-05 15:40:56 +0100271static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100272{
Jiri Olsa1975d362015-11-05 15:40:56 +0100273 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100274 process_synthesized_event,
275 NULL);
276}
277
278#define WRITE_STAT_ROUND_EVENT(time, interval) \
279 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
280
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100281#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
282
283static int
284perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
285 struct perf_counts_values *count)
286{
287 struct perf_sample_id *sid = SID(counter, cpu, thread);
288
289 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
290 process_synthesized_event, NULL);
291}
292
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200293/*
294 * Read out the results of a single counter:
295 * do not aggregate counts across CPUs in system-wide mode
296 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200297static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200298{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100299 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100300 int ncpus, cpu, thread;
301
302 if (target__has_cpu(&target))
303 ncpus = perf_evsel__nr_cpus(counter);
304 else
305 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200306
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000307 if (!counter->supported)
308 return -ENOENT;
309
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100310 if (counter->system_wide)
311 nthreads = 1;
312
313 for (thread = 0; thread < nthreads; thread++) {
314 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200315 struct perf_counts_values *count;
316
317 count = perf_counts(counter->counts, cpu, thread);
Stephane Eraniandb49a712017-04-12 11:23:01 -0700318 if (perf_evsel__read(counter, cpu, thread, count)) {
319 counter->counts->scaled = -1;
320 perf_counts(counter->counts, cpu, thread)->ena = 0;
321 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100322 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700323 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100324
325 if (STAT_RECORD) {
326 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
327 pr_err("failed to write stat event\n");
328 return -1;
329 }
330 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700331
332 if (verbose > 1) {
333 fprintf(stat_config.output,
334 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
335 perf_evsel__name(counter),
336 cpu,
337 count->val, count->ena, count->run);
338 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100339 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200340 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200341
342 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200343}
344
Mark Rutland3df33ef2016-08-09 14:04:29 +0100345static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200346{
347 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700348 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200349
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300350 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700351 ret = read_counter(counter);
352 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700353 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200354
Stephane Eraniandb49a712017-04-12 11:23:01 -0700355 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200356 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200357 }
358}
359
Jiri Olsaba411a92015-06-26 11:29:24 +0200360static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100361{
Stephane Eranian13370a92013-01-29 12:47:44 +0100362 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100363
Mark Rutland3df33ef2016-08-09 14:04:29 +0100364 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100365
Stephane Eranian13370a92013-01-29 12:47:44 +0100366 clock_gettime(CLOCK_MONOTONIC, &ts);
367 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100368
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100369 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300370 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100371 pr_err("failed to write stat round event\n");
372 }
373
Jiri Olsad4f63a42015-06-26 11:29:26 +0200374 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100375}
376
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100377static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700378{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100379 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300380 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100381
382 /*
383 * We need to enable counters only if:
384 * - we don't have tracee (attaching to task or cpu)
385 * - we have initial delay configured
386 */
387 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100388 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700389}
390
Mark Rutland3df33ef2016-08-09 14:04:29 +0100391static void disable_counters(void)
392{
393 /*
394 * If we don't have tracee (attaching to task or cpu), counters may
395 * still be running. To get accurate group ratios, we must stop groups
396 * from counting before reading their constituent counters.
397 */
398 if (!target__none(&target))
399 perf_evlist__disable(evsel_list);
400}
401
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300402static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300403
404/*
405 * perf_evlist__prepare_workload will send a SIGUSR1
406 * if the fork fails, since we asked by setting its
407 * want_signal to true.
408 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300409static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
410 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300411{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300412 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300413}
414
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100415static bool has_unit(struct perf_evsel *counter)
416{
417 return counter->unit && *counter->unit;
418}
419
420static bool has_scale(struct perf_evsel *counter)
421{
422 return counter->scale != 1;
423}
424
Jiri Olsa664c98d2015-11-05 15:40:50 +0100425static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100426{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100427 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100428 int err;
429
Jiri Olsa664c98d2015-11-05 15:40:50 +0100430 if (is_pipe) {
431 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
432 process_synthesized_event);
433 if (err < 0) {
434 pr_err("Couldn't synthesize attrs.\n");
435 return err;
436 }
437 }
438
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100439 /*
440 * Synthesize other events stuff not carried within
441 * attr event - unit, scale, name
442 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300443 evlist__for_each_entry(evsel_list, counter) {
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100444 if (!counter->supported)
445 continue;
446
447 /*
448 * Synthesize unit and scale only if it's defined.
449 */
450 if (has_unit(counter)) {
451 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
452 if (err < 0) {
453 pr_err("Couldn't synthesize evsel unit.\n");
454 return err;
455 }
456 }
457
458 if (has_scale(counter)) {
459 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
460 if (err < 0) {
461 pr_err("Couldn't synthesize evsel scale.\n");
462 return err;
463 }
464 }
465
466 if (counter->own_cpus) {
467 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
468 if (err < 0) {
469 pr_err("Couldn't synthesize evsel scale.\n");
470 return err;
471 }
472 }
473
474 /*
475 * Name is needed only for pipe output,
476 * perf.data carries event names.
477 */
478 if (is_pipe) {
479 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
480 if (err < 0) {
481 pr_err("Couldn't synthesize evsel name.\n");
482 return err;
483 }
484 }
485 }
486
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100487 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
488 process_synthesized_event,
489 NULL);
490 if (err < 0) {
491 pr_err("Couldn't synthesize thread map.\n");
492 return err;
493 }
494
495 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
496 process_synthesized_event, NULL);
497 if (err < 0) {
498 pr_err("Couldn't synthesize thread map.\n");
499 return err;
500 }
501
502 err = perf_event__synthesize_stat_config(NULL, &stat_config,
503 process_synthesized_event, NULL);
504 if (err < 0) {
505 pr_err("Couldn't synthesize config.\n");
506 return err;
507 }
508
509 return 0;
510}
511
Jiri Olsa2af46462015-11-05 15:40:49 +0100512#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
513
514static int __store_counter_ids(struct perf_evsel *counter,
515 struct cpu_map *cpus,
516 struct thread_map *threads)
517{
518 int cpu, thread;
519
520 for (cpu = 0; cpu < cpus->nr; cpu++) {
521 for (thread = 0; thread < threads->nr; thread++) {
522 int fd = FD(counter, cpu, thread);
523
524 if (perf_evlist__id_add_fd(evsel_list, counter,
525 cpu, thread, fd) < 0)
526 return -1;
527 }
528 }
529
530 return 0;
531}
532
533static int store_counter_ids(struct perf_evsel *counter)
534{
535 struct cpu_map *cpus = counter->cpus;
536 struct thread_map *threads = counter->threads;
537
538 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
539 return -ENOMEM;
540
541 return __store_counter_ids(counter, cpus, threads);
542}
543
Namhyung Kimacf28922013-03-11 16:43:18 +0900544static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200545{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200546 int interval = stat_config.interval;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300547 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200548 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100549 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100550 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100551 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200552 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300553 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100554 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600555 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200556
Stephane Eranian13370a92013-01-29 12:47:44 +0100557 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300558 ts.tv_sec = interval / USEC_PER_MSEC;
559 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100560 } else {
561 ts.tv_sec = 1;
562 ts.tv_nsec = 0;
563 }
564
Liming Wang60666c62009-12-31 16:05:50 +0800565 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100566 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300567 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900568 perror("failed to prepare workload");
569 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800570 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900571 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000572 }
573
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200574 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300575 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200576
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300577 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300578try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100579 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600580 /*
581 * PPC returns ENXIO for HW counters until 2.6.37
582 * (behavior changed with commit b0a873e).
583 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100584 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600585 errno == ENOENT || errno == EOPNOTSUPP ||
586 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900587 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600588 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300589 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600590 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400591
592 if ((counter->leader != counter) ||
593 !(counter->leader->nr_members > 1))
594 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300595 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900596 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300597 ui__warning("%s\n", msg);
598 goto try_again;
599 }
Ingo Molnarede70292011-04-28 08:48:42 +0200600
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300601 perf_evsel__open_strerror(counter, &target,
602 errno, msg, sizeof(msg));
603 ui__error("%s\n", msg);
604
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200605 if (child_pid != -1)
606 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600607
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200608 return -1;
609 }
David Ahern2cee77c2011-05-30 08:55:59 -0600610 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100611
612 l = strlen(counter->unit);
613 if (l > unit_width)
614 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100615
616 if (STAT_RECORD && store_counter_ids(counter))
617 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300618 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200619
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300620 if (perf_evlist__apply_filters(evsel_list, &counter)) {
621 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
622 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300623 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100624 return -1;
625 }
626
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600627 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
628 error("failed to set config \"%s\" on event %s with %d (%s)\n",
629 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
630 str_error_r(errno, msg, sizeof(msg)));
631 return -1;
632 }
633
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100634 if (STAT_RECORD) {
635 int err, fd = perf_data_file__fd(&perf_stat.file);
636
Jiri Olsa664c98d2015-11-05 15:40:50 +0100637 if (is_pipe) {
638 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
639 } else {
640 err = perf_session__write_header(perf_stat.session, evsel_list,
641 fd, false);
642 }
643
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100644 if (err < 0)
645 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100646
Jiri Olsa664c98d2015-11-05 15:40:50 +0100647 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100648 if (err < 0)
649 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100650 }
651
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200652 /*
653 * Enable counters and exec the command:
654 */
655 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100656 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200657
Liming Wang60666c62009-12-31 16:05:50 +0800658 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900659 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100660 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900661
Stephane Eranian13370a92013-01-29 12:47:44 +0100662 if (interval) {
663 while (!waitpid(child_pid, &status, WNOHANG)) {
664 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200665 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100666 }
667 }
Liming Wang60666c62009-12-31 16:05:50 +0800668 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300669
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300670 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300671 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300672 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300673 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300674 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300675
Andi Kleen33e49ea2011-09-15 14:31:40 -0700676 if (WIFSIGNALED(status))
677 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800678 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100679 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100680 while (!done) {
681 nanosleep(&ts, NULL);
682 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200683 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100684 }
Liming Wang60666c62009-12-31 16:05:50 +0800685 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200686
Mark Rutland3df33ef2016-08-09 14:04:29 +0100687 disable_counters();
688
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200689 t1 = rdclock();
690
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200691 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200692
Mark Rutland3df33ef2016-08-09 14:04:29 +0100693 /*
694 * Closing a group leader splits the group, and as we only disable
695 * group leaders, results in remaining events becoming enabled. To
696 * avoid arbitrary skew, we must read all counters before closing any
697 * group leaders.
698 */
699 read_counters();
700 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200701
Ingo Molnar42202dd2009-06-13 14:57:28 +0200702 return WEXITSTATUS(status);
703}
704
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300705static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200706{
707 int ret;
708
709 if (pre_cmd) {
710 ret = system(pre_cmd);
711 if (ret)
712 return ret;
713 }
714
715 if (sync_run)
716 sync();
717
718 ret = __run_perf_stat(argc, argv);
719 if (ret)
720 return ret;
721
722 if (post_cmd) {
723 ret = system(post_cmd);
724 if (ret)
725 return ret;
726 }
727
728 return ret;
729}
730
Andi Kleend73515c2015-03-11 07:16:27 -0700731static void print_running(u64 run, u64 ena)
732{
733 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200734 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700735 csv_sep,
736 run,
737 csv_sep,
738 ena ? 100.0 * run / ena : 100.0);
739 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200740 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700741 }
742}
743
Ingo Molnarf99844c2011-04-27 05:35:39 +0200744static void print_noise_pct(double total, double avg)
745{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800746 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200747
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700748 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200749 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600750 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200751 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200752}
753
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200754static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200755{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200756 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200757
Peter Zijlstra849abde2009-09-04 18:23:38 +0200758 if (run_count == 1)
759 return;
760
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200761 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200762 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200763}
764
Stephane Eranian12c08a92013-02-14 13:57:29 +0100765static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200766{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200767 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100768 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200769 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100770 cpu_map__id_to_socket(id),
771 csv_output ? 0 : -8,
772 cpu_map__id_to_cpu(id),
773 csv_sep,
774 csv_output ? 0 : 4,
775 nr,
776 csv_sep);
777 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100778 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200779 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100780 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100781 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100782 csv_sep,
783 csv_output ? 0 : 4,
784 nr,
785 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100786 break;
787 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200788 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200789 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100790 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100791 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200792 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200793 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200794 csv_output ? 0 : 16,
795 thread_map__comm(evsel->threads, id),
796 csv_output ? 0 : -8,
797 thread_map__pid(evsel->threads, id),
798 csv_sep);
799 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100800 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200801 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100802 default:
803 break;
804 }
805}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200806
Andi Kleen140aead2016-01-30 09:06:49 -0800807struct outstate {
808 FILE *fh;
809 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800810 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800811 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800812 int id, nr;
813 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800814};
815
816#define METRIC_LEN 35
817
818static void new_line_std(void *ctx)
819{
820 struct outstate *os = ctx;
821
822 os->newline = true;
823}
824
825static void do_new_line_std(struct outstate *os)
826{
827 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800828 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800829 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800830 if (stat_config.aggr_mode == AGGR_NONE)
831 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800832 fprintf(os->fh, " ");
833}
834
835static void print_metric_std(void *ctx, const char *color, const char *fmt,
836 const char *unit, double val)
837{
838 struct outstate *os = ctx;
839 FILE *out = os->fh;
840 int n;
841 bool newline = os->newline;
842
843 os->newline = false;
844
845 if (unit == NULL || fmt == NULL) {
846 fprintf(out, "%-*s", METRIC_LEN, "");
847 return;
848 }
849
850 if (newline)
851 do_new_line_std(os);
852
853 n = fprintf(out, " # ");
854 if (color)
855 n += color_fprintf(out, color, fmt, val);
856 else
857 n += fprintf(out, fmt, val);
858 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
859}
860
Andi Kleen92a61f62016-02-29 14:36:21 -0800861static void new_line_csv(void *ctx)
862{
863 struct outstate *os = ctx;
864 int i;
865
866 fputc('\n', os->fh);
867 if (os->prefix)
868 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800869 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800870 for (i = 0; i < os->nfields; i++)
871 fputs(csv_sep, os->fh);
872}
873
874static void print_metric_csv(void *ctx,
875 const char *color __maybe_unused,
876 const char *fmt, const char *unit, double val)
877{
878 struct outstate *os = ctx;
879 FILE *out = os->fh;
880 char buf[64], *vals, *ends;
881
882 if (unit == NULL || fmt == NULL) {
883 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
884 return;
885 }
886 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900887 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800888 while (isdigit(*ends) || *ends == '.')
889 ends++;
890 *ends = 0;
891 while (isspace(*unit))
892 unit++;
893 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
894}
895
Andi Kleen54b50912016-03-03 15:57:36 -0800896#define METRIC_ONLY_LEN 20
897
898/* Filter out some columns that don't work well in metrics only mode */
899
900static bool valid_only_metric(const char *unit)
901{
902 if (!unit)
903 return false;
904 if (strstr(unit, "/sec") ||
905 strstr(unit, "hz") ||
906 strstr(unit, "Hz") ||
907 strstr(unit, "CPUs utilized"))
908 return false;
909 return true;
910}
911
912static const char *fixunit(char *buf, struct perf_evsel *evsel,
913 const char *unit)
914{
915 if (!strncmp(unit, "of all", 6)) {
916 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
917 unit);
918 return buf;
919 }
920 return unit;
921}
922
923static void print_metric_only(void *ctx, const char *color, const char *fmt,
924 const char *unit, double val)
925{
926 struct outstate *os = ctx;
927 FILE *out = os->fh;
928 int n;
929 char buf[1024];
930 unsigned mlen = METRIC_ONLY_LEN;
931
932 if (!valid_only_metric(unit))
933 return;
934 unit = fixunit(buf, os->evsel, unit);
935 if (color)
936 n = color_fprintf(out, color, fmt, val);
937 else
938 n = fprintf(out, fmt, val);
939 if (n > METRIC_ONLY_LEN)
940 n = METRIC_ONLY_LEN;
941 if (mlen < strlen(unit))
942 mlen = strlen(unit) + 1;
943 fprintf(out, "%*s", mlen - n, "");
944}
945
946static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
947 const char *fmt,
948 const char *unit, double val)
949{
950 struct outstate *os = ctx;
951 FILE *out = os->fh;
952 char buf[64], *vals, *ends;
953 char tbuf[1024];
954
955 if (!valid_only_metric(unit))
956 return;
957 unit = fixunit(tbuf, os->evsel, unit);
958 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900959 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -0800960 while (isdigit(*ends) || *ends == '.')
961 ends++;
962 *ends = 0;
963 fprintf(out, "%s%s", vals, csv_sep);
964}
965
966static void new_line_metric(void *ctx __maybe_unused)
967{
968}
969
970static void print_metric_header(void *ctx, const char *color __maybe_unused,
971 const char *fmt __maybe_unused,
972 const char *unit, double val __maybe_unused)
973{
974 struct outstate *os = ctx;
975 char tbuf[1024];
976
977 if (!valid_only_metric(unit))
978 return;
979 unit = fixunit(tbuf, os->evsel, unit);
980 if (csv_output)
981 fprintf(os->fh, "%s%s", unit, csv_sep);
982 else
983 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
984}
985
Andi Kleenda88c7f2014-09-24 13:50:46 -0700986static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100987{
Jiri Olsa58215222015-07-21 14:31:24 +0200988 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300989 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +0100990 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600991 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100992
Stephane Eranian410136f2013-11-12 17:58:49 +0100993 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
994 fmt_n = csv_output ? "%s" : "%-25s";
995
Andi Kleenda88c7f2014-09-24 13:50:46 -0700996 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100997
David Ahern4bbe5a62013-09-28 14:28:00 -0600998 scnprintf(name, sizeof(name), "%s%s",
999 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +01001000
1001 fprintf(output, fmt_v, msecs, csv_sep);
1002
1003 if (csv_output)
1004 fprintf(output, "%s%s", evsel->unit, csv_sep);
1005 else
1006 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1007
1008 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001009
Stephane Eranian023695d2011-02-14 11:20:01 +02001010 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001011 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001012}
1013
Andi Kleen44d49a62016-02-29 14:36:22 -08001014static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1015{
1016 int i;
1017
1018 if (!aggr_get_id)
1019 return 0;
1020
1021 if (stat_config.aggr_mode == AGGR_NONE)
1022 return id;
1023
1024 if (stat_config.aggr_mode == AGGR_GLOBAL)
1025 return 0;
1026
1027 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1028 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1029
1030 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1031 return cpu2;
1032 }
1033 return 0;
1034}
1035
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001036static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1037{
Jiri Olsa58215222015-07-21 14:31:24 +02001038 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001039 double sc = evsel->scale;
1040 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001041
1042 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001043 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001044 } else {
1045 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001046 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001047 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001048 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001049 }
1050
1051 aggr_printout(evsel, id, nr);
1052
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001053 fprintf(output, fmt, avg, csv_sep);
1054
1055 if (evsel->unit)
1056 fprintf(output, "%-*s%s",
1057 csv_output ? 0 : unit_width,
1058 evsel->unit, csv_sep);
1059
1060 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1061
1062 if (evsel->cgrp)
1063 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001064}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001065
Andi Kleenf9483392016-01-30 09:06:50 -08001066static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Andi Kleencb110f42016-01-30 09:06:51 -08001067 char *prefix, u64 run, u64 ena, double noise)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001068{
Andi Kleen140aead2016-01-30 09:06:49 -08001069 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001070 struct outstate os = {
1071 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001072 .prefix = prefix ? prefix : "",
1073 .id = id,
1074 .nr = nr,
1075 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001076 };
Andi Kleen140aead2016-01-30 09:06:49 -08001077 print_metric_t pm = print_metric_std;
1078 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001079
Andi Kleen54b50912016-03-03 15:57:36 -08001080 if (metric_only) {
1081 nl = new_line_metric;
1082 if (csv_output)
1083 pm = print_metric_only_csv;
1084 else
1085 pm = print_metric_only;
1086 } else
1087 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001088
Andi Kleen54b50912016-03-03 15:57:36 -08001089 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001090 static int aggr_fields[] = {
1091 [AGGR_GLOBAL] = 0,
1092 [AGGR_THREAD] = 1,
1093 [AGGR_NONE] = 1,
1094 [AGGR_SOCKET] = 2,
1095 [AGGR_CORE] = 2,
1096 };
1097
1098 pm = print_metric_csv;
1099 nl = new_line_csv;
1100 os.nfields = 3;
1101 os.nfields += aggr_fields[stat_config.aggr_mode];
1102 if (counter->cgrp)
1103 os.nfields++;
1104 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001105 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001106 if (metric_only) {
1107 pm(&os, NULL, "", "", 0);
1108 return;
1109 }
Andi Kleencb110f42016-01-30 09:06:51 -08001110 aggr_printout(counter, id, nr);
1111
1112 fprintf(stat_config.output, "%*s%s",
1113 csv_output ? 0 : 18,
1114 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1115 csv_sep);
1116
Borislav Petkov02d492e2017-02-07 01:40:05 +01001117 if (counter->supported)
1118 print_free_counters_hint = 1;
1119
Andi Kleencb110f42016-01-30 09:06:51 -08001120 fprintf(stat_config.output, "%-*s%s",
1121 csv_output ? 0 : unit_width,
1122 counter->unit, csv_sep);
1123
1124 fprintf(stat_config.output, "%*s",
1125 csv_output ? 0 : -25,
1126 perf_evsel__name(counter));
1127
1128 if (counter->cgrp)
1129 fprintf(stat_config.output, "%s%s",
1130 csv_sep, counter->cgrp->name);
1131
Andi Kleen92a61f62016-02-29 14:36:21 -08001132 if (!csv_output)
1133 pm(&os, NULL, NULL, "", 0);
1134 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001135 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001136 if (csv_output)
1137 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001138 return;
1139 }
1140
Andi Kleen54b50912016-03-03 15:57:36 -08001141 if (metric_only)
1142 /* nothing */;
1143 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001144 nsec_printout(id, nr, counter, uval);
1145 else
1146 abs_printout(id, nr, counter, uval);
1147
Andi Kleen140aead2016-01-30 09:06:49 -08001148 out.print_metric = pm;
1149 out.new_line = nl;
1150 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001151 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001152
Andi Kleen54b50912016-03-03 15:57:36 -08001153 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001154 print_noise(counter, noise);
1155 print_running(run, ena);
1156 }
1157
1158 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001159 first_shadow_cpu(counter, id),
Andi Kleen140aead2016-01-30 09:06:49 -08001160 &out);
Andi Kleen54b50912016-03-03 15:57:36 -08001161 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001162 print_noise(counter, noise);
1163 print_running(run, ena);
1164 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001165}
1166
Andi Kleen44d49a62016-02-29 14:36:22 -08001167static void aggr_update_shadow(void)
1168{
1169 int cpu, s2, id, s;
1170 u64 val;
1171 struct perf_evsel *counter;
1172
1173 for (s = 0; s < aggr_map->nr; s++) {
1174 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001175 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001176 val = 0;
1177 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1178 s2 = aggr_get_id(evsel_list->cpus, cpu);
1179 if (s2 != id)
1180 continue;
1181 val += perf_counts(counter->counts, cpu, 0)->val;
1182 }
1183 val = val * counter->scale;
1184 perf_stat__update_shadow_stats(counter, &val,
1185 first_shadow_cpu(counter, id));
1186 }
1187 }
1188}
1189
Andi Kleen430daf22017-03-20 13:17:00 -07001190static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001191 void (*cb)(struct perf_evsel *counter, void *data,
1192 bool first),
1193 void *data)
1194{
Andi Kleen430daf22017-03-20 13:17:00 -07001195 struct perf_evsel *alias;
1196
1197 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1198 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1199 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1200 alias->scale != counter->scale ||
1201 alias->cgrp != counter->cgrp ||
1202 strcmp(alias->unit, counter->unit) ||
1203 nsec_counter(alias) != nsec_counter(counter))
1204 break;
1205 alias->merged_stat = true;
1206 cb(alias, data, false);
1207 }
1208}
1209
1210static bool collect_data(struct perf_evsel *counter,
1211 void (*cb)(struct perf_evsel *counter, void *data,
1212 bool first),
1213 void *data)
1214{
1215 if (counter->merged_stat)
1216 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001217 cb(counter, data, true);
Andi Kleen430daf22017-03-20 13:17:00 -07001218 if (!no_merge)
1219 collect_all_aliases(counter, cb, data);
1220 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001221}
1222
1223struct aggr_data {
1224 u64 ena, run, val;
1225 int id;
1226 int nr;
1227 int cpu;
1228};
1229
1230static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1231{
1232 struct aggr_data *ad = data;
1233 int cpu, s2;
1234
1235 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1236 struct perf_counts_values *counts;
1237
1238 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1239 if (s2 != ad->id)
1240 continue;
1241 if (first)
1242 ad->nr++;
1243 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001244 /*
1245 * When any result is bad, make them all to give
1246 * consistent output in interval mode.
1247 */
1248 if (counts->ena == 0 || counts->run == 0 ||
1249 counter->counts->scaled == -1) {
1250 ad->ena = 0;
1251 ad->run = 0;
1252 break;
1253 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001254 ad->val += counts->val;
1255 ad->ena += counts->ena;
1256 ad->run += counts->run;
1257 }
1258}
1259
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001260static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001261{
Jiri Olsa58215222015-07-21 14:31:24 +02001262 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001263 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001264 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001265 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001266 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001267 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001268
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001269 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001270 return;
1271
Andi Kleen44d49a62016-02-29 14:36:22 -08001272 aggr_update_shadow();
1273
Andi Kleen54b50912016-03-03 15:57:36 -08001274 /*
1275 * With metric_only everything is on a single line.
1276 * Without each counter has its own line.
1277 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001278 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001279 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001280 if (prefix && metric_only)
1281 fprintf(output, "%s", prefix);
1282
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001283 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001284 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001285 evlist__for_each_entry(evsel_list, counter) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001286 ad.val = ad.ena = ad.run = 0;
1287 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001288 if (!collect_data(counter, aggr_cb, &ad))
1289 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001290 nr = ad.nr;
1291 ena = ad.ena;
1292 run = ad.run;
1293 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001294 if (first && metric_only) {
1295 first = false;
1296 aggr_printout(counter, id, nr);
1297 }
1298 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001299 fprintf(output, "%s", prefix);
1300
Stephane Eranian410136f2013-11-12 17:58:49 +01001301 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001302 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
Andi Kleen54b50912016-03-03 15:57:36 -08001303 if (!metric_only)
1304 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001305 }
Andi Kleen54b50912016-03-03 15:57:36 -08001306 if (metric_only)
1307 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001308 }
1309}
1310
Jiri Olsa32b8af82015-06-26 11:29:27 +02001311static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1312{
Jiri Olsa58215222015-07-21 14:31:24 +02001313 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001314 int nthreads = thread_map__nr(counter->threads);
1315 int ncpus = cpu_map__nr(counter->cpus);
1316 int cpu, thread;
1317 double uval;
1318
1319 for (thread = 0; thread < nthreads; thread++) {
1320 u64 ena = 0, run = 0, val = 0;
1321
1322 for (cpu = 0; cpu < ncpus; cpu++) {
1323 val += perf_counts(counter->counts, cpu, thread)->val;
1324 ena += perf_counts(counter->counts, cpu, thread)->ena;
1325 run += perf_counts(counter->counts, cpu, thread)->run;
1326 }
1327
1328 if (prefix)
1329 fprintf(output, "%s", prefix);
1330
1331 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001332 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001333 fputc('\n', output);
1334 }
1335}
1336
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001337struct caggr_data {
1338 double avg, avg_enabled, avg_running;
1339};
1340
1341static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1342 bool first __maybe_unused)
1343{
1344 struct caggr_data *cd = data;
1345 struct perf_stat_evsel *ps = counter->priv;
1346
1347 cd->avg += avg_stats(&ps->res_stats[0]);
1348 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1349 cd->avg_running += avg_stats(&ps->res_stats[2]);
1350}
1351
Ingo Molnar42202dd2009-06-13 14:57:28 +02001352/*
1353 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001354 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001355 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001356static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001357{
Jiri Olsa58215222015-07-21 14:31:24 +02001358 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001359 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001360 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001361
Andi Kleen430daf22017-03-20 13:17:00 -07001362 if (!collect_data(counter, counter_aggr_cb, &cd))
1363 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001364
Andi Kleen54b50912016-03-03 15:57:36 -08001365 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001366 fprintf(output, "%s", prefix);
1367
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001368 uval = cd.avg * counter->scale;
1369 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, cd.avg);
Andi Kleen54b50912016-03-03 15:57:36 -08001370 if (!metric_only)
1371 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001372}
1373
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001374static void counter_cb(struct perf_evsel *counter, void *data,
1375 bool first __maybe_unused)
1376{
1377 struct aggr_data *ad = data;
1378
1379 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1380 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1381 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1382}
1383
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001384/*
1385 * Print out the results of a single counter:
1386 * does not use aggregated count in system-wide
1387 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001388static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001389{
Jiri Olsa58215222015-07-21 14:31:24 +02001390 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001391 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001392 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001393 int cpu;
1394
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001395 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001396 struct aggr_data ad = { .cpu = cpu };
1397
Andi Kleen430daf22017-03-20 13:17:00 -07001398 if (!collect_data(counter, counter_cb, &ad))
1399 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001400 val = ad.val;
1401 ena = ad.ena;
1402 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001403
1404 if (prefix)
1405 fprintf(output, "%s", prefix);
1406
Stephane Eranian410136f2013-11-12 17:58:49 +01001407 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001408 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001409
Stephane Eranian4aa90152011-08-15 22:22:33 +02001410 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001411 }
1412}
1413
Andi Kleen206cab62016-03-03 15:57:37 -08001414static void print_no_aggr_metric(char *prefix)
1415{
1416 int cpu;
1417 int nrcpus = 0;
1418 struct perf_evsel *counter;
1419 u64 ena, run, val;
1420 double uval;
1421
1422 nrcpus = evsel_list->cpus->nr;
1423 for (cpu = 0; cpu < nrcpus; cpu++) {
1424 bool first = true;
1425
1426 if (prefix)
1427 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001428 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen206cab62016-03-03 15:57:37 -08001429 if (first) {
1430 aggr_printout(counter, cpu, 0);
1431 first = false;
1432 }
1433 val = perf_counts(counter->counts, cpu, 0)->val;
1434 ena = perf_counts(counter->counts, cpu, 0)->ena;
1435 run = perf_counts(counter->counts, cpu, 0)->run;
1436
1437 uval = val * counter->scale;
1438 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1439 }
1440 fputc('\n', stat_config.output);
1441 }
1442}
1443
Andi Kleen54b50912016-03-03 15:57:36 -08001444static int aggr_header_lens[] = {
1445 [AGGR_CORE] = 18,
1446 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001447 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001448 [AGGR_THREAD] = 24,
1449 [AGGR_GLOBAL] = 0,
1450};
1451
Andi Kleenc51fd632016-05-24 12:52:39 -07001452static const char *aggr_header_csv[] = {
1453 [AGGR_CORE] = "core,cpus,",
1454 [AGGR_SOCKET] = "socket,cpus",
1455 [AGGR_NONE] = "cpu,",
1456 [AGGR_THREAD] = "comm-pid,",
1457 [AGGR_GLOBAL] = ""
1458};
1459
Andi Kleen41c8ca22016-05-24 12:52:38 -07001460static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001461{
1462 struct perf_stat_output_ctx out;
1463 struct perf_evsel *counter;
1464 struct outstate os = {
1465 .fh = stat_config.output
1466 };
1467
1468 if (prefix)
1469 fprintf(stat_config.output, "%s", prefix);
1470
Andi Kleen41c8ca22016-05-24 12:52:38 -07001471 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001472 fprintf(stat_config.output, "%*s",
1473 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001474 if (csv_output) {
1475 if (stat_config.interval)
1476 fputs("time,", stat_config.output);
1477 fputs(aggr_header_csv[stat_config.aggr_mode],
1478 stat_config.output);
1479 }
Andi Kleen54b50912016-03-03 15:57:36 -08001480
1481 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001482 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen54b50912016-03-03 15:57:36 -08001483 os.evsel = counter;
1484 out.ctx = &os;
1485 out.print_metric = print_metric_header;
1486 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001487 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001488 os.evsel = counter;
1489 perf_stat__print_shadow_stats(counter, 0,
1490 0,
1491 &out);
1492 }
1493 fputc('\n', stat_config.output);
1494}
1495
Jiri Olsad4f63a42015-06-26 11:29:26 +02001496static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001497{
Jiri Olsa58215222015-07-21 14:31:24 +02001498 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001499 static int num_print_interval;
1500
1501 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1502
Andi Kleen41c8ca22016-05-24 12:52:38 -07001503 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001504 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001505 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001506 fprintf(output, "# time socket cpus");
1507 if (!metric_only)
1508 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001509 break;
1510 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001511 fprintf(output, "# time core cpus");
1512 if (!metric_only)
1513 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001514 break;
1515 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001516 fprintf(output, "# time CPU");
1517 if (!metric_only)
1518 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001519 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001520 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001521 fprintf(output, "# time comm-pid");
1522 if (!metric_only)
1523 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001524 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001525 case AGGR_GLOBAL:
1526 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001527 fprintf(output, "# time");
1528 if (!metric_only)
1529 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001530 case AGGR_UNSET:
1531 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001532 }
1533 }
1534
Andi Kleen41c8ca22016-05-24 12:52:38 -07001535 if (num_print_interval == 0 && metric_only)
1536 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001537 if (++num_print_interval == 25)
1538 num_print_interval = 0;
1539}
1540
1541static void print_header(int argc, const char **argv)
1542{
Jiri Olsa58215222015-07-21 14:31:24 +02001543 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001544 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001545
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001546 fflush(stdout);
1547
Stephane Eraniand7470b62010-12-01 18:49:05 +02001548 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001549 fprintf(output, "\n");
1550 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001551 if (target.system_wide)
1552 fprintf(output, "\'system wide");
1553 else if (target.cpu_list)
1554 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001555 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001556 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1557 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001558 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001559 } else if (target.pid)
1560 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001561 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001562 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001563
Stephane Eranian4aa90152011-08-15 22:22:33 +02001564 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001565 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001566 fprintf(output, " (%d runs)", run_count);
1567 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001568 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001569}
1570
1571static void print_footer(void)
1572{
Jiri Olsa58215222015-07-21 14:31:24 +02001573 FILE *output = stat_config.output;
1574
Jiri Olsad4f63a42015-06-26 11:29:26 +02001575 if (!null_run)
1576 fprintf(output, "\n");
1577 fprintf(output, " %17.9f seconds time elapsed",
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001578 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001579 if (run_count > 1) {
1580 fprintf(output, " ");
1581 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1582 avg_stats(&walltime_nsecs_stats));
1583 }
1584 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001585
1586 if (print_free_counters_hint)
1587 fprintf(output,
1588"Some events weren't counted. Try disabling the NMI watchdog:\n"
1589" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1590" perf stat ...\n"
1591" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001592}
1593
1594static void print_counters(struct timespec *ts, int argc, const char **argv)
1595{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001596 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001597 struct perf_evsel *counter;
1598 char buf[64], *prefix = NULL;
1599
Jiri Olsa664c98d2015-11-05 15:40:50 +01001600 /* Do not print anything if we record to the pipe. */
1601 if (STAT_RECORD && perf_stat.file.is_pipe)
1602 return;
1603
Jiri Olsad4f63a42015-06-26 11:29:26 +02001604 if (interval)
1605 print_interval(prefix = buf, ts);
1606 else
1607 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001608
Andi Kleen54b50912016-03-03 15:57:36 -08001609 if (metric_only) {
1610 static int num_print_iv;
1611
Andi Kleen41c8ca22016-05-24 12:52:38 -07001612 if (num_print_iv == 0 && !interval)
1613 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001614 if (num_print_iv++ == 25)
1615 num_print_iv = 0;
1616 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1617 fprintf(stat_config.output, "%s", prefix);
1618 }
1619
Jiri Olsa421a50f2015-07-21 14:31:22 +02001620 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001621 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001622 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001623 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001624 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001625 case AGGR_THREAD:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001626 evlist__for_each_entry(evsel_list, counter)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001627 print_aggr_thread(counter, prefix);
1628 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001629 case AGGR_GLOBAL:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001630 evlist__for_each_entry(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001631 print_counter_aggr(counter, prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001632 if (metric_only)
1633 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001634 break;
1635 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001636 if (metric_only)
1637 print_no_aggr_metric(prefix);
1638 else {
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001639 evlist__for_each_entry(evsel_list, counter)
Andi Kleen206cab62016-03-03 15:57:37 -08001640 print_counter(counter, prefix);
1641 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001642 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001643 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001644 default:
1645 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001646 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001647
Jiri Olsad4f63a42015-06-26 11:29:26 +02001648 if (!interval && !csv_output)
1649 print_footer();
1650
Jiri Olsa58215222015-07-21 14:31:24 +02001651 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001652}
1653
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001654static volatile int signr = -1;
1655
Ingo Molnar52425192009-05-26 09:17:18 +02001656static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001657{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001658 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001659 done = 1;
1660
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001661 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001662 /*
1663 * render child_pid harmless
1664 * won't send SIGTERM to a random
1665 * process in case of race condition
1666 * and fast PID recycling
1667 */
1668 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001669}
1670
1671static void sig_atexit(void)
1672{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001673 sigset_t set, oset;
1674
1675 /*
1676 * avoid race condition with SIGCHLD handler
1677 * in skip_signal() which is modifying child_pid
1678 * goal is to avoid send SIGTERM to a random
1679 * process
1680 */
1681 sigemptyset(&set);
1682 sigaddset(&set, SIGCHLD);
1683 sigprocmask(SIG_BLOCK, &set, &oset);
1684
Chris Wilson933da832009-10-04 01:35:01 +01001685 if (child_pid != -1)
1686 kill(child_pid, SIGTERM);
1687
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001688 sigprocmask(SIG_SETMASK, &oset, NULL);
1689
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001690 if (signr == -1)
1691 return;
1692
1693 signal(signr, SIG_DFL);
1694 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001695}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001696
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001697static int stat__set_big_num(const struct option *opt __maybe_unused,
1698 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001699{
1700 big_num_opt = unset ? 0 : 1;
1701 return 0;
1702}
1703
Andi Kleen44b1e602016-05-30 12:49:42 -03001704static int enable_metric_only(const struct option *opt __maybe_unused,
1705 const char *s __maybe_unused, int unset)
1706{
1707 force_metric_only = true;
1708 metric_only = !unset;
1709 return 0;
1710}
1711
Jiri Olsae0547312015-11-05 15:40:45 +01001712static const struct option stat_options[] = {
1713 OPT_BOOLEAN('T', "transaction", &transaction_run,
1714 "hardware transaction statistics"),
1715 OPT_CALLBACK('e', "event", &evsel_list, "event",
1716 "event selector. use 'perf list' to list available events",
1717 parse_events_option),
1718 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1719 "event filter", parse_filter),
1720 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1721 "child tasks do not inherit counters"),
1722 OPT_STRING('p', "pid", &target.pid, "pid",
1723 "stat events on existing process id"),
1724 OPT_STRING('t', "tid", &target.tid, "tid",
1725 "stat events on existing thread id"),
1726 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1727 "system-wide collection from all CPUs"),
1728 OPT_BOOLEAN('g', "group", &group,
1729 "put the counters into a counter group"),
1730 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1731 OPT_INCR('v', "verbose", &verbose,
1732 "be more verbose (show counter open errors, etc)"),
1733 OPT_INTEGER('r', "repeat", &run_count,
1734 "repeat command and print average + stddev (max: 100, forever: 0)"),
1735 OPT_BOOLEAN('n', "null", &null_run,
1736 "null run - dont start any counters"),
1737 OPT_INCR('d', "detailed", &detailed_run,
1738 "detailed run - start a lot of events"),
1739 OPT_BOOLEAN('S', "sync", &sync_run,
1740 "call sync() before starting a run"),
1741 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1742 "print large numbers with thousands\' separators",
1743 stat__set_big_num),
1744 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1745 "list of cpus to monitor in system-wide"),
1746 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1747 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001748 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001749 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1750 "print counts with custom separator"),
1751 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1752 "monitor event in cgroup name only", parse_cgroups),
1753 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1754 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1755 OPT_INTEGER(0, "log-fd", &output_fd,
1756 "log output to fd, instead of stderr"),
1757 OPT_STRING(0, "pre", &pre_cmd, "command",
1758 "command to run prior to the measured command"),
1759 OPT_STRING(0, "post", &post_cmd, "command",
1760 "command to run after to the measured command"),
1761 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1762 "print counts at regular interval in ms (>= 10)"),
1763 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1764 "aggregate counts per processor socket", AGGR_SOCKET),
1765 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1766 "aggregate counts per physical processor core", AGGR_CORE),
1767 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1768 "aggregate counts per thread", AGGR_THREAD),
1769 OPT_UINTEGER('D', "delay", &initial_delay,
1770 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001771 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1772 "Only print computed metrics. No raw values", enable_metric_only),
1773 OPT_BOOLEAN(0, "topdown", &topdown_run,
1774 "measure topdown level 1 statistics"),
Jiri Olsae0547312015-11-05 15:40:45 +01001775 OPT_END()
1776};
1777
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001778static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1779{
1780 return cpu_map__get_socket(map, cpu, NULL);
1781}
1782
1783static int perf_stat__get_core(struct cpu_map *map, int cpu)
1784{
1785 return cpu_map__get_core(map, cpu, NULL);
1786}
1787
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001788static int cpu_map__get_max(struct cpu_map *map)
1789{
1790 int i, max = -1;
1791
1792 for (i = 0; i < map->nr; i++) {
1793 if (map->map[i] > max)
1794 max = map->map[i];
1795 }
1796
1797 return max;
1798}
1799
1800static struct cpu_map *cpus_aggr_map;
1801
1802static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1803{
1804 int cpu;
1805
1806 if (idx >= map->nr)
1807 return -1;
1808
1809 cpu = map->map[idx];
1810
1811 if (cpus_aggr_map->map[cpu] == -1)
1812 cpus_aggr_map->map[cpu] = get_id(map, idx);
1813
1814 return cpus_aggr_map->map[cpu];
1815}
1816
1817static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1818{
1819 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1820}
1821
1822static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1823{
1824 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1825}
1826
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001827static int perf_stat_init_aggr_mode(void)
1828{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001829 int nr;
1830
Jiri Olsa421a50f2015-07-21 14:31:22 +02001831 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001832 case AGGR_SOCKET:
1833 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1834 perror("cannot build socket map");
1835 return -1;
1836 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001837 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001838 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001839 case AGGR_CORE:
1840 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1841 perror("cannot build core map");
1842 return -1;
1843 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001844 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001845 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001846 case AGGR_NONE:
1847 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001848 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001849 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001850 default:
1851 break;
1852 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001853
1854 /*
1855 * The evsel_list->cpus is the base we operate on,
1856 * taking the highest cpu number to be the size of
1857 * the aggregation translate cpumap.
1858 */
1859 nr = cpu_map__get_max(evsel_list->cpus);
1860 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1861 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001862}
1863
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001864static void perf_stat__exit_aggr_mode(void)
1865{
1866 cpu_map__put(aggr_map);
1867 cpu_map__put(cpus_aggr_map);
1868 aggr_map = NULL;
1869 cpus_aggr_map = NULL;
1870}
1871
Jiri Olsa68d702f2015-11-05 15:40:58 +01001872static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1873{
1874 int cpu;
1875
1876 if (idx > map->nr)
1877 return -1;
1878
1879 cpu = map->map[idx];
1880
Jan Stancekda8a58b2017-02-17 12:10:26 +01001881 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001882 return -1;
1883
1884 return cpu;
1885}
1886
1887static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1888{
1889 struct perf_env *env = data;
1890 int cpu = perf_env__get_cpu(env, map, idx);
1891
1892 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1893}
1894
1895static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1896{
1897 struct perf_env *env = data;
1898 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1899
1900 if (cpu != -1) {
1901 int socket_id = env->cpu[cpu].socket_id;
1902
1903 /*
1904 * Encode socket in upper 16 bits
1905 * core_id is relative to socket, and
1906 * we need a global id. So we combine
1907 * socket + core id.
1908 */
1909 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1910 }
1911
1912 return core;
1913}
1914
1915static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1916 struct cpu_map **sockp)
1917{
1918 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1919}
1920
1921static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1922 struct cpu_map **corep)
1923{
1924 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1925}
1926
1927static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1928{
1929 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1930}
1931
1932static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1933{
1934 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1935}
1936
1937static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1938{
1939 struct perf_env *env = &st->session->header.env;
1940
1941 switch (stat_config.aggr_mode) {
1942 case AGGR_SOCKET:
1943 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1944 perror("cannot build socket map");
1945 return -1;
1946 }
1947 aggr_get_id = perf_stat__get_socket_file;
1948 break;
1949 case AGGR_CORE:
1950 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1951 perror("cannot build core map");
1952 return -1;
1953 }
1954 aggr_get_id = perf_stat__get_core_file;
1955 break;
1956 case AGGR_NONE:
1957 case AGGR_GLOBAL:
1958 case AGGR_THREAD:
1959 case AGGR_UNSET:
1960 default:
1961 break;
1962 }
1963
1964 return 0;
1965}
1966
Andi Kleen44b1e602016-05-30 12:49:42 -03001967static int topdown_filter_events(const char **attr, char **str, bool use_group)
1968{
1969 int off = 0;
1970 int i;
1971 int len = 0;
1972 char *s;
1973
1974 for (i = 0; attr[i]; i++) {
1975 if (pmu_have_event("cpu", attr[i])) {
1976 len += strlen(attr[i]) + 1;
1977 attr[i - off] = attr[i];
1978 } else
1979 off++;
1980 }
1981 attr[i - off] = NULL;
1982
1983 *str = malloc(len + 1 + 2);
1984 if (!*str)
1985 return -1;
1986 s = *str;
1987 if (i - off == 0) {
1988 *s = 0;
1989 return 0;
1990 }
1991 if (use_group)
1992 *s++ = '{';
1993 for (i = 0; attr[i]; i++) {
1994 strcpy(s, attr[i]);
1995 s += strlen(s);
1996 *s++ = ',';
1997 }
1998 if (use_group) {
1999 s[-1] = '}';
2000 *s = 0;
2001 } else
2002 s[-1] = 0;
2003 return 0;
2004}
2005
2006__weak bool arch_topdown_check_group(bool *warn)
2007{
2008 *warn = false;
2009 return false;
2010}
2011
2012__weak void arch_topdown_group_warn(void)
2013{
2014}
2015
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002016/*
2017 * Add default attributes, if there were no attributes specified or
2018 * if -d/--detailed, -d -d or -d -d -d is used:
2019 */
2020static int add_default_attributes(void)
2021{
Andi Kleen44b1e602016-05-30 12:49:42 -03002022 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002023 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002024
2025 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2026 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2027 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2028 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2029
2030 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002031};
2032 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002033 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002034};
2035 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002036 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002037};
2038 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002039 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2040 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2041 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2042
2043};
2044
2045/*
2046 * Detailed stats (-d), covering the L1 and last level data caches:
2047 */
2048 struct perf_event_attr detailed_attrs[] = {
2049
2050 { .type = PERF_TYPE_HW_CACHE,
2051 .config =
2052 PERF_COUNT_HW_CACHE_L1D << 0 |
2053 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2054 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2055
2056 { .type = PERF_TYPE_HW_CACHE,
2057 .config =
2058 PERF_COUNT_HW_CACHE_L1D << 0 |
2059 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2060 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2061
2062 { .type = PERF_TYPE_HW_CACHE,
2063 .config =
2064 PERF_COUNT_HW_CACHE_LL << 0 |
2065 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2066 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2067
2068 { .type = PERF_TYPE_HW_CACHE,
2069 .config =
2070 PERF_COUNT_HW_CACHE_LL << 0 |
2071 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2072 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2073};
2074
2075/*
2076 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2077 */
2078 struct perf_event_attr very_detailed_attrs[] = {
2079
2080 { .type = PERF_TYPE_HW_CACHE,
2081 .config =
2082 PERF_COUNT_HW_CACHE_L1I << 0 |
2083 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2084 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2085
2086 { .type = PERF_TYPE_HW_CACHE,
2087 .config =
2088 PERF_COUNT_HW_CACHE_L1I << 0 |
2089 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2090 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2091
2092 { .type = PERF_TYPE_HW_CACHE,
2093 .config =
2094 PERF_COUNT_HW_CACHE_DTLB << 0 |
2095 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2096 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2097
2098 { .type = PERF_TYPE_HW_CACHE,
2099 .config =
2100 PERF_COUNT_HW_CACHE_DTLB << 0 |
2101 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2102 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2103
2104 { .type = PERF_TYPE_HW_CACHE,
2105 .config =
2106 PERF_COUNT_HW_CACHE_ITLB << 0 |
2107 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2108 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2109
2110 { .type = PERF_TYPE_HW_CACHE,
2111 .config =
2112 PERF_COUNT_HW_CACHE_ITLB << 0 |
2113 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2114 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2115
2116};
2117
2118/*
2119 * Very, very detailed stats (-d -d -d), adding prefetch events:
2120 */
2121 struct perf_event_attr very_very_detailed_attrs[] = {
2122
2123 { .type = PERF_TYPE_HW_CACHE,
2124 .config =
2125 PERF_COUNT_HW_CACHE_L1D << 0 |
2126 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2127 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2128
2129 { .type = PERF_TYPE_HW_CACHE,
2130 .config =
2131 PERF_COUNT_HW_CACHE_L1D << 0 |
2132 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2133 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2134};
2135
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002136 /* Set attrs if no event is selected and !null_run: */
2137 if (null_run)
2138 return 0;
2139
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002140 if (transaction_run) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002141 if (pmu_have_event("cpu", "cycles-ct") &&
2142 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02002143 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002144 else
Jiri Olsaa4547422015-06-03 16:25:53 +02002145 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2146 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002147 fprintf(stderr, "Cannot set up transaction events\n");
2148 return -1;
2149 }
2150 return 0;
2151 }
2152
Andi Kleen44b1e602016-05-30 12:49:42 -03002153 if (topdown_run) {
2154 char *str = NULL;
2155 bool warn = false;
2156
2157 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2158 stat_config.aggr_mode != AGGR_CORE) {
2159 pr_err("top down event configuration requires --per-core mode\n");
2160 return -1;
2161 }
2162 stat_config.aggr_mode = AGGR_CORE;
2163 if (nr_cgroups || !target__has_cpu(&target)) {
2164 pr_err("top down event configuration requires system-wide mode (-a)\n");
2165 return -1;
2166 }
2167
2168 if (!force_metric_only)
2169 metric_only = true;
2170 if (topdown_filter_events(topdown_attrs, &str,
2171 arch_topdown_check_group(&warn)) < 0) {
2172 pr_err("Out of memory\n");
2173 return -1;
2174 }
2175 if (topdown_attrs[0] && str) {
2176 if (warn)
2177 arch_topdown_group_warn();
2178 err = parse_events(evsel_list, str, NULL);
2179 if (err) {
2180 fprintf(stderr,
2181 "Cannot set up top down events %s: %d\n",
2182 str, err);
2183 free(str);
2184 return -1;
2185 }
2186 } else {
2187 fprintf(stderr, "System does not support topdown\n");
2188 return -1;
2189 }
2190 free(str);
2191 }
2192
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002193 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002194 if (target__has_cpu(&target))
2195 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2196
Andi Kleen9dec4472016-02-26 16:27:56 -08002197 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2198 return -1;
2199 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2200 if (perf_evlist__add_default_attrs(evsel_list,
2201 frontend_attrs) < 0)
2202 return -1;
2203 }
2204 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2205 if (perf_evlist__add_default_attrs(evsel_list,
2206 backend_attrs) < 0)
2207 return -1;
2208 }
2209 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002210 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002211 }
2212
2213 /* Detailed events get appended to the event list: */
2214
2215 if (detailed_run < 1)
2216 return 0;
2217
2218 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002219 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002220 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002221
2222 if (detailed_run < 2)
2223 return 0;
2224
2225 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002226 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002227 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002228
2229 if (detailed_run < 3)
2230 return 0;
2231
2232 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002233 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002234}
2235
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002236static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002237 "perf stat record [<options>]",
2238 NULL,
2239};
2240
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002241static void init_features(struct perf_session *session)
2242{
2243 int feat;
2244
2245 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2246 perf_header__set_feat(&session->header, feat);
2247
2248 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2249 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2250 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2251 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2252}
2253
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002254static int __cmd_record(int argc, const char **argv)
2255{
2256 struct perf_session *session;
2257 struct perf_data_file *file = &perf_stat.file;
2258
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002259 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002260 PARSE_OPT_STOP_AT_NON_OPTION);
2261
2262 if (output_name)
2263 file->path = output_name;
2264
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002265 if (run_count != 1 || forever) {
2266 pr_err("Cannot use -r option with perf stat record.\n");
2267 return -1;
2268 }
2269
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002270 session = perf_session__new(file, false, NULL);
2271 if (session == NULL) {
2272 pr_err("Perf session creation failed.\n");
2273 return -1;
2274 }
2275
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002276 init_features(session);
2277
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002278 session->evlist = evsel_list;
2279 perf_stat.session = session;
2280 perf_stat.record = true;
2281 return argc;
2282}
2283
Jiri Olsaa56f9392015-11-05 15:40:59 +01002284static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2285 union perf_event *event,
2286 struct perf_session *session)
2287{
Andi Kleene3b03b62016-05-05 16:04:03 -07002288 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002289 struct perf_evsel *counter;
2290 struct timespec tsh, *ts = NULL;
2291 const char **argv = session->header.env.cmdline_argv;
2292 int argc = session->header.env.nr_cmdline;
2293
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002294 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002295 perf_stat_process_counter(&stat_config, counter);
2296
Andi Kleene3b03b62016-05-05 16:04:03 -07002297 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2298 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002299
Andi Kleene3b03b62016-05-05 16:04:03 -07002300 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002301 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2302 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002303 ts = &tsh;
2304 }
2305
2306 print_counters(ts, argc, argv);
2307 return 0;
2308}
2309
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002310static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002311int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002312 union perf_event *event,
2313 struct perf_session *session __maybe_unused)
2314{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002315 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2316
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002317 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002318
Jiri Olsa89af4e02015-11-05 15:41:02 +01002319 if (cpu_map__empty(st->cpus)) {
2320 if (st->aggr_mode != AGGR_UNSET)
2321 pr_warning("warning: processing task data, aggregation mode not set\n");
2322 return 0;
2323 }
2324
2325 if (st->aggr_mode != AGGR_UNSET)
2326 stat_config.aggr_mode = st->aggr_mode;
2327
Jiri Olsa68d702f2015-11-05 15:40:58 +01002328 if (perf_stat.file.is_pipe)
2329 perf_stat_init_aggr_mode();
2330 else
2331 perf_stat_init_aggr_mode_file(st);
2332
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002333 return 0;
2334}
2335
Jiri Olsa1975d362015-11-05 15:40:56 +01002336static int set_maps(struct perf_stat *st)
2337{
2338 if (!st->cpus || !st->threads)
2339 return 0;
2340
2341 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2342 return -EINVAL;
2343
2344 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2345
2346 if (perf_evlist__alloc_stats(evsel_list, true))
2347 return -ENOMEM;
2348
2349 st->maps_allocated = true;
2350 return 0;
2351}
2352
2353static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002354int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002355 union perf_event *event,
2356 struct perf_session *session __maybe_unused)
2357{
2358 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2359
2360 if (st->threads) {
2361 pr_warning("Extra thread map event, ignoring.\n");
2362 return 0;
2363 }
2364
2365 st->threads = thread_map__new_event(&event->thread_map);
2366 if (!st->threads)
2367 return -ENOMEM;
2368
2369 return set_maps(st);
2370}
2371
2372static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002373int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002374 union perf_event *event,
2375 struct perf_session *session __maybe_unused)
2376{
2377 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2378 struct cpu_map *cpus;
2379
2380 if (st->cpus) {
2381 pr_warning("Extra cpu map event, ignoring.\n");
2382 return 0;
2383 }
2384
2385 cpus = cpu_map__new_data(&event->cpu_map.data);
2386 if (!cpus)
2387 return -ENOMEM;
2388
2389 st->cpus = cpus;
2390 return set_maps(st);
2391}
2392
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002393static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002394 "perf stat report [<options>]",
2395 NULL,
2396};
2397
2398static struct perf_stat perf_stat = {
2399 .tool = {
2400 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002401 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002402 .thread_map = process_thread_map_event,
2403 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002404 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002405 .stat = perf_event__process_stat_event,
2406 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002407 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002408 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002409};
2410
2411static int __cmd_report(int argc, const char **argv)
2412{
2413 struct perf_session *session;
2414 const struct option options[] = {
2415 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002416 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2417 "aggregate counts per processor socket", AGGR_SOCKET),
2418 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2419 "aggregate counts per physical processor core", AGGR_CORE),
2420 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2421 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002422 OPT_END()
2423 };
2424 struct stat st;
2425 int ret;
2426
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002427 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002428
2429 if (!input_name || !strlen(input_name)) {
2430 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2431 input_name = "-";
2432 else
2433 input_name = "perf.data";
2434 }
2435
2436 perf_stat.file.path = input_name;
2437 perf_stat.file.mode = PERF_DATA_MODE_READ;
2438
2439 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2440 if (session == NULL)
2441 return -1;
2442
2443 perf_stat.session = session;
2444 stat_config.output = stderr;
2445 evsel_list = session->evlist;
2446
2447 ret = perf_session__process_events(session);
2448 if (ret)
2449 return ret;
2450
2451 perf_session__delete(session);
2452 return 0;
2453}
2454
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002455static void setup_system_wide(int forks)
2456{
2457 /*
2458 * Make system wide (-a) the default target if
2459 * no target was specified and one of following
2460 * conditions is met:
2461 *
2462 * - there's no workload specified
2463 * - there is workload specified but all requested
2464 * events are system wide events
2465 */
2466 if (!target__none(&target))
2467 return;
2468
2469 if (!forks)
2470 target.system_wide = true;
2471 else {
2472 struct perf_evsel *counter;
2473
2474 evlist__for_each_entry(evsel_list, counter) {
2475 if (!counter->system_wide)
2476 return;
2477 }
2478
2479 if (evsel_list->nr_entries)
2480 target.system_wide = true;
2481 }
2482}
2483
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002484int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002485{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002486 const char * const stat_usage[] = {
2487 "perf stat [<options>] [<command>]",
2488 NULL
2489 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002490 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002491 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002492 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002493 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002494 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002495
Stephane Eranian5af52b52010-05-18 15:00:01 +02002496 setlocale(LC_ALL, "");
2497
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002498 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002499 if (evsel_list == NULL)
2500 return -ENOMEM;
2501
Wang Nan1669e502016-02-19 11:43:58 +00002502 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002503 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2504 (const char **) stat_usage,
2505 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002506 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002507 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002508
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002509 if (csv_sep) {
2510 csv_output = true;
2511 if (!strcmp(csv_sep, "\\t"))
2512 csv_sep = "\t";
2513 } else
2514 csv_sep = DEFAULT_SEPARATOR;
2515
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002516 if (argc && !strncmp(argv[0], "rec", 3)) {
2517 argc = __cmd_record(argc, argv);
2518 if (argc < 0)
2519 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002520 } else if (argc && !strncmp(argv[0], "rep", 3))
2521 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002522
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002523 interval = stat_config.interval;
2524
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002525 /*
2526 * For record command the -o is already taken care of.
2527 */
2528 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002529 output = NULL;
2530
Jim Cromie56f3bae2011-09-07 17:14:00 -06002531 if (output_name && output_fd) {
2532 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002533 parse_options_usage(stat_usage, stat_options, "o", 1);
2534 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002535 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002536 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002537
Andi Kleen54b50912016-03-03 15:57:36 -08002538 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2539 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2540 goto out;
2541 }
2542
Andi Kleen54b50912016-03-03 15:57:36 -08002543 if (metric_only && run_count > 1) {
2544 fprintf(stderr, "--metric-only is not supported with -r\n");
2545 goto out;
2546 }
2547
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002548 if (output_fd < 0) {
2549 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002550 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002551 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002552 }
2553
Stephane Eranian4aa90152011-08-15 22:22:33 +02002554 if (!output) {
2555 struct timespec tm;
2556 mode = append_file ? "a" : "w";
2557
2558 output = fopen(output_name, mode);
2559 if (!output) {
2560 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002561 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002562 }
2563 clock_gettime(CLOCK_REALTIME, &tm);
2564 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002565 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002566 mode = append_file ? "a" : "w";
2567 output = fdopen(output_fd, mode);
2568 if (!output) {
2569 perror("Failed opening logfd");
2570 return -errno;
2571 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002572 }
2573
Jiri Olsa58215222015-07-21 14:31:24 +02002574 stat_config.output = output;
2575
Stephane Eraniand7470b62010-12-01 18:49:05 +02002576 /*
2577 * let the spreadsheet do the pretty-printing
2578 */
2579 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002580 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002581 if (big_num_opt == 1) {
2582 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002583 parse_options_usage(stat_usage, stat_options, "B", 1);
2584 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002585 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002586 } else /* Nope, so disable big number formatting */
2587 big_num = false;
2588 } else if (big_num_opt == 0) /* User passed --no-big-num */
2589 big_num = false;
2590
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002591 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002592
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002593 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002594 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002595 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002596 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002597 } else if (run_count == 0) {
2598 forever = true;
2599 run_count = 1;
2600 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002601
Jiri Olsa421a50f2015-07-21 14:31:22 +02002602 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002603 fprintf(stderr, "The --per-thread option is only available "
2604 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002605 parse_options_usage(NULL, stat_options, "p", 1);
2606 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002607 goto out;
2608 }
2609
2610 /*
2611 * no_aggr, cgroup are for system-wide only
2612 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2613 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002614 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2615 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002616 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002617 fprintf(stderr, "both cgroup and no-aggregation "
2618 "modes only available in system-wide mode\n");
2619
Jiri Olsae0547312015-11-05 15:40:45 +01002620 parse_options_usage(stat_usage, stat_options, "G", 1);
2621 parse_options_usage(NULL, stat_options, "A", 1);
2622 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002623 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002624 }
2625
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002626 if (add_default_attributes())
2627 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002628
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002629 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002630
Namhyung Kim77a6f012012-05-07 14:09:04 +09002631 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002632 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002633 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002634 parse_options_usage(stat_usage, stat_options, "p", 1);
2635 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002636 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002637 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002638 parse_options_usage(stat_usage, stat_options, "C", 1);
2639 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002640 }
2641 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002642 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002643
2644 /*
2645 * Initialize thread_map with comm names,
2646 * so we could print it out on output.
2647 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002648 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02002649 thread_map__read_comms(evsel_list->threads);
2650
Stephane Eranian13370a92013-01-29 12:47:44 +01002651 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002652 if (interval < 10) {
2653 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002654 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002655 goto out;
2656 } else
2657 pr_warning("print interval < 100ms. "
2658 "The overhead percentage could be high in some cases. "
2659 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002660 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002661
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002662 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002663 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002664
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002665 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002666 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002667
Ingo Molnar58d7e992009-05-15 11:03:23 +02002668 /*
2669 * We dont want to block the signals - that would cause
2670 * child tasks to inherit that and Ctrl-C would not work.
2671 * What we want is for Ctrl-C to work in the exec()-ed
2672 * task, but being ignored by perf stat itself:
2673 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002674 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002675 if (!forever)
2676 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002677 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002678 signal(SIGALRM, skip_signal);
2679 signal(SIGABRT, skip_signal);
2680
Ingo Molnar42202dd2009-06-13 14:57:28 +02002681 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002682 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002683 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002684 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2685 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002686
Ingo Molnar42202dd2009-06-13 14:57:28 +02002687 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002688 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002689 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002690 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002691 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002692 }
2693
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002694 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002695 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002696
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002697 if (STAT_RECORD) {
2698 /*
2699 * We synthesize the kernel mmap record just so that older tools
2700 * don't emit warnings about not being able to resolve symbols
2701 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2702 * a saner message about no samples being in the perf.data file.
2703 *
2704 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002705 * in header.c at the moment 'perf stat record' gets introduced, which
2706 * is not really needed once we start adding the stat specific PERF_RECORD_
2707 * records, but the need to suppress the kptr_restrict messages in older
2708 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002709 */
2710 int fd = perf_data_file__fd(&perf_stat.file);
2711 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2712 process_synthesized_event,
2713 &perf_stat.session->machines.host);
2714 if (err) {
2715 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2716 "older tools may produce warnings about this file\n.");
2717 }
2718
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002719 if (!interval) {
2720 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2721 pr_err("failed to write stat round event\n");
2722 }
2723
Jiri Olsa664c98d2015-11-05 15:40:50 +01002724 if (!perf_stat.file.is_pipe) {
2725 perf_stat.session->header.data_size += perf_stat.bytes_written;
2726 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2727 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002728
2729 perf_session__delete(perf_stat.session);
2730 }
2731
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002732 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002733 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002734out:
2735 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002736 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002737}