blob: 90882b1d6a915db8288d5705e88c277e3e8be0cc [file] [log] [blame]
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-stat.c
3 *
4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
6 *
7 * Sample output:
Ingo Molnarddcacfa2009-04-20 15:37:32 +02008
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02009 $ perf stat ./hackbench 10
Ingo Molnarddcacfa2009-04-20 15:37:32 +020010
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020011 Time: 0.118
Ingo Molnarddcacfa2009-04-20 15:37:32 +020012
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020013 Performance counter stats for './hackbench 10':
Ingo Molnarddcacfa2009-04-20 15:37:32 +020014
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020015 1708.761321 task-clock # 11.037 CPUs utilized
16 41,190 context-switches # 0.024 M/sec
17 6,735 CPU-migrations # 0.004 M/sec
18 17,318 page-faults # 0.010 M/sec
19 5,205,202,243 cycles # 3.046 GHz
20 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
21 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
22 2,603,501,247 instructions # 0.50 insns per cycle
23 # 1.48 stalled cycles per insn
24 484,357,498 branches # 283.455 M/sec
25 6,388,934 branch-misses # 1.32% of all branches
26
27 0.154822978 seconds time elapsed
Ingo Molnarddcacfa2009-04-20 15:37:32 +020028
Ingo Molnar52425192009-05-26 09:17:18 +020029 *
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020030 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
Ingo Molnar52425192009-05-26 09:17:18 +020031 *
32 * Improvements and fixes by:
33 *
34 * Arjan van de Ven <arjan@linux.intel.com>
35 * Yanmin Zhang <yanmin.zhang@intel.com>
36 * Wu Fengguang <fengguang.wu@intel.com>
37 * Mike Galbraith <efault@gmx.de>
38 * Paul Mackerras <paulus@samba.org>
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +053039 * Jaswinder Singh Rajput <jaswinder@kernel.org>
Ingo Molnar52425192009-05-26 09:17:18 +020040 *
41 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnarddcacfa2009-04-20 15:37:32 +020042 */
43
Peter Zijlstra1a482f32009-05-23 18:28:58 +020044#include "perf.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +020045#include "builtin.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030046#include "util/cgroup.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020047#include "util/util.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060048#include <subcmd/parse-options.h>
Ingo Molnar52425192009-05-26 09:17:18 +020049#include "util/parse-events.h"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070050#include "util/pmu.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020051#include "util/event.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020052#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020053#include "util/evsel.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020054#include "util/debug.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020055#include "util/color.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080056#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080057#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110058#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030059#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020060#include "util/thread_map.h"
Jiri Olsad8095602015-08-07 12:51:03 +020061#include "util/counts.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030062#include "util/group.h"
Jiri Olsa4979d0c2015-11-05 15:40:46 +010063#include "util/session.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010064#include "util/tool.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030065#include "util/group.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010066#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020067
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030068#include <linux/time64.h>
Andi Kleen44b1e602016-05-30 12:49:42 -030069#include <api/fs/fs.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020070#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020071#include <sys/prctl.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020072#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070073#include <math.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020074
Stephane Eraniand7470b62010-12-01 18:49:05 +020075#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060076#define CNTR_NOT_SUPPORTED "<not supported>"
77#define CNTR_NOT_COUNTED "<not counted>"
Stephane Eraniand7470b62010-12-01 18:49:05 +020078
Jiri Olsad4f63a42015-06-26 11:29:26 +020079static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010080
Andi Kleen4cabc3d2013-08-21 16:47:26 -070081/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020082static const char *transaction_attrs = {
83 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070084 "{"
85 "instructions,"
86 "cycles,"
87 "cpu/cycles-t/,"
88 "cpu/tx-start/,"
89 "cpu/el-start/,"
90 "cpu/cycles-ct/"
91 "}"
92};
93
94/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +020095static const char * transaction_limited_attrs = {
96 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070097 "{"
98 "instructions,"
99 "cycles,"
100 "cpu/cycles-t/,"
101 "cpu/tx-start/"
102 "}"
103};
104
Andi Kleen44b1e602016-05-30 12:49:42 -0300105static const char * topdown_attrs[] = {
106 "topdown-total-slots",
107 "topdown-slots-retired",
108 "topdown-recovery-bubbles",
109 "topdown-fetch-bubbles",
110 "topdown-slots-issued",
111 NULL,
112};
113
Robert Richter666e6d42012-04-05 18:26:27 +0200114static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200115
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300116static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900117 .uid = UINT_MAX,
118};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530119
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100120typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
121
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530122static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200123static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200124static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000125static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200126static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700127static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300128static bool topdown_run = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200129static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200130static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200131static const char *csv_sep = NULL;
132static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800133static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200134static const char *pre_cmd = NULL;
135static const char *post_cmd = NULL;
136static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700137static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100138static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500139static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800140static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300141static bool force_metric_only = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100142static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100143static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100144static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100145static bool append_file;
146static const char *output_name;
147static int output_fd;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200148
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100149struct perf_stat {
150 bool record;
151 struct perf_data_file file;
152 struct perf_session *session;
153 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100154 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100155 bool maps_allocated;
156 struct cpu_map *cpus;
157 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100158 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100159};
160
161static struct perf_stat perf_stat;
162#define STAT_RECORD perf_stat.record
163
Liming Wang60666c62009-12-31 16:05:50 +0800164static volatile int done = 0;
165
Jiri Olsa421a50f2015-07-21 14:31:22 +0200166static struct perf_stat_config stat_config = {
167 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200168 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200169};
170
Stephane Eranian13370a92013-01-29 12:47:44 +0100171static inline void diff_timespec(struct timespec *r, struct timespec *a,
172 struct timespec *b)
173{
174 r->tv_sec = a->tv_sec - b->tv_sec;
175 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300176 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100177 r->tv_sec--;
178 } else {
179 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
180 }
181}
182
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200183static void perf_stat__reset_stats(void)
184{
185 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200186 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200187}
188
Jiri Olsacac21422012-11-12 18:34:00 +0100189static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200190{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200191 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200192
Jiri Olsa711a5722015-07-21 14:31:23 +0200193 if (stat_config.scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200194 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
195 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200196
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200197 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300198
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100199 /*
200 * Some events get initialized with sample_(period/type) set,
201 * like tracepoints. Clear it up for counting.
202 */
203 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100204
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100205 /*
206 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
207 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100208 *
209 * However for pipe sessions we need to keep it zero,
210 * because script's perf_evsel__check_attr is triggered
211 * by attr->sample_type != 0, and we can't run it on
212 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100213 */
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100214 if (!(STAT_RECORD && perf_stat.file.is_pipe))
215 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100216
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100217 /*
218 * Disabling all counters initially, they will be enabled
219 * either manually by us or by kernel via enable_on_exec
220 * set later.
221 */
Jiri Olsac8280cec2015-12-03 10:06:45 +0100222 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100223 attr->disabled = 1;
224
Jiri Olsac8280cec2015-12-03 10:06:45 +0100225 /*
226 * In case of initial_delay we enable tracee
227 * events manually.
228 */
229 if (target__none(&target) && !initial_delay)
230 attr->enable_on_exec = 1;
231 }
232
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300233 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300234 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200235
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300236 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200237}
238
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200239/*
240 * Does the counter have nsecs as a unit?
241 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200242static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200243{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200244 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
245 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200246 return 1;
247
248 return 0;
249}
250
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100251static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100252 union perf_event *event,
253 struct perf_sample *sample __maybe_unused,
254 struct machine *machine __maybe_unused)
255{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100256 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
257 pr_err("failed to write perf data, error: %m\n");
258 return -1;
259 }
260
261 perf_stat.bytes_written += event->header.size;
262 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100263}
264
Jiri Olsa1975d362015-11-05 15:40:56 +0100265static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100266{
Jiri Olsa1975d362015-11-05 15:40:56 +0100267 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100268 process_synthesized_event,
269 NULL);
270}
271
272#define WRITE_STAT_ROUND_EVENT(time, interval) \
273 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
274
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100275#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
276
277static int
278perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
279 struct perf_counts_values *count)
280{
281 struct perf_sample_id *sid = SID(counter, cpu, thread);
282
283 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
284 process_synthesized_event, NULL);
285}
286
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200287/*
288 * Read out the results of a single counter:
289 * do not aggregate counts across CPUs in system-wide mode
290 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200291static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200292{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100293 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100294 int ncpus, cpu, thread;
295
296 if (target__has_cpu(&target))
297 ncpus = perf_evsel__nr_cpus(counter);
298 else
299 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200300
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000301 if (!counter->supported)
302 return -ENOENT;
303
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100304 if (counter->system_wide)
305 nthreads = 1;
306
307 for (thread = 0; thread < nthreads; thread++) {
308 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200309 struct perf_counts_values *count;
310
311 count = perf_counts(counter->counts, cpu, thread);
312 if (perf_evsel__read(counter, cpu, thread, count))
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100313 return -1;
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100314
315 if (STAT_RECORD) {
316 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
317 pr_err("failed to write stat event\n");
318 return -1;
319 }
320 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700321
322 if (verbose > 1) {
323 fprintf(stat_config.output,
324 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
325 perf_evsel__name(counter),
326 cpu,
327 count->val, count->ena, count->run);
328 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100329 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200330 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200331
332 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200333}
334
Mark Rutland3df33ef2016-08-09 14:04:29 +0100335static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200336{
337 struct perf_evsel *counter;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200338
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300339 evlist__for_each_entry(evsel_list, counter) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200340 if (read_counter(counter))
Andi Kleen245bad82015-09-01 15:52:46 -0700341 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200342
Jiri Olsaf80010e2015-07-21 14:31:27 +0200343 if (perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200344 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200345 }
346}
347
Jiri Olsaba411a92015-06-26 11:29:24 +0200348static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100349{
Stephane Eranian13370a92013-01-29 12:47:44 +0100350 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100351
Mark Rutland3df33ef2016-08-09 14:04:29 +0100352 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100353
Stephane Eranian13370a92013-01-29 12:47:44 +0100354 clock_gettime(CLOCK_MONOTONIC, &ts);
355 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100356
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100357 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300358 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100359 pr_err("failed to write stat round event\n");
360 }
361
Jiri Olsad4f63a42015-06-26 11:29:26 +0200362 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100363}
364
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100365static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700366{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100367 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300368 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100369
370 /*
371 * We need to enable counters only if:
372 * - we don't have tracee (attaching to task or cpu)
373 * - we have initial delay configured
374 */
375 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100376 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700377}
378
Mark Rutland3df33ef2016-08-09 14:04:29 +0100379static void disable_counters(void)
380{
381 /*
382 * If we don't have tracee (attaching to task or cpu), counters may
383 * still be running. To get accurate group ratios, we must stop groups
384 * from counting before reading their constituent counters.
385 */
386 if (!target__none(&target))
387 perf_evlist__disable(evsel_list);
388}
389
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300390static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300391
392/*
393 * perf_evlist__prepare_workload will send a SIGUSR1
394 * if the fork fails, since we asked by setting its
395 * want_signal to true.
396 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300397static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
398 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300399{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300400 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300401}
402
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100403static bool has_unit(struct perf_evsel *counter)
404{
405 return counter->unit && *counter->unit;
406}
407
408static bool has_scale(struct perf_evsel *counter)
409{
410 return counter->scale != 1;
411}
412
Jiri Olsa664c98d2015-11-05 15:40:50 +0100413static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100414{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100415 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100416 int err;
417
Jiri Olsa664c98d2015-11-05 15:40:50 +0100418 if (is_pipe) {
419 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
420 process_synthesized_event);
421 if (err < 0) {
422 pr_err("Couldn't synthesize attrs.\n");
423 return err;
424 }
425 }
426
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100427 /*
428 * Synthesize other events stuff not carried within
429 * attr event - unit, scale, name
430 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300431 evlist__for_each_entry(evsel_list, counter) {
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100432 if (!counter->supported)
433 continue;
434
435 /*
436 * Synthesize unit and scale only if it's defined.
437 */
438 if (has_unit(counter)) {
439 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
440 if (err < 0) {
441 pr_err("Couldn't synthesize evsel unit.\n");
442 return err;
443 }
444 }
445
446 if (has_scale(counter)) {
447 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
448 if (err < 0) {
449 pr_err("Couldn't synthesize evsel scale.\n");
450 return err;
451 }
452 }
453
454 if (counter->own_cpus) {
455 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
456 if (err < 0) {
457 pr_err("Couldn't synthesize evsel scale.\n");
458 return err;
459 }
460 }
461
462 /*
463 * Name is needed only for pipe output,
464 * perf.data carries event names.
465 */
466 if (is_pipe) {
467 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
468 if (err < 0) {
469 pr_err("Couldn't synthesize evsel name.\n");
470 return err;
471 }
472 }
473 }
474
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100475 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
476 process_synthesized_event,
477 NULL);
478 if (err < 0) {
479 pr_err("Couldn't synthesize thread map.\n");
480 return err;
481 }
482
483 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
484 process_synthesized_event, NULL);
485 if (err < 0) {
486 pr_err("Couldn't synthesize thread map.\n");
487 return err;
488 }
489
490 err = perf_event__synthesize_stat_config(NULL, &stat_config,
491 process_synthesized_event, NULL);
492 if (err < 0) {
493 pr_err("Couldn't synthesize config.\n");
494 return err;
495 }
496
497 return 0;
498}
499
Jiri Olsa2af46462015-11-05 15:40:49 +0100500#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
501
502static int __store_counter_ids(struct perf_evsel *counter,
503 struct cpu_map *cpus,
504 struct thread_map *threads)
505{
506 int cpu, thread;
507
508 for (cpu = 0; cpu < cpus->nr; cpu++) {
509 for (thread = 0; thread < threads->nr; thread++) {
510 int fd = FD(counter, cpu, thread);
511
512 if (perf_evlist__id_add_fd(evsel_list, counter,
513 cpu, thread, fd) < 0)
514 return -1;
515 }
516 }
517
518 return 0;
519}
520
521static int store_counter_ids(struct perf_evsel *counter)
522{
523 struct cpu_map *cpus = counter->cpus;
524 struct thread_map *threads = counter->threads;
525
526 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
527 return -ENOMEM;
528
529 return __store_counter_ids(counter, cpus, threads);
530}
531
Namhyung Kimacf28922013-03-11 16:43:18 +0900532static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200533{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200534 int interval = stat_config.interval;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300535 char msg[512];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200536 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100537 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100538 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100539 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200540 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300541 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100542 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200543
Stephane Eranian13370a92013-01-29 12:47:44 +0100544 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300545 ts.tv_sec = interval / USEC_PER_MSEC;
546 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100547 } else {
548 ts.tv_sec = 1;
549 ts.tv_nsec = 0;
550 }
551
Liming Wang60666c62009-12-31 16:05:50 +0800552 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100553 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300554 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900555 perror("failed to prepare workload");
556 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800557 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900558 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000559 }
560
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200561 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300562 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200563
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300564 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300565try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100566 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600567 /*
568 * PPC returns ENXIO for HW counters until 2.6.37
569 * (behavior changed with commit b0a873e).
570 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100571 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600572 errno == ENOENT || errno == EOPNOTSUPP ||
573 errno == ENXIO) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600574 if (verbose)
575 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300576 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600577 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400578
579 if ((counter->leader != counter) ||
580 !(counter->leader->nr_members > 1))
581 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300582 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
583 if (verbose)
584 ui__warning("%s\n", msg);
585 goto try_again;
586 }
Ingo Molnarede70292011-04-28 08:48:42 +0200587
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300588 perf_evsel__open_strerror(counter, &target,
589 errno, msg, sizeof(msg));
590 ui__error("%s\n", msg);
591
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200592 if (child_pid != -1)
593 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600594
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200595 return -1;
596 }
David Ahern2cee77c2011-05-30 08:55:59 -0600597 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100598
599 l = strlen(counter->unit);
600 if (l > unit_width)
601 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100602
603 if (STAT_RECORD && store_counter_ids(counter))
604 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300605 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200606
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300607 if (perf_evlist__apply_filters(evsel_list, &counter)) {
608 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
609 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300610 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100611 return -1;
612 }
613
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100614 if (STAT_RECORD) {
615 int err, fd = perf_data_file__fd(&perf_stat.file);
616
Jiri Olsa664c98d2015-11-05 15:40:50 +0100617 if (is_pipe) {
618 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
619 } else {
620 err = perf_session__write_header(perf_stat.session, evsel_list,
621 fd, false);
622 }
623
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100624 if (err < 0)
625 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100626
Jiri Olsa664c98d2015-11-05 15:40:50 +0100627 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100628 if (err < 0)
629 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100630 }
631
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200632 /*
633 * Enable counters and exec the command:
634 */
635 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100636 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200637
Liming Wang60666c62009-12-31 16:05:50 +0800638 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900639 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100640 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900641
Stephane Eranian13370a92013-01-29 12:47:44 +0100642 if (interval) {
643 while (!waitpid(child_pid, &status, WNOHANG)) {
644 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200645 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100646 }
647 }
Liming Wang60666c62009-12-31 16:05:50 +0800648 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300649
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300650 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300651 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300652 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300653 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300654 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300655
Andi Kleen33e49ea2011-09-15 14:31:40 -0700656 if (WIFSIGNALED(status))
657 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800658 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100659 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100660 while (!done) {
661 nanosleep(&ts, NULL);
662 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200663 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100664 }
Liming Wang60666c62009-12-31 16:05:50 +0800665 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200666
Mark Rutland3df33ef2016-08-09 14:04:29 +0100667 disable_counters();
668
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200669 t1 = rdclock();
670
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200671 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200672
Mark Rutland3df33ef2016-08-09 14:04:29 +0100673 /*
674 * Closing a group leader splits the group, and as we only disable
675 * group leaders, results in remaining events becoming enabled. To
676 * avoid arbitrary skew, we must read all counters before closing any
677 * group leaders.
678 */
679 read_counters();
680 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200681
Ingo Molnar42202dd2009-06-13 14:57:28 +0200682 return WEXITSTATUS(status);
683}
684
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300685static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200686{
687 int ret;
688
689 if (pre_cmd) {
690 ret = system(pre_cmd);
691 if (ret)
692 return ret;
693 }
694
695 if (sync_run)
696 sync();
697
698 ret = __run_perf_stat(argc, argv);
699 if (ret)
700 return ret;
701
702 if (post_cmd) {
703 ret = system(post_cmd);
704 if (ret)
705 return ret;
706 }
707
708 return ret;
709}
710
Andi Kleend73515c2015-03-11 07:16:27 -0700711static void print_running(u64 run, u64 ena)
712{
713 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200714 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700715 csv_sep,
716 run,
717 csv_sep,
718 ena ? 100.0 * run / ena : 100.0);
719 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200720 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700721 }
722}
723
Ingo Molnarf99844c2011-04-27 05:35:39 +0200724static void print_noise_pct(double total, double avg)
725{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800726 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200727
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700728 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200729 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600730 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200731 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200732}
733
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200734static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200735{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200736 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200737
Peter Zijlstra849abde2009-09-04 18:23:38 +0200738 if (run_count == 1)
739 return;
740
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200741 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200742 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200743}
744
Stephane Eranian12c08a92013-02-14 13:57:29 +0100745static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200746{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200747 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100748 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200749 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100750 cpu_map__id_to_socket(id),
751 csv_output ? 0 : -8,
752 cpu_map__id_to_cpu(id),
753 csv_sep,
754 csv_output ? 0 : 4,
755 nr,
756 csv_sep);
757 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100758 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200759 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100760 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100761 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100762 csv_sep,
763 csv_output ? 0 : 4,
764 nr,
765 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100766 break;
767 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200768 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200769 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100770 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100771 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200772 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200773 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200774 csv_output ? 0 : 16,
775 thread_map__comm(evsel->threads, id),
776 csv_output ? 0 : -8,
777 thread_map__pid(evsel->threads, id),
778 csv_sep);
779 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100780 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200781 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100782 default:
783 break;
784 }
785}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200786
Andi Kleen140aead2016-01-30 09:06:49 -0800787struct outstate {
788 FILE *fh;
789 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800790 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800791 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800792 int id, nr;
793 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800794};
795
796#define METRIC_LEN 35
797
798static void new_line_std(void *ctx)
799{
800 struct outstate *os = ctx;
801
802 os->newline = true;
803}
804
805static void do_new_line_std(struct outstate *os)
806{
807 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800808 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800809 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800810 if (stat_config.aggr_mode == AGGR_NONE)
811 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800812 fprintf(os->fh, " ");
813}
814
815static void print_metric_std(void *ctx, const char *color, const char *fmt,
816 const char *unit, double val)
817{
818 struct outstate *os = ctx;
819 FILE *out = os->fh;
820 int n;
821 bool newline = os->newline;
822
823 os->newline = false;
824
825 if (unit == NULL || fmt == NULL) {
826 fprintf(out, "%-*s", METRIC_LEN, "");
827 return;
828 }
829
830 if (newline)
831 do_new_line_std(os);
832
833 n = fprintf(out, " # ");
834 if (color)
835 n += color_fprintf(out, color, fmt, val);
836 else
837 n += fprintf(out, fmt, val);
838 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
839}
840
Andi Kleen92a61f62016-02-29 14:36:21 -0800841static void new_line_csv(void *ctx)
842{
843 struct outstate *os = ctx;
844 int i;
845
846 fputc('\n', os->fh);
847 if (os->prefix)
848 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800849 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800850 for (i = 0; i < os->nfields; i++)
851 fputs(csv_sep, os->fh);
852}
853
854static void print_metric_csv(void *ctx,
855 const char *color __maybe_unused,
856 const char *fmt, const char *unit, double val)
857{
858 struct outstate *os = ctx;
859 FILE *out = os->fh;
860 char buf[64], *vals, *ends;
861
862 if (unit == NULL || fmt == NULL) {
863 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
864 return;
865 }
866 snprintf(buf, sizeof(buf), fmt, val);
867 vals = buf;
868 while (isspace(*vals))
869 vals++;
870 ends = vals;
871 while (isdigit(*ends) || *ends == '.')
872 ends++;
873 *ends = 0;
874 while (isspace(*unit))
875 unit++;
876 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
877}
878
Andi Kleen54b50912016-03-03 15:57:36 -0800879#define METRIC_ONLY_LEN 20
880
881/* Filter out some columns that don't work well in metrics only mode */
882
883static bool valid_only_metric(const char *unit)
884{
885 if (!unit)
886 return false;
887 if (strstr(unit, "/sec") ||
888 strstr(unit, "hz") ||
889 strstr(unit, "Hz") ||
890 strstr(unit, "CPUs utilized"))
891 return false;
892 return true;
893}
894
895static const char *fixunit(char *buf, struct perf_evsel *evsel,
896 const char *unit)
897{
898 if (!strncmp(unit, "of all", 6)) {
899 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
900 unit);
901 return buf;
902 }
903 return unit;
904}
905
906static void print_metric_only(void *ctx, const char *color, const char *fmt,
907 const char *unit, double val)
908{
909 struct outstate *os = ctx;
910 FILE *out = os->fh;
911 int n;
912 char buf[1024];
913 unsigned mlen = METRIC_ONLY_LEN;
914
915 if (!valid_only_metric(unit))
916 return;
917 unit = fixunit(buf, os->evsel, unit);
918 if (color)
919 n = color_fprintf(out, color, fmt, val);
920 else
921 n = fprintf(out, fmt, val);
922 if (n > METRIC_ONLY_LEN)
923 n = METRIC_ONLY_LEN;
924 if (mlen < strlen(unit))
925 mlen = strlen(unit) + 1;
926 fprintf(out, "%*s", mlen - n, "");
927}
928
929static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
930 const char *fmt,
931 const char *unit, double val)
932{
933 struct outstate *os = ctx;
934 FILE *out = os->fh;
935 char buf[64], *vals, *ends;
936 char tbuf[1024];
937
938 if (!valid_only_metric(unit))
939 return;
940 unit = fixunit(tbuf, os->evsel, unit);
941 snprintf(buf, sizeof buf, fmt, val);
942 vals = buf;
943 while (isspace(*vals))
944 vals++;
945 ends = vals;
946 while (isdigit(*ends) || *ends == '.')
947 ends++;
948 *ends = 0;
949 fprintf(out, "%s%s", vals, csv_sep);
950}
951
952static void new_line_metric(void *ctx __maybe_unused)
953{
954}
955
956static void print_metric_header(void *ctx, const char *color __maybe_unused,
957 const char *fmt __maybe_unused,
958 const char *unit, double val __maybe_unused)
959{
960 struct outstate *os = ctx;
961 char tbuf[1024];
962
963 if (!valid_only_metric(unit))
964 return;
965 unit = fixunit(tbuf, os->evsel, unit);
966 if (csv_output)
967 fprintf(os->fh, "%s%s", unit, csv_sep);
968 else
969 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
970}
971
Andi Kleenda88c7f2014-09-24 13:50:46 -0700972static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100973{
Jiri Olsa58215222015-07-21 14:31:24 +0200974 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300975 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +0100976 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600977 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100978
Stephane Eranian410136f2013-11-12 17:58:49 +0100979 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
980 fmt_n = csv_output ? "%s" : "%-25s";
981
Andi Kleenda88c7f2014-09-24 13:50:46 -0700982 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100983
David Ahern4bbe5a62013-09-28 14:28:00 -0600984 scnprintf(name, sizeof(name), "%s%s",
985 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +0100986
987 fprintf(output, fmt_v, msecs, csv_sep);
988
989 if (csv_output)
990 fprintf(output, "%s%s", evsel->unit, csv_sep);
991 else
992 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
993
994 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200995
Stephane Eranian023695d2011-02-14 11:20:01 +0200996 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200997 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200998}
999
Andi Kleen44d49a62016-02-29 14:36:22 -08001000static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1001{
1002 int i;
1003
1004 if (!aggr_get_id)
1005 return 0;
1006
1007 if (stat_config.aggr_mode == AGGR_NONE)
1008 return id;
1009
1010 if (stat_config.aggr_mode == AGGR_GLOBAL)
1011 return 0;
1012
1013 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1014 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1015
1016 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1017 return cpu2;
1018 }
1019 return 0;
1020}
1021
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001022static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1023{
Jiri Olsa58215222015-07-21 14:31:24 +02001024 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001025 double sc = evsel->scale;
1026 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001027
1028 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001029 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001030 } else {
1031 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001032 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001033 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001034 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001035 }
1036
1037 aggr_printout(evsel, id, nr);
1038
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001039 fprintf(output, fmt, avg, csv_sep);
1040
1041 if (evsel->unit)
1042 fprintf(output, "%-*s%s",
1043 csv_output ? 0 : unit_width,
1044 evsel->unit, csv_sep);
1045
1046 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1047
1048 if (evsel->cgrp)
1049 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001050}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001051
Andi Kleenf9483392016-01-30 09:06:50 -08001052static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Andi Kleencb110f42016-01-30 09:06:51 -08001053 char *prefix, u64 run, u64 ena, double noise)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001054{
Andi Kleen140aead2016-01-30 09:06:49 -08001055 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001056 struct outstate os = {
1057 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001058 .prefix = prefix ? prefix : "",
1059 .id = id,
1060 .nr = nr,
1061 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001062 };
Andi Kleen140aead2016-01-30 09:06:49 -08001063 print_metric_t pm = print_metric_std;
1064 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001065
Andi Kleen54b50912016-03-03 15:57:36 -08001066 if (metric_only) {
1067 nl = new_line_metric;
1068 if (csv_output)
1069 pm = print_metric_only_csv;
1070 else
1071 pm = print_metric_only;
1072 } else
1073 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001074
Andi Kleen54b50912016-03-03 15:57:36 -08001075 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001076 static int aggr_fields[] = {
1077 [AGGR_GLOBAL] = 0,
1078 [AGGR_THREAD] = 1,
1079 [AGGR_NONE] = 1,
1080 [AGGR_SOCKET] = 2,
1081 [AGGR_CORE] = 2,
1082 };
1083
1084 pm = print_metric_csv;
1085 nl = new_line_csv;
1086 os.nfields = 3;
1087 os.nfields += aggr_fields[stat_config.aggr_mode];
1088 if (counter->cgrp)
1089 os.nfields++;
1090 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001091 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001092 if (metric_only) {
1093 pm(&os, NULL, "", "", 0);
1094 return;
1095 }
Andi Kleencb110f42016-01-30 09:06:51 -08001096 aggr_printout(counter, id, nr);
1097
1098 fprintf(stat_config.output, "%*s%s",
1099 csv_output ? 0 : 18,
1100 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1101 csv_sep);
1102
1103 fprintf(stat_config.output, "%-*s%s",
1104 csv_output ? 0 : unit_width,
1105 counter->unit, csv_sep);
1106
1107 fprintf(stat_config.output, "%*s",
1108 csv_output ? 0 : -25,
1109 perf_evsel__name(counter));
1110
1111 if (counter->cgrp)
1112 fprintf(stat_config.output, "%s%s",
1113 csv_sep, counter->cgrp->name);
1114
Andi Kleen92a61f62016-02-29 14:36:21 -08001115 if (!csv_output)
1116 pm(&os, NULL, NULL, "", 0);
1117 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001118 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001119 if (csv_output)
1120 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001121 return;
1122 }
1123
Andi Kleen54b50912016-03-03 15:57:36 -08001124 if (metric_only)
1125 /* nothing */;
1126 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001127 nsec_printout(id, nr, counter, uval);
1128 else
1129 abs_printout(id, nr, counter, uval);
1130
Andi Kleen140aead2016-01-30 09:06:49 -08001131 out.print_metric = pm;
1132 out.new_line = nl;
1133 out.ctx = &os;
1134
Andi Kleen54b50912016-03-03 15:57:36 -08001135 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001136 print_noise(counter, noise);
1137 print_running(run, ena);
1138 }
1139
1140 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001141 first_shadow_cpu(counter, id),
Andi Kleen140aead2016-01-30 09:06:49 -08001142 &out);
Andi Kleen54b50912016-03-03 15:57:36 -08001143 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001144 print_noise(counter, noise);
1145 print_running(run, ena);
1146 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001147}
1148
Andi Kleen44d49a62016-02-29 14:36:22 -08001149static void aggr_update_shadow(void)
1150{
1151 int cpu, s2, id, s;
1152 u64 val;
1153 struct perf_evsel *counter;
1154
1155 for (s = 0; s < aggr_map->nr; s++) {
1156 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001157 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001158 val = 0;
1159 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1160 s2 = aggr_get_id(evsel_list->cpus, cpu);
1161 if (s2 != id)
1162 continue;
1163 val += perf_counts(counter->counts, cpu, 0)->val;
1164 }
1165 val = val * counter->scale;
1166 perf_stat__update_shadow_stats(counter, &val,
1167 first_shadow_cpu(counter, id));
1168 }
1169 }
1170}
1171
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001172static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001173{
Jiri Olsa58215222015-07-21 14:31:24 +02001174 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001175 struct perf_evsel *counter;
Kan Liang601083c2015-07-02 03:08:43 -04001176 int cpu, s, s2, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001177 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001178 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001179 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001180
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001181 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001182 return;
1183
Andi Kleen44d49a62016-02-29 14:36:22 -08001184 aggr_update_shadow();
1185
Andi Kleen54b50912016-03-03 15:57:36 -08001186 /*
1187 * With metric_only everything is on a single line.
1188 * Without each counter has its own line.
1189 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001190 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleen54b50912016-03-03 15:57:36 -08001191 if (prefix && metric_only)
1192 fprintf(output, "%s", prefix);
1193
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001194 id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001195 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001196 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001197 val = ena = run = 0;
1198 nr = 0;
1199 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Kan Liang601083c2015-07-02 03:08:43 -04001200 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001201 if (s2 != id)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001202 continue;
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001203 val += perf_counts(counter->counts, cpu, 0)->val;
1204 ena += perf_counts(counter->counts, cpu, 0)->ena;
1205 run += perf_counts(counter->counts, cpu, 0)->run;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001206 nr++;
1207 }
Andi Kleen54b50912016-03-03 15:57:36 -08001208 if (first && metric_only) {
1209 first = false;
1210 aggr_printout(counter, id, nr);
1211 }
1212 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001213 fprintf(output, "%s", prefix);
1214
Stephane Eranian410136f2013-11-12 17:58:49 +01001215 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001216 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
Andi Kleen54b50912016-03-03 15:57:36 -08001217 if (!metric_only)
1218 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001219 }
Andi Kleen54b50912016-03-03 15:57:36 -08001220 if (metric_only)
1221 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001222 }
1223}
1224
Jiri Olsa32b8af82015-06-26 11:29:27 +02001225static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1226{
Jiri Olsa58215222015-07-21 14:31:24 +02001227 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001228 int nthreads = thread_map__nr(counter->threads);
1229 int ncpus = cpu_map__nr(counter->cpus);
1230 int cpu, thread;
1231 double uval;
1232
1233 for (thread = 0; thread < nthreads; thread++) {
1234 u64 ena = 0, run = 0, val = 0;
1235
1236 for (cpu = 0; cpu < ncpus; cpu++) {
1237 val += perf_counts(counter->counts, cpu, thread)->val;
1238 ena += perf_counts(counter->counts, cpu, thread)->ena;
1239 run += perf_counts(counter->counts, cpu, thread)->run;
1240 }
1241
1242 if (prefix)
1243 fprintf(output, "%s", prefix);
1244
1245 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001246 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001247 fputc('\n', output);
1248 }
1249}
1250
Ingo Molnar42202dd2009-06-13 14:57:28 +02001251/*
1252 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001253 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001254 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001255static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001256{
Jiri Olsa58215222015-07-21 14:31:24 +02001257 FILE *output = stat_config.output;
Jiri Olsa581cc8a2015-10-16 12:41:03 +02001258 struct perf_stat_evsel *ps = counter->priv;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001259 double avg = avg_stats(&ps->res_stats[0]);
Stephane Eranian410136f2013-11-12 17:58:49 +01001260 double uval;
Andi Kleend73515c2015-03-11 07:16:27 -07001261 double avg_enabled, avg_running;
1262
1263 avg_enabled = avg_stats(&ps->res_stats[1]);
1264 avg_running = avg_stats(&ps->res_stats[2]);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001265
Andi Kleen54b50912016-03-03 15:57:36 -08001266 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001267 fprintf(output, "%s", prefix);
1268
Stephane Eranian410136f2013-11-12 17:58:49 +01001269 uval = avg * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001270 printout(-1, 0, counter, uval, prefix, avg_running, avg_enabled, avg);
Andi Kleen54b50912016-03-03 15:57:36 -08001271 if (!metric_only)
1272 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001273}
1274
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001275/*
1276 * Print out the results of a single counter:
1277 * does not use aggregated count in system-wide
1278 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001279static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001280{
Jiri Olsa58215222015-07-21 14:31:24 +02001281 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001282 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001283 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001284 int cpu;
1285
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001286 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001287 val = perf_counts(counter->counts, cpu, 0)->val;
1288 ena = perf_counts(counter->counts, cpu, 0)->ena;
1289 run = perf_counts(counter->counts, cpu, 0)->run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001290
1291 if (prefix)
1292 fprintf(output, "%s", prefix);
1293
Stephane Eranian410136f2013-11-12 17:58:49 +01001294 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001295 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001296
Stephane Eranian4aa90152011-08-15 22:22:33 +02001297 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001298 }
1299}
1300
Andi Kleen206cab62016-03-03 15:57:37 -08001301static void print_no_aggr_metric(char *prefix)
1302{
1303 int cpu;
1304 int nrcpus = 0;
1305 struct perf_evsel *counter;
1306 u64 ena, run, val;
1307 double uval;
1308
1309 nrcpus = evsel_list->cpus->nr;
1310 for (cpu = 0; cpu < nrcpus; cpu++) {
1311 bool first = true;
1312
1313 if (prefix)
1314 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001315 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen206cab62016-03-03 15:57:37 -08001316 if (first) {
1317 aggr_printout(counter, cpu, 0);
1318 first = false;
1319 }
1320 val = perf_counts(counter->counts, cpu, 0)->val;
1321 ena = perf_counts(counter->counts, cpu, 0)->ena;
1322 run = perf_counts(counter->counts, cpu, 0)->run;
1323
1324 uval = val * counter->scale;
1325 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1326 }
1327 fputc('\n', stat_config.output);
1328 }
1329}
1330
Andi Kleen54b50912016-03-03 15:57:36 -08001331static int aggr_header_lens[] = {
1332 [AGGR_CORE] = 18,
1333 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001334 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001335 [AGGR_THREAD] = 24,
1336 [AGGR_GLOBAL] = 0,
1337};
1338
Andi Kleenc51fd632016-05-24 12:52:39 -07001339static const char *aggr_header_csv[] = {
1340 [AGGR_CORE] = "core,cpus,",
1341 [AGGR_SOCKET] = "socket,cpus",
1342 [AGGR_NONE] = "cpu,",
1343 [AGGR_THREAD] = "comm-pid,",
1344 [AGGR_GLOBAL] = ""
1345};
1346
Andi Kleen41c8ca22016-05-24 12:52:38 -07001347static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001348{
1349 struct perf_stat_output_ctx out;
1350 struct perf_evsel *counter;
1351 struct outstate os = {
1352 .fh = stat_config.output
1353 };
1354
1355 if (prefix)
1356 fprintf(stat_config.output, "%s", prefix);
1357
Andi Kleen41c8ca22016-05-24 12:52:38 -07001358 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001359 fprintf(stat_config.output, "%*s",
1360 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001361 if (csv_output) {
1362 if (stat_config.interval)
1363 fputs("time,", stat_config.output);
1364 fputs(aggr_header_csv[stat_config.aggr_mode],
1365 stat_config.output);
1366 }
Andi Kleen54b50912016-03-03 15:57:36 -08001367
1368 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001369 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen54b50912016-03-03 15:57:36 -08001370 os.evsel = counter;
1371 out.ctx = &os;
1372 out.print_metric = print_metric_header;
1373 out.new_line = new_line_metric;
1374 os.evsel = counter;
1375 perf_stat__print_shadow_stats(counter, 0,
1376 0,
1377 &out);
1378 }
1379 fputc('\n', stat_config.output);
1380}
1381
Jiri Olsad4f63a42015-06-26 11:29:26 +02001382static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001383{
Jiri Olsa58215222015-07-21 14:31:24 +02001384 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001385 static int num_print_interval;
1386
1387 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1388
Andi Kleen41c8ca22016-05-24 12:52:38 -07001389 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001390 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001391 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001392 fprintf(output, "# time socket cpus");
1393 if (!metric_only)
1394 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001395 break;
1396 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001397 fprintf(output, "# time core cpus");
1398 if (!metric_only)
1399 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001400 break;
1401 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001402 fprintf(output, "# time CPU");
1403 if (!metric_only)
1404 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001405 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001406 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001407 fprintf(output, "# time comm-pid");
1408 if (!metric_only)
1409 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001410 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001411 case AGGR_GLOBAL:
1412 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001413 fprintf(output, "# time");
1414 if (!metric_only)
1415 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001416 case AGGR_UNSET:
1417 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001418 }
1419 }
1420
Andi Kleen41c8ca22016-05-24 12:52:38 -07001421 if (num_print_interval == 0 && metric_only)
1422 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001423 if (++num_print_interval == 25)
1424 num_print_interval = 0;
1425}
1426
1427static void print_header(int argc, const char **argv)
1428{
Jiri Olsa58215222015-07-21 14:31:24 +02001429 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001430 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001431
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001432 fflush(stdout);
1433
Stephane Eraniand7470b62010-12-01 18:49:05 +02001434 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001435 fprintf(output, "\n");
1436 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001437 if (target.system_wide)
1438 fprintf(output, "\'system wide");
1439 else if (target.cpu_list)
1440 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001441 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001442 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1443 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001444 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001445 } else if (target.pid)
1446 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001447 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001448 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001449
Stephane Eranian4aa90152011-08-15 22:22:33 +02001450 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001451 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001452 fprintf(output, " (%d runs)", run_count);
1453 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001454 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001455}
1456
1457static void print_footer(void)
1458{
Jiri Olsa58215222015-07-21 14:31:24 +02001459 FILE *output = stat_config.output;
1460
Jiri Olsad4f63a42015-06-26 11:29:26 +02001461 if (!null_run)
1462 fprintf(output, "\n");
1463 fprintf(output, " %17.9f seconds time elapsed",
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001464 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001465 if (run_count > 1) {
1466 fprintf(output, " ");
1467 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1468 avg_stats(&walltime_nsecs_stats));
1469 }
1470 fprintf(output, "\n\n");
1471}
1472
1473static void print_counters(struct timespec *ts, int argc, const char **argv)
1474{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001475 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001476 struct perf_evsel *counter;
1477 char buf[64], *prefix = NULL;
1478
Jiri Olsa664c98d2015-11-05 15:40:50 +01001479 /* Do not print anything if we record to the pipe. */
1480 if (STAT_RECORD && perf_stat.file.is_pipe)
1481 return;
1482
Jiri Olsad4f63a42015-06-26 11:29:26 +02001483 if (interval)
1484 print_interval(prefix = buf, ts);
1485 else
1486 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001487
Andi Kleen54b50912016-03-03 15:57:36 -08001488 if (metric_only) {
1489 static int num_print_iv;
1490
Andi Kleen41c8ca22016-05-24 12:52:38 -07001491 if (num_print_iv == 0 && !interval)
1492 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001493 if (num_print_iv++ == 25)
1494 num_print_iv = 0;
1495 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1496 fprintf(stat_config.output, "%s", prefix);
1497 }
1498
Jiri Olsa421a50f2015-07-21 14:31:22 +02001499 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001500 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001501 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001502 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001503 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001504 case AGGR_THREAD:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001505 evlist__for_each_entry(evsel_list, counter)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001506 print_aggr_thread(counter, prefix);
1507 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001508 case AGGR_GLOBAL:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001509 evlist__for_each_entry(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001510 print_counter_aggr(counter, prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001511 if (metric_only)
1512 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001513 break;
1514 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001515 if (metric_only)
1516 print_no_aggr_metric(prefix);
1517 else {
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001518 evlist__for_each_entry(evsel_list, counter)
Andi Kleen206cab62016-03-03 15:57:37 -08001519 print_counter(counter, prefix);
1520 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001521 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001522 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001523 default:
1524 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001525 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001526
Jiri Olsad4f63a42015-06-26 11:29:26 +02001527 if (!interval && !csv_output)
1528 print_footer();
1529
Jiri Olsa58215222015-07-21 14:31:24 +02001530 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001531}
1532
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001533static volatile int signr = -1;
1534
Ingo Molnar52425192009-05-26 09:17:18 +02001535static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001536{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001537 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001538 done = 1;
1539
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001540 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001541 /*
1542 * render child_pid harmless
1543 * won't send SIGTERM to a random
1544 * process in case of race condition
1545 * and fast PID recycling
1546 */
1547 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001548}
1549
1550static void sig_atexit(void)
1551{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001552 sigset_t set, oset;
1553
1554 /*
1555 * avoid race condition with SIGCHLD handler
1556 * in skip_signal() which is modifying child_pid
1557 * goal is to avoid send SIGTERM to a random
1558 * process
1559 */
1560 sigemptyset(&set);
1561 sigaddset(&set, SIGCHLD);
1562 sigprocmask(SIG_BLOCK, &set, &oset);
1563
Chris Wilson933da832009-10-04 01:35:01 +01001564 if (child_pid != -1)
1565 kill(child_pid, SIGTERM);
1566
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001567 sigprocmask(SIG_SETMASK, &oset, NULL);
1568
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001569 if (signr == -1)
1570 return;
1571
1572 signal(signr, SIG_DFL);
1573 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001574}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001575
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001576static int stat__set_big_num(const struct option *opt __maybe_unused,
1577 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001578{
1579 big_num_opt = unset ? 0 : 1;
1580 return 0;
1581}
1582
Andi Kleen44b1e602016-05-30 12:49:42 -03001583static int enable_metric_only(const struct option *opt __maybe_unused,
1584 const char *s __maybe_unused, int unset)
1585{
1586 force_metric_only = true;
1587 metric_only = !unset;
1588 return 0;
1589}
1590
Jiri Olsae0547312015-11-05 15:40:45 +01001591static const struct option stat_options[] = {
1592 OPT_BOOLEAN('T', "transaction", &transaction_run,
1593 "hardware transaction statistics"),
1594 OPT_CALLBACK('e', "event", &evsel_list, "event",
1595 "event selector. use 'perf list' to list available events",
1596 parse_events_option),
1597 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1598 "event filter", parse_filter),
1599 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1600 "child tasks do not inherit counters"),
1601 OPT_STRING('p', "pid", &target.pid, "pid",
1602 "stat events on existing process id"),
1603 OPT_STRING('t', "tid", &target.tid, "tid",
1604 "stat events on existing thread id"),
1605 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1606 "system-wide collection from all CPUs"),
1607 OPT_BOOLEAN('g', "group", &group,
1608 "put the counters into a counter group"),
1609 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1610 OPT_INCR('v', "verbose", &verbose,
1611 "be more verbose (show counter open errors, etc)"),
1612 OPT_INTEGER('r', "repeat", &run_count,
1613 "repeat command and print average + stddev (max: 100, forever: 0)"),
1614 OPT_BOOLEAN('n', "null", &null_run,
1615 "null run - dont start any counters"),
1616 OPT_INCR('d', "detailed", &detailed_run,
1617 "detailed run - start a lot of events"),
1618 OPT_BOOLEAN('S', "sync", &sync_run,
1619 "call sync() before starting a run"),
1620 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1621 "print large numbers with thousands\' separators",
1622 stat__set_big_num),
1623 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1624 "list of cpus to monitor in system-wide"),
1625 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1626 "disable CPU count aggregation", AGGR_NONE),
1627 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1628 "print counts with custom separator"),
1629 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1630 "monitor event in cgroup name only", parse_cgroups),
1631 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1632 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1633 OPT_INTEGER(0, "log-fd", &output_fd,
1634 "log output to fd, instead of stderr"),
1635 OPT_STRING(0, "pre", &pre_cmd, "command",
1636 "command to run prior to the measured command"),
1637 OPT_STRING(0, "post", &post_cmd, "command",
1638 "command to run after to the measured command"),
1639 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1640 "print counts at regular interval in ms (>= 10)"),
1641 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1642 "aggregate counts per processor socket", AGGR_SOCKET),
1643 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1644 "aggregate counts per physical processor core", AGGR_CORE),
1645 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1646 "aggregate counts per thread", AGGR_THREAD),
1647 OPT_UINTEGER('D', "delay", &initial_delay,
1648 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001649 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1650 "Only print computed metrics. No raw values", enable_metric_only),
1651 OPT_BOOLEAN(0, "topdown", &topdown_run,
1652 "measure topdown level 1 statistics"),
Jiri Olsae0547312015-11-05 15:40:45 +01001653 OPT_END()
1654};
1655
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001656static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1657{
1658 return cpu_map__get_socket(map, cpu, NULL);
1659}
1660
1661static int perf_stat__get_core(struct cpu_map *map, int cpu)
1662{
1663 return cpu_map__get_core(map, cpu, NULL);
1664}
1665
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001666static int cpu_map__get_max(struct cpu_map *map)
1667{
1668 int i, max = -1;
1669
1670 for (i = 0; i < map->nr; i++) {
1671 if (map->map[i] > max)
1672 max = map->map[i];
1673 }
1674
1675 return max;
1676}
1677
1678static struct cpu_map *cpus_aggr_map;
1679
1680static int perf_stat__get_aggr(aggr_get_id_t get_id, 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 (cpus_aggr_map->map[cpu] == -1)
1690 cpus_aggr_map->map[cpu] = get_id(map, idx);
1691
1692 return cpus_aggr_map->map[cpu];
1693}
1694
1695static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1696{
1697 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1698}
1699
1700static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1701{
1702 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1703}
1704
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001705static int perf_stat_init_aggr_mode(void)
1706{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001707 int nr;
1708
Jiri Olsa421a50f2015-07-21 14:31:22 +02001709 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001710 case AGGR_SOCKET:
1711 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1712 perror("cannot build socket map");
1713 return -1;
1714 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001715 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001716 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001717 case AGGR_CORE:
1718 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1719 perror("cannot build core map");
1720 return -1;
1721 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001722 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001723 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001724 case AGGR_NONE:
1725 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001726 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001727 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001728 default:
1729 break;
1730 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001731
1732 /*
1733 * The evsel_list->cpus is the base we operate on,
1734 * taking the highest cpu number to be the size of
1735 * the aggregation translate cpumap.
1736 */
1737 nr = cpu_map__get_max(evsel_list->cpus);
1738 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1739 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001740}
1741
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001742static void perf_stat__exit_aggr_mode(void)
1743{
1744 cpu_map__put(aggr_map);
1745 cpu_map__put(cpus_aggr_map);
1746 aggr_map = NULL;
1747 cpus_aggr_map = NULL;
1748}
1749
Jiri Olsa68d702f2015-11-05 15:40:58 +01001750static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1751{
1752 int cpu;
1753
1754 if (idx > map->nr)
1755 return -1;
1756
1757 cpu = map->map[idx];
1758
1759 if (cpu >= env->nr_cpus_online)
1760 return -1;
1761
1762 return cpu;
1763}
1764
1765static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1766{
1767 struct perf_env *env = data;
1768 int cpu = perf_env__get_cpu(env, map, idx);
1769
1770 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1771}
1772
1773static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1774{
1775 struct perf_env *env = data;
1776 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1777
1778 if (cpu != -1) {
1779 int socket_id = env->cpu[cpu].socket_id;
1780
1781 /*
1782 * Encode socket in upper 16 bits
1783 * core_id is relative to socket, and
1784 * we need a global id. So we combine
1785 * socket + core id.
1786 */
1787 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1788 }
1789
1790 return core;
1791}
1792
1793static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1794 struct cpu_map **sockp)
1795{
1796 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1797}
1798
1799static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1800 struct cpu_map **corep)
1801{
1802 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1803}
1804
1805static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1806{
1807 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1808}
1809
1810static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1811{
1812 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1813}
1814
1815static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1816{
1817 struct perf_env *env = &st->session->header.env;
1818
1819 switch (stat_config.aggr_mode) {
1820 case AGGR_SOCKET:
1821 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1822 perror("cannot build socket map");
1823 return -1;
1824 }
1825 aggr_get_id = perf_stat__get_socket_file;
1826 break;
1827 case AGGR_CORE:
1828 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1829 perror("cannot build core map");
1830 return -1;
1831 }
1832 aggr_get_id = perf_stat__get_core_file;
1833 break;
1834 case AGGR_NONE:
1835 case AGGR_GLOBAL:
1836 case AGGR_THREAD:
1837 case AGGR_UNSET:
1838 default:
1839 break;
1840 }
1841
1842 return 0;
1843}
1844
Andi Kleen44b1e602016-05-30 12:49:42 -03001845static int topdown_filter_events(const char **attr, char **str, bool use_group)
1846{
1847 int off = 0;
1848 int i;
1849 int len = 0;
1850 char *s;
1851
1852 for (i = 0; attr[i]; i++) {
1853 if (pmu_have_event("cpu", attr[i])) {
1854 len += strlen(attr[i]) + 1;
1855 attr[i - off] = attr[i];
1856 } else
1857 off++;
1858 }
1859 attr[i - off] = NULL;
1860
1861 *str = malloc(len + 1 + 2);
1862 if (!*str)
1863 return -1;
1864 s = *str;
1865 if (i - off == 0) {
1866 *s = 0;
1867 return 0;
1868 }
1869 if (use_group)
1870 *s++ = '{';
1871 for (i = 0; attr[i]; i++) {
1872 strcpy(s, attr[i]);
1873 s += strlen(s);
1874 *s++ = ',';
1875 }
1876 if (use_group) {
1877 s[-1] = '}';
1878 *s = 0;
1879 } else
1880 s[-1] = 0;
1881 return 0;
1882}
1883
1884__weak bool arch_topdown_check_group(bool *warn)
1885{
1886 *warn = false;
1887 return false;
1888}
1889
1890__weak void arch_topdown_group_warn(void)
1891{
1892}
1893
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001894/*
1895 * Add default attributes, if there were no attributes specified or
1896 * if -d/--detailed, -d -d or -d -d -d is used:
1897 */
1898static int add_default_attributes(void)
1899{
Andi Kleen44b1e602016-05-30 12:49:42 -03001900 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08001901 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001902
1903 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1904 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1905 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1906 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1907
1908 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08001909};
1910 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001911 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001912};
1913 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001914 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001915};
1916 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001917 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1918 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1919 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1920
1921};
1922
1923/*
1924 * Detailed stats (-d), covering the L1 and last level data caches:
1925 */
1926 struct perf_event_attr detailed_attrs[] = {
1927
1928 { .type = PERF_TYPE_HW_CACHE,
1929 .config =
1930 PERF_COUNT_HW_CACHE_L1D << 0 |
1931 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1932 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1933
1934 { .type = PERF_TYPE_HW_CACHE,
1935 .config =
1936 PERF_COUNT_HW_CACHE_L1D << 0 |
1937 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1938 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1939
1940 { .type = PERF_TYPE_HW_CACHE,
1941 .config =
1942 PERF_COUNT_HW_CACHE_LL << 0 |
1943 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1944 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1945
1946 { .type = PERF_TYPE_HW_CACHE,
1947 .config =
1948 PERF_COUNT_HW_CACHE_LL << 0 |
1949 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1950 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1951};
1952
1953/*
1954 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1955 */
1956 struct perf_event_attr very_detailed_attrs[] = {
1957
1958 { .type = PERF_TYPE_HW_CACHE,
1959 .config =
1960 PERF_COUNT_HW_CACHE_L1I << 0 |
1961 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1962 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1963
1964 { .type = PERF_TYPE_HW_CACHE,
1965 .config =
1966 PERF_COUNT_HW_CACHE_L1I << 0 |
1967 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1968 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1969
1970 { .type = PERF_TYPE_HW_CACHE,
1971 .config =
1972 PERF_COUNT_HW_CACHE_DTLB << 0 |
1973 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1974 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1975
1976 { .type = PERF_TYPE_HW_CACHE,
1977 .config =
1978 PERF_COUNT_HW_CACHE_DTLB << 0 |
1979 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1980 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1981
1982 { .type = PERF_TYPE_HW_CACHE,
1983 .config =
1984 PERF_COUNT_HW_CACHE_ITLB << 0 |
1985 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1986 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1987
1988 { .type = PERF_TYPE_HW_CACHE,
1989 .config =
1990 PERF_COUNT_HW_CACHE_ITLB << 0 |
1991 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1992 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1993
1994};
1995
1996/*
1997 * Very, very detailed stats (-d -d -d), adding prefetch events:
1998 */
1999 struct perf_event_attr very_very_detailed_attrs[] = {
2000
2001 { .type = PERF_TYPE_HW_CACHE,
2002 .config =
2003 PERF_COUNT_HW_CACHE_L1D << 0 |
2004 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2005 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2006
2007 { .type = PERF_TYPE_HW_CACHE,
2008 .config =
2009 PERF_COUNT_HW_CACHE_L1D << 0 |
2010 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2011 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2012};
2013
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002014 /* Set attrs if no event is selected and !null_run: */
2015 if (null_run)
2016 return 0;
2017
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002018 if (transaction_run) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002019 if (pmu_have_event("cpu", "cycles-ct") &&
2020 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02002021 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002022 else
Jiri Olsaa4547422015-06-03 16:25:53 +02002023 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2024 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002025 fprintf(stderr, "Cannot set up transaction events\n");
2026 return -1;
2027 }
2028 return 0;
2029 }
2030
Andi Kleen44b1e602016-05-30 12:49:42 -03002031 if (topdown_run) {
2032 char *str = NULL;
2033 bool warn = false;
2034
2035 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2036 stat_config.aggr_mode != AGGR_CORE) {
2037 pr_err("top down event configuration requires --per-core mode\n");
2038 return -1;
2039 }
2040 stat_config.aggr_mode = AGGR_CORE;
2041 if (nr_cgroups || !target__has_cpu(&target)) {
2042 pr_err("top down event configuration requires system-wide mode (-a)\n");
2043 return -1;
2044 }
2045
2046 if (!force_metric_only)
2047 metric_only = true;
2048 if (topdown_filter_events(topdown_attrs, &str,
2049 arch_topdown_check_group(&warn)) < 0) {
2050 pr_err("Out of memory\n");
2051 return -1;
2052 }
2053 if (topdown_attrs[0] && str) {
2054 if (warn)
2055 arch_topdown_group_warn();
2056 err = parse_events(evsel_list, str, NULL);
2057 if (err) {
2058 fprintf(stderr,
2059 "Cannot set up top down events %s: %d\n",
2060 str, err);
2061 free(str);
2062 return -1;
2063 }
2064 } else {
2065 fprintf(stderr, "System does not support topdown\n");
2066 return -1;
2067 }
2068 free(str);
2069 }
2070
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002071 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002072 if (target__has_cpu(&target))
2073 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2074
Andi Kleen9dec4472016-02-26 16:27:56 -08002075 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2076 return -1;
2077 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2078 if (perf_evlist__add_default_attrs(evsel_list,
2079 frontend_attrs) < 0)
2080 return -1;
2081 }
2082 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2083 if (perf_evlist__add_default_attrs(evsel_list,
2084 backend_attrs) < 0)
2085 return -1;
2086 }
2087 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002088 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002089 }
2090
2091 /* Detailed events get appended to the event list: */
2092
2093 if (detailed_run < 1)
2094 return 0;
2095
2096 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002097 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002098 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002099
2100 if (detailed_run < 2)
2101 return 0;
2102
2103 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002104 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002105 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002106
2107 if (detailed_run < 3)
2108 return 0;
2109
2110 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002111 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002112}
2113
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002114static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002115 "perf stat record [<options>]",
2116 NULL,
2117};
2118
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002119static void init_features(struct perf_session *session)
2120{
2121 int feat;
2122
2123 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2124 perf_header__set_feat(&session->header, feat);
2125
2126 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2127 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2128 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2129 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2130}
2131
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002132static int __cmd_record(int argc, const char **argv)
2133{
2134 struct perf_session *session;
2135 struct perf_data_file *file = &perf_stat.file;
2136
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002137 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002138 PARSE_OPT_STOP_AT_NON_OPTION);
2139
2140 if (output_name)
2141 file->path = output_name;
2142
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002143 if (run_count != 1 || forever) {
2144 pr_err("Cannot use -r option with perf stat record.\n");
2145 return -1;
2146 }
2147
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002148 session = perf_session__new(file, false, NULL);
2149 if (session == NULL) {
2150 pr_err("Perf session creation failed.\n");
2151 return -1;
2152 }
2153
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002154 init_features(session);
2155
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002156 session->evlist = evsel_list;
2157 perf_stat.session = session;
2158 perf_stat.record = true;
2159 return argc;
2160}
2161
Jiri Olsaa56f9392015-11-05 15:40:59 +01002162static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2163 union perf_event *event,
2164 struct perf_session *session)
2165{
Andi Kleene3b03b62016-05-05 16:04:03 -07002166 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002167 struct perf_evsel *counter;
2168 struct timespec tsh, *ts = NULL;
2169 const char **argv = session->header.env.cmdline_argv;
2170 int argc = session->header.env.nr_cmdline;
2171
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002172 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002173 perf_stat_process_counter(&stat_config, counter);
2174
Andi Kleene3b03b62016-05-05 16:04:03 -07002175 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2176 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002177
Andi Kleene3b03b62016-05-05 16:04:03 -07002178 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002179 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2180 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002181 ts = &tsh;
2182 }
2183
2184 print_counters(ts, argc, argv);
2185 return 0;
2186}
2187
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002188static
2189int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2190 union perf_event *event,
2191 struct perf_session *session __maybe_unused)
2192{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002193 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2194
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002195 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002196
Jiri Olsa89af4e02015-11-05 15:41:02 +01002197 if (cpu_map__empty(st->cpus)) {
2198 if (st->aggr_mode != AGGR_UNSET)
2199 pr_warning("warning: processing task data, aggregation mode not set\n");
2200 return 0;
2201 }
2202
2203 if (st->aggr_mode != AGGR_UNSET)
2204 stat_config.aggr_mode = st->aggr_mode;
2205
Jiri Olsa68d702f2015-11-05 15:40:58 +01002206 if (perf_stat.file.is_pipe)
2207 perf_stat_init_aggr_mode();
2208 else
2209 perf_stat_init_aggr_mode_file(st);
2210
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002211 return 0;
2212}
2213
Jiri Olsa1975d362015-11-05 15:40:56 +01002214static int set_maps(struct perf_stat *st)
2215{
2216 if (!st->cpus || !st->threads)
2217 return 0;
2218
2219 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2220 return -EINVAL;
2221
2222 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2223
2224 if (perf_evlist__alloc_stats(evsel_list, true))
2225 return -ENOMEM;
2226
2227 st->maps_allocated = true;
2228 return 0;
2229}
2230
2231static
2232int process_thread_map_event(struct perf_tool *tool __maybe_unused,
2233 union perf_event *event,
2234 struct perf_session *session __maybe_unused)
2235{
2236 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2237
2238 if (st->threads) {
2239 pr_warning("Extra thread map event, ignoring.\n");
2240 return 0;
2241 }
2242
2243 st->threads = thread_map__new_event(&event->thread_map);
2244 if (!st->threads)
2245 return -ENOMEM;
2246
2247 return set_maps(st);
2248}
2249
2250static
2251int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2252 union perf_event *event,
2253 struct perf_session *session __maybe_unused)
2254{
2255 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2256 struct cpu_map *cpus;
2257
2258 if (st->cpus) {
2259 pr_warning("Extra cpu map event, ignoring.\n");
2260 return 0;
2261 }
2262
2263 cpus = cpu_map__new_data(&event->cpu_map.data);
2264 if (!cpus)
2265 return -ENOMEM;
2266
2267 st->cpus = cpus;
2268 return set_maps(st);
2269}
2270
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002271static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002272 "perf stat report [<options>]",
2273 NULL,
2274};
2275
2276static struct perf_stat perf_stat = {
2277 .tool = {
2278 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002279 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002280 .thread_map = process_thread_map_event,
2281 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002282 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002283 .stat = perf_event__process_stat_event,
2284 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002285 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002286 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002287};
2288
2289static int __cmd_report(int argc, const char **argv)
2290{
2291 struct perf_session *session;
2292 const struct option options[] = {
2293 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002294 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2295 "aggregate counts per processor socket", AGGR_SOCKET),
2296 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2297 "aggregate counts per physical processor core", AGGR_CORE),
2298 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2299 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002300 OPT_END()
2301 };
2302 struct stat st;
2303 int ret;
2304
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002305 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002306
2307 if (!input_name || !strlen(input_name)) {
2308 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2309 input_name = "-";
2310 else
2311 input_name = "perf.data";
2312 }
2313
2314 perf_stat.file.path = input_name;
2315 perf_stat.file.mode = PERF_DATA_MODE_READ;
2316
2317 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2318 if (session == NULL)
2319 return -1;
2320
2321 perf_stat.session = session;
2322 stat_config.output = stderr;
2323 evsel_list = session->evlist;
2324
2325 ret = perf_session__process_events(session);
2326 if (ret)
2327 return ret;
2328
2329 perf_session__delete(session);
2330 return 0;
2331}
2332
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002333int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02002334{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002335 const char * const stat_usage[] = {
2336 "perf stat [<options>] [<command>]",
2337 NULL
2338 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002339 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002340 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002341 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002342 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002343 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002344
Stephane Eranian5af52b52010-05-18 15:00:01 +02002345 setlocale(LC_ALL, "");
2346
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002347 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002348 if (evsel_list == NULL)
2349 return -ENOMEM;
2350
Wang Nan1669e502016-02-19 11:43:58 +00002351 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002352 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2353 (const char **) stat_usage,
2354 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002355 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002356
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002357 if (csv_sep) {
2358 csv_output = true;
2359 if (!strcmp(csv_sep, "\\t"))
2360 csv_sep = "\t";
2361 } else
2362 csv_sep = DEFAULT_SEPARATOR;
2363
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002364 if (argc && !strncmp(argv[0], "rec", 3)) {
2365 argc = __cmd_record(argc, argv);
2366 if (argc < 0)
2367 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002368 } else if (argc && !strncmp(argv[0], "rep", 3))
2369 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002370
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002371 interval = stat_config.interval;
2372
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002373 /*
2374 * For record command the -o is already taken care of.
2375 */
2376 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002377 output = NULL;
2378
Jim Cromie56f3bae2011-09-07 17:14:00 -06002379 if (output_name && output_fd) {
2380 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002381 parse_options_usage(stat_usage, stat_options, "o", 1);
2382 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002383 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002384 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002385
Andi Kleen54b50912016-03-03 15:57:36 -08002386 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2387 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2388 goto out;
2389 }
2390
Andi Kleen54b50912016-03-03 15:57:36 -08002391 if (metric_only && run_count > 1) {
2392 fprintf(stderr, "--metric-only is not supported with -r\n");
2393 goto out;
2394 }
2395
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002396 if (output_fd < 0) {
2397 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002398 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002399 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002400 }
2401
Stephane Eranian4aa90152011-08-15 22:22:33 +02002402 if (!output) {
2403 struct timespec tm;
2404 mode = append_file ? "a" : "w";
2405
2406 output = fopen(output_name, mode);
2407 if (!output) {
2408 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002409 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002410 }
2411 clock_gettime(CLOCK_REALTIME, &tm);
2412 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002413 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002414 mode = append_file ? "a" : "w";
2415 output = fdopen(output_fd, mode);
2416 if (!output) {
2417 perror("Failed opening logfd");
2418 return -errno;
2419 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002420 }
2421
Jiri Olsa58215222015-07-21 14:31:24 +02002422 stat_config.output = output;
2423
Stephane Eraniand7470b62010-12-01 18:49:05 +02002424 /*
2425 * let the spreadsheet do the pretty-printing
2426 */
2427 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002428 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002429 if (big_num_opt == 1) {
2430 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002431 parse_options_usage(stat_usage, stat_options, "B", 1);
2432 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002433 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002434 } else /* Nope, so disable big number formatting */
2435 big_num = false;
2436 } else if (big_num_opt == 0) /* User passed --no-big-num */
2437 big_num = false;
2438
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002439 if (!argc && target__none(&target))
Jiri Olsae0547312015-11-05 15:40:45 +01002440 usage_with_options(stat_usage, stat_options);
David Ahernac3063b2013-09-30 07:37:37 -06002441
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002442 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002443 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002444 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002445 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002446 } else if (run_count == 0) {
2447 forever = true;
2448 run_count = 1;
2449 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002450
Jiri Olsa421a50f2015-07-21 14:31:22 +02002451 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002452 fprintf(stderr, "The --per-thread option is only available "
2453 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002454 parse_options_usage(NULL, stat_options, "p", 1);
2455 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002456 goto out;
2457 }
2458
2459 /*
2460 * no_aggr, cgroup are for system-wide only
2461 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2462 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002463 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2464 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002465 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002466 fprintf(stderr, "both cgroup and no-aggregation "
2467 "modes only available in system-wide mode\n");
2468
Jiri Olsae0547312015-11-05 15:40:45 +01002469 parse_options_usage(stat_usage, stat_options, "G", 1);
2470 parse_options_usage(NULL, stat_options, "A", 1);
2471 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002472 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002473 }
2474
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002475 if (add_default_attributes())
2476 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002477
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002478 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002479
Namhyung Kim77a6f012012-05-07 14:09:04 +09002480 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002481 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002482 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002483 parse_options_usage(stat_usage, stat_options, "p", 1);
2484 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002485 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002486 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002487 parse_options_usage(stat_usage, stat_options, "C", 1);
2488 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002489 }
2490 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002491 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002492
2493 /*
2494 * Initialize thread_map with comm names,
2495 * so we could print it out on output.
2496 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002497 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02002498 thread_map__read_comms(evsel_list->threads);
2499
Stephane Eranian13370a92013-01-29 12:47:44 +01002500 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002501 if (interval < 10) {
2502 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002503 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002504 goto out;
2505 } else
2506 pr_warning("print interval < 100ms. "
2507 "The overhead percentage could be high in some cases. "
2508 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002509 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002510
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002511 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002512 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002513
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002514 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002515 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002516
Ingo Molnar58d7e992009-05-15 11:03:23 +02002517 /*
2518 * We dont want to block the signals - that would cause
2519 * child tasks to inherit that and Ctrl-C would not work.
2520 * What we want is for Ctrl-C to work in the exec()-ed
2521 * task, but being ignored by perf stat itself:
2522 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002523 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002524 if (!forever)
2525 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002526 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002527 signal(SIGALRM, skip_signal);
2528 signal(SIGABRT, skip_signal);
2529
Ingo Molnar42202dd2009-06-13 14:57:28 +02002530 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002531 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Ingo Molnar42202dd2009-06-13 14:57:28 +02002532 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002533 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2534 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002535
Ingo Molnar42202dd2009-06-13 14:57:28 +02002536 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002537 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002538 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002539 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002540 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002541 }
2542
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002543 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002544 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002545
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002546 if (STAT_RECORD) {
2547 /*
2548 * We synthesize the kernel mmap record just so that older tools
2549 * don't emit warnings about not being able to resolve symbols
2550 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2551 * a saner message about no samples being in the perf.data file.
2552 *
2553 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002554 * in header.c at the moment 'perf stat record' gets introduced, which
2555 * is not really needed once we start adding the stat specific PERF_RECORD_
2556 * records, but the need to suppress the kptr_restrict messages in older
2557 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002558 */
2559 int fd = perf_data_file__fd(&perf_stat.file);
2560 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2561 process_synthesized_event,
2562 &perf_stat.session->machines.host);
2563 if (err) {
2564 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2565 "older tools may produce warnings about this file\n.");
2566 }
2567
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002568 if (!interval) {
2569 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2570 pr_err("failed to write stat round event\n");
2571 }
2572
Jiri Olsa664c98d2015-11-05 15:40:50 +01002573 if (!perf_stat.file.is_pipe) {
2574 perf_stat.session->header.data_size += perf_stat.bytes_written;
2575 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2576 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002577
2578 perf_session__delete(perf_stat.session);
2579 }
2580
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002581 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002582 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002583out:
2584 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002585 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002586}