blob: 5c13a0f40adc04ae6abba9e06ecd4d60222a2d01 [file] [log] [blame]
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-stat.c
3 *
4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
6 *
7 * Sample output:
Ingo Molnarddcacfa2009-04-20 15:37:32 +02008
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02009 $ perf stat ./hackbench 10
Ingo Molnarddcacfa2009-04-20 15:37:32 +020010
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020011 Time: 0.118
Ingo Molnarddcacfa2009-04-20 15:37:32 +020012
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020013 Performance counter stats for './hackbench 10':
Ingo Molnarddcacfa2009-04-20 15:37:32 +020014
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020015 1708.761321 task-clock # 11.037 CPUs utilized
16 41,190 context-switches # 0.024 M/sec
17 6,735 CPU-migrations # 0.004 M/sec
18 17,318 page-faults # 0.010 M/sec
19 5,205,202,243 cycles # 3.046 GHz
20 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
21 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
22 2,603,501,247 instructions # 0.50 insns per cycle
23 # 1.48 stalled cycles per insn
24 484,357,498 branches # 283.455 M/sec
25 6,388,934 branch-misses # 1.32% of all branches
26
27 0.154822978 seconds time elapsed
Ingo Molnarddcacfa2009-04-20 15:37:32 +020028
Ingo Molnar52425192009-05-26 09:17:18 +020029 *
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020030 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
Ingo Molnar52425192009-05-26 09:17:18 +020031 *
32 * Improvements and fixes by:
33 *
34 * Arjan van de Ven <arjan@linux.intel.com>
35 * Yanmin Zhang <yanmin.zhang@intel.com>
36 * Wu Fengguang <fengguang.wu@intel.com>
37 * Mike Galbraith <efault@gmx.de>
38 * Paul Mackerras <paulus@samba.org>
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +053039 * Jaswinder Singh Rajput <jaswinder@kernel.org>
Ingo Molnar52425192009-05-26 09:17:18 +020040 *
41 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnarddcacfa2009-04-20 15:37:32 +020042 */
43
Peter Zijlstra1a482f32009-05-23 18:28:58 +020044#include "perf.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +020045#include "builtin.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030046#include "util/cgroup.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020047#include "util/util.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060048#include <subcmd/parse-options.h>
Ingo Molnar52425192009-05-26 09:17:18 +020049#include "util/parse-events.h"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070050#include "util/pmu.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020051#include "util/event.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020052#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020053#include "util/evsel.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020054#include "util/debug.h"
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -060055#include "util/drv_configs.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020056#include "util/color.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080057#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080058#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110059#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030060#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020061#include "util/thread_map.h"
Jiri Olsad8095602015-08-07 12:51:03 +020062#include "util/counts.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030063#include "util/group.h"
Jiri Olsa4979d0c2015-11-05 15:40:46 +010064#include "util/session.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010065#include "util/tool.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030066#include "util/group.h"
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 Petkov02d492e2017-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 Olsa254ecbc72015-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);
314 if (perf_evsel__read(counter, cpu, thread, count))
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100315 return -1;
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100316
317 if (STAT_RECORD) {
318 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
319 pr_err("failed to write stat event\n");
320 return -1;
321 }
322 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700323
324 if (verbose > 1) {
325 fprintf(stat_config.output,
326 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
327 perf_evsel__name(counter),
328 cpu,
329 count->val, count->ena, count->run);
330 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100331 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200332 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200333
334 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200335}
336
Mark Rutland3df33ef2016-08-09 14:04:29 +0100337static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200338{
339 struct perf_evsel *counter;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200340
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300341 evlist__for_each_entry(evsel_list, counter) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200342 if (read_counter(counter))
Andi Kleen245bad82015-09-01 15:52:46 -0700343 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200344
Jiri Olsaf80010e2015-07-21 14:31:27 +0200345 if (perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200346 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200347 }
348}
349
Jiri Olsaba411a92015-06-26 11:29:24 +0200350static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100351{
Stephane Eranian13370a92013-01-29 12:47:44 +0100352 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100353
Mark Rutland3df33ef2016-08-09 14:04:29 +0100354 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100355
Stephane Eranian13370a92013-01-29 12:47:44 +0100356 clock_gettime(CLOCK_MONOTONIC, &ts);
357 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100358
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100359 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300360 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100361 pr_err("failed to write stat round event\n");
362 }
363
Jiri Olsad4f63a42015-06-26 11:29:26 +0200364 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100365}
366
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100367static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700368{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100369 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300370 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100371
372 /*
373 * We need to enable counters only if:
374 * - we don't have tracee (attaching to task or cpu)
375 * - we have initial delay configured
376 */
377 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100378 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700379}
380
Mark Rutland3df33ef2016-08-09 14:04:29 +0100381static void disable_counters(void)
382{
383 /*
384 * If we don't have tracee (attaching to task or cpu), counters may
385 * still be running. To get accurate group ratios, we must stop groups
386 * from counting before reading their constituent counters.
387 */
388 if (!target__none(&target))
389 perf_evlist__disable(evsel_list);
390}
391
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300392static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300393
394/*
395 * perf_evlist__prepare_workload will send a SIGUSR1
396 * if the fork fails, since we asked by setting its
397 * want_signal to true.
398 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300399static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
400 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300401{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300402 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300403}
404
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100405static bool has_unit(struct perf_evsel *counter)
406{
407 return counter->unit && *counter->unit;
408}
409
410static bool has_scale(struct perf_evsel *counter)
411{
412 return counter->scale != 1;
413}
414
Jiri Olsa664c98d2015-11-05 15:40:50 +0100415static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100416{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100417 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100418 int err;
419
Jiri Olsa664c98d2015-11-05 15:40:50 +0100420 if (is_pipe) {
421 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
422 process_synthesized_event);
423 if (err < 0) {
424 pr_err("Couldn't synthesize attrs.\n");
425 return err;
426 }
427 }
428
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100429 /*
430 * Synthesize other events stuff not carried within
431 * attr event - unit, scale, name
432 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300433 evlist__for_each_entry(evsel_list, counter) {
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100434 if (!counter->supported)
435 continue;
436
437 /*
438 * Synthesize unit and scale only if it's defined.
439 */
440 if (has_unit(counter)) {
441 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
442 if (err < 0) {
443 pr_err("Couldn't synthesize evsel unit.\n");
444 return err;
445 }
446 }
447
448 if (has_scale(counter)) {
449 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
450 if (err < 0) {
451 pr_err("Couldn't synthesize evsel scale.\n");
452 return err;
453 }
454 }
455
456 if (counter->own_cpus) {
457 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
458 if (err < 0) {
459 pr_err("Couldn't synthesize evsel scale.\n");
460 return err;
461 }
462 }
463
464 /*
465 * Name is needed only for pipe output,
466 * perf.data carries event names.
467 */
468 if (is_pipe) {
469 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
470 if (err < 0) {
471 pr_err("Couldn't synthesize evsel name.\n");
472 return err;
473 }
474 }
475 }
476
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100477 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
478 process_synthesized_event,
479 NULL);
480 if (err < 0) {
481 pr_err("Couldn't synthesize thread map.\n");
482 return err;
483 }
484
485 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
486 process_synthesized_event, NULL);
487 if (err < 0) {
488 pr_err("Couldn't synthesize thread map.\n");
489 return err;
490 }
491
492 err = perf_event__synthesize_stat_config(NULL, &stat_config,
493 process_synthesized_event, NULL);
494 if (err < 0) {
495 pr_err("Couldn't synthesize config.\n");
496 return err;
497 }
498
499 return 0;
500}
501
Jiri Olsa2af46462015-11-05 15:40:49 +0100502#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
503
504static int __store_counter_ids(struct perf_evsel *counter,
505 struct cpu_map *cpus,
506 struct thread_map *threads)
507{
508 int cpu, thread;
509
510 for (cpu = 0; cpu < cpus->nr; cpu++) {
511 for (thread = 0; thread < threads->nr; thread++) {
512 int fd = FD(counter, cpu, thread);
513
514 if (perf_evlist__id_add_fd(evsel_list, counter,
515 cpu, thread, fd) < 0)
516 return -1;
517 }
518 }
519
520 return 0;
521}
522
523static int store_counter_ids(struct perf_evsel *counter)
524{
525 struct cpu_map *cpus = counter->cpus;
526 struct thread_map *threads = counter->threads;
527
528 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
529 return -ENOMEM;
530
531 return __store_counter_ids(counter, cpus, threads);
532}
533
Namhyung Kimacf28922013-03-11 16:43:18 +0900534static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200535{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200536 int interval = stat_config.interval;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300537 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200538 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100539 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100540 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100541 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200542 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300543 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100544 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600545 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200546
Stephane Eranian13370a92013-01-29 12:47:44 +0100547 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300548 ts.tv_sec = interval / USEC_PER_MSEC;
549 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100550 } else {
551 ts.tv_sec = 1;
552 ts.tv_nsec = 0;
553 }
554
Liming Wang60666c62009-12-31 16:05:50 +0800555 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100556 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300557 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900558 perror("failed to prepare workload");
559 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800560 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900561 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000562 }
563
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200564 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300565 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200566
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300567 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300568try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100569 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600570 /*
571 * PPC returns ENXIO for HW counters until 2.6.37
572 * (behavior changed with commit b0a873e).
573 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100574 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600575 errno == ENOENT || errno == EOPNOTSUPP ||
576 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900577 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600578 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300579 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600580 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400581
582 if ((counter->leader != counter) ||
583 !(counter->leader->nr_members > 1))
584 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300585 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900586 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300587 ui__warning("%s\n", msg);
588 goto try_again;
589 }
Ingo Molnarede70292011-04-28 08:48:42 +0200590
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300591 perf_evsel__open_strerror(counter, &target,
592 errno, msg, sizeof(msg));
593 ui__error("%s\n", msg);
594
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200595 if (child_pid != -1)
596 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600597
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200598 return -1;
599 }
David Ahern2cee77c2011-05-30 08:55:59 -0600600 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100601
602 l = strlen(counter->unit);
603 if (l > unit_width)
604 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100605
606 if (STAT_RECORD && store_counter_ids(counter))
607 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300608 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200609
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300610 if (perf_evlist__apply_filters(evsel_list, &counter)) {
611 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
612 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300613 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100614 return -1;
615 }
616
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600617 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
618 error("failed to set config \"%s\" on event %s with %d (%s)\n",
619 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
620 str_error_r(errno, msg, sizeof(msg)));
621 return -1;
622 }
623
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100624 if (STAT_RECORD) {
625 int err, fd = perf_data_file__fd(&perf_stat.file);
626
Jiri Olsa664c98d2015-11-05 15:40:50 +0100627 if (is_pipe) {
628 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
629 } else {
630 err = perf_session__write_header(perf_stat.session, evsel_list,
631 fd, false);
632 }
633
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100634 if (err < 0)
635 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100636
Jiri Olsa664c98d2015-11-05 15:40:50 +0100637 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100638 if (err < 0)
639 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100640 }
641
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200642 /*
643 * Enable counters and exec the command:
644 */
645 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100646 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200647
Liming Wang60666c62009-12-31 16:05:50 +0800648 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900649 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100650 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900651
Stephane Eranian13370a92013-01-29 12:47:44 +0100652 if (interval) {
653 while (!waitpid(child_pid, &status, WNOHANG)) {
654 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200655 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100656 }
657 }
Liming Wang60666c62009-12-31 16:05:50 +0800658 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300659
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300660 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300661 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300662 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300663 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300664 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300665
Andi Kleen33e49ea2011-09-15 14:31:40 -0700666 if (WIFSIGNALED(status))
667 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800668 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100669 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100670 while (!done) {
671 nanosleep(&ts, NULL);
672 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200673 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100674 }
Liming Wang60666c62009-12-31 16:05:50 +0800675 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200676
Mark Rutland3df33ef2016-08-09 14:04:29 +0100677 disable_counters();
678
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200679 t1 = rdclock();
680
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200681 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200682
Mark Rutland3df33ef2016-08-09 14:04:29 +0100683 /*
684 * Closing a group leader splits the group, and as we only disable
685 * group leaders, results in remaining events becoming enabled. To
686 * avoid arbitrary skew, we must read all counters before closing any
687 * group leaders.
688 */
689 read_counters();
690 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200691
Ingo Molnar42202dd2009-06-13 14:57:28 +0200692 return WEXITSTATUS(status);
693}
694
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300695static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200696{
697 int ret;
698
699 if (pre_cmd) {
700 ret = system(pre_cmd);
701 if (ret)
702 return ret;
703 }
704
705 if (sync_run)
706 sync();
707
708 ret = __run_perf_stat(argc, argv);
709 if (ret)
710 return ret;
711
712 if (post_cmd) {
713 ret = system(post_cmd);
714 if (ret)
715 return ret;
716 }
717
718 return ret;
719}
720
Andi Kleend73515c2015-03-11 07:16:27 -0700721static void print_running(u64 run, u64 ena)
722{
723 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200724 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700725 csv_sep,
726 run,
727 csv_sep,
728 ena ? 100.0 * run / ena : 100.0);
729 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200730 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700731 }
732}
733
Ingo Molnarf99844c2011-04-27 05:35:39 +0200734static void print_noise_pct(double total, double avg)
735{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800736 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200737
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700738 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200739 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600740 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200741 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200742}
743
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200744static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200745{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200746 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200747
Peter Zijlstra849abde2009-09-04 18:23:38 +0200748 if (run_count == 1)
749 return;
750
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200751 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200752 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200753}
754
Stephane Eranian12c08a92013-02-14 13:57:29 +0100755static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200756{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200757 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100758 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200759 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100760 cpu_map__id_to_socket(id),
761 csv_output ? 0 : -8,
762 cpu_map__id_to_cpu(id),
763 csv_sep,
764 csv_output ? 0 : 4,
765 nr,
766 csv_sep);
767 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100768 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200769 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100770 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100771 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100772 csv_sep,
773 csv_output ? 0 : 4,
774 nr,
775 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100776 break;
777 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200778 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200779 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100780 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100781 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200782 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200783 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200784 csv_output ? 0 : 16,
785 thread_map__comm(evsel->threads, id),
786 csv_output ? 0 : -8,
787 thread_map__pid(evsel->threads, id),
788 csv_sep);
789 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100790 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200791 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100792 default:
793 break;
794 }
795}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200796
Andi Kleen140aead2016-01-30 09:06:49 -0800797struct outstate {
798 FILE *fh;
799 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800800 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800801 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800802 int id, nr;
803 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800804};
805
806#define METRIC_LEN 35
807
808static void new_line_std(void *ctx)
809{
810 struct outstate *os = ctx;
811
812 os->newline = true;
813}
814
815static void do_new_line_std(struct outstate *os)
816{
817 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800818 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800819 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800820 if (stat_config.aggr_mode == AGGR_NONE)
821 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800822 fprintf(os->fh, " ");
823}
824
825static void print_metric_std(void *ctx, const char *color, const char *fmt,
826 const char *unit, double val)
827{
828 struct outstate *os = ctx;
829 FILE *out = os->fh;
830 int n;
831 bool newline = os->newline;
832
833 os->newline = false;
834
835 if (unit == NULL || fmt == NULL) {
836 fprintf(out, "%-*s", METRIC_LEN, "");
837 return;
838 }
839
840 if (newline)
841 do_new_line_std(os);
842
843 n = fprintf(out, " # ");
844 if (color)
845 n += color_fprintf(out, color, fmt, val);
846 else
847 n += fprintf(out, fmt, val);
848 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
849}
850
Andi Kleen92a61f62016-02-29 14:36:21 -0800851static void new_line_csv(void *ctx)
852{
853 struct outstate *os = ctx;
854 int i;
855
856 fputc('\n', os->fh);
857 if (os->prefix)
858 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800859 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800860 for (i = 0; i < os->nfields; i++)
861 fputs(csv_sep, os->fh);
862}
863
864static void print_metric_csv(void *ctx,
865 const char *color __maybe_unused,
866 const char *fmt, const char *unit, double val)
867{
868 struct outstate *os = ctx;
869 FILE *out = os->fh;
870 char buf[64], *vals, *ends;
871
872 if (unit == NULL || fmt == NULL) {
873 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
874 return;
875 }
876 snprintf(buf, sizeof(buf), fmt, val);
877 vals = buf;
878 while (isspace(*vals))
879 vals++;
880 ends = vals;
881 while (isdigit(*ends) || *ends == '.')
882 ends++;
883 *ends = 0;
884 while (isspace(*unit))
885 unit++;
886 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
887}
888
Andi Kleen54b50912016-03-03 15:57:36 -0800889#define METRIC_ONLY_LEN 20
890
891/* Filter out some columns that don't work well in metrics only mode */
892
893static bool valid_only_metric(const char *unit)
894{
895 if (!unit)
896 return false;
897 if (strstr(unit, "/sec") ||
898 strstr(unit, "hz") ||
899 strstr(unit, "Hz") ||
900 strstr(unit, "CPUs utilized"))
901 return false;
902 return true;
903}
904
905static const char *fixunit(char *buf, struct perf_evsel *evsel,
906 const char *unit)
907{
908 if (!strncmp(unit, "of all", 6)) {
909 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
910 unit);
911 return buf;
912 }
913 return unit;
914}
915
916static void print_metric_only(void *ctx, const char *color, const char *fmt,
917 const char *unit, double val)
918{
919 struct outstate *os = ctx;
920 FILE *out = os->fh;
921 int n;
922 char buf[1024];
923 unsigned mlen = METRIC_ONLY_LEN;
924
925 if (!valid_only_metric(unit))
926 return;
927 unit = fixunit(buf, os->evsel, unit);
928 if (color)
929 n = color_fprintf(out, color, fmt, val);
930 else
931 n = fprintf(out, fmt, val);
932 if (n > METRIC_ONLY_LEN)
933 n = METRIC_ONLY_LEN;
934 if (mlen < strlen(unit))
935 mlen = strlen(unit) + 1;
936 fprintf(out, "%*s", mlen - n, "");
937}
938
939static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
940 const char *fmt,
941 const char *unit, double val)
942{
943 struct outstate *os = ctx;
944 FILE *out = os->fh;
945 char buf[64], *vals, *ends;
946 char tbuf[1024];
947
948 if (!valid_only_metric(unit))
949 return;
950 unit = fixunit(tbuf, os->evsel, unit);
951 snprintf(buf, sizeof buf, fmt, val);
952 vals = buf;
953 while (isspace(*vals))
954 vals++;
955 ends = vals;
956 while (isdigit(*ends) || *ends == '.')
957 ends++;
958 *ends = 0;
959 fprintf(out, "%s%s", vals, csv_sep);
960}
961
962static void new_line_metric(void *ctx __maybe_unused)
963{
964}
965
966static void print_metric_header(void *ctx, const char *color __maybe_unused,
967 const char *fmt __maybe_unused,
968 const char *unit, double val __maybe_unused)
969{
970 struct outstate *os = ctx;
971 char tbuf[1024];
972
973 if (!valid_only_metric(unit))
974 return;
975 unit = fixunit(tbuf, os->evsel, unit);
976 if (csv_output)
977 fprintf(os->fh, "%s%s", unit, csv_sep);
978 else
979 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
980}
981
Andi Kleenda88c7f2014-09-24 13:50:46 -0700982static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100983{
Jiri Olsa58215222015-07-21 14:31:24 +0200984 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300985 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +0100986 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -0600987 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100988
Stephane Eranian410136f2013-11-12 17:58:49 +0100989 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
990 fmt_n = csv_output ? "%s" : "%-25s";
991
Andi Kleenda88c7f2014-09-24 13:50:46 -0700992 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100993
David Ahern4bbe5a62013-09-28 14:28:00 -0600994 scnprintf(name, sizeof(name), "%s%s",
995 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +0100996
997 fprintf(output, fmt_v, msecs, csv_sep);
998
999 if (csv_output)
1000 fprintf(output, "%s%s", evsel->unit, csv_sep);
1001 else
1002 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1003
1004 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001005
Stephane Eranian023695d2011-02-14 11:20:01 +02001006 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001007 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001008}
1009
Andi Kleen44d49a62016-02-29 14:36:22 -08001010static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1011{
1012 int i;
1013
1014 if (!aggr_get_id)
1015 return 0;
1016
1017 if (stat_config.aggr_mode == AGGR_NONE)
1018 return id;
1019
1020 if (stat_config.aggr_mode == AGGR_GLOBAL)
1021 return 0;
1022
1023 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1024 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1025
1026 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1027 return cpu2;
1028 }
1029 return 0;
1030}
1031
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001032static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1033{
Jiri Olsa58215222015-07-21 14:31:24 +02001034 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001035 double sc = evsel->scale;
1036 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001037
1038 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001039 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001040 } else {
1041 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001042 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001043 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001044 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001045 }
1046
1047 aggr_printout(evsel, id, nr);
1048
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001049 fprintf(output, fmt, avg, csv_sep);
1050
1051 if (evsel->unit)
1052 fprintf(output, "%-*s%s",
1053 csv_output ? 0 : unit_width,
1054 evsel->unit, csv_sep);
1055
1056 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1057
1058 if (evsel->cgrp)
1059 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001060}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001061
Andi Kleenf9483392016-01-30 09:06:50 -08001062static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Andi Kleencb110f42016-01-30 09:06:51 -08001063 char *prefix, u64 run, u64 ena, double noise)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001064{
Andi Kleen140aead2016-01-30 09:06:49 -08001065 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001066 struct outstate os = {
1067 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001068 .prefix = prefix ? prefix : "",
1069 .id = id,
1070 .nr = nr,
1071 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001072 };
Andi Kleen140aead2016-01-30 09:06:49 -08001073 print_metric_t pm = print_metric_std;
1074 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001075
Andi Kleen54b50912016-03-03 15:57:36 -08001076 if (metric_only) {
1077 nl = new_line_metric;
1078 if (csv_output)
1079 pm = print_metric_only_csv;
1080 else
1081 pm = print_metric_only;
1082 } else
1083 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001084
Andi Kleen54b50912016-03-03 15:57:36 -08001085 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001086 static int aggr_fields[] = {
1087 [AGGR_GLOBAL] = 0,
1088 [AGGR_THREAD] = 1,
1089 [AGGR_NONE] = 1,
1090 [AGGR_SOCKET] = 2,
1091 [AGGR_CORE] = 2,
1092 };
1093
1094 pm = print_metric_csv;
1095 nl = new_line_csv;
1096 os.nfields = 3;
1097 os.nfields += aggr_fields[stat_config.aggr_mode];
1098 if (counter->cgrp)
1099 os.nfields++;
1100 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001101 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001102 if (metric_only) {
1103 pm(&os, NULL, "", "", 0);
1104 return;
1105 }
Andi Kleencb110f42016-01-30 09:06:51 -08001106 aggr_printout(counter, id, nr);
1107
1108 fprintf(stat_config.output, "%*s%s",
1109 csv_output ? 0 : 18,
1110 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1111 csv_sep);
1112
Borislav Petkov02d492e2017-02-07 01:40:05 +01001113 if (counter->supported)
1114 print_free_counters_hint = 1;
1115
Andi Kleencb110f42016-01-30 09:06:51 -08001116 fprintf(stat_config.output, "%-*s%s",
1117 csv_output ? 0 : unit_width,
1118 counter->unit, csv_sep);
1119
1120 fprintf(stat_config.output, "%*s",
1121 csv_output ? 0 : -25,
1122 perf_evsel__name(counter));
1123
1124 if (counter->cgrp)
1125 fprintf(stat_config.output, "%s%s",
1126 csv_sep, counter->cgrp->name);
1127
Andi Kleen92a61f62016-02-29 14:36:21 -08001128 if (!csv_output)
1129 pm(&os, NULL, NULL, "", 0);
1130 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001131 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001132 if (csv_output)
1133 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001134 return;
1135 }
1136
Andi Kleen54b50912016-03-03 15:57:36 -08001137 if (metric_only)
1138 /* nothing */;
1139 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001140 nsec_printout(id, nr, counter, uval);
1141 else
1142 abs_printout(id, nr, counter, uval);
1143
Andi Kleen140aead2016-01-30 09:06:49 -08001144 out.print_metric = pm;
1145 out.new_line = nl;
1146 out.ctx = &os;
1147
Andi Kleen54b50912016-03-03 15:57:36 -08001148 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001149 print_noise(counter, noise);
1150 print_running(run, ena);
1151 }
1152
1153 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001154 first_shadow_cpu(counter, id),
Andi Kleen140aead2016-01-30 09:06:49 -08001155 &out);
Andi Kleen54b50912016-03-03 15:57:36 -08001156 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001157 print_noise(counter, noise);
1158 print_running(run, ena);
1159 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001160}
1161
Andi Kleen44d49a62016-02-29 14:36:22 -08001162static void aggr_update_shadow(void)
1163{
1164 int cpu, s2, id, s;
1165 u64 val;
1166 struct perf_evsel *counter;
1167
1168 for (s = 0; s < aggr_map->nr; s++) {
1169 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001170 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001171 val = 0;
1172 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1173 s2 = aggr_get_id(evsel_list->cpus, cpu);
1174 if (s2 != id)
1175 continue;
1176 val += perf_counts(counter->counts, cpu, 0)->val;
1177 }
1178 val = val * counter->scale;
1179 perf_stat__update_shadow_stats(counter, &val,
1180 first_shadow_cpu(counter, id));
1181 }
1182 }
1183}
1184
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001185static void collect_data(struct perf_evsel *counter,
1186 void (*cb)(struct perf_evsel *counter, void *data,
1187 bool first),
1188 void *data)
1189{
1190 cb(counter, data, true);
1191}
1192
1193struct aggr_data {
1194 u64 ena, run, val;
1195 int id;
1196 int nr;
1197 int cpu;
1198};
1199
1200static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1201{
1202 struct aggr_data *ad = data;
1203 int cpu, s2;
1204
1205 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1206 struct perf_counts_values *counts;
1207
1208 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1209 if (s2 != ad->id)
1210 continue;
1211 if (first)
1212 ad->nr++;
1213 counts = perf_counts(counter->counts, cpu, 0);
1214 ad->val += counts->val;
1215 ad->ena += counts->ena;
1216 ad->run += counts->run;
1217 }
1218}
1219
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001220static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001221{
Jiri Olsa58215222015-07-21 14:31:24 +02001222 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001223 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001224 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001225 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001226 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001227 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001228
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001229 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001230 return;
1231
Andi Kleen44d49a62016-02-29 14:36:22 -08001232 aggr_update_shadow();
1233
Andi Kleen54b50912016-03-03 15:57:36 -08001234 /*
1235 * With metric_only everything is on a single line.
1236 * Without each counter has its own line.
1237 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001238 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001239 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001240 if (prefix && metric_only)
1241 fprintf(output, "%s", prefix);
1242
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001243 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001244 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001245 evlist__for_each_entry(evsel_list, counter) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001246 ad.val = ad.ena = ad.run = 0;
1247 ad.nr = 0;
1248 collect_data(counter, aggr_cb, &ad);
1249 nr = ad.nr;
1250 ena = ad.ena;
1251 run = ad.run;
1252 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001253 if (first && metric_only) {
1254 first = false;
1255 aggr_printout(counter, id, nr);
1256 }
1257 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001258 fprintf(output, "%s", prefix);
1259
Stephane Eranian410136f2013-11-12 17:58:49 +01001260 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001261 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
Andi Kleen54b50912016-03-03 15:57:36 -08001262 if (!metric_only)
1263 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001264 }
Andi Kleen54b50912016-03-03 15:57:36 -08001265 if (metric_only)
1266 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001267 }
1268}
1269
Jiri Olsa32b8af82015-06-26 11:29:27 +02001270static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1271{
Jiri Olsa58215222015-07-21 14:31:24 +02001272 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001273 int nthreads = thread_map__nr(counter->threads);
1274 int ncpus = cpu_map__nr(counter->cpus);
1275 int cpu, thread;
1276 double uval;
1277
1278 for (thread = 0; thread < nthreads; thread++) {
1279 u64 ena = 0, run = 0, val = 0;
1280
1281 for (cpu = 0; cpu < ncpus; cpu++) {
1282 val += perf_counts(counter->counts, cpu, thread)->val;
1283 ena += perf_counts(counter->counts, cpu, thread)->ena;
1284 run += perf_counts(counter->counts, cpu, thread)->run;
1285 }
1286
1287 if (prefix)
1288 fprintf(output, "%s", prefix);
1289
1290 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001291 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001292 fputc('\n', output);
1293 }
1294}
1295
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001296struct caggr_data {
1297 double avg, avg_enabled, avg_running;
1298};
1299
1300static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1301 bool first __maybe_unused)
1302{
1303 struct caggr_data *cd = data;
1304 struct perf_stat_evsel *ps = counter->priv;
1305
1306 cd->avg += avg_stats(&ps->res_stats[0]);
1307 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1308 cd->avg_running += avg_stats(&ps->res_stats[2]);
1309}
1310
Ingo Molnar42202dd2009-06-13 14:57:28 +02001311/*
1312 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001313 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001314 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001315static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001316{
Jiri Olsa58215222015-07-21 14:31:24 +02001317 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001318 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001319 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001320
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001321 collect_data(counter, counter_aggr_cb, &cd);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001322
Andi Kleen54b50912016-03-03 15:57:36 -08001323 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001324 fprintf(output, "%s", prefix);
1325
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001326 uval = cd.avg * counter->scale;
1327 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, cd.avg);
Andi Kleen54b50912016-03-03 15:57:36 -08001328 if (!metric_only)
1329 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001330}
1331
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001332static void counter_cb(struct perf_evsel *counter, void *data,
1333 bool first __maybe_unused)
1334{
1335 struct aggr_data *ad = data;
1336
1337 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1338 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1339 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1340}
1341
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001342/*
1343 * Print out the results of a single counter:
1344 * does not use aggregated count in system-wide
1345 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001346static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001347{
Jiri Olsa58215222015-07-21 14:31:24 +02001348 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001349 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001350 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001351 int cpu;
1352
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001353 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001354 struct aggr_data ad = { .cpu = cpu };
1355
1356 collect_data(counter, counter_cb, &ad);
1357 val = ad.val;
1358 ena = ad.ena;
1359 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001360
1361 if (prefix)
1362 fprintf(output, "%s", prefix);
1363
Stephane Eranian410136f2013-11-12 17:58:49 +01001364 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001365 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001366
Stephane Eranian4aa90152011-08-15 22:22:33 +02001367 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001368 }
1369}
1370
Andi Kleen206cab62016-03-03 15:57:37 -08001371static void print_no_aggr_metric(char *prefix)
1372{
1373 int cpu;
1374 int nrcpus = 0;
1375 struct perf_evsel *counter;
1376 u64 ena, run, val;
1377 double uval;
1378
1379 nrcpus = evsel_list->cpus->nr;
1380 for (cpu = 0; cpu < nrcpus; cpu++) {
1381 bool first = true;
1382
1383 if (prefix)
1384 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001385 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen206cab62016-03-03 15:57:37 -08001386 if (first) {
1387 aggr_printout(counter, cpu, 0);
1388 first = false;
1389 }
1390 val = perf_counts(counter->counts, cpu, 0)->val;
1391 ena = perf_counts(counter->counts, cpu, 0)->ena;
1392 run = perf_counts(counter->counts, cpu, 0)->run;
1393
1394 uval = val * counter->scale;
1395 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1396 }
1397 fputc('\n', stat_config.output);
1398 }
1399}
1400
Andi Kleen54b50912016-03-03 15:57:36 -08001401static int aggr_header_lens[] = {
1402 [AGGR_CORE] = 18,
1403 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001404 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001405 [AGGR_THREAD] = 24,
1406 [AGGR_GLOBAL] = 0,
1407};
1408
Andi Kleenc51fd632016-05-24 12:52:39 -07001409static const char *aggr_header_csv[] = {
1410 [AGGR_CORE] = "core,cpus,",
1411 [AGGR_SOCKET] = "socket,cpus",
1412 [AGGR_NONE] = "cpu,",
1413 [AGGR_THREAD] = "comm-pid,",
1414 [AGGR_GLOBAL] = ""
1415};
1416
Andi Kleen41c8ca22016-05-24 12:52:38 -07001417static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001418{
1419 struct perf_stat_output_ctx out;
1420 struct perf_evsel *counter;
1421 struct outstate os = {
1422 .fh = stat_config.output
1423 };
1424
1425 if (prefix)
1426 fprintf(stat_config.output, "%s", prefix);
1427
Andi Kleen41c8ca22016-05-24 12:52:38 -07001428 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001429 fprintf(stat_config.output, "%*s",
1430 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001431 if (csv_output) {
1432 if (stat_config.interval)
1433 fputs("time,", stat_config.output);
1434 fputs(aggr_header_csv[stat_config.aggr_mode],
1435 stat_config.output);
1436 }
Andi Kleen54b50912016-03-03 15:57:36 -08001437
1438 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001439 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen54b50912016-03-03 15:57:36 -08001440 os.evsel = counter;
1441 out.ctx = &os;
1442 out.print_metric = print_metric_header;
1443 out.new_line = new_line_metric;
1444 os.evsel = counter;
1445 perf_stat__print_shadow_stats(counter, 0,
1446 0,
1447 &out);
1448 }
1449 fputc('\n', stat_config.output);
1450}
1451
Jiri Olsad4f63a42015-06-26 11:29:26 +02001452static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001453{
Jiri Olsa58215222015-07-21 14:31:24 +02001454 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001455 static int num_print_interval;
1456
1457 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1458
Andi Kleen41c8ca22016-05-24 12:52:38 -07001459 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001460 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001461 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001462 fprintf(output, "# time socket cpus");
1463 if (!metric_only)
1464 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001465 break;
1466 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001467 fprintf(output, "# time core cpus");
1468 if (!metric_only)
1469 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001470 break;
1471 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001472 fprintf(output, "# time CPU");
1473 if (!metric_only)
1474 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001475 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001476 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001477 fprintf(output, "# time comm-pid");
1478 if (!metric_only)
1479 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001480 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001481 case AGGR_GLOBAL:
1482 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001483 fprintf(output, "# time");
1484 if (!metric_only)
1485 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001486 case AGGR_UNSET:
1487 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001488 }
1489 }
1490
Andi Kleen41c8ca22016-05-24 12:52:38 -07001491 if (num_print_interval == 0 && metric_only)
1492 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001493 if (++num_print_interval == 25)
1494 num_print_interval = 0;
1495}
1496
1497static void print_header(int argc, const char **argv)
1498{
Jiri Olsa58215222015-07-21 14:31:24 +02001499 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001500 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001501
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001502 fflush(stdout);
1503
Stephane Eraniand7470b62010-12-01 18:49:05 +02001504 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001505 fprintf(output, "\n");
1506 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001507 if (target.system_wide)
1508 fprintf(output, "\'system wide");
1509 else if (target.cpu_list)
1510 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001511 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001512 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1513 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001514 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001515 } else if (target.pid)
1516 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001517 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001518 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001519
Stephane Eranian4aa90152011-08-15 22:22:33 +02001520 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001521 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001522 fprintf(output, " (%d runs)", run_count);
1523 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001524 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001525}
1526
1527static void print_footer(void)
1528{
Jiri Olsa58215222015-07-21 14:31:24 +02001529 FILE *output = stat_config.output;
1530
Jiri Olsad4f63a42015-06-26 11:29:26 +02001531 if (!null_run)
1532 fprintf(output, "\n");
1533 fprintf(output, " %17.9f seconds time elapsed",
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001534 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001535 if (run_count > 1) {
1536 fprintf(output, " ");
1537 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1538 avg_stats(&walltime_nsecs_stats));
1539 }
1540 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001541
1542 if (print_free_counters_hint)
1543 fprintf(output,
1544"Some events weren't counted. Try disabling the NMI watchdog:\n"
1545" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1546" perf stat ...\n"
1547" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001548}
1549
1550static void print_counters(struct timespec *ts, int argc, const char **argv)
1551{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001552 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001553 struct perf_evsel *counter;
1554 char buf[64], *prefix = NULL;
1555
Jiri Olsa664c98d2015-11-05 15:40:50 +01001556 /* Do not print anything if we record to the pipe. */
1557 if (STAT_RECORD && perf_stat.file.is_pipe)
1558 return;
1559
Jiri Olsad4f63a42015-06-26 11:29:26 +02001560 if (interval)
1561 print_interval(prefix = buf, ts);
1562 else
1563 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001564
Andi Kleen54b50912016-03-03 15:57:36 -08001565 if (metric_only) {
1566 static int num_print_iv;
1567
Andi Kleen41c8ca22016-05-24 12:52:38 -07001568 if (num_print_iv == 0 && !interval)
1569 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001570 if (num_print_iv++ == 25)
1571 num_print_iv = 0;
1572 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1573 fprintf(stat_config.output, "%s", prefix);
1574 }
1575
Jiri Olsa421a50f2015-07-21 14:31:22 +02001576 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001577 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001578 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001579 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001580 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001581 case AGGR_THREAD:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001582 evlist__for_each_entry(evsel_list, counter)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001583 print_aggr_thread(counter, prefix);
1584 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001585 case AGGR_GLOBAL:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001586 evlist__for_each_entry(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001587 print_counter_aggr(counter, prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001588 if (metric_only)
1589 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001590 break;
1591 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001592 if (metric_only)
1593 print_no_aggr_metric(prefix);
1594 else {
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001595 evlist__for_each_entry(evsel_list, counter)
Andi Kleen206cab62016-03-03 15:57:37 -08001596 print_counter(counter, prefix);
1597 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001598 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001599 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001600 default:
1601 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001602 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001603
Jiri Olsad4f63a42015-06-26 11:29:26 +02001604 if (!interval && !csv_output)
1605 print_footer();
1606
Jiri Olsa58215222015-07-21 14:31:24 +02001607 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001608}
1609
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001610static volatile int signr = -1;
1611
Ingo Molnar52425192009-05-26 09:17:18 +02001612static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001613{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001614 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001615 done = 1;
1616
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001617 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001618 /*
1619 * render child_pid harmless
1620 * won't send SIGTERM to a random
1621 * process in case of race condition
1622 * and fast PID recycling
1623 */
1624 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001625}
1626
1627static void sig_atexit(void)
1628{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001629 sigset_t set, oset;
1630
1631 /*
1632 * avoid race condition with SIGCHLD handler
1633 * in skip_signal() which is modifying child_pid
1634 * goal is to avoid send SIGTERM to a random
1635 * process
1636 */
1637 sigemptyset(&set);
1638 sigaddset(&set, SIGCHLD);
1639 sigprocmask(SIG_BLOCK, &set, &oset);
1640
Chris Wilson933da832009-10-04 01:35:01 +01001641 if (child_pid != -1)
1642 kill(child_pid, SIGTERM);
1643
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001644 sigprocmask(SIG_SETMASK, &oset, NULL);
1645
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001646 if (signr == -1)
1647 return;
1648
1649 signal(signr, SIG_DFL);
1650 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001651}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001652
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001653static int stat__set_big_num(const struct option *opt __maybe_unused,
1654 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001655{
1656 big_num_opt = unset ? 0 : 1;
1657 return 0;
1658}
1659
Andi Kleen44b1e602016-05-30 12:49:42 -03001660static int enable_metric_only(const struct option *opt __maybe_unused,
1661 const char *s __maybe_unused, int unset)
1662{
1663 force_metric_only = true;
1664 metric_only = !unset;
1665 return 0;
1666}
1667
Jiri Olsae0547312015-11-05 15:40:45 +01001668static const struct option stat_options[] = {
1669 OPT_BOOLEAN('T', "transaction", &transaction_run,
1670 "hardware transaction statistics"),
1671 OPT_CALLBACK('e', "event", &evsel_list, "event",
1672 "event selector. use 'perf list' to list available events",
1673 parse_events_option),
1674 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1675 "event filter", parse_filter),
1676 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1677 "child tasks do not inherit counters"),
1678 OPT_STRING('p', "pid", &target.pid, "pid",
1679 "stat events on existing process id"),
1680 OPT_STRING('t', "tid", &target.tid, "tid",
1681 "stat events on existing thread id"),
1682 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1683 "system-wide collection from all CPUs"),
1684 OPT_BOOLEAN('g', "group", &group,
1685 "put the counters into a counter group"),
1686 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1687 OPT_INCR('v', "verbose", &verbose,
1688 "be more verbose (show counter open errors, etc)"),
1689 OPT_INTEGER('r', "repeat", &run_count,
1690 "repeat command and print average + stddev (max: 100, forever: 0)"),
1691 OPT_BOOLEAN('n', "null", &null_run,
1692 "null run - dont start any counters"),
1693 OPT_INCR('d', "detailed", &detailed_run,
1694 "detailed run - start a lot of events"),
1695 OPT_BOOLEAN('S', "sync", &sync_run,
1696 "call sync() before starting a run"),
1697 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1698 "print large numbers with thousands\' separators",
1699 stat__set_big_num),
1700 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1701 "list of cpus to monitor in system-wide"),
1702 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1703 "disable CPU count aggregation", AGGR_NONE),
1704 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1705 "print counts with custom separator"),
1706 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1707 "monitor event in cgroup name only", parse_cgroups),
1708 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1709 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1710 OPT_INTEGER(0, "log-fd", &output_fd,
1711 "log output to fd, instead of stderr"),
1712 OPT_STRING(0, "pre", &pre_cmd, "command",
1713 "command to run prior to the measured command"),
1714 OPT_STRING(0, "post", &post_cmd, "command",
1715 "command to run after to the measured command"),
1716 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1717 "print counts at regular interval in ms (>= 10)"),
1718 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1719 "aggregate counts per processor socket", AGGR_SOCKET),
1720 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1721 "aggregate counts per physical processor core", AGGR_CORE),
1722 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1723 "aggregate counts per thread", AGGR_THREAD),
1724 OPT_UINTEGER('D', "delay", &initial_delay,
1725 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001726 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1727 "Only print computed metrics. No raw values", enable_metric_only),
1728 OPT_BOOLEAN(0, "topdown", &topdown_run,
1729 "measure topdown level 1 statistics"),
Jiri Olsae0547312015-11-05 15:40:45 +01001730 OPT_END()
1731};
1732
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001733static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1734{
1735 return cpu_map__get_socket(map, cpu, NULL);
1736}
1737
1738static int perf_stat__get_core(struct cpu_map *map, int cpu)
1739{
1740 return cpu_map__get_core(map, cpu, NULL);
1741}
1742
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001743static int cpu_map__get_max(struct cpu_map *map)
1744{
1745 int i, max = -1;
1746
1747 for (i = 0; i < map->nr; i++) {
1748 if (map->map[i] > max)
1749 max = map->map[i];
1750 }
1751
1752 return max;
1753}
1754
1755static struct cpu_map *cpus_aggr_map;
1756
1757static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1758{
1759 int cpu;
1760
1761 if (idx >= map->nr)
1762 return -1;
1763
1764 cpu = map->map[idx];
1765
1766 if (cpus_aggr_map->map[cpu] == -1)
1767 cpus_aggr_map->map[cpu] = get_id(map, idx);
1768
1769 return cpus_aggr_map->map[cpu];
1770}
1771
1772static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1773{
1774 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1775}
1776
1777static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1778{
1779 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1780}
1781
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001782static int perf_stat_init_aggr_mode(void)
1783{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001784 int nr;
1785
Jiri Olsa421a50f2015-07-21 14:31:22 +02001786 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001787 case AGGR_SOCKET:
1788 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1789 perror("cannot build socket map");
1790 return -1;
1791 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001792 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001793 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001794 case AGGR_CORE:
1795 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1796 perror("cannot build core map");
1797 return -1;
1798 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001799 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001800 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001801 case AGGR_NONE:
1802 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001803 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001804 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001805 default:
1806 break;
1807 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001808
1809 /*
1810 * The evsel_list->cpus is the base we operate on,
1811 * taking the highest cpu number to be the size of
1812 * the aggregation translate cpumap.
1813 */
1814 nr = cpu_map__get_max(evsel_list->cpus);
1815 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1816 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001817}
1818
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001819static void perf_stat__exit_aggr_mode(void)
1820{
1821 cpu_map__put(aggr_map);
1822 cpu_map__put(cpus_aggr_map);
1823 aggr_map = NULL;
1824 cpus_aggr_map = NULL;
1825}
1826
Jiri Olsa68d702f2015-11-05 15:40:58 +01001827static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1828{
1829 int cpu;
1830
1831 if (idx > map->nr)
1832 return -1;
1833
1834 cpu = map->map[idx];
1835
Jan Stancekda8a58b2017-02-17 12:10:26 +01001836 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001837 return -1;
1838
1839 return cpu;
1840}
1841
1842static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1843{
1844 struct perf_env *env = data;
1845 int cpu = perf_env__get_cpu(env, map, idx);
1846
1847 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1848}
1849
1850static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1851{
1852 struct perf_env *env = data;
1853 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1854
1855 if (cpu != -1) {
1856 int socket_id = env->cpu[cpu].socket_id;
1857
1858 /*
1859 * Encode socket in upper 16 bits
1860 * core_id is relative to socket, and
1861 * we need a global id. So we combine
1862 * socket + core id.
1863 */
1864 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1865 }
1866
1867 return core;
1868}
1869
1870static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1871 struct cpu_map **sockp)
1872{
1873 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1874}
1875
1876static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1877 struct cpu_map **corep)
1878{
1879 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1880}
1881
1882static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1883{
1884 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1885}
1886
1887static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1888{
1889 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1890}
1891
1892static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1893{
1894 struct perf_env *env = &st->session->header.env;
1895
1896 switch (stat_config.aggr_mode) {
1897 case AGGR_SOCKET:
1898 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1899 perror("cannot build socket map");
1900 return -1;
1901 }
1902 aggr_get_id = perf_stat__get_socket_file;
1903 break;
1904 case AGGR_CORE:
1905 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1906 perror("cannot build core map");
1907 return -1;
1908 }
1909 aggr_get_id = perf_stat__get_core_file;
1910 break;
1911 case AGGR_NONE:
1912 case AGGR_GLOBAL:
1913 case AGGR_THREAD:
1914 case AGGR_UNSET:
1915 default:
1916 break;
1917 }
1918
1919 return 0;
1920}
1921
Andi Kleen44b1e602016-05-30 12:49:42 -03001922static int topdown_filter_events(const char **attr, char **str, bool use_group)
1923{
1924 int off = 0;
1925 int i;
1926 int len = 0;
1927 char *s;
1928
1929 for (i = 0; attr[i]; i++) {
1930 if (pmu_have_event("cpu", attr[i])) {
1931 len += strlen(attr[i]) + 1;
1932 attr[i - off] = attr[i];
1933 } else
1934 off++;
1935 }
1936 attr[i - off] = NULL;
1937
1938 *str = malloc(len + 1 + 2);
1939 if (!*str)
1940 return -1;
1941 s = *str;
1942 if (i - off == 0) {
1943 *s = 0;
1944 return 0;
1945 }
1946 if (use_group)
1947 *s++ = '{';
1948 for (i = 0; attr[i]; i++) {
1949 strcpy(s, attr[i]);
1950 s += strlen(s);
1951 *s++ = ',';
1952 }
1953 if (use_group) {
1954 s[-1] = '}';
1955 *s = 0;
1956 } else
1957 s[-1] = 0;
1958 return 0;
1959}
1960
1961__weak bool arch_topdown_check_group(bool *warn)
1962{
1963 *warn = false;
1964 return false;
1965}
1966
1967__weak void arch_topdown_group_warn(void)
1968{
1969}
1970
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001971/*
1972 * Add default attributes, if there were no attributes specified or
1973 * if -d/--detailed, -d -d or -d -d -d is used:
1974 */
1975static int add_default_attributes(void)
1976{
Andi Kleen44b1e602016-05-30 12:49:42 -03001977 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08001978 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001979
1980 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1981 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1982 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1983 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1984
1985 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08001986};
1987 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001988 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001989};
1990 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001991 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08001992};
1993 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001994 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1995 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1996 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1997
1998};
1999
2000/*
2001 * Detailed stats (-d), covering the L1 and last level data caches:
2002 */
2003 struct perf_event_attr detailed_attrs[] = {
2004
2005 { .type = PERF_TYPE_HW_CACHE,
2006 .config =
2007 PERF_COUNT_HW_CACHE_L1D << 0 |
2008 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2009 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2010
2011 { .type = PERF_TYPE_HW_CACHE,
2012 .config =
2013 PERF_COUNT_HW_CACHE_L1D << 0 |
2014 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2015 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2016
2017 { .type = PERF_TYPE_HW_CACHE,
2018 .config =
2019 PERF_COUNT_HW_CACHE_LL << 0 |
2020 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2021 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2022
2023 { .type = PERF_TYPE_HW_CACHE,
2024 .config =
2025 PERF_COUNT_HW_CACHE_LL << 0 |
2026 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2027 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2028};
2029
2030/*
2031 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2032 */
2033 struct perf_event_attr very_detailed_attrs[] = {
2034
2035 { .type = PERF_TYPE_HW_CACHE,
2036 .config =
2037 PERF_COUNT_HW_CACHE_L1I << 0 |
2038 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2039 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2040
2041 { .type = PERF_TYPE_HW_CACHE,
2042 .config =
2043 PERF_COUNT_HW_CACHE_L1I << 0 |
2044 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2045 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2046
2047 { .type = PERF_TYPE_HW_CACHE,
2048 .config =
2049 PERF_COUNT_HW_CACHE_DTLB << 0 |
2050 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2051 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2052
2053 { .type = PERF_TYPE_HW_CACHE,
2054 .config =
2055 PERF_COUNT_HW_CACHE_DTLB << 0 |
2056 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2057 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2058
2059 { .type = PERF_TYPE_HW_CACHE,
2060 .config =
2061 PERF_COUNT_HW_CACHE_ITLB << 0 |
2062 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2063 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2064
2065 { .type = PERF_TYPE_HW_CACHE,
2066 .config =
2067 PERF_COUNT_HW_CACHE_ITLB << 0 |
2068 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2069 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2070
2071};
2072
2073/*
2074 * Very, very detailed stats (-d -d -d), adding prefetch events:
2075 */
2076 struct perf_event_attr very_very_detailed_attrs[] = {
2077
2078 { .type = PERF_TYPE_HW_CACHE,
2079 .config =
2080 PERF_COUNT_HW_CACHE_L1D << 0 |
2081 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2082 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2083
2084 { .type = PERF_TYPE_HW_CACHE,
2085 .config =
2086 PERF_COUNT_HW_CACHE_L1D << 0 |
2087 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2088 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2089};
2090
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002091 /* Set attrs if no event is selected and !null_run: */
2092 if (null_run)
2093 return 0;
2094
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002095 if (transaction_run) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002096 if (pmu_have_event("cpu", "cycles-ct") &&
2097 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02002098 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002099 else
Jiri Olsaa4547422015-06-03 16:25:53 +02002100 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2101 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002102 fprintf(stderr, "Cannot set up transaction events\n");
2103 return -1;
2104 }
2105 return 0;
2106 }
2107
Andi Kleen44b1e602016-05-30 12:49:42 -03002108 if (topdown_run) {
2109 char *str = NULL;
2110 bool warn = false;
2111
2112 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2113 stat_config.aggr_mode != AGGR_CORE) {
2114 pr_err("top down event configuration requires --per-core mode\n");
2115 return -1;
2116 }
2117 stat_config.aggr_mode = AGGR_CORE;
2118 if (nr_cgroups || !target__has_cpu(&target)) {
2119 pr_err("top down event configuration requires system-wide mode (-a)\n");
2120 return -1;
2121 }
2122
2123 if (!force_metric_only)
2124 metric_only = true;
2125 if (topdown_filter_events(topdown_attrs, &str,
2126 arch_topdown_check_group(&warn)) < 0) {
2127 pr_err("Out of memory\n");
2128 return -1;
2129 }
2130 if (topdown_attrs[0] && str) {
2131 if (warn)
2132 arch_topdown_group_warn();
2133 err = parse_events(evsel_list, str, NULL);
2134 if (err) {
2135 fprintf(stderr,
2136 "Cannot set up top down events %s: %d\n",
2137 str, err);
2138 free(str);
2139 return -1;
2140 }
2141 } else {
2142 fprintf(stderr, "System does not support topdown\n");
2143 return -1;
2144 }
2145 free(str);
2146 }
2147
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002148 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002149 if (target__has_cpu(&target))
2150 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2151
Andi Kleen9dec4472016-02-26 16:27:56 -08002152 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2153 return -1;
2154 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2155 if (perf_evlist__add_default_attrs(evsel_list,
2156 frontend_attrs) < 0)
2157 return -1;
2158 }
2159 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2160 if (perf_evlist__add_default_attrs(evsel_list,
2161 backend_attrs) < 0)
2162 return -1;
2163 }
2164 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002165 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002166 }
2167
2168 /* Detailed events get appended to the event list: */
2169
2170 if (detailed_run < 1)
2171 return 0;
2172
2173 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002174 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002175 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002176
2177 if (detailed_run < 2)
2178 return 0;
2179
2180 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002181 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002182 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002183
2184 if (detailed_run < 3)
2185 return 0;
2186
2187 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002188 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002189}
2190
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002191static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002192 "perf stat record [<options>]",
2193 NULL,
2194};
2195
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002196static void init_features(struct perf_session *session)
2197{
2198 int feat;
2199
2200 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2201 perf_header__set_feat(&session->header, feat);
2202
2203 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2204 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2205 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2206 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2207}
2208
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002209static int __cmd_record(int argc, const char **argv)
2210{
2211 struct perf_session *session;
2212 struct perf_data_file *file = &perf_stat.file;
2213
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002214 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002215 PARSE_OPT_STOP_AT_NON_OPTION);
2216
2217 if (output_name)
2218 file->path = output_name;
2219
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002220 if (run_count != 1 || forever) {
2221 pr_err("Cannot use -r option with perf stat record.\n");
2222 return -1;
2223 }
2224
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002225 session = perf_session__new(file, false, NULL);
2226 if (session == NULL) {
2227 pr_err("Perf session creation failed.\n");
2228 return -1;
2229 }
2230
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002231 init_features(session);
2232
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002233 session->evlist = evsel_list;
2234 perf_stat.session = session;
2235 perf_stat.record = true;
2236 return argc;
2237}
2238
Jiri Olsaa56f9392015-11-05 15:40:59 +01002239static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2240 union perf_event *event,
2241 struct perf_session *session)
2242{
Andi Kleene3b03b62016-05-05 16:04:03 -07002243 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002244 struct perf_evsel *counter;
2245 struct timespec tsh, *ts = NULL;
2246 const char **argv = session->header.env.cmdline_argv;
2247 int argc = session->header.env.nr_cmdline;
2248
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002249 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002250 perf_stat_process_counter(&stat_config, counter);
2251
Andi Kleene3b03b62016-05-05 16:04:03 -07002252 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2253 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002254
Andi Kleene3b03b62016-05-05 16:04:03 -07002255 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002256 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2257 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002258 ts = &tsh;
2259 }
2260
2261 print_counters(ts, argc, argv);
2262 return 0;
2263}
2264
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002265static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002266int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002267 union perf_event *event,
2268 struct perf_session *session __maybe_unused)
2269{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002270 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2271
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002272 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002273
Jiri Olsa89af4e02015-11-05 15:41:02 +01002274 if (cpu_map__empty(st->cpus)) {
2275 if (st->aggr_mode != AGGR_UNSET)
2276 pr_warning("warning: processing task data, aggregation mode not set\n");
2277 return 0;
2278 }
2279
2280 if (st->aggr_mode != AGGR_UNSET)
2281 stat_config.aggr_mode = st->aggr_mode;
2282
Jiri Olsa68d702f2015-11-05 15:40:58 +01002283 if (perf_stat.file.is_pipe)
2284 perf_stat_init_aggr_mode();
2285 else
2286 perf_stat_init_aggr_mode_file(st);
2287
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002288 return 0;
2289}
2290
Jiri Olsa1975d362015-11-05 15:40:56 +01002291static int set_maps(struct perf_stat *st)
2292{
2293 if (!st->cpus || !st->threads)
2294 return 0;
2295
2296 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2297 return -EINVAL;
2298
2299 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2300
2301 if (perf_evlist__alloc_stats(evsel_list, true))
2302 return -ENOMEM;
2303
2304 st->maps_allocated = true;
2305 return 0;
2306}
2307
2308static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002309int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002310 union perf_event *event,
2311 struct perf_session *session __maybe_unused)
2312{
2313 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2314
2315 if (st->threads) {
2316 pr_warning("Extra thread map event, ignoring.\n");
2317 return 0;
2318 }
2319
2320 st->threads = thread_map__new_event(&event->thread_map);
2321 if (!st->threads)
2322 return -ENOMEM;
2323
2324 return set_maps(st);
2325}
2326
2327static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002328int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002329 union perf_event *event,
2330 struct perf_session *session __maybe_unused)
2331{
2332 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2333 struct cpu_map *cpus;
2334
2335 if (st->cpus) {
2336 pr_warning("Extra cpu map event, ignoring.\n");
2337 return 0;
2338 }
2339
2340 cpus = cpu_map__new_data(&event->cpu_map.data);
2341 if (!cpus)
2342 return -ENOMEM;
2343
2344 st->cpus = cpus;
2345 return set_maps(st);
2346}
2347
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002348static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002349 "perf stat report [<options>]",
2350 NULL,
2351};
2352
2353static struct perf_stat perf_stat = {
2354 .tool = {
2355 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002356 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002357 .thread_map = process_thread_map_event,
2358 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002359 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002360 .stat = perf_event__process_stat_event,
2361 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002362 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002363 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002364};
2365
2366static int __cmd_report(int argc, const char **argv)
2367{
2368 struct perf_session *session;
2369 const struct option options[] = {
2370 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002371 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2372 "aggregate counts per processor socket", AGGR_SOCKET),
2373 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2374 "aggregate counts per physical processor core", AGGR_CORE),
2375 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2376 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002377 OPT_END()
2378 };
2379 struct stat st;
2380 int ret;
2381
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002382 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002383
2384 if (!input_name || !strlen(input_name)) {
2385 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2386 input_name = "-";
2387 else
2388 input_name = "perf.data";
2389 }
2390
2391 perf_stat.file.path = input_name;
2392 perf_stat.file.mode = PERF_DATA_MODE_READ;
2393
2394 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2395 if (session == NULL)
2396 return -1;
2397
2398 perf_stat.session = session;
2399 stat_config.output = stderr;
2400 evsel_list = session->evlist;
2401
2402 ret = perf_session__process_events(session);
2403 if (ret)
2404 return ret;
2405
2406 perf_session__delete(session);
2407 return 0;
2408}
2409
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002410static void setup_system_wide(int forks)
2411{
2412 /*
2413 * Make system wide (-a) the default target if
2414 * no target was specified and one of following
2415 * conditions is met:
2416 *
2417 * - there's no workload specified
2418 * - there is workload specified but all requested
2419 * events are system wide events
2420 */
2421 if (!target__none(&target))
2422 return;
2423
2424 if (!forks)
2425 target.system_wide = true;
2426 else {
2427 struct perf_evsel *counter;
2428
2429 evlist__for_each_entry(evsel_list, counter) {
2430 if (!counter->system_wide)
2431 return;
2432 }
2433
2434 if (evsel_list->nr_entries)
2435 target.system_wide = true;
2436 }
2437}
2438
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002439int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02002440{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002441 const char * const stat_usage[] = {
2442 "perf stat [<options>] [<command>]",
2443 NULL
2444 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002445 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002446 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002447 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002448 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002449 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002450
Stephane Eranian5af52b52010-05-18 15:00:01 +02002451 setlocale(LC_ALL, "");
2452
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002453 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002454 if (evsel_list == NULL)
2455 return -ENOMEM;
2456
Wang Nan1669e502016-02-19 11:43:58 +00002457 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002458 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2459 (const char **) stat_usage,
2460 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002461 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002462
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002463 if (csv_sep) {
2464 csv_output = true;
2465 if (!strcmp(csv_sep, "\\t"))
2466 csv_sep = "\t";
2467 } else
2468 csv_sep = DEFAULT_SEPARATOR;
2469
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002470 if (argc && !strncmp(argv[0], "rec", 3)) {
2471 argc = __cmd_record(argc, argv);
2472 if (argc < 0)
2473 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002474 } else if (argc && !strncmp(argv[0], "rep", 3))
2475 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002476
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002477 interval = stat_config.interval;
2478
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002479 /*
2480 * For record command the -o is already taken care of.
2481 */
2482 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002483 output = NULL;
2484
Jim Cromie56f3bae2011-09-07 17:14:00 -06002485 if (output_name && output_fd) {
2486 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002487 parse_options_usage(stat_usage, stat_options, "o", 1);
2488 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002489 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002490 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002491
Andi Kleen54b50912016-03-03 15:57:36 -08002492 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2493 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2494 goto out;
2495 }
2496
Andi Kleen54b50912016-03-03 15:57:36 -08002497 if (metric_only && run_count > 1) {
2498 fprintf(stderr, "--metric-only is not supported with -r\n");
2499 goto out;
2500 }
2501
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002502 if (output_fd < 0) {
2503 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002504 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002505 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002506 }
2507
Stephane Eranian4aa90152011-08-15 22:22:33 +02002508 if (!output) {
2509 struct timespec tm;
2510 mode = append_file ? "a" : "w";
2511
2512 output = fopen(output_name, mode);
2513 if (!output) {
2514 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002515 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002516 }
2517 clock_gettime(CLOCK_REALTIME, &tm);
2518 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002519 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002520 mode = append_file ? "a" : "w";
2521 output = fdopen(output_fd, mode);
2522 if (!output) {
2523 perror("Failed opening logfd");
2524 return -errno;
2525 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002526 }
2527
Jiri Olsa58215222015-07-21 14:31:24 +02002528 stat_config.output = output;
2529
Stephane Eraniand7470b62010-12-01 18:49:05 +02002530 /*
2531 * let the spreadsheet do the pretty-printing
2532 */
2533 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002534 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002535 if (big_num_opt == 1) {
2536 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002537 parse_options_usage(stat_usage, stat_options, "B", 1);
2538 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002539 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002540 } else /* Nope, so disable big number formatting */
2541 big_num = false;
2542 } else if (big_num_opt == 0) /* User passed --no-big-num */
2543 big_num = false;
2544
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002545 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002546
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002547 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002548 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002549 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002550 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002551 } else if (run_count == 0) {
2552 forever = true;
2553 run_count = 1;
2554 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002555
Jiri Olsa421a50f2015-07-21 14:31:22 +02002556 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002557 fprintf(stderr, "The --per-thread option is only available "
2558 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002559 parse_options_usage(NULL, stat_options, "p", 1);
2560 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002561 goto out;
2562 }
2563
2564 /*
2565 * no_aggr, cgroup are for system-wide only
2566 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2567 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002568 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2569 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002570 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002571 fprintf(stderr, "both cgroup and no-aggregation "
2572 "modes only available in system-wide mode\n");
2573
Jiri Olsae0547312015-11-05 15:40:45 +01002574 parse_options_usage(stat_usage, stat_options, "G", 1);
2575 parse_options_usage(NULL, stat_options, "A", 1);
2576 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002577 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002578 }
2579
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002580 if (add_default_attributes())
2581 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002582
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002583 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002584
Namhyung Kim77a6f012012-05-07 14:09:04 +09002585 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002586 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002587 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002588 parse_options_usage(stat_usage, stat_options, "p", 1);
2589 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002590 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002591 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002592 parse_options_usage(stat_usage, stat_options, "C", 1);
2593 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002594 }
2595 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002596 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002597
2598 /*
2599 * Initialize thread_map with comm names,
2600 * so we could print it out on output.
2601 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002602 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02002603 thread_map__read_comms(evsel_list->threads);
2604
Stephane Eranian13370a92013-01-29 12:47:44 +01002605 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002606 if (interval < 10) {
2607 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002608 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002609 goto out;
2610 } else
2611 pr_warning("print interval < 100ms. "
2612 "The overhead percentage could be high in some cases. "
2613 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002614 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002615
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002616 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002617 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002618
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002619 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002620 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002621
Ingo Molnar58d7e992009-05-15 11:03:23 +02002622 /*
2623 * We dont want to block the signals - that would cause
2624 * child tasks to inherit that and Ctrl-C would not work.
2625 * What we want is for Ctrl-C to work in the exec()-ed
2626 * task, but being ignored by perf stat itself:
2627 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002628 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002629 if (!forever)
2630 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002631 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002632 signal(SIGALRM, skip_signal);
2633 signal(SIGABRT, skip_signal);
2634
Ingo Molnar42202dd2009-06-13 14:57:28 +02002635 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002636 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002637 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002638 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2639 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002640
Ingo Molnar42202dd2009-06-13 14:57:28 +02002641 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002642 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002643 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002644 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002645 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002646 }
2647
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002648 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002649 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002650
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002651 if (STAT_RECORD) {
2652 /*
2653 * We synthesize the kernel mmap record just so that older tools
2654 * don't emit warnings about not being able to resolve symbols
2655 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2656 * a saner message about no samples being in the perf.data file.
2657 *
2658 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002659 * in header.c at the moment 'perf stat record' gets introduced, which
2660 * is not really needed once we start adding the stat specific PERF_RECORD_
2661 * records, but the need to suppress the kptr_restrict messages in older
2662 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002663 */
2664 int fd = perf_data_file__fd(&perf_stat.file);
2665 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2666 process_synthesized_event,
2667 &perf_stat.session->machines.host);
2668 if (err) {
2669 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2670 "older tools may produce warnings about this file\n.");
2671 }
2672
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002673 if (!interval) {
2674 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2675 pr_err("failed to write stat round event\n");
2676 }
2677
Jiri Olsa664c98d2015-11-05 15:40:50 +01002678 if (!perf_stat.file.is_pipe) {
2679 perf_stat.session->header.data_size += perf_stat.bytes_written;
2680 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2681 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002682
2683 perf_session__delete(perf_stat.session);
2684 }
2685
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002686 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002687 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002688out:
2689 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002690 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002691}