blob: e3837febb4ffb796e55fdf3349ad95de3218e92d [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"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030067#include "util/string2.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010068#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020069
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030070#include <linux/time64.h>
Andi Kleen44b1e602016-05-30 12:49:42 -030071#include <api/fs/fs.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030072#include <errno.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030073#include <signal.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020074#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020075#include <sys/prctl.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030076#include <inttypes.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020077#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070078#include <math.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020079
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030080#include "sane_ctype.h"
81
Stephane Eraniand7470b62010-12-01 18:49:05 +020082#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060083#define CNTR_NOT_SUPPORTED "<not supported>"
84#define CNTR_NOT_COUNTED "<not counted>"
Stephane Eraniand7470b62010-12-01 18:49:05 +020085
Jiri Olsad4f63a42015-06-26 11:29:26 +020086static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010087
Andi Kleen4cabc3d2013-08-21 16:47:26 -070088/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020089static const char *transaction_attrs = {
90 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070091 "{"
92 "instructions,"
93 "cycles,"
94 "cpu/cycles-t/,"
95 "cpu/tx-start/,"
96 "cpu/el-start/,"
97 "cpu/cycles-ct/"
98 "}"
99};
100
101/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +0200102static const char * transaction_limited_attrs = {
103 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700104 "{"
105 "instructions,"
106 "cycles,"
107 "cpu/cycles-t/,"
108 "cpu/tx-start/"
109 "}"
110};
111
Andi Kleen44b1e602016-05-30 12:49:42 -0300112static const char * topdown_attrs[] = {
113 "topdown-total-slots",
114 "topdown-slots-retired",
115 "topdown-recovery-bubbles",
116 "topdown-fetch-bubbles",
117 "topdown-slots-issued",
118 NULL,
119};
120
Robert Richter666e6d42012-04-05 18:26:27 +0200121static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200122
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300123static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900124 .uid = UINT_MAX,
125};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530126
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100127typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
128
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530129static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200130static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200131static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000132static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200133static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700134static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300135static bool topdown_run = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200136static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200137static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200138static const char *csv_sep = NULL;
139static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800140static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200141static const char *pre_cmd = NULL;
142static const char *post_cmd = NULL;
143static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700144static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100145static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500146static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800147static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300148static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700149static bool no_merge = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100150static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100151static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100152static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100153static bool append_file;
154static const char *output_name;
155static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100156static int print_free_counters_hint;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200157
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100158struct perf_stat {
159 bool record;
160 struct perf_data_file file;
161 struct perf_session *session;
162 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100163 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100164 bool maps_allocated;
165 struct cpu_map *cpus;
166 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100167 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100168};
169
170static struct perf_stat perf_stat;
171#define STAT_RECORD perf_stat.record
172
Liming Wang60666c62009-12-31 16:05:50 +0800173static volatile int done = 0;
174
Jiri Olsa421a50f2015-07-21 14:31:22 +0200175static struct perf_stat_config stat_config = {
176 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200177 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200178};
179
Stephane Eranian13370a92013-01-29 12:47:44 +0100180static inline void diff_timespec(struct timespec *r, struct timespec *a,
181 struct timespec *b)
182{
183 r->tv_sec = a->tv_sec - b->tv_sec;
184 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300185 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100186 r->tv_sec--;
187 } else {
188 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
189 }
190}
191
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200192static void perf_stat__reset_stats(void)
193{
194 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200195 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200196}
197
Jiri Olsacac21422012-11-12 18:34:00 +0100198static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200199{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200200 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200201
Jiri Olsa711a5722015-07-21 14:31:23 +0200202 if (stat_config.scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200203 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
204 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200205
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200206 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300207
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100208 /*
209 * Some events get initialized with sample_(period/type) set,
210 * like tracepoints. Clear it up for counting.
211 */
212 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100213
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100214 /*
215 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
216 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100217 *
218 * However for pipe sessions we need to keep it zero,
219 * because script's perf_evsel__check_attr is triggered
220 * by attr->sample_type != 0, and we can't run it on
221 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100222 */
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100223 if (!(STAT_RECORD && perf_stat.file.is_pipe))
224 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100225
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100226 /*
227 * Disabling all counters initially, they will be enabled
228 * either manually by us or by kernel via enable_on_exec
229 * set later.
230 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100231 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100232 attr->disabled = 1;
233
Jiri Olsac8280ce2015-12-03 10:06:45 +0100234 /*
235 * In case of initial_delay we enable tracee
236 * events manually.
237 */
238 if (target__none(&target) && !initial_delay)
239 attr->enable_on_exec = 1;
240 }
241
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300242 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300243 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200244
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300245 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200246}
247
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200248/*
249 * Does the counter have nsecs as a unit?
250 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200251static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200252{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200253 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
254 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200255 return 1;
256
257 return 0;
258}
259
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100260static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100261 union perf_event *event,
262 struct perf_sample *sample __maybe_unused,
263 struct machine *machine __maybe_unused)
264{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100265 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
266 pr_err("failed to write perf data, error: %m\n");
267 return -1;
268 }
269
270 perf_stat.bytes_written += event->header.size;
271 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100272}
273
Jiri Olsa1975d362015-11-05 15:40:56 +0100274static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100275{
Jiri Olsa1975d362015-11-05 15:40:56 +0100276 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100277 process_synthesized_event,
278 NULL);
279}
280
281#define WRITE_STAT_ROUND_EVENT(time, interval) \
282 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
283
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100284#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
285
286static int
287perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
288 struct perf_counts_values *count)
289{
290 struct perf_sample_id *sid = SID(counter, cpu, thread);
291
292 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
293 process_synthesized_event, NULL);
294}
295
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200296/*
297 * Read out the results of a single counter:
298 * do not aggregate counts across CPUs in system-wide mode
299 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200300static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200301{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100302 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100303 int ncpus, cpu, thread;
304
305 if (target__has_cpu(&target))
306 ncpus = perf_evsel__nr_cpus(counter);
307 else
308 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200309
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000310 if (!counter->supported)
311 return -ENOENT;
312
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100313 if (counter->system_wide)
314 nthreads = 1;
315
316 for (thread = 0; thread < nthreads; thread++) {
317 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200318 struct perf_counts_values *count;
319
320 count = perf_counts(counter->counts, cpu, thread);
Stephane Eraniandb49a712017-04-12 11:23:01 -0700321 if (perf_evsel__read(counter, cpu, thread, count)) {
322 counter->counts->scaled = -1;
323 perf_counts(counter->counts, cpu, thread)->ena = 0;
324 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100325 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700326 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100327
328 if (STAT_RECORD) {
329 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
330 pr_err("failed to write stat event\n");
331 return -1;
332 }
333 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700334
335 if (verbose > 1) {
336 fprintf(stat_config.output,
337 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
338 perf_evsel__name(counter),
339 cpu,
340 count->val, count->ena, count->run);
341 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100342 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200343 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200344
345 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200346}
347
Mark Rutland3df33ef2016-08-09 14:04:29 +0100348static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200349{
350 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700351 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200352
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300353 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700354 ret = read_counter(counter);
355 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700356 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200357
Stephane Eraniandb49a712017-04-12 11:23:01 -0700358 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200359 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200360 }
361}
362
Jiri Olsaba411a92015-06-26 11:29:24 +0200363static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100364{
Stephane Eranian13370a92013-01-29 12:47:44 +0100365 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100366
Mark Rutland3df33ef2016-08-09 14:04:29 +0100367 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100368
Stephane Eranian13370a92013-01-29 12:47:44 +0100369 clock_gettime(CLOCK_MONOTONIC, &ts);
370 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100371
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100372 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300373 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100374 pr_err("failed to write stat round event\n");
375 }
376
Jiri Olsad4f63a42015-06-26 11:29:26 +0200377 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100378}
379
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100380static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700381{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100382 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300383 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100384
385 /*
386 * We need to enable counters only if:
387 * - we don't have tracee (attaching to task or cpu)
388 * - we have initial delay configured
389 */
390 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100391 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700392}
393
Mark Rutland3df33ef2016-08-09 14:04:29 +0100394static void disable_counters(void)
395{
396 /*
397 * If we don't have tracee (attaching to task or cpu), counters may
398 * still be running. To get accurate group ratios, we must stop groups
399 * from counting before reading their constituent counters.
400 */
401 if (!target__none(&target))
402 perf_evlist__disable(evsel_list);
403}
404
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300405static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300406
407/*
408 * perf_evlist__prepare_workload will send a SIGUSR1
409 * if the fork fails, since we asked by setting its
410 * want_signal to true.
411 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300412static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
413 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300414{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300415 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300416}
417
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100418static bool has_unit(struct perf_evsel *counter)
419{
420 return counter->unit && *counter->unit;
421}
422
423static bool has_scale(struct perf_evsel *counter)
424{
425 return counter->scale != 1;
426}
427
Jiri Olsa664c98d2015-11-05 15:40:50 +0100428static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100429{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100430 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100431 int err;
432
Jiri Olsa664c98d2015-11-05 15:40:50 +0100433 if (is_pipe) {
434 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
435 process_synthesized_event);
436 if (err < 0) {
437 pr_err("Couldn't synthesize attrs.\n");
438 return err;
439 }
440 }
441
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100442 /*
443 * Synthesize other events stuff not carried within
444 * attr event - unit, scale, name
445 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300446 evlist__for_each_entry(evsel_list, counter) {
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100447 if (!counter->supported)
448 continue;
449
450 /*
451 * Synthesize unit and scale only if it's defined.
452 */
453 if (has_unit(counter)) {
454 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
455 if (err < 0) {
456 pr_err("Couldn't synthesize evsel unit.\n");
457 return err;
458 }
459 }
460
461 if (has_scale(counter)) {
462 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
463 if (err < 0) {
464 pr_err("Couldn't synthesize evsel scale.\n");
465 return err;
466 }
467 }
468
469 if (counter->own_cpus) {
470 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
471 if (err < 0) {
472 pr_err("Couldn't synthesize evsel scale.\n");
473 return err;
474 }
475 }
476
477 /*
478 * Name is needed only for pipe output,
479 * perf.data carries event names.
480 */
481 if (is_pipe) {
482 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
483 if (err < 0) {
484 pr_err("Couldn't synthesize evsel name.\n");
485 return err;
486 }
487 }
488 }
489
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100490 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
491 process_synthesized_event,
492 NULL);
493 if (err < 0) {
494 pr_err("Couldn't synthesize thread map.\n");
495 return err;
496 }
497
498 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
499 process_synthesized_event, NULL);
500 if (err < 0) {
501 pr_err("Couldn't synthesize thread map.\n");
502 return err;
503 }
504
505 err = perf_event__synthesize_stat_config(NULL, &stat_config,
506 process_synthesized_event, NULL);
507 if (err < 0) {
508 pr_err("Couldn't synthesize config.\n");
509 return err;
510 }
511
512 return 0;
513}
514
Jiri Olsa2af46462015-11-05 15:40:49 +0100515#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
516
517static int __store_counter_ids(struct perf_evsel *counter,
518 struct cpu_map *cpus,
519 struct thread_map *threads)
520{
521 int cpu, thread;
522
523 for (cpu = 0; cpu < cpus->nr; cpu++) {
524 for (thread = 0; thread < threads->nr; thread++) {
525 int fd = FD(counter, cpu, thread);
526
527 if (perf_evlist__id_add_fd(evsel_list, counter,
528 cpu, thread, fd) < 0)
529 return -1;
530 }
531 }
532
533 return 0;
534}
535
536static int store_counter_ids(struct perf_evsel *counter)
537{
538 struct cpu_map *cpus = counter->cpus;
539 struct thread_map *threads = counter->threads;
540
541 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
542 return -ENOMEM;
543
544 return __store_counter_ids(counter, cpus, threads);
545}
546
Namhyung Kimacf28922013-03-11 16:43:18 +0900547static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200548{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200549 int interval = stat_config.interval;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300550 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200551 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100552 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100553 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100554 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200555 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300556 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100557 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600558 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200559
Stephane Eranian13370a92013-01-29 12:47:44 +0100560 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300561 ts.tv_sec = interval / USEC_PER_MSEC;
562 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100563 } else {
564 ts.tv_sec = 1;
565 ts.tv_nsec = 0;
566 }
567
Liming Wang60666c62009-12-31 16:05:50 +0800568 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100569 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300570 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900571 perror("failed to prepare workload");
572 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800573 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900574 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000575 }
576
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200577 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300578 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200579
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300580 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300581try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100582 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600583 /*
584 * PPC returns ENXIO for HW counters until 2.6.37
585 * (behavior changed with commit b0a873e).
586 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100587 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600588 errno == ENOENT || errno == EOPNOTSUPP ||
589 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900590 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600591 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300592 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600593 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400594
595 if ((counter->leader != counter) ||
596 !(counter->leader->nr_members > 1))
597 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300598 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900599 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300600 ui__warning("%s\n", msg);
601 goto try_again;
602 }
Ingo Molnarede70292011-04-28 08:48:42 +0200603
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300604 perf_evsel__open_strerror(counter, &target,
605 errno, msg, sizeof(msg));
606 ui__error("%s\n", msg);
607
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200608 if (child_pid != -1)
609 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600610
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200611 return -1;
612 }
David Ahern2cee77c2011-05-30 08:55:59 -0600613 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100614
615 l = strlen(counter->unit);
616 if (l > unit_width)
617 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100618
619 if (STAT_RECORD && store_counter_ids(counter))
620 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300621 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200622
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300623 if (perf_evlist__apply_filters(evsel_list, &counter)) {
624 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
625 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300626 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100627 return -1;
628 }
629
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600630 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
631 error("failed to set config \"%s\" on event %s with %d (%s)\n",
632 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
633 str_error_r(errno, msg, sizeof(msg)));
634 return -1;
635 }
636
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100637 if (STAT_RECORD) {
638 int err, fd = perf_data_file__fd(&perf_stat.file);
639
Jiri Olsa664c98d2015-11-05 15:40:50 +0100640 if (is_pipe) {
641 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
642 } else {
643 err = perf_session__write_header(perf_stat.session, evsel_list,
644 fd, false);
645 }
646
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100647 if (err < 0)
648 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100649
Jiri Olsa664c98d2015-11-05 15:40:50 +0100650 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100651 if (err < 0)
652 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100653 }
654
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200655 /*
656 * Enable counters and exec the command:
657 */
658 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100659 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200660
Liming Wang60666c62009-12-31 16:05:50 +0800661 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900662 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100663 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900664
Stephane Eranian13370a92013-01-29 12:47:44 +0100665 if (interval) {
666 while (!waitpid(child_pid, &status, WNOHANG)) {
667 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200668 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100669 }
670 }
Liming Wang60666c62009-12-31 16:05:50 +0800671 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300672
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300673 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300674 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300675 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300676 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300677 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300678
Andi Kleen33e49ea2011-09-15 14:31:40 -0700679 if (WIFSIGNALED(status))
680 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800681 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100682 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100683 while (!done) {
684 nanosleep(&ts, NULL);
685 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200686 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100687 }
Liming Wang60666c62009-12-31 16:05:50 +0800688 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200689
Mark Rutland3df33ef2016-08-09 14:04:29 +0100690 disable_counters();
691
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200692 t1 = rdclock();
693
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200694 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200695
Mark Rutland3df33ef2016-08-09 14:04:29 +0100696 /*
697 * Closing a group leader splits the group, and as we only disable
698 * group leaders, results in remaining events becoming enabled. To
699 * avoid arbitrary skew, we must read all counters before closing any
700 * group leaders.
701 */
702 read_counters();
703 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200704
Ingo Molnar42202dd2009-06-13 14:57:28 +0200705 return WEXITSTATUS(status);
706}
707
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300708static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200709{
710 int ret;
711
712 if (pre_cmd) {
713 ret = system(pre_cmd);
714 if (ret)
715 return ret;
716 }
717
718 if (sync_run)
719 sync();
720
721 ret = __run_perf_stat(argc, argv);
722 if (ret)
723 return ret;
724
725 if (post_cmd) {
726 ret = system(post_cmd);
727 if (ret)
728 return ret;
729 }
730
731 return ret;
732}
733
Andi Kleend73515c2015-03-11 07:16:27 -0700734static void print_running(u64 run, u64 ena)
735{
736 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200737 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700738 csv_sep,
739 run,
740 csv_sep,
741 ena ? 100.0 * run / ena : 100.0);
742 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200743 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700744 }
745}
746
Ingo Molnarf99844c2011-04-27 05:35:39 +0200747static void print_noise_pct(double total, double avg)
748{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800749 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200750
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700751 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200752 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600753 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200754 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200755}
756
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200757static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200758{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200759 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200760
Peter Zijlstra849abde2009-09-04 18:23:38 +0200761 if (run_count == 1)
762 return;
763
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200764 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200765 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200766}
767
Stephane Eranian12c08a92013-02-14 13:57:29 +0100768static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200769{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200770 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100771 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200772 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100773 cpu_map__id_to_socket(id),
774 csv_output ? 0 : -8,
775 cpu_map__id_to_cpu(id),
776 csv_sep,
777 csv_output ? 0 : 4,
778 nr,
779 csv_sep);
780 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100781 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200782 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100783 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100784 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100785 csv_sep,
786 csv_output ? 0 : 4,
787 nr,
788 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100789 break;
790 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200791 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200792 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100793 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100794 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200795 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200796 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200797 csv_output ? 0 : 16,
798 thread_map__comm(evsel->threads, id),
799 csv_output ? 0 : -8,
800 thread_map__pid(evsel->threads, id),
801 csv_sep);
802 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100803 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200804 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100805 default:
806 break;
807 }
808}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200809
Andi Kleen140aead2016-01-30 09:06:49 -0800810struct outstate {
811 FILE *fh;
812 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800813 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800814 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800815 int id, nr;
816 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800817};
818
819#define METRIC_LEN 35
820
821static void new_line_std(void *ctx)
822{
823 struct outstate *os = ctx;
824
825 os->newline = true;
826}
827
828static void do_new_line_std(struct outstate *os)
829{
830 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800831 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800832 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800833 if (stat_config.aggr_mode == AGGR_NONE)
834 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800835 fprintf(os->fh, " ");
836}
837
838static void print_metric_std(void *ctx, const char *color, const char *fmt,
839 const char *unit, double val)
840{
841 struct outstate *os = ctx;
842 FILE *out = os->fh;
843 int n;
844 bool newline = os->newline;
845
846 os->newline = false;
847
848 if (unit == NULL || fmt == NULL) {
849 fprintf(out, "%-*s", METRIC_LEN, "");
850 return;
851 }
852
853 if (newline)
854 do_new_line_std(os);
855
856 n = fprintf(out, " # ");
857 if (color)
858 n += color_fprintf(out, color, fmt, val);
859 else
860 n += fprintf(out, fmt, val);
861 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
862}
863
Andi Kleen92a61f62016-02-29 14:36:21 -0800864static void new_line_csv(void *ctx)
865{
866 struct outstate *os = ctx;
867 int i;
868
869 fputc('\n', os->fh);
870 if (os->prefix)
871 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800872 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800873 for (i = 0; i < os->nfields; i++)
874 fputs(csv_sep, os->fh);
875}
876
877static void print_metric_csv(void *ctx,
878 const char *color __maybe_unused,
879 const char *fmt, const char *unit, double val)
880{
881 struct outstate *os = ctx;
882 FILE *out = os->fh;
883 char buf[64], *vals, *ends;
884
885 if (unit == NULL || fmt == NULL) {
886 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
887 return;
888 }
889 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900890 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800891 while (isdigit(*ends) || *ends == '.')
892 ends++;
893 *ends = 0;
894 while (isspace(*unit))
895 unit++;
896 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
897}
898
Andi Kleen54b50912016-03-03 15:57:36 -0800899#define METRIC_ONLY_LEN 20
900
901/* Filter out some columns that don't work well in metrics only mode */
902
903static bool valid_only_metric(const char *unit)
904{
905 if (!unit)
906 return false;
907 if (strstr(unit, "/sec") ||
908 strstr(unit, "hz") ||
909 strstr(unit, "Hz") ||
910 strstr(unit, "CPUs utilized"))
911 return false;
912 return true;
913}
914
915static const char *fixunit(char *buf, struct perf_evsel *evsel,
916 const char *unit)
917{
918 if (!strncmp(unit, "of all", 6)) {
919 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
920 unit);
921 return buf;
922 }
923 return unit;
924}
925
926static void print_metric_only(void *ctx, const char *color, const char *fmt,
927 const char *unit, double val)
928{
929 struct outstate *os = ctx;
930 FILE *out = os->fh;
931 int n;
932 char buf[1024];
933 unsigned mlen = METRIC_ONLY_LEN;
934
935 if (!valid_only_metric(unit))
936 return;
937 unit = fixunit(buf, os->evsel, unit);
938 if (color)
939 n = color_fprintf(out, color, fmt, val);
940 else
941 n = fprintf(out, fmt, val);
942 if (n > METRIC_ONLY_LEN)
943 n = METRIC_ONLY_LEN;
944 if (mlen < strlen(unit))
945 mlen = strlen(unit) + 1;
946 fprintf(out, "%*s", mlen - n, "");
947}
948
949static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
950 const char *fmt,
951 const char *unit, double val)
952{
953 struct outstate *os = ctx;
954 FILE *out = os->fh;
955 char buf[64], *vals, *ends;
956 char tbuf[1024];
957
958 if (!valid_only_metric(unit))
959 return;
960 unit = fixunit(tbuf, os->evsel, unit);
961 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900962 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -0800963 while (isdigit(*ends) || *ends == '.')
964 ends++;
965 *ends = 0;
966 fprintf(out, "%s%s", vals, csv_sep);
967}
968
969static void new_line_metric(void *ctx __maybe_unused)
970{
971}
972
973static void print_metric_header(void *ctx, const char *color __maybe_unused,
974 const char *fmt __maybe_unused,
975 const char *unit, double val __maybe_unused)
976{
977 struct outstate *os = ctx;
978 char tbuf[1024];
979
980 if (!valid_only_metric(unit))
981 return;
982 unit = fixunit(tbuf, os->evsel, unit);
983 if (csv_output)
984 fprintf(os->fh, "%s%s", unit, csv_sep);
985 else
986 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
987}
988
Andi Kleenda88c7f2014-09-24 13:50:46 -0700989static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100990{
Jiri Olsa58215222015-07-21 14:31:24 +0200991 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300992 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +0100993 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600994 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100995
Stephane Eranian410136f2013-11-12 17:58:49 +0100996 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
997 fmt_n = csv_output ? "%s" : "%-25s";
998
Andi Kleenda88c7f2014-09-24 13:50:46 -0700999 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001000
David Ahern4bbe5a62013-09-28 14:28:00 -06001001 scnprintf(name, sizeof(name), "%s%s",
1002 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +01001003
1004 fprintf(output, fmt_v, msecs, csv_sep);
1005
1006 if (csv_output)
1007 fprintf(output, "%s%s", evsel->unit, csv_sep);
1008 else
1009 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1010
1011 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001012
Stephane Eranian023695d2011-02-14 11:20:01 +02001013 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001014 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001015}
1016
Andi Kleen44d49a62016-02-29 14:36:22 -08001017static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1018{
1019 int i;
1020
1021 if (!aggr_get_id)
1022 return 0;
1023
1024 if (stat_config.aggr_mode == AGGR_NONE)
1025 return id;
1026
1027 if (stat_config.aggr_mode == AGGR_GLOBAL)
1028 return 0;
1029
1030 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1031 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1032
1033 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1034 return cpu2;
1035 }
1036 return 0;
1037}
1038
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001039static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1040{
Jiri Olsa58215222015-07-21 14:31:24 +02001041 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001042 double sc = evsel->scale;
1043 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001044
1045 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001046 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001047 } else {
1048 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001049 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001050 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001051 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001052 }
1053
1054 aggr_printout(evsel, id, nr);
1055
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001056 fprintf(output, fmt, avg, csv_sep);
1057
1058 if (evsel->unit)
1059 fprintf(output, "%-*s%s",
1060 csv_output ? 0 : unit_width,
1061 evsel->unit, csv_sep);
1062
1063 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1064
1065 if (evsel->cgrp)
1066 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001067}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001068
Andi Kleenf9483392016-01-30 09:06:50 -08001069static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Andi Kleencb110f42016-01-30 09:06:51 -08001070 char *prefix, u64 run, u64 ena, double noise)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001071{
Andi Kleen140aead2016-01-30 09:06:49 -08001072 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001073 struct outstate os = {
1074 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001075 .prefix = prefix ? prefix : "",
1076 .id = id,
1077 .nr = nr,
1078 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001079 };
Andi Kleen140aead2016-01-30 09:06:49 -08001080 print_metric_t pm = print_metric_std;
1081 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001082
Andi Kleen54b50912016-03-03 15:57:36 -08001083 if (metric_only) {
1084 nl = new_line_metric;
1085 if (csv_output)
1086 pm = print_metric_only_csv;
1087 else
1088 pm = print_metric_only;
1089 } else
1090 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001091
Andi Kleen54b50912016-03-03 15:57:36 -08001092 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001093 static int aggr_fields[] = {
1094 [AGGR_GLOBAL] = 0,
1095 [AGGR_THREAD] = 1,
1096 [AGGR_NONE] = 1,
1097 [AGGR_SOCKET] = 2,
1098 [AGGR_CORE] = 2,
1099 };
1100
1101 pm = print_metric_csv;
1102 nl = new_line_csv;
1103 os.nfields = 3;
1104 os.nfields += aggr_fields[stat_config.aggr_mode];
1105 if (counter->cgrp)
1106 os.nfields++;
1107 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001108 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001109 if (metric_only) {
1110 pm(&os, NULL, "", "", 0);
1111 return;
1112 }
Andi Kleencb110f42016-01-30 09:06:51 -08001113 aggr_printout(counter, id, nr);
1114
1115 fprintf(stat_config.output, "%*s%s",
1116 csv_output ? 0 : 18,
1117 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1118 csv_sep);
1119
Borislav Petkov02d492e2017-02-07 01:40:05 +01001120 if (counter->supported)
1121 print_free_counters_hint = 1;
1122
Andi Kleencb110f42016-01-30 09:06:51 -08001123 fprintf(stat_config.output, "%-*s%s",
1124 csv_output ? 0 : unit_width,
1125 counter->unit, csv_sep);
1126
1127 fprintf(stat_config.output, "%*s",
1128 csv_output ? 0 : -25,
1129 perf_evsel__name(counter));
1130
1131 if (counter->cgrp)
1132 fprintf(stat_config.output, "%s%s",
1133 csv_sep, counter->cgrp->name);
1134
Andi Kleen92a61f62016-02-29 14:36:21 -08001135 if (!csv_output)
1136 pm(&os, NULL, NULL, "", 0);
1137 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001138 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001139 if (csv_output)
1140 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001141 return;
1142 }
1143
Andi Kleen54b50912016-03-03 15:57:36 -08001144 if (metric_only)
1145 /* nothing */;
1146 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001147 nsec_printout(id, nr, counter, uval);
1148 else
1149 abs_printout(id, nr, counter, uval);
1150
Andi Kleen140aead2016-01-30 09:06:49 -08001151 out.print_metric = pm;
1152 out.new_line = nl;
1153 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001154 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001155
Andi Kleen54b50912016-03-03 15:57:36 -08001156 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001157 print_noise(counter, noise);
1158 print_running(run, ena);
1159 }
1160
1161 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001162 first_shadow_cpu(counter, id),
Andi Kleen140aead2016-01-30 09:06:49 -08001163 &out);
Andi Kleen54b50912016-03-03 15:57:36 -08001164 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001165 print_noise(counter, noise);
1166 print_running(run, ena);
1167 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001168}
1169
Andi Kleen44d49a62016-02-29 14:36:22 -08001170static void aggr_update_shadow(void)
1171{
1172 int cpu, s2, id, s;
1173 u64 val;
1174 struct perf_evsel *counter;
1175
1176 for (s = 0; s < aggr_map->nr; s++) {
1177 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001178 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001179 val = 0;
1180 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1181 s2 = aggr_get_id(evsel_list->cpus, cpu);
1182 if (s2 != id)
1183 continue;
1184 val += perf_counts(counter->counts, cpu, 0)->val;
1185 }
1186 val = val * counter->scale;
1187 perf_stat__update_shadow_stats(counter, &val,
1188 first_shadow_cpu(counter, id));
1189 }
1190 }
1191}
1192
Andi Kleen430daf22017-03-20 13:17:00 -07001193static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001194 void (*cb)(struct perf_evsel *counter, void *data,
1195 bool first),
1196 void *data)
1197{
Andi Kleen430daf22017-03-20 13:17:00 -07001198 struct perf_evsel *alias;
1199
1200 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1201 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1202 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1203 alias->scale != counter->scale ||
1204 alias->cgrp != counter->cgrp ||
1205 strcmp(alias->unit, counter->unit) ||
1206 nsec_counter(alias) != nsec_counter(counter))
1207 break;
1208 alias->merged_stat = true;
1209 cb(alias, data, false);
1210 }
1211}
1212
1213static bool collect_data(struct perf_evsel *counter,
1214 void (*cb)(struct perf_evsel *counter, void *data,
1215 bool first),
1216 void *data)
1217{
1218 if (counter->merged_stat)
1219 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001220 cb(counter, data, true);
Andi Kleen430daf22017-03-20 13:17:00 -07001221 if (!no_merge)
1222 collect_all_aliases(counter, cb, data);
1223 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001224}
1225
1226struct aggr_data {
1227 u64 ena, run, val;
1228 int id;
1229 int nr;
1230 int cpu;
1231};
1232
1233static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1234{
1235 struct aggr_data *ad = data;
1236 int cpu, s2;
1237
1238 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1239 struct perf_counts_values *counts;
1240
1241 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1242 if (s2 != ad->id)
1243 continue;
1244 if (first)
1245 ad->nr++;
1246 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001247 /*
1248 * When any result is bad, make them all to give
1249 * consistent output in interval mode.
1250 */
1251 if (counts->ena == 0 || counts->run == 0 ||
1252 counter->counts->scaled == -1) {
1253 ad->ena = 0;
1254 ad->run = 0;
1255 break;
1256 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001257 ad->val += counts->val;
1258 ad->ena += counts->ena;
1259 ad->run += counts->run;
1260 }
1261}
1262
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001263static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001264{
Jiri Olsa58215222015-07-21 14:31:24 +02001265 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001266 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001267 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001268 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001269 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001270 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001271
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001272 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001273 return;
1274
Andi Kleen44d49a62016-02-29 14:36:22 -08001275 aggr_update_shadow();
1276
Andi Kleen54b50912016-03-03 15:57:36 -08001277 /*
1278 * With metric_only everything is on a single line.
1279 * Without each counter has its own line.
1280 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001281 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001282 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001283 if (prefix && metric_only)
1284 fprintf(output, "%s", prefix);
1285
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001286 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001287 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001288 evlist__for_each_entry(evsel_list, counter) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001289 ad.val = ad.ena = ad.run = 0;
1290 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001291 if (!collect_data(counter, aggr_cb, &ad))
1292 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001293 nr = ad.nr;
1294 ena = ad.ena;
1295 run = ad.run;
1296 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001297 if (first && metric_only) {
1298 first = false;
1299 aggr_printout(counter, id, nr);
1300 }
1301 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001302 fprintf(output, "%s", prefix);
1303
Stephane Eranian410136f2013-11-12 17:58:49 +01001304 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001305 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
Andi Kleen54b50912016-03-03 15:57:36 -08001306 if (!metric_only)
1307 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001308 }
Andi Kleen54b50912016-03-03 15:57:36 -08001309 if (metric_only)
1310 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001311 }
1312}
1313
Jiri Olsa32b8af82015-06-26 11:29:27 +02001314static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1315{
Jiri Olsa58215222015-07-21 14:31:24 +02001316 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001317 int nthreads = thread_map__nr(counter->threads);
1318 int ncpus = cpu_map__nr(counter->cpus);
1319 int cpu, thread;
1320 double uval;
1321
1322 for (thread = 0; thread < nthreads; thread++) {
1323 u64 ena = 0, run = 0, val = 0;
1324
1325 for (cpu = 0; cpu < ncpus; cpu++) {
1326 val += perf_counts(counter->counts, cpu, thread)->val;
1327 ena += perf_counts(counter->counts, cpu, thread)->ena;
1328 run += perf_counts(counter->counts, cpu, thread)->run;
1329 }
1330
1331 if (prefix)
1332 fprintf(output, "%s", prefix);
1333
1334 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001335 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001336 fputc('\n', output);
1337 }
1338}
1339
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001340struct caggr_data {
1341 double avg, avg_enabled, avg_running;
1342};
1343
1344static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1345 bool first __maybe_unused)
1346{
1347 struct caggr_data *cd = data;
1348 struct perf_stat_evsel *ps = counter->priv;
1349
1350 cd->avg += avg_stats(&ps->res_stats[0]);
1351 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1352 cd->avg_running += avg_stats(&ps->res_stats[2]);
1353}
1354
Ingo Molnar42202dd2009-06-13 14:57:28 +02001355/*
1356 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001357 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001358 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001359static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001360{
Jiri Olsa58215222015-07-21 14:31:24 +02001361 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001362 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001363 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001364
Andi Kleen430daf22017-03-20 13:17:00 -07001365 if (!collect_data(counter, counter_aggr_cb, &cd))
1366 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001367
Andi Kleen54b50912016-03-03 15:57:36 -08001368 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001369 fprintf(output, "%s", prefix);
1370
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001371 uval = cd.avg * counter->scale;
1372 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, cd.avg);
Andi Kleen54b50912016-03-03 15:57:36 -08001373 if (!metric_only)
1374 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001375}
1376
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001377static void counter_cb(struct perf_evsel *counter, void *data,
1378 bool first __maybe_unused)
1379{
1380 struct aggr_data *ad = data;
1381
1382 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1383 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1384 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1385}
1386
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001387/*
1388 * Print out the results of a single counter:
1389 * does not use aggregated count in system-wide
1390 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001391static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001392{
Jiri Olsa58215222015-07-21 14:31:24 +02001393 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001394 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001395 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001396 int cpu;
1397
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001398 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001399 struct aggr_data ad = { .cpu = cpu };
1400
Andi Kleen430daf22017-03-20 13:17:00 -07001401 if (!collect_data(counter, counter_cb, &ad))
1402 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001403 val = ad.val;
1404 ena = ad.ena;
1405 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001406
1407 if (prefix)
1408 fprintf(output, "%s", prefix);
1409
Stephane Eranian410136f2013-11-12 17:58:49 +01001410 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001411 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001412
Stephane Eranian4aa90152011-08-15 22:22:33 +02001413 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001414 }
1415}
1416
Andi Kleen206cab62016-03-03 15:57:37 -08001417static void print_no_aggr_metric(char *prefix)
1418{
1419 int cpu;
1420 int nrcpus = 0;
1421 struct perf_evsel *counter;
1422 u64 ena, run, val;
1423 double uval;
1424
1425 nrcpus = evsel_list->cpus->nr;
1426 for (cpu = 0; cpu < nrcpus; cpu++) {
1427 bool first = true;
1428
1429 if (prefix)
1430 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001431 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen206cab62016-03-03 15:57:37 -08001432 if (first) {
1433 aggr_printout(counter, cpu, 0);
1434 first = false;
1435 }
1436 val = perf_counts(counter->counts, cpu, 0)->val;
1437 ena = perf_counts(counter->counts, cpu, 0)->ena;
1438 run = perf_counts(counter->counts, cpu, 0)->run;
1439
1440 uval = val * counter->scale;
1441 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1442 }
1443 fputc('\n', stat_config.output);
1444 }
1445}
1446
Andi Kleen54b50912016-03-03 15:57:36 -08001447static int aggr_header_lens[] = {
1448 [AGGR_CORE] = 18,
1449 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001450 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001451 [AGGR_THREAD] = 24,
1452 [AGGR_GLOBAL] = 0,
1453};
1454
Andi Kleenc51fd632016-05-24 12:52:39 -07001455static const char *aggr_header_csv[] = {
1456 [AGGR_CORE] = "core,cpus,",
1457 [AGGR_SOCKET] = "socket,cpus",
1458 [AGGR_NONE] = "cpu,",
1459 [AGGR_THREAD] = "comm-pid,",
1460 [AGGR_GLOBAL] = ""
1461};
1462
Andi Kleen41c8ca22016-05-24 12:52:38 -07001463static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001464{
1465 struct perf_stat_output_ctx out;
1466 struct perf_evsel *counter;
1467 struct outstate os = {
1468 .fh = stat_config.output
1469 };
1470
1471 if (prefix)
1472 fprintf(stat_config.output, "%s", prefix);
1473
Andi Kleen41c8ca22016-05-24 12:52:38 -07001474 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001475 fprintf(stat_config.output, "%*s",
1476 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001477 if (csv_output) {
1478 if (stat_config.interval)
1479 fputs("time,", stat_config.output);
1480 fputs(aggr_header_csv[stat_config.aggr_mode],
1481 stat_config.output);
1482 }
Andi Kleen54b50912016-03-03 15:57:36 -08001483
1484 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001485 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen54b50912016-03-03 15:57:36 -08001486 os.evsel = counter;
1487 out.ctx = &os;
1488 out.print_metric = print_metric_header;
1489 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001490 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001491 os.evsel = counter;
1492 perf_stat__print_shadow_stats(counter, 0,
1493 0,
1494 &out);
1495 }
1496 fputc('\n', stat_config.output);
1497}
1498
Jiri Olsad4f63a42015-06-26 11:29:26 +02001499static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001500{
Jiri Olsa58215222015-07-21 14:31:24 +02001501 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001502 static int num_print_interval;
1503
1504 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1505
Andi Kleen41c8ca22016-05-24 12:52:38 -07001506 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001507 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001508 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001509 fprintf(output, "# time socket cpus");
1510 if (!metric_only)
1511 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001512 break;
1513 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001514 fprintf(output, "# time core cpus");
1515 if (!metric_only)
1516 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001517 break;
1518 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001519 fprintf(output, "# time CPU");
1520 if (!metric_only)
1521 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001522 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001523 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001524 fprintf(output, "# time comm-pid");
1525 if (!metric_only)
1526 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001527 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001528 case AGGR_GLOBAL:
1529 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001530 fprintf(output, "# time");
1531 if (!metric_only)
1532 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001533 case AGGR_UNSET:
1534 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001535 }
1536 }
1537
Andi Kleen41c8ca22016-05-24 12:52:38 -07001538 if (num_print_interval == 0 && metric_only)
1539 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001540 if (++num_print_interval == 25)
1541 num_print_interval = 0;
1542}
1543
1544static void print_header(int argc, const char **argv)
1545{
Jiri Olsa58215222015-07-21 14:31:24 +02001546 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001547 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001548
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001549 fflush(stdout);
1550
Stephane Eraniand7470b62010-12-01 18:49:05 +02001551 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001552 fprintf(output, "\n");
1553 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001554 if (target.system_wide)
1555 fprintf(output, "\'system wide");
1556 else if (target.cpu_list)
1557 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001558 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001559 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1560 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001561 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001562 } else if (target.pid)
1563 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001564 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001565 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001566
Stephane Eranian4aa90152011-08-15 22:22:33 +02001567 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001568 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001569 fprintf(output, " (%d runs)", run_count);
1570 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001571 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001572}
1573
1574static void print_footer(void)
1575{
Jiri Olsa58215222015-07-21 14:31:24 +02001576 FILE *output = stat_config.output;
1577
Jiri Olsad4f63a42015-06-26 11:29:26 +02001578 if (!null_run)
1579 fprintf(output, "\n");
1580 fprintf(output, " %17.9f seconds time elapsed",
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001581 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001582 if (run_count > 1) {
1583 fprintf(output, " ");
1584 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1585 avg_stats(&walltime_nsecs_stats));
1586 }
1587 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001588
1589 if (print_free_counters_hint)
1590 fprintf(output,
1591"Some events weren't counted. Try disabling the NMI watchdog:\n"
1592" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1593" perf stat ...\n"
1594" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001595}
1596
1597static void print_counters(struct timespec *ts, int argc, const char **argv)
1598{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001599 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001600 struct perf_evsel *counter;
1601 char buf[64], *prefix = NULL;
1602
Jiri Olsa664c98d2015-11-05 15:40:50 +01001603 /* Do not print anything if we record to the pipe. */
1604 if (STAT_RECORD && perf_stat.file.is_pipe)
1605 return;
1606
Jiri Olsad4f63a42015-06-26 11:29:26 +02001607 if (interval)
1608 print_interval(prefix = buf, ts);
1609 else
1610 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001611
Andi Kleen54b50912016-03-03 15:57:36 -08001612 if (metric_only) {
1613 static int num_print_iv;
1614
Andi Kleen41c8ca22016-05-24 12:52:38 -07001615 if (num_print_iv == 0 && !interval)
1616 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001617 if (num_print_iv++ == 25)
1618 num_print_iv = 0;
1619 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1620 fprintf(stat_config.output, "%s", prefix);
1621 }
1622
Jiri Olsa421a50f2015-07-21 14:31:22 +02001623 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001624 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001625 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001626 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001627 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001628 case AGGR_THREAD:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001629 evlist__for_each_entry(evsel_list, counter)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001630 print_aggr_thread(counter, prefix);
1631 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001632 case AGGR_GLOBAL:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001633 evlist__for_each_entry(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001634 print_counter_aggr(counter, prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001635 if (metric_only)
1636 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001637 break;
1638 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001639 if (metric_only)
1640 print_no_aggr_metric(prefix);
1641 else {
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001642 evlist__for_each_entry(evsel_list, counter)
Andi Kleen206cab62016-03-03 15:57:37 -08001643 print_counter(counter, prefix);
1644 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001645 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001646 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001647 default:
1648 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001649 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001650
Jiri Olsad4f63a42015-06-26 11:29:26 +02001651 if (!interval && !csv_output)
1652 print_footer();
1653
Jiri Olsa58215222015-07-21 14:31:24 +02001654 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001655}
1656
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001657static volatile int signr = -1;
1658
Ingo Molnar52425192009-05-26 09:17:18 +02001659static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001660{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001661 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001662 done = 1;
1663
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001664 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001665 /*
1666 * render child_pid harmless
1667 * won't send SIGTERM to a random
1668 * process in case of race condition
1669 * and fast PID recycling
1670 */
1671 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001672}
1673
1674static void sig_atexit(void)
1675{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001676 sigset_t set, oset;
1677
1678 /*
1679 * avoid race condition with SIGCHLD handler
1680 * in skip_signal() which is modifying child_pid
1681 * goal is to avoid send SIGTERM to a random
1682 * process
1683 */
1684 sigemptyset(&set);
1685 sigaddset(&set, SIGCHLD);
1686 sigprocmask(SIG_BLOCK, &set, &oset);
1687
Chris Wilson933da832009-10-04 01:35:01 +01001688 if (child_pid != -1)
1689 kill(child_pid, SIGTERM);
1690
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001691 sigprocmask(SIG_SETMASK, &oset, NULL);
1692
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001693 if (signr == -1)
1694 return;
1695
1696 signal(signr, SIG_DFL);
1697 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001698}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001699
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001700static int stat__set_big_num(const struct option *opt __maybe_unused,
1701 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001702{
1703 big_num_opt = unset ? 0 : 1;
1704 return 0;
1705}
1706
Andi Kleen44b1e602016-05-30 12:49:42 -03001707static int enable_metric_only(const struct option *opt __maybe_unused,
1708 const char *s __maybe_unused, int unset)
1709{
1710 force_metric_only = true;
1711 metric_only = !unset;
1712 return 0;
1713}
1714
Jiri Olsae0547312015-11-05 15:40:45 +01001715static const struct option stat_options[] = {
1716 OPT_BOOLEAN('T', "transaction", &transaction_run,
1717 "hardware transaction statistics"),
1718 OPT_CALLBACK('e', "event", &evsel_list, "event",
1719 "event selector. use 'perf list' to list available events",
1720 parse_events_option),
1721 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1722 "event filter", parse_filter),
1723 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1724 "child tasks do not inherit counters"),
1725 OPT_STRING('p', "pid", &target.pid, "pid",
1726 "stat events on existing process id"),
1727 OPT_STRING('t', "tid", &target.tid, "tid",
1728 "stat events on existing thread id"),
1729 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1730 "system-wide collection from all CPUs"),
1731 OPT_BOOLEAN('g', "group", &group,
1732 "put the counters into a counter group"),
1733 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1734 OPT_INCR('v', "verbose", &verbose,
1735 "be more verbose (show counter open errors, etc)"),
1736 OPT_INTEGER('r', "repeat", &run_count,
1737 "repeat command and print average + stddev (max: 100, forever: 0)"),
1738 OPT_BOOLEAN('n', "null", &null_run,
1739 "null run - dont start any counters"),
1740 OPT_INCR('d', "detailed", &detailed_run,
1741 "detailed run - start a lot of events"),
1742 OPT_BOOLEAN('S', "sync", &sync_run,
1743 "call sync() before starting a run"),
1744 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1745 "print large numbers with thousands\' separators",
1746 stat__set_big_num),
1747 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1748 "list of cpus to monitor in system-wide"),
1749 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1750 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001751 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001752 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1753 "print counts with custom separator"),
1754 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1755 "monitor event in cgroup name only", parse_cgroups),
1756 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1757 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1758 OPT_INTEGER(0, "log-fd", &output_fd,
1759 "log output to fd, instead of stderr"),
1760 OPT_STRING(0, "pre", &pre_cmd, "command",
1761 "command to run prior to the measured command"),
1762 OPT_STRING(0, "post", &post_cmd, "command",
1763 "command to run after to the measured command"),
1764 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1765 "print counts at regular interval in ms (>= 10)"),
1766 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1767 "aggregate counts per processor socket", AGGR_SOCKET),
1768 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1769 "aggregate counts per physical processor core", AGGR_CORE),
1770 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1771 "aggregate counts per thread", AGGR_THREAD),
1772 OPT_UINTEGER('D', "delay", &initial_delay,
1773 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001774 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1775 "Only print computed metrics. No raw values", enable_metric_only),
1776 OPT_BOOLEAN(0, "topdown", &topdown_run,
1777 "measure topdown level 1 statistics"),
Jiri Olsae0547312015-11-05 15:40:45 +01001778 OPT_END()
1779};
1780
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001781static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1782{
1783 return cpu_map__get_socket(map, cpu, NULL);
1784}
1785
1786static int perf_stat__get_core(struct cpu_map *map, int cpu)
1787{
1788 return cpu_map__get_core(map, cpu, NULL);
1789}
1790
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001791static int cpu_map__get_max(struct cpu_map *map)
1792{
1793 int i, max = -1;
1794
1795 for (i = 0; i < map->nr; i++) {
1796 if (map->map[i] > max)
1797 max = map->map[i];
1798 }
1799
1800 return max;
1801}
1802
1803static struct cpu_map *cpus_aggr_map;
1804
1805static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1806{
1807 int cpu;
1808
1809 if (idx >= map->nr)
1810 return -1;
1811
1812 cpu = map->map[idx];
1813
1814 if (cpus_aggr_map->map[cpu] == -1)
1815 cpus_aggr_map->map[cpu] = get_id(map, idx);
1816
1817 return cpus_aggr_map->map[cpu];
1818}
1819
1820static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1821{
1822 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1823}
1824
1825static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1826{
1827 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1828}
1829
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001830static int perf_stat_init_aggr_mode(void)
1831{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001832 int nr;
1833
Jiri Olsa421a50f2015-07-21 14:31:22 +02001834 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001835 case AGGR_SOCKET:
1836 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1837 perror("cannot build socket map");
1838 return -1;
1839 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001840 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001841 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001842 case AGGR_CORE:
1843 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1844 perror("cannot build core map");
1845 return -1;
1846 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001847 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001848 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001849 case AGGR_NONE:
1850 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001851 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001852 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001853 default:
1854 break;
1855 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001856
1857 /*
1858 * The evsel_list->cpus is the base we operate on,
1859 * taking the highest cpu number to be the size of
1860 * the aggregation translate cpumap.
1861 */
1862 nr = cpu_map__get_max(evsel_list->cpus);
1863 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1864 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001865}
1866
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001867static void perf_stat__exit_aggr_mode(void)
1868{
1869 cpu_map__put(aggr_map);
1870 cpu_map__put(cpus_aggr_map);
1871 aggr_map = NULL;
1872 cpus_aggr_map = NULL;
1873}
1874
Jiri Olsa68d702f2015-11-05 15:40:58 +01001875static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1876{
1877 int cpu;
1878
1879 if (idx > map->nr)
1880 return -1;
1881
1882 cpu = map->map[idx];
1883
Jan Stancekda8a58b2017-02-17 12:10:26 +01001884 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001885 return -1;
1886
1887 return cpu;
1888}
1889
1890static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1891{
1892 struct perf_env *env = data;
1893 int cpu = perf_env__get_cpu(env, map, idx);
1894
1895 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1896}
1897
1898static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1899{
1900 struct perf_env *env = data;
1901 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1902
1903 if (cpu != -1) {
1904 int socket_id = env->cpu[cpu].socket_id;
1905
1906 /*
1907 * Encode socket in upper 16 bits
1908 * core_id is relative to socket, and
1909 * we need a global id. So we combine
1910 * socket + core id.
1911 */
1912 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1913 }
1914
1915 return core;
1916}
1917
1918static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1919 struct cpu_map **sockp)
1920{
1921 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1922}
1923
1924static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1925 struct cpu_map **corep)
1926{
1927 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1928}
1929
1930static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1931{
1932 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1933}
1934
1935static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1936{
1937 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1938}
1939
1940static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1941{
1942 struct perf_env *env = &st->session->header.env;
1943
1944 switch (stat_config.aggr_mode) {
1945 case AGGR_SOCKET:
1946 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1947 perror("cannot build socket map");
1948 return -1;
1949 }
1950 aggr_get_id = perf_stat__get_socket_file;
1951 break;
1952 case AGGR_CORE:
1953 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1954 perror("cannot build core map");
1955 return -1;
1956 }
1957 aggr_get_id = perf_stat__get_core_file;
1958 break;
1959 case AGGR_NONE:
1960 case AGGR_GLOBAL:
1961 case AGGR_THREAD:
1962 case AGGR_UNSET:
1963 default:
1964 break;
1965 }
1966
1967 return 0;
1968}
1969
Andi Kleen44b1e602016-05-30 12:49:42 -03001970static int topdown_filter_events(const char **attr, char **str, bool use_group)
1971{
1972 int off = 0;
1973 int i;
1974 int len = 0;
1975 char *s;
1976
1977 for (i = 0; attr[i]; i++) {
1978 if (pmu_have_event("cpu", attr[i])) {
1979 len += strlen(attr[i]) + 1;
1980 attr[i - off] = attr[i];
1981 } else
1982 off++;
1983 }
1984 attr[i - off] = NULL;
1985
1986 *str = malloc(len + 1 + 2);
1987 if (!*str)
1988 return -1;
1989 s = *str;
1990 if (i - off == 0) {
1991 *s = 0;
1992 return 0;
1993 }
1994 if (use_group)
1995 *s++ = '{';
1996 for (i = 0; attr[i]; i++) {
1997 strcpy(s, attr[i]);
1998 s += strlen(s);
1999 *s++ = ',';
2000 }
2001 if (use_group) {
2002 s[-1] = '}';
2003 *s = 0;
2004 } else
2005 s[-1] = 0;
2006 return 0;
2007}
2008
2009__weak bool arch_topdown_check_group(bool *warn)
2010{
2011 *warn = false;
2012 return false;
2013}
2014
2015__weak void arch_topdown_group_warn(void)
2016{
2017}
2018
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002019/*
2020 * Add default attributes, if there were no attributes specified or
2021 * if -d/--detailed, -d -d or -d -d -d is used:
2022 */
2023static int add_default_attributes(void)
2024{
Andi Kleen44b1e602016-05-30 12:49:42 -03002025 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002026 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002027
2028 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2029 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2030 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2031 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2032
2033 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002034};
2035 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002036 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002037};
2038 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002039 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002040};
2041 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002042 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2043 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2044 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2045
2046};
2047
2048/*
2049 * Detailed stats (-d), covering the L1 and last level data caches:
2050 */
2051 struct perf_event_attr detailed_attrs[] = {
2052
2053 { .type = PERF_TYPE_HW_CACHE,
2054 .config =
2055 PERF_COUNT_HW_CACHE_L1D << 0 |
2056 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2057 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2058
2059 { .type = PERF_TYPE_HW_CACHE,
2060 .config =
2061 PERF_COUNT_HW_CACHE_L1D << 0 |
2062 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2063 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2064
2065 { .type = PERF_TYPE_HW_CACHE,
2066 .config =
2067 PERF_COUNT_HW_CACHE_LL << 0 |
2068 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2069 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2070
2071 { .type = PERF_TYPE_HW_CACHE,
2072 .config =
2073 PERF_COUNT_HW_CACHE_LL << 0 |
2074 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2075 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2076};
2077
2078/*
2079 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2080 */
2081 struct perf_event_attr very_detailed_attrs[] = {
2082
2083 { .type = PERF_TYPE_HW_CACHE,
2084 .config =
2085 PERF_COUNT_HW_CACHE_L1I << 0 |
2086 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2087 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2088
2089 { .type = PERF_TYPE_HW_CACHE,
2090 .config =
2091 PERF_COUNT_HW_CACHE_L1I << 0 |
2092 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2093 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2094
2095 { .type = PERF_TYPE_HW_CACHE,
2096 .config =
2097 PERF_COUNT_HW_CACHE_DTLB << 0 |
2098 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2099 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2100
2101 { .type = PERF_TYPE_HW_CACHE,
2102 .config =
2103 PERF_COUNT_HW_CACHE_DTLB << 0 |
2104 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2105 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2106
2107 { .type = PERF_TYPE_HW_CACHE,
2108 .config =
2109 PERF_COUNT_HW_CACHE_ITLB << 0 |
2110 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2111 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2112
2113 { .type = PERF_TYPE_HW_CACHE,
2114 .config =
2115 PERF_COUNT_HW_CACHE_ITLB << 0 |
2116 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2117 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2118
2119};
2120
2121/*
2122 * Very, very detailed stats (-d -d -d), adding prefetch events:
2123 */
2124 struct perf_event_attr very_very_detailed_attrs[] = {
2125
2126 { .type = PERF_TYPE_HW_CACHE,
2127 .config =
2128 PERF_COUNT_HW_CACHE_L1D << 0 |
2129 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2130 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2131
2132 { .type = PERF_TYPE_HW_CACHE,
2133 .config =
2134 PERF_COUNT_HW_CACHE_L1D << 0 |
2135 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2136 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2137};
2138
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002139 /* Set attrs if no event is selected and !null_run: */
2140 if (null_run)
2141 return 0;
2142
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002143 if (transaction_run) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002144 if (pmu_have_event("cpu", "cycles-ct") &&
2145 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02002146 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002147 else
Jiri Olsaa4547422015-06-03 16:25:53 +02002148 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2149 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002150 fprintf(stderr, "Cannot set up transaction events\n");
2151 return -1;
2152 }
2153 return 0;
2154 }
2155
Andi Kleen44b1e602016-05-30 12:49:42 -03002156 if (topdown_run) {
2157 char *str = NULL;
2158 bool warn = false;
2159
2160 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2161 stat_config.aggr_mode != AGGR_CORE) {
2162 pr_err("top down event configuration requires --per-core mode\n");
2163 return -1;
2164 }
2165 stat_config.aggr_mode = AGGR_CORE;
2166 if (nr_cgroups || !target__has_cpu(&target)) {
2167 pr_err("top down event configuration requires system-wide mode (-a)\n");
2168 return -1;
2169 }
2170
2171 if (!force_metric_only)
2172 metric_only = true;
2173 if (topdown_filter_events(topdown_attrs, &str,
2174 arch_topdown_check_group(&warn)) < 0) {
2175 pr_err("Out of memory\n");
2176 return -1;
2177 }
2178 if (topdown_attrs[0] && str) {
2179 if (warn)
2180 arch_topdown_group_warn();
2181 err = parse_events(evsel_list, str, NULL);
2182 if (err) {
2183 fprintf(stderr,
2184 "Cannot set up top down events %s: %d\n",
2185 str, err);
2186 free(str);
2187 return -1;
2188 }
2189 } else {
2190 fprintf(stderr, "System does not support topdown\n");
2191 return -1;
2192 }
2193 free(str);
2194 }
2195
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002196 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002197 if (target__has_cpu(&target))
2198 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2199
Andi Kleen9dec4472016-02-26 16:27:56 -08002200 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2201 return -1;
2202 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2203 if (perf_evlist__add_default_attrs(evsel_list,
2204 frontend_attrs) < 0)
2205 return -1;
2206 }
2207 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2208 if (perf_evlist__add_default_attrs(evsel_list,
2209 backend_attrs) < 0)
2210 return -1;
2211 }
2212 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002213 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002214 }
2215
2216 /* Detailed events get appended to the event list: */
2217
2218 if (detailed_run < 1)
2219 return 0;
2220
2221 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002222 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002223 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002224
2225 if (detailed_run < 2)
2226 return 0;
2227
2228 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002229 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002230 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002231
2232 if (detailed_run < 3)
2233 return 0;
2234
2235 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002236 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002237}
2238
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002239static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002240 "perf stat record [<options>]",
2241 NULL,
2242};
2243
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002244static void init_features(struct perf_session *session)
2245{
2246 int feat;
2247
2248 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2249 perf_header__set_feat(&session->header, feat);
2250
2251 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2252 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2253 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2254 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2255}
2256
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002257static int __cmd_record(int argc, const char **argv)
2258{
2259 struct perf_session *session;
2260 struct perf_data_file *file = &perf_stat.file;
2261
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002262 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002263 PARSE_OPT_STOP_AT_NON_OPTION);
2264
2265 if (output_name)
2266 file->path = output_name;
2267
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002268 if (run_count != 1 || forever) {
2269 pr_err("Cannot use -r option with perf stat record.\n");
2270 return -1;
2271 }
2272
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002273 session = perf_session__new(file, false, NULL);
2274 if (session == NULL) {
2275 pr_err("Perf session creation failed.\n");
2276 return -1;
2277 }
2278
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002279 init_features(session);
2280
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002281 session->evlist = evsel_list;
2282 perf_stat.session = session;
2283 perf_stat.record = true;
2284 return argc;
2285}
2286
Jiri Olsaa56f9392015-11-05 15:40:59 +01002287static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2288 union perf_event *event,
2289 struct perf_session *session)
2290{
Andi Kleene3b03b62016-05-05 16:04:03 -07002291 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002292 struct perf_evsel *counter;
2293 struct timespec tsh, *ts = NULL;
2294 const char **argv = session->header.env.cmdline_argv;
2295 int argc = session->header.env.nr_cmdline;
2296
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002297 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002298 perf_stat_process_counter(&stat_config, counter);
2299
Andi Kleene3b03b62016-05-05 16:04:03 -07002300 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2301 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002302
Andi Kleene3b03b62016-05-05 16:04:03 -07002303 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002304 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2305 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002306 ts = &tsh;
2307 }
2308
2309 print_counters(ts, argc, argv);
2310 return 0;
2311}
2312
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002313static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002314int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002315 union perf_event *event,
2316 struct perf_session *session __maybe_unused)
2317{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002318 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2319
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002320 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002321
Jiri Olsa89af4e02015-11-05 15:41:02 +01002322 if (cpu_map__empty(st->cpus)) {
2323 if (st->aggr_mode != AGGR_UNSET)
2324 pr_warning("warning: processing task data, aggregation mode not set\n");
2325 return 0;
2326 }
2327
2328 if (st->aggr_mode != AGGR_UNSET)
2329 stat_config.aggr_mode = st->aggr_mode;
2330
Jiri Olsa68d702f2015-11-05 15:40:58 +01002331 if (perf_stat.file.is_pipe)
2332 perf_stat_init_aggr_mode();
2333 else
2334 perf_stat_init_aggr_mode_file(st);
2335
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002336 return 0;
2337}
2338
Jiri Olsa1975d362015-11-05 15:40:56 +01002339static int set_maps(struct perf_stat *st)
2340{
2341 if (!st->cpus || !st->threads)
2342 return 0;
2343
2344 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2345 return -EINVAL;
2346
2347 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2348
2349 if (perf_evlist__alloc_stats(evsel_list, true))
2350 return -ENOMEM;
2351
2352 st->maps_allocated = true;
2353 return 0;
2354}
2355
2356static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002357int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002358 union perf_event *event,
2359 struct perf_session *session __maybe_unused)
2360{
2361 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2362
2363 if (st->threads) {
2364 pr_warning("Extra thread map event, ignoring.\n");
2365 return 0;
2366 }
2367
2368 st->threads = thread_map__new_event(&event->thread_map);
2369 if (!st->threads)
2370 return -ENOMEM;
2371
2372 return set_maps(st);
2373}
2374
2375static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002376int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002377 union perf_event *event,
2378 struct perf_session *session __maybe_unused)
2379{
2380 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2381 struct cpu_map *cpus;
2382
2383 if (st->cpus) {
2384 pr_warning("Extra cpu map event, ignoring.\n");
2385 return 0;
2386 }
2387
2388 cpus = cpu_map__new_data(&event->cpu_map.data);
2389 if (!cpus)
2390 return -ENOMEM;
2391
2392 st->cpus = cpus;
2393 return set_maps(st);
2394}
2395
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002396static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002397 "perf stat report [<options>]",
2398 NULL,
2399};
2400
2401static struct perf_stat perf_stat = {
2402 .tool = {
2403 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002404 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002405 .thread_map = process_thread_map_event,
2406 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002407 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002408 .stat = perf_event__process_stat_event,
2409 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002410 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002411 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002412};
2413
2414static int __cmd_report(int argc, const char **argv)
2415{
2416 struct perf_session *session;
2417 const struct option options[] = {
2418 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002419 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2420 "aggregate counts per processor socket", AGGR_SOCKET),
2421 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2422 "aggregate counts per physical processor core", AGGR_CORE),
2423 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2424 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002425 OPT_END()
2426 };
2427 struct stat st;
2428 int ret;
2429
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002430 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002431
2432 if (!input_name || !strlen(input_name)) {
2433 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2434 input_name = "-";
2435 else
2436 input_name = "perf.data";
2437 }
2438
2439 perf_stat.file.path = input_name;
2440 perf_stat.file.mode = PERF_DATA_MODE_READ;
2441
2442 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2443 if (session == NULL)
2444 return -1;
2445
2446 perf_stat.session = session;
2447 stat_config.output = stderr;
2448 evsel_list = session->evlist;
2449
2450 ret = perf_session__process_events(session);
2451 if (ret)
2452 return ret;
2453
2454 perf_session__delete(session);
2455 return 0;
2456}
2457
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002458static void setup_system_wide(int forks)
2459{
2460 /*
2461 * Make system wide (-a) the default target if
2462 * no target was specified and one of following
2463 * conditions is met:
2464 *
2465 * - there's no workload specified
2466 * - there is workload specified but all requested
2467 * events are system wide events
2468 */
2469 if (!target__none(&target))
2470 return;
2471
2472 if (!forks)
2473 target.system_wide = true;
2474 else {
2475 struct perf_evsel *counter;
2476
2477 evlist__for_each_entry(evsel_list, counter) {
2478 if (!counter->system_wide)
2479 return;
2480 }
2481
2482 if (evsel_list->nr_entries)
2483 target.system_wide = true;
2484 }
2485}
2486
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002487int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002488{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002489 const char * const stat_usage[] = {
2490 "perf stat [<options>] [<command>]",
2491 NULL
2492 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002493 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002494 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002495 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002496 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002497 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002498
Stephane Eranian5af52b52010-05-18 15:00:01 +02002499 setlocale(LC_ALL, "");
2500
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002501 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002502 if (evsel_list == NULL)
2503 return -ENOMEM;
2504
Wang Nan1669e502016-02-19 11:43:58 +00002505 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002506 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2507 (const char **) stat_usage,
2508 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002509 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002510 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002511
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002512 if (csv_sep) {
2513 csv_output = true;
2514 if (!strcmp(csv_sep, "\\t"))
2515 csv_sep = "\t";
2516 } else
2517 csv_sep = DEFAULT_SEPARATOR;
2518
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002519 if (argc && !strncmp(argv[0], "rec", 3)) {
2520 argc = __cmd_record(argc, argv);
2521 if (argc < 0)
2522 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002523 } else if (argc && !strncmp(argv[0], "rep", 3))
2524 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002525
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002526 interval = stat_config.interval;
2527
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002528 /*
2529 * For record command the -o is already taken care of.
2530 */
2531 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002532 output = NULL;
2533
Jim Cromie56f3bae2011-09-07 17:14:00 -06002534 if (output_name && output_fd) {
2535 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002536 parse_options_usage(stat_usage, stat_options, "o", 1);
2537 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002538 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002539 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002540
Andi Kleen54b50912016-03-03 15:57:36 -08002541 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2542 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2543 goto out;
2544 }
2545
Andi Kleen54b50912016-03-03 15:57:36 -08002546 if (metric_only && run_count > 1) {
2547 fprintf(stderr, "--metric-only is not supported with -r\n");
2548 goto out;
2549 }
2550
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002551 if (output_fd < 0) {
2552 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002553 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002554 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002555 }
2556
Stephane Eranian4aa90152011-08-15 22:22:33 +02002557 if (!output) {
2558 struct timespec tm;
2559 mode = append_file ? "a" : "w";
2560
2561 output = fopen(output_name, mode);
2562 if (!output) {
2563 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002564 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002565 }
2566 clock_gettime(CLOCK_REALTIME, &tm);
2567 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002568 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002569 mode = append_file ? "a" : "w";
2570 output = fdopen(output_fd, mode);
2571 if (!output) {
2572 perror("Failed opening logfd");
2573 return -errno;
2574 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002575 }
2576
Jiri Olsa58215222015-07-21 14:31:24 +02002577 stat_config.output = output;
2578
Stephane Eraniand7470b62010-12-01 18:49:05 +02002579 /*
2580 * let the spreadsheet do the pretty-printing
2581 */
2582 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002583 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002584 if (big_num_opt == 1) {
2585 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002586 parse_options_usage(stat_usage, stat_options, "B", 1);
2587 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002588 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002589 } else /* Nope, so disable big number formatting */
2590 big_num = false;
2591 } else if (big_num_opt == 0) /* User passed --no-big-num */
2592 big_num = false;
2593
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002594 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002595
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002596 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002597 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002598 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002599 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002600 } else if (run_count == 0) {
2601 forever = true;
2602 run_count = 1;
2603 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002604
Jiri Olsa421a50f2015-07-21 14:31:22 +02002605 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002606 fprintf(stderr, "The --per-thread option is only available "
2607 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002608 parse_options_usage(NULL, stat_options, "p", 1);
2609 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002610 goto out;
2611 }
2612
2613 /*
2614 * no_aggr, cgroup are for system-wide only
2615 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2616 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002617 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2618 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002619 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002620 fprintf(stderr, "both cgroup and no-aggregation "
2621 "modes only available in system-wide mode\n");
2622
Jiri Olsae0547312015-11-05 15:40:45 +01002623 parse_options_usage(stat_usage, stat_options, "G", 1);
2624 parse_options_usage(NULL, stat_options, "A", 1);
2625 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002626 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002627 }
2628
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002629 if (add_default_attributes())
2630 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002631
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002632 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002633
Namhyung Kim77a6f012012-05-07 14:09:04 +09002634 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002635 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002636 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002637 parse_options_usage(stat_usage, stat_options, "p", 1);
2638 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002639 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002640 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002641 parse_options_usage(stat_usage, stat_options, "C", 1);
2642 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002643 }
2644 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002645 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002646
2647 /*
2648 * Initialize thread_map with comm names,
2649 * so we could print it out on output.
2650 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002651 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02002652 thread_map__read_comms(evsel_list->threads);
2653
Stephane Eranian13370a92013-01-29 12:47:44 +01002654 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002655 if (interval < 10) {
2656 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002657 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002658 goto out;
2659 } else
2660 pr_warning("print interval < 100ms. "
2661 "The overhead percentage could be high in some cases. "
2662 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002663 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002664
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002665 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002666 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002667
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002668 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002669 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002670
Ingo Molnar58d7e992009-05-15 11:03:23 +02002671 /*
2672 * We dont want to block the signals - that would cause
2673 * child tasks to inherit that and Ctrl-C would not work.
2674 * What we want is for Ctrl-C to work in the exec()-ed
2675 * task, but being ignored by perf stat itself:
2676 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002677 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002678 if (!forever)
2679 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002680 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002681 signal(SIGALRM, skip_signal);
2682 signal(SIGABRT, skip_signal);
2683
Ingo Molnar42202dd2009-06-13 14:57:28 +02002684 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002685 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002686 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002687 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2688 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002689
Ingo Molnar42202dd2009-06-13 14:57:28 +02002690 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002691 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002692 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002693 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002694 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002695 }
2696
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002697 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002698 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002699
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002700 if (STAT_RECORD) {
2701 /*
2702 * We synthesize the kernel mmap record just so that older tools
2703 * don't emit warnings about not being able to resolve symbols
2704 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2705 * a saner message about no samples being in the perf.data file.
2706 *
2707 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002708 * in header.c at the moment 'perf stat record' gets introduced, which
2709 * is not really needed once we start adding the stat specific PERF_RECORD_
2710 * records, but the need to suppress the kptr_restrict messages in older
2711 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002712 */
2713 int fd = perf_data_file__fd(&perf_stat.file);
2714 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2715 process_synthesized_event,
2716 &perf_stat.session->machines.host);
2717 if (err) {
2718 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2719 "older tools may produce warnings about this file\n.");
2720 }
2721
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002722 if (!interval) {
2723 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2724 pr_err("failed to write stat round event\n");
2725 }
2726
Jiri Olsa664c98d2015-11-05 15:40:50 +01002727 if (!perf_stat.file.is_pipe) {
2728 perf_stat.session->header.data_size += perf_stat.bytes_written;
2729 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2730 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002731
2732 perf_session__delete(perf_stat.session);
2733 }
2734
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002735 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002736 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002737out:
2738 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002739 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002740}