blob: 0c16d20d7e32fa2eb7377247c2e5542d2a5d076a [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
Andi Kleen44b1e602016-05-30 12:49:42 -030068#include <api/fs/fs.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020069#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020070#include <sys/prctl.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020071#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070072#include <math.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020073
Stephane Eraniand7470b62010-12-01 18:49:05 +020074#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060075#define CNTR_NOT_SUPPORTED "<not supported>"
76#define CNTR_NOT_COUNTED "<not counted>"
Stephane Eraniand7470b62010-12-01 18:49:05 +020077
Jiri Olsad4f63a42015-06-26 11:29:26 +020078static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010079
Andi Kleen4cabc3d2013-08-21 16:47:26 -070080/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020081static const char *transaction_attrs = {
82 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070083 "{"
84 "instructions,"
85 "cycles,"
86 "cpu/cycles-t/,"
87 "cpu/tx-start/,"
88 "cpu/el-start/,"
89 "cpu/cycles-ct/"
90 "}"
91};
92
93/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +020094static const char * transaction_limited_attrs = {
95 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070096 "{"
97 "instructions,"
98 "cycles,"
99 "cpu/cycles-t/,"
100 "cpu/tx-start/"
101 "}"
102};
103
Andi Kleen44b1e602016-05-30 12:49:42 -0300104static const char * topdown_attrs[] = {
105 "topdown-total-slots",
106 "topdown-slots-retired",
107 "topdown-recovery-bubbles",
108 "topdown-fetch-bubbles",
109 "topdown-slots-issued",
110 NULL,
111};
112
Robert Richter666e6d42012-04-05 18:26:27 +0200113static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200114
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300115static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900116 .uid = UINT_MAX,
117};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530118
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100119typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
120
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530121static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200122static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200123static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000124static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200125static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700126static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300127static bool topdown_run = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200128static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200129static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200130static const char *csv_sep = NULL;
131static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800132static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200133static const char *pre_cmd = NULL;
134static const char *post_cmd = NULL;
135static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700136static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100137static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500138static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800139static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300140static bool force_metric_only = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100141static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100142static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100143static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100144static bool append_file;
145static const char *output_name;
146static int output_fd;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200147
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100148struct perf_stat {
149 bool record;
150 struct perf_data_file file;
151 struct perf_session *session;
152 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100153 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100154 bool maps_allocated;
155 struct cpu_map *cpus;
156 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100157 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100158};
159
160static struct perf_stat perf_stat;
161#define STAT_RECORD perf_stat.record
162
Liming Wang60666c62009-12-31 16:05:50 +0800163static volatile int done = 0;
164
Jiri Olsa421a50f2015-07-21 14:31:22 +0200165static struct perf_stat_config stat_config = {
166 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200167 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200168};
169
Stephane Eranian13370a92013-01-29 12:47:44 +0100170static inline void diff_timespec(struct timespec *r, struct timespec *a,
171 struct timespec *b)
172{
173 r->tv_sec = a->tv_sec - b->tv_sec;
174 if (a->tv_nsec < b->tv_nsec) {
175 r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
176 r->tv_sec--;
177 } else {
178 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
179 }
180}
181
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200182static void perf_stat__reset_stats(void)
183{
184 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200185 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200186}
187
Jiri Olsacac21422012-11-12 18:34:00 +0100188static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200189{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200190 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200191
Jiri Olsa711a5722015-07-21 14:31:23 +0200192 if (stat_config.scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200193 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
194 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200195
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200196 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300197
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100198 /*
199 * Some events get initialized with sample_(period/type) set,
200 * like tracepoints. Clear it up for counting.
201 */
202 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100203
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100204 /*
205 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
206 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100207 *
208 * However for pipe sessions we need to keep it zero,
209 * because script's perf_evsel__check_attr is triggered
210 * by attr->sample_type != 0, and we can't run it on
211 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100212 */
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100213 if (!(STAT_RECORD && perf_stat.file.is_pipe))
214 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100215
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100216 /*
217 * Disabling all counters initially, they will be enabled
218 * either manually by us or by kernel via enable_on_exec
219 * set later.
220 */
Jiri Olsac8280cec2015-12-03 10:06:45 +0100221 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100222 attr->disabled = 1;
223
Jiri Olsac8280cec2015-12-03 10:06:45 +0100224 /*
225 * In case of initial_delay we enable tracee
226 * events manually.
227 */
228 if (target__none(&target) && !initial_delay)
229 attr->enable_on_exec = 1;
230 }
231
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300232 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300233 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200234
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300235 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200236}
237
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200238/*
239 * Does the counter have nsecs as a unit?
240 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200241static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200242{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200243 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
244 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200245 return 1;
246
247 return 0;
248}
249
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100250static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100251 union perf_event *event,
252 struct perf_sample *sample __maybe_unused,
253 struct machine *machine __maybe_unused)
254{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100255 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
256 pr_err("failed to write perf data, error: %m\n");
257 return -1;
258 }
259
260 perf_stat.bytes_written += event->header.size;
261 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100262}
263
Jiri Olsa1975d362015-11-05 15:40:56 +0100264static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100265{
Jiri Olsa1975d362015-11-05 15:40:56 +0100266 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100267 process_synthesized_event,
268 NULL);
269}
270
271#define WRITE_STAT_ROUND_EVENT(time, interval) \
272 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
273
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100274#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
275
276static int
277perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
278 struct perf_counts_values *count)
279{
280 struct perf_sample_id *sid = SID(counter, cpu, thread);
281
282 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
283 process_synthesized_event, NULL);
284}
285
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200286/*
287 * Read out the results of a single counter:
288 * do not aggregate counts across CPUs in system-wide mode
289 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200290static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200291{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100292 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100293 int ncpus, cpu, thread;
294
295 if (target__has_cpu(&target))
296 ncpus = perf_evsel__nr_cpus(counter);
297 else
298 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200299
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000300 if (!counter->supported)
301 return -ENOENT;
302
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100303 if (counter->system_wide)
304 nthreads = 1;
305
306 for (thread = 0; thread < nthreads; thread++) {
307 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200308 struct perf_counts_values *count;
309
310 count = perf_counts(counter->counts, cpu, thread);
311 if (perf_evsel__read(counter, cpu, thread, count))
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100312 return -1;
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100313
314 if (STAT_RECORD) {
315 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
316 pr_err("failed to write stat event\n");
317 return -1;
318 }
319 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700320
321 if (verbose > 1) {
322 fprintf(stat_config.output,
323 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
324 perf_evsel__name(counter),
325 cpu,
326 count->val, count->ena, count->run);
327 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100328 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200329 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200330
331 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200332}
333
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200334static void read_counters(bool close_counters)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200335{
336 struct perf_evsel *counter;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200337
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300338 evlist__for_each_entry(evsel_list, counter) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200339 if (read_counter(counter))
Andi Kleen245bad82015-09-01 15:52:46 -0700340 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200341
Jiri Olsaf80010e2015-07-21 14:31:27 +0200342 if (perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200343 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200344
Jiri Olsa5fc472a2015-07-08 13:17:31 +0200345 if (close_counters) {
Jiri Olsa106a94a2015-06-26 11:29:19 +0200346 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
347 thread_map__nr(evsel_list->threads));
348 }
349 }
350}
351
Jiri Olsaba411a92015-06-26 11:29:24 +0200352static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100353{
Stephane Eranian13370a92013-01-29 12:47:44 +0100354 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100355
Jiri Olsa106a94a2015-06-26 11:29:19 +0200356 read_counters(false);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100357
Stephane Eranian13370a92013-01-29 12:47:44 +0100358 clock_gettime(CLOCK_MONOTONIC, &ts);
359 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100360
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100361 if (STAT_RECORD) {
362 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSECS_PER_SEC + rs.tv_nsec, INTERVAL))
363 pr_err("failed to write stat round event\n");
364 }
365
Jiri Olsad4f63a42015-06-26 11:29:26 +0200366 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100367}
368
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100369static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700370{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100371 if (initial_delay)
Andi Kleen41191682013-08-02 17:41:11 -0700372 usleep(initial_delay * 1000);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100373
374 /*
375 * We need to enable counters only if:
376 * - we don't have tracee (attaching to task or cpu)
377 * - we have initial delay configured
378 */
379 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100380 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700381}
382
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300383static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300384
385/*
386 * perf_evlist__prepare_workload will send a SIGUSR1
387 * if the fork fails, since we asked by setting its
388 * want_signal to true.
389 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300390static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
391 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300392{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300393 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300394}
395
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100396static bool has_unit(struct perf_evsel *counter)
397{
398 return counter->unit && *counter->unit;
399}
400
401static bool has_scale(struct perf_evsel *counter)
402{
403 return counter->scale != 1;
404}
405
Jiri Olsa664c98d2015-11-05 15:40:50 +0100406static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100407{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100408 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100409 int err;
410
Jiri Olsa664c98d2015-11-05 15:40:50 +0100411 if (is_pipe) {
412 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
413 process_synthesized_event);
414 if (err < 0) {
415 pr_err("Couldn't synthesize attrs.\n");
416 return err;
417 }
418 }
419
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100420 /*
421 * Synthesize other events stuff not carried within
422 * attr event - unit, scale, name
423 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300424 evlist__for_each_entry(evsel_list, counter) {
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100425 if (!counter->supported)
426 continue;
427
428 /*
429 * Synthesize unit and scale only if it's defined.
430 */
431 if (has_unit(counter)) {
432 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
433 if (err < 0) {
434 pr_err("Couldn't synthesize evsel unit.\n");
435 return err;
436 }
437 }
438
439 if (has_scale(counter)) {
440 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
441 if (err < 0) {
442 pr_err("Couldn't synthesize evsel scale.\n");
443 return err;
444 }
445 }
446
447 if (counter->own_cpus) {
448 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
449 if (err < 0) {
450 pr_err("Couldn't synthesize evsel scale.\n");
451 return err;
452 }
453 }
454
455 /*
456 * Name is needed only for pipe output,
457 * perf.data carries event names.
458 */
459 if (is_pipe) {
460 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
461 if (err < 0) {
462 pr_err("Couldn't synthesize evsel name.\n");
463 return err;
464 }
465 }
466 }
467
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100468 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
469 process_synthesized_event,
470 NULL);
471 if (err < 0) {
472 pr_err("Couldn't synthesize thread map.\n");
473 return err;
474 }
475
476 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
477 process_synthesized_event, NULL);
478 if (err < 0) {
479 pr_err("Couldn't synthesize thread map.\n");
480 return err;
481 }
482
483 err = perf_event__synthesize_stat_config(NULL, &stat_config,
484 process_synthesized_event, NULL);
485 if (err < 0) {
486 pr_err("Couldn't synthesize config.\n");
487 return err;
488 }
489
490 return 0;
491}
492
Jiri Olsa2af46462015-11-05 15:40:49 +0100493#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
494
495static int __store_counter_ids(struct perf_evsel *counter,
496 struct cpu_map *cpus,
497 struct thread_map *threads)
498{
499 int cpu, thread;
500
501 for (cpu = 0; cpu < cpus->nr; cpu++) {
502 for (thread = 0; thread < threads->nr; thread++) {
503 int fd = FD(counter, cpu, thread);
504
505 if (perf_evlist__id_add_fd(evsel_list, counter,
506 cpu, thread, fd) < 0)
507 return -1;
508 }
509 }
510
511 return 0;
512}
513
514static int store_counter_ids(struct perf_evsel *counter)
515{
516 struct cpu_map *cpus = counter->cpus;
517 struct thread_map *threads = counter->threads;
518
519 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
520 return -ENOMEM;
521
522 return __store_counter_ids(counter, cpus, threads);
523}
524
Namhyung Kimacf28922013-03-11 16:43:18 +0900525static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200526{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200527 int interval = stat_config.interval;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300528 char msg[512];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200529 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100530 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100531 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100532 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200533 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300534 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100535 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200536
Stephane Eranian13370a92013-01-29 12:47:44 +0100537 if (interval) {
538 ts.tv_sec = interval / 1000;
539 ts.tv_nsec = (interval % 1000) * 1000000;
540 } else {
541 ts.tv_sec = 1;
542 ts.tv_nsec = 0;
543 }
544
Liming Wang60666c62009-12-31 16:05:50 +0800545 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100546 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300547 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900548 perror("failed to prepare workload");
549 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800550 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900551 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000552 }
553
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200554 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300555 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200556
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300557 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300558try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100559 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600560 /*
561 * PPC returns ENXIO for HW counters until 2.6.37
562 * (behavior changed with commit b0a873e).
563 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100564 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600565 errno == ENOENT || errno == EOPNOTSUPP ||
566 errno == ENXIO) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600567 if (verbose)
568 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300569 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600570 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400571
572 if ((counter->leader != counter) ||
573 !(counter->leader->nr_members > 1))
574 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300575 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
576 if (verbose)
577 ui__warning("%s\n", msg);
578 goto try_again;
579 }
Ingo Molnarede70292011-04-28 08:48:42 +0200580
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300581 perf_evsel__open_strerror(counter, &target,
582 errno, msg, sizeof(msg));
583 ui__error("%s\n", msg);
584
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200585 if (child_pid != -1)
586 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600587
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200588 return -1;
589 }
David Ahern2cee77c2011-05-30 08:55:59 -0600590 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100591
592 l = strlen(counter->unit);
593 if (l > unit_width)
594 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100595
596 if (STAT_RECORD && store_counter_ids(counter))
597 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300598 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200599
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300600 if (perf_evlist__apply_filters(evsel_list, &counter)) {
601 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
602 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300603 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100604 return -1;
605 }
606
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100607 if (STAT_RECORD) {
608 int err, fd = perf_data_file__fd(&perf_stat.file);
609
Jiri Olsa664c98d2015-11-05 15:40:50 +0100610 if (is_pipe) {
611 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
612 } else {
613 err = perf_session__write_header(perf_stat.session, evsel_list,
614 fd, false);
615 }
616
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100617 if (err < 0)
618 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100619
Jiri Olsa664c98d2015-11-05 15:40:50 +0100620 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100621 if (err < 0)
622 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100623 }
624
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200625 /*
626 * Enable counters and exec the command:
627 */
628 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100629 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200630
Liming Wang60666c62009-12-31 16:05:50 +0800631 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900632 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100633 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900634
Stephane Eranian13370a92013-01-29 12:47:44 +0100635 if (interval) {
636 while (!waitpid(child_pid, &status, WNOHANG)) {
637 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200638 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100639 }
640 }
Liming Wang60666c62009-12-31 16:05:50 +0800641 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300642
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300643 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300644 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300645 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300646 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300647 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300648
Andi Kleen33e49ea2011-09-15 14:31:40 -0700649 if (WIFSIGNALED(status))
650 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800651 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100652 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100653 while (!done) {
654 nanosleep(&ts, NULL);
655 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200656 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100657 }
Liming Wang60666c62009-12-31 16:05:50 +0800658 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200659
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200660 t1 = rdclock();
661
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200662 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200663
Jiri Olsa106a94a2015-06-26 11:29:19 +0200664 read_counters(true);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200665
Ingo Molnar42202dd2009-06-13 14:57:28 +0200666 return WEXITSTATUS(status);
667}
668
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300669static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200670{
671 int ret;
672
673 if (pre_cmd) {
674 ret = system(pre_cmd);
675 if (ret)
676 return ret;
677 }
678
679 if (sync_run)
680 sync();
681
682 ret = __run_perf_stat(argc, argv);
683 if (ret)
684 return ret;
685
686 if (post_cmd) {
687 ret = system(post_cmd);
688 if (ret)
689 return ret;
690 }
691
692 return ret;
693}
694
Andi Kleend73515c2015-03-11 07:16:27 -0700695static void print_running(u64 run, u64 ena)
696{
697 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200698 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700699 csv_sep,
700 run,
701 csv_sep,
702 ena ? 100.0 * run / ena : 100.0);
703 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200704 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700705 }
706}
707
Ingo Molnarf99844c2011-04-27 05:35:39 +0200708static void print_noise_pct(double total, double avg)
709{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800710 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200711
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700712 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200713 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600714 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200715 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200716}
717
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200718static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200719{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200720 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200721
Peter Zijlstra849abde2009-09-04 18:23:38 +0200722 if (run_count == 1)
723 return;
724
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200725 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200726 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200727}
728
Stephane Eranian12c08a92013-02-14 13:57:29 +0100729static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200730{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200731 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100732 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200733 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100734 cpu_map__id_to_socket(id),
735 csv_output ? 0 : -8,
736 cpu_map__id_to_cpu(id),
737 csv_sep,
738 csv_output ? 0 : 4,
739 nr,
740 csv_sep);
741 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100742 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200743 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100744 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100745 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100746 csv_sep,
747 csv_output ? 0 : 4,
748 nr,
749 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100750 break;
751 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200752 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200753 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100754 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100755 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200756 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200757 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200758 csv_output ? 0 : 16,
759 thread_map__comm(evsel->threads, id),
760 csv_output ? 0 : -8,
761 thread_map__pid(evsel->threads, id),
762 csv_sep);
763 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100764 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200765 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100766 default:
767 break;
768 }
769}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200770
Andi Kleen140aead2016-01-30 09:06:49 -0800771struct outstate {
772 FILE *fh;
773 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800774 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800775 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800776 int id, nr;
777 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800778};
779
780#define METRIC_LEN 35
781
782static void new_line_std(void *ctx)
783{
784 struct outstate *os = ctx;
785
786 os->newline = true;
787}
788
789static void do_new_line_std(struct outstate *os)
790{
791 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800792 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800793 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800794 if (stat_config.aggr_mode == AGGR_NONE)
795 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800796 fprintf(os->fh, " ");
797}
798
799static void print_metric_std(void *ctx, const char *color, const char *fmt,
800 const char *unit, double val)
801{
802 struct outstate *os = ctx;
803 FILE *out = os->fh;
804 int n;
805 bool newline = os->newline;
806
807 os->newline = false;
808
809 if (unit == NULL || fmt == NULL) {
810 fprintf(out, "%-*s", METRIC_LEN, "");
811 return;
812 }
813
814 if (newline)
815 do_new_line_std(os);
816
817 n = fprintf(out, " # ");
818 if (color)
819 n += color_fprintf(out, color, fmt, val);
820 else
821 n += fprintf(out, fmt, val);
822 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
823}
824
Andi Kleen92a61f62016-02-29 14:36:21 -0800825static void new_line_csv(void *ctx)
826{
827 struct outstate *os = ctx;
828 int i;
829
830 fputc('\n', os->fh);
831 if (os->prefix)
832 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800833 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800834 for (i = 0; i < os->nfields; i++)
835 fputs(csv_sep, os->fh);
836}
837
838static void print_metric_csv(void *ctx,
839 const char *color __maybe_unused,
840 const char *fmt, const char *unit, double val)
841{
842 struct outstate *os = ctx;
843 FILE *out = os->fh;
844 char buf[64], *vals, *ends;
845
846 if (unit == NULL || fmt == NULL) {
847 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
848 return;
849 }
850 snprintf(buf, sizeof(buf), fmt, val);
851 vals = buf;
852 while (isspace(*vals))
853 vals++;
854 ends = vals;
855 while (isdigit(*ends) || *ends == '.')
856 ends++;
857 *ends = 0;
858 while (isspace(*unit))
859 unit++;
860 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
861}
862
Andi Kleen54b50912016-03-03 15:57:36 -0800863#define METRIC_ONLY_LEN 20
864
865/* Filter out some columns that don't work well in metrics only mode */
866
867static bool valid_only_metric(const char *unit)
868{
869 if (!unit)
870 return false;
871 if (strstr(unit, "/sec") ||
872 strstr(unit, "hz") ||
873 strstr(unit, "Hz") ||
874 strstr(unit, "CPUs utilized"))
875 return false;
876 return true;
877}
878
879static const char *fixunit(char *buf, struct perf_evsel *evsel,
880 const char *unit)
881{
882 if (!strncmp(unit, "of all", 6)) {
883 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
884 unit);
885 return buf;
886 }
887 return unit;
888}
889
890static void print_metric_only(void *ctx, const char *color, const char *fmt,
891 const char *unit, double val)
892{
893 struct outstate *os = ctx;
894 FILE *out = os->fh;
895 int n;
896 char buf[1024];
897 unsigned mlen = METRIC_ONLY_LEN;
898
899 if (!valid_only_metric(unit))
900 return;
901 unit = fixunit(buf, os->evsel, unit);
902 if (color)
903 n = color_fprintf(out, color, fmt, val);
904 else
905 n = fprintf(out, fmt, val);
906 if (n > METRIC_ONLY_LEN)
907 n = METRIC_ONLY_LEN;
908 if (mlen < strlen(unit))
909 mlen = strlen(unit) + 1;
910 fprintf(out, "%*s", mlen - n, "");
911}
912
913static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
914 const char *fmt,
915 const char *unit, double val)
916{
917 struct outstate *os = ctx;
918 FILE *out = os->fh;
919 char buf[64], *vals, *ends;
920 char tbuf[1024];
921
922 if (!valid_only_metric(unit))
923 return;
924 unit = fixunit(tbuf, os->evsel, unit);
925 snprintf(buf, sizeof buf, fmt, val);
926 vals = buf;
927 while (isspace(*vals))
928 vals++;
929 ends = vals;
930 while (isdigit(*ends) || *ends == '.')
931 ends++;
932 *ends = 0;
933 fprintf(out, "%s%s", vals, csv_sep);
934}
935
936static void new_line_metric(void *ctx __maybe_unused)
937{
938}
939
940static void print_metric_header(void *ctx, const char *color __maybe_unused,
941 const char *fmt __maybe_unused,
942 const char *unit, double val __maybe_unused)
943{
944 struct outstate *os = ctx;
945 char tbuf[1024];
946
947 if (!valid_only_metric(unit))
948 return;
949 unit = fixunit(tbuf, os->evsel, unit);
950 if (csv_output)
951 fprintf(os->fh, "%s%s", unit, csv_sep);
952 else
953 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
954}
955
Andi Kleenda88c7f2014-09-24 13:50:46 -0700956static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100957{
Jiri Olsa58215222015-07-21 14:31:24 +0200958 FILE *output = stat_config.output;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100959 double msecs = avg / 1e6;
Stephane Eranian410136f2013-11-12 17:58:49 +0100960 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600961 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100962
Stephane Eranian410136f2013-11-12 17:58:49 +0100963 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
964 fmt_n = csv_output ? "%s" : "%-25s";
965
Andi Kleenda88c7f2014-09-24 13:50:46 -0700966 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100967
David Ahern4bbe5a62013-09-28 14:28:00 -0600968 scnprintf(name, sizeof(name), "%s%s",
969 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +0100970
971 fprintf(output, fmt_v, msecs, csv_sep);
972
973 if (csv_output)
974 fprintf(output, "%s%s", evsel->unit, csv_sep);
975 else
976 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
977
978 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200979
Stephane Eranian023695d2011-02-14 11:20:01 +0200980 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200981 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200982}
983
Andi Kleen44d49a62016-02-29 14:36:22 -0800984static int first_shadow_cpu(struct perf_evsel *evsel, int id)
985{
986 int i;
987
988 if (!aggr_get_id)
989 return 0;
990
991 if (stat_config.aggr_mode == AGGR_NONE)
992 return id;
993
994 if (stat_config.aggr_mode == AGGR_GLOBAL)
995 return 0;
996
997 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
998 int cpu2 = perf_evsel__cpus(evsel)->map[i];
999
1000 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1001 return cpu2;
1002 }
1003 return 0;
1004}
1005
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001006static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1007{
Jiri Olsa58215222015-07-21 14:31:24 +02001008 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001009 double sc = evsel->scale;
1010 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001011
1012 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001013 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001014 } else {
1015 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001016 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001017 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001018 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001019 }
1020
1021 aggr_printout(evsel, id, nr);
1022
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001023 fprintf(output, fmt, avg, csv_sep);
1024
1025 if (evsel->unit)
1026 fprintf(output, "%-*s%s",
1027 csv_output ? 0 : unit_width,
1028 evsel->unit, csv_sep);
1029
1030 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1031
1032 if (evsel->cgrp)
1033 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001034}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001035
Andi Kleenf9483392016-01-30 09:06:50 -08001036static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Andi Kleencb110f42016-01-30 09:06:51 -08001037 char *prefix, u64 run, u64 ena, double noise)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001038{
Andi Kleen140aead2016-01-30 09:06:49 -08001039 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001040 struct outstate os = {
1041 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001042 .prefix = prefix ? prefix : "",
1043 .id = id,
1044 .nr = nr,
1045 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001046 };
Andi Kleen140aead2016-01-30 09:06:49 -08001047 print_metric_t pm = print_metric_std;
1048 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001049
Andi Kleen54b50912016-03-03 15:57:36 -08001050 if (metric_only) {
1051 nl = new_line_metric;
1052 if (csv_output)
1053 pm = print_metric_only_csv;
1054 else
1055 pm = print_metric_only;
1056 } else
1057 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001058
Andi Kleen54b50912016-03-03 15:57:36 -08001059 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001060 static int aggr_fields[] = {
1061 [AGGR_GLOBAL] = 0,
1062 [AGGR_THREAD] = 1,
1063 [AGGR_NONE] = 1,
1064 [AGGR_SOCKET] = 2,
1065 [AGGR_CORE] = 2,
1066 };
1067
1068 pm = print_metric_csv;
1069 nl = new_line_csv;
1070 os.nfields = 3;
1071 os.nfields += aggr_fields[stat_config.aggr_mode];
1072 if (counter->cgrp)
1073 os.nfields++;
1074 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001075 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001076 if (metric_only) {
1077 pm(&os, NULL, "", "", 0);
1078 return;
1079 }
Andi Kleencb110f42016-01-30 09:06:51 -08001080 aggr_printout(counter, id, nr);
1081
1082 fprintf(stat_config.output, "%*s%s",
1083 csv_output ? 0 : 18,
1084 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1085 csv_sep);
1086
1087 fprintf(stat_config.output, "%-*s%s",
1088 csv_output ? 0 : unit_width,
1089 counter->unit, csv_sep);
1090
1091 fprintf(stat_config.output, "%*s",
1092 csv_output ? 0 : -25,
1093 perf_evsel__name(counter));
1094
1095 if (counter->cgrp)
1096 fprintf(stat_config.output, "%s%s",
1097 csv_sep, counter->cgrp->name);
1098
Andi Kleen92a61f62016-02-29 14:36:21 -08001099 if (!csv_output)
1100 pm(&os, NULL, NULL, "", 0);
1101 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001102 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001103 if (csv_output)
1104 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001105 return;
1106 }
1107
Andi Kleen54b50912016-03-03 15:57:36 -08001108 if (metric_only)
1109 /* nothing */;
1110 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001111 nsec_printout(id, nr, counter, uval);
1112 else
1113 abs_printout(id, nr, counter, uval);
1114
Andi Kleen140aead2016-01-30 09:06:49 -08001115 out.print_metric = pm;
1116 out.new_line = nl;
1117 out.ctx = &os;
1118
Andi Kleen54b50912016-03-03 15:57:36 -08001119 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001120 print_noise(counter, noise);
1121 print_running(run, ena);
1122 }
1123
1124 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001125 first_shadow_cpu(counter, id),
Andi Kleen140aead2016-01-30 09:06:49 -08001126 &out);
Andi Kleen54b50912016-03-03 15:57:36 -08001127 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001128 print_noise(counter, noise);
1129 print_running(run, ena);
1130 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001131}
1132
Andi Kleen44d49a62016-02-29 14:36:22 -08001133static void aggr_update_shadow(void)
1134{
1135 int cpu, s2, id, s;
1136 u64 val;
1137 struct perf_evsel *counter;
1138
1139 for (s = 0; s < aggr_map->nr; s++) {
1140 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001141 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001142 val = 0;
1143 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1144 s2 = aggr_get_id(evsel_list->cpus, cpu);
1145 if (s2 != id)
1146 continue;
1147 val += perf_counts(counter->counts, cpu, 0)->val;
1148 }
1149 val = val * counter->scale;
1150 perf_stat__update_shadow_stats(counter, &val,
1151 first_shadow_cpu(counter, id));
1152 }
1153 }
1154}
1155
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001156static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001157{
Jiri Olsa58215222015-07-21 14:31:24 +02001158 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001159 struct perf_evsel *counter;
Kan Liang601083c2015-07-02 03:08:43 -04001160 int cpu, s, s2, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001161 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001162 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001163 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001164
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001165 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001166 return;
1167
Andi Kleen44d49a62016-02-29 14:36:22 -08001168 aggr_update_shadow();
1169
Andi Kleen54b50912016-03-03 15:57:36 -08001170 /*
1171 * With metric_only everything is on a single line.
1172 * Without each counter has its own line.
1173 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001174 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleen54b50912016-03-03 15:57:36 -08001175 if (prefix && metric_only)
1176 fprintf(output, "%s", prefix);
1177
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001178 id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001179 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001180 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001181 val = ena = run = 0;
1182 nr = 0;
1183 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Kan Liang601083c2015-07-02 03:08:43 -04001184 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001185 if (s2 != id)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001186 continue;
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001187 val += perf_counts(counter->counts, cpu, 0)->val;
1188 ena += perf_counts(counter->counts, cpu, 0)->ena;
1189 run += perf_counts(counter->counts, cpu, 0)->run;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001190 nr++;
1191 }
Andi Kleen54b50912016-03-03 15:57:36 -08001192 if (first && metric_only) {
1193 first = false;
1194 aggr_printout(counter, id, nr);
1195 }
1196 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001197 fprintf(output, "%s", prefix);
1198
Stephane Eranian410136f2013-11-12 17:58:49 +01001199 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001200 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
Andi Kleen54b50912016-03-03 15:57:36 -08001201 if (!metric_only)
1202 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001203 }
Andi Kleen54b50912016-03-03 15:57:36 -08001204 if (metric_only)
1205 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001206 }
1207}
1208
Jiri Olsa32b8af82015-06-26 11:29:27 +02001209static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1210{
Jiri Olsa58215222015-07-21 14:31:24 +02001211 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001212 int nthreads = thread_map__nr(counter->threads);
1213 int ncpus = cpu_map__nr(counter->cpus);
1214 int cpu, thread;
1215 double uval;
1216
1217 for (thread = 0; thread < nthreads; thread++) {
1218 u64 ena = 0, run = 0, val = 0;
1219
1220 for (cpu = 0; cpu < ncpus; cpu++) {
1221 val += perf_counts(counter->counts, cpu, thread)->val;
1222 ena += perf_counts(counter->counts, cpu, thread)->ena;
1223 run += perf_counts(counter->counts, cpu, thread)->run;
1224 }
1225
1226 if (prefix)
1227 fprintf(output, "%s", prefix);
1228
1229 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001230 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001231 fputc('\n', output);
1232 }
1233}
1234
Ingo Molnar42202dd2009-06-13 14:57:28 +02001235/*
1236 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001237 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001238 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001239static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001240{
Jiri Olsa58215222015-07-21 14:31:24 +02001241 FILE *output = stat_config.output;
Jiri Olsa581cc8a2015-10-16 12:41:03 +02001242 struct perf_stat_evsel *ps = counter->priv;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001243 double avg = avg_stats(&ps->res_stats[0]);
Stephane Eranian410136f2013-11-12 17:58:49 +01001244 double uval;
Andi Kleend73515c2015-03-11 07:16:27 -07001245 double avg_enabled, avg_running;
1246
1247 avg_enabled = avg_stats(&ps->res_stats[1]);
1248 avg_running = avg_stats(&ps->res_stats[2]);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001249
Andi Kleen54b50912016-03-03 15:57:36 -08001250 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001251 fprintf(output, "%s", prefix);
1252
Stephane Eranian410136f2013-11-12 17:58:49 +01001253 uval = avg * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001254 printout(-1, 0, counter, uval, prefix, avg_running, avg_enabled, avg);
Andi Kleen54b50912016-03-03 15:57:36 -08001255 if (!metric_only)
1256 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001257}
1258
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001259/*
1260 * Print out the results of a single counter:
1261 * does not use aggregated count in system-wide
1262 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001263static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001264{
Jiri Olsa58215222015-07-21 14:31:24 +02001265 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001266 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001267 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001268 int cpu;
1269
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001270 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001271 val = perf_counts(counter->counts, cpu, 0)->val;
1272 ena = perf_counts(counter->counts, cpu, 0)->ena;
1273 run = perf_counts(counter->counts, cpu, 0)->run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001274
1275 if (prefix)
1276 fprintf(output, "%s", prefix);
1277
Stephane Eranian410136f2013-11-12 17:58:49 +01001278 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001279 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001280
Stephane Eranian4aa90152011-08-15 22:22:33 +02001281 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001282 }
1283}
1284
Andi Kleen206cab62016-03-03 15:57:37 -08001285static void print_no_aggr_metric(char *prefix)
1286{
1287 int cpu;
1288 int nrcpus = 0;
1289 struct perf_evsel *counter;
1290 u64 ena, run, val;
1291 double uval;
1292
1293 nrcpus = evsel_list->cpus->nr;
1294 for (cpu = 0; cpu < nrcpus; cpu++) {
1295 bool first = true;
1296
1297 if (prefix)
1298 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001299 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen206cab62016-03-03 15:57:37 -08001300 if (first) {
1301 aggr_printout(counter, cpu, 0);
1302 first = false;
1303 }
1304 val = perf_counts(counter->counts, cpu, 0)->val;
1305 ena = perf_counts(counter->counts, cpu, 0)->ena;
1306 run = perf_counts(counter->counts, cpu, 0)->run;
1307
1308 uval = val * counter->scale;
1309 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1310 }
1311 fputc('\n', stat_config.output);
1312 }
1313}
1314
Andi Kleen54b50912016-03-03 15:57:36 -08001315static int aggr_header_lens[] = {
1316 [AGGR_CORE] = 18,
1317 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001318 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001319 [AGGR_THREAD] = 24,
1320 [AGGR_GLOBAL] = 0,
1321};
1322
Andi Kleenc51fd632016-05-24 12:52:39 -07001323static const char *aggr_header_csv[] = {
1324 [AGGR_CORE] = "core,cpus,",
1325 [AGGR_SOCKET] = "socket,cpus",
1326 [AGGR_NONE] = "cpu,",
1327 [AGGR_THREAD] = "comm-pid,",
1328 [AGGR_GLOBAL] = ""
1329};
1330
Andi Kleen41c8ca22016-05-24 12:52:38 -07001331static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001332{
1333 struct perf_stat_output_ctx out;
1334 struct perf_evsel *counter;
1335 struct outstate os = {
1336 .fh = stat_config.output
1337 };
1338
1339 if (prefix)
1340 fprintf(stat_config.output, "%s", prefix);
1341
Andi Kleen41c8ca22016-05-24 12:52:38 -07001342 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001343 fprintf(stat_config.output, "%*s",
1344 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001345 if (csv_output) {
1346 if (stat_config.interval)
1347 fputs("time,", stat_config.output);
1348 fputs(aggr_header_csv[stat_config.aggr_mode],
1349 stat_config.output);
1350 }
Andi Kleen54b50912016-03-03 15:57:36 -08001351
1352 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001353 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen54b50912016-03-03 15:57:36 -08001354 os.evsel = counter;
1355 out.ctx = &os;
1356 out.print_metric = print_metric_header;
1357 out.new_line = new_line_metric;
1358 os.evsel = counter;
1359 perf_stat__print_shadow_stats(counter, 0,
1360 0,
1361 &out);
1362 }
1363 fputc('\n', stat_config.output);
1364}
1365
Jiri Olsad4f63a42015-06-26 11:29:26 +02001366static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001367{
Jiri Olsa58215222015-07-21 14:31:24 +02001368 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001369 static int num_print_interval;
1370
1371 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1372
Andi Kleen41c8ca22016-05-24 12:52:38 -07001373 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001374 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001375 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001376 fprintf(output, "# time socket cpus");
1377 if (!metric_only)
1378 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001379 break;
1380 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001381 fprintf(output, "# time core cpus");
1382 if (!metric_only)
1383 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001384 break;
1385 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001386 fprintf(output, "# time CPU");
1387 if (!metric_only)
1388 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001389 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001390 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001391 fprintf(output, "# time comm-pid");
1392 if (!metric_only)
1393 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001394 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001395 case AGGR_GLOBAL:
1396 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001397 fprintf(output, "# time");
1398 if (!metric_only)
1399 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001400 case AGGR_UNSET:
1401 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001402 }
1403 }
1404
Andi Kleen41c8ca22016-05-24 12:52:38 -07001405 if (num_print_interval == 0 && metric_only)
1406 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001407 if (++num_print_interval == 25)
1408 num_print_interval = 0;
1409}
1410
1411static void print_header(int argc, const char **argv)
1412{
Jiri Olsa58215222015-07-21 14:31:24 +02001413 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001414 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001415
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001416 fflush(stdout);
1417
Stephane Eraniand7470b62010-12-01 18:49:05 +02001418 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001419 fprintf(output, "\n");
1420 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001421 if (target.system_wide)
1422 fprintf(output, "\'system wide");
1423 else if (target.cpu_list)
1424 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001425 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001426 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1427 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001428 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001429 } else if (target.pid)
1430 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001431 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001432 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001433
Stephane Eranian4aa90152011-08-15 22:22:33 +02001434 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001435 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001436 fprintf(output, " (%d runs)", run_count);
1437 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001438 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001439}
1440
1441static void print_footer(void)
1442{
Jiri Olsa58215222015-07-21 14:31:24 +02001443 FILE *output = stat_config.output;
1444
Jiri Olsad4f63a42015-06-26 11:29:26 +02001445 if (!null_run)
1446 fprintf(output, "\n");
1447 fprintf(output, " %17.9f seconds time elapsed",
1448 avg_stats(&walltime_nsecs_stats)/1e9);
1449 if (run_count > 1) {
1450 fprintf(output, " ");
1451 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1452 avg_stats(&walltime_nsecs_stats));
1453 }
1454 fprintf(output, "\n\n");
1455}
1456
1457static void print_counters(struct timespec *ts, int argc, const char **argv)
1458{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001459 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001460 struct perf_evsel *counter;
1461 char buf[64], *prefix = NULL;
1462
Jiri Olsa664c98d2015-11-05 15:40:50 +01001463 /* Do not print anything if we record to the pipe. */
1464 if (STAT_RECORD && perf_stat.file.is_pipe)
1465 return;
1466
Jiri Olsad4f63a42015-06-26 11:29:26 +02001467 if (interval)
1468 print_interval(prefix = buf, ts);
1469 else
1470 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001471
Andi Kleen54b50912016-03-03 15:57:36 -08001472 if (metric_only) {
1473 static int num_print_iv;
1474
Andi Kleen41c8ca22016-05-24 12:52:38 -07001475 if (num_print_iv == 0 && !interval)
1476 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001477 if (num_print_iv++ == 25)
1478 num_print_iv = 0;
1479 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1480 fprintf(stat_config.output, "%s", prefix);
1481 }
1482
Jiri Olsa421a50f2015-07-21 14:31:22 +02001483 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001484 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001485 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001486 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001487 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001488 case AGGR_THREAD:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001489 evlist__for_each_entry(evsel_list, counter)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001490 print_aggr_thread(counter, prefix);
1491 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001492 case AGGR_GLOBAL:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001493 evlist__for_each_entry(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001494 print_counter_aggr(counter, prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001495 if (metric_only)
1496 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001497 break;
1498 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001499 if (metric_only)
1500 print_no_aggr_metric(prefix);
1501 else {
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001502 evlist__for_each_entry(evsel_list, counter)
Andi Kleen206cab62016-03-03 15:57:37 -08001503 print_counter(counter, prefix);
1504 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001505 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001506 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001507 default:
1508 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001509 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001510
Jiri Olsad4f63a42015-06-26 11:29:26 +02001511 if (!interval && !csv_output)
1512 print_footer();
1513
Jiri Olsa58215222015-07-21 14:31:24 +02001514 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001515}
1516
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001517static volatile int signr = -1;
1518
Ingo Molnar52425192009-05-26 09:17:18 +02001519static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001520{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001521 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001522 done = 1;
1523
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001524 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001525 /*
1526 * render child_pid harmless
1527 * won't send SIGTERM to a random
1528 * process in case of race condition
1529 * and fast PID recycling
1530 */
1531 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001532}
1533
1534static void sig_atexit(void)
1535{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001536 sigset_t set, oset;
1537
1538 /*
1539 * avoid race condition with SIGCHLD handler
1540 * in skip_signal() which is modifying child_pid
1541 * goal is to avoid send SIGTERM to a random
1542 * process
1543 */
1544 sigemptyset(&set);
1545 sigaddset(&set, SIGCHLD);
1546 sigprocmask(SIG_BLOCK, &set, &oset);
1547
Chris Wilson933da832009-10-04 01:35:01 +01001548 if (child_pid != -1)
1549 kill(child_pid, SIGTERM);
1550
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001551 sigprocmask(SIG_SETMASK, &oset, NULL);
1552
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001553 if (signr == -1)
1554 return;
1555
1556 signal(signr, SIG_DFL);
1557 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001558}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001559
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001560static int stat__set_big_num(const struct option *opt __maybe_unused,
1561 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001562{
1563 big_num_opt = unset ? 0 : 1;
1564 return 0;
1565}
1566
Andi Kleen44b1e602016-05-30 12:49:42 -03001567static int enable_metric_only(const struct option *opt __maybe_unused,
1568 const char *s __maybe_unused, int unset)
1569{
1570 force_metric_only = true;
1571 metric_only = !unset;
1572 return 0;
1573}
1574
Jiri Olsae0547312015-11-05 15:40:45 +01001575static const struct option stat_options[] = {
1576 OPT_BOOLEAN('T', "transaction", &transaction_run,
1577 "hardware transaction statistics"),
1578 OPT_CALLBACK('e', "event", &evsel_list, "event",
1579 "event selector. use 'perf list' to list available events",
1580 parse_events_option),
1581 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1582 "event filter", parse_filter),
1583 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1584 "child tasks do not inherit counters"),
1585 OPT_STRING('p', "pid", &target.pid, "pid",
1586 "stat events on existing process id"),
1587 OPT_STRING('t', "tid", &target.tid, "tid",
1588 "stat events on existing thread id"),
1589 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1590 "system-wide collection from all CPUs"),
1591 OPT_BOOLEAN('g', "group", &group,
1592 "put the counters into a counter group"),
1593 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1594 OPT_INCR('v', "verbose", &verbose,
1595 "be more verbose (show counter open errors, etc)"),
1596 OPT_INTEGER('r', "repeat", &run_count,
1597 "repeat command and print average + stddev (max: 100, forever: 0)"),
1598 OPT_BOOLEAN('n', "null", &null_run,
1599 "null run - dont start any counters"),
1600 OPT_INCR('d', "detailed", &detailed_run,
1601 "detailed run - start a lot of events"),
1602 OPT_BOOLEAN('S', "sync", &sync_run,
1603 "call sync() before starting a run"),
1604 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1605 "print large numbers with thousands\' separators",
1606 stat__set_big_num),
1607 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1608 "list of cpus to monitor in system-wide"),
1609 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1610 "disable CPU count aggregation", AGGR_NONE),
1611 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1612 "print counts with custom separator"),
1613 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1614 "monitor event in cgroup name only", parse_cgroups),
1615 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1616 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1617 OPT_INTEGER(0, "log-fd", &output_fd,
1618 "log output to fd, instead of stderr"),
1619 OPT_STRING(0, "pre", &pre_cmd, "command",
1620 "command to run prior to the measured command"),
1621 OPT_STRING(0, "post", &post_cmd, "command",
1622 "command to run after to the measured command"),
1623 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1624 "print counts at regular interval in ms (>= 10)"),
1625 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1626 "aggregate counts per processor socket", AGGR_SOCKET),
1627 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1628 "aggregate counts per physical processor core", AGGR_CORE),
1629 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1630 "aggregate counts per thread", AGGR_THREAD),
1631 OPT_UINTEGER('D', "delay", &initial_delay,
1632 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001633 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1634 "Only print computed metrics. No raw values", enable_metric_only),
1635 OPT_BOOLEAN(0, "topdown", &topdown_run,
1636 "measure topdown level 1 statistics"),
Jiri Olsae0547312015-11-05 15:40:45 +01001637 OPT_END()
1638};
1639
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001640static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1641{
1642 return cpu_map__get_socket(map, cpu, NULL);
1643}
1644
1645static int perf_stat__get_core(struct cpu_map *map, int cpu)
1646{
1647 return cpu_map__get_core(map, cpu, NULL);
1648}
1649
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001650static int cpu_map__get_max(struct cpu_map *map)
1651{
1652 int i, max = -1;
1653
1654 for (i = 0; i < map->nr; i++) {
1655 if (map->map[i] > max)
1656 max = map->map[i];
1657 }
1658
1659 return max;
1660}
1661
1662static struct cpu_map *cpus_aggr_map;
1663
1664static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1665{
1666 int cpu;
1667
1668 if (idx >= map->nr)
1669 return -1;
1670
1671 cpu = map->map[idx];
1672
1673 if (cpus_aggr_map->map[cpu] == -1)
1674 cpus_aggr_map->map[cpu] = get_id(map, idx);
1675
1676 return cpus_aggr_map->map[cpu];
1677}
1678
1679static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1680{
1681 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1682}
1683
1684static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1685{
1686 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1687}
1688
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001689static int perf_stat_init_aggr_mode(void)
1690{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001691 int nr;
1692
Jiri Olsa421a50f2015-07-21 14:31:22 +02001693 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001694 case AGGR_SOCKET:
1695 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1696 perror("cannot build socket map");
1697 return -1;
1698 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001699 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001700 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001701 case AGGR_CORE:
1702 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1703 perror("cannot build core map");
1704 return -1;
1705 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001706 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001707 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001708 case AGGR_NONE:
1709 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001710 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001711 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001712 default:
1713 break;
1714 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001715
1716 /*
1717 * The evsel_list->cpus is the base we operate on,
1718 * taking the highest cpu number to be the size of
1719 * the aggregation translate cpumap.
1720 */
1721 nr = cpu_map__get_max(evsel_list->cpus);
1722 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1723 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001724}
1725
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001726static void perf_stat__exit_aggr_mode(void)
1727{
1728 cpu_map__put(aggr_map);
1729 cpu_map__put(cpus_aggr_map);
1730 aggr_map = NULL;
1731 cpus_aggr_map = NULL;
1732}
1733
Jiri Olsa68d702f2015-11-05 15:40:58 +01001734static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1735{
1736 int cpu;
1737
1738 if (idx > map->nr)
1739 return -1;
1740
1741 cpu = map->map[idx];
1742
1743 if (cpu >= env->nr_cpus_online)
1744 return -1;
1745
1746 return cpu;
1747}
1748
1749static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1750{
1751 struct perf_env *env = data;
1752 int cpu = perf_env__get_cpu(env, map, idx);
1753
1754 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1755}
1756
1757static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1758{
1759 struct perf_env *env = data;
1760 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1761
1762 if (cpu != -1) {
1763 int socket_id = env->cpu[cpu].socket_id;
1764
1765 /*
1766 * Encode socket in upper 16 bits
1767 * core_id is relative to socket, and
1768 * we need a global id. So we combine
1769 * socket + core id.
1770 */
1771 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1772 }
1773
1774 return core;
1775}
1776
1777static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1778 struct cpu_map **sockp)
1779{
1780 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1781}
1782
1783static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1784 struct cpu_map **corep)
1785{
1786 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1787}
1788
1789static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1790{
1791 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1792}
1793
1794static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1795{
1796 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1797}
1798
1799static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1800{
1801 struct perf_env *env = &st->session->header.env;
1802
1803 switch (stat_config.aggr_mode) {
1804 case AGGR_SOCKET:
1805 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1806 perror("cannot build socket map");
1807 return -1;
1808 }
1809 aggr_get_id = perf_stat__get_socket_file;
1810 break;
1811 case AGGR_CORE:
1812 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1813 perror("cannot build core map");
1814 return -1;
1815 }
1816 aggr_get_id = perf_stat__get_core_file;
1817 break;
1818 case AGGR_NONE:
1819 case AGGR_GLOBAL:
1820 case AGGR_THREAD:
1821 case AGGR_UNSET:
1822 default:
1823 break;
1824 }
1825
1826 return 0;
1827}
1828
Andi Kleen44b1e602016-05-30 12:49:42 -03001829static int topdown_filter_events(const char **attr, char **str, bool use_group)
1830{
1831 int off = 0;
1832 int i;
1833 int len = 0;
1834 char *s;
1835
1836 for (i = 0; attr[i]; i++) {
1837 if (pmu_have_event("cpu", attr[i])) {
1838 len += strlen(attr[i]) + 1;
1839 attr[i - off] = attr[i];
1840 } else
1841 off++;
1842 }
1843 attr[i - off] = NULL;
1844
1845 *str = malloc(len + 1 + 2);
1846 if (!*str)
1847 return -1;
1848 s = *str;
1849 if (i - off == 0) {
1850 *s = 0;
1851 return 0;
1852 }
1853 if (use_group)
1854 *s++ = '{';
1855 for (i = 0; attr[i]; i++) {
1856 strcpy(s, attr[i]);
1857 s += strlen(s);
1858 *s++ = ',';
1859 }
1860 if (use_group) {
1861 s[-1] = '}';
1862 *s = 0;
1863 } else
1864 s[-1] = 0;
1865 return 0;
1866}
1867
1868__weak bool arch_topdown_check_group(bool *warn)
1869{
1870 *warn = false;
1871 return false;
1872}
1873
1874__weak void arch_topdown_group_warn(void)
1875{
1876}
1877
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001878/*
1879 * Add default attributes, if there were no attributes specified or
1880 * if -d/--detailed, -d -d or -d -d -d is used:
1881 */
1882static int add_default_attributes(void)
1883{
Andi Kleen44b1e602016-05-30 12:49:42 -03001884 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08001885 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001886
1887 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1888 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1889 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1890 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1891
1892 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08001893};
1894 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001895 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001896};
1897 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001898 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001899};
1900 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001901 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1902 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1903 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1904
1905};
1906
1907/*
1908 * Detailed stats (-d), covering the L1 and last level data caches:
1909 */
1910 struct perf_event_attr detailed_attrs[] = {
1911
1912 { .type = PERF_TYPE_HW_CACHE,
1913 .config =
1914 PERF_COUNT_HW_CACHE_L1D << 0 |
1915 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1916 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1917
1918 { .type = PERF_TYPE_HW_CACHE,
1919 .config =
1920 PERF_COUNT_HW_CACHE_L1D << 0 |
1921 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1922 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1923
1924 { .type = PERF_TYPE_HW_CACHE,
1925 .config =
1926 PERF_COUNT_HW_CACHE_LL << 0 |
1927 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1928 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1929
1930 { .type = PERF_TYPE_HW_CACHE,
1931 .config =
1932 PERF_COUNT_HW_CACHE_LL << 0 |
1933 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1934 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1935};
1936
1937/*
1938 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1939 */
1940 struct perf_event_attr very_detailed_attrs[] = {
1941
1942 { .type = PERF_TYPE_HW_CACHE,
1943 .config =
1944 PERF_COUNT_HW_CACHE_L1I << 0 |
1945 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1946 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1947
1948 { .type = PERF_TYPE_HW_CACHE,
1949 .config =
1950 PERF_COUNT_HW_CACHE_L1I << 0 |
1951 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1952 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1953
1954 { .type = PERF_TYPE_HW_CACHE,
1955 .config =
1956 PERF_COUNT_HW_CACHE_DTLB << 0 |
1957 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1958 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1959
1960 { .type = PERF_TYPE_HW_CACHE,
1961 .config =
1962 PERF_COUNT_HW_CACHE_DTLB << 0 |
1963 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1964 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1965
1966 { .type = PERF_TYPE_HW_CACHE,
1967 .config =
1968 PERF_COUNT_HW_CACHE_ITLB << 0 |
1969 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1970 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1971
1972 { .type = PERF_TYPE_HW_CACHE,
1973 .config =
1974 PERF_COUNT_HW_CACHE_ITLB << 0 |
1975 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1976 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1977
1978};
1979
1980/*
1981 * Very, very detailed stats (-d -d -d), adding prefetch events:
1982 */
1983 struct perf_event_attr very_very_detailed_attrs[] = {
1984
1985 { .type = PERF_TYPE_HW_CACHE,
1986 .config =
1987 PERF_COUNT_HW_CACHE_L1D << 0 |
1988 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1989 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1990
1991 { .type = PERF_TYPE_HW_CACHE,
1992 .config =
1993 PERF_COUNT_HW_CACHE_L1D << 0 |
1994 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1995 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1996};
1997
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001998 /* Set attrs if no event is selected and !null_run: */
1999 if (null_run)
2000 return 0;
2001
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002002 if (transaction_run) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002003 if (pmu_have_event("cpu", "cycles-ct") &&
2004 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02002005 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002006 else
Jiri Olsaa4547422015-06-03 16:25:53 +02002007 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2008 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002009 fprintf(stderr, "Cannot set up transaction events\n");
2010 return -1;
2011 }
2012 return 0;
2013 }
2014
Andi Kleen44b1e602016-05-30 12:49:42 -03002015 if (topdown_run) {
2016 char *str = NULL;
2017 bool warn = false;
2018
2019 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2020 stat_config.aggr_mode != AGGR_CORE) {
2021 pr_err("top down event configuration requires --per-core mode\n");
2022 return -1;
2023 }
2024 stat_config.aggr_mode = AGGR_CORE;
2025 if (nr_cgroups || !target__has_cpu(&target)) {
2026 pr_err("top down event configuration requires system-wide mode (-a)\n");
2027 return -1;
2028 }
2029
2030 if (!force_metric_only)
2031 metric_only = true;
2032 if (topdown_filter_events(topdown_attrs, &str,
2033 arch_topdown_check_group(&warn)) < 0) {
2034 pr_err("Out of memory\n");
2035 return -1;
2036 }
2037 if (topdown_attrs[0] && str) {
2038 if (warn)
2039 arch_topdown_group_warn();
2040 err = parse_events(evsel_list, str, NULL);
2041 if (err) {
2042 fprintf(stderr,
2043 "Cannot set up top down events %s: %d\n",
2044 str, err);
2045 free(str);
2046 return -1;
2047 }
2048 } else {
2049 fprintf(stderr, "System does not support topdown\n");
2050 return -1;
2051 }
2052 free(str);
2053 }
2054
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002055 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002056 if (target__has_cpu(&target))
2057 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2058
Andi Kleen9dec4472016-02-26 16:27:56 -08002059 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2060 return -1;
2061 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2062 if (perf_evlist__add_default_attrs(evsel_list,
2063 frontend_attrs) < 0)
2064 return -1;
2065 }
2066 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2067 if (perf_evlist__add_default_attrs(evsel_list,
2068 backend_attrs) < 0)
2069 return -1;
2070 }
2071 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002072 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002073 }
2074
2075 /* Detailed events get appended to the event list: */
2076
2077 if (detailed_run < 1)
2078 return 0;
2079
2080 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002081 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002082 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002083
2084 if (detailed_run < 2)
2085 return 0;
2086
2087 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002088 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002089 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002090
2091 if (detailed_run < 3)
2092 return 0;
2093
2094 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002095 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002096}
2097
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002098static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002099 "perf stat record [<options>]",
2100 NULL,
2101};
2102
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002103static void init_features(struct perf_session *session)
2104{
2105 int feat;
2106
2107 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2108 perf_header__set_feat(&session->header, feat);
2109
2110 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2111 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2112 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2113 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2114}
2115
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002116static int __cmd_record(int argc, const char **argv)
2117{
2118 struct perf_session *session;
2119 struct perf_data_file *file = &perf_stat.file;
2120
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002121 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002122 PARSE_OPT_STOP_AT_NON_OPTION);
2123
2124 if (output_name)
2125 file->path = output_name;
2126
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002127 if (run_count != 1 || forever) {
2128 pr_err("Cannot use -r option with perf stat record.\n");
2129 return -1;
2130 }
2131
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002132 session = perf_session__new(file, false, NULL);
2133 if (session == NULL) {
2134 pr_err("Perf session creation failed.\n");
2135 return -1;
2136 }
2137
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002138 init_features(session);
2139
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002140 session->evlist = evsel_list;
2141 perf_stat.session = session;
2142 perf_stat.record = true;
2143 return argc;
2144}
2145
Jiri Olsaa56f9392015-11-05 15:40:59 +01002146static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2147 union perf_event *event,
2148 struct perf_session *session)
2149{
Andi Kleene3b03b62016-05-05 16:04:03 -07002150 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002151 struct perf_evsel *counter;
2152 struct timespec tsh, *ts = NULL;
2153 const char **argv = session->header.env.cmdline_argv;
2154 int argc = session->header.env.nr_cmdline;
2155
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002156 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002157 perf_stat_process_counter(&stat_config, counter);
2158
Andi Kleene3b03b62016-05-05 16:04:03 -07002159 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2160 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002161
Andi Kleene3b03b62016-05-05 16:04:03 -07002162 if (stat_config.interval && stat_round->time) {
2163 tsh.tv_sec = stat_round->time / NSECS_PER_SEC;
2164 tsh.tv_nsec = stat_round->time % NSECS_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002165 ts = &tsh;
2166 }
2167
2168 print_counters(ts, argc, argv);
2169 return 0;
2170}
2171
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002172static
2173int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2174 union perf_event *event,
2175 struct perf_session *session __maybe_unused)
2176{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002177 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2178
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002179 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002180
Jiri Olsa89af4e02015-11-05 15:41:02 +01002181 if (cpu_map__empty(st->cpus)) {
2182 if (st->aggr_mode != AGGR_UNSET)
2183 pr_warning("warning: processing task data, aggregation mode not set\n");
2184 return 0;
2185 }
2186
2187 if (st->aggr_mode != AGGR_UNSET)
2188 stat_config.aggr_mode = st->aggr_mode;
2189
Jiri Olsa68d702f2015-11-05 15:40:58 +01002190 if (perf_stat.file.is_pipe)
2191 perf_stat_init_aggr_mode();
2192 else
2193 perf_stat_init_aggr_mode_file(st);
2194
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002195 return 0;
2196}
2197
Jiri Olsa1975d362015-11-05 15:40:56 +01002198static int set_maps(struct perf_stat *st)
2199{
2200 if (!st->cpus || !st->threads)
2201 return 0;
2202
2203 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2204 return -EINVAL;
2205
2206 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2207
2208 if (perf_evlist__alloc_stats(evsel_list, true))
2209 return -ENOMEM;
2210
2211 st->maps_allocated = true;
2212 return 0;
2213}
2214
2215static
2216int process_thread_map_event(struct perf_tool *tool __maybe_unused,
2217 union perf_event *event,
2218 struct perf_session *session __maybe_unused)
2219{
2220 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2221
2222 if (st->threads) {
2223 pr_warning("Extra thread map event, ignoring.\n");
2224 return 0;
2225 }
2226
2227 st->threads = thread_map__new_event(&event->thread_map);
2228 if (!st->threads)
2229 return -ENOMEM;
2230
2231 return set_maps(st);
2232}
2233
2234static
2235int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2236 union perf_event *event,
2237 struct perf_session *session __maybe_unused)
2238{
2239 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2240 struct cpu_map *cpus;
2241
2242 if (st->cpus) {
2243 pr_warning("Extra cpu map event, ignoring.\n");
2244 return 0;
2245 }
2246
2247 cpus = cpu_map__new_data(&event->cpu_map.data);
2248 if (!cpus)
2249 return -ENOMEM;
2250
2251 st->cpus = cpus;
2252 return set_maps(st);
2253}
2254
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002255static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002256 "perf stat report [<options>]",
2257 NULL,
2258};
2259
2260static struct perf_stat perf_stat = {
2261 .tool = {
2262 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002263 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002264 .thread_map = process_thread_map_event,
2265 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002266 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002267 .stat = perf_event__process_stat_event,
2268 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002269 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002270 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002271};
2272
2273static int __cmd_report(int argc, const char **argv)
2274{
2275 struct perf_session *session;
2276 const struct option options[] = {
2277 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002278 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2279 "aggregate counts per processor socket", AGGR_SOCKET),
2280 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2281 "aggregate counts per physical processor core", AGGR_CORE),
2282 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2283 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002284 OPT_END()
2285 };
2286 struct stat st;
2287 int ret;
2288
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002289 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002290
2291 if (!input_name || !strlen(input_name)) {
2292 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2293 input_name = "-";
2294 else
2295 input_name = "perf.data";
2296 }
2297
2298 perf_stat.file.path = input_name;
2299 perf_stat.file.mode = PERF_DATA_MODE_READ;
2300
2301 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2302 if (session == NULL)
2303 return -1;
2304
2305 perf_stat.session = session;
2306 stat_config.output = stderr;
2307 evsel_list = session->evlist;
2308
2309 ret = perf_session__process_events(session);
2310 if (ret)
2311 return ret;
2312
2313 perf_session__delete(session);
2314 return 0;
2315}
2316
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002317int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02002318{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002319 const char * const stat_usage[] = {
2320 "perf stat [<options>] [<command>]",
2321 NULL
2322 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002323 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002324 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002325 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002326 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002327 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002328
Stephane Eranian5af52b52010-05-18 15:00:01 +02002329 setlocale(LC_ALL, "");
2330
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002331 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002332 if (evsel_list == NULL)
2333 return -ENOMEM;
2334
Wang Nan1669e502016-02-19 11:43:58 +00002335 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002336 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2337 (const char **) stat_usage,
2338 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002339 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002340
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002341 if (csv_sep) {
2342 csv_output = true;
2343 if (!strcmp(csv_sep, "\\t"))
2344 csv_sep = "\t";
2345 } else
2346 csv_sep = DEFAULT_SEPARATOR;
2347
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002348 if (argc && !strncmp(argv[0], "rec", 3)) {
2349 argc = __cmd_record(argc, argv);
2350 if (argc < 0)
2351 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002352 } else if (argc && !strncmp(argv[0], "rep", 3))
2353 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002354
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002355 interval = stat_config.interval;
2356
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002357 /*
2358 * For record command the -o is already taken care of.
2359 */
2360 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002361 output = NULL;
2362
Jim Cromie56f3bae2011-09-07 17:14:00 -06002363 if (output_name && output_fd) {
2364 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002365 parse_options_usage(stat_usage, stat_options, "o", 1);
2366 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002367 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002368 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002369
Andi Kleen54b50912016-03-03 15:57:36 -08002370 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2371 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2372 goto out;
2373 }
2374
Andi Kleen54b50912016-03-03 15:57:36 -08002375 if (metric_only && run_count > 1) {
2376 fprintf(stderr, "--metric-only is not supported with -r\n");
2377 goto out;
2378 }
2379
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002380 if (output_fd < 0) {
2381 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002382 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002383 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002384 }
2385
Stephane Eranian4aa90152011-08-15 22:22:33 +02002386 if (!output) {
2387 struct timespec tm;
2388 mode = append_file ? "a" : "w";
2389
2390 output = fopen(output_name, mode);
2391 if (!output) {
2392 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002393 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002394 }
2395 clock_gettime(CLOCK_REALTIME, &tm);
2396 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002397 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002398 mode = append_file ? "a" : "w";
2399 output = fdopen(output_fd, mode);
2400 if (!output) {
2401 perror("Failed opening logfd");
2402 return -errno;
2403 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002404 }
2405
Jiri Olsa58215222015-07-21 14:31:24 +02002406 stat_config.output = output;
2407
Stephane Eraniand7470b62010-12-01 18:49:05 +02002408 /*
2409 * let the spreadsheet do the pretty-printing
2410 */
2411 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002412 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002413 if (big_num_opt == 1) {
2414 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002415 parse_options_usage(stat_usage, stat_options, "B", 1);
2416 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002417 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002418 } else /* Nope, so disable big number formatting */
2419 big_num = false;
2420 } else if (big_num_opt == 0) /* User passed --no-big-num */
2421 big_num = false;
2422
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002423 if (!argc && target__none(&target))
Jiri Olsae0547312015-11-05 15:40:45 +01002424 usage_with_options(stat_usage, stat_options);
David Ahernac3063b2013-09-30 07:37:37 -06002425
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002426 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002427 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002428 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002429 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002430 } else if (run_count == 0) {
2431 forever = true;
2432 run_count = 1;
2433 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002434
Jiri Olsa421a50f2015-07-21 14:31:22 +02002435 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002436 fprintf(stderr, "The --per-thread option is only available "
2437 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002438 parse_options_usage(NULL, stat_options, "p", 1);
2439 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002440 goto out;
2441 }
2442
2443 /*
2444 * no_aggr, cgroup are for system-wide only
2445 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2446 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002447 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2448 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002449 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002450 fprintf(stderr, "both cgroup and no-aggregation "
2451 "modes only available in system-wide mode\n");
2452
Jiri Olsae0547312015-11-05 15:40:45 +01002453 parse_options_usage(stat_usage, stat_options, "G", 1);
2454 parse_options_usage(NULL, stat_options, "A", 1);
2455 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002456 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002457 }
2458
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002459 if (add_default_attributes())
2460 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002461
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002462 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002463
Namhyung Kim77a6f012012-05-07 14:09:04 +09002464 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002465 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002466 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002467 parse_options_usage(stat_usage, stat_options, "p", 1);
2468 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002469 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002470 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002471 parse_options_usage(stat_usage, stat_options, "C", 1);
2472 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002473 }
2474 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002475 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002476
2477 /*
2478 * Initialize thread_map with comm names,
2479 * so we could print it out on output.
2480 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002481 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02002482 thread_map__read_comms(evsel_list->threads);
2483
Stephane Eranian13370a92013-01-29 12:47:44 +01002484 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002485 if (interval < 10) {
2486 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002487 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002488 goto out;
2489 } else
2490 pr_warning("print interval < 100ms. "
2491 "The overhead percentage could be high in some cases. "
2492 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002493 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002494
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002495 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002496 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002497
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002498 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002499 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002500
Ingo Molnar58d7e992009-05-15 11:03:23 +02002501 /*
2502 * We dont want to block the signals - that would cause
2503 * child tasks to inherit that and Ctrl-C would not work.
2504 * What we want is for Ctrl-C to work in the exec()-ed
2505 * task, but being ignored by perf stat itself:
2506 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002507 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002508 if (!forever)
2509 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002510 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002511 signal(SIGALRM, skip_signal);
2512 signal(SIGABRT, skip_signal);
2513
Ingo Molnar42202dd2009-06-13 14:57:28 +02002514 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002515 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Ingo Molnar42202dd2009-06-13 14:57:28 +02002516 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002517 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2518 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002519
Ingo Molnar42202dd2009-06-13 14:57:28 +02002520 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002521 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002522 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002523 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002524 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002525 }
2526
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002527 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002528 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002529
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002530 if (STAT_RECORD) {
2531 /*
2532 * We synthesize the kernel mmap record just so that older tools
2533 * don't emit warnings about not being able to resolve symbols
2534 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2535 * a saner message about no samples being in the perf.data file.
2536 *
2537 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002538 * in header.c at the moment 'perf stat record' gets introduced, which
2539 * is not really needed once we start adding the stat specific PERF_RECORD_
2540 * records, but the need to suppress the kptr_restrict messages in older
2541 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002542 */
2543 int fd = perf_data_file__fd(&perf_stat.file);
2544 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2545 process_synthesized_event,
2546 &perf_stat.session->machines.host);
2547 if (err) {
2548 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2549 "older tools may produce warnings about this file\n.");
2550 }
2551
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002552 if (!interval) {
2553 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2554 pr_err("failed to write stat round event\n");
2555 }
2556
Jiri Olsa664c98d2015-11-05 15:40:50 +01002557 if (!perf_stat.file.is_pipe) {
2558 perf_stat.session->header.data_size += perf_stat.bytes_written;
2559 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2560 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002561
2562 perf_session__delete(perf_stat.session);
2563 }
2564
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002565 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002566 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002567out:
2568 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002569 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002570}