blob: 324363054c3fe1b8380acba5899d75473626a189 [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"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030067#include "util/string2.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010068#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020069
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030070#include <linux/time64.h>
Andi Kleen44b1e602016-05-30 12:49:42 -030071#include <api/fs/fs.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030072#include <errno.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030073#include <signal.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020074#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020075#include <sys/prctl.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030076#include <inttypes.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020077#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070078#include <math.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030079#include <sys/types.h>
80#include <sys/stat.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030081#include <sys/wait.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030082#include <unistd.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020083
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030084#include "sane_ctype.h"
85
Stephane Eraniand7470b62010-12-01 18:49:05 +020086#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060087#define CNTR_NOT_SUPPORTED "<not supported>"
88#define CNTR_NOT_COUNTED "<not counted>"
Kan Liangdaefd0b2017-05-26 12:05:38 -070089#define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
Stephane Eraniand7470b62010-12-01 18:49:05 +020090
Jiri Olsad4f63a42015-06-26 11:29:26 +020091static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010092
Andi Kleen4cabc3d2013-08-21 16:47:26 -070093/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020094static const char *transaction_attrs = {
95 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070096 "{"
97 "instructions,"
98 "cycles,"
99 "cpu/cycles-t/,"
100 "cpu/tx-start/,"
101 "cpu/el-start/,"
102 "cpu/cycles-ct/"
103 "}"
104};
105
106/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +0200107static const char * transaction_limited_attrs = {
108 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700109 "{"
110 "instructions,"
111 "cycles,"
112 "cpu/cycles-t/,"
113 "cpu/tx-start/"
114 "}"
115};
116
Andi Kleen44b1e602016-05-30 12:49:42 -0300117static const char * topdown_attrs[] = {
118 "topdown-total-slots",
119 "topdown-slots-retired",
120 "topdown-recovery-bubbles",
121 "topdown-fetch-bubbles",
122 "topdown-slots-issued",
123 NULL,
124};
125
Kan Liangdaefd0b2017-05-26 12:05:38 -0700126static const char *smi_cost_attrs = {
127 "{"
128 "msr/aperf/,"
129 "msr/smi/,"
130 "cycles"
131 "}"
132};
133
Robert Richter666e6d42012-04-05 18:26:27 +0200134static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200135
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300136static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900137 .uid = UINT_MAX,
138};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530139
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100140typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
141
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530142static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200143static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200144static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000145static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200146static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700147static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300148static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700149static bool smi_cost = false;
150static bool smi_reset = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200151static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200152static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200153static const char *csv_sep = NULL;
154static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800155static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200156static const char *pre_cmd = NULL;
157static const char *post_cmd = NULL;
158static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700159static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100160static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500161static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800162static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300163static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700164static bool no_merge = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100165static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100166static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100167static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100168static bool append_file;
169static const char *output_name;
170static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100171static int print_free_counters_hint;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200172
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100173struct perf_stat {
174 bool record;
175 struct perf_data_file file;
176 struct perf_session *session;
177 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100178 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100179 bool maps_allocated;
180 struct cpu_map *cpus;
181 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100182 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100183};
184
185static struct perf_stat perf_stat;
186#define STAT_RECORD perf_stat.record
187
Liming Wang60666c62009-12-31 16:05:50 +0800188static volatile int done = 0;
189
Jiri Olsa421a50f2015-07-21 14:31:22 +0200190static struct perf_stat_config stat_config = {
191 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200192 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200193};
194
Stephane Eranian13370a92013-01-29 12:47:44 +0100195static inline void diff_timespec(struct timespec *r, struct timespec *a,
196 struct timespec *b)
197{
198 r->tv_sec = a->tv_sec - b->tv_sec;
199 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300200 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100201 r->tv_sec--;
202 } else {
203 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
204 }
205}
206
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200207static void perf_stat__reset_stats(void)
208{
209 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200210 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200211}
212
Jiri Olsacac21422012-11-12 18:34:00 +0100213static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200214{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200215 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200216
Jiri Olsa711a5722015-07-21 14:31:23 +0200217 if (stat_config.scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200218 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
219 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200220
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200221 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300222
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100223 /*
224 * Some events get initialized with sample_(period/type) set,
225 * like tracepoints. Clear it up for counting.
226 */
227 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100228
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100229 /*
230 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
231 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100232 *
233 * However for pipe sessions we need to keep it zero,
234 * because script's perf_evsel__check_attr is triggered
235 * by attr->sample_type != 0, and we can't run it on
236 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100237 */
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100238 if (!(STAT_RECORD && perf_stat.file.is_pipe))
239 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100240
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100241 /*
242 * Disabling all counters initially, they will be enabled
243 * either manually by us or by kernel via enable_on_exec
244 * set later.
245 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100246 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100247 attr->disabled = 1;
248
Jiri Olsac8280ce2015-12-03 10:06:45 +0100249 /*
250 * In case of initial_delay we enable tracee
251 * events manually.
252 */
253 if (target__none(&target) && !initial_delay)
254 attr->enable_on_exec = 1;
255 }
256
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300257 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300258 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200259
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300260 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200261}
262
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200263/*
264 * Does the counter have nsecs as a unit?
265 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200266static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200267{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200268 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
269 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200270 return 1;
271
272 return 0;
273}
274
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100275static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100276 union perf_event *event,
277 struct perf_sample *sample __maybe_unused,
278 struct machine *machine __maybe_unused)
279{
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100280 if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) {
281 pr_err("failed to write perf data, error: %m\n");
282 return -1;
283 }
284
285 perf_stat.bytes_written += event->header.size;
286 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100287}
288
Jiri Olsa1975d362015-11-05 15:40:56 +0100289static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100290{
Jiri Olsa1975d362015-11-05 15:40:56 +0100291 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100292 process_synthesized_event,
293 NULL);
294}
295
296#define WRITE_STAT_ROUND_EVENT(time, interval) \
297 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
298
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100299#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
300
301static int
302perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
303 struct perf_counts_values *count)
304{
305 struct perf_sample_id *sid = SID(counter, cpu, thread);
306
307 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
308 process_synthesized_event, NULL);
309}
310
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200311/*
312 * Read out the results of a single counter:
313 * do not aggregate counts across CPUs in system-wide mode
314 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200315static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200316{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100317 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100318 int ncpus, cpu, thread;
319
320 if (target__has_cpu(&target))
321 ncpus = perf_evsel__nr_cpus(counter);
322 else
323 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200324
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000325 if (!counter->supported)
326 return -ENOENT;
327
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100328 if (counter->system_wide)
329 nthreads = 1;
330
331 for (thread = 0; thread < nthreads; thread++) {
332 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200333 struct perf_counts_values *count;
334
335 count = perf_counts(counter->counts, cpu, thread);
Stephane Eraniandb49a712017-04-12 11:23:01 -0700336 if (perf_evsel__read(counter, cpu, thread, count)) {
337 counter->counts->scaled = -1;
338 perf_counts(counter->counts, cpu, thread)->ena = 0;
339 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100340 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700341 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100342
343 if (STAT_RECORD) {
344 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
345 pr_err("failed to write stat event\n");
346 return -1;
347 }
348 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700349
350 if (verbose > 1) {
351 fprintf(stat_config.output,
352 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
353 perf_evsel__name(counter),
354 cpu,
355 count->val, count->ena, count->run);
356 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100357 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200358 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200359
360 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200361}
362
Mark Rutland3df33ef2016-08-09 14:04:29 +0100363static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200364{
365 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700366 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200367
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300368 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700369 ret = read_counter(counter);
370 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700371 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200372
Stephane Eraniandb49a712017-04-12 11:23:01 -0700373 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200374 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200375 }
376}
377
Jiri Olsaba411a92015-06-26 11:29:24 +0200378static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100379{
Stephane Eranian13370a92013-01-29 12:47:44 +0100380 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100381
Mark Rutland3df33ef2016-08-09 14:04:29 +0100382 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100383
Stephane Eranian13370a92013-01-29 12:47:44 +0100384 clock_gettime(CLOCK_MONOTONIC, &ts);
385 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100386
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100387 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300388 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100389 pr_err("failed to write stat round event\n");
390 }
391
Jiri Olsad4f63a42015-06-26 11:29:26 +0200392 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100393}
394
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100395static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700396{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100397 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300398 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100399
400 /*
401 * We need to enable counters only if:
402 * - we don't have tracee (attaching to task or cpu)
403 * - we have initial delay configured
404 */
405 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100406 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700407}
408
Mark Rutland3df33ef2016-08-09 14:04:29 +0100409static void disable_counters(void)
410{
411 /*
412 * If we don't have tracee (attaching to task or cpu), counters may
413 * still be running. To get accurate group ratios, we must stop groups
414 * from counting before reading their constituent counters.
415 */
416 if (!target__none(&target))
417 perf_evlist__disable(evsel_list);
418}
419
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300420static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300421
422/*
423 * perf_evlist__prepare_workload will send a SIGUSR1
424 * if the fork fails, since we asked by setting its
425 * want_signal to true.
426 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300427static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
428 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300429{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300430 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300431}
432
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100433static bool has_unit(struct perf_evsel *counter)
434{
435 return counter->unit && *counter->unit;
436}
437
438static bool has_scale(struct perf_evsel *counter)
439{
440 return counter->scale != 1;
441}
442
Jiri Olsa664c98d2015-11-05 15:40:50 +0100443static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100444{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100445 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100446 int err;
447
Jiri Olsa664c98d2015-11-05 15:40:50 +0100448 if (is_pipe) {
449 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
450 process_synthesized_event);
451 if (err < 0) {
452 pr_err("Couldn't synthesize attrs.\n");
453 return err;
454 }
455 }
456
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100457 /*
458 * Synthesize other events stuff not carried within
459 * attr event - unit, scale, name
460 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300461 evlist__for_each_entry(evsel_list, counter) {
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100462 if (!counter->supported)
463 continue;
464
465 /*
466 * Synthesize unit and scale only if it's defined.
467 */
468 if (has_unit(counter)) {
469 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
470 if (err < 0) {
471 pr_err("Couldn't synthesize evsel unit.\n");
472 return err;
473 }
474 }
475
476 if (has_scale(counter)) {
477 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
478 if (err < 0) {
479 pr_err("Couldn't synthesize evsel scale.\n");
480 return err;
481 }
482 }
483
484 if (counter->own_cpus) {
485 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
486 if (err < 0) {
487 pr_err("Couldn't synthesize evsel scale.\n");
488 return err;
489 }
490 }
491
492 /*
493 * Name is needed only for pipe output,
494 * perf.data carries event names.
495 */
496 if (is_pipe) {
497 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
498 if (err < 0) {
499 pr_err("Couldn't synthesize evsel name.\n");
500 return err;
501 }
502 }
503 }
504
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100505 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
506 process_synthesized_event,
507 NULL);
508 if (err < 0) {
509 pr_err("Couldn't synthesize thread map.\n");
510 return err;
511 }
512
513 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
514 process_synthesized_event, NULL);
515 if (err < 0) {
516 pr_err("Couldn't synthesize thread map.\n");
517 return err;
518 }
519
520 err = perf_event__synthesize_stat_config(NULL, &stat_config,
521 process_synthesized_event, NULL);
522 if (err < 0) {
523 pr_err("Couldn't synthesize config.\n");
524 return err;
525 }
526
527 return 0;
528}
529
Jiri Olsa2af46462015-11-05 15:40:49 +0100530#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
531
532static int __store_counter_ids(struct perf_evsel *counter,
533 struct cpu_map *cpus,
534 struct thread_map *threads)
535{
536 int cpu, thread;
537
538 for (cpu = 0; cpu < cpus->nr; cpu++) {
539 for (thread = 0; thread < threads->nr; thread++) {
540 int fd = FD(counter, cpu, thread);
541
542 if (perf_evlist__id_add_fd(evsel_list, counter,
543 cpu, thread, fd) < 0)
544 return -1;
545 }
546 }
547
548 return 0;
549}
550
551static int store_counter_ids(struct perf_evsel *counter)
552{
553 struct cpu_map *cpus = counter->cpus;
554 struct thread_map *threads = counter->threads;
555
556 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
557 return -ENOMEM;
558
559 return __store_counter_ids(counter, cpus, threads);
560}
561
Namhyung Kimacf28922013-03-11 16:43:18 +0900562static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200563{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200564 int interval = stat_config.interval;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300565 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200566 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100567 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100568 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100569 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200570 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300571 const bool forks = (argc > 0);
Jiri Olsa664c98d2015-11-05 15:40:50 +0100572 bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600573 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200574
Stephane Eranian13370a92013-01-29 12:47:44 +0100575 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300576 ts.tv_sec = interval / USEC_PER_MSEC;
577 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100578 } else {
579 ts.tv_sec = 1;
580 ts.tv_nsec = 0;
581 }
582
Liming Wang60666c62009-12-31 16:05:50 +0800583 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100584 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300585 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900586 perror("failed to prepare workload");
587 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800588 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900589 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000590 }
591
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200592 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300593 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200594
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300595 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300596try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100597 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600598 /*
599 * PPC returns ENXIO for HW counters until 2.6.37
600 * (behavior changed with commit b0a873e).
601 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100602 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600603 errno == ENOENT || errno == EOPNOTSUPP ||
604 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900605 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600606 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300607 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600608 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400609
610 if ((counter->leader != counter) ||
611 !(counter->leader->nr_members > 1))
612 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300613 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900614 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300615 ui__warning("%s\n", msg);
616 goto try_again;
617 }
Ingo Molnarede70292011-04-28 08:48:42 +0200618
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300619 perf_evsel__open_strerror(counter, &target,
620 errno, msg, sizeof(msg));
621 ui__error("%s\n", msg);
622
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200623 if (child_pid != -1)
624 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600625
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200626 return -1;
627 }
David Ahern2cee77c2011-05-30 08:55:59 -0600628 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100629
630 l = strlen(counter->unit);
631 if (l > unit_width)
632 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100633
634 if (STAT_RECORD && store_counter_ids(counter))
635 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300636 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200637
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300638 if (perf_evlist__apply_filters(evsel_list, &counter)) {
639 error("failed to set filter \"%s\" on event %s with %d (%s)\n",
640 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300641 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100642 return -1;
643 }
644
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600645 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
646 error("failed to set config \"%s\" on event %s with %d (%s)\n",
647 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
648 str_error_r(errno, msg, sizeof(msg)));
649 return -1;
650 }
651
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100652 if (STAT_RECORD) {
653 int err, fd = perf_data_file__fd(&perf_stat.file);
654
Jiri Olsa664c98d2015-11-05 15:40:50 +0100655 if (is_pipe) {
656 err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file));
657 } else {
658 err = perf_session__write_header(perf_stat.session, evsel_list,
659 fd, false);
660 }
661
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100662 if (err < 0)
663 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100664
Jiri Olsa664c98d2015-11-05 15:40:50 +0100665 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100666 if (err < 0)
667 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100668 }
669
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200670 /*
671 * Enable counters and exec the command:
672 */
673 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100674 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200675
Liming Wang60666c62009-12-31 16:05:50 +0800676 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900677 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100678 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900679
Stephane Eranian13370a92013-01-29 12:47:44 +0100680 if (interval) {
681 while (!waitpid(child_pid, &status, WNOHANG)) {
682 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200683 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100684 }
685 }
Liming Wang60666c62009-12-31 16:05:50 +0800686 wait(&status);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300687
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300688 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300689 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300690 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300691 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300692 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300693
Andi Kleen33e49ea2011-09-15 14:31:40 -0700694 if (WIFSIGNALED(status))
695 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800696 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100697 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100698 while (!done) {
699 nanosleep(&ts, NULL);
700 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200701 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100702 }
Liming Wang60666c62009-12-31 16:05:50 +0800703 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200704
Mark Rutland3df33ef2016-08-09 14:04:29 +0100705 disable_counters();
706
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200707 t1 = rdclock();
708
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200709 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200710
Mark Rutland3df33ef2016-08-09 14:04:29 +0100711 /*
712 * Closing a group leader splits the group, and as we only disable
713 * group leaders, results in remaining events becoming enabled. To
714 * avoid arbitrary skew, we must read all counters before closing any
715 * group leaders.
716 */
717 read_counters();
718 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200719
Ingo Molnar42202dd2009-06-13 14:57:28 +0200720 return WEXITSTATUS(status);
721}
722
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300723static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200724{
725 int ret;
726
727 if (pre_cmd) {
728 ret = system(pre_cmd);
729 if (ret)
730 return ret;
731 }
732
733 if (sync_run)
734 sync();
735
736 ret = __run_perf_stat(argc, argv);
737 if (ret)
738 return ret;
739
740 if (post_cmd) {
741 ret = system(post_cmd);
742 if (ret)
743 return ret;
744 }
745
746 return ret;
747}
748
Andi Kleend73515c2015-03-11 07:16:27 -0700749static void print_running(u64 run, u64 ena)
750{
751 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200752 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700753 csv_sep,
754 run,
755 csv_sep,
756 ena ? 100.0 * run / ena : 100.0);
757 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200758 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700759 }
760}
761
Ingo Molnarf99844c2011-04-27 05:35:39 +0200762static void print_noise_pct(double total, double avg)
763{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800764 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200765
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700766 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200767 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600768 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200769 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200770}
771
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200772static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200773{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200774 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200775
Peter Zijlstra849abde2009-09-04 18:23:38 +0200776 if (run_count == 1)
777 return;
778
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200779 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200780 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200781}
782
Stephane Eranian12c08a92013-02-14 13:57:29 +0100783static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200784{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200785 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100786 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200787 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100788 cpu_map__id_to_socket(id),
789 csv_output ? 0 : -8,
790 cpu_map__id_to_cpu(id),
791 csv_sep,
792 csv_output ? 0 : 4,
793 nr,
794 csv_sep);
795 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100796 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200797 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100798 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100799 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100800 csv_sep,
801 csv_output ? 0 : 4,
802 nr,
803 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100804 break;
805 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200806 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200807 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100808 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100809 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200810 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200811 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200812 csv_output ? 0 : 16,
813 thread_map__comm(evsel->threads, id),
814 csv_output ? 0 : -8,
815 thread_map__pid(evsel->threads, id),
816 csv_sep);
817 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100818 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200819 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100820 default:
821 break;
822 }
823}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200824
Andi Kleen140aead2016-01-30 09:06:49 -0800825struct outstate {
826 FILE *fh;
827 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800828 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800829 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800830 int id, nr;
831 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800832};
833
834#define METRIC_LEN 35
835
836static void new_line_std(void *ctx)
837{
838 struct outstate *os = ctx;
839
840 os->newline = true;
841}
842
843static void do_new_line_std(struct outstate *os)
844{
845 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800846 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800847 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800848 if (stat_config.aggr_mode == AGGR_NONE)
849 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800850 fprintf(os->fh, " ");
851}
852
853static void print_metric_std(void *ctx, const char *color, const char *fmt,
854 const char *unit, double val)
855{
856 struct outstate *os = ctx;
857 FILE *out = os->fh;
858 int n;
859 bool newline = os->newline;
860
861 os->newline = false;
862
863 if (unit == NULL || fmt == NULL) {
864 fprintf(out, "%-*s", METRIC_LEN, "");
865 return;
866 }
867
868 if (newline)
869 do_new_line_std(os);
870
871 n = fprintf(out, " # ");
872 if (color)
873 n += color_fprintf(out, color, fmt, val);
874 else
875 n += fprintf(out, fmt, val);
876 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
877}
878
Andi Kleen92a61f62016-02-29 14:36:21 -0800879static void new_line_csv(void *ctx)
880{
881 struct outstate *os = ctx;
882 int i;
883
884 fputc('\n', os->fh);
885 if (os->prefix)
886 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800887 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800888 for (i = 0; i < os->nfields; i++)
889 fputs(csv_sep, os->fh);
890}
891
892static void print_metric_csv(void *ctx,
893 const char *color __maybe_unused,
894 const char *fmt, const char *unit, double val)
895{
896 struct outstate *os = ctx;
897 FILE *out = os->fh;
898 char buf[64], *vals, *ends;
899
900 if (unit == NULL || fmt == NULL) {
901 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
902 return;
903 }
904 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900905 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800906 while (isdigit(*ends) || *ends == '.')
907 ends++;
908 *ends = 0;
909 while (isspace(*unit))
910 unit++;
911 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
912}
913
Andi Kleen54b50912016-03-03 15:57:36 -0800914#define METRIC_ONLY_LEN 20
915
916/* Filter out some columns that don't work well in metrics only mode */
917
918static bool valid_only_metric(const char *unit)
919{
920 if (!unit)
921 return false;
922 if (strstr(unit, "/sec") ||
923 strstr(unit, "hz") ||
924 strstr(unit, "Hz") ||
925 strstr(unit, "CPUs utilized"))
926 return false;
927 return true;
928}
929
930static const char *fixunit(char *buf, struct perf_evsel *evsel,
931 const char *unit)
932{
933 if (!strncmp(unit, "of all", 6)) {
934 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
935 unit);
936 return buf;
937 }
938 return unit;
939}
940
941static void print_metric_only(void *ctx, const char *color, const char *fmt,
942 const char *unit, double val)
943{
944 struct outstate *os = ctx;
945 FILE *out = os->fh;
946 int n;
947 char buf[1024];
948 unsigned mlen = METRIC_ONLY_LEN;
949
950 if (!valid_only_metric(unit))
951 return;
952 unit = fixunit(buf, os->evsel, unit);
953 if (color)
954 n = color_fprintf(out, color, fmt, val);
955 else
956 n = fprintf(out, fmt, val);
957 if (n > METRIC_ONLY_LEN)
958 n = METRIC_ONLY_LEN;
959 if (mlen < strlen(unit))
960 mlen = strlen(unit) + 1;
961 fprintf(out, "%*s", mlen - n, "");
962}
963
964static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
965 const char *fmt,
966 const char *unit, double val)
967{
968 struct outstate *os = ctx;
969 FILE *out = os->fh;
970 char buf[64], *vals, *ends;
971 char tbuf[1024];
972
973 if (!valid_only_metric(unit))
974 return;
975 unit = fixunit(tbuf, os->evsel, unit);
976 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900977 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -0800978 while (isdigit(*ends) || *ends == '.')
979 ends++;
980 *ends = 0;
981 fprintf(out, "%s%s", vals, csv_sep);
982}
983
984static void new_line_metric(void *ctx __maybe_unused)
985{
986}
987
988static void print_metric_header(void *ctx, const char *color __maybe_unused,
989 const char *fmt __maybe_unused,
990 const char *unit, double val __maybe_unused)
991{
992 struct outstate *os = ctx;
993 char tbuf[1024];
994
995 if (!valid_only_metric(unit))
996 return;
997 unit = fixunit(tbuf, os->evsel, unit);
998 if (csv_output)
999 fprintf(os->fh, "%s%s", unit, csv_sep);
1000 else
1001 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
1002}
1003
Andi Kleenda88c7f2014-09-24 13:50:46 -07001004static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001005{
Jiri Olsa58215222015-07-21 14:31:24 +02001006 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001007 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +01001008 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -06001009 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001010
Stephane Eranian410136f2013-11-12 17:58:49 +01001011 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
1012 fmt_n = csv_output ? "%s" : "%-25s";
1013
Andi Kleenda88c7f2014-09-24 13:50:46 -07001014 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001015
David Ahern4bbe5a62013-09-28 14:28:00 -06001016 scnprintf(name, sizeof(name), "%s%s",
1017 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +01001018
1019 fprintf(output, fmt_v, msecs, csv_sep);
1020
1021 if (csv_output)
1022 fprintf(output, "%s%s", evsel->unit, csv_sep);
1023 else
1024 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1025
1026 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001027
Stephane Eranian023695d2011-02-14 11:20:01 +02001028 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001029 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001030}
1031
Andi Kleen44d49a62016-02-29 14:36:22 -08001032static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1033{
1034 int i;
1035
1036 if (!aggr_get_id)
1037 return 0;
1038
1039 if (stat_config.aggr_mode == AGGR_NONE)
1040 return id;
1041
1042 if (stat_config.aggr_mode == AGGR_GLOBAL)
1043 return 0;
1044
1045 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1046 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1047
1048 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1049 return cpu2;
1050 }
1051 return 0;
1052}
1053
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001054static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1055{
Jiri Olsa58215222015-07-21 14:31:24 +02001056 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001057 double sc = evsel->scale;
1058 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001059
1060 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001061 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001062 } else {
1063 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001064 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001065 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001066 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001067 }
1068
1069 aggr_printout(evsel, id, nr);
1070
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001071 fprintf(output, fmt, avg, csv_sep);
1072
1073 if (evsel->unit)
1074 fprintf(output, "%-*s%s",
1075 csv_output ? 0 : unit_width,
1076 evsel->unit, csv_sep);
1077
1078 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1079
1080 if (evsel->cgrp)
1081 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001082}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001083
Andi Kleenf9483392016-01-30 09:06:50 -08001084static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Andi Kleencb110f42016-01-30 09:06:51 -08001085 char *prefix, u64 run, u64 ena, double noise)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001086{
Andi Kleen140aead2016-01-30 09:06:49 -08001087 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001088 struct outstate os = {
1089 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001090 .prefix = prefix ? prefix : "",
1091 .id = id,
1092 .nr = nr,
1093 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001094 };
Andi Kleen140aead2016-01-30 09:06:49 -08001095 print_metric_t pm = print_metric_std;
1096 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001097
Andi Kleen54b50912016-03-03 15:57:36 -08001098 if (metric_only) {
1099 nl = new_line_metric;
1100 if (csv_output)
1101 pm = print_metric_only_csv;
1102 else
1103 pm = print_metric_only;
1104 } else
1105 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001106
Andi Kleen54b50912016-03-03 15:57:36 -08001107 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001108 static int aggr_fields[] = {
1109 [AGGR_GLOBAL] = 0,
1110 [AGGR_THREAD] = 1,
1111 [AGGR_NONE] = 1,
1112 [AGGR_SOCKET] = 2,
1113 [AGGR_CORE] = 2,
1114 };
1115
1116 pm = print_metric_csv;
1117 nl = new_line_csv;
1118 os.nfields = 3;
1119 os.nfields += aggr_fields[stat_config.aggr_mode];
1120 if (counter->cgrp)
1121 os.nfields++;
1122 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001123 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001124 if (metric_only) {
1125 pm(&os, NULL, "", "", 0);
1126 return;
1127 }
Andi Kleencb110f42016-01-30 09:06:51 -08001128 aggr_printout(counter, id, nr);
1129
1130 fprintf(stat_config.output, "%*s%s",
1131 csv_output ? 0 : 18,
1132 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1133 csv_sep);
1134
Borislav Petkov02d492e2017-02-07 01:40:05 +01001135 if (counter->supported)
1136 print_free_counters_hint = 1;
1137
Andi Kleencb110f42016-01-30 09:06:51 -08001138 fprintf(stat_config.output, "%-*s%s",
1139 csv_output ? 0 : unit_width,
1140 counter->unit, csv_sep);
1141
1142 fprintf(stat_config.output, "%*s",
1143 csv_output ? 0 : -25,
1144 perf_evsel__name(counter));
1145
1146 if (counter->cgrp)
1147 fprintf(stat_config.output, "%s%s",
1148 csv_sep, counter->cgrp->name);
1149
Andi Kleen92a61f62016-02-29 14:36:21 -08001150 if (!csv_output)
1151 pm(&os, NULL, NULL, "", 0);
1152 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001153 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001154 if (csv_output)
1155 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001156 return;
1157 }
1158
Andi Kleen54b50912016-03-03 15:57:36 -08001159 if (metric_only)
1160 /* nothing */;
1161 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001162 nsec_printout(id, nr, counter, uval);
1163 else
1164 abs_printout(id, nr, counter, uval);
1165
Andi Kleen140aead2016-01-30 09:06:49 -08001166 out.print_metric = pm;
1167 out.new_line = nl;
1168 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001169 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001170
Andi Kleen54b50912016-03-03 15:57:36 -08001171 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001172 print_noise(counter, noise);
1173 print_running(run, ena);
1174 }
1175
1176 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001177 first_shadow_cpu(counter, id),
Andi Kleen140aead2016-01-30 09:06:49 -08001178 &out);
Andi Kleen54b50912016-03-03 15:57:36 -08001179 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001180 print_noise(counter, noise);
1181 print_running(run, ena);
1182 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001183}
1184
Andi Kleen44d49a62016-02-29 14:36:22 -08001185static void aggr_update_shadow(void)
1186{
1187 int cpu, s2, id, s;
1188 u64 val;
1189 struct perf_evsel *counter;
1190
1191 for (s = 0; s < aggr_map->nr; s++) {
1192 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001193 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001194 val = 0;
1195 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1196 s2 = aggr_get_id(evsel_list->cpus, cpu);
1197 if (s2 != id)
1198 continue;
1199 val += perf_counts(counter->counts, cpu, 0)->val;
1200 }
1201 val = val * counter->scale;
1202 perf_stat__update_shadow_stats(counter, &val,
1203 first_shadow_cpu(counter, id));
1204 }
1205 }
1206}
1207
Andi Kleen430daf22017-03-20 13:17:00 -07001208static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001209 void (*cb)(struct perf_evsel *counter, void *data,
1210 bool first),
1211 void *data)
1212{
Andi Kleen430daf22017-03-20 13:17:00 -07001213 struct perf_evsel *alias;
1214
1215 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1216 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1217 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1218 alias->scale != counter->scale ||
1219 alias->cgrp != counter->cgrp ||
1220 strcmp(alias->unit, counter->unit) ||
1221 nsec_counter(alias) != nsec_counter(counter))
1222 break;
1223 alias->merged_stat = true;
1224 cb(alias, data, false);
1225 }
1226}
1227
1228static bool collect_data(struct perf_evsel *counter,
1229 void (*cb)(struct perf_evsel *counter, void *data,
1230 bool first),
1231 void *data)
1232{
1233 if (counter->merged_stat)
1234 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001235 cb(counter, data, true);
Andi Kleen430daf22017-03-20 13:17:00 -07001236 if (!no_merge)
1237 collect_all_aliases(counter, cb, data);
1238 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001239}
1240
1241struct aggr_data {
1242 u64 ena, run, val;
1243 int id;
1244 int nr;
1245 int cpu;
1246};
1247
1248static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1249{
1250 struct aggr_data *ad = data;
1251 int cpu, s2;
1252
1253 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1254 struct perf_counts_values *counts;
1255
1256 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1257 if (s2 != ad->id)
1258 continue;
1259 if (first)
1260 ad->nr++;
1261 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001262 /*
1263 * When any result is bad, make them all to give
1264 * consistent output in interval mode.
1265 */
1266 if (counts->ena == 0 || counts->run == 0 ||
1267 counter->counts->scaled == -1) {
1268 ad->ena = 0;
1269 ad->run = 0;
1270 break;
1271 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001272 ad->val += counts->val;
1273 ad->ena += counts->ena;
1274 ad->run += counts->run;
1275 }
1276}
1277
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001278static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001279{
Jiri Olsa58215222015-07-21 14:31:24 +02001280 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001281 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001282 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001283 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001284 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001285 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001286
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001287 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001288 return;
1289
Andi Kleen44d49a62016-02-29 14:36:22 -08001290 aggr_update_shadow();
1291
Andi Kleen54b50912016-03-03 15:57:36 -08001292 /*
1293 * With metric_only everything is on a single line.
1294 * Without each counter has its own line.
1295 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001296 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001297 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001298 if (prefix && metric_only)
1299 fprintf(output, "%s", prefix);
1300
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001301 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001302 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001303 evlist__for_each_entry(evsel_list, counter) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001304 ad.val = ad.ena = ad.run = 0;
1305 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001306 if (!collect_data(counter, aggr_cb, &ad))
1307 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001308 nr = ad.nr;
1309 ena = ad.ena;
1310 run = ad.run;
1311 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001312 if (first && metric_only) {
1313 first = false;
1314 aggr_printout(counter, id, nr);
1315 }
1316 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001317 fprintf(output, "%s", prefix);
1318
Stephane Eranian410136f2013-11-12 17:58:49 +01001319 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001320 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
Andi Kleen54b50912016-03-03 15:57:36 -08001321 if (!metric_only)
1322 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001323 }
Andi Kleen54b50912016-03-03 15:57:36 -08001324 if (metric_only)
1325 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001326 }
1327}
1328
Jiri Olsa32b8af82015-06-26 11:29:27 +02001329static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1330{
Jiri Olsa58215222015-07-21 14:31:24 +02001331 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001332 int nthreads = thread_map__nr(counter->threads);
1333 int ncpus = cpu_map__nr(counter->cpus);
1334 int cpu, thread;
1335 double uval;
1336
1337 for (thread = 0; thread < nthreads; thread++) {
1338 u64 ena = 0, run = 0, val = 0;
1339
1340 for (cpu = 0; cpu < ncpus; cpu++) {
1341 val += perf_counts(counter->counts, cpu, thread)->val;
1342 ena += perf_counts(counter->counts, cpu, thread)->ena;
1343 run += perf_counts(counter->counts, cpu, thread)->run;
1344 }
1345
1346 if (prefix)
1347 fprintf(output, "%s", prefix);
1348
1349 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001350 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001351 fputc('\n', output);
1352 }
1353}
1354
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001355struct caggr_data {
1356 double avg, avg_enabled, avg_running;
1357};
1358
1359static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1360 bool first __maybe_unused)
1361{
1362 struct caggr_data *cd = data;
1363 struct perf_stat_evsel *ps = counter->priv;
1364
1365 cd->avg += avg_stats(&ps->res_stats[0]);
1366 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1367 cd->avg_running += avg_stats(&ps->res_stats[2]);
1368}
1369
Ingo Molnar42202dd2009-06-13 14:57:28 +02001370/*
1371 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001372 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001373 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001374static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001375{
Jiri Olsa58215222015-07-21 14:31:24 +02001376 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001377 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001378 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001379
Andi Kleen430daf22017-03-20 13:17:00 -07001380 if (!collect_data(counter, counter_aggr_cb, &cd))
1381 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001382
Andi Kleen54b50912016-03-03 15:57:36 -08001383 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001384 fprintf(output, "%s", prefix);
1385
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001386 uval = cd.avg * counter->scale;
1387 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, cd.avg);
Andi Kleen54b50912016-03-03 15:57:36 -08001388 if (!metric_only)
1389 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001390}
1391
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001392static void counter_cb(struct perf_evsel *counter, void *data,
1393 bool first __maybe_unused)
1394{
1395 struct aggr_data *ad = data;
1396
1397 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1398 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1399 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1400}
1401
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001402/*
1403 * Print out the results of a single counter:
1404 * does not use aggregated count in system-wide
1405 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001406static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001407{
Jiri Olsa58215222015-07-21 14:31:24 +02001408 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001409 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001410 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001411 int cpu;
1412
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001413 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001414 struct aggr_data ad = { .cpu = cpu };
1415
Andi Kleen430daf22017-03-20 13:17:00 -07001416 if (!collect_data(counter, counter_cb, &ad))
1417 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001418 val = ad.val;
1419 ena = ad.ena;
1420 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001421
1422 if (prefix)
1423 fprintf(output, "%s", prefix);
1424
Stephane Eranian410136f2013-11-12 17:58:49 +01001425 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001426 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001427
Stephane Eranian4aa90152011-08-15 22:22:33 +02001428 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001429 }
1430}
1431
Andi Kleen206cab62016-03-03 15:57:37 -08001432static void print_no_aggr_metric(char *prefix)
1433{
1434 int cpu;
1435 int nrcpus = 0;
1436 struct perf_evsel *counter;
1437 u64 ena, run, val;
1438 double uval;
1439
1440 nrcpus = evsel_list->cpus->nr;
1441 for (cpu = 0; cpu < nrcpus; cpu++) {
1442 bool first = true;
1443
1444 if (prefix)
1445 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001446 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen206cab62016-03-03 15:57:37 -08001447 if (first) {
1448 aggr_printout(counter, cpu, 0);
1449 first = false;
1450 }
1451 val = perf_counts(counter->counts, cpu, 0)->val;
1452 ena = perf_counts(counter->counts, cpu, 0)->ena;
1453 run = perf_counts(counter->counts, cpu, 0)->run;
1454
1455 uval = val * counter->scale;
1456 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1457 }
1458 fputc('\n', stat_config.output);
1459 }
1460}
1461
Andi Kleen54b50912016-03-03 15:57:36 -08001462static int aggr_header_lens[] = {
1463 [AGGR_CORE] = 18,
1464 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001465 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001466 [AGGR_THREAD] = 24,
1467 [AGGR_GLOBAL] = 0,
1468};
1469
Andi Kleenc51fd632016-05-24 12:52:39 -07001470static const char *aggr_header_csv[] = {
1471 [AGGR_CORE] = "core,cpus,",
1472 [AGGR_SOCKET] = "socket,cpus",
1473 [AGGR_NONE] = "cpu,",
1474 [AGGR_THREAD] = "comm-pid,",
1475 [AGGR_GLOBAL] = ""
1476};
1477
Andi Kleen41c8ca22016-05-24 12:52:38 -07001478static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001479{
1480 struct perf_stat_output_ctx out;
1481 struct perf_evsel *counter;
1482 struct outstate os = {
1483 .fh = stat_config.output
1484 };
1485
1486 if (prefix)
1487 fprintf(stat_config.output, "%s", prefix);
1488
Andi Kleen41c8ca22016-05-24 12:52:38 -07001489 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001490 fprintf(stat_config.output, "%*s",
1491 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001492 if (csv_output) {
1493 if (stat_config.interval)
1494 fputs("time,", stat_config.output);
1495 fputs(aggr_header_csv[stat_config.aggr_mode],
1496 stat_config.output);
1497 }
Andi Kleen54b50912016-03-03 15:57:36 -08001498
1499 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001500 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen54b50912016-03-03 15:57:36 -08001501 os.evsel = counter;
1502 out.ctx = &os;
1503 out.print_metric = print_metric_header;
1504 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001505 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001506 os.evsel = counter;
1507 perf_stat__print_shadow_stats(counter, 0,
1508 0,
1509 &out);
1510 }
1511 fputc('\n', stat_config.output);
1512}
1513
Jiri Olsad4f63a42015-06-26 11:29:26 +02001514static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001515{
Jiri Olsa58215222015-07-21 14:31:24 +02001516 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001517 static int num_print_interval;
1518
1519 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1520
Andi Kleen41c8ca22016-05-24 12:52:38 -07001521 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001522 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001523 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001524 fprintf(output, "# time socket cpus");
1525 if (!metric_only)
1526 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001527 break;
1528 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001529 fprintf(output, "# time core cpus");
1530 if (!metric_only)
1531 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001532 break;
1533 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001534 fprintf(output, "# time CPU");
1535 if (!metric_only)
1536 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001537 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001538 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001539 fprintf(output, "# time comm-pid");
1540 if (!metric_only)
1541 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001542 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001543 case AGGR_GLOBAL:
1544 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001545 fprintf(output, "# time");
1546 if (!metric_only)
1547 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001548 case AGGR_UNSET:
1549 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001550 }
1551 }
1552
Andi Kleen41c8ca22016-05-24 12:52:38 -07001553 if (num_print_interval == 0 && metric_only)
1554 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001555 if (++num_print_interval == 25)
1556 num_print_interval = 0;
1557}
1558
1559static void print_header(int argc, const char **argv)
1560{
Jiri Olsa58215222015-07-21 14:31:24 +02001561 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001562 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001563
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001564 fflush(stdout);
1565
Stephane Eraniand7470b62010-12-01 18:49:05 +02001566 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001567 fprintf(output, "\n");
1568 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001569 if (target.system_wide)
1570 fprintf(output, "\'system wide");
1571 else if (target.cpu_list)
1572 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001573 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001574 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1575 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001576 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001577 } else if (target.pid)
1578 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001579 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001580 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001581
Stephane Eranian4aa90152011-08-15 22:22:33 +02001582 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001583 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001584 fprintf(output, " (%d runs)", run_count);
1585 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001586 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001587}
1588
1589static void print_footer(void)
1590{
Jiri Olsa58215222015-07-21 14:31:24 +02001591 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001592 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001593
Jiri Olsad4f63a42015-06-26 11:29:26 +02001594 if (!null_run)
1595 fprintf(output, "\n");
1596 fprintf(output, " %17.9f seconds time elapsed",
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001597 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001598 if (run_count > 1) {
1599 fprintf(output, " ");
1600 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1601 avg_stats(&walltime_nsecs_stats));
1602 }
1603 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001604
Andi Kleen918c7b062017-05-22 18:00:16 -07001605 if (print_free_counters_hint &&
1606 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1607 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001608 fprintf(output,
1609"Some events weren't counted. Try disabling the NMI watchdog:\n"
1610" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1611" perf stat ...\n"
1612" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001613}
1614
1615static void print_counters(struct timespec *ts, int argc, const char **argv)
1616{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001617 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001618 struct perf_evsel *counter;
1619 char buf[64], *prefix = NULL;
1620
Jiri Olsa664c98d2015-11-05 15:40:50 +01001621 /* Do not print anything if we record to the pipe. */
1622 if (STAT_RECORD && perf_stat.file.is_pipe)
1623 return;
1624
Jiri Olsad4f63a42015-06-26 11:29:26 +02001625 if (interval)
1626 print_interval(prefix = buf, ts);
1627 else
1628 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001629
Andi Kleen54b50912016-03-03 15:57:36 -08001630 if (metric_only) {
1631 static int num_print_iv;
1632
Andi Kleen41c8ca22016-05-24 12:52:38 -07001633 if (num_print_iv == 0 && !interval)
1634 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001635 if (num_print_iv++ == 25)
1636 num_print_iv = 0;
1637 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1638 fprintf(stat_config.output, "%s", prefix);
1639 }
1640
Jiri Olsa421a50f2015-07-21 14:31:22 +02001641 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001642 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001643 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001644 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001645 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001646 case AGGR_THREAD:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001647 evlist__for_each_entry(evsel_list, counter)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001648 print_aggr_thread(counter, prefix);
1649 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001650 case AGGR_GLOBAL:
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001651 evlist__for_each_entry(evsel_list, counter)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001652 print_counter_aggr(counter, prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001653 if (metric_only)
1654 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001655 break;
1656 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001657 if (metric_only)
1658 print_no_aggr_metric(prefix);
1659 else {
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001660 evlist__for_each_entry(evsel_list, counter)
Andi Kleen206cab62016-03-03 15:57:37 -08001661 print_counter(counter, prefix);
1662 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001663 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001664 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001665 default:
1666 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001667 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001668
Jiri Olsad4f63a42015-06-26 11:29:26 +02001669 if (!interval && !csv_output)
1670 print_footer();
1671
Jiri Olsa58215222015-07-21 14:31:24 +02001672 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001673}
1674
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001675static volatile int signr = -1;
1676
Ingo Molnar52425192009-05-26 09:17:18 +02001677static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001678{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001679 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001680 done = 1;
1681
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001682 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001683 /*
1684 * render child_pid harmless
1685 * won't send SIGTERM to a random
1686 * process in case of race condition
1687 * and fast PID recycling
1688 */
1689 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001690}
1691
1692static void sig_atexit(void)
1693{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001694 sigset_t set, oset;
1695
1696 /*
1697 * avoid race condition with SIGCHLD handler
1698 * in skip_signal() which is modifying child_pid
1699 * goal is to avoid send SIGTERM to a random
1700 * process
1701 */
1702 sigemptyset(&set);
1703 sigaddset(&set, SIGCHLD);
1704 sigprocmask(SIG_BLOCK, &set, &oset);
1705
Chris Wilson933da832009-10-04 01:35:01 +01001706 if (child_pid != -1)
1707 kill(child_pid, SIGTERM);
1708
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001709 sigprocmask(SIG_SETMASK, &oset, NULL);
1710
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001711 if (signr == -1)
1712 return;
1713
1714 signal(signr, SIG_DFL);
1715 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001716}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001717
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001718static int stat__set_big_num(const struct option *opt __maybe_unused,
1719 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001720{
1721 big_num_opt = unset ? 0 : 1;
1722 return 0;
1723}
1724
Andi Kleen44b1e602016-05-30 12:49:42 -03001725static int enable_metric_only(const struct option *opt __maybe_unused,
1726 const char *s __maybe_unused, int unset)
1727{
1728 force_metric_only = true;
1729 metric_only = !unset;
1730 return 0;
1731}
1732
Jiri Olsae0547312015-11-05 15:40:45 +01001733static const struct option stat_options[] = {
1734 OPT_BOOLEAN('T', "transaction", &transaction_run,
1735 "hardware transaction statistics"),
1736 OPT_CALLBACK('e', "event", &evsel_list, "event",
1737 "event selector. use 'perf list' to list available events",
1738 parse_events_option),
1739 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1740 "event filter", parse_filter),
1741 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1742 "child tasks do not inherit counters"),
1743 OPT_STRING('p', "pid", &target.pid, "pid",
1744 "stat events on existing process id"),
1745 OPT_STRING('t', "tid", &target.tid, "tid",
1746 "stat events on existing thread id"),
1747 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1748 "system-wide collection from all CPUs"),
1749 OPT_BOOLEAN('g', "group", &group,
1750 "put the counters into a counter group"),
1751 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1752 OPT_INCR('v', "verbose", &verbose,
1753 "be more verbose (show counter open errors, etc)"),
1754 OPT_INTEGER('r', "repeat", &run_count,
1755 "repeat command and print average + stddev (max: 100, forever: 0)"),
1756 OPT_BOOLEAN('n', "null", &null_run,
1757 "null run - dont start any counters"),
1758 OPT_INCR('d', "detailed", &detailed_run,
1759 "detailed run - start a lot of events"),
1760 OPT_BOOLEAN('S', "sync", &sync_run,
1761 "call sync() before starting a run"),
1762 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1763 "print large numbers with thousands\' separators",
1764 stat__set_big_num),
1765 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1766 "list of cpus to monitor in system-wide"),
1767 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1768 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001769 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001770 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1771 "print counts with custom separator"),
1772 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1773 "monitor event in cgroup name only", parse_cgroups),
1774 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1775 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1776 OPT_INTEGER(0, "log-fd", &output_fd,
1777 "log output to fd, instead of stderr"),
1778 OPT_STRING(0, "pre", &pre_cmd, "command",
1779 "command to run prior to the measured command"),
1780 OPT_STRING(0, "post", &post_cmd, "command",
1781 "command to run after to the measured command"),
1782 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1783 "print counts at regular interval in ms (>= 10)"),
1784 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1785 "aggregate counts per processor socket", AGGR_SOCKET),
1786 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1787 "aggregate counts per physical processor core", AGGR_CORE),
1788 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1789 "aggregate counts per thread", AGGR_THREAD),
1790 OPT_UINTEGER('D', "delay", &initial_delay,
1791 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001792 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1793 "Only print computed metrics. No raw values", enable_metric_only),
1794 OPT_BOOLEAN(0, "topdown", &topdown_run,
1795 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001796 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1797 "measure SMI cost"),
Jiri Olsae0547312015-11-05 15:40:45 +01001798 OPT_END()
1799};
1800
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001801static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1802{
1803 return cpu_map__get_socket(map, cpu, NULL);
1804}
1805
1806static int perf_stat__get_core(struct cpu_map *map, int cpu)
1807{
1808 return cpu_map__get_core(map, cpu, NULL);
1809}
1810
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001811static int cpu_map__get_max(struct cpu_map *map)
1812{
1813 int i, max = -1;
1814
1815 for (i = 0; i < map->nr; i++) {
1816 if (map->map[i] > max)
1817 max = map->map[i];
1818 }
1819
1820 return max;
1821}
1822
1823static struct cpu_map *cpus_aggr_map;
1824
1825static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1826{
1827 int cpu;
1828
1829 if (idx >= map->nr)
1830 return -1;
1831
1832 cpu = map->map[idx];
1833
1834 if (cpus_aggr_map->map[cpu] == -1)
1835 cpus_aggr_map->map[cpu] = get_id(map, idx);
1836
1837 return cpus_aggr_map->map[cpu];
1838}
1839
1840static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1841{
1842 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1843}
1844
1845static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1846{
1847 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1848}
1849
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001850static int perf_stat_init_aggr_mode(void)
1851{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001852 int nr;
1853
Jiri Olsa421a50f2015-07-21 14:31:22 +02001854 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001855 case AGGR_SOCKET:
1856 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1857 perror("cannot build socket map");
1858 return -1;
1859 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001860 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001861 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001862 case AGGR_CORE:
1863 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1864 perror("cannot build core map");
1865 return -1;
1866 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001867 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001868 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001869 case AGGR_NONE:
1870 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001871 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001872 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001873 default:
1874 break;
1875 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001876
1877 /*
1878 * The evsel_list->cpus is the base we operate on,
1879 * taking the highest cpu number to be the size of
1880 * the aggregation translate cpumap.
1881 */
1882 nr = cpu_map__get_max(evsel_list->cpus);
1883 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1884 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001885}
1886
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001887static void perf_stat__exit_aggr_mode(void)
1888{
1889 cpu_map__put(aggr_map);
1890 cpu_map__put(cpus_aggr_map);
1891 aggr_map = NULL;
1892 cpus_aggr_map = NULL;
1893}
1894
Jiri Olsa68d702f2015-11-05 15:40:58 +01001895static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1896{
1897 int cpu;
1898
1899 if (idx > map->nr)
1900 return -1;
1901
1902 cpu = map->map[idx];
1903
Jan Stancekda8a58b2017-02-17 12:10:26 +01001904 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001905 return -1;
1906
1907 return cpu;
1908}
1909
1910static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1911{
1912 struct perf_env *env = data;
1913 int cpu = perf_env__get_cpu(env, map, idx);
1914
1915 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1916}
1917
1918static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1919{
1920 struct perf_env *env = data;
1921 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1922
1923 if (cpu != -1) {
1924 int socket_id = env->cpu[cpu].socket_id;
1925
1926 /*
1927 * Encode socket in upper 16 bits
1928 * core_id is relative to socket, and
1929 * we need a global id. So we combine
1930 * socket + core id.
1931 */
1932 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1933 }
1934
1935 return core;
1936}
1937
1938static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1939 struct cpu_map **sockp)
1940{
1941 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1942}
1943
1944static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1945 struct cpu_map **corep)
1946{
1947 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1948}
1949
1950static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1951{
1952 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1953}
1954
1955static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1956{
1957 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1958}
1959
1960static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1961{
1962 struct perf_env *env = &st->session->header.env;
1963
1964 switch (stat_config.aggr_mode) {
1965 case AGGR_SOCKET:
1966 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
1967 perror("cannot build socket map");
1968 return -1;
1969 }
1970 aggr_get_id = perf_stat__get_socket_file;
1971 break;
1972 case AGGR_CORE:
1973 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
1974 perror("cannot build core map");
1975 return -1;
1976 }
1977 aggr_get_id = perf_stat__get_core_file;
1978 break;
1979 case AGGR_NONE:
1980 case AGGR_GLOBAL:
1981 case AGGR_THREAD:
1982 case AGGR_UNSET:
1983 default:
1984 break;
1985 }
1986
1987 return 0;
1988}
1989
Andi Kleen44b1e602016-05-30 12:49:42 -03001990static int topdown_filter_events(const char **attr, char **str, bool use_group)
1991{
1992 int off = 0;
1993 int i;
1994 int len = 0;
1995 char *s;
1996
1997 for (i = 0; attr[i]; i++) {
1998 if (pmu_have_event("cpu", attr[i])) {
1999 len += strlen(attr[i]) + 1;
2000 attr[i - off] = attr[i];
2001 } else
2002 off++;
2003 }
2004 attr[i - off] = NULL;
2005
2006 *str = malloc(len + 1 + 2);
2007 if (!*str)
2008 return -1;
2009 s = *str;
2010 if (i - off == 0) {
2011 *s = 0;
2012 return 0;
2013 }
2014 if (use_group)
2015 *s++ = '{';
2016 for (i = 0; attr[i]; i++) {
2017 strcpy(s, attr[i]);
2018 s += strlen(s);
2019 *s++ = ',';
2020 }
2021 if (use_group) {
2022 s[-1] = '}';
2023 *s = 0;
2024 } else
2025 s[-1] = 0;
2026 return 0;
2027}
2028
2029__weak bool arch_topdown_check_group(bool *warn)
2030{
2031 *warn = false;
2032 return false;
2033}
2034
2035__weak void arch_topdown_group_warn(void)
2036{
2037}
2038
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002039/*
2040 * Add default attributes, if there were no attributes specified or
2041 * if -d/--detailed, -d -d or -d -d -d is used:
2042 */
2043static int add_default_attributes(void)
2044{
Andi Kleen44b1e602016-05-30 12:49:42 -03002045 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002046 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002047
2048 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2049 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2050 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2051 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2052
2053 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002054};
2055 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002056 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002057};
2058 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002059 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002060};
2061 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002062 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2063 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2064 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2065
2066};
2067
2068/*
2069 * Detailed stats (-d), covering the L1 and last level data caches:
2070 */
2071 struct perf_event_attr detailed_attrs[] = {
2072
2073 { .type = PERF_TYPE_HW_CACHE,
2074 .config =
2075 PERF_COUNT_HW_CACHE_L1D << 0 |
2076 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2077 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2078
2079 { .type = PERF_TYPE_HW_CACHE,
2080 .config =
2081 PERF_COUNT_HW_CACHE_L1D << 0 |
2082 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2083 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2084
2085 { .type = PERF_TYPE_HW_CACHE,
2086 .config =
2087 PERF_COUNT_HW_CACHE_LL << 0 |
2088 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2089 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2090
2091 { .type = PERF_TYPE_HW_CACHE,
2092 .config =
2093 PERF_COUNT_HW_CACHE_LL << 0 |
2094 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2095 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2096};
2097
2098/*
2099 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2100 */
2101 struct perf_event_attr very_detailed_attrs[] = {
2102
2103 { .type = PERF_TYPE_HW_CACHE,
2104 .config =
2105 PERF_COUNT_HW_CACHE_L1I << 0 |
2106 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2107 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2108
2109 { .type = PERF_TYPE_HW_CACHE,
2110 .config =
2111 PERF_COUNT_HW_CACHE_L1I << 0 |
2112 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2113 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2114
2115 { .type = PERF_TYPE_HW_CACHE,
2116 .config =
2117 PERF_COUNT_HW_CACHE_DTLB << 0 |
2118 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2119 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2120
2121 { .type = PERF_TYPE_HW_CACHE,
2122 .config =
2123 PERF_COUNT_HW_CACHE_DTLB << 0 |
2124 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2125 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2126
2127 { .type = PERF_TYPE_HW_CACHE,
2128 .config =
2129 PERF_COUNT_HW_CACHE_ITLB << 0 |
2130 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2131 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2132
2133 { .type = PERF_TYPE_HW_CACHE,
2134 .config =
2135 PERF_COUNT_HW_CACHE_ITLB << 0 |
2136 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2137 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2138
2139};
2140
2141/*
2142 * Very, very detailed stats (-d -d -d), adding prefetch events:
2143 */
2144 struct perf_event_attr very_very_detailed_attrs[] = {
2145
2146 { .type = PERF_TYPE_HW_CACHE,
2147 .config =
2148 PERF_COUNT_HW_CACHE_L1D << 0 |
2149 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2150 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2151
2152 { .type = PERF_TYPE_HW_CACHE,
2153 .config =
2154 PERF_COUNT_HW_CACHE_L1D << 0 |
2155 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2156 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2157};
2158
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002159 /* Set attrs if no event is selected and !null_run: */
2160 if (null_run)
2161 return 0;
2162
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002163 if (transaction_run) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002164 if (pmu_have_event("cpu", "cycles-ct") &&
2165 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02002166 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002167 else
Jiri Olsaa4547422015-06-03 16:25:53 +02002168 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2169 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002170 fprintf(stderr, "Cannot set up transaction events\n");
2171 return -1;
2172 }
2173 return 0;
2174 }
2175
Kan Liangdaefd0b2017-05-26 12:05:38 -07002176 if (smi_cost) {
2177 int smi;
2178
2179 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2180 fprintf(stderr, "freeze_on_smi is not supported.\n");
2181 return -1;
2182 }
2183
2184 if (!smi) {
2185 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2186 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2187 return -1;
2188 }
2189 smi_reset = true;
2190 }
2191
2192 if (pmu_have_event("msr", "aperf") &&
2193 pmu_have_event("msr", "smi")) {
2194 if (!force_metric_only)
2195 metric_only = true;
2196 err = parse_events(evsel_list, smi_cost_attrs, NULL);
2197 } else {
2198 fprintf(stderr, "To measure SMI cost, it needs "
2199 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2200 return -1;
2201 }
2202 if (err) {
2203 fprintf(stderr, "Cannot set up SMI cost events\n");
2204 return -1;
2205 }
2206 return 0;
2207 }
2208
Andi Kleen44b1e602016-05-30 12:49:42 -03002209 if (topdown_run) {
2210 char *str = NULL;
2211 bool warn = false;
2212
2213 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2214 stat_config.aggr_mode != AGGR_CORE) {
2215 pr_err("top down event configuration requires --per-core mode\n");
2216 return -1;
2217 }
2218 stat_config.aggr_mode = AGGR_CORE;
2219 if (nr_cgroups || !target__has_cpu(&target)) {
2220 pr_err("top down event configuration requires system-wide mode (-a)\n");
2221 return -1;
2222 }
2223
2224 if (!force_metric_only)
2225 metric_only = true;
2226 if (topdown_filter_events(topdown_attrs, &str,
2227 arch_topdown_check_group(&warn)) < 0) {
2228 pr_err("Out of memory\n");
2229 return -1;
2230 }
2231 if (topdown_attrs[0] && str) {
2232 if (warn)
2233 arch_topdown_group_warn();
2234 err = parse_events(evsel_list, str, NULL);
2235 if (err) {
2236 fprintf(stderr,
2237 "Cannot set up top down events %s: %d\n",
2238 str, err);
2239 free(str);
2240 return -1;
2241 }
2242 } else {
2243 fprintf(stderr, "System does not support topdown\n");
2244 return -1;
2245 }
2246 free(str);
2247 }
2248
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002249 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002250 if (target__has_cpu(&target))
2251 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2252
Andi Kleen9dec4472016-02-26 16:27:56 -08002253 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2254 return -1;
2255 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2256 if (perf_evlist__add_default_attrs(evsel_list,
2257 frontend_attrs) < 0)
2258 return -1;
2259 }
2260 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2261 if (perf_evlist__add_default_attrs(evsel_list,
2262 backend_attrs) < 0)
2263 return -1;
2264 }
2265 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002266 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002267 }
2268
2269 /* Detailed events get appended to the event list: */
2270
2271 if (detailed_run < 1)
2272 return 0;
2273
2274 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002275 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002276 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002277
2278 if (detailed_run < 2)
2279 return 0;
2280
2281 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002282 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002283 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002284
2285 if (detailed_run < 3)
2286 return 0;
2287
2288 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002289 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002290}
2291
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002292static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002293 "perf stat record [<options>]",
2294 NULL,
2295};
2296
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002297static void init_features(struct perf_session *session)
2298{
2299 int feat;
2300
2301 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2302 perf_header__set_feat(&session->header, feat);
2303
2304 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2305 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2306 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2307 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2308}
2309
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002310static int __cmd_record(int argc, const char **argv)
2311{
2312 struct perf_session *session;
2313 struct perf_data_file *file = &perf_stat.file;
2314
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002315 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002316 PARSE_OPT_STOP_AT_NON_OPTION);
2317
2318 if (output_name)
2319 file->path = output_name;
2320
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002321 if (run_count != 1 || forever) {
2322 pr_err("Cannot use -r option with perf stat record.\n");
2323 return -1;
2324 }
2325
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002326 session = perf_session__new(file, false, NULL);
2327 if (session == NULL) {
2328 pr_err("Perf session creation failed.\n");
2329 return -1;
2330 }
2331
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002332 init_features(session);
2333
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002334 session->evlist = evsel_list;
2335 perf_stat.session = session;
2336 perf_stat.record = true;
2337 return argc;
2338}
2339
Jiri Olsaa56f9392015-11-05 15:40:59 +01002340static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2341 union perf_event *event,
2342 struct perf_session *session)
2343{
Andi Kleene3b03b62016-05-05 16:04:03 -07002344 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002345 struct perf_evsel *counter;
2346 struct timespec tsh, *ts = NULL;
2347 const char **argv = session->header.env.cmdline_argv;
2348 int argc = session->header.env.nr_cmdline;
2349
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002350 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002351 perf_stat_process_counter(&stat_config, counter);
2352
Andi Kleene3b03b62016-05-05 16:04:03 -07002353 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2354 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002355
Andi Kleene3b03b62016-05-05 16:04:03 -07002356 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002357 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2358 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002359 ts = &tsh;
2360 }
2361
2362 print_counters(ts, argc, argv);
2363 return 0;
2364}
2365
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002366static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002367int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002368 union perf_event *event,
2369 struct perf_session *session __maybe_unused)
2370{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002371 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2372
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002373 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002374
Jiri Olsa89af4e02015-11-05 15:41:02 +01002375 if (cpu_map__empty(st->cpus)) {
2376 if (st->aggr_mode != AGGR_UNSET)
2377 pr_warning("warning: processing task data, aggregation mode not set\n");
2378 return 0;
2379 }
2380
2381 if (st->aggr_mode != AGGR_UNSET)
2382 stat_config.aggr_mode = st->aggr_mode;
2383
Jiri Olsa68d702f2015-11-05 15:40:58 +01002384 if (perf_stat.file.is_pipe)
2385 perf_stat_init_aggr_mode();
2386 else
2387 perf_stat_init_aggr_mode_file(st);
2388
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002389 return 0;
2390}
2391
Jiri Olsa1975d362015-11-05 15:40:56 +01002392static int set_maps(struct perf_stat *st)
2393{
2394 if (!st->cpus || !st->threads)
2395 return 0;
2396
2397 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2398 return -EINVAL;
2399
2400 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2401
2402 if (perf_evlist__alloc_stats(evsel_list, true))
2403 return -ENOMEM;
2404
2405 st->maps_allocated = true;
2406 return 0;
2407}
2408
2409static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002410int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002411 union perf_event *event,
2412 struct perf_session *session __maybe_unused)
2413{
2414 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2415
2416 if (st->threads) {
2417 pr_warning("Extra thread map event, ignoring.\n");
2418 return 0;
2419 }
2420
2421 st->threads = thread_map__new_event(&event->thread_map);
2422 if (!st->threads)
2423 return -ENOMEM;
2424
2425 return set_maps(st);
2426}
2427
2428static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002429int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002430 union perf_event *event,
2431 struct perf_session *session __maybe_unused)
2432{
2433 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2434 struct cpu_map *cpus;
2435
2436 if (st->cpus) {
2437 pr_warning("Extra cpu map event, ignoring.\n");
2438 return 0;
2439 }
2440
2441 cpus = cpu_map__new_data(&event->cpu_map.data);
2442 if (!cpus)
2443 return -ENOMEM;
2444
2445 st->cpus = cpus;
2446 return set_maps(st);
2447}
2448
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002449static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002450 "perf stat report [<options>]",
2451 NULL,
2452};
2453
2454static struct perf_stat perf_stat = {
2455 .tool = {
2456 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002457 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002458 .thread_map = process_thread_map_event,
2459 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002460 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002461 .stat = perf_event__process_stat_event,
2462 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002463 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002464 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002465};
2466
2467static int __cmd_report(int argc, const char **argv)
2468{
2469 struct perf_session *session;
2470 const struct option options[] = {
2471 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002472 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2473 "aggregate counts per processor socket", AGGR_SOCKET),
2474 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2475 "aggregate counts per physical processor core", AGGR_CORE),
2476 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2477 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002478 OPT_END()
2479 };
2480 struct stat st;
2481 int ret;
2482
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002483 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002484
2485 if (!input_name || !strlen(input_name)) {
2486 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2487 input_name = "-";
2488 else
2489 input_name = "perf.data";
2490 }
2491
2492 perf_stat.file.path = input_name;
2493 perf_stat.file.mode = PERF_DATA_MODE_READ;
2494
2495 session = perf_session__new(&perf_stat.file, false, &perf_stat.tool);
2496 if (session == NULL)
2497 return -1;
2498
2499 perf_stat.session = session;
2500 stat_config.output = stderr;
2501 evsel_list = session->evlist;
2502
2503 ret = perf_session__process_events(session);
2504 if (ret)
2505 return ret;
2506
2507 perf_session__delete(session);
2508 return 0;
2509}
2510
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002511static void setup_system_wide(int forks)
2512{
2513 /*
2514 * Make system wide (-a) the default target if
2515 * no target was specified and one of following
2516 * conditions is met:
2517 *
2518 * - there's no workload specified
2519 * - there is workload specified but all requested
2520 * events are system wide events
2521 */
2522 if (!target__none(&target))
2523 return;
2524
2525 if (!forks)
2526 target.system_wide = true;
2527 else {
2528 struct perf_evsel *counter;
2529
2530 evlist__for_each_entry(evsel_list, counter) {
2531 if (!counter->system_wide)
2532 return;
2533 }
2534
2535 if (evsel_list->nr_entries)
2536 target.system_wide = true;
2537 }
2538}
2539
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002540int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002541{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002542 const char * const stat_usage[] = {
2543 "perf stat [<options>] [<command>]",
2544 NULL
2545 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002546 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002547 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002548 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002549 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002550 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002551
Stephane Eranian5af52b52010-05-18 15:00:01 +02002552 setlocale(LC_ALL, "");
2553
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002554 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002555 if (evsel_list == NULL)
2556 return -ENOMEM;
2557
Wang Nan1669e502016-02-19 11:43:58 +00002558 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002559 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2560 (const char **) stat_usage,
2561 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002562 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002563 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002564
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002565 if (csv_sep) {
2566 csv_output = true;
2567 if (!strcmp(csv_sep, "\\t"))
2568 csv_sep = "\t";
2569 } else
2570 csv_sep = DEFAULT_SEPARATOR;
2571
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002572 if (argc && !strncmp(argv[0], "rec", 3)) {
2573 argc = __cmd_record(argc, argv);
2574 if (argc < 0)
2575 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002576 } else if (argc && !strncmp(argv[0], "rep", 3))
2577 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002578
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002579 interval = stat_config.interval;
2580
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002581 /*
2582 * For record command the -o is already taken care of.
2583 */
2584 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002585 output = NULL;
2586
Jim Cromie56f3bae2011-09-07 17:14:00 -06002587 if (output_name && output_fd) {
2588 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002589 parse_options_usage(stat_usage, stat_options, "o", 1);
2590 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002591 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002592 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002593
Andi Kleen54b50912016-03-03 15:57:36 -08002594 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2595 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2596 goto out;
2597 }
2598
Andi Kleen54b50912016-03-03 15:57:36 -08002599 if (metric_only && run_count > 1) {
2600 fprintf(stderr, "--metric-only is not supported with -r\n");
2601 goto out;
2602 }
2603
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002604 if (output_fd < 0) {
2605 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002606 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002607 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002608 }
2609
Stephane Eranian4aa90152011-08-15 22:22:33 +02002610 if (!output) {
2611 struct timespec tm;
2612 mode = append_file ? "a" : "w";
2613
2614 output = fopen(output_name, mode);
2615 if (!output) {
2616 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002617 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002618 }
2619 clock_gettime(CLOCK_REALTIME, &tm);
2620 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002621 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002622 mode = append_file ? "a" : "w";
2623 output = fdopen(output_fd, mode);
2624 if (!output) {
2625 perror("Failed opening logfd");
2626 return -errno;
2627 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002628 }
2629
Jiri Olsa58215222015-07-21 14:31:24 +02002630 stat_config.output = output;
2631
Stephane Eraniand7470b62010-12-01 18:49:05 +02002632 /*
2633 * let the spreadsheet do the pretty-printing
2634 */
2635 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002636 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002637 if (big_num_opt == 1) {
2638 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002639 parse_options_usage(stat_usage, stat_options, "B", 1);
2640 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002641 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002642 } else /* Nope, so disable big number formatting */
2643 big_num = false;
2644 } else if (big_num_opt == 0) /* User passed --no-big-num */
2645 big_num = false;
2646
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002647 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002648
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002649 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002650 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002651 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002652 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002653 } else if (run_count == 0) {
2654 forever = true;
2655 run_count = 1;
2656 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002657
Jiri Olsa421a50f2015-07-21 14:31:22 +02002658 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002659 fprintf(stderr, "The --per-thread option is only available "
2660 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002661 parse_options_usage(NULL, stat_options, "p", 1);
2662 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002663 goto out;
2664 }
2665
2666 /*
2667 * no_aggr, cgroup are for system-wide only
2668 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2669 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002670 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2671 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002672 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002673 fprintf(stderr, "both cgroup and no-aggregation "
2674 "modes only available in system-wide mode\n");
2675
Jiri Olsae0547312015-11-05 15:40:45 +01002676 parse_options_usage(stat_usage, stat_options, "G", 1);
2677 parse_options_usage(NULL, stat_options, "A", 1);
2678 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002679 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002680 }
2681
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002682 if (add_default_attributes())
2683 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002684
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002685 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002686
Namhyung Kim77a6f012012-05-07 14:09:04 +09002687 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002688 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002689 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002690 parse_options_usage(stat_usage, stat_options, "p", 1);
2691 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002692 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002693 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002694 parse_options_usage(stat_usage, stat_options, "C", 1);
2695 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002696 }
2697 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002698 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002699
2700 /*
2701 * Initialize thread_map with comm names,
2702 * so we could print it out on output.
2703 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002704 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02002705 thread_map__read_comms(evsel_list->threads);
2706
Stephane Eranian13370a92013-01-29 12:47:44 +01002707 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002708 if (interval < 10) {
2709 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002710 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002711 goto out;
2712 } else
2713 pr_warning("print interval < 100ms. "
2714 "The overhead percentage could be high in some cases. "
2715 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002716 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002717
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002718 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002719 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002720
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002721 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002722 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002723
Ingo Molnar58d7e992009-05-15 11:03:23 +02002724 /*
2725 * We dont want to block the signals - that would cause
2726 * child tasks to inherit that and Ctrl-C would not work.
2727 * What we want is for Ctrl-C to work in the exec()-ed
2728 * task, but being ignored by perf stat itself:
2729 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002730 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002731 if (!forever)
2732 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002733 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002734 signal(SIGALRM, skip_signal);
2735 signal(SIGABRT, skip_signal);
2736
Ingo Molnar42202dd2009-06-13 14:57:28 +02002737 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002738 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002739 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002740 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2741 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002742
Ingo Molnar42202dd2009-06-13 14:57:28 +02002743 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002744 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002745 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002746 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002747 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002748 }
2749
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002750 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002751 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002752
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002753 if (STAT_RECORD) {
2754 /*
2755 * We synthesize the kernel mmap record just so that older tools
2756 * don't emit warnings about not being able to resolve symbols
2757 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2758 * a saner message about no samples being in the perf.data file.
2759 *
2760 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002761 * in header.c at the moment 'perf stat record' gets introduced, which
2762 * is not really needed once we start adding the stat specific PERF_RECORD_
2763 * records, but the need to suppress the kptr_restrict messages in older
2764 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002765 */
2766 int fd = perf_data_file__fd(&perf_stat.file);
2767 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2768 process_synthesized_event,
2769 &perf_stat.session->machines.host);
2770 if (err) {
2771 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2772 "older tools may produce warnings about this file\n.");
2773 }
2774
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002775 if (!interval) {
2776 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2777 pr_err("failed to write stat round event\n");
2778 }
2779
Jiri Olsa664c98d2015-11-05 15:40:50 +01002780 if (!perf_stat.file.is_pipe) {
2781 perf_stat.session->header.data_size += perf_stat.bytes_written;
2782 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2783 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002784
2785 perf_session__delete(perf_stat.session);
2786 }
2787
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002788 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002789 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002790out:
Kan Liangdaefd0b2017-05-26 12:05:38 -07002791 if (smi_cost && smi_reset)
2792 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
2793
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002794 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002795 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002796}