blob: 54a4c152edb3917405dd064a1f79037acbb0d239 [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"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030066#include "util/string2.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070067#include "util/metricgroup.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
Andi Kleenb18f3e32017-08-31 12:40:31 -0700136static struct rblist metric_events;
137
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300138static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900139 .uid = UINT_MAX,
140};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530141
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100142typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
143
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530144static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200145static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200146static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000147static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200148static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700149static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300150static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700151static bool smi_cost = false;
152static bool smi_reset = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200153static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200154static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200155static const char *csv_sep = NULL;
156static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800157static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200158static const char *pre_cmd = NULL;
159static const char *post_cmd = NULL;
160static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700161static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100162static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500163static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800164static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300165static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700166static bool no_merge = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100167static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100168static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100169static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100170static bool append_file;
171static const char *output_name;
172static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100173static int print_free_counters_hint;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200174
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100175struct perf_stat {
176 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100177 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100178 struct perf_session *session;
179 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100180 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100181 bool maps_allocated;
182 struct cpu_map *cpus;
183 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100184 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100185};
186
187static struct perf_stat perf_stat;
188#define STAT_RECORD perf_stat.record
189
Liming Wang60666c62009-12-31 16:05:50 +0800190static volatile int done = 0;
191
Jiri Olsa421a50f2015-07-21 14:31:22 +0200192static struct perf_stat_config stat_config = {
193 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200194 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200195};
196
Andi Kleene864c5c2017-08-31 12:40:35 -0700197static bool is_duration_time(struct perf_evsel *evsel)
198{
199 return !strcmp(evsel->name, "duration_time");
200}
201
Stephane Eranian13370a92013-01-29 12:47:44 +0100202static inline void diff_timespec(struct timespec *r, struct timespec *a,
203 struct timespec *b)
204{
205 r->tv_sec = a->tv_sec - b->tv_sec;
206 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300207 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100208 r->tv_sec--;
209 } else {
210 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
211 }
212}
213
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200214static void perf_stat__reset_stats(void)
215{
Jin Yao56739442017-12-05 22:03:07 +0800216 int i;
217
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200218 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200219 perf_stat__reset_shadow_stats();
Jin Yao56739442017-12-05 22:03:07 +0800220
221 for (i = 0; i < stat_config.stats_num; i++)
222 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200223}
224
Jiri Olsacac21422012-11-12 18:34:00 +0100225static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200226{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200227 struct perf_event_attr *attr = &evsel->attr;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200228 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200229
Jiri Olsa82bf3112017-07-26 14:02:06 +0200230 if (stat_config.scale) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200231 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
232 PERF_FORMAT_TOTAL_TIME_RUNNING;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200233 }
234
235 /*
236 * The event is part of non trivial group, let's enable
237 * the group read (for leader) and ID retrieval for all
238 * members.
239 */
240 if (leader->nr_members > 1)
241 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200242
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200243 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300244
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100245 /*
246 * Some events get initialized with sample_(period/type) set,
247 * like tracepoints. Clear it up for counting.
248 */
249 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100250
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100251 /*
252 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
253 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100254 *
255 * However for pipe sessions we need to keep it zero,
256 * because script's perf_evsel__check_attr is triggered
257 * by attr->sample_type != 0, and we can't run it on
258 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100259 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100260 if (!(STAT_RECORD && perf_stat.data.is_pipe))
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100261 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100262
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100263 /*
264 * Disabling all counters initially, they will be enabled
265 * either manually by us or by kernel via enable_on_exec
266 * set later.
267 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100268 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100269 attr->disabled = 1;
270
Jiri Olsac8280ce2015-12-03 10:06:45 +0100271 /*
272 * In case of initial_delay we enable tracee
273 * events manually.
274 */
275 if (target__none(&target) && !initial_delay)
276 attr->enable_on_exec = 1;
277 }
278
Jin Yao1d9f8d12017-12-05 22:03:10 +0800279 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300280 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200281
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300282 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200283}
284
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200285/*
286 * Does the counter have nsecs as a unit?
287 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200288static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200289{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200290 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
291 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200292 return 1;
293
294 return 0;
295}
296
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100297static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100298 union perf_event *event,
299 struct perf_sample *sample __maybe_unused,
300 struct machine *machine __maybe_unused)
301{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100302 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100303 pr_err("failed to write perf data, error: %m\n");
304 return -1;
305 }
306
307 perf_stat.bytes_written += event->header.size;
308 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100309}
310
Jiri Olsa1975d362015-11-05 15:40:56 +0100311static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100312{
Jiri Olsa1975d362015-11-05 15:40:56 +0100313 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100314 process_synthesized_event,
315 NULL);
316}
317
318#define WRITE_STAT_ROUND_EVENT(time, interval) \
319 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
320
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100321#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
322
323static int
324perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
325 struct perf_counts_values *count)
326{
327 struct perf_sample_id *sid = SID(counter, cpu, thread);
328
329 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
330 process_synthesized_event, NULL);
331}
332
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200333/*
334 * Read out the results of a single counter:
335 * do not aggregate counts across CPUs in system-wide mode
336 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200337static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200338{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100339 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100340 int ncpus, cpu, thread;
341
Jin Yao1d9f8d12017-12-05 22:03:10 +0800342 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Mark Rutland00e727b2016-07-15 11:08:10 +0100343 ncpus = perf_evsel__nr_cpus(counter);
344 else
345 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200346
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000347 if (!counter->supported)
348 return -ENOENT;
349
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100350 if (counter->system_wide)
351 nthreads = 1;
352
353 for (thread = 0; thread < nthreads; thread++) {
354 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200355 struct perf_counts_values *count;
356
357 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200358
359 /*
360 * The leader's group read loads data into its group members
361 * (via perf_evsel__read_counter) and sets threir count->loaded.
362 */
363 if (!count->loaded &&
364 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700365 counter->counts->scaled = -1;
366 perf_counts(counter->counts, cpu, thread)->ena = 0;
367 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100368 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700369 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100370
Jiri Olsa82bf3112017-07-26 14:02:06 +0200371 count->loaded = false;
372
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100373 if (STAT_RECORD) {
374 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
375 pr_err("failed to write stat event\n");
376 return -1;
377 }
378 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700379
380 if (verbose > 1) {
381 fprintf(stat_config.output,
382 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
383 perf_evsel__name(counter),
384 cpu,
385 count->val, count->ena, count->run);
386 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100387 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200388 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200389
390 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200391}
392
Mark Rutland3df33ef2016-08-09 14:04:29 +0100393static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200394{
395 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700396 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200397
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300398 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700399 ret = read_counter(counter);
400 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700401 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200402
Stephane Eraniandb49a712017-04-12 11:23:01 -0700403 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200404 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200405 }
406}
407
Jiri Olsaba411a92015-06-26 11:29:24 +0200408static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100409{
Stephane Eranian13370a92013-01-29 12:47:44 +0100410 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100411
Mark Rutland3df33ef2016-08-09 14:04:29 +0100412 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100413
Stephane Eranian13370a92013-01-29 12:47:44 +0100414 clock_gettime(CLOCK_MONOTONIC, &ts);
415 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100416
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100417 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300418 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100419 pr_err("failed to write stat round event\n");
420 }
421
Andi Kleenb90f1332017-08-31 12:40:36 -0700422 init_stats(&walltime_nsecs_stats);
423 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200424 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100425}
426
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100427static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700428{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100429 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300430 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100431
432 /*
433 * We need to enable counters only if:
434 * - we don't have tracee (attaching to task or cpu)
435 * - we have initial delay configured
436 */
437 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100438 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700439}
440
Mark Rutland3df33ef2016-08-09 14:04:29 +0100441static void disable_counters(void)
442{
443 /*
444 * If we don't have tracee (attaching to task or cpu), counters may
445 * still be running. To get accurate group ratios, we must stop groups
446 * from counting before reading their constituent counters.
447 */
448 if (!target__none(&target))
449 perf_evlist__disable(evsel_list);
450}
451
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300452static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300453
454/*
455 * perf_evlist__prepare_workload will send a SIGUSR1
456 * if the fork fails, since we asked by setting its
457 * want_signal to true.
458 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300459static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
460 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300461{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300462 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300463}
464
Jiri Olsa664c98d2015-11-05 15:40:50 +0100465static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100466{
467 int err;
468
Jiri Olsa664c98d2015-11-05 15:40:50 +0100469 if (is_pipe) {
470 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
471 process_synthesized_event);
472 if (err < 0) {
473 pr_err("Couldn't synthesize attrs.\n");
474 return err;
475 }
476 }
477
Andi Kleenbfd8f722017-11-17 13:42:58 -0800478 err = perf_event__synthesize_extra_attr(NULL,
479 evsel_list,
480 process_synthesized_event,
481 is_pipe);
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100482
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100483 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
484 process_synthesized_event,
485 NULL);
486 if (err < 0) {
487 pr_err("Couldn't synthesize thread map.\n");
488 return err;
489 }
490
491 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
492 process_synthesized_event, NULL);
493 if (err < 0) {
494 pr_err("Couldn't synthesize thread map.\n");
495 return err;
496 }
497
498 err = perf_event__synthesize_stat_config(NULL, &stat_config,
499 process_synthesized_event, NULL);
500 if (err < 0) {
501 pr_err("Couldn't synthesize config.\n");
502 return err;
503 }
504
505 return 0;
506}
507
Jiri Olsa2af46462015-11-05 15:40:49 +0100508#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
509
510static int __store_counter_ids(struct perf_evsel *counter,
511 struct cpu_map *cpus,
512 struct thread_map *threads)
513{
514 int cpu, thread;
515
516 for (cpu = 0; cpu < cpus->nr; cpu++) {
517 for (thread = 0; thread < threads->nr; thread++) {
518 int fd = FD(counter, cpu, thread);
519
520 if (perf_evlist__id_add_fd(evsel_list, counter,
521 cpu, thread, fd) < 0)
522 return -1;
523 }
524 }
525
526 return 0;
527}
528
529static int store_counter_ids(struct perf_evsel *counter)
530{
531 struct cpu_map *cpus = counter->cpus;
532 struct thread_map *threads = counter->threads;
533
534 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
535 return -ENOMEM;
536
537 return __store_counter_ids(counter, cpus, threads);
538}
539
Jiri Olsa82bf3112017-07-26 14:02:06 +0200540static bool perf_evsel__should_store_id(struct perf_evsel *counter)
541{
542 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
543}
544
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700545static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
546{
547 struct perf_evsel *c2, *leader;
548 bool is_open = true;
549
550 leader = evsel->leader;
551 pr_debug("Weak group for %s/%d failed\n",
552 leader->name, leader->nr_members);
553
554 /*
555 * for_each_group_member doesn't work here because it doesn't
556 * include the first entry.
557 */
558 evlist__for_each_entry(evsel_list, c2) {
559 if (c2 == evsel)
560 is_open = false;
561 if (c2->leader == leader) {
562 if (is_open)
563 perf_evsel__close(c2);
564 c2->leader = c2;
565 c2->nr_members = 0;
566 }
567 }
568 return leader;
569}
570
Namhyung Kimacf28922013-03-11 16:43:18 +0900571static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200572{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200573 int interval = stat_config.interval;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300574 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200575 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100576 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100577 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100578 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200579 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300580 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100581 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600582 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200583
Stephane Eranian13370a92013-01-29 12:47:44 +0100584 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300585 ts.tv_sec = interval / USEC_PER_MSEC;
586 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100587 } else {
588 ts.tv_sec = 1;
589 ts.tv_nsec = 0;
590 }
591
Liming Wang60666c62009-12-31 16:05:50 +0800592 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100593 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300594 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900595 perror("failed to prepare workload");
596 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800597 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900598 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000599 }
600
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200601 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300602 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200603
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300604 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300605try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100606 if (create_perf_stat_counter(counter) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700607
608 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700609 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700610 counter->leader != counter &&
611 counter->weak_group) {
612 counter = perf_evsel__reset_weak_group(counter);
613 goto try_again;
614 }
615
David Ahern979987a2012-05-08 09:29:16 -0600616 /*
617 * PPC returns ENXIO for HW counters until 2.6.37
618 * (behavior changed with commit b0a873e).
619 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100620 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600621 errno == ENOENT || errno == EOPNOTSUPP ||
622 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900623 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600624 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300625 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600626 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400627
628 if ((counter->leader != counter) ||
629 !(counter->leader->nr_members > 1))
630 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300631 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900632 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300633 ui__warning("%s\n", msg);
634 goto try_again;
635 }
Ingo Molnarede70292011-04-28 08:48:42 +0200636
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300637 perf_evsel__open_strerror(counter, &target,
638 errno, msg, sizeof(msg));
639 ui__error("%s\n", msg);
640
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200641 if (child_pid != -1)
642 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600643
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200644 return -1;
645 }
David Ahern2cee77c2011-05-30 08:55:59 -0600646 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100647
648 l = strlen(counter->unit);
649 if (l > unit_width)
650 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100651
Jiri Olsa82bf3112017-07-26 14:02:06 +0200652 if (perf_evsel__should_store_id(counter) &&
653 store_counter_ids(counter))
Jiri Olsa2af46462015-11-05 15:40:49 +0100654 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300655 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200656
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300657 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300658 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300659 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300660 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100661 return -1;
662 }
663
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600664 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300665 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600666 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
667 str_error_r(errno, msg, sizeof(msg)));
668 return -1;
669 }
670
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100671 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100672 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100673
Jiri Olsa664c98d2015-11-05 15:40:50 +0100674 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100675 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100676 } else {
677 err = perf_session__write_header(perf_stat.session, evsel_list,
678 fd, false);
679 }
680
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100681 if (err < 0)
682 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100683
Jiri Olsa664c98d2015-11-05 15:40:50 +0100684 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100685 if (err < 0)
686 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100687 }
688
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200689 /*
690 * Enable counters and exec the command:
691 */
692 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100693 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200694
Liming Wang60666c62009-12-31 16:05:50 +0800695 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900696 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100697 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900698
Stephane Eranian13370a92013-01-29 12:47:44 +0100699 if (interval) {
700 while (!waitpid(child_pid, &status, WNOHANG)) {
701 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200702 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100703 }
704 }
Milian Wolffdfc9eec2017-09-12 17:25:23 +0200705 waitpid(child_pid, &status, 0);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300706
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300707 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300708 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300709 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300710 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300711 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300712
Andi Kleen33e49ea2011-09-15 14:31:40 -0700713 if (WIFSIGNALED(status))
714 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800715 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100716 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100717 while (!done) {
718 nanosleep(&ts, NULL);
719 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200720 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100721 }
Liming Wang60666c62009-12-31 16:05:50 +0800722 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200723
Mark Rutland3df33ef2016-08-09 14:04:29 +0100724 disable_counters();
725
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200726 t1 = rdclock();
727
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200728 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200729
Mark Rutland3df33ef2016-08-09 14:04:29 +0100730 /*
731 * Closing a group leader splits the group, and as we only disable
732 * group leaders, results in remaining events becoming enabled. To
733 * avoid arbitrary skew, we must read all counters before closing any
734 * group leaders.
735 */
736 read_counters();
737 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200738
Ingo Molnar42202dd2009-06-13 14:57:28 +0200739 return WEXITSTATUS(status);
740}
741
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300742static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200743{
744 int ret;
745
746 if (pre_cmd) {
747 ret = system(pre_cmd);
748 if (ret)
749 return ret;
750 }
751
752 if (sync_run)
753 sync();
754
755 ret = __run_perf_stat(argc, argv);
756 if (ret)
757 return ret;
758
759 if (post_cmd) {
760 ret = system(post_cmd);
761 if (ret)
762 return ret;
763 }
764
765 return ret;
766}
767
Andi Kleend73515c2015-03-11 07:16:27 -0700768static void print_running(u64 run, u64 ena)
769{
770 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200771 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700772 csv_sep,
773 run,
774 csv_sep,
775 ena ? 100.0 * run / ena : 100.0);
776 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200777 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700778 }
779}
780
Ingo Molnarf99844c2011-04-27 05:35:39 +0200781static void print_noise_pct(double total, double avg)
782{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800783 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200784
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700785 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200786 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600787 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200788 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200789}
790
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200791static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200792{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200793 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200794
Peter Zijlstra849abde2009-09-04 18:23:38 +0200795 if (run_count == 1)
796 return;
797
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300798 ps = evsel->stats;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200799 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200800}
801
Stephane Eranian12c08a92013-02-14 13:57:29 +0100802static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200803{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200804 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100805 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200806 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100807 cpu_map__id_to_socket(id),
808 csv_output ? 0 : -8,
809 cpu_map__id_to_cpu(id),
810 csv_sep,
811 csv_output ? 0 : 4,
812 nr,
813 csv_sep);
814 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100815 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200816 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100817 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100818 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100819 csv_sep,
820 csv_output ? 0 : 4,
821 nr,
822 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100823 break;
824 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200825 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200826 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100827 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100828 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200829 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200830 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200831 csv_output ? 0 : 16,
832 thread_map__comm(evsel->threads, id),
833 csv_output ? 0 : -8,
834 thread_map__pid(evsel->threads, id),
835 csv_sep);
836 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100837 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200838 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100839 default:
840 break;
841 }
842}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200843
Andi Kleen140aead2016-01-30 09:06:49 -0800844struct outstate {
845 FILE *fh;
846 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800847 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800848 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800849 int id, nr;
850 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800851};
852
853#define METRIC_LEN 35
854
855static void new_line_std(void *ctx)
856{
857 struct outstate *os = ctx;
858
859 os->newline = true;
860}
861
862static void do_new_line_std(struct outstate *os)
863{
864 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800865 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800866 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800867 if (stat_config.aggr_mode == AGGR_NONE)
868 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800869 fprintf(os->fh, " ");
870}
871
872static void print_metric_std(void *ctx, const char *color, const char *fmt,
873 const char *unit, double val)
874{
875 struct outstate *os = ctx;
876 FILE *out = os->fh;
877 int n;
878 bool newline = os->newline;
879
880 os->newline = false;
881
882 if (unit == NULL || fmt == NULL) {
883 fprintf(out, "%-*s", METRIC_LEN, "");
884 return;
885 }
886
887 if (newline)
888 do_new_line_std(os);
889
890 n = fprintf(out, " # ");
891 if (color)
892 n += color_fprintf(out, color, fmt, val);
893 else
894 n += fprintf(out, fmt, val);
895 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
896}
897
Andi Kleen92a61f62016-02-29 14:36:21 -0800898static void new_line_csv(void *ctx)
899{
900 struct outstate *os = ctx;
901 int i;
902
903 fputc('\n', os->fh);
904 if (os->prefix)
905 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800906 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800907 for (i = 0; i < os->nfields; i++)
908 fputs(csv_sep, os->fh);
909}
910
911static void print_metric_csv(void *ctx,
912 const char *color __maybe_unused,
913 const char *fmt, const char *unit, double val)
914{
915 struct outstate *os = ctx;
916 FILE *out = os->fh;
917 char buf[64], *vals, *ends;
918
919 if (unit == NULL || fmt == NULL) {
Ilya Pronin40c21892018-03-05 22:43:53 -0800920 fprintf(out, "%s%s", csv_sep, csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800921 return;
922 }
923 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900924 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800925 while (isdigit(*ends) || *ends == '.')
926 ends++;
927 *ends = 0;
928 while (isspace(*unit))
929 unit++;
930 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
931}
932
Andi Kleen54b50912016-03-03 15:57:36 -0800933#define METRIC_ONLY_LEN 20
934
935/* Filter out some columns that don't work well in metrics only mode */
936
937static bool valid_only_metric(const char *unit)
938{
939 if (!unit)
940 return false;
941 if (strstr(unit, "/sec") ||
942 strstr(unit, "hz") ||
943 strstr(unit, "Hz") ||
944 strstr(unit, "CPUs utilized"))
945 return false;
946 return true;
947}
948
949static const char *fixunit(char *buf, struct perf_evsel *evsel,
950 const char *unit)
951{
952 if (!strncmp(unit, "of all", 6)) {
953 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
954 unit);
955 return buf;
956 }
957 return unit;
958}
959
960static void print_metric_only(void *ctx, const char *color, const char *fmt,
961 const char *unit, double val)
962{
963 struct outstate *os = ctx;
964 FILE *out = os->fh;
965 int n;
966 char buf[1024];
967 unsigned mlen = METRIC_ONLY_LEN;
968
969 if (!valid_only_metric(unit))
970 return;
971 unit = fixunit(buf, os->evsel, unit);
972 if (color)
973 n = color_fprintf(out, color, fmt, val);
974 else
975 n = fprintf(out, fmt, val);
976 if (n > METRIC_ONLY_LEN)
977 n = METRIC_ONLY_LEN;
978 if (mlen < strlen(unit))
979 mlen = strlen(unit) + 1;
980 fprintf(out, "%*s", mlen - n, "");
981}
982
983static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
984 const char *fmt,
985 const char *unit, double val)
986{
987 struct outstate *os = ctx;
988 FILE *out = os->fh;
989 char buf[64], *vals, *ends;
990 char tbuf[1024];
991
992 if (!valid_only_metric(unit))
993 return;
994 unit = fixunit(tbuf, os->evsel, unit);
995 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900996 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -0800997 while (isdigit(*ends) || *ends == '.')
998 ends++;
999 *ends = 0;
1000 fprintf(out, "%s%s", vals, csv_sep);
1001}
1002
1003static void new_line_metric(void *ctx __maybe_unused)
1004{
1005}
1006
1007static void print_metric_header(void *ctx, const char *color __maybe_unused,
1008 const char *fmt __maybe_unused,
1009 const char *unit, double val __maybe_unused)
1010{
1011 struct outstate *os = ctx;
1012 char tbuf[1024];
1013
1014 if (!valid_only_metric(unit))
1015 return;
1016 unit = fixunit(tbuf, os->evsel, unit);
1017 if (csv_output)
1018 fprintf(os->fh, "%s%s", unit, csv_sep);
1019 else
1020 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
1021}
1022
Andi Kleenda88c7f2014-09-24 13:50:46 -07001023static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001024{
Jiri Olsa58215222015-07-21 14:31:24 +02001025 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001026 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +01001027 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -06001028 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001029
Stephane Eranian410136f2013-11-12 17:58:49 +01001030 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
1031 fmt_n = csv_output ? "%s" : "%-25s";
1032
Andi Kleenda88c7f2014-09-24 13:50:46 -07001033 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001034
David Ahern4bbe5a62013-09-28 14:28:00 -06001035 scnprintf(name, sizeof(name), "%s%s",
1036 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +01001037
1038 fprintf(output, fmt_v, msecs, csv_sep);
1039
1040 if (csv_output)
1041 fprintf(output, "%s%s", evsel->unit, csv_sep);
1042 else
1043 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1044
1045 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001046
Stephane Eranian023695d2011-02-14 11:20:01 +02001047 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001048 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001049}
1050
Andi Kleen44d49a62016-02-29 14:36:22 -08001051static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1052{
1053 int i;
1054
1055 if (!aggr_get_id)
1056 return 0;
1057
1058 if (stat_config.aggr_mode == AGGR_NONE)
1059 return id;
1060
1061 if (stat_config.aggr_mode == AGGR_GLOBAL)
1062 return 0;
1063
1064 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1065 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1066
1067 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1068 return cpu2;
1069 }
1070 return 0;
1071}
1072
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001073static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1074{
Jiri Olsa58215222015-07-21 14:31:24 +02001075 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001076 double sc = evsel->scale;
1077 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001078
1079 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001080 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001081 } else {
1082 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001083 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001084 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001085 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001086 }
1087
1088 aggr_printout(evsel, id, nr);
1089
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001090 fprintf(output, fmt, avg, csv_sep);
1091
1092 if (evsel->unit)
1093 fprintf(output, "%-*s%s",
1094 csv_output ? 0 : unit_width,
1095 evsel->unit, csv_sep);
1096
1097 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1098
1099 if (evsel->cgrp)
1100 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001101}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001102
Andi Kleenf9483392016-01-30 09:06:50 -08001103static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +08001104 char *prefix, u64 run, u64 ena, double noise,
1105 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001106{
Andi Kleen140aead2016-01-30 09:06:49 -08001107 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001108 struct outstate os = {
1109 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001110 .prefix = prefix ? prefix : "",
1111 .id = id,
1112 .nr = nr,
1113 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001114 };
Andi Kleen140aead2016-01-30 09:06:49 -08001115 print_metric_t pm = print_metric_std;
1116 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001117
Andi Kleen54b50912016-03-03 15:57:36 -08001118 if (metric_only) {
1119 nl = new_line_metric;
1120 if (csv_output)
1121 pm = print_metric_only_csv;
1122 else
1123 pm = print_metric_only;
1124 } else
1125 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001126
Andi Kleen54b50912016-03-03 15:57:36 -08001127 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001128 static int aggr_fields[] = {
1129 [AGGR_GLOBAL] = 0,
1130 [AGGR_THREAD] = 1,
1131 [AGGR_NONE] = 1,
1132 [AGGR_SOCKET] = 2,
1133 [AGGR_CORE] = 2,
1134 };
1135
1136 pm = print_metric_csv;
1137 nl = new_line_csv;
1138 os.nfields = 3;
1139 os.nfields += aggr_fields[stat_config.aggr_mode];
1140 if (counter->cgrp)
1141 os.nfields++;
1142 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001143 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001144 if (metric_only) {
1145 pm(&os, NULL, "", "", 0);
1146 return;
1147 }
Andi Kleencb110f42016-01-30 09:06:51 -08001148 aggr_printout(counter, id, nr);
1149
1150 fprintf(stat_config.output, "%*s%s",
1151 csv_output ? 0 : 18,
1152 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1153 csv_sep);
1154
Borislav Petkov02d492e2017-02-07 01:40:05 +01001155 if (counter->supported)
1156 print_free_counters_hint = 1;
1157
Andi Kleencb110f42016-01-30 09:06:51 -08001158 fprintf(stat_config.output, "%-*s%s",
1159 csv_output ? 0 : unit_width,
1160 counter->unit, csv_sep);
1161
1162 fprintf(stat_config.output, "%*s",
1163 csv_output ? 0 : -25,
1164 perf_evsel__name(counter));
1165
1166 if (counter->cgrp)
1167 fprintf(stat_config.output, "%s%s",
1168 csv_sep, counter->cgrp->name);
1169
Andi Kleen92a61f62016-02-29 14:36:21 -08001170 if (!csv_output)
1171 pm(&os, NULL, NULL, "", 0);
1172 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001173 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001174 if (csv_output)
1175 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001176 return;
1177 }
1178
Andi Kleen54b50912016-03-03 15:57:36 -08001179 if (metric_only)
1180 /* nothing */;
1181 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001182 nsec_printout(id, nr, counter, uval);
1183 else
1184 abs_printout(id, nr, counter, uval);
1185
Andi Kleen140aead2016-01-30 09:06:49 -08001186 out.print_metric = pm;
1187 out.new_line = nl;
1188 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001189 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001190
Andi Kleen54b50912016-03-03 15:57:36 -08001191 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001192 print_noise(counter, noise);
1193 print_running(run, ena);
1194 }
1195
1196 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001197 first_shadow_cpu(counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001198 &out, &metric_events, st);
Andi Kleen54b50912016-03-03 15:57:36 -08001199 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001200 print_noise(counter, noise);
1201 print_running(run, ena);
1202 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001203}
1204
Andi Kleen44d49a62016-02-29 14:36:22 -08001205static void aggr_update_shadow(void)
1206{
1207 int cpu, s2, id, s;
1208 u64 val;
1209 struct perf_evsel *counter;
1210
1211 for (s = 0; s < aggr_map->nr; s++) {
1212 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001213 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001214 val = 0;
1215 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1216 s2 = aggr_get_id(evsel_list->cpus, cpu);
1217 if (s2 != id)
1218 continue;
1219 val += perf_counts(counter->counts, cpu, 0)->val;
1220 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001221 perf_stat__update_shadow_stats(counter, val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001222 first_shadow_cpu(counter, id),
1223 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001224 }
1225 }
1226}
1227
Andi Kleen430daf22017-03-20 13:17:00 -07001228static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001229 void (*cb)(struct perf_evsel *counter, void *data,
1230 bool first),
1231 void *data)
1232{
Andi Kleen430daf22017-03-20 13:17:00 -07001233 struct perf_evsel *alias;
1234
1235 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1236 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1237 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1238 alias->scale != counter->scale ||
1239 alias->cgrp != counter->cgrp ||
1240 strcmp(alias->unit, counter->unit) ||
1241 nsec_counter(alias) != nsec_counter(counter))
1242 break;
1243 alias->merged_stat = true;
1244 cb(alias, data, false);
1245 }
1246}
1247
1248static bool collect_data(struct perf_evsel *counter,
1249 void (*cb)(struct perf_evsel *counter, void *data,
1250 bool first),
1251 void *data)
1252{
1253 if (counter->merged_stat)
1254 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001255 cb(counter, data, true);
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -03001256 if (!no_merge && counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001257 collect_all_aliases(counter, cb, data);
1258 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001259}
1260
1261struct aggr_data {
1262 u64 ena, run, val;
1263 int id;
1264 int nr;
1265 int cpu;
1266};
1267
1268static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1269{
1270 struct aggr_data *ad = data;
1271 int cpu, s2;
1272
1273 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1274 struct perf_counts_values *counts;
1275
1276 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1277 if (s2 != ad->id)
1278 continue;
1279 if (first)
1280 ad->nr++;
1281 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001282 /*
1283 * When any result is bad, make them all to give
1284 * consistent output in interval mode.
1285 */
1286 if (counts->ena == 0 || counts->run == 0 ||
1287 counter->counts->scaled == -1) {
1288 ad->ena = 0;
1289 ad->run = 0;
1290 break;
1291 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001292 ad->val += counts->val;
1293 ad->ena += counts->ena;
1294 ad->run += counts->run;
1295 }
1296}
1297
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001298static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001299{
Jiri Olsa58215222015-07-21 14:31:24 +02001300 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001301 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001302 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001303 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001304 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001305 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001306
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001307 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001308 return;
1309
Andi Kleen44d49a62016-02-29 14:36:22 -08001310 aggr_update_shadow();
1311
Andi Kleen54b50912016-03-03 15:57:36 -08001312 /*
1313 * With metric_only everything is on a single line.
1314 * Without each counter has its own line.
1315 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001316 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001317 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001318 if (prefix && metric_only)
1319 fprintf(output, "%s", prefix);
1320
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001321 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001322 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001323 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001324 if (is_duration_time(counter))
1325 continue;
1326
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001327 ad.val = ad.ena = ad.run = 0;
1328 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001329 if (!collect_data(counter, aggr_cb, &ad))
1330 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001331 nr = ad.nr;
1332 ena = ad.ena;
1333 run = ad.run;
1334 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001335 if (first && metric_only) {
1336 first = false;
1337 aggr_printout(counter, id, nr);
1338 }
1339 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001340 fprintf(output, "%s", prefix);
1341
Stephane Eranian410136f2013-11-12 17:58:49 +01001342 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001343 printout(id, nr, counter, uval, prefix, run, ena, 1.0,
1344 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001345 if (!metric_only)
1346 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001347 }
Andi Kleen54b50912016-03-03 15:57:36 -08001348 if (metric_only)
1349 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001350 }
1351}
1352
Jin Yao29734552017-12-05 22:03:11 +08001353static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001354{
Jin Yao29734552017-12-05 22:03:11 +08001355 return ((struct perf_aggr_thread_value *)b)->val -
1356 ((struct perf_aggr_thread_value *)a)->val;
1357}
1358
1359static struct perf_aggr_thread_value *sort_aggr_thread(
1360 struct perf_evsel *counter,
1361 int nthreads, int ncpus,
1362 int *ret)
1363{
1364 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001365 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001366 struct perf_aggr_thread_value *buf;
1367
1368 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1369 if (!buf)
1370 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001371
1372 for (thread = 0; thread < nthreads; thread++) {
1373 u64 ena = 0, run = 0, val = 0;
1374
1375 for (cpu = 0; cpu < ncpus; cpu++) {
1376 val += perf_counts(counter->counts, cpu, thread)->val;
1377 ena += perf_counts(counter->counts, cpu, thread)->ena;
1378 run += perf_counts(counter->counts, cpu, thread)->run;
1379 }
1380
Jin Yao29734552017-12-05 22:03:11 +08001381 uval = val * counter->scale;
1382
1383 /*
1384 * Skip value 0 when enabling --per-thread globally,
1385 * otherwise too many 0 output.
1386 */
1387 if (uval == 0.0 && target__has_per_thread(&target))
1388 continue;
1389
1390 buf[i].counter = counter;
1391 buf[i].id = thread;
1392 buf[i].uval = uval;
1393 buf[i].val = val;
1394 buf[i].run = run;
1395 buf[i].ena = ena;
1396 i++;
1397 }
1398
1399 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1400
1401 if (ret)
1402 *ret = i;
1403
1404 return buf;
1405}
1406
1407static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1408{
1409 FILE *output = stat_config.output;
1410 int nthreads = thread_map__nr(counter->threads);
1411 int ncpus = cpu_map__nr(counter->cpus);
1412 int thread, sorted_threads, id;
1413 struct perf_aggr_thread_value *buf;
1414
1415 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads);
1416 if (!buf) {
1417 perror("cannot sort aggr thread");
1418 return;
1419 }
1420
1421 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001422 if (prefix)
1423 fprintf(output, "%s", prefix);
1424
Jin Yao29734552017-12-05 22:03:11 +08001425 id = buf[thread].id;
Jin Yao14e72a22017-12-05 22:03:08 +08001426 if (stat_config.stats)
Jin Yao29734552017-12-05 22:03:11 +08001427 printout(id, 0, buf[thread].counter, buf[thread].uval,
1428 prefix, buf[thread].run, buf[thread].ena, 1.0,
1429 &stat_config.stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001430 else
Jin Yao29734552017-12-05 22:03:11 +08001431 printout(id, 0, buf[thread].counter, buf[thread].uval,
1432 prefix, buf[thread].run, buf[thread].ena, 1.0,
1433 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001434 fputc('\n', output);
1435 }
Jin Yao29734552017-12-05 22:03:11 +08001436
1437 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001438}
1439
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001440struct caggr_data {
1441 double avg, avg_enabled, avg_running;
1442};
1443
1444static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1445 bool first __maybe_unused)
1446{
1447 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001448 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001449
1450 cd->avg += avg_stats(&ps->res_stats[0]);
1451 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1452 cd->avg_running += avg_stats(&ps->res_stats[2]);
1453}
1454
Ingo Molnar42202dd2009-06-13 14:57:28 +02001455/*
1456 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001457 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001458 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001459static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001460{
Jiri Olsa58215222015-07-21 14:31:24 +02001461 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001462 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001463 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001464
Andi Kleen430daf22017-03-20 13:17:00 -07001465 if (!collect_data(counter, counter_aggr_cb, &cd))
1466 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001467
Andi Kleen54b50912016-03-03 15:57:36 -08001468 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001469 fprintf(output, "%s", prefix);
1470
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001471 uval = cd.avg * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001472 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
1473 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001474 if (!metric_only)
1475 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001476}
1477
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001478static void counter_cb(struct perf_evsel *counter, void *data,
1479 bool first __maybe_unused)
1480{
1481 struct aggr_data *ad = data;
1482
1483 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1484 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1485 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1486}
1487
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001488/*
1489 * Print out the results of a single counter:
1490 * does not use aggregated count in system-wide
1491 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001492static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001493{
Jiri Olsa58215222015-07-21 14:31:24 +02001494 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001495 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001496 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001497 int cpu;
1498
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001499 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001500 struct aggr_data ad = { .cpu = cpu };
1501
Andi Kleen430daf22017-03-20 13:17:00 -07001502 if (!collect_data(counter, counter_cb, &ad))
1503 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001504 val = ad.val;
1505 ena = ad.ena;
1506 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001507
1508 if (prefix)
1509 fprintf(output, "%s", prefix);
1510
Stephane Eranian410136f2013-11-12 17:58:49 +01001511 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001512 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1513 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001514
Stephane Eranian4aa90152011-08-15 22:22:33 +02001515 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001516 }
1517}
1518
Andi Kleen206cab62016-03-03 15:57:37 -08001519static void print_no_aggr_metric(char *prefix)
1520{
1521 int cpu;
1522 int nrcpus = 0;
1523 struct perf_evsel *counter;
1524 u64 ena, run, val;
1525 double uval;
1526
1527 nrcpus = evsel_list->cpus->nr;
1528 for (cpu = 0; cpu < nrcpus; cpu++) {
1529 bool first = true;
1530
1531 if (prefix)
1532 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001533 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001534 if (is_duration_time(counter))
1535 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001536 if (first) {
1537 aggr_printout(counter, cpu, 0);
1538 first = false;
1539 }
1540 val = perf_counts(counter->counts, cpu, 0)->val;
1541 ena = perf_counts(counter->counts, cpu, 0)->ena;
1542 run = perf_counts(counter->counts, cpu, 0)->run;
1543
1544 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001545 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1546 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001547 }
1548 fputc('\n', stat_config.output);
1549 }
1550}
1551
Andi Kleen54b50912016-03-03 15:57:36 -08001552static int aggr_header_lens[] = {
1553 [AGGR_CORE] = 18,
1554 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001555 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001556 [AGGR_THREAD] = 24,
1557 [AGGR_GLOBAL] = 0,
1558};
1559
Andi Kleenc51fd632016-05-24 12:52:39 -07001560static const char *aggr_header_csv[] = {
1561 [AGGR_CORE] = "core,cpus,",
1562 [AGGR_SOCKET] = "socket,cpus",
1563 [AGGR_NONE] = "cpu,",
1564 [AGGR_THREAD] = "comm-pid,",
1565 [AGGR_GLOBAL] = ""
1566};
1567
Andi Kleen41c8ca22016-05-24 12:52:38 -07001568static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001569{
1570 struct perf_stat_output_ctx out;
1571 struct perf_evsel *counter;
1572 struct outstate os = {
1573 .fh = stat_config.output
1574 };
1575
1576 if (prefix)
1577 fprintf(stat_config.output, "%s", prefix);
1578
Andi Kleen41c8ca22016-05-24 12:52:38 -07001579 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001580 fprintf(stat_config.output, "%*s",
1581 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001582 if (csv_output) {
1583 if (stat_config.interval)
1584 fputs("time,", stat_config.output);
1585 fputs(aggr_header_csv[stat_config.aggr_mode],
1586 stat_config.output);
1587 }
Andi Kleen54b50912016-03-03 15:57:36 -08001588
1589 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001590 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001591 if (is_duration_time(counter))
1592 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001593 os.evsel = counter;
1594 out.ctx = &os;
1595 out.print_metric = print_metric_header;
1596 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001597 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001598 os.evsel = counter;
1599 perf_stat__print_shadow_stats(counter, 0,
1600 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001601 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001602 &metric_events,
1603 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001604 }
1605 fputc('\n', stat_config.output);
1606}
1607
Jiri Olsad4f63a42015-06-26 11:29:26 +02001608static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001609{
Jiri Olsa58215222015-07-21 14:31:24 +02001610 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001611 static int num_print_interval;
1612
1613 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1614
Andi Kleen41c8ca22016-05-24 12:52:38 -07001615 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001616 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001617 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001618 fprintf(output, "# time socket cpus");
1619 if (!metric_only)
1620 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001621 break;
1622 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001623 fprintf(output, "# time core cpus");
1624 if (!metric_only)
1625 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001626 break;
1627 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001628 fprintf(output, "# time CPU");
1629 if (!metric_only)
1630 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001631 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001632 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001633 fprintf(output, "# time comm-pid");
1634 if (!metric_only)
1635 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001636 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001637 case AGGR_GLOBAL:
1638 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001639 fprintf(output, "# time");
1640 if (!metric_only)
1641 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001642 case AGGR_UNSET:
1643 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001644 }
1645 }
1646
Andi Kleen41c8ca22016-05-24 12:52:38 -07001647 if (num_print_interval == 0 && metric_only)
1648 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001649 if (++num_print_interval == 25)
1650 num_print_interval = 0;
1651}
1652
1653static void print_header(int argc, const char **argv)
1654{
Jiri Olsa58215222015-07-21 14:31:24 +02001655 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001656 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001657
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001658 fflush(stdout);
1659
Stephane Eraniand7470b62010-12-01 18:49:05 +02001660 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001661 fprintf(output, "\n");
1662 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001663 if (target.system_wide)
1664 fprintf(output, "\'system wide");
1665 else if (target.cpu_list)
1666 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001667 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001668 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1669 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001670 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001671 } else if (target.pid)
1672 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001673 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001674 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001675
Stephane Eranian4aa90152011-08-15 22:22:33 +02001676 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001677 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001678 fprintf(output, " (%d runs)", run_count);
1679 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001680 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001681}
1682
1683static void print_footer(void)
1684{
Jiri Olsa58215222015-07-21 14:31:24 +02001685 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001686 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001687
Jiri Olsad4f63a42015-06-26 11:29:26 +02001688 if (!null_run)
1689 fprintf(output, "\n");
1690 fprintf(output, " %17.9f seconds time elapsed",
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001691 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001692 if (run_count > 1) {
1693 fprintf(output, " ");
1694 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1695 avg_stats(&walltime_nsecs_stats));
1696 }
1697 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001698
Andi Kleen918c7b062017-05-22 18:00:16 -07001699 if (print_free_counters_hint &&
1700 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1701 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001702 fprintf(output,
1703"Some events weren't counted. Try disabling the NMI watchdog:\n"
1704" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1705" perf stat ...\n"
1706" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001707}
1708
1709static void print_counters(struct timespec *ts, int argc, const char **argv)
1710{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001711 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001712 struct perf_evsel *counter;
1713 char buf[64], *prefix = NULL;
1714
Jiri Olsa664c98d2015-11-05 15:40:50 +01001715 /* Do not print anything if we record to the pipe. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001716 if (STAT_RECORD && perf_stat.data.is_pipe)
Jiri Olsa664c98d2015-11-05 15:40:50 +01001717 return;
1718
Jiri Olsad4f63a42015-06-26 11:29:26 +02001719 if (interval)
1720 print_interval(prefix = buf, ts);
1721 else
1722 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001723
Andi Kleen54b50912016-03-03 15:57:36 -08001724 if (metric_only) {
1725 static int num_print_iv;
1726
Andi Kleen41c8ca22016-05-24 12:52:38 -07001727 if (num_print_iv == 0 && !interval)
1728 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001729 if (num_print_iv++ == 25)
1730 num_print_iv = 0;
1731 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1732 fprintf(stat_config.output, "%s", prefix);
1733 }
1734
Jiri Olsa421a50f2015-07-21 14:31:22 +02001735 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001736 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001737 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001738 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001739 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001740 case AGGR_THREAD:
Andi Kleene864c5c2017-08-31 12:40:35 -07001741 evlist__for_each_entry(evsel_list, counter) {
1742 if (is_duration_time(counter))
1743 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001744 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001745 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001746 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001747 case AGGR_GLOBAL:
Andi Kleene864c5c2017-08-31 12:40:35 -07001748 evlist__for_each_entry(evsel_list, counter) {
1749 if (is_duration_time(counter))
1750 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001751 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001752 }
Andi Kleen54b50912016-03-03 15:57:36 -08001753 if (metric_only)
1754 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001755 break;
1756 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001757 if (metric_only)
1758 print_no_aggr_metric(prefix);
1759 else {
Andi Kleene864c5c2017-08-31 12:40:35 -07001760 evlist__for_each_entry(evsel_list, counter) {
1761 if (is_duration_time(counter))
1762 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001763 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001764 }
Andi Kleen206cab62016-03-03 15:57:37 -08001765 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001766 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001767 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001768 default:
1769 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001770 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001771
Jiri Olsad4f63a42015-06-26 11:29:26 +02001772 if (!interval && !csv_output)
1773 print_footer();
1774
Jiri Olsa58215222015-07-21 14:31:24 +02001775 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001776}
1777
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001778static volatile int signr = -1;
1779
Ingo Molnar52425192009-05-26 09:17:18 +02001780static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001781{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001782 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001783 done = 1;
1784
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001785 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001786 /*
1787 * render child_pid harmless
1788 * won't send SIGTERM to a random
1789 * process in case of race condition
1790 * and fast PID recycling
1791 */
1792 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001793}
1794
1795static void sig_atexit(void)
1796{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001797 sigset_t set, oset;
1798
1799 /*
1800 * avoid race condition with SIGCHLD handler
1801 * in skip_signal() which is modifying child_pid
1802 * goal is to avoid send SIGTERM to a random
1803 * process
1804 */
1805 sigemptyset(&set);
1806 sigaddset(&set, SIGCHLD);
1807 sigprocmask(SIG_BLOCK, &set, &oset);
1808
Chris Wilson933da832009-10-04 01:35:01 +01001809 if (child_pid != -1)
1810 kill(child_pid, SIGTERM);
1811
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001812 sigprocmask(SIG_SETMASK, &oset, NULL);
1813
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001814 if (signr == -1)
1815 return;
1816
1817 signal(signr, SIG_DFL);
1818 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001819}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001820
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001821static int stat__set_big_num(const struct option *opt __maybe_unused,
1822 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001823{
1824 big_num_opt = unset ? 0 : 1;
1825 return 0;
1826}
1827
Andi Kleen44b1e602016-05-30 12:49:42 -03001828static int enable_metric_only(const struct option *opt __maybe_unused,
1829 const char *s __maybe_unused, int unset)
1830{
1831 force_metric_only = true;
1832 metric_only = !unset;
1833 return 0;
1834}
1835
Andi Kleenb18f3e32017-08-31 12:40:31 -07001836static int parse_metric_groups(const struct option *opt,
1837 const char *str,
1838 int unset __maybe_unused)
1839{
1840 return metricgroup__parse_groups(opt, str, &metric_events);
1841}
1842
Jiri Olsae0547312015-11-05 15:40:45 +01001843static const struct option stat_options[] = {
1844 OPT_BOOLEAN('T', "transaction", &transaction_run,
1845 "hardware transaction statistics"),
1846 OPT_CALLBACK('e', "event", &evsel_list, "event",
1847 "event selector. use 'perf list' to list available events",
1848 parse_events_option),
1849 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1850 "event filter", parse_filter),
1851 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1852 "child tasks do not inherit counters"),
1853 OPT_STRING('p', "pid", &target.pid, "pid",
1854 "stat events on existing process id"),
1855 OPT_STRING('t', "tid", &target.tid, "tid",
1856 "stat events on existing thread id"),
1857 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1858 "system-wide collection from all CPUs"),
1859 OPT_BOOLEAN('g', "group", &group,
1860 "put the counters into a counter group"),
1861 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1862 OPT_INCR('v', "verbose", &verbose,
1863 "be more verbose (show counter open errors, etc)"),
1864 OPT_INTEGER('r', "repeat", &run_count,
1865 "repeat command and print average + stddev (max: 100, forever: 0)"),
1866 OPT_BOOLEAN('n', "null", &null_run,
1867 "null run - dont start any counters"),
1868 OPT_INCR('d', "detailed", &detailed_run,
1869 "detailed run - start a lot of events"),
1870 OPT_BOOLEAN('S', "sync", &sync_run,
1871 "call sync() before starting a run"),
1872 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1873 "print large numbers with thousands\' separators",
1874 stat__set_big_num),
1875 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1876 "list of cpus to monitor in system-wide"),
1877 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1878 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001879 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001880 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1881 "print counts with custom separator"),
1882 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1883 "monitor event in cgroup name only", parse_cgroups),
1884 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1885 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1886 OPT_INTEGER(0, "log-fd", &output_fd,
1887 "log output to fd, instead of stderr"),
1888 OPT_STRING(0, "pre", &pre_cmd, "command",
1889 "command to run prior to the measured command"),
1890 OPT_STRING(0, "post", &post_cmd, "command",
1891 "command to run after to the measured command"),
1892 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1893 "print counts at regular interval in ms (>= 10)"),
1894 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1895 "aggregate counts per processor socket", AGGR_SOCKET),
1896 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1897 "aggregate counts per physical processor core", AGGR_CORE),
1898 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1899 "aggregate counts per thread", AGGR_THREAD),
1900 OPT_UINTEGER('D', "delay", &initial_delay,
1901 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001902 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1903 "Only print computed metrics. No raw values", enable_metric_only),
1904 OPT_BOOLEAN(0, "topdown", &topdown_run,
1905 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001906 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1907 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001908 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1909 "monitor specified metrics or metric groups (separated by ,)",
1910 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01001911 OPT_END()
1912};
1913
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001914static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1915{
1916 return cpu_map__get_socket(map, cpu, NULL);
1917}
1918
1919static int perf_stat__get_core(struct cpu_map *map, int cpu)
1920{
1921 return cpu_map__get_core(map, cpu, NULL);
1922}
1923
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001924static int cpu_map__get_max(struct cpu_map *map)
1925{
1926 int i, max = -1;
1927
1928 for (i = 0; i < map->nr; i++) {
1929 if (map->map[i] > max)
1930 max = map->map[i];
1931 }
1932
1933 return max;
1934}
1935
1936static struct cpu_map *cpus_aggr_map;
1937
1938static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1939{
1940 int cpu;
1941
1942 if (idx >= map->nr)
1943 return -1;
1944
1945 cpu = map->map[idx];
1946
1947 if (cpus_aggr_map->map[cpu] == -1)
1948 cpus_aggr_map->map[cpu] = get_id(map, idx);
1949
1950 return cpus_aggr_map->map[cpu];
1951}
1952
1953static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1954{
1955 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1956}
1957
1958static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1959{
1960 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1961}
1962
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001963static int perf_stat_init_aggr_mode(void)
1964{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001965 int nr;
1966
Jiri Olsa421a50f2015-07-21 14:31:22 +02001967 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001968 case AGGR_SOCKET:
1969 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1970 perror("cannot build socket map");
1971 return -1;
1972 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001973 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001974 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001975 case AGGR_CORE:
1976 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1977 perror("cannot build core map");
1978 return -1;
1979 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001980 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001981 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001982 case AGGR_NONE:
1983 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001984 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001985 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001986 default:
1987 break;
1988 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001989
1990 /*
1991 * The evsel_list->cpus is the base we operate on,
1992 * taking the highest cpu number to be the size of
1993 * the aggregation translate cpumap.
1994 */
1995 nr = cpu_map__get_max(evsel_list->cpus);
1996 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1997 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001998}
1999
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002000static void perf_stat__exit_aggr_mode(void)
2001{
2002 cpu_map__put(aggr_map);
2003 cpu_map__put(cpus_aggr_map);
2004 aggr_map = NULL;
2005 cpus_aggr_map = NULL;
2006}
2007
Jiri Olsa68d702f2015-11-05 15:40:58 +01002008static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2009{
2010 int cpu;
2011
2012 if (idx > map->nr)
2013 return -1;
2014
2015 cpu = map->map[idx];
2016
Jan Stancekda8a58b2017-02-17 12:10:26 +01002017 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002018 return -1;
2019
2020 return cpu;
2021}
2022
2023static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2024{
2025 struct perf_env *env = data;
2026 int cpu = perf_env__get_cpu(env, map, idx);
2027
2028 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2029}
2030
2031static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2032{
2033 struct perf_env *env = data;
2034 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2035
2036 if (cpu != -1) {
2037 int socket_id = env->cpu[cpu].socket_id;
2038
2039 /*
2040 * Encode socket in upper 16 bits
2041 * core_id is relative to socket, and
2042 * we need a global id. So we combine
2043 * socket + core id.
2044 */
2045 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2046 }
2047
2048 return core;
2049}
2050
2051static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2052 struct cpu_map **sockp)
2053{
2054 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2055}
2056
2057static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2058 struct cpu_map **corep)
2059{
2060 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2061}
2062
2063static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2064{
2065 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2066}
2067
2068static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2069{
2070 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2071}
2072
2073static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2074{
2075 struct perf_env *env = &st->session->header.env;
2076
2077 switch (stat_config.aggr_mode) {
2078 case AGGR_SOCKET:
2079 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2080 perror("cannot build socket map");
2081 return -1;
2082 }
2083 aggr_get_id = perf_stat__get_socket_file;
2084 break;
2085 case AGGR_CORE:
2086 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2087 perror("cannot build core map");
2088 return -1;
2089 }
2090 aggr_get_id = perf_stat__get_core_file;
2091 break;
2092 case AGGR_NONE:
2093 case AGGR_GLOBAL:
2094 case AGGR_THREAD:
2095 case AGGR_UNSET:
2096 default:
2097 break;
2098 }
2099
2100 return 0;
2101}
2102
Andi Kleen44b1e602016-05-30 12:49:42 -03002103static int topdown_filter_events(const char **attr, char **str, bool use_group)
2104{
2105 int off = 0;
2106 int i;
2107 int len = 0;
2108 char *s;
2109
2110 for (i = 0; attr[i]; i++) {
2111 if (pmu_have_event("cpu", attr[i])) {
2112 len += strlen(attr[i]) + 1;
2113 attr[i - off] = attr[i];
2114 } else
2115 off++;
2116 }
2117 attr[i - off] = NULL;
2118
2119 *str = malloc(len + 1 + 2);
2120 if (!*str)
2121 return -1;
2122 s = *str;
2123 if (i - off == 0) {
2124 *s = 0;
2125 return 0;
2126 }
2127 if (use_group)
2128 *s++ = '{';
2129 for (i = 0; attr[i]; i++) {
2130 strcpy(s, attr[i]);
2131 s += strlen(s);
2132 *s++ = ',';
2133 }
2134 if (use_group) {
2135 s[-1] = '}';
2136 *s = 0;
2137 } else
2138 s[-1] = 0;
2139 return 0;
2140}
2141
2142__weak bool arch_topdown_check_group(bool *warn)
2143{
2144 *warn = false;
2145 return false;
2146}
2147
2148__weak void arch_topdown_group_warn(void)
2149{
2150}
2151
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002152/*
2153 * Add default attributes, if there were no attributes specified or
2154 * if -d/--detailed, -d -d or -d -d -d is used:
2155 */
2156static int add_default_attributes(void)
2157{
Andi Kleen44b1e602016-05-30 12:49:42 -03002158 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002159 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002160
2161 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2162 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2163 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2164 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2165
2166 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002167};
2168 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002169 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002170};
2171 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002172 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002173};
2174 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002175 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2176 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2177 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2178
2179};
2180
2181/*
2182 * Detailed stats (-d), covering the L1 and last level data caches:
2183 */
2184 struct perf_event_attr detailed_attrs[] = {
2185
2186 { .type = PERF_TYPE_HW_CACHE,
2187 .config =
2188 PERF_COUNT_HW_CACHE_L1D << 0 |
2189 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2190 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2191
2192 { .type = PERF_TYPE_HW_CACHE,
2193 .config =
2194 PERF_COUNT_HW_CACHE_L1D << 0 |
2195 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2196 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2197
2198 { .type = PERF_TYPE_HW_CACHE,
2199 .config =
2200 PERF_COUNT_HW_CACHE_LL << 0 |
2201 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2202 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2203
2204 { .type = PERF_TYPE_HW_CACHE,
2205 .config =
2206 PERF_COUNT_HW_CACHE_LL << 0 |
2207 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2208 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2209};
2210
2211/*
2212 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2213 */
2214 struct perf_event_attr very_detailed_attrs[] = {
2215
2216 { .type = PERF_TYPE_HW_CACHE,
2217 .config =
2218 PERF_COUNT_HW_CACHE_L1I << 0 |
2219 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2220 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2221
2222 { .type = PERF_TYPE_HW_CACHE,
2223 .config =
2224 PERF_COUNT_HW_CACHE_L1I << 0 |
2225 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2226 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2227
2228 { .type = PERF_TYPE_HW_CACHE,
2229 .config =
2230 PERF_COUNT_HW_CACHE_DTLB << 0 |
2231 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2232 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2233
2234 { .type = PERF_TYPE_HW_CACHE,
2235 .config =
2236 PERF_COUNT_HW_CACHE_DTLB << 0 |
2237 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2238 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2239
2240 { .type = PERF_TYPE_HW_CACHE,
2241 .config =
2242 PERF_COUNT_HW_CACHE_ITLB << 0 |
2243 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2244 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2245
2246 { .type = PERF_TYPE_HW_CACHE,
2247 .config =
2248 PERF_COUNT_HW_CACHE_ITLB << 0 |
2249 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2250 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2251
2252};
2253
2254/*
2255 * Very, very detailed stats (-d -d -d), adding prefetch events:
2256 */
2257 struct perf_event_attr very_very_detailed_attrs[] = {
2258
2259 { .type = PERF_TYPE_HW_CACHE,
2260 .config =
2261 PERF_COUNT_HW_CACHE_L1D << 0 |
2262 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2263 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2264
2265 { .type = PERF_TYPE_HW_CACHE,
2266 .config =
2267 PERF_COUNT_HW_CACHE_L1D << 0 |
2268 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2269 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2270};
2271
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002272 /* Set attrs if no event is selected and !null_run: */
2273 if (null_run)
2274 return 0;
2275
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002276 if (transaction_run) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002277 if (pmu_have_event("cpu", "cycles-ct") &&
2278 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02002279 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002280 else
Jiri Olsaa4547422015-06-03 16:25:53 +02002281 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2282 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002283 fprintf(stderr, "Cannot set up transaction events\n");
2284 return -1;
2285 }
2286 return 0;
2287 }
2288
Kan Liangdaefd0b2017-05-26 12:05:38 -07002289 if (smi_cost) {
2290 int smi;
2291
2292 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2293 fprintf(stderr, "freeze_on_smi is not supported.\n");
2294 return -1;
2295 }
2296
2297 if (!smi) {
2298 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2299 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2300 return -1;
2301 }
2302 smi_reset = true;
2303 }
2304
2305 if (pmu_have_event("msr", "aperf") &&
2306 pmu_have_event("msr", "smi")) {
2307 if (!force_metric_only)
2308 metric_only = true;
2309 err = parse_events(evsel_list, smi_cost_attrs, NULL);
2310 } else {
2311 fprintf(stderr, "To measure SMI cost, it needs "
2312 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2313 return -1;
2314 }
2315 if (err) {
2316 fprintf(stderr, "Cannot set up SMI cost events\n");
2317 return -1;
2318 }
2319 return 0;
2320 }
2321
Andi Kleen44b1e602016-05-30 12:49:42 -03002322 if (topdown_run) {
2323 char *str = NULL;
2324 bool warn = false;
2325
2326 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2327 stat_config.aggr_mode != AGGR_CORE) {
2328 pr_err("top down event configuration requires --per-core mode\n");
2329 return -1;
2330 }
2331 stat_config.aggr_mode = AGGR_CORE;
2332 if (nr_cgroups || !target__has_cpu(&target)) {
2333 pr_err("top down event configuration requires system-wide mode (-a)\n");
2334 return -1;
2335 }
2336
2337 if (!force_metric_only)
2338 metric_only = true;
2339 if (topdown_filter_events(topdown_attrs, &str,
2340 arch_topdown_check_group(&warn)) < 0) {
2341 pr_err("Out of memory\n");
2342 return -1;
2343 }
2344 if (topdown_attrs[0] && str) {
2345 if (warn)
2346 arch_topdown_group_warn();
2347 err = parse_events(evsel_list, str, NULL);
2348 if (err) {
2349 fprintf(stderr,
2350 "Cannot set up top down events %s: %d\n",
2351 str, err);
2352 free(str);
2353 return -1;
2354 }
2355 } else {
2356 fprintf(stderr, "System does not support topdown\n");
2357 return -1;
2358 }
2359 free(str);
2360 }
2361
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002362 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002363 if (target__has_cpu(&target))
2364 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2365
Andi Kleen9dec4472016-02-26 16:27:56 -08002366 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2367 return -1;
2368 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2369 if (perf_evlist__add_default_attrs(evsel_list,
2370 frontend_attrs) < 0)
2371 return -1;
2372 }
2373 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2374 if (perf_evlist__add_default_attrs(evsel_list,
2375 backend_attrs) < 0)
2376 return -1;
2377 }
2378 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002379 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002380 }
2381
2382 /* Detailed events get appended to the event list: */
2383
2384 if (detailed_run < 1)
2385 return 0;
2386
2387 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002388 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002389 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002390
2391 if (detailed_run < 2)
2392 return 0;
2393
2394 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002395 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002396 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002397
2398 if (detailed_run < 3)
2399 return 0;
2400
2401 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002402 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002403}
2404
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002405static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002406 "perf stat record [<options>]",
2407 NULL,
2408};
2409
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002410static void init_features(struct perf_session *session)
2411{
2412 int feat;
2413
2414 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2415 perf_header__set_feat(&session->header, feat);
2416
2417 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2418 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2419 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2420 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2421}
2422
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002423static int __cmd_record(int argc, const char **argv)
2424{
2425 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002426 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002427
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002428 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002429 PARSE_OPT_STOP_AT_NON_OPTION);
2430
2431 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002432 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002433
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002434 if (run_count != 1 || forever) {
2435 pr_err("Cannot use -r option with perf stat record.\n");
2436 return -1;
2437 }
2438
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002439 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002440 if (session == NULL) {
2441 pr_err("Perf session creation failed.\n");
2442 return -1;
2443 }
2444
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002445 init_features(session);
2446
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002447 session->evlist = evsel_list;
2448 perf_stat.session = session;
2449 perf_stat.record = true;
2450 return argc;
2451}
2452
Jiri Olsaa56f9392015-11-05 15:40:59 +01002453static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2454 union perf_event *event,
2455 struct perf_session *session)
2456{
Andi Kleene3b03b62016-05-05 16:04:03 -07002457 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002458 struct perf_evsel *counter;
2459 struct timespec tsh, *ts = NULL;
2460 const char **argv = session->header.env.cmdline_argv;
2461 int argc = session->header.env.nr_cmdline;
2462
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002463 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002464 perf_stat_process_counter(&stat_config, counter);
2465
Andi Kleene3b03b62016-05-05 16:04:03 -07002466 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2467 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002468
Andi Kleene3b03b62016-05-05 16:04:03 -07002469 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002470 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2471 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002472 ts = &tsh;
2473 }
2474
2475 print_counters(ts, argc, argv);
2476 return 0;
2477}
2478
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002479static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002480int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002481 union perf_event *event,
2482 struct perf_session *session __maybe_unused)
2483{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002484 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2485
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002486 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002487
Jiri Olsa89af4e02015-11-05 15:41:02 +01002488 if (cpu_map__empty(st->cpus)) {
2489 if (st->aggr_mode != AGGR_UNSET)
2490 pr_warning("warning: processing task data, aggregation mode not set\n");
2491 return 0;
2492 }
2493
2494 if (st->aggr_mode != AGGR_UNSET)
2495 stat_config.aggr_mode = st->aggr_mode;
2496
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002497 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002498 perf_stat_init_aggr_mode();
2499 else
2500 perf_stat_init_aggr_mode_file(st);
2501
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002502 return 0;
2503}
2504
Jiri Olsa1975d362015-11-05 15:40:56 +01002505static int set_maps(struct perf_stat *st)
2506{
2507 if (!st->cpus || !st->threads)
2508 return 0;
2509
2510 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2511 return -EINVAL;
2512
2513 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2514
2515 if (perf_evlist__alloc_stats(evsel_list, true))
2516 return -ENOMEM;
2517
2518 st->maps_allocated = true;
2519 return 0;
2520}
2521
2522static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002523int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002524 union perf_event *event,
2525 struct perf_session *session __maybe_unused)
2526{
2527 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2528
2529 if (st->threads) {
2530 pr_warning("Extra thread map event, ignoring.\n");
2531 return 0;
2532 }
2533
2534 st->threads = thread_map__new_event(&event->thread_map);
2535 if (!st->threads)
2536 return -ENOMEM;
2537
2538 return set_maps(st);
2539}
2540
2541static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002542int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002543 union perf_event *event,
2544 struct perf_session *session __maybe_unused)
2545{
2546 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2547 struct cpu_map *cpus;
2548
2549 if (st->cpus) {
2550 pr_warning("Extra cpu map event, ignoring.\n");
2551 return 0;
2552 }
2553
2554 cpus = cpu_map__new_data(&event->cpu_map.data);
2555 if (!cpus)
2556 return -ENOMEM;
2557
2558 st->cpus = cpus;
2559 return set_maps(st);
2560}
2561
Jin Yao56739442017-12-05 22:03:07 +08002562static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2563{
2564 int i;
2565
2566 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2567 if (!config->stats)
2568 return -1;
2569
2570 config->stats_num = nthreads;
2571
2572 for (i = 0; i < nthreads; i++)
2573 runtime_stat__init(&config->stats[i]);
2574
2575 return 0;
2576}
2577
2578static void runtime_stat_delete(struct perf_stat_config *config)
2579{
2580 int i;
2581
2582 if (!config->stats)
2583 return;
2584
2585 for (i = 0; i < config->stats_num; i++)
2586 runtime_stat__exit(&config->stats[i]);
2587
2588 free(config->stats);
2589}
2590
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002591static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002592 "perf stat report [<options>]",
2593 NULL,
2594};
2595
2596static struct perf_stat perf_stat = {
2597 .tool = {
2598 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002599 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002600 .thread_map = process_thread_map_event,
2601 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002602 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002603 .stat = perf_event__process_stat_event,
2604 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002605 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002606 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002607};
2608
2609static int __cmd_report(int argc, const char **argv)
2610{
2611 struct perf_session *session;
2612 const struct option options[] = {
2613 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002614 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2615 "aggregate counts per processor socket", AGGR_SOCKET),
2616 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2617 "aggregate counts per physical processor core", AGGR_CORE),
2618 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2619 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002620 OPT_END()
2621 };
2622 struct stat st;
2623 int ret;
2624
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002625 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002626
2627 if (!input_name || !strlen(input_name)) {
2628 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2629 input_name = "-";
2630 else
2631 input_name = "perf.data";
2632 }
2633
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002634 perf_stat.data.file.path = input_name;
2635 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002636
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002637 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002638 if (session == NULL)
2639 return -1;
2640
2641 perf_stat.session = session;
2642 stat_config.output = stderr;
2643 evsel_list = session->evlist;
2644
2645 ret = perf_session__process_events(session);
2646 if (ret)
2647 return ret;
2648
2649 perf_session__delete(session);
2650 return 0;
2651}
2652
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002653static void setup_system_wide(int forks)
2654{
2655 /*
2656 * Make system wide (-a) the default target if
2657 * no target was specified and one of following
2658 * conditions is met:
2659 *
2660 * - there's no workload specified
2661 * - there is workload specified but all requested
2662 * events are system wide events
2663 */
2664 if (!target__none(&target))
2665 return;
2666
2667 if (!forks)
2668 target.system_wide = true;
2669 else {
2670 struct perf_evsel *counter;
2671
2672 evlist__for_each_entry(evsel_list, counter) {
2673 if (!counter->system_wide)
2674 return;
2675 }
2676
2677 if (evsel_list->nr_entries)
2678 target.system_wide = true;
2679 }
2680}
2681
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002682int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002683{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002684 const char * const stat_usage[] = {
2685 "perf stat [<options>] [<command>]",
2686 NULL
2687 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002688 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002689 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002690 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002691 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002692 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002693
Stephane Eranian5af52b52010-05-18 15:00:01 +02002694 setlocale(LC_ALL, "");
2695
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002696 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002697 if (evsel_list == NULL)
2698 return -ENOMEM;
2699
Wang Nan1669e502016-02-19 11:43:58 +00002700 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002701 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2702 (const char **) stat_usage,
2703 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002704 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002705 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002706
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002707 if (csv_sep) {
2708 csv_output = true;
2709 if (!strcmp(csv_sep, "\\t"))
2710 csv_sep = "\t";
2711 } else
2712 csv_sep = DEFAULT_SEPARATOR;
2713
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002714 if (argc && !strncmp(argv[0], "rec", 3)) {
2715 argc = __cmd_record(argc, argv);
2716 if (argc < 0)
2717 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002718 } else if (argc && !strncmp(argv[0], "rep", 3))
2719 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002720
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002721 interval = stat_config.interval;
2722
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002723 /*
2724 * For record command the -o is already taken care of.
2725 */
2726 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002727 output = NULL;
2728
Jim Cromie56f3bae2011-09-07 17:14:00 -06002729 if (output_name && output_fd) {
2730 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002731 parse_options_usage(stat_usage, stat_options, "o", 1);
2732 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002733 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002734 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002735
Andi Kleen54b50912016-03-03 15:57:36 -08002736 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2737 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2738 goto out;
2739 }
2740
Andi Kleen54b50912016-03-03 15:57:36 -08002741 if (metric_only && run_count > 1) {
2742 fprintf(stderr, "--metric-only is not supported with -r\n");
2743 goto out;
2744 }
2745
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002746 if (output_fd < 0) {
2747 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002748 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002749 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002750 }
2751
Stephane Eranian4aa90152011-08-15 22:22:33 +02002752 if (!output) {
2753 struct timespec tm;
2754 mode = append_file ? "a" : "w";
2755
2756 output = fopen(output_name, mode);
2757 if (!output) {
2758 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002759 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002760 }
2761 clock_gettime(CLOCK_REALTIME, &tm);
2762 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002763 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002764 mode = append_file ? "a" : "w";
2765 output = fdopen(output_fd, mode);
2766 if (!output) {
2767 perror("Failed opening logfd");
2768 return -errno;
2769 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002770 }
2771
Jiri Olsa58215222015-07-21 14:31:24 +02002772 stat_config.output = output;
2773
Stephane Eraniand7470b62010-12-01 18:49:05 +02002774 /*
2775 * let the spreadsheet do the pretty-printing
2776 */
2777 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002778 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002779 if (big_num_opt == 1) {
2780 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002781 parse_options_usage(stat_usage, stat_options, "B", 1);
2782 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002783 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002784 } else /* Nope, so disable big number formatting */
2785 big_num = false;
2786 } else if (big_num_opt == 0) /* User passed --no-big-num */
2787 big_num = false;
2788
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002789 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002790
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002791 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002792 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002793 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002794 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002795 } else if (run_count == 0) {
2796 forever = true;
2797 run_count = 1;
2798 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002799
Jin Yao1d9f8d12017-12-05 22:03:10 +08002800 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2801 !target__has_task(&target)) {
2802 if (!target.system_wide || target.cpu_list) {
2803 fprintf(stderr, "The --per-thread option is only "
2804 "available when monitoring via -p -t -a "
2805 "options or only --per-thread.\n");
2806 parse_options_usage(NULL, stat_options, "p", 1);
2807 parse_options_usage(NULL, stat_options, "t", 1);
2808 goto out;
2809 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002810 }
2811
2812 /*
2813 * no_aggr, cgroup are for system-wide only
2814 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2815 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002816 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2817 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002818 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002819 fprintf(stderr, "both cgroup and no-aggregation "
2820 "modes only available in system-wide mode\n");
2821
Jiri Olsae0547312015-11-05 15:40:45 +01002822 parse_options_usage(stat_usage, stat_options, "G", 1);
2823 parse_options_usage(NULL, stat_options, "A", 1);
2824 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002825 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002826 }
2827
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002828 if (add_default_attributes())
2829 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002830
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002831 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002832
Jin Yao1d9f8d12017-12-05 22:03:10 +08002833 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2834 target.per_thread = true;
2835
Namhyung Kim77a6f012012-05-07 14:09:04 +09002836 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002837 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002838 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002839 parse_options_usage(stat_usage, stat_options, "p", 1);
2840 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002841 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002842 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002843 parse_options_usage(stat_usage, stat_options, "C", 1);
2844 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002845 }
2846 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002847 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002848
2849 /*
2850 * Initialize thread_map with comm names,
2851 * so we could print it out on output.
2852 */
Jin Yao56739442017-12-05 22:03:07 +08002853 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002854 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08002855 if (target.system_wide) {
2856 if (runtime_stat_new(&stat_config,
2857 thread_map__nr(evsel_list->threads))) {
2858 goto out;
2859 }
2860 }
2861 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002862
Stephane Eranian13370a92013-01-29 12:47:44 +01002863 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002864 if (interval < 10) {
2865 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002866 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002867 goto out;
2868 } else
2869 pr_warning("print interval < 100ms. "
2870 "The overhead percentage could be high in some cases. "
2871 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002872 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002873
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002874 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002875 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002876
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002877 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002878 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002879
Ingo Molnar58d7e992009-05-15 11:03:23 +02002880 /*
2881 * We dont want to block the signals - that would cause
2882 * child tasks to inherit that and Ctrl-C would not work.
2883 * What we want is for Ctrl-C to work in the exec()-ed
2884 * task, but being ignored by perf stat itself:
2885 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002886 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002887 if (!forever)
2888 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002889 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002890 signal(SIGALRM, skip_signal);
2891 signal(SIGABRT, skip_signal);
2892
Ingo Molnar42202dd2009-06-13 14:57:28 +02002893 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002894 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002895 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002896 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2897 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002898
Ingo Molnar42202dd2009-06-13 14:57:28 +02002899 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002900 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002901 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002902 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002903 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002904 }
2905
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002906 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002907 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002908
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002909 if (STAT_RECORD) {
2910 /*
2911 * We synthesize the kernel mmap record just so that older tools
2912 * don't emit warnings about not being able to resolve symbols
2913 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2914 * a saner message about no samples being in the perf.data file.
2915 *
2916 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002917 * in header.c at the moment 'perf stat record' gets introduced, which
2918 * is not really needed once we start adding the stat specific PERF_RECORD_
2919 * records, but the need to suppress the kptr_restrict messages in older
2920 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002921 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002922 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002923 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2924 process_synthesized_event,
2925 &perf_stat.session->machines.host);
2926 if (err) {
2927 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2928 "older tools may produce warnings about this file\n.");
2929 }
2930
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002931 if (!interval) {
2932 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2933 pr_err("failed to write stat round event\n");
2934 }
2935
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002936 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01002937 perf_stat.session->header.data_size += perf_stat.bytes_written;
2938 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2939 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002940
2941 perf_session__delete(perf_stat.session);
2942 }
2943
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002944 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002945 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002946out:
Kan Liangdaefd0b2017-05-26 12:05:38 -07002947 if (smi_cost && smi_reset)
2948 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
2949
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002950 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08002951
2952 runtime_stat_delete(&stat_config);
2953
Ingo Molnar42202dd2009-06-13 14:57:28 +02002954 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002955}