blob: 5cb58f3afa3559bc4c68926fcc0854a18bd927c8 [file] [log] [blame]
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-stat.c
3 *
4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
6 *
7 * Sample output:
Ingo Molnarddcacfa2009-04-20 15:37:32 +02008
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02009 $ perf stat ./hackbench 10
Ingo Molnarddcacfa2009-04-20 15:37:32 +020010
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020011 Time: 0.118
Ingo Molnarddcacfa2009-04-20 15:37:32 +020012
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020013 Performance counter stats for './hackbench 10':
Ingo Molnarddcacfa2009-04-20 15:37:32 +020014
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020015 1708.761321 task-clock # 11.037 CPUs utilized
16 41,190 context-switches # 0.024 M/sec
17 6,735 CPU-migrations # 0.004 M/sec
18 17,318 page-faults # 0.010 M/sec
19 5,205,202,243 cycles # 3.046 GHz
20 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
21 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
22 2,603,501,247 instructions # 0.50 insns per cycle
23 # 1.48 stalled cycles per insn
24 484,357,498 branches # 283.455 M/sec
25 6,388,934 branch-misses # 1.32% of all branches
26
27 0.154822978 seconds time elapsed
Ingo Molnarddcacfa2009-04-20 15:37:32 +020028
Ingo Molnar52425192009-05-26 09:17:18 +020029 *
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020030 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
Ingo Molnar52425192009-05-26 09:17:18 +020031 *
32 * Improvements and fixes by:
33 *
34 * Arjan van de Ven <arjan@linux.intel.com>
35 * Yanmin Zhang <yanmin.zhang@intel.com>
36 * Wu Fengguang <fengguang.wu@intel.com>
37 * Mike Galbraith <efault@gmx.de>
38 * Paul Mackerras <paulus@samba.org>
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +053039 * Jaswinder Singh Rajput <jaswinder@kernel.org>
Ingo Molnar52425192009-05-26 09:17:18 +020040 *
41 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnarddcacfa2009-04-20 15:37:32 +020042 */
43
Peter Zijlstra1a482f32009-05-23 18:28:58 +020044#include "perf.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +020045#include "builtin.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030046#include "util/cgroup.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020047#include "util/util.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060048#include <subcmd/parse-options.h>
Ingo Molnar52425192009-05-26 09:17:18 +020049#include "util/parse-events.h"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070050#include "util/pmu.h"
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020051#include "util/event.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020052#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020053#include "util/evsel.h"
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020054#include "util/debug.h"
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -060055#include "util/drv_configs.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020056#include "util/color.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080057#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080058#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110059#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030060#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020061#include "util/thread_map.h"
Jiri Olsad8095602015-08-07 12:51:03 +020062#include "util/counts.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030063#include "util/group.h"
Jiri Olsa4979d0c2015-11-05 15:40:46 +010064#include "util/session.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010065#include "util/tool.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030066#include "util/group.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010067#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020068
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030069#include <linux/time64.h>
Andi Kleen44b1e602016-05-30 12:49:42 -030070#include <api/fs/fs.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020071#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020072#include <sys/prctl.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020073#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070074#include <math.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020075
Stephane Eraniand7470b62010-12-01 18:49:05 +020076#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060077#define CNTR_NOT_SUPPORTED "<not supported>"
78#define CNTR_NOT_COUNTED "<not counted>"
Stephane Eraniand7470b62010-12-01 18:49:05 +020079
Jiri Olsad4f63a42015-06-26 11:29:26 +020080static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010081
Andi Kleen4cabc3d2013-08-21 16:47:26 -070082/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020083static const char *transaction_attrs = {
84 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070085 "{"
86 "instructions,"
87 "cycles,"
88 "cpu/cycles-t/,"
89 "cpu/tx-start/,"
90 "cpu/el-start/,"
91 "cpu/cycles-ct/"
92 "}"
93};
94
95/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +020096static const char * transaction_limited_attrs = {
97 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070098 "{"
99 "instructions,"
100 "cycles,"
101 "cpu/cycles-t/,"
102 "cpu/tx-start/"
103 "}"
104};
105
Andi Kleen44b1e602016-05-30 12:49:42 -0300106static const char * topdown_attrs[] = {
107 "topdown-total-slots",
108 "topdown-slots-retired",
109 "topdown-recovery-bubbles",
110 "topdown-fetch-bubbles",
111 "topdown-slots-issued",
112 NULL,
113};
114
Robert Richter666e6d42012-04-05 18:26:27 +0200115static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200116
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300117static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900118 .uid = UINT_MAX,
119};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530120
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100121typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
122
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530123static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200124static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200125static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000126static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200127static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700128static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300129static bool topdown_run = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200130static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200131static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200132static const char *csv_sep = NULL;
133static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800134static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200135static const char *pre_cmd = NULL;
136static const char *post_cmd = NULL;
137static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700138static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100139static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500140static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800141static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300142static bool force_metric_only = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100143static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100144static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100145static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100146static bool append_file;
147static const char *output_name;
148static int output_fd;
Borislav Petkovb0119cd2017-02-07 01:40:05 +0100149static int print_free_counters_hint;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200150
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100151struct perf_stat {
152 bool record;
153 struct perf_data_file file;
154 struct perf_session *session;
155 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100156 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100157 bool maps_allocated;
158 struct cpu_map *cpus;
159 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100160 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100161};
162
163static struct perf_stat perf_stat;
164#define STAT_RECORD perf_stat.record
165
Liming Wang60666c62009-12-31 16:05:50 +0800166static volatile int done = 0;
167
Jiri Olsa421a50f2015-07-21 14:31:22 +0200168static struct perf_stat_config stat_config = {
169 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200170 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200171};
172
Stephane Eranian13370a92013-01-29 12:47:44 +0100173static inline void diff_timespec(struct timespec *r, struct timespec *a,
174 struct timespec *b)
175{
176 r->tv_sec = a->tv_sec - b->tv_sec;
177 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300178 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100179 r->tv_sec--;
180 } else {
181 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
182 }
183}
184
Jiri Olsa254ecbc2015-06-26 11:29:13 +0200185static void perf_stat__reset_stats(void)
186{
187 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200188 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200189}
190
Jiri Olsacac21422012-11-12 18:34:00 +0100191static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200192{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200193 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200194
Jiri Olsa711a5722015-07-21 14:31:23 +0200195 if (stat_config.scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200196 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
197 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200198
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200199 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300200
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100201 /*
202 * Some events get initialized with sample_(period/type) set,
203 * like tracepoints. Clear it up for counting.
204 */
205 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100206
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100207 /*
208 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
209 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100210 *
211 * However for pipe sessions we need to keep it zero,
212 * because script's perf_evsel__check_attr is triggered
213 * by attr->sample_type != 0, and we can't run it on
214 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100215 */
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100216 if (!(STAT_RECORD && perf_stat.file.is_pipe))
217 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100218
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100219 /*
220 * Disabling all counters initially, they will be enabled
221 * either manually by us or by kernel via enable_on_exec
222 * set later.
223 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100224 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100225 attr->disabled = 1;
226
Jiri Olsac8280ce2015-12-03 10:06:45 +0100227 /*
228 * In case of initial_delay we enable tracee
229 * events manually.
230 */
231 if (target__none(&target) && !initial_delay)
232 attr->enable_on_exec = 1;
233 }
234
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300235 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300236 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200237
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300238 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200239}
240
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200241/*
242 * Does the counter have nsecs as a unit?
243 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200244static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200245{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200246 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
247 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200248 return 1;
249
250 return 0;
251}
252
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100253static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100254 union perf_event *event,
255 struct perf_sample *sample __maybe_unused,
256 struct machine *machine __maybe_unused)
257{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100258 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
259 pr_err("failed to write perf data, error: %m\n");
260 return -1;
261 }
262
263 perf_stat.bytes_written += event->header.size;
264 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100265}
266
Jiri Olsa1975d362015-11-05 15:40:56 +0100267static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100268{
Jiri Olsa1975d362015-11-05 15:40:56 +0100269 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100270 process_synthesized_event,
271 NULL);
272}
273
274#define WRITE_STAT_ROUND_EVENT(time, interval) \
275 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
276
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100277#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
278
279static int
280perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
281 struct perf_counts_values *count)
282{
283 struct perf_sample_id *sid = SID(counter, cpu, thread);
284
285 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
286 process_synthesized_event, NULL);
287}
288
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200289/*
290 * Read out the results of a single counter:
291 * do not aggregate counts across CPUs in system-wide mode
292 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200293static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200294{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100295 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100296 int ncpus, cpu, thread;
297
298 if (target__has_cpu(&target))
299 ncpus = perf_evsel__nr_cpus(counter);
300 else
301 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200302
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000303 if (!counter->supported)
304 return -ENOENT;
305
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100306 if (counter->system_wide)
307 nthreads = 1;
308
309 for (thread = 0; thread < nthreads; thread++) {
310 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200311 struct perf_counts_values *count;
312
313 count = perf_counts(counter->counts, cpu, thread);
Stephane Eranian46eae022017-04-12 11:23:01 -0700314 if (perf_evsel__read(counter, cpu, thread, count)) {
315 counter->counts->scaled = -1;
316 perf_counts(counter->counts, cpu, thread)->ena = 0;
317 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100318 return -1;
Stephane Eranian46eae022017-04-12 11:23:01 -0700319 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100320
321 if (STAT_RECORD) {
322 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
323 pr_err("failed to write stat event\n");
324 return -1;
325 }
326 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700327
328 if (verbose > 1) {
329 fprintf(stat_config.output,
330 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
331 perf_evsel__name(counter),
332 cpu,
333 count->val, count->ena, count->run);
334 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100335 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200336 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200337
338 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200339}
340
Mark Rutland3df33ef2016-08-09 14:04:29 +0100341static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200342{
343 struct perf_evsel *counter;
Stephane Eranian46eae022017-04-12 11:23:01 -0700344 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200345
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300346 evlist__for_each_entry(evsel_list, counter) {
Stephane Eranian46eae022017-04-12 11:23:01 -0700347 ret = read_counter(counter);
348 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700349 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200350
Stephane Eranian46eae022017-04-12 11:23:01 -0700351 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200352 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200353 }
354}
355
Jiri Olsaba411a92015-06-26 11:29:24 +0200356static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100357{
Stephane Eranian13370a92013-01-29 12:47:44 +0100358 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100359
Mark Rutland3df33ef2016-08-09 14:04:29 +0100360 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100361
Stephane Eranian13370a92013-01-29 12:47:44 +0100362 clock_gettime(CLOCK_MONOTONIC, &ts);
363 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100364
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100365 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300366 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100367 pr_err("failed to write stat round event\n");
368 }
369
Jiri Olsad4f63a42015-06-26 11:29:26 +0200370 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100371}
372
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100373static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700374{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100375 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300376 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100377
378 /*
379 * We need to enable counters only if:
380 * - we don't have tracee (attaching to task or cpu)
381 * - we have initial delay configured
382 */
383 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100384 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700385}
386
Mark Rutland3df33ef2016-08-09 14:04:29 +0100387static void disable_counters(void)
388{
389 /*
390 * If we don't have tracee (attaching to task or cpu), counters may
391 * still be running. To get accurate group ratios, we must stop groups
392 * from counting before reading their constituent counters.
393 */
394 if (!target__none(&target))
395 perf_evlist__disable(evsel_list);
396}
397
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300398static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300399
400/*
401 * perf_evlist__prepare_workload will send a SIGUSR1
402 * if the fork fails, since we asked by setting its
403 * want_signal to true.
404 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300405static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
406 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300407{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300408 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300409}
410
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100411static bool has_unit(struct perf_evsel *counter)
412{
413 return counter->unit && *counter->unit;
414}
415
416static bool has_scale(struct perf_evsel *counter)
417{
418 return counter->scale != 1;
419}
420
Jiri Olsa664c98d2015-11-05 15:40:50 +0100421static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100422{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100423 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100424 int err;
425
Jiri Olsa664c98d2015-11-05 15:40:50 +0100426 if (is_pipe) {
427 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
428 process_synthesized_event);
429 if (err < 0) {
430 pr_err("Couldn't synthesize attrs.\n");
431 return err;
432 }
433 }
434
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100435 /*
436 * Synthesize other events stuff not carried within
437 * attr event - unit, scale, name
438 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300439 evlist__for_each_entry(evsel_list, counter) {
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100440 if (!counter->supported)
441 continue;
442
443 /*
444 * Synthesize unit and scale only if it's defined.
445 */
446 if (has_unit(counter)) {
447 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
448 if (err < 0) {
449 pr_err("Couldn't synthesize evsel unit.\n");
450 return err;
451 }
452 }
453
454 if (has_scale(counter)) {
455 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
456 if (err < 0) {
457 pr_err("Couldn't synthesize evsel scale.\n");
458 return err;
459 }
460 }
461
462 if (counter->own_cpus) {
463 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
464 if (err < 0) {
465 pr_err("Couldn't synthesize evsel scale.\n");
466 return err;
467 }
468 }
469
470 /*
471 * Name is needed only for pipe output,
472 * perf.data carries event names.
473 */
474 if (is_pipe) {
475 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
476 if (err < 0) {
477 pr_err("Couldn't synthesize evsel name.\n");
478 return err;
479 }
480 }
481 }
482
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100483 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
484 process_synthesized_event,
485 NULL);
486 if (err < 0) {
487 pr_err("Couldn't synthesize thread map.\n");
488 return err;
489 }
490
491 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
492 process_synthesized_event, NULL);
493 if (err < 0) {
494 pr_err("Couldn't synthesize thread map.\n");
495 return err;
496 }
497
498 err = perf_event__synthesize_stat_config(NULL, &stat_config,
499 process_synthesized_event, NULL);
500 if (err < 0) {
501 pr_err("Couldn't synthesize config.\n");
502 return err;
503 }
504
505 return 0;
506}
507
Jiri Olsa2af46462015-11-05 15:40:49 +0100508#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
509
510static int __store_counter_ids(struct perf_evsel *counter,
511 struct cpu_map *cpus,
512 struct thread_map *threads)
513{
514 int cpu, thread;
515
516 for (cpu = 0; cpu < cpus->nr; cpu++) {
517 for (thread = 0; thread < threads->nr; thread++) {
518 int fd = FD(counter, cpu, thread);
519
520 if (perf_evlist__id_add_fd(evsel_list, counter,
521 cpu, thread, fd) < 0)
522 return -1;
523 }
524 }
525
526 return 0;
527}
528
529static int store_counter_ids(struct perf_evsel *counter)
530{
531 struct cpu_map *cpus = counter->cpus;
532 struct thread_map *threads = counter->threads;
533
534 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
535 return -ENOMEM;
536
537 return __store_counter_ids(counter, cpus, threads);
538}
539
Namhyung Kimacf28922013-03-11 16:43:18 +0900540static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200541{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200542 int interval = stat_config.interval;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300543 char msg[512];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200544 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100545 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100546 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100547 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200548 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300549 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100550 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600551 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200552
Stephane Eranian13370a92013-01-29 12:47:44 +0100553 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300554 ts.tv_sec = interval / USEC_PER_MSEC;
555 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100556 } else {
557 ts.tv_sec = 1;
558 ts.tv_nsec = 0;
559 }
560
Liming Wang60666c62009-12-31 16:05:50 +0800561 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100562 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300563 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900564 perror("failed to prepare workload");
565 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800566 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900567 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000568 }
569
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200570 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300571 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200572
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300573 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300574try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100575 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600576 /*
577 * PPC returns ENXIO for HW counters until 2.6.37
578 * (behavior changed with commit b0a873e).
579 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100580 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600581 errno == ENOENT || errno == EOPNOTSUPP ||
582 errno == ENXIO) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600583 if (verbose)
584 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300585 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600586 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400587
588 if ((counter->leader != counter) ||
589 !(counter->leader->nr_members > 1))
590 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300591 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
592 if (verbose)
593 ui__warning("%s\n", msg);
594 goto try_again;
595 }
Ingo Molnarede70292011-04-28 08:48:42 +0200596
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300597 perf_evsel__open_strerror(counter, &target,
598 errno, msg, sizeof(msg));
599 ui__error("%s\n", msg);
600
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200601 if (child_pid != -1)
602 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600603
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200604 return -1;
605 }
David Ahern2cee77c2011-05-30 08:55:59 -0600606 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100607
608 l = strlen(counter->unit);
609 if (l > unit_width)
610 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100611
612 if (STAT_RECORD && store_counter_ids(counter))
613 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300614 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200615
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300616 if (perf_evlist__apply_filters(evsel_list, &counter)) {
617 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
618 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300619 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100620 return -1;
621 }
622
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600623 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
624 error("failed to set config \"%s\" on event %s with %d (%s)\n",
625 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
626 str_error_r(errno, msg, sizeof(msg)));
627 return -1;
628 }
629
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100630 if (STAT_RECORD) {
631 int err, fd = perf_data_file__fd(&perf_stat.file);
632
Jiri Olsa664c98d2015-11-05 15:40:50 +0100633 if (is_pipe) {
634 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
635 } else {
636 err = perf_session__write_header(perf_stat.session, evsel_list,
637 fd, false);
638 }
639
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100640 if (err < 0)
641 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100642
Jiri Olsa664c98d2015-11-05 15:40:50 +0100643 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100644 if (err < 0)
645 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100646 }
647
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200648 /*
649 * Enable counters and exec the command:
650 */
651 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100652 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200653
Liming Wang60666c62009-12-31 16:05:50 +0800654 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900655 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100656 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900657
Stephane Eranian13370a92013-01-29 12:47:44 +0100658 if (interval) {
659 while (!waitpid(child_pid, &status, WNOHANG)) {
660 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200661 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100662 }
663 }
Liming Wang60666c62009-12-31 16:05:50 +0800664 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300665
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300666 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300667 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300668 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300669 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300670 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300671
Andi Kleen33e49ea2011-09-15 14:31:40 -0700672 if (WIFSIGNALED(status))
673 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800674 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100675 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100676 while (!done) {
677 nanosleep(&ts, NULL);
678 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200679 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100680 }
Liming Wang60666c62009-12-31 16:05:50 +0800681 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200682
Mark Rutland3df33ef2016-08-09 14:04:29 +0100683 disable_counters();
684
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200685 t1 = rdclock();
686
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200687 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200688
Mark Rutland3df33ef2016-08-09 14:04:29 +0100689 /*
690 * Closing a group leader splits the group, and as we only disable
691 * group leaders, results in remaining events becoming enabled. To
692 * avoid arbitrary skew, we must read all counters before closing any
693 * group leaders.
694 */
695 read_counters();
696 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200697
Ingo Molnar42202dd2009-06-13 14:57:28 +0200698 return WEXITSTATUS(status);
699}
700
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300701static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200702{
703 int ret;
704
705 if (pre_cmd) {
706 ret = system(pre_cmd);
707 if (ret)
708 return ret;
709 }
710
711 if (sync_run)
712 sync();
713
714 ret = __run_perf_stat(argc, argv);
715 if (ret)
716 return ret;
717
718 if (post_cmd) {
719 ret = system(post_cmd);
720 if (ret)
721 return ret;
722 }
723
724 return ret;
725}
726
Andi Kleend73515c2015-03-11 07:16:27 -0700727static void print_running(u64 run, u64 ena)
728{
729 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200730 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700731 csv_sep,
732 run,
733 csv_sep,
734 ena ? 100.0 * run / ena : 100.0);
735 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200736 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700737 }
738}
739
Ingo Molnarf99844c2011-04-27 05:35:39 +0200740static void print_noise_pct(double total, double avg)
741{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800742 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200743
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700744 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200745 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600746 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200747 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200748}
749
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200750static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200751{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200752 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200753
Peter Zijlstra849abde2009-09-04 18:23:38 +0200754 if (run_count == 1)
755 return;
756
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200757 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200758 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200759}
760
Stephane Eranian12c08a92013-02-14 13:57:29 +0100761static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200762{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200763 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100764 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200765 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100766 cpu_map__id_to_socket(id),
767 csv_output ? 0 : -8,
768 cpu_map__id_to_cpu(id),
769 csv_sep,
770 csv_output ? 0 : 4,
771 nr,
772 csv_sep);
773 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100774 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200775 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100776 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100777 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100778 csv_sep,
779 csv_output ? 0 : 4,
780 nr,
781 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100782 break;
783 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200784 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200785 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100786 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100787 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200788 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200789 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200790 csv_output ? 0 : 16,
791 thread_map__comm(evsel->threads, id),
792 csv_output ? 0 : -8,
793 thread_map__pid(evsel->threads, id),
794 csv_sep);
795 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100796 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200797 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100798 default:
799 break;
800 }
801}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200802
Andi Kleen140aead2016-01-30 09:06:49 -0800803struct outstate {
804 FILE *fh;
805 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800806 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800807 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800808 int id, nr;
809 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800810};
811
812#define METRIC_LEN 35
813
814static void new_line_std(void *ctx)
815{
816 struct outstate *os = ctx;
817
818 os->newline = true;
819}
820
821static void do_new_line_std(struct outstate *os)
822{
823 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800824 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800825 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800826 if (stat_config.aggr_mode == AGGR_NONE)
827 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800828 fprintf(os->fh, " ");
829}
830
831static void print_metric_std(void *ctx, const char *color, const char *fmt,
832 const char *unit, double val)
833{
834 struct outstate *os = ctx;
835 FILE *out = os->fh;
836 int n;
837 bool newline = os->newline;
838
839 os->newline = false;
840
841 if (unit == NULL || fmt == NULL) {
842 fprintf(out, "%-*s", METRIC_LEN, "");
843 return;
844 }
845
846 if (newline)
847 do_new_line_std(os);
848
849 n = fprintf(out, " # ");
850 if (color)
851 n += color_fprintf(out, color, fmt, val);
852 else
853 n += fprintf(out, fmt, val);
854 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
855}
856
Andi Kleen92a61f62016-02-29 14:36:21 -0800857static void new_line_csv(void *ctx)
858{
859 struct outstate *os = ctx;
860 int i;
861
862 fputc('\n', os->fh);
863 if (os->prefix)
864 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800865 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800866 for (i = 0; i < os->nfields; i++)
867 fputs(csv_sep, os->fh);
868}
869
870static void print_metric_csv(void *ctx,
871 const char *color __maybe_unused,
872 const char *fmt, const char *unit, double val)
873{
874 struct outstate *os = ctx;
875 FILE *out = os->fh;
876 char buf[64], *vals, *ends;
877
878 if (unit == NULL || fmt == NULL) {
Ilya Pronina8b3a6a2018-03-05 22:43:53 -0800879 fprintf(out, "%s%s", csv_sep, csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800880 return;
881 }
882 snprintf(buf, sizeof(buf), fmt, val);
883 vals = buf;
884 while (isspace(*vals))
885 vals++;
886 ends = vals;
887 while (isdigit(*ends) || *ends == '.')
888 ends++;
889 *ends = 0;
890 while (isspace(*unit))
891 unit++;
892 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
893}
894
Andi Kleen54b50912016-03-03 15:57:36 -0800895#define METRIC_ONLY_LEN 20
896
897/* Filter out some columns that don't work well in metrics only mode */
898
899static bool valid_only_metric(const char *unit)
900{
901 if (!unit)
902 return false;
903 if (strstr(unit, "/sec") ||
904 strstr(unit, "hz") ||
905 strstr(unit, "Hz") ||
906 strstr(unit, "CPUs utilized"))
907 return false;
908 return true;
909}
910
911static const char *fixunit(char *buf, struct perf_evsel *evsel,
912 const char *unit)
913{
914 if (!strncmp(unit, "of all", 6)) {
915 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
916 unit);
917 return buf;
918 }
919 return unit;
920}
921
922static void print_metric_only(void *ctx, const char *color, const char *fmt,
923 const char *unit, double val)
924{
925 struct outstate *os = ctx;
926 FILE *out = os->fh;
927 int n;
928 char buf[1024];
929 unsigned mlen = METRIC_ONLY_LEN;
930
931 if (!valid_only_metric(unit))
932 return;
933 unit = fixunit(buf, os->evsel, unit);
934 if (color)
935 n = color_fprintf(out, color, fmt, val);
936 else
937 n = fprintf(out, fmt, val);
938 if (n > METRIC_ONLY_LEN)
939 n = METRIC_ONLY_LEN;
940 if (mlen < strlen(unit))
941 mlen = strlen(unit) + 1;
942 fprintf(out, "%*s", mlen - n, "");
943}
944
945static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
946 const char *fmt,
947 const char *unit, double val)
948{
949 struct outstate *os = ctx;
950 FILE *out = os->fh;
951 char buf[64], *vals, *ends;
952 char tbuf[1024];
953
954 if (!valid_only_metric(unit))
955 return;
956 unit = fixunit(tbuf, os->evsel, unit);
957 snprintf(buf, sizeof buf, fmt, val);
958 vals = buf;
959 while (isspace(*vals))
960 vals++;
961 ends = vals;
962 while (isdigit(*ends) || *ends == '.')
963 ends++;
964 *ends = 0;
965 fprintf(out, "%s%s", vals, csv_sep);
966}
967
968static void new_line_metric(void *ctx __maybe_unused)
969{
970}
971
972static void print_metric_header(void *ctx, const char *color __maybe_unused,
973 const char *fmt __maybe_unused,
974 const char *unit, double val __maybe_unused)
975{
976 struct outstate *os = ctx;
977 char tbuf[1024];
978
979 if (!valid_only_metric(unit))
980 return;
981 unit = fixunit(tbuf, os->evsel, unit);
982 if (csv_output)
983 fprintf(os->fh, "%s%s", unit, csv_sep);
984 else
985 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
986}
987
Andi Kleenda88c7f2014-09-24 13:50:46 -0700988static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100989{
Jiri Olsa58215222015-07-21 14:31:24 +0200990 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300991 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +0100992 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600993 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100994
Stephane Eranian410136f2013-11-12 17:58:49 +0100995 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
996 fmt_n = csv_output ? "%s" : "%-25s";
997
Andi Kleenda88c7f2014-09-24 13:50:46 -0700998 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100999
David Ahern4bbe5a62013-09-28 14:28:00 -06001000 scnprintf(name, sizeof(name), "%s%s",
1001 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +01001002
1003 fprintf(output, fmt_v, msecs, csv_sep);
1004
1005 if (csv_output)
1006 fprintf(output, "%s%s", evsel->unit, csv_sep);
1007 else
1008 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1009
1010 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001011
Stephane Eranian023695d2011-02-14 11:20:01 +02001012 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001013 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001014}
1015
Andi Kleen44d49a62016-02-29 14:36:22 -08001016static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1017{
1018 int i;
1019
1020 if (!aggr_get_id)
1021 return 0;
1022
1023 if (stat_config.aggr_mode == AGGR_NONE)
1024 return id;
1025
1026 if (stat_config.aggr_mode == AGGR_GLOBAL)
1027 return 0;
1028
1029 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1030 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1031
1032 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1033 return cpu2;
1034 }
1035 return 0;
1036}
1037
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001038static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1039{
Jiri Olsa58215222015-07-21 14:31:24 +02001040 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001041 double sc = evsel->scale;
1042 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001043
1044 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001045 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001046 } else {
1047 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001048 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001049 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001050 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001051 }
1052
1053 aggr_printout(evsel, id, nr);
1054
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001055 fprintf(output, fmt, avg, csv_sep);
1056
1057 if (evsel->unit)
1058 fprintf(output, "%-*s%s",
1059 csv_output ? 0 : unit_width,
1060 evsel->unit, csv_sep);
1061
1062 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1063
1064 if (evsel->cgrp)
1065 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001066}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001067
Andi Kleenf9483392016-01-30 09:06:50 -08001068static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Andi Kleencb110f42016-01-30 09:06:51 -08001069 char *prefix, u64 run, u64 ena, double noise)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001070{
Andi Kleen140aead2016-01-30 09:06:49 -08001071 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001072 struct outstate os = {
1073 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001074 .prefix = prefix ? prefix : "",
1075 .id = id,
1076 .nr = nr,
1077 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001078 };
Andi Kleen140aead2016-01-30 09:06:49 -08001079 print_metric_t pm = print_metric_std;
1080 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001081
Andi Kleen54b50912016-03-03 15:57:36 -08001082 if (metric_only) {
1083 nl = new_line_metric;
1084 if (csv_output)
1085 pm = print_metric_only_csv;
1086 else
1087 pm = print_metric_only;
1088 } else
1089 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001090
Andi Kleen54b50912016-03-03 15:57:36 -08001091 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001092 static int aggr_fields[] = {
1093 [AGGR_GLOBAL] = 0,
1094 [AGGR_THREAD] = 1,
1095 [AGGR_NONE] = 1,
1096 [AGGR_SOCKET] = 2,
1097 [AGGR_CORE] = 2,
1098 };
1099
1100 pm = print_metric_csv;
1101 nl = new_line_csv;
1102 os.nfields = 3;
1103 os.nfields += aggr_fields[stat_config.aggr_mode];
1104 if (counter->cgrp)
1105 os.nfields++;
1106 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001107 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001108 if (metric_only) {
1109 pm(&os, NULL, "", "", 0);
1110 return;
1111 }
Andi Kleencb110f42016-01-30 09:06:51 -08001112 aggr_printout(counter, id, nr);
1113
1114 fprintf(stat_config.output, "%*s%s",
1115 csv_output ? 0 : 18,
1116 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1117 csv_sep);
1118
Borislav Petkovb0119cd2017-02-07 01:40:05 +01001119 if (counter->supported)
1120 print_free_counters_hint = 1;
1121
Andi Kleencb110f42016-01-30 09:06:51 -08001122 fprintf(stat_config.output, "%-*s%s",
1123 csv_output ? 0 : unit_width,
1124 counter->unit, csv_sep);
1125
1126 fprintf(stat_config.output, "%*s",
1127 csv_output ? 0 : -25,
1128 perf_evsel__name(counter));
1129
1130 if (counter->cgrp)
1131 fprintf(stat_config.output, "%s%s",
1132 csv_sep, counter->cgrp->name);
1133
Andi Kleen92a61f62016-02-29 14:36:21 -08001134 if (!csv_output)
1135 pm(&os, NULL, NULL, "", 0);
1136 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001137 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001138 if (csv_output)
1139 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001140 return;
1141 }
1142
Andi Kleen54b50912016-03-03 15:57:36 -08001143 if (metric_only)
1144 /* nothing */;
1145 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001146 nsec_printout(id, nr, counter, uval);
1147 else
1148 abs_printout(id, nr, counter, uval);
1149
Andi Kleen140aead2016-01-30 09:06:49 -08001150 out.print_metric = pm;
1151 out.new_line = nl;
1152 out.ctx = &os;
1153
Andi Kleen54b50912016-03-03 15:57:36 -08001154 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001155 print_noise(counter, noise);
1156 print_running(run, ena);
1157 }
1158
1159 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001160 first_shadow_cpu(counter, id),
Andi Kleen140aead2016-01-30 09:06:49 -08001161 &out);
Andi Kleen54b50912016-03-03 15:57:36 -08001162 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001163 print_noise(counter, noise);
1164 print_running(run, ena);
1165 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001166}
1167
Andi Kleen44d49a62016-02-29 14:36:22 -08001168static void aggr_update_shadow(void)
1169{
1170 int cpu, s2, id, s;
1171 u64 val;
1172 struct perf_evsel *counter;
1173
1174 for (s = 0; s < aggr_map->nr; s++) {
1175 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001176 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001177 val = 0;
1178 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1179 s2 = aggr_get_id(evsel_list->cpus, cpu);
1180 if (s2 != id)
1181 continue;
1182 val += perf_counts(counter->counts, cpu, 0)->val;
1183 }
1184 val = val * counter->scale;
1185 perf_stat__update_shadow_stats(counter, &val,
1186 first_shadow_cpu(counter, id));
1187 }
1188 }
1189}
1190
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001191static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001192{
Jiri Olsa58215222015-07-21 14:31:24 +02001193 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001194 struct perf_evsel *counter;
Kan Liang601083c2015-07-02 03:08:43 -04001195 int cpu, s, s2, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001196 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001197 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001198 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001199
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001200 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001201 return;
1202
Andi Kleen44d49a62016-02-29 14:36:22 -08001203 aggr_update_shadow();
1204
Andi Kleen54b50912016-03-03 15:57:36 -08001205 /*
1206 * With metric_only everything is on a single line.
1207 * Without each counter has its own line.
1208 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001209 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleen54b50912016-03-03 15:57:36 -08001210 if (prefix && metric_only)
1211 fprintf(output, "%s", prefix);
1212
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001213 id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001214 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001215 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001216 val = ena = run = 0;
1217 nr = 0;
1218 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Kan Liang601083c2015-07-02 03:08:43 -04001219 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001220 if (s2 != id)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001221 continue;
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001222 val += perf_counts(counter->counts, cpu, 0)->val;
1223 ena += perf_counts(counter->counts, cpu, 0)->ena;
1224 run += perf_counts(counter->counts, cpu, 0)->run;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001225 nr++;
1226 }
Andi Kleen54b50912016-03-03 15:57:36 -08001227 if (first && metric_only) {
1228 first = false;
1229 aggr_printout(counter, id, nr);
1230 }
1231 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001232 fprintf(output, "%s", prefix);
1233
Stephane Eranian410136f2013-11-12 17:58:49 +01001234 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001235 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
Andi Kleen54b50912016-03-03 15:57:36 -08001236 if (!metric_only)
1237 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001238 }
Andi Kleen54b50912016-03-03 15:57:36 -08001239 if (metric_only)
1240 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001241 }
1242}
1243
Jiri Olsa32b8af82015-06-26 11:29:27 +02001244static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1245{
Jiri Olsa58215222015-07-21 14:31:24 +02001246 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001247 int nthreads = thread_map__nr(counter->threads);
1248 int ncpus = cpu_map__nr(counter->cpus);
1249 int cpu, thread;
1250 double uval;
1251
1252 for (thread = 0; thread < nthreads; thread++) {
1253 u64 ena = 0, run = 0, val = 0;
1254
1255 for (cpu = 0; cpu < ncpus; cpu++) {
1256 val += perf_counts(counter->counts, cpu, thread)->val;
1257 ena += perf_counts(counter->counts, cpu, thread)->ena;
1258 run += perf_counts(counter->counts, cpu, thread)->run;
1259 }
1260
1261 if (prefix)
1262 fprintf(output, "%s", prefix);
1263
1264 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001265 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001266 fputc('\n', output);
1267 }
1268}
1269
Ingo Molnar42202dd2009-06-13 14:57:28 +02001270/*
1271 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001272 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001273 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001274static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001275{
Jiri Olsa58215222015-07-21 14:31:24 +02001276 FILE *output = stat_config.output;
Jiri Olsa581cc8a2015-10-16 12:41:03 +02001277 struct perf_stat_evsel *ps = counter->priv;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001278 double avg = avg_stats(&ps->res_stats[0]);
Stephane Eranian410136f2013-11-12 17:58:49 +01001279 double uval;
Andi Kleend73515c2015-03-11 07:16:27 -07001280 double avg_enabled, avg_running;
1281
1282 avg_enabled = avg_stats(&ps->res_stats[1]);
1283 avg_running = avg_stats(&ps->res_stats[2]);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001284
Andi Kleen54b50912016-03-03 15:57:36 -08001285 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001286 fprintf(output, "%s", prefix);
1287
Stephane Eranian410136f2013-11-12 17:58:49 +01001288 uval = avg * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001289 printout(-1, 0, counter, uval, prefix, avg_running, avg_enabled, avg);
Andi Kleen54b50912016-03-03 15:57:36 -08001290 if (!metric_only)
1291 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001292}
1293
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001294/*
1295 * Print out the results of a single counter:
1296 * does not use aggregated count in system-wide
1297 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001298static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001299{
Jiri Olsa58215222015-07-21 14:31:24 +02001300 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001301 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001302 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001303 int cpu;
1304
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001305 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001306 val = perf_counts(counter->counts, cpu, 0)->val;
1307 ena = perf_counts(counter->counts, cpu, 0)->ena;
1308 run = perf_counts(counter->counts, cpu, 0)->run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001309
1310 if (prefix)
1311 fprintf(output, "%s", prefix);
1312
Stephane Eranian410136f2013-11-12 17:58:49 +01001313 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001314 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001315
Stephane Eranian4aa90152011-08-15 22:22:33 +02001316 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001317 }
1318}
1319
Andi Kleen206cab62016-03-03 15:57:37 -08001320static void print_no_aggr_metric(char *prefix)
1321{
1322 int cpu;
1323 int nrcpus = 0;
1324 struct perf_evsel *counter;
1325 u64 ena, run, val;
1326 double uval;
1327
1328 nrcpus = evsel_list->cpus->nr;
1329 for (cpu = 0; cpu < nrcpus; cpu++) {
1330 bool first = true;
1331
1332 if (prefix)
1333 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001334 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen206cab62016-03-03 15:57:37 -08001335 if (first) {
1336 aggr_printout(counter, cpu, 0);
1337 first = false;
1338 }
1339 val = perf_counts(counter->counts, cpu, 0)->val;
1340 ena = perf_counts(counter->counts, cpu, 0)->ena;
1341 run = perf_counts(counter->counts, cpu, 0)->run;
1342
1343 uval = val * counter->scale;
1344 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1345 }
1346 fputc('\n', stat_config.output);
1347 }
1348}
1349
Andi Kleen54b50912016-03-03 15:57:36 -08001350static int aggr_header_lens[] = {
1351 [AGGR_CORE] = 18,
1352 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001353 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001354 [AGGR_THREAD] = 24,
1355 [AGGR_GLOBAL] = 0,
1356};
1357
Andi Kleenc51fd632016-05-24 12:52:39 -07001358static const char *aggr_header_csv[] = {
1359 [AGGR_CORE] = "core,cpus,",
1360 [AGGR_SOCKET] = "socket,cpus",
1361 [AGGR_NONE] = "cpu,",
1362 [AGGR_THREAD] = "comm-pid,",
1363 [AGGR_GLOBAL] = ""
1364};
1365
Andi Kleen41c8ca22016-05-24 12:52:38 -07001366static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001367{
1368 struct perf_stat_output_ctx out;
1369 struct perf_evsel *counter;
1370 struct outstate os = {
1371 .fh = stat_config.output
1372 };
1373
1374 if (prefix)
1375 fprintf(stat_config.output, "%s", prefix);
1376
Andi Kleen41c8ca22016-05-24 12:52:38 -07001377 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001378 fprintf(stat_config.output, "%*s",
1379 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001380 if (csv_output) {
1381 if (stat_config.interval)
1382 fputs("time,", stat_config.output);
1383 fputs(aggr_header_csv[stat_config.aggr_mode],
1384 stat_config.output);
1385 }
Andi Kleen54b50912016-03-03 15:57:36 -08001386
1387 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001388 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen54b50912016-03-03 15:57:36 -08001389 os.evsel = counter;
1390 out.ctx = &os;
1391 out.print_metric = print_metric_header;
1392 out.new_line = new_line_metric;
1393 os.evsel = counter;
1394 perf_stat__print_shadow_stats(counter, 0,
1395 0,
1396 &out);
1397 }
1398 fputc('\n', stat_config.output);
1399}
1400
Jiri Olsad4f63a42015-06-26 11:29:26 +02001401static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001402{
Jiri Olsa58215222015-07-21 14:31:24 +02001403 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001404 static int num_print_interval;
1405
1406 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1407
Andi Kleen41c8ca22016-05-24 12:52:38 -07001408 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001409 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001410 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001411 fprintf(output, "# time socket cpus");
1412 if (!metric_only)
1413 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001414 break;
1415 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001416 fprintf(output, "# time core cpus");
1417 if (!metric_only)
1418 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001419 break;
1420 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001421 fprintf(output, "# time CPU");
1422 if (!metric_only)
1423 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001424 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001425 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001426 fprintf(output, "# time comm-pid");
1427 if (!metric_only)
1428 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001429 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001430 case AGGR_GLOBAL:
1431 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001432 fprintf(output, "# time");
1433 if (!metric_only)
1434 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001435 case AGGR_UNSET:
1436 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001437 }
1438 }
1439
Andi Kleen41c8ca22016-05-24 12:52:38 -07001440 if (num_print_interval == 0 && metric_only)
1441 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001442 if (++num_print_interval == 25)
1443 num_print_interval = 0;
1444}
1445
1446static void print_header(int argc, const char **argv)
1447{
Jiri Olsa58215222015-07-21 14:31:24 +02001448 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001449 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001450
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001451 fflush(stdout);
1452
Stephane Eraniand7470b62010-12-01 18:49:05 +02001453 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001454 fprintf(output, "\n");
1455 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001456 if (target.system_wide)
1457 fprintf(output, "\'system wide");
1458 else if (target.cpu_list)
1459 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001460 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001461 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1462 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001463 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001464 } else if (target.pid)
1465 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001466 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001467 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001468
Stephane Eranian4aa90152011-08-15 22:22:33 +02001469 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001470 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001471 fprintf(output, " (%d runs)", run_count);
1472 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001473 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001474}
1475
1476static void print_footer(void)
1477{
Jiri Olsa58215222015-07-21 14:31:24 +02001478 FILE *output = stat_config.output;
1479
Jiri Olsad4f63a42015-06-26 11:29:26 +02001480 if (!null_run)
1481 fprintf(output, "\n");
1482 fprintf(output, " %17.9f seconds time elapsed",
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001483 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001484 if (run_count > 1) {
1485 fprintf(output, " ");
1486 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1487 avg_stats(&walltime_nsecs_stats));
1488 }
1489 fprintf(output, "\n\n");
Borislav Petkovb0119cd2017-02-07 01:40:05 +01001490
1491 if (print_free_counters_hint)
1492 fprintf(output,
1493"Some events weren't counted. Try disabling the NMI watchdog:\n"
1494" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1495" perf stat ...\n"
1496" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001497}
1498
1499static void print_counters(struct timespec *ts, int argc, const char **argv)
1500{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001501 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001502 struct perf_evsel *counter;
1503 char buf[64], *prefix = NULL;
1504
Jiri Olsa664c98d2015-11-05 15:40:50 +01001505 /* Do not print anything if we record to the pipe. */
1506 if (STAT_RECORD && perf_stat.file.is_pipe)
1507 return;
1508
Jiri Olsad4f63a42015-06-26 11:29:26 +02001509 if (interval)
1510 print_interval(prefix = buf, ts);
1511 else
1512 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001513
Andi Kleen54b50912016-03-03 15:57:36 -08001514 if (metric_only) {
1515 static int num_print_iv;
1516
Andi Kleen41c8ca22016-05-24 12:52:38 -07001517 if (num_print_iv == 0 && !interval)
1518 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001519 if (num_print_iv++ == 25)
1520 num_print_iv = 0;
1521 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1522 fprintf(stat_config.output, "%s", prefix);
1523 }
1524
Jiri Olsa421a50f2015-07-21 14:31:22 +02001525 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001526 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001527 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001528 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001529 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001530 case AGGR_THREAD:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001531 evlist__for_each_entry(evsel_list, counter)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001532 print_aggr_thread(counter, prefix);
1533 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001534 case AGGR_GLOBAL:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001535 evlist__for_each_entry(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001536 print_counter_aggr(counter, prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001537 if (metric_only)
1538 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001539 break;
1540 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001541 if (metric_only)
1542 print_no_aggr_metric(prefix);
1543 else {
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001544 evlist__for_each_entry(evsel_list, counter)
Andi Kleen206cab62016-03-03 15:57:37 -08001545 print_counter(counter, prefix);
1546 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001547 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001548 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001549 default:
1550 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001551 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001552
Jiri Olsad4f63a42015-06-26 11:29:26 +02001553 if (!interval && !csv_output)
1554 print_footer();
1555
Jiri Olsa58215222015-07-21 14:31:24 +02001556 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001557}
1558
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001559static volatile int signr = -1;
1560
Ingo Molnar52425192009-05-26 09:17:18 +02001561static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001562{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001563 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001564 done = 1;
1565
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001566 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001567 /*
1568 * render child_pid harmless
1569 * won't send SIGTERM to a random
1570 * process in case of race condition
1571 * and fast PID recycling
1572 */
1573 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001574}
1575
1576static void sig_atexit(void)
1577{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001578 sigset_t set, oset;
1579
1580 /*
1581 * avoid race condition with SIGCHLD handler
1582 * in skip_signal() which is modifying child_pid
1583 * goal is to avoid send SIGTERM to a random
1584 * process
1585 */
1586 sigemptyset(&set);
1587 sigaddset(&set, SIGCHLD);
1588 sigprocmask(SIG_BLOCK, &set, &oset);
1589
Chris Wilson933da832009-10-04 01:35:01 +01001590 if (child_pid != -1)
1591 kill(child_pid, SIGTERM);
1592
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001593 sigprocmask(SIG_SETMASK, &oset, NULL);
1594
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001595 if (signr == -1)
1596 return;
1597
1598 signal(signr, SIG_DFL);
1599 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001600}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001601
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001602static int stat__set_big_num(const struct option *opt __maybe_unused,
1603 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001604{
1605 big_num_opt = unset ? 0 : 1;
1606 return 0;
1607}
1608
Andi Kleen44b1e602016-05-30 12:49:42 -03001609static int enable_metric_only(const struct option *opt __maybe_unused,
1610 const char *s __maybe_unused, int unset)
1611{
1612 force_metric_only = true;
1613 metric_only = !unset;
1614 return 0;
1615}
1616
Jiri Olsae0547312015-11-05 15:40:45 +01001617static const struct option stat_options[] = {
1618 OPT_BOOLEAN('T', "transaction", &transaction_run,
1619 "hardware transaction statistics"),
1620 OPT_CALLBACK('e', "event", &evsel_list, "event",
1621 "event selector. use 'perf list' to list available events",
1622 parse_events_option),
1623 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1624 "event filter", parse_filter),
1625 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1626 "child tasks do not inherit counters"),
1627 OPT_STRING('p', "pid", &target.pid, "pid",
1628 "stat events on existing process id"),
1629 OPT_STRING('t', "tid", &target.tid, "tid",
1630 "stat events on existing thread id"),
1631 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1632 "system-wide collection from all CPUs"),
1633 OPT_BOOLEAN('g', "group", &group,
1634 "put the counters into a counter group"),
1635 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1636 OPT_INCR('v', "verbose", &verbose,
1637 "be more verbose (show counter open errors, etc)"),
1638 OPT_INTEGER('r', "repeat", &run_count,
1639 "repeat command and print average + stddev (max: 100, forever: 0)"),
1640 OPT_BOOLEAN('n', "null", &null_run,
1641 "null run - dont start any counters"),
1642 OPT_INCR('d', "detailed", &detailed_run,
1643 "detailed run - start a lot of events"),
1644 OPT_BOOLEAN('S', "sync", &sync_run,
1645 "call sync() before starting a run"),
1646 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1647 "print large numbers with thousands\' separators",
1648 stat__set_big_num),
1649 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1650 "list of cpus to monitor in system-wide"),
1651 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1652 "disable CPU count aggregation", AGGR_NONE),
1653 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1654 "print counts with custom separator"),
1655 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1656 "monitor event in cgroup name only", parse_cgroups),
1657 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1658 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1659 OPT_INTEGER(0, "log-fd", &output_fd,
1660 "log output to fd, instead of stderr"),
1661 OPT_STRING(0, "pre", &pre_cmd, "command",
1662 "command to run prior to the measured command"),
1663 OPT_STRING(0, "post", &post_cmd, "command",
1664 "command to run after to the measured command"),
1665 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1666 "print counts at regular interval in ms (>= 10)"),
1667 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1668 "aggregate counts per processor socket", AGGR_SOCKET),
1669 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1670 "aggregate counts per physical processor core", AGGR_CORE),
1671 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1672 "aggregate counts per thread", AGGR_THREAD),
1673 OPT_UINTEGER('D', "delay", &initial_delay,
1674 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001675 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1676 "Only print computed metrics. No raw values", enable_metric_only),
1677 OPT_BOOLEAN(0, "topdown", &topdown_run,
1678 "measure topdown level 1 statistics"),
Jiri Olsae0547312015-11-05 15:40:45 +01001679 OPT_END()
1680};
1681
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001682static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1683{
1684 return cpu_map__get_socket(map, cpu, NULL);
1685}
1686
1687static int perf_stat__get_core(struct cpu_map *map, int cpu)
1688{
1689 return cpu_map__get_core(map, cpu, NULL);
1690}
1691
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001692static int cpu_map__get_max(struct cpu_map *map)
1693{
1694 int i, max = -1;
1695
1696 for (i = 0; i < map->nr; i++) {
1697 if (map->map[i] > max)
1698 max = map->map[i];
1699 }
1700
1701 return max;
1702}
1703
1704static struct cpu_map *cpus_aggr_map;
1705
1706static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1707{
1708 int cpu;
1709
1710 if (idx >= map->nr)
1711 return -1;
1712
1713 cpu = map->map[idx];
1714
1715 if (cpus_aggr_map->map[cpu] == -1)
1716 cpus_aggr_map->map[cpu] = get_id(map, idx);
1717
1718 return cpus_aggr_map->map[cpu];
1719}
1720
1721static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1722{
1723 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1724}
1725
1726static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1727{
1728 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1729}
1730
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001731static int perf_stat_init_aggr_mode(void)
1732{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001733 int nr;
1734
Jiri Olsa421a50f2015-07-21 14:31:22 +02001735 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001736 case AGGR_SOCKET:
1737 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1738 perror("cannot build socket map");
1739 return -1;
1740 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001741 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001742 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001743 case AGGR_CORE:
1744 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1745 perror("cannot build core map");
1746 return -1;
1747 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001748 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001749 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001750 case AGGR_NONE:
1751 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001752 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001753 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001754 default:
1755 break;
1756 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001757
1758 /*
1759 * The evsel_list->cpus is the base we operate on,
1760 * taking the highest cpu number to be the size of
1761 * the aggregation translate cpumap.
1762 */
1763 nr = cpu_map__get_max(evsel_list->cpus);
1764 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1765 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001766}
1767
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001768static void perf_stat__exit_aggr_mode(void)
1769{
1770 cpu_map__put(aggr_map);
1771 cpu_map__put(cpus_aggr_map);
1772 aggr_map = NULL;
1773 cpus_aggr_map = NULL;
1774}
1775
Jiri Olsa68d702f2015-11-05 15:40:58 +01001776static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1777{
1778 int cpu;
1779
1780 if (idx > map->nr)
1781 return -1;
1782
1783 cpu = map->map[idx];
1784
1785 if (cpu >= env->nr_cpus_online)
1786 return -1;
1787
1788 return cpu;
1789}
1790
1791static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1792{
1793 struct perf_env *env = data;
1794 int cpu = perf_env__get_cpu(env, map, idx);
1795
1796 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1797}
1798
1799static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1800{
1801 struct perf_env *env = data;
1802 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1803
1804 if (cpu != -1) {
1805 int socket_id = env->cpu[cpu].socket_id;
1806
1807 /*
1808 * Encode socket in upper 16 bits
1809 * core_id is relative to socket, and
1810 * we need a global id. So we combine
1811 * socket + core id.
1812 */
1813 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1814 }
1815
1816 return core;
1817}
1818
1819static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1820 struct cpu_map **sockp)
1821{
1822 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1823}
1824
1825static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1826 struct cpu_map **corep)
1827{
1828 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1829}
1830
1831static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1832{
1833 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1834}
1835
1836static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1837{
1838 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1839}
1840
1841static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1842{
1843 struct perf_env *env = &st->session->header.env;
1844
1845 switch (stat_config.aggr_mode) {
1846 case AGGR_SOCKET:
1847 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1848 perror("cannot build socket map");
1849 return -1;
1850 }
1851 aggr_get_id = perf_stat__get_socket_file;
1852 break;
1853 case AGGR_CORE:
1854 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1855 perror("cannot build core map");
1856 return -1;
1857 }
1858 aggr_get_id = perf_stat__get_core_file;
1859 break;
1860 case AGGR_NONE:
1861 case AGGR_GLOBAL:
1862 case AGGR_THREAD:
1863 case AGGR_UNSET:
1864 default:
1865 break;
1866 }
1867
1868 return 0;
1869}
1870
Andi Kleen44b1e602016-05-30 12:49:42 -03001871static int topdown_filter_events(const char **attr, char **str, bool use_group)
1872{
1873 int off = 0;
1874 int i;
1875 int len = 0;
1876 char *s;
1877
1878 for (i = 0; attr[i]; i++) {
1879 if (pmu_have_event("cpu", attr[i])) {
1880 len += strlen(attr[i]) + 1;
1881 attr[i - off] = attr[i];
1882 } else
1883 off++;
1884 }
1885 attr[i - off] = NULL;
1886
1887 *str = malloc(len + 1 + 2);
1888 if (!*str)
1889 return -1;
1890 s = *str;
1891 if (i - off == 0) {
1892 *s = 0;
1893 return 0;
1894 }
1895 if (use_group)
1896 *s++ = '{';
1897 for (i = 0; attr[i]; i++) {
1898 strcpy(s, attr[i]);
1899 s += strlen(s);
1900 *s++ = ',';
1901 }
1902 if (use_group) {
1903 s[-1] = '}';
1904 *s = 0;
1905 } else
1906 s[-1] = 0;
1907 return 0;
1908}
1909
1910__weak bool arch_topdown_check_group(bool *warn)
1911{
1912 *warn = false;
1913 return false;
1914}
1915
1916__weak void arch_topdown_group_warn(void)
1917{
1918}
1919
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001920/*
1921 * Add default attributes, if there were no attributes specified or
1922 * if -d/--detailed, -d -d or -d -d -d is used:
1923 */
1924static int add_default_attributes(void)
1925{
Andi Kleen44b1e602016-05-30 12:49:42 -03001926 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08001927 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001928
1929 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1930 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1931 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1932 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1933
1934 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08001935};
1936 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001937 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001938};
1939 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001940 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001941};
1942 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001943 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1944 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1945 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1946
1947};
1948
1949/*
1950 * Detailed stats (-d), covering the L1 and last level data caches:
1951 */
1952 struct perf_event_attr detailed_attrs[] = {
1953
1954 { .type = PERF_TYPE_HW_CACHE,
1955 .config =
1956 PERF_COUNT_HW_CACHE_L1D << 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_L1D << 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_LL << 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_LL << 0 |
1975 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1976 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1977};
1978
1979/*
1980 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1981 */
1982 struct perf_event_attr very_detailed_attrs[] = {
1983
1984 { .type = PERF_TYPE_HW_CACHE,
1985 .config =
1986 PERF_COUNT_HW_CACHE_L1I << 0 |
1987 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1988 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1989
1990 { .type = PERF_TYPE_HW_CACHE,
1991 .config =
1992 PERF_COUNT_HW_CACHE_L1I << 0 |
1993 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1994 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1995
1996 { .type = PERF_TYPE_HW_CACHE,
1997 .config =
1998 PERF_COUNT_HW_CACHE_DTLB << 0 |
1999 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2000 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2001
2002 { .type = PERF_TYPE_HW_CACHE,
2003 .config =
2004 PERF_COUNT_HW_CACHE_DTLB << 0 |
2005 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2006 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2007
2008 { .type = PERF_TYPE_HW_CACHE,
2009 .config =
2010 PERF_COUNT_HW_CACHE_ITLB << 0 |
2011 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2012 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2013
2014 { .type = PERF_TYPE_HW_CACHE,
2015 .config =
2016 PERF_COUNT_HW_CACHE_ITLB << 0 |
2017 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2018 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2019
2020};
2021
2022/*
2023 * Very, very detailed stats (-d -d -d), adding prefetch events:
2024 */
2025 struct perf_event_attr very_very_detailed_attrs[] = {
2026
2027 { .type = PERF_TYPE_HW_CACHE,
2028 .config =
2029 PERF_COUNT_HW_CACHE_L1D << 0 |
2030 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2031 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2032
2033 { .type = PERF_TYPE_HW_CACHE,
2034 .config =
2035 PERF_COUNT_HW_CACHE_L1D << 0 |
2036 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2037 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2038};
2039
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002040 /* Set attrs if no event is selected and !null_run: */
2041 if (null_run)
2042 return 0;
2043
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002044 if (transaction_run) {
Thomas Richter5bb5f952018-03-08 15:57:35 +01002045 struct parse_events_error errinfo;
2046
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002047 if (pmu_have_event("cpu", "cycles-ct") &&
2048 pmu_have_event("cpu", "el-start"))
Thomas Richter5bb5f952018-03-08 15:57:35 +01002049 err = parse_events(evsel_list, transaction_attrs,
2050 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002051 else
Thomas Richter5bb5f952018-03-08 15:57:35 +01002052 err = parse_events(evsel_list,
2053 transaction_limited_attrs,
2054 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002055 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002056 fprintf(stderr, "Cannot set up transaction events\n");
2057 return -1;
2058 }
2059 return 0;
2060 }
2061
Andi Kleen44b1e602016-05-30 12:49:42 -03002062 if (topdown_run) {
2063 char *str = NULL;
2064 bool warn = false;
2065
2066 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2067 stat_config.aggr_mode != AGGR_CORE) {
2068 pr_err("top down event configuration requires --per-core mode\n");
2069 return -1;
2070 }
2071 stat_config.aggr_mode = AGGR_CORE;
2072 if (nr_cgroups || !target__has_cpu(&target)) {
2073 pr_err("top down event configuration requires system-wide mode (-a)\n");
2074 return -1;
2075 }
2076
2077 if (!force_metric_only)
2078 metric_only = true;
2079 if (topdown_filter_events(topdown_attrs, &str,
2080 arch_topdown_check_group(&warn)) < 0) {
2081 pr_err("Out of memory\n");
2082 return -1;
2083 }
2084 if (topdown_attrs[0] && str) {
2085 if (warn)
2086 arch_topdown_group_warn();
2087 err = parse_events(evsel_list, str, NULL);
2088 if (err) {
2089 fprintf(stderr,
2090 "Cannot set up top down events %s: %d\n",
2091 str, err);
2092 free(str);
2093 return -1;
2094 }
2095 } else {
2096 fprintf(stderr, "System does not support topdown\n");
2097 return -1;
2098 }
2099 free(str);
2100 }
2101
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002102 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002103 if (target__has_cpu(&target))
2104 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2105
Andi Kleen9dec4472016-02-26 16:27:56 -08002106 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2107 return -1;
2108 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2109 if (perf_evlist__add_default_attrs(evsel_list,
2110 frontend_attrs) < 0)
2111 return -1;
2112 }
2113 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2114 if (perf_evlist__add_default_attrs(evsel_list,
2115 backend_attrs) < 0)
2116 return -1;
2117 }
2118 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002119 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002120 }
2121
2122 /* Detailed events get appended to the event list: */
2123
2124 if (detailed_run < 1)
2125 return 0;
2126
2127 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002128 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002129 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002130
2131 if (detailed_run < 2)
2132 return 0;
2133
2134 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002135 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002136 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002137
2138 if (detailed_run < 3)
2139 return 0;
2140
2141 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002142 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002143}
2144
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002145static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002146 "perf stat record [<options>]",
2147 NULL,
2148};
2149
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002150static void init_features(struct perf_session *session)
2151{
2152 int feat;
2153
2154 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2155 perf_header__set_feat(&session->header, feat);
2156
2157 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2158 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2159 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2160 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2161}
2162
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002163static int __cmd_record(int argc, const char **argv)
2164{
2165 struct perf_session *session;
2166 struct perf_data_file *file = &perf_stat.file;
2167
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002168 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002169 PARSE_OPT_STOP_AT_NON_OPTION);
2170
2171 if (output_name)
2172 file->path = output_name;
2173
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002174 if (run_count != 1 || forever) {
2175 pr_err("Cannot use -r option with perf stat record.\n");
2176 return -1;
2177 }
2178
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002179 session = perf_session__new(file, false, NULL);
2180 if (session == NULL) {
2181 pr_err("Perf session creation failed.\n");
2182 return -1;
2183 }
2184
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002185 init_features(session);
2186
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002187 session->evlist = evsel_list;
2188 perf_stat.session = session;
2189 perf_stat.record = true;
2190 return argc;
2191}
2192
Jiri Olsaa56f9392015-11-05 15:40:59 +01002193static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2194 union perf_event *event,
2195 struct perf_session *session)
2196{
Andi Kleene3b03b62016-05-05 16:04:03 -07002197 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002198 struct perf_evsel *counter;
2199 struct timespec tsh, *ts = NULL;
2200 const char **argv = session->header.env.cmdline_argv;
2201 int argc = session->header.env.nr_cmdline;
2202
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002203 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002204 perf_stat_process_counter(&stat_config, counter);
2205
Andi Kleene3b03b62016-05-05 16:04:03 -07002206 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2207 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002208
Andi Kleene3b03b62016-05-05 16:04:03 -07002209 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002210 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2211 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002212 ts = &tsh;
2213 }
2214
2215 print_counters(ts, argc, argv);
2216 return 0;
2217}
2218
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002219static
2220int process_stat_config_event(struct perf_tool *tool __maybe_unused,
2221 union perf_event *event,
2222 struct perf_session *session __maybe_unused)
2223{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002224 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2225
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002226 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002227
Jiri Olsa89af4e02015-11-05 15:41:02 +01002228 if (cpu_map__empty(st->cpus)) {
2229 if (st->aggr_mode != AGGR_UNSET)
2230 pr_warning("warning: processing task data, aggregation mode not set\n");
2231 return 0;
2232 }
2233
2234 if (st->aggr_mode != AGGR_UNSET)
2235 stat_config.aggr_mode = st->aggr_mode;
2236
Jiri Olsa68d702f2015-11-05 15:40:58 +01002237 if (perf_stat.file.is_pipe)
2238 perf_stat_init_aggr_mode();
2239 else
2240 perf_stat_init_aggr_mode_file(st);
2241
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002242 return 0;
2243}
2244
Jiri Olsa1975d362015-11-05 15:40:56 +01002245static int set_maps(struct perf_stat *st)
2246{
2247 if (!st->cpus || !st->threads)
2248 return 0;
2249
2250 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2251 return -EINVAL;
2252
2253 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2254
2255 if (perf_evlist__alloc_stats(evsel_list, true))
2256 return -ENOMEM;
2257
2258 st->maps_allocated = true;
2259 return 0;
2260}
2261
2262static
2263int process_thread_map_event(struct perf_tool *tool __maybe_unused,
2264 union perf_event *event,
2265 struct perf_session *session __maybe_unused)
2266{
2267 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2268
2269 if (st->threads) {
2270 pr_warning("Extra thread map event, ignoring.\n");
2271 return 0;
2272 }
2273
2274 st->threads = thread_map__new_event(&event->thread_map);
2275 if (!st->threads)
2276 return -ENOMEM;
2277
2278 return set_maps(st);
2279}
2280
2281static
2282int process_cpu_map_event(struct perf_tool *tool __maybe_unused,
2283 union perf_event *event,
2284 struct perf_session *session __maybe_unused)
2285{
2286 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2287 struct cpu_map *cpus;
2288
2289 if (st->cpus) {
2290 pr_warning("Extra cpu map event, ignoring.\n");
2291 return 0;
2292 }
2293
2294 cpus = cpu_map__new_data(&event->cpu_map.data);
2295 if (!cpus)
2296 return -ENOMEM;
2297
2298 st->cpus = cpus;
2299 return set_maps(st);
2300}
2301
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002302static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002303 "perf stat report [<options>]",
2304 NULL,
2305};
2306
2307static struct perf_stat perf_stat = {
2308 .tool = {
2309 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002310 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002311 .thread_map = process_thread_map_event,
2312 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002313 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002314 .stat = perf_event__process_stat_event,
2315 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002316 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002317 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002318};
2319
2320static int __cmd_report(int argc, const char **argv)
2321{
2322 struct perf_session *session;
2323 const struct option options[] = {
2324 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002325 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2326 "aggregate counts per processor socket", AGGR_SOCKET),
2327 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2328 "aggregate counts per physical processor core", AGGR_CORE),
2329 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2330 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002331 OPT_END()
2332 };
2333 struct stat st;
2334 int ret;
2335
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002336 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002337
2338 if (!input_name || !strlen(input_name)) {
2339 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2340 input_name = "-";
2341 else
2342 input_name = "perf.data";
2343 }
2344
2345 perf_stat.file.path = input_name;
2346 perf_stat.file.mode = PERF_DATA_MODE_READ;
2347
2348 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2349 if (session == NULL)
2350 return -1;
2351
2352 perf_stat.session = session;
2353 stat_config.output = stderr;
2354 evsel_list = session->evlist;
2355
2356 ret = perf_session__process_events(session);
2357 if (ret)
2358 return ret;
2359
2360 perf_session__delete(session);
2361 return 0;
2362}
2363
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002364int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02002365{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002366 const char * const stat_usage[] = {
2367 "perf stat [<options>] [<command>]",
2368 NULL
2369 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002370 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002371 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002372 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002373 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002374 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002375
Stephane Eranian5af52b52010-05-18 15:00:01 +02002376 setlocale(LC_ALL, "");
2377
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002378 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002379 if (evsel_list == NULL)
2380 return -ENOMEM;
2381
Wang Nan1669e502016-02-19 11:43:58 +00002382 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002383 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2384 (const char **) stat_usage,
2385 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002386 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002387
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002388 if (csv_sep) {
2389 csv_output = true;
2390 if (!strcmp(csv_sep, "\\t"))
2391 csv_sep = "\t";
2392 } else
2393 csv_sep = DEFAULT_SEPARATOR;
2394
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002395 if (argc && !strncmp(argv[0], "rec", 3)) {
2396 argc = __cmd_record(argc, argv);
2397 if (argc < 0)
2398 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002399 } else if (argc && !strncmp(argv[0], "rep", 3))
2400 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002401
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002402 interval = stat_config.interval;
2403
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002404 /*
2405 * For record command the -o is already taken care of.
2406 */
2407 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002408 output = NULL;
2409
Jim Cromie56f3bae2011-09-07 17:14:00 -06002410 if (output_name && output_fd) {
2411 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002412 parse_options_usage(stat_usage, stat_options, "o", 1);
2413 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002414 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002415 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002416
Andi Kleen54b50912016-03-03 15:57:36 -08002417 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2418 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2419 goto out;
2420 }
2421
Andi Kleen54b50912016-03-03 15:57:36 -08002422 if (metric_only && run_count > 1) {
2423 fprintf(stderr, "--metric-only is not supported with -r\n");
2424 goto out;
2425 }
2426
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002427 if (output_fd < 0) {
2428 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002429 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002430 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002431 }
2432
Stephane Eranian4aa90152011-08-15 22:22:33 +02002433 if (!output) {
2434 struct timespec tm;
2435 mode = append_file ? "a" : "w";
2436
2437 output = fopen(output_name, mode);
2438 if (!output) {
2439 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002440 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002441 }
2442 clock_gettime(CLOCK_REALTIME, &tm);
2443 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002444 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002445 mode = append_file ? "a" : "w";
2446 output = fdopen(output_fd, mode);
2447 if (!output) {
2448 perror("Failed opening logfd");
2449 return -errno;
2450 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002451 }
2452
Jiri Olsa58215222015-07-21 14:31:24 +02002453 stat_config.output = output;
2454
Stephane Eraniand7470b62010-12-01 18:49:05 +02002455 /*
2456 * let the spreadsheet do the pretty-printing
2457 */
2458 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002459 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002460 if (big_num_opt == 1) {
2461 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002462 parse_options_usage(stat_usage, stat_options, "B", 1);
2463 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002464 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002465 } else /* Nope, so disable big number formatting */
2466 big_num = false;
2467 } else if (big_num_opt == 0) /* User passed --no-big-num */
2468 big_num = false;
2469
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002470 if (!argc && target__none(&target))
Jiri Olsae0547312015-11-05 15:40:45 +01002471 usage_with_options(stat_usage, stat_options);
David Ahernac3063b2013-09-30 07:37:37 -06002472
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002473 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002474 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002475 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002476 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002477 } else if (run_count == 0) {
2478 forever = true;
2479 run_count = 1;
2480 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002481
Jiri Olsa421a50f2015-07-21 14:31:22 +02002482 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002483 fprintf(stderr, "The --per-thread option is only available "
2484 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002485 parse_options_usage(NULL, stat_options, "p", 1);
2486 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002487 goto out;
2488 }
2489
2490 /*
2491 * no_aggr, cgroup are for system-wide only
2492 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2493 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002494 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2495 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002496 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002497 fprintf(stderr, "both cgroup and no-aggregation "
2498 "modes only available in system-wide mode\n");
2499
Jiri Olsae0547312015-11-05 15:40:45 +01002500 parse_options_usage(stat_usage, stat_options, "G", 1);
2501 parse_options_usage(NULL, stat_options, "A", 1);
2502 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002503 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002504 }
2505
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002506 if (add_default_attributes())
2507 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002508
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002509 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002510
Namhyung Kim77a6f012012-05-07 14:09:04 +09002511 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002512 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002513 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002514 parse_options_usage(stat_usage, stat_options, "p", 1);
2515 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002516 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002517 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002518 parse_options_usage(stat_usage, stat_options, "C", 1);
2519 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002520 }
2521 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002522 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002523
2524 /*
2525 * Initialize thread_map with comm names,
2526 * so we could print it out on output.
2527 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002528 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02002529 thread_map__read_comms(evsel_list->threads);
2530
Stephane Eranian13370a92013-01-29 12:47:44 +01002531 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002532 if (interval < 10) {
2533 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002534 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002535 goto out;
2536 } else
2537 pr_warning("print interval < 100ms. "
2538 "The overhead percentage could be high in some cases. "
2539 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002540 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002541
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002542 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002543 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002544
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002545 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002546 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002547
Ingo Molnar58d7e992009-05-15 11:03:23 +02002548 /*
2549 * We dont want to block the signals - that would cause
2550 * child tasks to inherit that and Ctrl-C would not work.
2551 * What we want is for Ctrl-C to work in the exec()-ed
2552 * task, but being ignored by perf stat itself:
2553 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002554 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002555 if (!forever)
2556 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002557 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002558 signal(SIGALRM, skip_signal);
2559 signal(SIGABRT, skip_signal);
2560
Ingo Molnar42202dd2009-06-13 14:57:28 +02002561 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002562 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Ingo Molnar42202dd2009-06-13 14:57:28 +02002563 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002564 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2565 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002566
Srikar Dronamraju375cf9a2019-09-04 15:17:37 +05302567 if (run_idx != 0)
2568 perf_evlist__reset_prev_raw_counts(evsel_list);
2569
Ingo Molnar42202dd2009-06-13 14:57:28 +02002570 status = run_perf_stat(argc, argv);
Srikar Dronamraju94505b62019-09-04 15:17:38 +05302571 if (forever && status != -1 && !interval) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002572 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc2015-06-26 11:29:13 +02002573 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002574 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002575 }
2576
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002577 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002578 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002579
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002580 if (STAT_RECORD) {
2581 /*
2582 * We synthesize the kernel mmap record just so that older tools
2583 * don't emit warnings about not being able to resolve symbols
2584 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2585 * a saner message about no samples being in the perf.data file.
2586 *
2587 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002588 * in header.c at the moment 'perf stat record' gets introduced, which
2589 * is not really needed once we start adding the stat specific PERF_RECORD_
2590 * records, but the need to suppress the kptr_restrict messages in older
2591 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002592 */
2593 int fd = perf_data_file__fd(&perf_stat.file);
2594 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2595 process_synthesized_event,
2596 &perf_stat.session->machines.host);
2597 if (err) {
2598 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2599 "older tools may produce warnings about this file\n.");
2600 }
2601
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002602 if (!interval) {
2603 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2604 pr_err("failed to write stat round event\n");
2605 }
2606
Jiri Olsa664c98d2015-11-05 15:40:50 +01002607 if (!perf_stat.file.is_pipe) {
2608 perf_stat.session->header.data_size += perf_stat.bytes_written;
2609 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2610 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002611
2612 perf_session__delete(perf_stat.session);
2613 }
2614
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002615 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002616 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002617out:
2618 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002619 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002620}