blob: ee7ada78d86f81c5f788feed43f42ec49e33bb96 [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 Weisbecker8f28827a2009-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 Weisbecker8f28827a2009-08-16 22:05:48 +020054#include "util/debug.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020055#include "util/color.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080056#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080057#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110058#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030059#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020060#include "util/thread_map.h"
Jiri Olsad8095602015-08-07 12:51:03 +020061#include "util/counts.h"
Jiri Olsa4979d0c2015-11-05 15:40:46 +010062#include "util/session.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010063#include "util/tool.h"
64#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020065
Peter Zijlstra1f16c572012-10-23 13:40:14 +020066#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020067#include <sys/prctl.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020068#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070069#include <math.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020070
Stephane Eraniand7470b62010-12-01 18:49:05 +020071#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060072#define CNTR_NOT_SUPPORTED "<not supported>"
73#define CNTR_NOT_COUNTED "<not counted>"
Stephane Eraniand7470b62010-12-01 18:49:05 +020074
Jiri Olsad4f63a42015-06-26 11:29:26 +020075static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010076
Andi Kleen4cabc3d2013-08-21 16:47:26 -070077/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020078static const char *transaction_attrs = {
79 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070080 "{"
81 "instructions,"
82 "cycles,"
83 "cpu/cycles-t/,"
84 "cpu/tx-start/,"
85 "cpu/el-start/,"
86 "cpu/cycles-ct/"
87 "}"
88};
89
90/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +020091static const char * transaction_limited_attrs = {
92 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070093 "{"
94 "instructions,"
95 "cycles,"
96 "cpu/cycles-t/,"
97 "cpu/tx-start/"
98 "}"
99};
100
Robert Richter666e6d42012-04-05 18:26:27 +0200101static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200102
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300103static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900104 .uid = UINT_MAX,
105};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530106
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100107typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
108
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530109static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200110static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200111static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000112static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200113static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700114static bool transaction_run;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200115static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200116static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200117static const char *csv_sep = NULL;
118static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800119static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200120static const char *pre_cmd = NULL;
121static const char *post_cmd = NULL;
122static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700123static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100124static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500125static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800126static bool metric_only = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100127static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100128static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100129static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100130static bool append_file;
131static const char *output_name;
132static int output_fd;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200133
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100134struct perf_stat {
135 bool record;
136 struct perf_data_file file;
137 struct perf_session *session;
138 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100139 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100140 bool maps_allocated;
141 struct cpu_map *cpus;
142 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100143 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100144};
145
146static struct perf_stat perf_stat;
147#define STAT_RECORD perf_stat.record
148
Liming Wang60666c62009-12-31 16:05:50 +0800149static volatile int done = 0;
150
Jiri Olsa421a50f2015-07-21 14:31:22 +0200151static struct perf_stat_config stat_config = {
152 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200153 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200154};
155
Stephane Eranian13370a92013-01-29 12:47:44 +0100156static inline void diff_timespec(struct timespec *r, struct timespec *a,
157 struct timespec *b)
158{
159 r->tv_sec = a->tv_sec - b->tv_sec;
160 if (a->tv_nsec < b->tv_nsec) {
161 r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
162 r->tv_sec--;
163 } else {
164 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
165 }
166}
167
Jiri Olsa254ecbc2015-06-26 11:29:13 +0200168static void perf_stat__reset_stats(void)
169{
170 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200171 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200172}
173
Jiri Olsacac21422012-11-12 18:34:00 +0100174static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200175{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200176 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200177
Jiri Olsa711a5722015-07-21 14:31:23 +0200178 if (stat_config.scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200179 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
180 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200181
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200182 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300183
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100184 /*
185 * Some events get initialized with sample_(period/type) set,
186 * like tracepoints. Clear it up for counting.
187 */
188 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100189
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100190 /*
191 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
192 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100193 *
194 * However for pipe sessions we need to keep it zero,
195 * because script's perf_evsel__check_attr is triggered
196 * by attr->sample_type != 0, and we can't run it on
197 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100198 */
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100199 if (!(STAT_RECORD && perf_stat.file.is_pipe))
200 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100201
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100202 /*
203 * Disabling all counters initially, they will be enabled
204 * either manually by us or by kernel via enable_on_exec
205 * set later.
206 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100207 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100208 attr->disabled = 1;
209
Jiri Olsac8280ce2015-12-03 10:06:45 +0100210 /*
211 * In case of initial_delay we enable tracee
212 * events manually.
213 */
214 if (target__none(&target) && !initial_delay)
215 attr->enable_on_exec = 1;
216 }
217
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300218 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300219 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200220
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300221 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200222}
223
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200224/*
225 * Does the counter have nsecs as a unit?
226 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200227static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200228{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200229 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
230 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200231 return 1;
232
233 return 0;
234}
235
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100236static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100237 union perf_event *event,
238 struct perf_sample *sample __maybe_unused,
239 struct machine *machine __maybe_unused)
240{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100241 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
242 pr_err("failed to write perf data, error: %m\n");
243 return -1;
244 }
245
246 perf_stat.bytes_written += event->header.size;
247 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100248}
249
Jiri Olsa1975d362015-11-05 15:40:56 +0100250static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100251{
Jiri Olsa1975d362015-11-05 15:40:56 +0100252 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100253 process_synthesized_event,
254 NULL);
255}
256
257#define WRITE_STAT_ROUND_EVENT(time, interval) \
258 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
259
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100260#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
261
262static int
263perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
264 struct perf_counts_values *count)
265{
266 struct perf_sample_id *sid = SID(counter, cpu, thread);
267
268 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
269 process_synthesized_event, NULL);
270}
271
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200272/*
273 * Read out the results of a single counter:
274 * do not aggregate counts across CPUs in system-wide mode
275 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200276static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200277{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100278 int nthreads = thread_map__nr(evsel_list->threads);
279 int ncpus = perf_evsel__nr_cpus(counter);
280 int cpu, thread;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200281
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000282 if (!counter->supported)
283 return -ENOENT;
284
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100285 if (counter->system_wide)
286 nthreads = 1;
287
288 for (thread = 0; thread < nthreads; thread++) {
289 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200290 struct perf_counts_values *count;
291
292 count = perf_counts(counter->counts, cpu, thread);
293 if (perf_evsel__read(counter, cpu, thread, count))
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100294 return -1;
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100295
296 if (STAT_RECORD) {
297 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
298 pr_err("failed to write stat event\n");
299 return -1;
300 }
301 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700302
303 if (verbose > 1) {
304 fprintf(stat_config.output,
305 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
306 perf_evsel__name(counter),
307 cpu,
308 count->val, count->ena, count->run);
309 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100310 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200311 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200312
313 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200314}
315
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200316static void read_counters(bool close_counters)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200317{
318 struct perf_evsel *counter;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200319
320 evlist__for_each(evsel_list, counter) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200321 if (read_counter(counter))
Andi Kleen245bad82015-09-01 15:52:46 -0700322 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200323
Jiri Olsaf80010e2015-07-21 14:31:27 +0200324 if (perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200325 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200326
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200327 if (close_counters) {
Jiri Olsa106a94a2015-06-26 11:29:19 +0200328 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
329 thread_map__nr(evsel_list->threads));
330 }
331 }
332}
333
Jiri Olsaba411a92015-06-26 11:29:24 +0200334static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100335{
Stephane Eranian13370a92013-01-29 12:47:44 +0100336 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100337
Jiri Olsa106a94a2015-06-26 11:29:19 +0200338 read_counters(false);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100339
Stephane Eranian13370a92013-01-29 12:47:44 +0100340 clock_gettime(CLOCK_MONOTONIC, &ts);
341 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100342
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100343 if (STAT_RECORD) {
344 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSECS_PER_SEC + rs.tv_nsec, INTERVAL))
345 pr_err("failed to write stat round event\n");
346 }
347
Jiri Olsad4f63a42015-06-26 11:29:26 +0200348 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100349}
350
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100351static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700352{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100353 if (initial_delay)
Andi Kleen41191682013-08-02 17:41:11 -0700354 usleep(initial_delay * 1000);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100355
356 /*
357 * We need to enable counters only if:
358 * - we don't have tracee (attaching to task or cpu)
359 * - we have initial delay configured
360 */
361 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100362 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700363}
364
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300365static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300366
367/*
368 * perf_evlist__prepare_workload will send a SIGUSR1
369 * if the fork fails, since we asked by setting its
370 * want_signal to true.
371 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300372static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
373 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300374{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300375 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300376}
377
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100378static bool has_unit(struct perf_evsel *counter)
379{
380 return counter->unit && *counter->unit;
381}
382
383static bool has_scale(struct perf_evsel *counter)
384{
385 return counter->scale != 1;
386}
387
Jiri Olsa664c98d2015-11-05 15:40:50 +0100388static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100389{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100390 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100391 int err;
392
Jiri Olsa664c98d2015-11-05 15:40:50 +0100393 if (is_pipe) {
394 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
395 process_synthesized_event);
396 if (err < 0) {
397 pr_err("Couldn't synthesize attrs.\n");
398 return err;
399 }
400 }
401
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100402 /*
403 * Synthesize other events stuff not carried within
404 * attr event - unit, scale, name
405 */
406 evlist__for_each(evsel_list, counter) {
407 if (!counter->supported)
408 continue;
409
410 /*
411 * Synthesize unit and scale only if it's defined.
412 */
413 if (has_unit(counter)) {
414 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
415 if (err < 0) {
416 pr_err("Couldn't synthesize evsel unit.\n");
417 return err;
418 }
419 }
420
421 if (has_scale(counter)) {
422 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
423 if (err < 0) {
424 pr_err("Couldn't synthesize evsel scale.\n");
425 return err;
426 }
427 }
428
429 if (counter->own_cpus) {
430 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
431 if (err < 0) {
432 pr_err("Couldn't synthesize evsel scale.\n");
433 return err;
434 }
435 }
436
437 /*
438 * Name is needed only for pipe output,
439 * perf.data carries event names.
440 */
441 if (is_pipe) {
442 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
443 if (err < 0) {
444 pr_err("Couldn't synthesize evsel name.\n");
445 return err;
446 }
447 }
448 }
449
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100450 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
451 process_synthesized_event,
452 NULL);
453 if (err < 0) {
454 pr_err("Couldn't synthesize thread map.\n");
455 return err;
456 }
457
458 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
459 process_synthesized_event, NULL);
460 if (err < 0) {
461 pr_err("Couldn't synthesize thread map.\n");
462 return err;
463 }
464
465 err = perf_event__synthesize_stat_config(NULL, &stat_config,
466 process_synthesized_event, NULL);
467 if (err < 0) {
468 pr_err("Couldn't synthesize config.\n");
469 return err;
470 }
471
472 return 0;
473}
474
Jiri Olsa2af46462015-11-05 15:40:49 +0100475#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
476
477static int __store_counter_ids(struct perf_evsel *counter,
478 struct cpu_map *cpus,
479 struct thread_map *threads)
480{
481 int cpu, thread;
482
483 for (cpu = 0; cpu < cpus->nr; cpu++) {
484 for (thread = 0; thread < threads->nr; thread++) {
485 int fd = FD(counter, cpu, thread);
486
487 if (perf_evlist__id_add_fd(evsel_list, counter,
488 cpu, thread, fd) < 0)
489 return -1;
490 }
491 }
492
493 return 0;
494}
495
496static int store_counter_ids(struct perf_evsel *counter)
497{
498 struct cpu_map *cpus = counter->cpus;
499 struct thread_map *threads = counter->threads;
500
501 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
502 return -ENOMEM;
503
504 return __store_counter_ids(counter, cpus, threads);
505}
506
Namhyung Kimacf28922013-03-11 16:43:18 +0900507static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200508{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200509 int interval = stat_config.interval;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300510 char msg[512];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200511 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100512 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100513 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100514 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200515 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300516 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100517 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200518
Stephane Eranian13370a92013-01-29 12:47:44 +0100519 if (interval) {
520 ts.tv_sec = interval / 1000;
521 ts.tv_nsec = (interval % 1000) * 1000000;
522 } else {
523 ts.tv_sec = 1;
524 ts.tv_nsec = 0;
525 }
526
Liming Wang60666c62009-12-31 16:05:50 +0800527 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100528 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300529 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900530 perror("failed to prepare workload");
531 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800532 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900533 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000534 }
535
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200536 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300537 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200538
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -0300539 evlist__for_each(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300540try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100541 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600542 /*
543 * PPC returns ENXIO for HW counters until 2.6.37
544 * (behavior changed with commit b0a873e).
545 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100546 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600547 errno == ENOENT || errno == EOPNOTSUPP ||
548 errno == ENXIO) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600549 if (verbose)
550 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300551 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600552 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400553
554 if ((counter->leader != counter) ||
555 !(counter->leader->nr_members > 1))
556 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300557 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
558 if (verbose)
559 ui__warning("%s\n", msg);
560 goto try_again;
561 }
Ingo Molnarede70292011-04-28 08:48:42 +0200562
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300563 perf_evsel__open_strerror(counter, &target,
564 errno, msg, sizeof(msg));
565 ui__error("%s\n", msg);
566
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200567 if (child_pid != -1)
568 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600569
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200570 return -1;
571 }
David Ahern2cee77c2011-05-30 08:55:59 -0600572 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100573
574 l = strlen(counter->unit);
575 if (l > unit_width)
576 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100577
578 if (STAT_RECORD && store_counter_ids(counter))
579 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300580 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200581
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300582 if (perf_evlist__apply_filters(evsel_list, &counter)) {
583 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
584 counter->filter, perf_evsel__name(counter), errno,
Masami Hiramatsu759e6122014-08-14 02:22:55 +0000585 strerror_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100586 return -1;
587 }
588
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100589 if (STAT_RECORD) {
590 int err, fd = perf_data_file__fd(&perf_stat.file);
591
Jiri Olsa664c98d2015-11-05 15:40:50 +0100592 if (is_pipe) {
593 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
594 } else {
595 err = perf_session__write_header(perf_stat.session, evsel_list,
596 fd, false);
597 }
598
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100599 if (err < 0)
600 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100601
Jiri Olsa664c98d2015-11-05 15:40:50 +0100602 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100603 if (err < 0)
604 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100605 }
606
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200607 /*
608 * Enable counters and exec the command:
609 */
610 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100611 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200612
Liming Wang60666c62009-12-31 16:05:50 +0800613 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900614 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100615 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900616
Stephane Eranian13370a92013-01-29 12:47:44 +0100617 if (interval) {
618 while (!waitpid(child_pid, &status, WNOHANG)) {
619 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200620 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100621 }
622 }
Liming Wang60666c62009-12-31 16:05:50 +0800623 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300624
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300625 if (workload_exec_errno) {
626 const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg));
627 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300628 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300629 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300630
Andi Kleen33e49ea2011-09-15 14:31:40 -0700631 if (WIFSIGNALED(status))
632 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800633 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100634 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100635 while (!done) {
636 nanosleep(&ts, NULL);
637 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200638 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100639 }
Liming Wang60666c62009-12-31 16:05:50 +0800640 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200641
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200642 t1 = rdclock();
643
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200644 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200645
Jiri Olsa106a94a2015-06-26 11:29:19 +0200646 read_counters(true);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200647
Ingo Molnar42202dd2009-06-13 14:57:28 +0200648 return WEXITSTATUS(status);
649}
650
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300651static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200652{
653 int ret;
654
655 if (pre_cmd) {
656 ret = system(pre_cmd);
657 if (ret)
658 return ret;
659 }
660
661 if (sync_run)
662 sync();
663
664 ret = __run_perf_stat(argc, argv);
665 if (ret)
666 return ret;
667
668 if (post_cmd) {
669 ret = system(post_cmd);
670 if (ret)
671 return ret;
672 }
673
674 return ret;
675}
676
Andi Kleend73515c2015-03-11 07:16:27 -0700677static void print_running(u64 run, u64 ena)
678{
679 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200680 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700681 csv_sep,
682 run,
683 csv_sep,
684 ena ? 100.0 * run / ena : 100.0);
685 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200686 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700687 }
688}
689
Ingo Molnarf99844c2011-04-27 05:35:39 +0200690static void print_noise_pct(double total, double avg)
691{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800692 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200693
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700694 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200695 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600696 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200697 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200698}
699
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200700static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200701{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200702 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200703
Peter Zijlstra849abde2009-09-04 18:23:38 +0200704 if (run_count == 1)
705 return;
706
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200707 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200708 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200709}
710
Stephane Eranian12c08a92013-02-14 13:57:29 +0100711static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200712{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200713 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100714 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200715 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100716 cpu_map__id_to_socket(id),
717 csv_output ? 0 : -8,
718 cpu_map__id_to_cpu(id),
719 csv_sep,
720 csv_output ? 0 : 4,
721 nr,
722 csv_sep);
723 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100724 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200725 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100726 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100727 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100728 csv_sep,
729 csv_output ? 0 : 4,
730 nr,
731 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100732 break;
733 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200734 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200735 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100736 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100737 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200738 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200739 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200740 csv_output ? 0 : 16,
741 thread_map__comm(evsel->threads, id),
742 csv_output ? 0 : -8,
743 thread_map__pid(evsel->threads, id),
744 csv_sep);
745 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100746 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200747 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100748 default:
749 break;
750 }
751}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200752
Andi Kleen140aead2016-01-30 09:06:49 -0800753struct outstate {
754 FILE *fh;
755 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800756 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800757 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800758 int id, nr;
759 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800760};
761
762#define METRIC_LEN 35
763
764static void new_line_std(void *ctx)
765{
766 struct outstate *os = ctx;
767
768 os->newline = true;
769}
770
771static void do_new_line_std(struct outstate *os)
772{
773 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800774 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800775 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800776 if (stat_config.aggr_mode == AGGR_NONE)
777 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800778 fprintf(os->fh, " ");
779}
780
781static void print_metric_std(void *ctx, const char *color, const char *fmt,
782 const char *unit, double val)
783{
784 struct outstate *os = ctx;
785 FILE *out = os->fh;
786 int n;
787 bool newline = os->newline;
788
789 os->newline = false;
790
791 if (unit == NULL || fmt == NULL) {
792 fprintf(out, "%-*s", METRIC_LEN, "");
793 return;
794 }
795
796 if (newline)
797 do_new_line_std(os);
798
799 n = fprintf(out, " # ");
800 if (color)
801 n += color_fprintf(out, color, fmt, val);
802 else
803 n += fprintf(out, fmt, val);
804 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
805}
806
Andi Kleen92a61f62016-02-29 14:36:21 -0800807static void new_line_csv(void *ctx)
808{
809 struct outstate *os = ctx;
810 int i;
811
812 fputc('\n', os->fh);
813 if (os->prefix)
814 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800815 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800816 for (i = 0; i < os->nfields; i++)
817 fputs(csv_sep, os->fh);
818}
819
820static void print_metric_csv(void *ctx,
821 const char *color __maybe_unused,
822 const char *fmt, const char *unit, double val)
823{
824 struct outstate *os = ctx;
825 FILE *out = os->fh;
826 char buf[64], *vals, *ends;
827
828 if (unit == NULL || fmt == NULL) {
829 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
830 return;
831 }
832 snprintf(buf, sizeof(buf), fmt, val);
833 vals = buf;
834 while (isspace(*vals))
835 vals++;
836 ends = vals;
837 while (isdigit(*ends) || *ends == '.')
838 ends++;
839 *ends = 0;
840 while (isspace(*unit))
841 unit++;
842 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
843}
844
Andi Kleen54b50912016-03-03 15:57:36 -0800845#define METRIC_ONLY_LEN 20
846
847/* Filter out some columns that don't work well in metrics only mode */
848
849static bool valid_only_metric(const char *unit)
850{
851 if (!unit)
852 return false;
853 if (strstr(unit, "/sec") ||
854 strstr(unit, "hz") ||
855 strstr(unit, "Hz") ||
856 strstr(unit, "CPUs utilized"))
857 return false;
858 return true;
859}
860
861static const char *fixunit(char *buf, struct perf_evsel *evsel,
862 const char *unit)
863{
864 if (!strncmp(unit, "of all", 6)) {
865 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
866 unit);
867 return buf;
868 }
869 return unit;
870}
871
872static void print_metric_only(void *ctx, const char *color, const char *fmt,
873 const char *unit, double val)
874{
875 struct outstate *os = ctx;
876 FILE *out = os->fh;
877 int n;
878 char buf[1024];
879 unsigned mlen = METRIC_ONLY_LEN;
880
881 if (!valid_only_metric(unit))
882 return;
883 unit = fixunit(buf, os->evsel, unit);
884 if (color)
885 n = color_fprintf(out, color, fmt, val);
886 else
887 n = fprintf(out, fmt, val);
888 if (n > METRIC_ONLY_LEN)
889 n = METRIC_ONLY_LEN;
890 if (mlen < strlen(unit))
891 mlen = strlen(unit) + 1;
892 fprintf(out, "%*s", mlen - n, "");
893}
894
895static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
896 const char *fmt,
897 const char *unit, double val)
898{
899 struct outstate *os = ctx;
900 FILE *out = os->fh;
901 char buf[64], *vals, *ends;
902 char tbuf[1024];
903
904 if (!valid_only_metric(unit))
905 return;
906 unit = fixunit(tbuf, os->evsel, unit);
907 snprintf(buf, sizeof buf, fmt, val);
908 vals = buf;
909 while (isspace(*vals))
910 vals++;
911 ends = vals;
912 while (isdigit(*ends) || *ends == '.')
913 ends++;
914 *ends = 0;
915 fprintf(out, "%s%s", vals, csv_sep);
916}
917
918static void new_line_metric(void *ctx __maybe_unused)
919{
920}
921
922static void print_metric_header(void *ctx, const char *color __maybe_unused,
923 const char *fmt __maybe_unused,
924 const char *unit, double val __maybe_unused)
925{
926 struct outstate *os = ctx;
927 char tbuf[1024];
928
929 if (!valid_only_metric(unit))
930 return;
931 unit = fixunit(tbuf, os->evsel, unit);
932 if (csv_output)
933 fprintf(os->fh, "%s%s", unit, csv_sep);
934 else
935 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
936}
937
Andi Kleenda88c7f2014-09-24 13:50:46 -0700938static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100939{
Jiri Olsa58215222015-07-21 14:31:24 +0200940 FILE *output = stat_config.output;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100941 double msecs = avg / 1e6;
Stephane Eranian410136f2013-11-12 17:58:49 +0100942 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600943 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100944
Stephane Eranian410136f2013-11-12 17:58:49 +0100945 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
946 fmt_n = csv_output ? "%s" : "%-25s";
947
Andi Kleenda88c7f2014-09-24 13:50:46 -0700948 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100949
David Ahern4bbe5a62013-09-28 14:28:00 -0600950 scnprintf(name, sizeof(name), "%s%s",
951 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +0100952
953 fprintf(output, fmt_v, msecs, csv_sep);
954
955 if (csv_output)
956 fprintf(output, "%s%s", evsel->unit, csv_sep);
957 else
958 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
959
960 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200961
Stephane Eranian023695d2011-02-14 11:20:01 +0200962 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200963 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200964}
965
Andi Kleen44d49a62016-02-29 14:36:22 -0800966static int first_shadow_cpu(struct perf_evsel *evsel, int id)
967{
968 int i;
969
970 if (!aggr_get_id)
971 return 0;
972
973 if (stat_config.aggr_mode == AGGR_NONE)
974 return id;
975
976 if (stat_config.aggr_mode == AGGR_GLOBAL)
977 return 0;
978
979 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
980 int cpu2 = perf_evsel__cpus(evsel)->map[i];
981
982 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
983 return cpu2;
984 }
985 return 0;
986}
987
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200988static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
989{
Jiri Olsa58215222015-07-21 14:31:24 +0200990 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200991 double sc = evsel->scale;
992 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200993
994 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -0700995 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200996 } else {
997 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -0700998 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200999 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001000 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001001 }
1002
1003 aggr_printout(evsel, id, nr);
1004
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001005 fprintf(output, fmt, avg, csv_sep);
1006
1007 if (evsel->unit)
1008 fprintf(output, "%-*s%s",
1009 csv_output ? 0 : unit_width,
1010 evsel->unit, csv_sep);
1011
1012 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1013
1014 if (evsel->cgrp)
1015 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001016}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001017
Andi Kleenf9483392016-01-30 09:06:50 -08001018static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Andi Kleencb110f42016-01-30 09:06:51 -08001019 char *prefix, u64 run, u64 ena, double noise)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001020{
Andi Kleen140aead2016-01-30 09:06:49 -08001021 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001022 struct outstate os = {
1023 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001024 .prefix = prefix ? prefix : "",
1025 .id = id,
1026 .nr = nr,
1027 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001028 };
Andi Kleen140aead2016-01-30 09:06:49 -08001029 print_metric_t pm = print_metric_std;
1030 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001031
Andi Kleen54b50912016-03-03 15:57:36 -08001032 if (metric_only) {
1033 nl = new_line_metric;
1034 if (csv_output)
1035 pm = print_metric_only_csv;
1036 else
1037 pm = print_metric_only;
1038 } else
1039 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001040
Andi Kleen54b50912016-03-03 15:57:36 -08001041 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001042 static int aggr_fields[] = {
1043 [AGGR_GLOBAL] = 0,
1044 [AGGR_THREAD] = 1,
1045 [AGGR_NONE] = 1,
1046 [AGGR_SOCKET] = 2,
1047 [AGGR_CORE] = 2,
1048 };
1049
1050 pm = print_metric_csv;
1051 nl = new_line_csv;
1052 os.nfields = 3;
1053 os.nfields += aggr_fields[stat_config.aggr_mode];
1054 if (counter->cgrp)
1055 os.nfields++;
1056 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001057 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001058 if (metric_only) {
1059 pm(&os, NULL, "", "", 0);
1060 return;
1061 }
Andi Kleencb110f42016-01-30 09:06:51 -08001062 aggr_printout(counter, id, nr);
1063
1064 fprintf(stat_config.output, "%*s%s",
1065 csv_output ? 0 : 18,
1066 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1067 csv_sep);
1068
1069 fprintf(stat_config.output, "%-*s%s",
1070 csv_output ? 0 : unit_width,
1071 counter->unit, csv_sep);
1072
1073 fprintf(stat_config.output, "%*s",
1074 csv_output ? 0 : -25,
1075 perf_evsel__name(counter));
1076
1077 if (counter->cgrp)
1078 fprintf(stat_config.output, "%s%s",
1079 csv_sep, counter->cgrp->name);
1080
Andi Kleen92a61f62016-02-29 14:36:21 -08001081 if (!csv_output)
1082 pm(&os, NULL, NULL, "", 0);
1083 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001084 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001085 if (csv_output)
1086 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001087 return;
1088 }
1089
Andi Kleen54b50912016-03-03 15:57:36 -08001090 if (metric_only)
1091 /* nothing */;
1092 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001093 nsec_printout(id, nr, counter, uval);
1094 else
1095 abs_printout(id, nr, counter, uval);
1096
Andi Kleen140aead2016-01-30 09:06:49 -08001097 out.print_metric = pm;
1098 out.new_line = nl;
1099 out.ctx = &os;
1100
Andi Kleen54b50912016-03-03 15:57:36 -08001101 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001102 print_noise(counter, noise);
1103 print_running(run, ena);
1104 }
1105
1106 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001107 first_shadow_cpu(counter, id),
Andi Kleen140aead2016-01-30 09:06:49 -08001108 &out);
Andi Kleen54b50912016-03-03 15:57:36 -08001109 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001110 print_noise(counter, noise);
1111 print_running(run, ena);
1112 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001113}
1114
Andi Kleen44d49a62016-02-29 14:36:22 -08001115static void aggr_update_shadow(void)
1116{
1117 int cpu, s2, id, s;
1118 u64 val;
1119 struct perf_evsel *counter;
1120
1121 for (s = 0; s < aggr_map->nr; s++) {
1122 id = aggr_map->map[s];
1123 evlist__for_each(evsel_list, counter) {
1124 val = 0;
1125 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1126 s2 = aggr_get_id(evsel_list->cpus, cpu);
1127 if (s2 != id)
1128 continue;
1129 val += perf_counts(counter->counts, cpu, 0)->val;
1130 }
1131 val = val * counter->scale;
1132 perf_stat__update_shadow_stats(counter, &val,
1133 first_shadow_cpu(counter, id));
1134 }
1135 }
1136}
1137
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001138static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001139{
Jiri Olsa58215222015-07-21 14:31:24 +02001140 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001141 struct perf_evsel *counter;
Kan Liang601083c2015-07-02 03:08:43 -04001142 int cpu, s, s2, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001143 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001144 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001145 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001146
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001147 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001148 return;
1149
Andi Kleen44d49a62016-02-29 14:36:22 -08001150 aggr_update_shadow();
1151
Andi Kleen54b50912016-03-03 15:57:36 -08001152 /*
1153 * With metric_only everything is on a single line.
1154 * Without each counter has its own line.
1155 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001156 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleen54b50912016-03-03 15:57:36 -08001157 if (prefix && metric_only)
1158 fprintf(output, "%s", prefix);
1159
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001160 id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001161 first = true;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001162 evlist__for_each(evsel_list, counter) {
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001163 val = ena = run = 0;
1164 nr = 0;
1165 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Kan Liang601083c2015-07-02 03:08:43 -04001166 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001167 if (s2 != id)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001168 continue;
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001169 val += perf_counts(counter->counts, cpu, 0)->val;
1170 ena += perf_counts(counter->counts, cpu, 0)->ena;
1171 run += perf_counts(counter->counts, cpu, 0)->run;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001172 nr++;
1173 }
Andi Kleen54b50912016-03-03 15:57:36 -08001174 if (first && metric_only) {
1175 first = false;
1176 aggr_printout(counter, id, nr);
1177 }
1178 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001179 fprintf(output, "%s", prefix);
1180
Stephane Eranian410136f2013-11-12 17:58:49 +01001181 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001182 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
Andi Kleen54b50912016-03-03 15:57:36 -08001183 if (!metric_only)
1184 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001185 }
Andi Kleen54b50912016-03-03 15:57:36 -08001186 if (metric_only)
1187 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001188 }
1189}
1190
Jiri Olsa32b8af82015-06-26 11:29:27 +02001191static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1192{
Jiri Olsa58215222015-07-21 14:31:24 +02001193 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001194 int nthreads = thread_map__nr(counter->threads);
1195 int ncpus = cpu_map__nr(counter->cpus);
1196 int cpu, thread;
1197 double uval;
1198
1199 for (thread = 0; thread < nthreads; thread++) {
1200 u64 ena = 0, run = 0, val = 0;
1201
1202 for (cpu = 0; cpu < ncpus; cpu++) {
1203 val += perf_counts(counter->counts, cpu, thread)->val;
1204 ena += perf_counts(counter->counts, cpu, thread)->ena;
1205 run += perf_counts(counter->counts, cpu, thread)->run;
1206 }
1207
1208 if (prefix)
1209 fprintf(output, "%s", prefix);
1210
1211 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001212 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001213 fputc('\n', output);
1214 }
1215}
1216
Ingo Molnar42202dd2009-06-13 14:57:28 +02001217/*
1218 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001219 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001220 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001221static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001222{
Jiri Olsa58215222015-07-21 14:31:24 +02001223 FILE *output = stat_config.output;
Jiri Olsa581cc8a2015-10-16 12:41:03 +02001224 struct perf_stat_evsel *ps = counter->priv;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001225 double avg = avg_stats(&ps->res_stats[0]);
Stephane Eranian410136f2013-11-12 17:58:49 +01001226 double uval;
Andi Kleend73515c2015-03-11 07:16:27 -07001227 double avg_enabled, avg_running;
1228
1229 avg_enabled = avg_stats(&ps->res_stats[1]);
1230 avg_running = avg_stats(&ps->res_stats[2]);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001231
Andi Kleen54b50912016-03-03 15:57:36 -08001232 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001233 fprintf(output, "%s", prefix);
1234
Stephane Eranian410136f2013-11-12 17:58:49 +01001235 uval = avg * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001236 printout(-1, 0, counter, uval, prefix, avg_running, avg_enabled, avg);
Andi Kleen54b50912016-03-03 15:57:36 -08001237 if (!metric_only)
1238 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001239}
1240
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001241/*
1242 * Print out the results of a single counter:
1243 * does not use aggregated count in system-wide
1244 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001245static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001246{
Jiri Olsa58215222015-07-21 14:31:24 +02001247 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001248 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001249 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001250 int cpu;
1251
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001252 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001253 val = perf_counts(counter->counts, cpu, 0)->val;
1254 ena = perf_counts(counter->counts, cpu, 0)->ena;
1255 run = perf_counts(counter->counts, cpu, 0)->run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001256
1257 if (prefix)
1258 fprintf(output, "%s", prefix);
1259
Stephane Eranian410136f2013-11-12 17:58:49 +01001260 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001261 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001262
Stephane Eranian4aa90152011-08-15 22:22:33 +02001263 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001264 }
1265}
1266
Andi Kleen206cab62016-03-03 15:57:37 -08001267static void print_no_aggr_metric(char *prefix)
1268{
1269 int cpu;
1270 int nrcpus = 0;
1271 struct perf_evsel *counter;
1272 u64 ena, run, val;
1273 double uval;
1274
1275 nrcpus = evsel_list->cpus->nr;
1276 for (cpu = 0; cpu < nrcpus; cpu++) {
1277 bool first = true;
1278
1279 if (prefix)
1280 fputs(prefix, stat_config.output);
1281 evlist__for_each(evsel_list, counter) {
1282 if (first) {
1283 aggr_printout(counter, cpu, 0);
1284 first = false;
1285 }
1286 val = perf_counts(counter->counts, cpu, 0)->val;
1287 ena = perf_counts(counter->counts, cpu, 0)->ena;
1288 run = perf_counts(counter->counts, cpu, 0)->run;
1289
1290 uval = val * counter->scale;
1291 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1292 }
1293 fputc('\n', stat_config.output);
1294 }
1295}
1296
Andi Kleen54b50912016-03-03 15:57:36 -08001297static int aggr_header_lens[] = {
1298 [AGGR_CORE] = 18,
1299 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001300 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001301 [AGGR_THREAD] = 24,
1302 [AGGR_GLOBAL] = 0,
1303};
1304
1305static void print_metric_headers(char *prefix)
1306{
1307 struct perf_stat_output_ctx out;
1308 struct perf_evsel *counter;
1309 struct outstate os = {
1310 .fh = stat_config.output
1311 };
1312
1313 if (prefix)
1314 fprintf(stat_config.output, "%s", prefix);
1315
1316 if (!csv_output)
1317 fprintf(stat_config.output, "%*s",
1318 aggr_header_lens[stat_config.aggr_mode], "");
1319
1320 /* Print metrics headers only */
1321 evlist__for_each(evsel_list, counter) {
1322 os.evsel = counter;
1323 out.ctx = &os;
1324 out.print_metric = print_metric_header;
1325 out.new_line = new_line_metric;
1326 os.evsel = counter;
1327 perf_stat__print_shadow_stats(counter, 0,
1328 0,
1329 &out);
1330 }
1331 fputc('\n', stat_config.output);
1332}
1333
Jiri Olsad4f63a42015-06-26 11:29:26 +02001334static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001335{
Jiri Olsa58215222015-07-21 14:31:24 +02001336 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001337 static int num_print_interval;
1338
1339 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1340
Andi Kleen54b50912016-03-03 15:57:36 -08001341 if (num_print_interval == 0 && !csv_output && !metric_only) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001342 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001343 case AGGR_SOCKET:
1344 fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit");
1345 break;
1346 case AGGR_CORE:
1347 fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit");
1348 break;
1349 case AGGR_NONE:
1350 fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit");
1351 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001352 case AGGR_THREAD:
1353 fprintf(output, "# time comm-pid counts %*s events\n", unit_width, "unit");
1354 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001355 case AGGR_GLOBAL:
1356 default:
1357 fprintf(output, "# time counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001358 case AGGR_UNSET:
1359 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001360 }
1361 }
1362
1363 if (++num_print_interval == 25)
1364 num_print_interval = 0;
1365}
1366
1367static void print_header(int argc, const char **argv)
1368{
Jiri Olsa58215222015-07-21 14:31:24 +02001369 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001370 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001371
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001372 fflush(stdout);
1373
Stephane Eraniand7470b62010-12-01 18:49:05 +02001374 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001375 fprintf(output, "\n");
1376 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001377 if (target.system_wide)
1378 fprintf(output, "\'system wide");
1379 else if (target.cpu_list)
1380 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001381 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001382 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1383 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001384 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001385 } else if (target.pid)
1386 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001387 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001388 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001389
Stephane Eranian4aa90152011-08-15 22:22:33 +02001390 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001391 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001392 fprintf(output, " (%d runs)", run_count);
1393 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001394 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001395}
1396
1397static void print_footer(void)
1398{
Jiri Olsa58215222015-07-21 14:31:24 +02001399 FILE *output = stat_config.output;
1400
Jiri Olsad4f63a42015-06-26 11:29:26 +02001401 if (!null_run)
1402 fprintf(output, "\n");
1403 fprintf(output, " %17.9f seconds time elapsed",
1404 avg_stats(&walltime_nsecs_stats)/1e9);
1405 if (run_count > 1) {
1406 fprintf(output, " ");
1407 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1408 avg_stats(&walltime_nsecs_stats));
1409 }
1410 fprintf(output, "\n\n");
1411}
1412
1413static void print_counters(struct timespec *ts, int argc, const char **argv)
1414{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001415 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001416 struct perf_evsel *counter;
1417 char buf[64], *prefix = NULL;
1418
Jiri Olsa664c98d2015-11-05 15:40:50 +01001419 /* Do not print anything if we record to the pipe. */
1420 if (STAT_RECORD && perf_stat.file.is_pipe)
1421 return;
1422
Jiri Olsad4f63a42015-06-26 11:29:26 +02001423 if (interval)
1424 print_interval(prefix = buf, ts);
1425 else
1426 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001427
Andi Kleen54b50912016-03-03 15:57:36 -08001428 if (metric_only) {
1429 static int num_print_iv;
1430
1431 if (num_print_iv == 0)
1432 print_metric_headers(prefix);
1433 if (num_print_iv++ == 25)
1434 num_print_iv = 0;
1435 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1436 fprintf(stat_config.output, "%s", prefix);
1437 }
1438
Jiri Olsa421a50f2015-07-21 14:31:22 +02001439 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001440 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001441 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001442 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001443 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001444 case AGGR_THREAD:
1445 evlist__for_each(evsel_list, counter)
1446 print_aggr_thread(counter, prefix);
1447 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001448 case AGGR_GLOBAL:
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03001449 evlist__for_each(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001450 print_counter_aggr(counter, prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001451 if (metric_only)
1452 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001453 break;
1454 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001455 if (metric_only)
1456 print_no_aggr_metric(prefix);
1457 else {
1458 evlist__for_each(evsel_list, counter)
1459 print_counter(counter, prefix);
1460 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001461 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001462 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001463 default:
1464 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001465 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001466
Jiri Olsad4f63a42015-06-26 11:29:26 +02001467 if (!interval && !csv_output)
1468 print_footer();
1469
Jiri Olsa58215222015-07-21 14:31:24 +02001470 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001471}
1472
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001473static volatile int signr = -1;
1474
Ingo Molnar52425192009-05-26 09:17:18 +02001475static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001476{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001477 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001478 done = 1;
1479
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001480 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001481 /*
1482 * render child_pid harmless
1483 * won't send SIGTERM to a random
1484 * process in case of race condition
1485 * and fast PID recycling
1486 */
1487 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001488}
1489
1490static void sig_atexit(void)
1491{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001492 sigset_t set, oset;
1493
1494 /*
1495 * avoid race condition with SIGCHLD handler
1496 * in skip_signal() which is modifying child_pid
1497 * goal is to avoid send SIGTERM to a random
1498 * process
1499 */
1500 sigemptyset(&set);
1501 sigaddset(&set, SIGCHLD);
1502 sigprocmask(SIG_BLOCK, &set, &oset);
1503
Chris Wilson933da832009-10-04 01:35:01 +01001504 if (child_pid != -1)
1505 kill(child_pid, SIGTERM);
1506
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001507 sigprocmask(SIG_SETMASK, &oset, NULL);
1508
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001509 if (signr == -1)
1510 return;
1511
1512 signal(signr, SIG_DFL);
1513 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001514}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001515
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001516static int stat__set_big_num(const struct option *opt __maybe_unused,
1517 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001518{
1519 big_num_opt = unset ? 0 : 1;
1520 return 0;
1521}
1522
Jiri Olsae0547312015-11-05 15:40:45 +01001523static const struct option stat_options[] = {
1524 OPT_BOOLEAN('T', "transaction", &transaction_run,
1525 "hardware transaction statistics"),
1526 OPT_CALLBACK('e', "event", &evsel_list, "event",
1527 "event selector. use 'perf list' to list available events",
1528 parse_events_option),
1529 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1530 "event filter", parse_filter),
1531 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1532 "child tasks do not inherit counters"),
1533 OPT_STRING('p', "pid", &target.pid, "pid",
1534 "stat events on existing process id"),
1535 OPT_STRING('t', "tid", &target.tid, "tid",
1536 "stat events on existing thread id"),
1537 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1538 "system-wide collection from all CPUs"),
1539 OPT_BOOLEAN('g', "group", &group,
1540 "put the counters into a counter group"),
1541 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1542 OPT_INCR('v', "verbose", &verbose,
1543 "be more verbose (show counter open errors, etc)"),
1544 OPT_INTEGER('r', "repeat", &run_count,
1545 "repeat command and print average + stddev (max: 100, forever: 0)"),
1546 OPT_BOOLEAN('n', "null", &null_run,
1547 "null run - dont start any counters"),
1548 OPT_INCR('d', "detailed", &detailed_run,
1549 "detailed run - start a lot of events"),
1550 OPT_BOOLEAN('S', "sync", &sync_run,
1551 "call sync() before starting a run"),
1552 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1553 "print large numbers with thousands\' separators",
1554 stat__set_big_num),
1555 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1556 "list of cpus to monitor in system-wide"),
1557 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1558 "disable CPU count aggregation", AGGR_NONE),
1559 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1560 "print counts with custom separator"),
1561 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1562 "monitor event in cgroup name only", parse_cgroups),
1563 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1564 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1565 OPT_INTEGER(0, "log-fd", &output_fd,
1566 "log output to fd, instead of stderr"),
1567 OPT_STRING(0, "pre", &pre_cmd, "command",
1568 "command to run prior to the measured command"),
1569 OPT_STRING(0, "post", &post_cmd, "command",
1570 "command to run after to the measured command"),
1571 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1572 "print counts at regular interval in ms (>= 10)"),
1573 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1574 "aggregate counts per processor socket", AGGR_SOCKET),
1575 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1576 "aggregate counts per physical processor core", AGGR_CORE),
1577 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1578 "aggregate counts per thread", AGGR_THREAD),
1579 OPT_UINTEGER('D', "delay", &initial_delay,
1580 "ms to wait before starting measurement after program start"),
Andi Kleen54b50912016-03-03 15:57:36 -08001581 OPT_BOOLEAN(0, "metric-only", &metric_only,
1582 "Only print computed metrics. No raw values"),
Jiri Olsae0547312015-11-05 15:40:45 +01001583 OPT_END()
1584};
1585
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001586static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1587{
1588 return cpu_map__get_socket(map, cpu, NULL);
1589}
1590
1591static int perf_stat__get_core(struct cpu_map *map, int cpu)
1592{
1593 return cpu_map__get_core(map, cpu, NULL);
1594}
1595
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001596static int cpu_map__get_max(struct cpu_map *map)
1597{
1598 int i, max = -1;
1599
1600 for (i = 0; i < map->nr; i++) {
1601 if (map->map[i] > max)
1602 max = map->map[i];
1603 }
1604
1605 return max;
1606}
1607
1608static struct cpu_map *cpus_aggr_map;
1609
1610static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1611{
1612 int cpu;
1613
1614 if (idx >= map->nr)
1615 return -1;
1616
1617 cpu = map->map[idx];
1618
1619 if (cpus_aggr_map->map[cpu] == -1)
1620 cpus_aggr_map->map[cpu] = get_id(map, idx);
1621
1622 return cpus_aggr_map->map[cpu];
1623}
1624
1625static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1626{
1627 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1628}
1629
1630static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1631{
1632 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1633}
1634
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001635static int perf_stat_init_aggr_mode(void)
1636{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001637 int nr;
1638
Jiri Olsa421a50f2015-07-21 14:31:22 +02001639 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001640 case AGGR_SOCKET:
1641 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1642 perror("cannot build socket map");
1643 return -1;
1644 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001645 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001646 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001647 case AGGR_CORE:
1648 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1649 perror("cannot build core map");
1650 return -1;
1651 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001652 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001653 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001654 case AGGR_NONE:
1655 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001656 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001657 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001658 default:
1659 break;
1660 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001661
1662 /*
1663 * The evsel_list->cpus is the base we operate on,
1664 * taking the highest cpu number to be the size of
1665 * the aggregation translate cpumap.
1666 */
1667 nr = cpu_map__get_max(evsel_list->cpus);
1668 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1669 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001670}
1671
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001672static void perf_stat__exit_aggr_mode(void)
1673{
1674 cpu_map__put(aggr_map);
1675 cpu_map__put(cpus_aggr_map);
1676 aggr_map = NULL;
1677 cpus_aggr_map = NULL;
1678}
1679
Jiri Olsa68d702f2015-11-05 15:40:58 +01001680static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1681{
1682 int cpu;
1683
1684 if (idx > map->nr)
1685 return -1;
1686
1687 cpu = map->map[idx];
1688
1689 if (cpu >= env->nr_cpus_online)
1690 return -1;
1691
1692 return cpu;
1693}
1694
1695static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1696{
1697 struct perf_env *env = data;
1698 int cpu = perf_env__get_cpu(env, map, idx);
1699
1700 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1701}
1702
1703static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1704{
1705 struct perf_env *env = data;
1706 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1707
1708 if (cpu != -1) {
1709 int socket_id = env->cpu[cpu].socket_id;
1710
1711 /*
1712 * Encode socket in upper 16 bits
1713 * core_id is relative to socket, and
1714 * we need a global id. So we combine
1715 * socket + core id.
1716 */
1717 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1718 }
1719
1720 return core;
1721}
1722
1723static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1724 struct cpu_map **sockp)
1725{
1726 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1727}
1728
1729static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1730 struct cpu_map **corep)
1731{
1732 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1733}
1734
1735static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1736{
1737 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1738}
1739
1740static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1741{
1742 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1743}
1744
1745static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1746{
1747 struct perf_env *env = &st->session->header.env;
1748
1749 switch (stat_config.aggr_mode) {
1750 case AGGR_SOCKET:
1751 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1752 perror("cannot build socket map");
1753 return -1;
1754 }
1755 aggr_get_id = perf_stat__get_socket_file;
1756 break;
1757 case AGGR_CORE:
1758 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1759 perror("cannot build core map");
1760 return -1;
1761 }
1762 aggr_get_id = perf_stat__get_core_file;
1763 break;
1764 case AGGR_NONE:
1765 case AGGR_GLOBAL:
1766 case AGGR_THREAD:
1767 case AGGR_UNSET:
1768 default:
1769 break;
1770 }
1771
1772 return 0;
1773}
1774
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001775/*
1776 * Add default attributes, if there were no attributes specified or
1777 * if -d/--detailed, -d -d or -d -d -d is used:
1778 */
1779static int add_default_attributes(void)
1780{
Andi Kleen9dec4472016-02-26 16:27:56 -08001781 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001782
1783 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1784 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1785 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1786 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1787
1788 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08001789};
1790 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001791 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001792};
1793 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001794 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001795};
1796 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001797 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1798 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1799 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1800
1801};
1802
1803/*
1804 * Detailed stats (-d), covering the L1 and last level data caches:
1805 */
1806 struct perf_event_attr detailed_attrs[] = {
1807
1808 { .type = PERF_TYPE_HW_CACHE,
1809 .config =
1810 PERF_COUNT_HW_CACHE_L1D << 0 |
1811 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1812 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1813
1814 { .type = PERF_TYPE_HW_CACHE,
1815 .config =
1816 PERF_COUNT_HW_CACHE_L1D << 0 |
1817 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1818 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1819
1820 { .type = PERF_TYPE_HW_CACHE,
1821 .config =
1822 PERF_COUNT_HW_CACHE_LL << 0 |
1823 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1824 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1825
1826 { .type = PERF_TYPE_HW_CACHE,
1827 .config =
1828 PERF_COUNT_HW_CACHE_LL << 0 |
1829 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1830 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1831};
1832
1833/*
1834 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1835 */
1836 struct perf_event_attr very_detailed_attrs[] = {
1837
1838 { .type = PERF_TYPE_HW_CACHE,
1839 .config =
1840 PERF_COUNT_HW_CACHE_L1I << 0 |
1841 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1842 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1843
1844 { .type = PERF_TYPE_HW_CACHE,
1845 .config =
1846 PERF_COUNT_HW_CACHE_L1I << 0 |
1847 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1848 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1849
1850 { .type = PERF_TYPE_HW_CACHE,
1851 .config =
1852 PERF_COUNT_HW_CACHE_DTLB << 0 |
1853 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1854 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1855
1856 { .type = PERF_TYPE_HW_CACHE,
1857 .config =
1858 PERF_COUNT_HW_CACHE_DTLB << 0 |
1859 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1860 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1861
1862 { .type = PERF_TYPE_HW_CACHE,
1863 .config =
1864 PERF_COUNT_HW_CACHE_ITLB << 0 |
1865 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1866 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1867
1868 { .type = PERF_TYPE_HW_CACHE,
1869 .config =
1870 PERF_COUNT_HW_CACHE_ITLB << 0 |
1871 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1872 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1873
1874};
1875
1876/*
1877 * Very, very detailed stats (-d -d -d), adding prefetch events:
1878 */
1879 struct perf_event_attr very_very_detailed_attrs[] = {
1880
1881 { .type = PERF_TYPE_HW_CACHE,
1882 .config =
1883 PERF_COUNT_HW_CACHE_L1D << 0 |
1884 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1885 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1886
1887 { .type = PERF_TYPE_HW_CACHE,
1888 .config =
1889 PERF_COUNT_HW_CACHE_L1D << 0 |
1890 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1891 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1892};
1893
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001894 /* Set attrs if no event is selected and !null_run: */
1895 if (null_run)
1896 return 0;
1897
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001898 if (transaction_run) {
1899 int err;
1900 if (pmu_have_event("cpu", "cycles-ct") &&
1901 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02001902 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001903 else
Jiri Olsaa4547422015-06-03 16:25:53 +02001904 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
1905 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07001906 fprintf(stderr, "Cannot set up transaction events\n");
1907 return -1;
1908 }
1909 return 0;
1910 }
1911
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001912 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09001913 if (target__has_cpu(&target))
1914 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
1915
Andi Kleen9dec4472016-02-26 16:27:56 -08001916 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
1917 return -1;
1918 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
1919 if (perf_evlist__add_default_attrs(evsel_list,
1920 frontend_attrs) < 0)
1921 return -1;
1922 }
1923 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
1924 if (perf_evlist__add_default_attrs(evsel_list,
1925 backend_attrs) < 0)
1926 return -1;
1927 }
1928 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001929 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001930 }
1931
1932 /* Detailed events get appended to the event list: */
1933
1934 if (detailed_run < 1)
1935 return 0;
1936
1937 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001938 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001939 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001940
1941 if (detailed_run < 2)
1942 return 0;
1943
1944 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001945 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001946 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001947
1948 if (detailed_run < 3)
1949 return 0;
1950
1951 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001952 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001953}
1954
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01001955static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001956 "perf stat record [<options>]",
1957 NULL,
1958};
1959
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001960static void init_features(struct perf_session *session)
1961{
1962 int feat;
1963
1964 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
1965 perf_header__set_feat(&session->header, feat);
1966
1967 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
1968 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
1969 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
1970 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
1971}
1972
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001973static int __cmd_record(int argc, const char **argv)
1974{
1975 struct perf_session *session;
1976 struct perf_data_file *file = &perf_stat.file;
1977
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01001978 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001979 PARSE_OPT_STOP_AT_NON_OPTION);
1980
1981 if (output_name)
1982 file->path = output_name;
1983
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01001984 if (run_count != 1 || forever) {
1985 pr_err("Cannot use -r option with perf stat record.\n");
1986 return -1;
1987 }
1988
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001989 session = perf_session__new(file, false, NULL);
1990 if (session == NULL) {
1991 pr_err("Perf session creation failed.\n");
1992 return -1;
1993 }
1994
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01001995 init_features(session);
1996
Jiri Olsa4979d0c2015-11-05 15:40:46 +01001997 session->evlist = evsel_list;
1998 perf_stat.session = session;
1999 perf_stat.record = true;
2000 return argc;
2001}
2002
Jiri Olsaa56f9392015-11-05 15:40:59 +01002003static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2004 union perf_event *event,
2005 struct perf_session *session)
2006{
Andi Kleene3b03b62016-05-05 16:04:03 -07002007 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002008 struct perf_evsel *counter;
2009 struct timespec tsh, *ts = NULL;
2010 const char **argv = session->header.env.cmdline_argv;
2011 int argc = session->header.env.nr_cmdline;
2012
2013 evlist__for_each(evsel_list, counter)
2014 perf_stat_process_counter(&stat_config, counter);
2015
Andi Kleene3b03b62016-05-05 16:04:03 -07002016 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2017 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002018
Andi Kleene3b03b62016-05-05 16:04:03 -07002019 if (stat_config.interval && stat_round->time) {
2020 tsh.tv_sec = stat_round->time / NSECS_PER_SEC;
2021 tsh.tv_nsec = stat_round->time % NSECS_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002022 ts = &tsh;
2023 }
2024
2025 print_counters(ts, argc, argv);
2026 return 0;
2027}
2028
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002029static
2030int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2031 union perf_event *event,
2032 struct perf_session *session __maybe_unused)
2033{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002034 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2035
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002036 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002037
Jiri Olsa89af4e02015-11-05 15:41:02 +01002038 if (cpu_map__empty(st->cpus)) {
2039 if (st->aggr_mode != AGGR_UNSET)
2040 pr_warning("warning: processing task data, aggregation mode not set\n");
2041 return 0;
2042 }
2043
2044 if (st->aggr_mode != AGGR_UNSET)
2045 stat_config.aggr_mode = st->aggr_mode;
2046
Jiri Olsa68d702f2015-11-05 15:40:58 +01002047 if (perf_stat.file.is_pipe)
2048 perf_stat_init_aggr_mode();
2049 else
2050 perf_stat_init_aggr_mode_file(st);
2051
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002052 return 0;
2053}
2054
Jiri Olsa1975d362015-11-05 15:40:56 +01002055static int set_maps(struct perf_stat *st)
2056{
2057 if (!st->cpus || !st->threads)
2058 return 0;
2059
2060 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2061 return -EINVAL;
2062
2063 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2064
2065 if (perf_evlist__alloc_stats(evsel_list, true))
2066 return -ENOMEM;
2067
2068 st->maps_allocated = true;
2069 return 0;
2070}
2071
2072static
2073int process_thread_map_event(struct perf_tool *tool __maybe_unused,
2074 union perf_event *event,
2075 struct perf_session *session __maybe_unused)
2076{
2077 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2078
2079 if (st->threads) {
2080 pr_warning("Extra thread map event, ignoring.\n");
2081 return 0;
2082 }
2083
2084 st->threads = thread_map__new_event(&event->thread_map);
2085 if (!st->threads)
2086 return -ENOMEM;
2087
2088 return set_maps(st);
2089}
2090
2091static
2092int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2093 union perf_event *event,
2094 struct perf_session *session __maybe_unused)
2095{
2096 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2097 struct cpu_map *cpus;
2098
2099 if (st->cpus) {
2100 pr_warning("Extra cpu map event, ignoring.\n");
2101 return 0;
2102 }
2103
2104 cpus = cpu_map__new_data(&event->cpu_map.data);
2105 if (!cpus)
2106 return -ENOMEM;
2107
2108 st->cpus = cpus;
2109 return set_maps(st);
2110}
2111
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002112static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002113 "perf stat report [<options>]",
2114 NULL,
2115};
2116
2117static struct perf_stat perf_stat = {
2118 .tool = {
2119 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002120 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002121 .thread_map = process_thread_map_event,
2122 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002123 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002124 .stat = perf_event__process_stat_event,
2125 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002126 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002127 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002128};
2129
2130static int __cmd_report(int argc, const char **argv)
2131{
2132 struct perf_session *session;
2133 const struct option options[] = {
2134 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002135 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2136 "aggregate counts per processor socket", AGGR_SOCKET),
2137 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2138 "aggregate counts per physical processor core", AGGR_CORE),
2139 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2140 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002141 OPT_END()
2142 };
2143 struct stat st;
2144 int ret;
2145
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002146 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002147
2148 if (!input_name || !strlen(input_name)) {
2149 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2150 input_name = "-";
2151 else
2152 input_name = "perf.data";
2153 }
2154
2155 perf_stat.file.path = input_name;
2156 perf_stat.file.mode = PERF_DATA_MODE_READ;
2157
2158 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2159 if (session == NULL)
2160 return -1;
2161
2162 perf_stat.session = session;
2163 stat_config.output = stderr;
2164 evsel_list = session->evlist;
2165
2166 ret = perf_session__process_events(session);
2167 if (ret)
2168 return ret;
2169
2170 perf_session__delete(session);
2171 return 0;
2172}
2173
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002174int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02002175{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002176 const char * const stat_usage[] = {
2177 "perf stat [<options>] [<command>]",
2178 NULL
2179 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002180 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002181 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002182 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002183 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002184 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002185
Stephane Eranian5af52b52010-05-18 15:00:01 +02002186 setlocale(LC_ALL, "");
2187
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002188 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002189 if (evsel_list == NULL)
2190 return -ENOMEM;
2191
Wang Nan1669e502016-02-19 11:43:58 +00002192 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002193 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2194 (const char **) stat_usage,
2195 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002196 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002197
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002198 if (csv_sep) {
2199 csv_output = true;
2200 if (!strcmp(csv_sep, "\\t"))
2201 csv_sep = "\t";
2202 } else
2203 csv_sep = DEFAULT_SEPARATOR;
2204
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002205 if (argc && !strncmp(argv[0], "rec", 3)) {
2206 argc = __cmd_record(argc, argv);
2207 if (argc < 0)
2208 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002209 } else if (argc && !strncmp(argv[0], "rep", 3))
2210 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002211
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002212 interval = stat_config.interval;
2213
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002214 /*
2215 * For record command the -o is already taken care of.
2216 */
2217 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002218 output = NULL;
2219
Jim Cromie56f3bae2011-09-07 17:14:00 -06002220 if (output_name && output_fd) {
2221 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002222 parse_options_usage(stat_usage, stat_options, "o", 1);
2223 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002224 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002225 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002226
Andi Kleen54b50912016-03-03 15:57:36 -08002227 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2228 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2229 goto out;
2230 }
2231
Andi Kleen54b50912016-03-03 15:57:36 -08002232 if (metric_only && run_count > 1) {
2233 fprintf(stderr, "--metric-only is not supported with -r\n");
2234 goto out;
2235 }
2236
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002237 if (output_fd < 0) {
2238 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002239 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002240 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002241 }
2242
Stephane Eranian4aa90152011-08-15 22:22:33 +02002243 if (!output) {
2244 struct timespec tm;
2245 mode = append_file ? "a" : "w";
2246
2247 output = fopen(output_name, mode);
2248 if (!output) {
2249 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002250 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002251 }
2252 clock_gettime(CLOCK_REALTIME, &tm);
2253 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002254 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002255 mode = append_file ? "a" : "w";
2256 output = fdopen(output_fd, mode);
2257 if (!output) {
2258 perror("Failed opening logfd");
2259 return -errno;
2260 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002261 }
2262
Jiri Olsa58215222015-07-21 14:31:24 +02002263 stat_config.output = output;
2264
Stephane Eraniand7470b62010-12-01 18:49:05 +02002265 /*
2266 * let the spreadsheet do the pretty-printing
2267 */
2268 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002269 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002270 if (big_num_opt == 1) {
2271 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002272 parse_options_usage(stat_usage, stat_options, "B", 1);
2273 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002274 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002275 } else /* Nope, so disable big number formatting */
2276 big_num = false;
2277 } else if (big_num_opt == 0) /* User passed --no-big-num */
2278 big_num = false;
2279
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002280 if (!argc && target__none(&target))
Jiri Olsae0547312015-11-05 15:40:45 +01002281 usage_with_options(stat_usage, stat_options);
David Ahernac3063b2013-09-30 07:37:37 -06002282
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002283 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002284 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002285 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002286 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002287 } else if (run_count == 0) {
2288 forever = true;
2289 run_count = 1;
2290 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002291
Jiri Olsa421a50f2015-07-21 14:31:22 +02002292 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002293 fprintf(stderr, "The --per-thread option is only available "
2294 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002295 parse_options_usage(NULL, stat_options, "p", 1);
2296 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002297 goto out;
2298 }
2299
2300 /*
2301 * no_aggr, cgroup are for system-wide only
2302 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2303 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002304 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2305 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002306 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002307 fprintf(stderr, "both cgroup and no-aggregation "
2308 "modes only available in system-wide mode\n");
2309
Jiri Olsae0547312015-11-05 15:40:45 +01002310 parse_options_usage(stat_usage, stat_options, "G", 1);
2311 parse_options_usage(NULL, stat_options, "A", 1);
2312 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002313 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002314 }
2315
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002316 if (add_default_attributes())
2317 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002318
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002319 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002320
Namhyung Kim77a6f012012-05-07 14:09:04 +09002321 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002322 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002323 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002324 parse_options_usage(stat_usage, stat_options, "p", 1);
2325 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002326 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002327 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002328 parse_options_usage(stat_usage, stat_options, "C", 1);
2329 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002330 }
2331 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002332 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002333
2334 /*
2335 * Initialize thread_map with comm names,
2336 * so we could print it out on output.
2337 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002338 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02002339 thread_map__read_comms(evsel_list->threads);
2340
Stephane Eranian13370a92013-01-29 12:47:44 +01002341 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002342 if (interval < 10) {
2343 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002344 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002345 goto out;
2346 } else
2347 pr_warning("print interval < 100ms. "
2348 "The overhead percentage could be high in some cases. "
2349 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002350 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002351
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002352 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002353 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002354
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002355 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002356 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002357
Ingo Molnar58d7e992009-05-15 11:03:23 +02002358 /*
2359 * We dont want to block the signals - that would cause
2360 * child tasks to inherit that and Ctrl-C would not work.
2361 * What we want is for Ctrl-C to work in the exec()-ed
2362 * task, but being ignored by perf stat itself:
2363 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002364 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002365 if (!forever)
2366 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002367 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002368 signal(SIGALRM, skip_signal);
2369 signal(SIGABRT, skip_signal);
2370
Ingo Molnar42202dd2009-06-13 14:57:28 +02002371 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002372 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Ingo Molnar42202dd2009-06-13 14:57:28 +02002373 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002374 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2375 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002376
Ingo Molnar42202dd2009-06-13 14:57:28 +02002377 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002378 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002379 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc2015-06-26 11:29:13 +02002380 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002381 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002382 }
2383
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002384 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002385 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002386
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002387 if (STAT_RECORD) {
2388 /*
2389 * We synthesize the kernel mmap record just so that older tools
2390 * don't emit warnings about not being able to resolve symbols
2391 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2392 * a saner message about no samples being in the perf.data file.
2393 *
2394 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002395 * in header.c at the moment 'perf stat record' gets introduced, which
2396 * is not really needed once we start adding the stat specific PERF_RECORD_
2397 * records, but the need to suppress the kptr_restrict messages in older
2398 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002399 */
2400 int fd = perf_data_file__fd(&perf_stat.file);
2401 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2402 process_synthesized_event,
2403 &perf_stat.session->machines.host);
2404 if (err) {
2405 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2406 "older tools may produce warnings about this file\n.");
2407 }
2408
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002409 if (!interval) {
2410 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2411 pr_err("failed to write stat round event\n");
2412 }
2413
Jiri Olsa664c98d2015-11-05 15:40:50 +01002414 if (!perf_stat.file.is_pipe) {
2415 perf_stat.session->header.data_size += perf_stat.bytes_written;
2416 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2417 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002418
2419 perf_session__delete(perf_stat.session);
2420 }
2421
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002422 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002423 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002424out:
2425 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002426 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002427}