blob: f5ac6545af343fdbf6b716f893102330c63b3407 [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 Olsa9660e082018-06-07 00:15:06 +020068#include "util/top.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010069#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020070
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030071#include <linux/time64.h>
Andi Kleen44b1e602016-05-30 12:49:42 -030072#include <api/fs/fs.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030073#include <errno.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030074#include <signal.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020075#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020076#include <sys/prctl.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030077#include <inttypes.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020078#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070079#include <math.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030080#include <sys/types.h>
81#include <sys/stat.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030082#include <sys/wait.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030083#include <unistd.h>
Jiri Olsa0ce2da12018-06-05 14:13:13 +020084#include <sys/time.h>
85#include <sys/resource.h>
86#include <sys/wait.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020087
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030088#include "sane_ctype.h"
89
Stephane Eraniand7470b62010-12-01 18:49:05 +020090#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060091#define CNTR_NOT_SUPPORTED "<not supported>"
92#define CNTR_NOT_COUNTED "<not counted>"
Kan Liangdaefd0b2017-05-26 12:05:38 -070093#define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
Stephane Eraniand7470b62010-12-01 18:49:05 +020094
Jiri Olsad4f63a42015-06-26 11:29:26 +020095static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010096
Andi Kleen4cabc3d2013-08-21 16:47:26 -070097/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020098static const char *transaction_attrs = {
99 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700100 "{"
101 "instructions,"
102 "cycles,"
103 "cpu/cycles-t/,"
104 "cpu/tx-start/,"
105 "cpu/el-start/,"
106 "cpu/cycles-ct/"
107 "}"
108};
109
110/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +0200111static const char * transaction_limited_attrs = {
112 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700113 "{"
114 "instructions,"
115 "cycles,"
116 "cpu/cycles-t/,"
117 "cpu/tx-start/"
118 "}"
119};
120
Andi Kleen44b1e602016-05-30 12:49:42 -0300121static const char * topdown_attrs[] = {
122 "topdown-total-slots",
123 "topdown-slots-retired",
124 "topdown-recovery-bubbles",
125 "topdown-fetch-bubbles",
126 "topdown-slots-issued",
127 NULL,
128};
129
Kan Liangdaefd0b2017-05-26 12:05:38 -0700130static const char *smi_cost_attrs = {
131 "{"
132 "msr/aperf/,"
133 "msr/smi/,"
134 "cycles"
135 "}"
136};
137
Robert Richter666e6d42012-04-05 18:26:27 +0200138static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200139
Andi Kleenb18f3e32017-08-31 12:40:31 -0700140static struct rblist metric_events;
141
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300142static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900143 .uid = UINT_MAX,
144};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530145
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100146typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
147
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200148#define METRIC_ONLY_LEN 20
149
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200150static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000151static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200152static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700153static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300154static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700155static bool smi_cost = false;
156static bool smi_reset = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200157static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200158static int big_num_opt = -1;
Lin Ming43bece72011-08-17 18:42:07 +0800159static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200160static const char *pre_cmd = NULL;
161static const char *post_cmd = NULL;
162static bool sync_run = false;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500163static bool forever = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300164static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700165static bool no_merge = false;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200166static bool walltime_run_table = 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;
yuzhoujiandb06a262018-01-29 10:25:22 +0100171static bool interval_count;
Jiri Olsae0547312015-11-05 15:40:45 +0100172static const char *output_name;
173static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100174static int print_free_counters_hint;
Kan Liang30060ea2018-04-24 11:20:11 -0700175static int print_mixed_hw_group_error;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200176static u64 *walltime_run;
Jiri Olsa0ce2da12018-06-05 14:13:13 +0200177static bool ru_display = false;
178static struct rusage ru_data;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200179
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100180struct perf_stat {
181 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100182 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100183 struct perf_session *session;
184 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100185 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100186 bool maps_allocated;
187 struct cpu_map *cpus;
188 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100189 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100190};
191
192static struct perf_stat perf_stat;
193#define STAT_RECORD perf_stat.record
194
Liming Wang60666c62009-12-31 16:05:50 +0800195static volatile int done = 0;
196
Jiri Olsa421a50f2015-07-21 14:31:22 +0200197static struct perf_stat_config stat_config = {
198 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200199 .scale = true,
Jiri Olsadf4f7b42018-08-30 08:32:32 +0200200 .unit_width = 4, /* strlen("unit") */
Jiri Olsad97ae042018-08-30 08:32:36 +0200201 .run_count = 1,
Jiri Olsaee1760e2018-08-30 08:32:37 +0200202 .metric_only_len = METRIC_ONLY_LEN,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200203};
204
Andi Kleene864c5c2017-08-31 12:40:35 -0700205static bool is_duration_time(struct perf_evsel *evsel)
206{
207 return !strcmp(evsel->name, "duration_time");
208}
209
Stephane Eranian13370a92013-01-29 12:47:44 +0100210static inline void diff_timespec(struct timespec *r, struct timespec *a,
211 struct timespec *b)
212{
213 r->tv_sec = a->tv_sec - b->tv_sec;
214 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300215 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100216 r->tv_sec--;
217 } else {
218 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
219 }
220}
221
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200222static void perf_stat__reset_stats(void)
223{
Jin Yao56739442017-12-05 22:03:07 +0800224 int i;
225
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200226 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200227 perf_stat__reset_shadow_stats();
Jin Yao56739442017-12-05 22:03:07 +0800228
229 for (i = 0; i < stat_config.stats_num; i++)
230 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200231}
232
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100233static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100234 union perf_event *event,
235 struct perf_sample *sample __maybe_unused,
236 struct machine *machine __maybe_unused)
237{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100238 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100239 pr_err("failed to write perf data, error: %m\n");
240 return -1;
241 }
242
243 perf_stat.bytes_written += event->header.size;
244 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100245}
246
Jiri Olsa1975d362015-11-05 15:40:56 +0100247static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100248{
Jiri Olsa1975d362015-11-05 15:40:56 +0100249 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100250 process_synthesized_event,
251 NULL);
252}
253
254#define WRITE_STAT_ROUND_EVENT(time, interval) \
255 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
256
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100257#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
258
259static int
260perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
261 struct perf_counts_values *count)
262{
263 struct perf_sample_id *sid = SID(counter, cpu, thread);
264
265 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
266 process_synthesized_event, NULL);
267}
268
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200269/*
270 * Read out the results of a single counter:
271 * do not aggregate counts across CPUs in system-wide mode
272 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200273static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200274{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100275 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100276 int ncpus, cpu, thread;
277
Jin Yao1d9f8d12017-12-05 22:03:10 +0800278 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Mark Rutland00e727b2016-07-15 11:08:10 +0100279 ncpus = perf_evsel__nr_cpus(counter);
280 else
281 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200282
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000283 if (!counter->supported)
284 return -ENOENT;
285
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100286 if (counter->system_wide)
287 nthreads = 1;
288
289 for (thread = 0; thread < nthreads; thread++) {
290 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200291 struct perf_counts_values *count;
292
293 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200294
295 /*
296 * The leader's group read loads data into its group members
297 * (via perf_evsel__read_counter) and sets threir count->loaded.
298 */
299 if (!count->loaded &&
300 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700301 counter->counts->scaled = -1;
302 perf_counts(counter->counts, cpu, thread)->ena = 0;
303 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100304 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700305 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100306
Jiri Olsa82bf3112017-07-26 14:02:06 +0200307 count->loaded = false;
308
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100309 if (STAT_RECORD) {
310 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
311 pr_err("failed to write stat event\n");
312 return -1;
313 }
314 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700315
316 if (verbose > 1) {
317 fprintf(stat_config.output,
318 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
319 perf_evsel__name(counter),
320 cpu,
321 count->val, count->ena, count->run);
322 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100323 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200324 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200325
326 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200327}
328
Mark Rutland3df33ef2016-08-09 14:04:29 +0100329static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200330{
331 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700332 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200333
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300334 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700335 ret = read_counter(counter);
336 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700337 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200338
Stephane Eraniandb49a712017-04-12 11:23:01 -0700339 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200340 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200341 }
342}
343
Jiri Olsaba411a92015-06-26 11:29:24 +0200344static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100345{
Stephane Eranian13370a92013-01-29 12:47:44 +0100346 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100347
Mark Rutland3df33ef2016-08-09 14:04:29 +0100348 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100349
Stephane Eranian13370a92013-01-29 12:47:44 +0100350 clock_gettime(CLOCK_MONOTONIC, &ts);
351 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100352
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100353 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300354 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100355 pr_err("failed to write stat round event\n");
356 }
357
Andi Kleenb90f1332017-08-31 12:40:36 -0700358 init_stats(&walltime_nsecs_stats);
359 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200360 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100361}
362
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100363static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700364{
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200365 if (stat_config.initial_delay)
366 usleep(stat_config.initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100367
368 /*
369 * We need to enable counters only if:
370 * - we don't have tracee (attaching to task or cpu)
371 * - we have initial delay configured
372 */
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200373 if (!target__none(&target) || stat_config.initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100374 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700375}
376
Mark Rutland3df33ef2016-08-09 14:04:29 +0100377static void disable_counters(void)
378{
379 /*
380 * If we don't have tracee (attaching to task or cpu), counters may
381 * still be running. To get accurate group ratios, we must stop groups
382 * from counting before reading their constituent counters.
383 */
384 if (!target__none(&target))
385 perf_evlist__disable(evsel_list);
386}
387
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300388static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300389
390/*
391 * perf_evlist__prepare_workload will send a SIGUSR1
392 * if the fork fails, since we asked by setting its
393 * want_signal to true.
394 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300395static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
396 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300397{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300398 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300399}
400
Jiri Olsa82bf3112017-07-26 14:02:06 +0200401static bool perf_evsel__should_store_id(struct perf_evsel *counter)
402{
403 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
404}
405
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700406static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
407{
408 struct perf_evsel *c2, *leader;
409 bool is_open = true;
410
411 leader = evsel->leader;
412 pr_debug("Weak group for %s/%d failed\n",
413 leader->name, leader->nr_members);
414
415 /*
416 * for_each_group_member doesn't work here because it doesn't
417 * include the first entry.
418 */
419 evlist__for_each_entry(evsel_list, c2) {
420 if (c2 == evsel)
421 is_open = false;
422 if (c2->leader == leader) {
423 if (is_open)
424 perf_evsel__close(c2);
425 c2->leader = c2;
426 c2->nr_members = 0;
427 }
428 }
429 return leader;
430}
431
Jiri Olsae55c14a2018-04-23 11:08:21 +0200432static int __run_perf_stat(int argc, const char **argv, int run_idx)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200433{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200434 int interval = stat_config.interval;
yuzhoujiandb06a262018-01-29 10:25:22 +0100435 int times = stat_config.times;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100436 int timeout = stat_config.timeout;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300437 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200438 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100439 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100440 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100441 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200442 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300443 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100444 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600445 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200446
Stephane Eranian13370a92013-01-29 12:47:44 +0100447 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300448 ts.tv_sec = interval / USEC_PER_MSEC;
449 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100450 } else if (timeout) {
451 ts.tv_sec = timeout / USEC_PER_MSEC;
452 ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100453 } else {
454 ts.tv_sec = 1;
455 ts.tv_nsec = 0;
456 }
457
Liming Wang60666c62009-12-31 16:05:50 +0800458 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100459 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300460 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900461 perror("failed to prepare workload");
462 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800463 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900464 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000465 }
466
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200467 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300468 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200469
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300470 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300471try_again:
Jiri Olsad09cefd2018-08-30 08:32:17 +0200472 if (create_perf_stat_counter(counter, &stat_config, &target) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700473
474 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700475 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700476 counter->leader != counter &&
477 counter->weak_group) {
478 counter = perf_evsel__reset_weak_group(counter);
479 goto try_again;
480 }
481
David Ahern979987a2012-05-08 09:29:16 -0600482 /*
483 * PPC returns ENXIO for HW counters until 2.6.37
484 * (behavior changed with commit b0a873e).
485 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100486 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600487 errno == ENOENT || errno == EOPNOTSUPP ||
488 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900489 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600490 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300491 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600492 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400493
494 if ((counter->leader != counter) ||
495 !(counter->leader->nr_members > 1))
496 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300497 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900498 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300499 ui__warning("%s\n", msg);
500 goto try_again;
Jin Yaoab6c79b2018-01-16 23:43:08 +0800501 } else if (target__has_per_thread(&target) &&
502 evsel_list->threads &&
503 evsel_list->threads->err_thread != -1) {
504 /*
505 * For global --per-thread case, skip current
506 * error thread.
507 */
508 if (!thread_map__remove(evsel_list->threads,
509 evsel_list->threads->err_thread)) {
510 evsel_list->threads->err_thread = -1;
511 goto try_again;
512 }
513 }
Ingo Molnarede70292011-04-28 08:48:42 +0200514
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300515 perf_evsel__open_strerror(counter, &target,
516 errno, msg, sizeof(msg));
517 ui__error("%s\n", msg);
518
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200519 if (child_pid != -1)
520 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600521
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200522 return -1;
523 }
David Ahern2cee77c2011-05-30 08:55:59 -0600524 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100525
526 l = strlen(counter->unit);
Jiri Olsadf4f7b42018-08-30 08:32:32 +0200527 if (l > stat_config.unit_width)
528 stat_config.unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100529
Jiri Olsa82bf3112017-07-26 14:02:06 +0200530 if (perf_evsel__should_store_id(counter) &&
Jiri Olsa650d6222018-08-30 08:32:16 +0200531 perf_evsel__store_ids(counter, evsel_list))
Jiri Olsa2af46462015-11-05 15:40:49 +0100532 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300533 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200534
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300535 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300536 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300537 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300538 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100539 return -1;
540 }
541
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600542 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300543 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600544 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
545 str_error_r(errno, msg, sizeof(msg)));
546 return -1;
547 }
548
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100549 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100550 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100551
Jiri Olsa664c98d2015-11-05 15:40:50 +0100552 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100553 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100554 } else {
555 err = perf_session__write_header(perf_stat.session, evsel_list,
556 fd, false);
557 }
558
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100559 if (err < 0)
560 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100561
Jiri Olsa1c21e982018-08-30 08:32:21 +0200562 err = perf_stat_synthesize_config(&stat_config, NULL, evsel_list,
Jiri Olsac2c247f2018-08-30 08:32:22 +0200563 process_synthesized_event, is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100564 if (err < 0)
565 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100566 }
567
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200568 /*
569 * Enable counters and exec the command:
570 */
571 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100572 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200573
Liming Wang60666c62009-12-31 16:05:50 +0800574 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900575 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100576 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900577
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100578 if (interval || timeout) {
Stephane Eranian13370a92013-01-29 12:47:44 +0100579 while (!waitpid(child_pid, &status, WNOHANG)) {
580 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100581 if (timeout)
582 break;
Jiri Olsaba411a92015-06-26 11:29:24 +0200583 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100584 if (interval_count && !(--times))
585 break;
Stephane Eranian13370a92013-01-29 12:47:44 +0100586 }
587 }
Jiri Olsa0ce2da12018-06-05 14:13:13 +0200588 wait4(child_pid, &status, 0, &ru_data);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300589
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300590 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300591 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300592 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300593 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300594 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300595
Andi Kleen33e49ea2011-09-15 14:31:40 -0700596 if (WIFSIGNALED(status))
597 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800598 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100599 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100600 while (!done) {
601 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100602 if (timeout)
603 break;
yuzhoujiandb06a262018-01-29 10:25:22 +0100604 if (interval) {
Jiri Olsaba411a92015-06-26 11:29:24 +0200605 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100606 if (interval_count && !(--times))
607 break;
608 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100609 }
Liming Wang60666c62009-12-31 16:05:50 +0800610 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200611
Mark Rutland3df33ef2016-08-09 14:04:29 +0100612 disable_counters();
613
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200614 t1 = rdclock();
615
Jiri Olsae55c14a2018-04-23 11:08:21 +0200616 if (walltime_run_table)
617 walltime_run[run_idx] = t1 - t0;
618
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200619 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200620
Mark Rutland3df33ef2016-08-09 14:04:29 +0100621 /*
622 * Closing a group leader splits the group, and as we only disable
623 * group leaders, results in remaining events becoming enabled. To
624 * avoid arbitrary skew, we must read all counters before closing any
625 * group leaders.
626 */
627 read_counters();
628 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200629
Ingo Molnar42202dd2009-06-13 14:57:28 +0200630 return WEXITSTATUS(status);
631}
632
Jiri Olsae55c14a2018-04-23 11:08:21 +0200633static int run_perf_stat(int argc, const char **argv, int run_idx)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200634{
635 int ret;
636
637 if (pre_cmd) {
638 ret = system(pre_cmd);
639 if (ret)
640 return ret;
641 }
642
643 if (sync_run)
644 sync();
645
Jiri Olsae55c14a2018-04-23 11:08:21 +0200646 ret = __run_perf_stat(argc, argv, run_idx);
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200647 if (ret)
648 return ret;
649
650 if (post_cmd) {
651 ret = system(post_cmd);
652 if (ret)
653 return ret;
654 }
655
656 return ret;
657}
658
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200659static void print_running(struct perf_stat_config *config,
660 u64 run, u64 ena)
Andi Kleend73515c2015-03-11 07:16:27 -0700661{
Jiri Olsafa7070a2018-08-30 08:32:29 +0200662 if (config->csv_output) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200663 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
Jiri Olsafa7070a2018-08-30 08:32:29 +0200664 config->csv_sep,
Andi Kleend73515c2015-03-11 07:16:27 -0700665 run,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200666 config->csv_sep,
Andi Kleend73515c2015-03-11 07:16:27 -0700667 ena ? 100.0 * run / ena : 100.0);
668 } else if (run != ena) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200669 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700670 }
671}
672
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200673static void print_noise_pct(struct perf_stat_config *config,
674 double total, double avg)
Ingo Molnarf99844c2011-04-27 05:35:39 +0200675{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800676 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200677
Jiri Olsafa7070a2018-08-30 08:32:29 +0200678 if (config->csv_output)
679 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600680 else if (pct)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200681 fprintf(config->output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200682}
683
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200684static void print_noise(struct perf_stat_config *config,
685 struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200686{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200687 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200688
Jiri Olsad97ae042018-08-30 08:32:36 +0200689 if (config->run_count == 1)
Peter Zijlstra849abde2009-09-04 18:23:38 +0200690 return;
691
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300692 ps = evsel->stats;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200693 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200694}
695
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200696static void aggr_printout(struct perf_stat_config *config,
697 struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200698{
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200699 switch (config->aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100700 case AGGR_CORE:
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200701 fprintf(config->output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100702 cpu_map__id_to_socket(id),
Jiri Olsafa7070a2018-08-30 08:32:29 +0200703 config->csv_output ? 0 : -8,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100704 cpu_map__id_to_cpu(id),
Jiri Olsafa7070a2018-08-30 08:32:29 +0200705 config->csv_sep,
706 config->csv_output ? 0 : 4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100707 nr,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200708 config->csv_sep);
Stephane Eranian12c08a92013-02-14 13:57:29 +0100709 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100710 case AGGR_SOCKET:
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200711 fprintf(config->output, "S%*d%s%*d%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +0200712 config->csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100713 id,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200714 config->csv_sep,
715 config->csv_output ? 0 : 4,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100716 nr,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200717 config->csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100718 break;
719 case AGGR_NONE:
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200720 fprintf(config->output, "CPU%*d%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +0200721 config->csv_output ? 0 : -4,
722 perf_evsel__cpus(evsel)->map[id], config->csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100723 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200724 case AGGR_THREAD:
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200725 fprintf(config->output, "%*s-%*d%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +0200726 config->csv_output ? 0 : 16,
Jiri Olsa32b8af82015-06-26 11:29:27 +0200727 thread_map__comm(evsel->threads, id),
Jiri Olsafa7070a2018-08-30 08:32:29 +0200728 config->csv_output ? 0 : -8,
Jiri Olsa32b8af82015-06-26 11:29:27 +0200729 thread_map__pid(evsel->threads, id),
Jiri Olsafa7070a2018-08-30 08:32:29 +0200730 config->csv_sep);
Jiri Olsa32b8af82015-06-26 11:29:27 +0200731 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100732 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200733 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100734 default:
735 break;
736 }
737}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200738
Andi Kleen140aead2016-01-30 09:06:49 -0800739struct outstate {
740 FILE *fh;
741 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800742 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800743 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800744 int id, nr;
745 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800746};
747
748#define METRIC_LEN 35
749
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200750static void new_line_std(struct perf_stat_config *config __maybe_unused,
751 void *ctx)
Andi Kleen140aead2016-01-30 09:06:49 -0800752{
753 struct outstate *os = ctx;
754
755 os->newline = true;
756}
757
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200758static void do_new_line_std(struct perf_stat_config *config,
759 struct outstate *os)
Andi Kleen140aead2016-01-30 09:06:49 -0800760{
761 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800762 fputs(os->prefix, os->fh);
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200763 aggr_printout(config, os->evsel, os->id, os->nr);
764 if (config->aggr_mode == AGGR_NONE)
Andi Kleen140aead2016-01-30 09:06:49 -0800765 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800766 fprintf(os->fh, " ");
767}
768
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200769static void print_metric_std(struct perf_stat_config *config,
770 void *ctx, const char *color, const char *fmt,
Andi Kleen140aead2016-01-30 09:06:49 -0800771 const char *unit, double val)
772{
773 struct outstate *os = ctx;
774 FILE *out = os->fh;
775 int n;
776 bool newline = os->newline;
777
778 os->newline = false;
779
780 if (unit == NULL || fmt == NULL) {
781 fprintf(out, "%-*s", METRIC_LEN, "");
782 return;
783 }
784
785 if (newline)
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200786 do_new_line_std(config, os);
Andi Kleen140aead2016-01-30 09:06:49 -0800787
788 n = fprintf(out, " # ");
789 if (color)
790 n += color_fprintf(out, color, fmt, val);
791 else
792 n += fprintf(out, fmt, val);
793 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
794}
795
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200796static void new_line_csv(struct perf_stat_config *config, void *ctx)
Andi Kleen92a61f62016-02-29 14:36:21 -0800797{
798 struct outstate *os = ctx;
799 int i;
800
801 fputc('\n', os->fh);
802 if (os->prefix)
Jiri Olsafa7070a2018-08-30 08:32:29 +0200803 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200804 aggr_printout(config, os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800805 for (i = 0; i < os->nfields; i++)
Jiri Olsafa7070a2018-08-30 08:32:29 +0200806 fputs(config->csv_sep, os->fh);
Andi Kleen92a61f62016-02-29 14:36:21 -0800807}
808
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200809static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
810 void *ctx,
Andi Kleen92a61f62016-02-29 14:36:21 -0800811 const char *color __maybe_unused,
812 const char *fmt, const char *unit, double val)
813{
814 struct outstate *os = ctx;
815 FILE *out = os->fh;
816 char buf[64], *vals, *ends;
817
818 if (unit == NULL || fmt == NULL) {
Jiri Olsafa7070a2018-08-30 08:32:29 +0200819 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800820 return;
821 }
822 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900823 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800824 while (isdigit(*ends) || *ends == '.')
825 ends++;
826 *ends = 0;
827 while (isspace(*unit))
828 unit++;
Jiri Olsafa7070a2018-08-30 08:32:29 +0200829 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, unit);
Andi Kleen92a61f62016-02-29 14:36:21 -0800830}
831
Andi Kleen54b50912016-03-03 15:57:36 -0800832/* Filter out some columns that don't work well in metrics only mode */
833
834static bool valid_only_metric(const char *unit)
835{
836 if (!unit)
837 return false;
838 if (strstr(unit, "/sec") ||
839 strstr(unit, "hz") ||
840 strstr(unit, "Hz") ||
841 strstr(unit, "CPUs utilized"))
842 return false;
843 return true;
844}
845
846static const char *fixunit(char *buf, struct perf_evsel *evsel,
847 const char *unit)
848{
849 if (!strncmp(unit, "of all", 6)) {
850 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
851 unit);
852 return buf;
853 }
854 return unit;
855}
856
Jiri Olsaee1760e2018-08-30 08:32:37 +0200857static void print_metric_only(struct perf_stat_config *config,
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200858 void *ctx, const char *color, const char *fmt,
Andi Kleen54b50912016-03-03 15:57:36 -0800859 const char *unit, double val)
860{
861 struct outstate *os = ctx;
862 FILE *out = os->fh;
Jiri Olsaf5155722018-06-07 00:15:08 +0200863 char buf[1024], str[1024];
Jiri Olsaee1760e2018-08-30 08:32:37 +0200864 unsigned mlen = config->metric_only_len;
Andi Kleen54b50912016-03-03 15:57:36 -0800865
866 if (!valid_only_metric(unit))
867 return;
868 unit = fixunit(buf, os->evsel, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800869 if (mlen < strlen(unit))
870 mlen = strlen(unit) + 1;
Jiri Olsaf5155722018-06-07 00:15:08 +0200871
872 if (color)
873 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
874
875 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
876 fprintf(out, "%*s ", mlen, str);
Andi Kleen54b50912016-03-03 15:57:36 -0800877}
878
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200879static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
880 void *ctx, const char *color __maybe_unused,
Andi Kleen54b50912016-03-03 15:57:36 -0800881 const char *fmt,
882 const char *unit, double val)
883{
884 struct outstate *os = ctx;
885 FILE *out = os->fh;
886 char buf[64], *vals, *ends;
887 char tbuf[1024];
888
889 if (!valid_only_metric(unit))
890 return;
891 unit = fixunit(tbuf, os->evsel, unit);
892 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900893 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -0800894 while (isdigit(*ends) || *ends == '.')
895 ends++;
896 *ends = 0;
Jiri Olsafa7070a2018-08-30 08:32:29 +0200897 fprintf(out, "%s%s", vals, config->csv_sep);
Andi Kleen54b50912016-03-03 15:57:36 -0800898}
899
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200900static void new_line_metric(struct perf_stat_config *config __maybe_unused,
901 void *ctx __maybe_unused)
Andi Kleen54b50912016-03-03 15:57:36 -0800902{
903}
904
Jiri Olsaee1760e2018-08-30 08:32:37 +0200905static void print_metric_header(struct perf_stat_config *config,
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200906 void *ctx, const char *color __maybe_unused,
Andi Kleen54b50912016-03-03 15:57:36 -0800907 const char *fmt __maybe_unused,
908 const char *unit, double val __maybe_unused)
909{
910 struct outstate *os = ctx;
911 char tbuf[1024];
912
913 if (!valid_only_metric(unit))
914 return;
915 unit = fixunit(tbuf, os->evsel, unit);
Jiri Olsafa7070a2018-08-30 08:32:29 +0200916 if (config->csv_output)
917 fprintf(os->fh, "%s%s", unit, config->csv_sep);
Andi Kleen54b50912016-03-03 15:57:36 -0800918 else
Jiri Olsaee1760e2018-08-30 08:32:37 +0200919 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800920}
921
Jiri Olsaae2d7da2018-08-30 08:32:38 +0200922static int first_shadow_cpu(struct perf_stat_config *config,
923 struct perf_evsel *evsel, int id)
Andi Kleen44d49a62016-02-29 14:36:22 -0800924{
925 int i;
926
927 if (!aggr_get_id)
928 return 0;
929
Jiri Olsaae2d7da2018-08-30 08:32:38 +0200930 if (config->aggr_mode == AGGR_NONE)
Andi Kleen44d49a62016-02-29 14:36:22 -0800931 return id;
932
Jiri Olsaae2d7da2018-08-30 08:32:38 +0200933 if (config->aggr_mode == AGGR_GLOBAL)
Andi Kleen44d49a62016-02-29 14:36:22 -0800934 return 0;
935
936 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
937 int cpu2 = perf_evsel__cpus(evsel)->map[i];
938
939 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
940 return cpu2;
941 }
942 return 0;
943}
944
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200945static void abs_printout(struct perf_stat_config *config,
946 int id, int nr, struct perf_evsel *evsel, double avg)
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200947{
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200948 FILE *output = config->output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200949 double sc = evsel->scale;
950 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200951
Jiri Olsafa7070a2018-08-30 08:32:29 +0200952 if (config->csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -0700953 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200954 } else {
955 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -0700956 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200957 else
Andi Kleene3b03b62016-05-05 16:04:03 -0700958 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200959 }
960
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200961 aggr_printout(config, evsel, id, nr);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200962
Jiri Olsafa7070a2018-08-30 08:32:29 +0200963 fprintf(output, fmt, avg, config->csv_sep);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200964
965 if (evsel->unit)
966 fprintf(output, "%-*s%s",
Jiri Olsadf4f7b42018-08-30 08:32:32 +0200967 config->csv_output ? 0 : config->unit_width,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200968 evsel->unit, config->csv_sep);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200969
Jiri Olsafa7070a2018-08-30 08:32:29 +0200970 fprintf(output, "%-*s", config->csv_output ? 0 : 25, perf_evsel__name(evsel));
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200971
972 if (evsel->cgrp)
Jiri Olsafa7070a2018-08-30 08:32:29 +0200973 fprintf(output, "%s%s", config->csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -0800974}
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200975
Kan Liang30060ea2018-04-24 11:20:11 -0700976static bool is_mixed_hw_group(struct perf_evsel *counter)
977{
978 struct perf_evlist *evlist = counter->evlist;
979 u32 pmu_type = counter->attr.type;
980 struct perf_evsel *pos;
981
982 if (counter->nr_members < 2)
983 return false;
984
985 evlist__for_each_entry(evlist, pos) {
986 /* software events can be part of any hardware group */
987 if (pos->attr.type == PERF_TYPE_SOFTWARE)
988 continue;
989 if (pmu_type == PERF_TYPE_SOFTWARE) {
990 pmu_type = pos->attr.type;
991 continue;
992 }
993 if (pmu_type != pos->attr.type)
994 return true;
995 }
996
997 return false;
998}
999
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001000static void printout(struct perf_stat_config *config, int id, int nr,
1001 struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +08001002 char *prefix, u64 run, u64 ena, double noise,
1003 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001004{
Andi Kleen140aead2016-01-30 09:06:49 -08001005 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001006 struct outstate os = {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001007 .fh = config->output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001008 .prefix = prefix ? prefix : "",
1009 .id = id,
1010 .nr = nr,
1011 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001012 };
Andi Kleen140aead2016-01-30 09:06:49 -08001013 print_metric_t pm = print_metric_std;
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001014 new_line_t nl;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001015
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001016 if (config->metric_only) {
Andi Kleen54b50912016-03-03 15:57:36 -08001017 nl = new_line_metric;
Jiri Olsafa7070a2018-08-30 08:32:29 +02001018 if (config->csv_output)
Andi Kleen54b50912016-03-03 15:57:36 -08001019 pm = print_metric_only_csv;
1020 else
1021 pm = print_metric_only;
1022 } else
1023 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001024
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001025 if (config->csv_output && !config->metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001026 static int aggr_fields[] = {
1027 [AGGR_GLOBAL] = 0,
1028 [AGGR_THREAD] = 1,
1029 [AGGR_NONE] = 1,
1030 [AGGR_SOCKET] = 2,
1031 [AGGR_CORE] = 2,
1032 };
1033
1034 pm = print_metric_csv;
1035 nl = new_line_csv;
1036 os.nfields = 3;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001037 os.nfields += aggr_fields[config->aggr_mode];
Andi Kleen92a61f62016-02-29 14:36:21 -08001038 if (counter->cgrp)
1039 os.nfields++;
1040 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001041 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001042 if (config->metric_only) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001043 pm(config, &os, NULL, "", "", 0);
Andi Kleen54b50912016-03-03 15:57:36 -08001044 return;
1045 }
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001046 aggr_printout(config, counter, id, nr);
Andi Kleencb110f42016-01-30 09:06:51 -08001047
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001048 fprintf(config->output, "%*s%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +02001049 config->csv_output ? 0 : 18,
Andi Kleencb110f42016-01-30 09:06:51 -08001050 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Jiri Olsafa7070a2018-08-30 08:32:29 +02001051 config->csv_sep);
Andi Kleencb110f42016-01-30 09:06:51 -08001052
Kan Liang30060ea2018-04-24 11:20:11 -07001053 if (counter->supported) {
Borislav Petkov02d492e2017-02-07 01:40:05 +01001054 print_free_counters_hint = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001055 if (is_mixed_hw_group(counter))
1056 print_mixed_hw_group_error = 1;
1057 }
Borislav Petkov02d492e2017-02-07 01:40:05 +01001058
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001059 fprintf(config->output, "%-*s%s",
Jiri Olsadf4f7b42018-08-30 08:32:32 +02001060 config->csv_output ? 0 : config->unit_width,
Jiri Olsafa7070a2018-08-30 08:32:29 +02001061 counter->unit, config->csv_sep);
Andi Kleencb110f42016-01-30 09:06:51 -08001062
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001063 fprintf(config->output, "%*s",
Jiri Olsafa7070a2018-08-30 08:32:29 +02001064 config->csv_output ? 0 : -25,
Andi Kleencb110f42016-01-30 09:06:51 -08001065 perf_evsel__name(counter));
1066
1067 if (counter->cgrp)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001068 fprintf(config->output, "%s%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +02001069 config->csv_sep, counter->cgrp->name);
Andi Kleencb110f42016-01-30 09:06:51 -08001070
Jiri Olsafa7070a2018-08-30 08:32:29 +02001071 if (!config->csv_output)
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001072 pm(config, &os, NULL, NULL, "", 0);
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001073 print_noise(config, counter, noise);
1074 print_running(config, run, ena);
Jiri Olsafa7070a2018-08-30 08:32:29 +02001075 if (config->csv_output)
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001076 pm(config, &os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001077 return;
1078 }
1079
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001080 if (!config->metric_only)
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001081 abs_printout(config, id, nr, counter, uval);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001082
Andi Kleen140aead2016-01-30 09:06:49 -08001083 out.print_metric = pm;
1084 out.new_line = nl;
1085 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001086 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001087
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001088 if (config->csv_output && !config->metric_only) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001089 print_noise(config, counter, noise);
1090 print_running(config, run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001091 }
1092
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001093 perf_stat__print_shadow_stats(config, counter, uval,
Jiri Olsaae2d7da2018-08-30 08:32:38 +02001094 first_shadow_cpu(config, counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001095 &out, &metric_events, st);
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001096 if (!config->csv_output && !config->metric_only) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001097 print_noise(config, counter, noise);
1098 print_running(config, run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001099 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001100}
1101
Jiri Olsaae2d7da2018-08-30 08:32:38 +02001102static void aggr_update_shadow(struct perf_stat_config *config)
Andi Kleen44d49a62016-02-29 14:36:22 -08001103{
1104 int cpu, s2, id, s;
1105 u64 val;
1106 struct perf_evsel *counter;
1107
1108 for (s = 0; s < aggr_map->nr; s++) {
1109 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001110 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001111 val = 0;
1112 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1113 s2 = aggr_get_id(evsel_list->cpus, cpu);
1114 if (s2 != id)
1115 continue;
1116 val += perf_counts(counter->counts, cpu, 0)->val;
1117 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001118 perf_stat__update_shadow_stats(counter, val,
Jiri Olsaae2d7da2018-08-30 08:32:38 +02001119 first_shadow_cpu(config, counter, id),
Jin Yao1fcd0392017-12-05 22:03:04 +08001120 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001121 }
1122 }
1123}
1124
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001125static void uniquify_event_name(struct perf_evsel *counter)
1126{
1127 char *new_name;
1128 char *config;
1129
Kan Liang80ee8c52018-04-24 11:20:14 -07001130 if (counter->uniquified_name ||
1131 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001132 strlen(counter->pmu_name)))
1133 return;
1134
1135 config = strchr(counter->name, '/');
1136 if (config) {
1137 if (asprintf(&new_name,
1138 "%s%s", counter->pmu_name, config) > 0) {
1139 free(counter->name);
1140 counter->name = new_name;
1141 }
1142 } else {
1143 if (asprintf(&new_name,
1144 "%s [%s]", counter->name, counter->pmu_name) > 0) {
1145 free(counter->name);
1146 counter->name = new_name;
1147 }
1148 }
Kan Liang80ee8c52018-04-24 11:20:14 -07001149
1150 counter->uniquified_name = true;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001151}
1152
Andi Kleen430daf22017-03-20 13:17:00 -07001153static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001154 void (*cb)(struct perf_evsel *counter, void *data,
1155 bool first),
1156 void *data)
1157{
Jiri Olsa0c538a92018-08-30 08:32:35 +02001158 struct perf_evlist *evlist = counter->evlist;
Andi Kleen430daf22017-03-20 13:17:00 -07001159 struct perf_evsel *alias;
1160
Jiri Olsa0c538a92018-08-30 08:32:35 +02001161 alias = list_prepare_entry(counter, &(evlist->entries), node);
1162 list_for_each_entry_continue (alias, &evlist->entries, node) {
Andi Kleen430daf22017-03-20 13:17:00 -07001163 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1164 alias->scale != counter->scale ||
1165 alias->cgrp != counter->cgrp ||
1166 strcmp(alias->unit, counter->unit) ||
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001167 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter))
Andi Kleen430daf22017-03-20 13:17:00 -07001168 break;
1169 alias->merged_stat = true;
1170 cb(alias, data, false);
1171 }
1172}
1173
1174static bool collect_data(struct perf_evsel *counter,
1175 void (*cb)(struct perf_evsel *counter, void *data,
1176 bool first),
1177 void *data)
1178{
1179 if (counter->merged_stat)
1180 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001181 cb(counter, data, true);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001182 if (no_merge)
1183 uniquify_event_name(counter);
1184 else if (counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001185 collect_all_aliases(counter, cb, data);
1186 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001187}
1188
1189struct aggr_data {
1190 u64 ena, run, val;
1191 int id;
1192 int nr;
1193 int cpu;
1194};
1195
1196static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1197{
1198 struct aggr_data *ad = data;
1199 int cpu, s2;
1200
1201 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1202 struct perf_counts_values *counts;
1203
1204 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1205 if (s2 != ad->id)
1206 continue;
1207 if (first)
1208 ad->nr++;
1209 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001210 /*
1211 * When any result is bad, make them all to give
1212 * consistent output in interval mode.
1213 */
1214 if (counts->ena == 0 || counts->run == 0 ||
1215 counter->counts->scaled == -1) {
1216 ad->ena = 0;
1217 ad->run = 0;
1218 break;
1219 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001220 ad->val += counts->val;
1221 ad->ena += counts->ena;
1222 ad->run += counts->run;
1223 }
1224}
1225
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001226static void print_aggr(struct perf_stat_config *config,
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001227 struct perf_evlist *evlist,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001228 char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001229{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001230 bool metric_only = config->metric_only;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001231 FILE *output = config->output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001232 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001233 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001234 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001235 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001236 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001237
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001238 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001239 return;
1240
Jiri Olsaae2d7da2018-08-30 08:32:38 +02001241 aggr_update_shadow(config);
Andi Kleen44d49a62016-02-29 14:36:22 -08001242
Andi Kleen54b50912016-03-03 15:57:36 -08001243 /*
1244 * With metric_only everything is on a single line.
1245 * Without each counter has its own line.
1246 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001247 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001248 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001249 if (prefix && metric_only)
1250 fprintf(output, "%s", prefix);
1251
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001252 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001253 first = true;
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001254 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001255 if (is_duration_time(counter))
1256 continue;
1257
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001258 ad.val = ad.ena = ad.run = 0;
1259 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001260 if (!collect_data(counter, aggr_cb, &ad))
1261 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001262 nr = ad.nr;
1263 ena = ad.ena;
1264 run = ad.run;
1265 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001266 if (first && metric_only) {
1267 first = false;
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001268 aggr_printout(config, counter, id, nr);
Andi Kleen54b50912016-03-03 15:57:36 -08001269 }
1270 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001271 fprintf(output, "%s", prefix);
1272
Stephane Eranian410136f2013-11-12 17:58:49 +01001273 uval = val * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001274 printout(config, id, nr, counter, uval, prefix,
1275 run, ena, 1.0, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001276 if (!metric_only)
1277 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001278 }
Andi Kleen54b50912016-03-03 15:57:36 -08001279 if (metric_only)
1280 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001281 }
1282}
1283
Jin Yao29734552017-12-05 22:03:11 +08001284static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001285{
Jin Yao29734552017-12-05 22:03:11 +08001286 return ((struct perf_aggr_thread_value *)b)->val -
1287 ((struct perf_aggr_thread_value *)a)->val;
1288}
1289
1290static struct perf_aggr_thread_value *sort_aggr_thread(
1291 struct perf_evsel *counter,
1292 int nthreads, int ncpus,
1293 int *ret)
1294{
1295 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001296 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001297 struct perf_aggr_thread_value *buf;
1298
1299 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1300 if (!buf)
1301 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001302
1303 for (thread = 0; thread < nthreads; thread++) {
1304 u64 ena = 0, run = 0, val = 0;
1305
1306 for (cpu = 0; cpu < ncpus; cpu++) {
1307 val += perf_counts(counter->counts, cpu, thread)->val;
1308 ena += perf_counts(counter->counts, cpu, thread)->ena;
1309 run += perf_counts(counter->counts, cpu, thread)->run;
1310 }
1311
Jin Yao29734552017-12-05 22:03:11 +08001312 uval = val * counter->scale;
1313
1314 /*
1315 * Skip value 0 when enabling --per-thread globally,
1316 * otherwise too many 0 output.
1317 */
1318 if (uval == 0.0 && target__has_per_thread(&target))
1319 continue;
1320
1321 buf[i].counter = counter;
1322 buf[i].id = thread;
1323 buf[i].uval = uval;
1324 buf[i].val = val;
1325 buf[i].run = run;
1326 buf[i].ena = ena;
1327 i++;
1328 }
1329
1330 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1331
1332 if (ret)
1333 *ret = i;
1334
1335 return buf;
1336}
1337
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001338static void print_aggr_thread(struct perf_stat_config *config,
1339 struct perf_evsel *counter, char *prefix)
Jin Yao29734552017-12-05 22:03:11 +08001340{
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001341 FILE *output = config->output;
Jin Yao29734552017-12-05 22:03:11 +08001342 int nthreads = thread_map__nr(counter->threads);
1343 int ncpus = cpu_map__nr(counter->cpus);
1344 int thread, sorted_threads, id;
1345 struct perf_aggr_thread_value *buf;
1346
1347 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads);
1348 if (!buf) {
1349 perror("cannot sort aggr thread");
1350 return;
1351 }
1352
1353 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001354 if (prefix)
1355 fprintf(output, "%s", prefix);
1356
Jin Yao29734552017-12-05 22:03:11 +08001357 id = buf[thread].id;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001358 if (config->stats)
1359 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
Jin Yao29734552017-12-05 22:03:11 +08001360 prefix, buf[thread].run, buf[thread].ena, 1.0,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001361 &config->stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001362 else
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001363 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
Jin Yao29734552017-12-05 22:03:11 +08001364 prefix, buf[thread].run, buf[thread].ena, 1.0,
1365 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001366 fputc('\n', output);
1367 }
Jin Yao29734552017-12-05 22:03:11 +08001368
1369 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001370}
1371
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001372struct caggr_data {
1373 double avg, avg_enabled, avg_running;
1374};
1375
1376static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1377 bool first __maybe_unused)
1378{
1379 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001380 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001381
1382 cd->avg += avg_stats(&ps->res_stats[0]);
1383 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1384 cd->avg_running += avg_stats(&ps->res_stats[2]);
1385}
1386
Ingo Molnar42202dd2009-06-13 14:57:28 +02001387/*
1388 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001389 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001390 */
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001391static void print_counter_aggr(struct perf_stat_config *config,
1392 struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001393{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001394 bool metric_only = config->metric_only;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001395 FILE *output = config->output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001396 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001397 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001398
Andi Kleen430daf22017-03-20 13:17:00 -07001399 if (!collect_data(counter, counter_aggr_cb, &cd))
1400 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001401
Andi Kleen54b50912016-03-03 15:57:36 -08001402 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001403 fprintf(output, "%s", prefix);
1404
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001405 uval = cd.avg * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001406 printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
Jin Yaoe0128b32017-12-05 22:03:05 +08001407 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001408 if (!metric_only)
1409 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001410}
1411
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001412static void counter_cb(struct perf_evsel *counter, void *data,
1413 bool first __maybe_unused)
1414{
1415 struct aggr_data *ad = data;
1416
1417 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1418 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1419 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1420}
1421
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001422/*
1423 * Print out the results of a single counter:
1424 * does not use aggregated count in system-wide
1425 */
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001426static void print_counter(struct perf_stat_config *config,
1427 struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001428{
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001429 FILE *output = config->output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001430 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001431 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001432 int cpu;
1433
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001434 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001435 struct aggr_data ad = { .cpu = cpu };
1436
Andi Kleen430daf22017-03-20 13:17:00 -07001437 if (!collect_data(counter, counter_cb, &ad))
1438 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001439 val = ad.val;
1440 ena = ad.ena;
1441 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001442
1443 if (prefix)
1444 fprintf(output, "%s", prefix);
1445
Stephane Eranian410136f2013-11-12 17:58:49 +01001446 uval = val * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001447 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
Jin Yaoe0128b32017-12-05 22:03:05 +08001448 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001449
Stephane Eranian4aa90152011-08-15 22:22:33 +02001450 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001451 }
1452}
1453
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001454static void print_no_aggr_metric(struct perf_stat_config *config,
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001455 struct perf_evlist *evlist,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001456 char *prefix)
Andi Kleen206cab62016-03-03 15:57:37 -08001457{
1458 int cpu;
1459 int nrcpus = 0;
1460 struct perf_evsel *counter;
1461 u64 ena, run, val;
1462 double uval;
1463
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001464 nrcpus = evlist->cpus->nr;
Andi Kleen206cab62016-03-03 15:57:37 -08001465 for (cpu = 0; cpu < nrcpus; cpu++) {
1466 bool first = true;
1467
1468 if (prefix)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001469 fputs(prefix, config->output);
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001470 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001471 if (is_duration_time(counter))
1472 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001473 if (first) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001474 aggr_printout(config, counter, cpu, 0);
Andi Kleen206cab62016-03-03 15:57:37 -08001475 first = false;
1476 }
1477 val = perf_counts(counter->counts, cpu, 0)->val;
1478 ena = perf_counts(counter->counts, cpu, 0)->ena;
1479 run = perf_counts(counter->counts, cpu, 0)->run;
1480
1481 uval = val * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001482 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
Jin Yaoe0128b32017-12-05 22:03:05 +08001483 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001484 }
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001485 fputc('\n', config->output);
Andi Kleen206cab62016-03-03 15:57:37 -08001486 }
1487}
1488
Andi Kleen54b50912016-03-03 15:57:36 -08001489static int aggr_header_lens[] = {
1490 [AGGR_CORE] = 18,
1491 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001492 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001493 [AGGR_THREAD] = 24,
1494 [AGGR_GLOBAL] = 0,
1495};
1496
Andi Kleenc51fd632016-05-24 12:52:39 -07001497static const char *aggr_header_csv[] = {
1498 [AGGR_CORE] = "core,cpus,",
1499 [AGGR_SOCKET] = "socket,cpus",
1500 [AGGR_NONE] = "cpu,",
1501 [AGGR_THREAD] = "comm-pid,",
1502 [AGGR_GLOBAL] = ""
1503};
1504
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001505static void print_metric_headers(struct perf_stat_config *config,
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001506 struct perf_evlist *evlist,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001507 const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001508{
1509 struct perf_stat_output_ctx out;
1510 struct perf_evsel *counter;
1511 struct outstate os = {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001512 .fh = config->output
Andi Kleen54b50912016-03-03 15:57:36 -08001513 };
1514
1515 if (prefix)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001516 fprintf(config->output, "%s", prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001517
Jiri Olsafa7070a2018-08-30 08:32:29 +02001518 if (!config->csv_output && !no_indent)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001519 fprintf(config->output, "%*s",
1520 aggr_header_lens[config->aggr_mode], "");
Jiri Olsafa7070a2018-08-30 08:32:29 +02001521 if (config->csv_output) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001522 if (config->interval)
1523 fputs("time,", config->output);
1524 fputs(aggr_header_csv[config->aggr_mode], config->output);
Andi Kleenc51fd632016-05-24 12:52:39 -07001525 }
Andi Kleen54b50912016-03-03 15:57:36 -08001526
1527 /* Print metrics headers only */
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001528 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001529 if (is_duration_time(counter))
1530 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001531 os.evsel = counter;
1532 out.ctx = &os;
1533 out.print_metric = print_metric_header;
1534 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001535 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001536 os.evsel = counter;
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001537 perf_stat__print_shadow_stats(config, counter, 0,
Andi Kleen54b50912016-03-03 15:57:36 -08001538 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001539 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001540 &metric_events,
1541 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001542 }
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001543 fputc('\n', config->output);
Andi Kleen54b50912016-03-03 15:57:36 -08001544}
1545
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001546static void print_interval(struct perf_stat_config *config,
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001547 struct perf_evlist *evlist,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001548 char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001549{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001550 bool metric_only = config->metric_only;
Jiri Olsadf4f7b42018-08-30 08:32:32 +02001551 unsigned int unit_width = config->unit_width;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001552 FILE *output = config->output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001553 static int num_print_interval;
1554
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001555 if (config->interval_clear)
Jiri Olsa9660e082018-06-07 00:15:06 +02001556 puts(CONSOLE_CLEAR);
1557
Jiri Olsafa7070a2018-08-30 08:32:29 +02001558 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001559
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001560 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001561 switch (config->aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001562 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001563 fprintf(output, "# time socket cpus");
1564 if (!metric_only)
1565 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001566 break;
1567 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001568 fprintf(output, "# time core cpus");
1569 if (!metric_only)
1570 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001571 break;
1572 case AGGR_NONE:
Jiri Olsaf5155722018-06-07 00:15:08 +02001573 fprintf(output, "# time CPU ");
Andi Kleen41c8ca22016-05-24 12:52:38 -07001574 if (!metric_only)
1575 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001576 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001577 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001578 fprintf(output, "# time comm-pid");
1579 if (!metric_only)
1580 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001581 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001582 case AGGR_GLOBAL:
1583 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001584 fprintf(output, "# time");
1585 if (!metric_only)
1586 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001587 case AGGR_UNSET:
1588 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001589 }
1590 }
1591
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001592 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001593 print_metric_headers(config, evlist, " ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001594 if (++num_print_interval == 25)
1595 num_print_interval = 0;
1596}
1597
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001598static void print_header(struct perf_stat_config *config,
Jiri Olsac512e0e2018-08-30 08:32:33 +02001599 struct target *_target,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001600 int argc, const char **argv)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001601{
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001602 FILE *output = config->output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001603 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001604
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001605 fflush(stdout);
1606
Jiri Olsafa7070a2018-08-30 08:32:29 +02001607 if (!config->csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001608 fprintf(output, "\n");
1609 fprintf(output, " Performance counter stats for ");
Jiri Olsac512e0e2018-08-30 08:32:33 +02001610 if (_target->system_wide)
David Ahern62d3b612013-09-28 14:27:58 -06001611 fprintf(output, "\'system wide");
Jiri Olsac512e0e2018-08-30 08:32:33 +02001612 else if (_target->cpu_list)
1613 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1614 else if (!target__has_task(_target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001615 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1616 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001617 fprintf(output, " %s", argv[i]);
Jiri Olsac512e0e2018-08-30 08:32:33 +02001618 } else if (_target->pid)
1619 fprintf(output, "process id \'%s", _target->pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001620 else
Jiri Olsac512e0e2018-08-30 08:32:33 +02001621 fprintf(output, "thread id \'%s", _target->tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001622
Stephane Eranian4aa90152011-08-15 22:22:33 +02001623 fprintf(output, "\'");
Jiri Olsad97ae042018-08-30 08:32:36 +02001624 if (config->run_count > 1)
1625 fprintf(output, " (%d runs)", config->run_count);
Stephane Eranian4aa90152011-08-15 22:22:33 +02001626 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001627 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001628}
1629
Jiri Olsabc22de92018-04-23 11:08:20 +02001630static int get_precision(double num)
1631{
1632 if (num > 1)
1633 return 0;
1634
1635 return lround(ceil(-log10(num)));
1636}
1637
Jiri Olsad97ae042018-08-30 08:32:36 +02001638static void print_table(struct perf_stat_config *config,
1639 FILE *output, int precision, double avg)
Jiri Olsae55c14a2018-04-23 11:08:21 +02001640{
1641 char tmp[64];
1642 int idx, indent = 0;
1643
1644 scnprintf(tmp, 64, " %17.*f", precision, avg);
1645 while (tmp[indent] == ' ')
1646 indent++;
1647
1648 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1649
Jiri Olsad97ae042018-08-30 08:32:36 +02001650 for (idx = 0; idx < config->run_count; idx++) {
Jiri Olsae55c14a2018-04-23 11:08:21 +02001651 double run = (double) walltime_run[idx] / NSEC_PER_SEC;
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001652 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001653
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001654 fprintf(output, " %17.*f (%+.*f) ",
Jiri Olsae55c14a2018-04-23 11:08:21 +02001655 precision, run, precision, run - avg);
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001656
1657 for (h = 0; h < n; h++)
1658 fprintf(output, "#");
1659
1660 fprintf(output, "\n");
Jiri Olsae55c14a2018-04-23 11:08:21 +02001661 }
1662
1663 fprintf(output, "\n%*s# Final result:\n", indent, "");
1664}
1665
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001666static double timeval2double(struct timeval *t)
1667{
1668 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1669}
1670
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001671static void print_footer(struct perf_stat_config *config)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001672{
Jiri Olsabc22de92018-04-23 11:08:20 +02001673 double avg = avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001674 FILE *output = config->output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001675 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001676
Jiri Olsad4f63a42015-06-26 11:29:26 +02001677 if (!null_run)
1678 fprintf(output, "\n");
Jiri Olsabc22de92018-04-23 11:08:20 +02001679
Jiri Olsad97ae042018-08-30 08:32:36 +02001680 if (config->run_count == 1) {
Jiri Olsabc22de92018-04-23 11:08:20 +02001681 fprintf(output, " %17.9f seconds time elapsed", avg);
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001682
1683 if (ru_display) {
1684 double ru_utime = timeval2double(&ru_data.ru_utime);
1685 double ru_stime = timeval2double(&ru_data.ru_stime);
1686
1687 fprintf(output, "\n\n");
1688 fprintf(output, " %17.9f seconds user\n", ru_utime);
1689 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1690 }
Jiri Olsabc22de92018-04-23 11:08:20 +02001691 } else {
1692 double sd = stddev_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
1693 /*
1694 * Display at most 2 more significant
1695 * digits than the stddev inaccuracy.
1696 */
1697 int precision = get_precision(sd) + 2;
1698
Jiri Olsae55c14a2018-04-23 11:08:21 +02001699 if (walltime_run_table)
Jiri Olsad97ae042018-08-30 08:32:36 +02001700 print_table(config, output, precision, avg);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001701
Jiri Olsabc22de92018-04-23 11:08:20 +02001702 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1703 precision, avg, precision, sd);
1704
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001705 print_noise_pct(config, sd, avg);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001706 }
1707 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001708
Andi Kleen918c7b062017-05-22 18:00:16 -07001709 if (print_free_counters_hint &&
1710 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1711 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001712 fprintf(output,
1713"Some events weren't counted. Try disabling the NMI watchdog:\n"
1714" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1715" perf stat ...\n"
1716" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001717
1718 if (print_mixed_hw_group_error)
1719 fprintf(output,
1720 "The events in group usually have to be from "
1721 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001722}
1723
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001724static void
1725perf_evlist__print_counters(struct perf_evlist *evlist,
Jiri Olsab64df7f2018-08-30 08:32:26 +02001726 struct perf_stat_config *config,
Jiri Olsac512e0e2018-08-30 08:32:33 +02001727 struct target *_target,
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001728 struct timespec *ts,
1729 int argc, const char **argv)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001730{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001731 bool metric_only = config->metric_only;
Jiri Olsab64df7f2018-08-30 08:32:26 +02001732 int interval = config->interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001733 struct perf_evsel *counter;
1734 char buf[64], *prefix = NULL;
1735
1736 if (interval)
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001737 print_interval(config, evlist, prefix = buf, ts);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001738 else
Jiri Olsac512e0e2018-08-30 08:32:33 +02001739 print_header(config, _target, argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001740
Andi Kleen54b50912016-03-03 15:57:36 -08001741 if (metric_only) {
1742 static int num_print_iv;
1743
Andi Kleen41c8ca22016-05-24 12:52:38 -07001744 if (num_print_iv == 0 && !interval)
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001745 print_metric_headers(config, evlist, prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001746 if (num_print_iv++ == 25)
1747 num_print_iv = 0;
Jiri Olsab64df7f2018-08-30 08:32:26 +02001748 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1749 fprintf(config->output, "%s", prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001750 }
1751
Jiri Olsab64df7f2018-08-30 08:32:26 +02001752 switch (config->aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001753 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001754 case AGGR_SOCKET:
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001755 print_aggr(config, evlist, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001756 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001757 case AGGR_THREAD:
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001758 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001759 if (is_duration_time(counter))
1760 continue;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001761 print_aggr_thread(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001762 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001763 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001764 case AGGR_GLOBAL:
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001765 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001766 if (is_duration_time(counter))
1767 continue;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001768 print_counter_aggr(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001769 }
Andi Kleen54b50912016-03-03 15:57:36 -08001770 if (metric_only)
Jiri Olsab64df7f2018-08-30 08:32:26 +02001771 fputc('\n', config->output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001772 break;
1773 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001774 if (metric_only)
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001775 print_no_aggr_metric(config, evlist, prefix);
Andi Kleen206cab62016-03-03 15:57:37 -08001776 else {
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001777 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001778 if (is_duration_time(counter))
1779 continue;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001780 print_counter(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001781 }
Andi Kleen206cab62016-03-03 15:57:37 -08001782 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001783 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001784 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001785 default:
1786 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001787 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001788
Jiri Olsafa7070a2018-08-30 08:32:29 +02001789 if (!interval && !config->csv_output)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001790 print_footer(config);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001791
Jiri Olsab64df7f2018-08-30 08:32:26 +02001792 fflush(config->output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001793}
1794
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001795static void print_counters(struct timespec *ts, int argc, const char **argv)
1796{
Jiri Olsa01748202018-08-30 08:32:25 +02001797 /* Do not print anything if we record to the pipe. */
1798 if (STAT_RECORD && perf_stat.data.is_pipe)
1799 return;
1800
Jiri Olsac512e0e2018-08-30 08:32:33 +02001801 perf_evlist__print_counters(evsel_list, &stat_config, &target,
Jiri Olsab64df7f2018-08-30 08:32:26 +02001802 ts, argc, argv);
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001803}
1804
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001805static volatile int signr = -1;
1806
Ingo Molnar52425192009-05-26 09:17:18 +02001807static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001808{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001809 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001810 done = 1;
1811
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001812 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001813 /*
1814 * render child_pid harmless
1815 * won't send SIGTERM to a random
1816 * process in case of race condition
1817 * and fast PID recycling
1818 */
1819 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001820}
1821
1822static void sig_atexit(void)
1823{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001824 sigset_t set, oset;
1825
1826 /*
1827 * avoid race condition with SIGCHLD handler
1828 * in skip_signal() which is modifying child_pid
1829 * goal is to avoid send SIGTERM to a random
1830 * process
1831 */
1832 sigemptyset(&set);
1833 sigaddset(&set, SIGCHLD);
1834 sigprocmask(SIG_BLOCK, &set, &oset);
1835
Chris Wilson933da832009-10-04 01:35:01 +01001836 if (child_pid != -1)
1837 kill(child_pid, SIGTERM);
1838
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001839 sigprocmask(SIG_SETMASK, &oset, NULL);
1840
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001841 if (signr == -1)
1842 return;
1843
1844 signal(signr, SIG_DFL);
1845 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001846}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001847
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001848static int stat__set_big_num(const struct option *opt __maybe_unused,
1849 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001850{
1851 big_num_opt = unset ? 0 : 1;
1852 return 0;
1853}
1854
Andi Kleen44b1e602016-05-30 12:49:42 -03001855static int enable_metric_only(const struct option *opt __maybe_unused,
1856 const char *s __maybe_unused, int unset)
1857{
1858 force_metric_only = true;
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001859 stat_config.metric_only = !unset;
Andi Kleen44b1e602016-05-30 12:49:42 -03001860 return 0;
1861}
1862
Andi Kleenb18f3e32017-08-31 12:40:31 -07001863static int parse_metric_groups(const struct option *opt,
1864 const char *str,
1865 int unset __maybe_unused)
1866{
1867 return metricgroup__parse_groups(opt, str, &metric_events);
1868}
1869
Jiri Olsae0547312015-11-05 15:40:45 +01001870static const struct option stat_options[] = {
1871 OPT_BOOLEAN('T', "transaction", &transaction_run,
1872 "hardware transaction statistics"),
1873 OPT_CALLBACK('e', "event", &evsel_list, "event",
1874 "event selector. use 'perf list' to list available events",
1875 parse_events_option),
1876 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1877 "event filter", parse_filter),
Jiri Olsa5698f262018-08-30 08:32:12 +02001878 OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
Jiri Olsae0547312015-11-05 15:40:45 +01001879 "child tasks do not inherit counters"),
1880 OPT_STRING('p', "pid", &target.pid, "pid",
1881 "stat events on existing process id"),
1882 OPT_STRING('t', "tid", &target.tid, "tid",
1883 "stat events on existing thread id"),
1884 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1885 "system-wide collection from all CPUs"),
1886 OPT_BOOLEAN('g', "group", &group,
1887 "put the counters into a counter group"),
1888 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1889 OPT_INCR('v', "verbose", &verbose,
1890 "be more verbose (show counter open errors, etc)"),
Jiri Olsad97ae042018-08-30 08:32:36 +02001891 OPT_INTEGER('r', "repeat", &stat_config.run_count,
Jiri Olsae0547312015-11-05 15:40:45 +01001892 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsae55c14a2018-04-23 11:08:21 +02001893 OPT_BOOLEAN(0, "table", &walltime_run_table,
1894 "display details about each run (only with -r option)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001895 OPT_BOOLEAN('n', "null", &null_run,
1896 "null run - dont start any counters"),
1897 OPT_INCR('d', "detailed", &detailed_run,
1898 "detailed run - start a lot of events"),
1899 OPT_BOOLEAN('S', "sync", &sync_run,
1900 "call sync() before starting a run"),
1901 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1902 "print large numbers with thousands\' separators",
1903 stat__set_big_num),
1904 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1905 "list of cpus to monitor in system-wide"),
1906 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1907 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001908 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsafa7070a2018-08-30 08:32:29 +02001909 OPT_STRING('x', "field-separator", &stat_config.csv_sep, "separator",
Jiri Olsae0547312015-11-05 15:40:45 +01001910 "print counts with custom separator"),
1911 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1912 "monitor event in cgroup name only", parse_cgroups),
1913 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1914 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1915 OPT_INTEGER(0, "log-fd", &output_fd,
1916 "log output to fd, instead of stderr"),
1917 OPT_STRING(0, "pre", &pre_cmd, "command",
1918 "command to run prior to the measured command"),
1919 OPT_STRING(0, "post", &post_cmd, "command",
1920 "command to run after to the measured command"),
1921 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03001922 "print counts at regular interval in ms "
1923 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01001924 OPT_INTEGER(0, "interval-count", &stat_config.times,
1925 "print counts for fixed number of times"),
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001926 OPT_BOOLEAN(0, "interval-clear", &stat_config.interval_clear,
Jiri Olsa9660e082018-06-07 00:15:06 +02001927 "clear screen in between new interval"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01001928 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1929 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001930 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1931 "aggregate counts per processor socket", AGGR_SOCKET),
1932 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1933 "aggregate counts per physical processor core", AGGR_CORE),
1934 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1935 "aggregate counts per thread", AGGR_THREAD),
Jiri Olsa728c0ee2018-08-30 08:32:11 +02001936 OPT_UINTEGER('D', "delay", &stat_config.initial_delay,
Jiri Olsae0547312015-11-05 15:40:45 +01001937 "ms to wait before starting measurement after program start"),
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001938 OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL,
Andi Kleen44b1e602016-05-30 12:49:42 -03001939 "Only print computed metrics. No raw values", enable_metric_only),
1940 OPT_BOOLEAN(0, "topdown", &topdown_run,
1941 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001942 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1943 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001944 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1945 "monitor specified metrics or metric groups (separated by ,)",
1946 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01001947 OPT_END()
1948};
1949
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001950static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1951{
1952 return cpu_map__get_socket(map, cpu, NULL);
1953}
1954
1955static int perf_stat__get_core(struct cpu_map *map, int cpu)
1956{
1957 return cpu_map__get_core(map, cpu, NULL);
1958}
1959
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001960static int cpu_map__get_max(struct cpu_map *map)
1961{
1962 int i, max = -1;
1963
1964 for (i = 0; i < map->nr; i++) {
1965 if (map->map[i] > max)
1966 max = map->map[i];
1967 }
1968
1969 return max;
1970}
1971
1972static struct cpu_map *cpus_aggr_map;
1973
1974static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1975{
1976 int cpu;
1977
1978 if (idx >= map->nr)
1979 return -1;
1980
1981 cpu = map->map[idx];
1982
1983 if (cpus_aggr_map->map[cpu] == -1)
1984 cpus_aggr_map->map[cpu] = get_id(map, idx);
1985
1986 return cpus_aggr_map->map[cpu];
1987}
1988
1989static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1990{
1991 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1992}
1993
1994static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1995{
1996 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1997}
1998
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001999static int perf_stat_init_aggr_mode(void)
2000{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002001 int nr;
2002
Jiri Olsa421a50f2015-07-21 14:31:22 +02002003 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002004 case AGGR_SOCKET:
2005 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
2006 perror("cannot build socket map");
2007 return -1;
2008 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002009 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002010 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002011 case AGGR_CORE:
2012 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
2013 perror("cannot build core map");
2014 return -1;
2015 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002016 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002017 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002018 case AGGR_NONE:
2019 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002020 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002021 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002022 default:
2023 break;
2024 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002025
2026 /*
2027 * The evsel_list->cpus is the base we operate on,
2028 * taking the highest cpu number to be the size of
2029 * the aggregation translate cpumap.
2030 */
2031 nr = cpu_map__get_max(evsel_list->cpus);
2032 cpus_aggr_map = cpu_map__empty_new(nr + 1);
2033 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002034}
2035
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002036static void perf_stat__exit_aggr_mode(void)
2037{
2038 cpu_map__put(aggr_map);
2039 cpu_map__put(cpus_aggr_map);
2040 aggr_map = NULL;
2041 cpus_aggr_map = NULL;
2042}
2043
Jiri Olsa68d702f2015-11-05 15:40:58 +01002044static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2045{
2046 int cpu;
2047
2048 if (idx > map->nr)
2049 return -1;
2050
2051 cpu = map->map[idx];
2052
Jan Stancekda8a58b2017-02-17 12:10:26 +01002053 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002054 return -1;
2055
2056 return cpu;
2057}
2058
2059static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2060{
2061 struct perf_env *env = data;
2062 int cpu = perf_env__get_cpu(env, map, idx);
2063
2064 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2065}
2066
2067static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2068{
2069 struct perf_env *env = data;
2070 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2071
2072 if (cpu != -1) {
2073 int socket_id = env->cpu[cpu].socket_id;
2074
2075 /*
2076 * Encode socket in upper 16 bits
2077 * core_id is relative to socket, and
2078 * we need a global id. So we combine
2079 * socket + core id.
2080 */
2081 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2082 }
2083
2084 return core;
2085}
2086
2087static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2088 struct cpu_map **sockp)
2089{
2090 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2091}
2092
2093static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2094 struct cpu_map **corep)
2095{
2096 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2097}
2098
2099static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2100{
2101 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2102}
2103
2104static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2105{
2106 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2107}
2108
2109static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2110{
2111 struct perf_env *env = &st->session->header.env;
2112
2113 switch (stat_config.aggr_mode) {
2114 case AGGR_SOCKET:
2115 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2116 perror("cannot build socket map");
2117 return -1;
2118 }
2119 aggr_get_id = perf_stat__get_socket_file;
2120 break;
2121 case AGGR_CORE:
2122 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2123 perror("cannot build core map");
2124 return -1;
2125 }
2126 aggr_get_id = perf_stat__get_core_file;
2127 break;
2128 case AGGR_NONE:
2129 case AGGR_GLOBAL:
2130 case AGGR_THREAD:
2131 case AGGR_UNSET:
2132 default:
2133 break;
2134 }
2135
2136 return 0;
2137}
2138
Andi Kleen44b1e602016-05-30 12:49:42 -03002139static int topdown_filter_events(const char **attr, char **str, bool use_group)
2140{
2141 int off = 0;
2142 int i;
2143 int len = 0;
2144 char *s;
2145
2146 for (i = 0; attr[i]; i++) {
2147 if (pmu_have_event("cpu", attr[i])) {
2148 len += strlen(attr[i]) + 1;
2149 attr[i - off] = attr[i];
2150 } else
2151 off++;
2152 }
2153 attr[i - off] = NULL;
2154
2155 *str = malloc(len + 1 + 2);
2156 if (!*str)
2157 return -1;
2158 s = *str;
2159 if (i - off == 0) {
2160 *s = 0;
2161 return 0;
2162 }
2163 if (use_group)
2164 *s++ = '{';
2165 for (i = 0; attr[i]; i++) {
2166 strcpy(s, attr[i]);
2167 s += strlen(s);
2168 *s++ = ',';
2169 }
2170 if (use_group) {
2171 s[-1] = '}';
2172 *s = 0;
2173 } else
2174 s[-1] = 0;
2175 return 0;
2176}
2177
2178__weak bool arch_topdown_check_group(bool *warn)
2179{
2180 *warn = false;
2181 return false;
2182}
2183
2184__weak void arch_topdown_group_warn(void)
2185{
2186}
2187
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002188/*
2189 * Add default attributes, if there were no attributes specified or
2190 * if -d/--detailed, -d -d or -d -d -d is used:
2191 */
2192static int add_default_attributes(void)
2193{
Andi Kleen44b1e602016-05-30 12:49:42 -03002194 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002195 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002196
2197 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2198 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2199 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2200 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2201
2202 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002203};
2204 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002205 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002206};
2207 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002208 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002209};
2210 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002211 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2212 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2213 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2214
2215};
2216
2217/*
2218 * Detailed stats (-d), covering the L1 and last level data caches:
2219 */
2220 struct perf_event_attr detailed_attrs[] = {
2221
2222 { .type = PERF_TYPE_HW_CACHE,
2223 .config =
2224 PERF_COUNT_HW_CACHE_L1D << 0 |
2225 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2226 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2227
2228 { .type = PERF_TYPE_HW_CACHE,
2229 .config =
2230 PERF_COUNT_HW_CACHE_L1D << 0 |
2231 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2232 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2233
2234 { .type = PERF_TYPE_HW_CACHE,
2235 .config =
2236 PERF_COUNT_HW_CACHE_LL << 0 |
2237 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2238 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2239
2240 { .type = PERF_TYPE_HW_CACHE,
2241 .config =
2242 PERF_COUNT_HW_CACHE_LL << 0 |
2243 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2244 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2245};
2246
2247/*
2248 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2249 */
2250 struct perf_event_attr very_detailed_attrs[] = {
2251
2252 { .type = PERF_TYPE_HW_CACHE,
2253 .config =
2254 PERF_COUNT_HW_CACHE_L1I << 0 |
2255 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2256 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2257
2258 { .type = PERF_TYPE_HW_CACHE,
2259 .config =
2260 PERF_COUNT_HW_CACHE_L1I << 0 |
2261 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2262 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2263
2264 { .type = PERF_TYPE_HW_CACHE,
2265 .config =
2266 PERF_COUNT_HW_CACHE_DTLB << 0 |
2267 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2268 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2269
2270 { .type = PERF_TYPE_HW_CACHE,
2271 .config =
2272 PERF_COUNT_HW_CACHE_DTLB << 0 |
2273 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2274 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2275
2276 { .type = PERF_TYPE_HW_CACHE,
2277 .config =
2278 PERF_COUNT_HW_CACHE_ITLB << 0 |
2279 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2280 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2281
2282 { .type = PERF_TYPE_HW_CACHE,
2283 .config =
2284 PERF_COUNT_HW_CACHE_ITLB << 0 |
2285 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2286 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2287
2288};
2289
2290/*
2291 * Very, very detailed stats (-d -d -d), adding prefetch events:
2292 */
2293 struct perf_event_attr very_very_detailed_attrs[] = {
2294
2295 { .type = PERF_TYPE_HW_CACHE,
2296 .config =
2297 PERF_COUNT_HW_CACHE_L1D << 0 |
2298 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2299 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2300
2301 { .type = PERF_TYPE_HW_CACHE,
2302 .config =
2303 PERF_COUNT_HW_CACHE_L1D << 0 |
2304 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2305 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2306};
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002307 struct parse_events_error errinfo;
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002308
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002309 /* Set attrs if no event is selected and !null_run: */
2310 if (null_run)
2311 return 0;
2312
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002313 if (transaction_run) {
Thomas Richter742d92f2018-06-26 09:17:01 +02002314 /* Handle -T as -M transaction. Once platform specific metrics
2315 * support has been added to the json files, all archictures
2316 * will use this approach. To determine transaction support
2317 * on an architecture test for such a metric name.
2318 */
2319 if (metricgroup__has_metric("transaction")) {
2320 struct option opt = { .value = &evsel_list };
2321
2322 return metricgroup__parse_groups(&opt, "transaction",
2323 &metric_events);
2324 }
2325
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002326 if (pmu_have_event("cpu", "cycles-ct") &&
2327 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002328 err = parse_events(evsel_list, transaction_attrs,
2329 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002330 else
Thomas Richterfca323402018-03-08 15:57:35 +01002331 err = parse_events(evsel_list,
2332 transaction_limited_attrs,
2333 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002334 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002335 fprintf(stderr, "Cannot set up transaction events\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002336 parse_events_print_error(&errinfo, transaction_attrs);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002337 return -1;
2338 }
2339 return 0;
2340 }
2341
Kan Liangdaefd0b2017-05-26 12:05:38 -07002342 if (smi_cost) {
2343 int smi;
2344
2345 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2346 fprintf(stderr, "freeze_on_smi is not supported.\n");
2347 return -1;
2348 }
2349
2350 if (!smi) {
2351 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2352 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2353 return -1;
2354 }
2355 smi_reset = true;
2356 }
2357
2358 if (pmu_have_event("msr", "aperf") &&
2359 pmu_have_event("msr", "smi")) {
2360 if (!force_metric_only)
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002361 stat_config.metric_only = true;
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002362 err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002363 } else {
2364 fprintf(stderr, "To measure SMI cost, it needs "
2365 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002366 parse_events_print_error(&errinfo, smi_cost_attrs);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002367 return -1;
2368 }
2369 if (err) {
2370 fprintf(stderr, "Cannot set up SMI cost events\n");
2371 return -1;
2372 }
2373 return 0;
2374 }
2375
Andi Kleen44b1e602016-05-30 12:49:42 -03002376 if (topdown_run) {
2377 char *str = NULL;
2378 bool warn = false;
2379
2380 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2381 stat_config.aggr_mode != AGGR_CORE) {
2382 pr_err("top down event configuration requires --per-core mode\n");
2383 return -1;
2384 }
2385 stat_config.aggr_mode = AGGR_CORE;
2386 if (nr_cgroups || !target__has_cpu(&target)) {
2387 pr_err("top down event configuration requires system-wide mode (-a)\n");
2388 return -1;
2389 }
2390
2391 if (!force_metric_only)
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002392 stat_config.metric_only = true;
Andi Kleen44b1e602016-05-30 12:49:42 -03002393 if (topdown_filter_events(topdown_attrs, &str,
2394 arch_topdown_check_group(&warn)) < 0) {
2395 pr_err("Out of memory\n");
2396 return -1;
2397 }
2398 if (topdown_attrs[0] && str) {
2399 if (warn)
2400 arch_topdown_group_warn();
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002401 err = parse_events(evsel_list, str, &errinfo);
Andi Kleen44b1e602016-05-30 12:49:42 -03002402 if (err) {
2403 fprintf(stderr,
2404 "Cannot set up top down events %s: %d\n",
2405 str, err);
2406 free(str);
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002407 parse_events_print_error(&errinfo, str);
Andi Kleen44b1e602016-05-30 12:49:42 -03002408 return -1;
2409 }
2410 } else {
2411 fprintf(stderr, "System does not support topdown\n");
2412 return -1;
2413 }
2414 free(str);
2415 }
2416
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002417 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002418 if (target__has_cpu(&target))
2419 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2420
Andi Kleen9dec4472016-02-26 16:27:56 -08002421 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2422 return -1;
2423 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2424 if (perf_evlist__add_default_attrs(evsel_list,
2425 frontend_attrs) < 0)
2426 return -1;
2427 }
2428 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2429 if (perf_evlist__add_default_attrs(evsel_list,
2430 backend_attrs) < 0)
2431 return -1;
2432 }
2433 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002434 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002435 }
2436
2437 /* Detailed events get appended to the event list: */
2438
2439 if (detailed_run < 1)
2440 return 0;
2441
2442 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002443 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002444 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002445
2446 if (detailed_run < 2)
2447 return 0;
2448
2449 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002450 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002451 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002452
2453 if (detailed_run < 3)
2454 return 0;
2455
2456 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002457 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002458}
2459
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002460static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002461 "perf stat record [<options>]",
2462 NULL,
2463};
2464
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002465static void init_features(struct perf_session *session)
2466{
2467 int feat;
2468
2469 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2470 perf_header__set_feat(&session->header, feat);
2471
2472 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2473 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2474 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2475 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2476}
2477
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002478static int __cmd_record(int argc, const char **argv)
2479{
2480 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002481 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002482
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002483 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002484 PARSE_OPT_STOP_AT_NON_OPTION);
2485
2486 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002487 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002488
Jiri Olsad97ae042018-08-30 08:32:36 +02002489 if (stat_config.run_count != 1 || forever) {
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002490 pr_err("Cannot use -r option with perf stat record.\n");
2491 return -1;
2492 }
2493
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002494 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002495 if (session == NULL) {
2496 pr_err("Perf session creation failed.\n");
2497 return -1;
2498 }
2499
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002500 init_features(session);
2501
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002502 session->evlist = evsel_list;
2503 perf_stat.session = session;
2504 perf_stat.record = true;
2505 return argc;
2506}
2507
Jiri Olsaa56f9392015-11-05 15:40:59 +01002508static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2509 union perf_event *event,
2510 struct perf_session *session)
2511{
Andi Kleene3b03b62016-05-05 16:04:03 -07002512 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002513 struct perf_evsel *counter;
2514 struct timespec tsh, *ts = NULL;
2515 const char **argv = session->header.env.cmdline_argv;
2516 int argc = session->header.env.nr_cmdline;
2517
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002518 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002519 perf_stat_process_counter(&stat_config, counter);
2520
Andi Kleene3b03b62016-05-05 16:04:03 -07002521 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2522 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002523
Andi Kleene3b03b62016-05-05 16:04:03 -07002524 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002525 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2526 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002527 ts = &tsh;
2528 }
2529
2530 print_counters(ts, argc, argv);
2531 return 0;
2532}
2533
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002534static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002535int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002536 union perf_event *event,
2537 struct perf_session *session __maybe_unused)
2538{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002539 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2540
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002541 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002542
Jiri Olsa89af4e02015-11-05 15:41:02 +01002543 if (cpu_map__empty(st->cpus)) {
2544 if (st->aggr_mode != AGGR_UNSET)
2545 pr_warning("warning: processing task data, aggregation mode not set\n");
2546 return 0;
2547 }
2548
2549 if (st->aggr_mode != AGGR_UNSET)
2550 stat_config.aggr_mode = st->aggr_mode;
2551
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002552 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002553 perf_stat_init_aggr_mode();
2554 else
2555 perf_stat_init_aggr_mode_file(st);
2556
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002557 return 0;
2558}
2559
Jiri Olsa1975d362015-11-05 15:40:56 +01002560static int set_maps(struct perf_stat *st)
2561{
2562 if (!st->cpus || !st->threads)
2563 return 0;
2564
2565 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2566 return -EINVAL;
2567
2568 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2569
2570 if (perf_evlist__alloc_stats(evsel_list, true))
2571 return -ENOMEM;
2572
2573 st->maps_allocated = true;
2574 return 0;
2575}
2576
2577static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002578int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002579 union perf_event *event,
2580 struct perf_session *session __maybe_unused)
2581{
2582 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2583
2584 if (st->threads) {
2585 pr_warning("Extra thread map event, ignoring.\n");
2586 return 0;
2587 }
2588
2589 st->threads = thread_map__new_event(&event->thread_map);
2590 if (!st->threads)
2591 return -ENOMEM;
2592
2593 return set_maps(st);
2594}
2595
2596static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002597int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002598 union perf_event *event,
2599 struct perf_session *session __maybe_unused)
2600{
2601 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2602 struct cpu_map *cpus;
2603
2604 if (st->cpus) {
2605 pr_warning("Extra cpu map event, ignoring.\n");
2606 return 0;
2607 }
2608
2609 cpus = cpu_map__new_data(&event->cpu_map.data);
2610 if (!cpus)
2611 return -ENOMEM;
2612
2613 st->cpus = cpus;
2614 return set_maps(st);
2615}
2616
Jin Yao56739442017-12-05 22:03:07 +08002617static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2618{
2619 int i;
2620
2621 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2622 if (!config->stats)
2623 return -1;
2624
2625 config->stats_num = nthreads;
2626
2627 for (i = 0; i < nthreads; i++)
2628 runtime_stat__init(&config->stats[i]);
2629
2630 return 0;
2631}
2632
2633static void runtime_stat_delete(struct perf_stat_config *config)
2634{
2635 int i;
2636
2637 if (!config->stats)
2638 return;
2639
2640 for (i = 0; i < config->stats_num; i++)
2641 runtime_stat__exit(&config->stats[i]);
2642
2643 free(config->stats);
2644}
2645
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002646static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002647 "perf stat report [<options>]",
2648 NULL,
2649};
2650
2651static struct perf_stat perf_stat = {
2652 .tool = {
2653 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002654 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002655 .thread_map = process_thread_map_event,
2656 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002657 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002658 .stat = perf_event__process_stat_event,
2659 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002660 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002661 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002662};
2663
2664static int __cmd_report(int argc, const char **argv)
2665{
2666 struct perf_session *session;
2667 const struct option options[] = {
2668 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002669 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2670 "aggregate counts per processor socket", AGGR_SOCKET),
2671 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2672 "aggregate counts per physical processor core", AGGR_CORE),
2673 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2674 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002675 OPT_END()
2676 };
2677 struct stat st;
2678 int ret;
2679
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002680 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002681
2682 if (!input_name || !strlen(input_name)) {
2683 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2684 input_name = "-";
2685 else
2686 input_name = "perf.data";
2687 }
2688
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002689 perf_stat.data.file.path = input_name;
2690 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002691
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002692 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002693 if (session == NULL)
2694 return -1;
2695
2696 perf_stat.session = session;
2697 stat_config.output = stderr;
2698 evsel_list = session->evlist;
2699
2700 ret = perf_session__process_events(session);
2701 if (ret)
2702 return ret;
2703
2704 perf_session__delete(session);
2705 return 0;
2706}
2707
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002708static void setup_system_wide(int forks)
2709{
2710 /*
2711 * Make system wide (-a) the default target if
2712 * no target was specified and one of following
2713 * conditions is met:
2714 *
2715 * - there's no workload specified
2716 * - there is workload specified but all requested
2717 * events are system wide events
2718 */
2719 if (!target__none(&target))
2720 return;
2721
2722 if (!forks)
2723 target.system_wide = true;
2724 else {
2725 struct perf_evsel *counter;
2726
2727 evlist__for_each_entry(evsel_list, counter) {
2728 if (!counter->system_wide)
2729 return;
2730 }
2731
2732 if (evsel_list->nr_entries)
2733 target.system_wide = true;
2734 }
2735}
2736
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002737int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002738{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002739 const char * const stat_usage[] = {
2740 "perf stat [<options>] [<command>]",
2741 NULL
2742 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002743 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002744 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002745 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002746 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002747 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002748
Stephane Eranian5af52b52010-05-18 15:00:01 +02002749 setlocale(LC_ALL, "");
2750
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002751 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002752 if (evsel_list == NULL)
2753 return -ENOMEM;
2754
Wang Nan1669e502016-02-19 11:43:58 +00002755 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002756 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2757 (const char **) stat_usage,
2758 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002759 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002760 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002761
Jiri Olsafa7070a2018-08-30 08:32:29 +02002762 if (stat_config.csv_sep) {
2763 stat_config.csv_output = true;
2764 if (!strcmp(stat_config.csv_sep, "\\t"))
2765 stat_config.csv_sep = "\t";
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002766 } else
Jiri Olsafa7070a2018-08-30 08:32:29 +02002767 stat_config.csv_sep = DEFAULT_SEPARATOR;
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002768
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002769 if (argc && !strncmp(argv[0], "rec", 3)) {
2770 argc = __cmd_record(argc, argv);
2771 if (argc < 0)
2772 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002773 } else if (argc && !strncmp(argv[0], "rep", 3))
2774 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002775
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002776 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002777 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002778
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002779 /*
2780 * For record command the -o is already taken care of.
2781 */
2782 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002783 output = NULL;
2784
Jim Cromie56f3bae2011-09-07 17:14:00 -06002785 if (output_name && output_fd) {
2786 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002787 parse_options_usage(stat_usage, stat_options, "o", 1);
2788 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002789 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002790 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002791
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002792 if (stat_config.metric_only && stat_config.aggr_mode == AGGR_THREAD) {
Andi Kleen54b50912016-03-03 15:57:36 -08002793 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2794 goto out;
2795 }
2796
Jiri Olsad97ae042018-08-30 08:32:36 +02002797 if (stat_config.metric_only && stat_config.run_count > 1) {
Andi Kleen54b50912016-03-03 15:57:36 -08002798 fprintf(stderr, "--metric-only is not supported with -r\n");
2799 goto out;
2800 }
2801
Jiri Olsad97ae042018-08-30 08:32:36 +02002802 if (walltime_run_table && stat_config.run_count <= 1) {
Jiri Olsae55c14a2018-04-23 11:08:21 +02002803 fprintf(stderr, "--table is only supported with -r\n");
2804 parse_options_usage(stat_usage, stat_options, "r", 1);
2805 parse_options_usage(NULL, stat_options, "table", 0);
2806 goto out;
2807 }
2808
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002809 if (output_fd < 0) {
2810 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002811 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002812 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002813 }
2814
Stephane Eranian4aa90152011-08-15 22:22:33 +02002815 if (!output) {
2816 struct timespec tm;
2817 mode = append_file ? "a" : "w";
2818
2819 output = fopen(output_name, mode);
2820 if (!output) {
2821 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002822 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002823 }
2824 clock_gettime(CLOCK_REALTIME, &tm);
2825 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002826 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002827 mode = append_file ? "a" : "w";
2828 output = fdopen(output_fd, mode);
2829 if (!output) {
2830 perror("Failed opening logfd");
2831 return -errno;
2832 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002833 }
2834
Jiri Olsa58215222015-07-21 14:31:24 +02002835 stat_config.output = output;
2836
Stephane Eraniand7470b62010-12-01 18:49:05 +02002837 /*
2838 * let the spreadsheet do the pretty-printing
2839 */
Jiri Olsafa7070a2018-08-30 08:32:29 +02002840 if (stat_config.csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002841 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002842 if (big_num_opt == 1) {
2843 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002844 parse_options_usage(stat_usage, stat_options, "B", 1);
2845 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002846 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002847 } else /* Nope, so disable big number formatting */
2848 big_num = false;
2849 } else if (big_num_opt == 0) /* User passed --no-big-num */
2850 big_num = false;
2851
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002852 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002853
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002854 /*
2855 * Display user/system times only for single
2856 * run and when there's specified tracee.
2857 */
Jiri Olsad97ae042018-08-30 08:32:36 +02002858 if ((stat_config.run_count == 1) && target__none(&target))
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002859 ru_display = true;
2860
Jiri Olsad97ae042018-08-30 08:32:36 +02002861 if (stat_config.run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002862 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002863 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002864 goto out;
Jiri Olsad97ae042018-08-30 08:32:36 +02002865 } else if (stat_config.run_count == 0) {
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002866 forever = true;
Jiri Olsad97ae042018-08-30 08:32:36 +02002867 stat_config.run_count = 1;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002868 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002869
Jiri Olsae55c14a2018-04-23 11:08:21 +02002870 if (walltime_run_table) {
Jiri Olsad97ae042018-08-30 08:32:36 +02002871 walltime_run = zalloc(stat_config.run_count * sizeof(walltime_run[0]));
Jiri Olsae55c14a2018-04-23 11:08:21 +02002872 if (!walltime_run) {
2873 pr_err("failed to setup -r option");
2874 goto out;
2875 }
2876 }
2877
Jin Yao1d9f8d12017-12-05 22:03:10 +08002878 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2879 !target__has_task(&target)) {
2880 if (!target.system_wide || target.cpu_list) {
2881 fprintf(stderr, "The --per-thread option is only "
2882 "available when monitoring via -p -t -a "
2883 "options or only --per-thread.\n");
2884 parse_options_usage(NULL, stat_options, "p", 1);
2885 parse_options_usage(NULL, stat_options, "t", 1);
2886 goto out;
2887 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002888 }
2889
2890 /*
2891 * no_aggr, cgroup are for system-wide only
2892 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2893 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002894 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2895 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002896 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002897 fprintf(stderr, "both cgroup and no-aggregation "
2898 "modes only available in system-wide mode\n");
2899
Jiri Olsae0547312015-11-05 15:40:45 +01002900 parse_options_usage(stat_usage, stat_options, "G", 1);
2901 parse_options_usage(NULL, stat_options, "A", 1);
2902 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002903 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002904 }
2905
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002906 if (add_default_attributes())
2907 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002908
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002909 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002910
Jin Yao1d9f8d12017-12-05 22:03:10 +08002911 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2912 target.per_thread = true;
2913
Namhyung Kim77a6f012012-05-07 14:09:04 +09002914 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002915 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002916 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002917 parse_options_usage(stat_usage, stat_options, "p", 1);
2918 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002919 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002920 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002921 parse_options_usage(stat_usage, stat_options, "C", 1);
2922 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002923 }
2924 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002925 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002926
2927 /*
2928 * Initialize thread_map with comm names,
2929 * so we could print it out on output.
2930 */
Jin Yao56739442017-12-05 22:03:07 +08002931 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002932 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08002933 if (target.system_wide) {
2934 if (runtime_stat_new(&stat_config,
2935 thread_map__nr(evsel_list->threads))) {
2936 goto out;
2937 }
2938 }
2939 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002940
yuzhoujiandb06a262018-01-29 10:25:22 +01002941 if (stat_config.times && interval)
2942 interval_count = true;
2943 else if (stat_config.times && !interval) {
2944 pr_err("interval-count option should be used together with "
2945 "interval-print.\n");
2946 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2947 parse_options_usage(stat_usage, stat_options, "I", 1);
2948 goto out;
2949 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002950
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002951 if (timeout && timeout < 100) {
2952 if (timeout < 10) {
2953 pr_err("timeout must be >= 10ms.\n");
2954 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2955 goto out;
2956 } else
2957 pr_warning("timeout < 100ms. "
2958 "The overhead percentage could be high in some cases. "
2959 "Please proceed with caution.\n");
2960 }
2961 if (timeout && interval) {
2962 pr_err("timeout option is not supported with interval-print.\n");
2963 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2964 parse_options_usage(stat_usage, stat_options, "I", 1);
2965 goto out;
2966 }
2967
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002968 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002969 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002970
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002971 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002972 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002973
Ingo Molnar58d7e992009-05-15 11:03:23 +02002974 /*
Jiri Olsa7d9ad162018-08-30 08:32:14 +02002975 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
2976 * while avoiding that older tools show confusing messages.
2977 *
2978 * However for pipe sessions we need to keep it zero,
2979 * because script's perf_evsel__check_attr is triggered
2980 * by attr->sample_type != 0, and we can't run it on
2981 * stat sessions.
2982 */
2983 stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
2984
2985 /*
Ingo Molnar58d7e992009-05-15 11:03:23 +02002986 * We dont want to block the signals - that would cause
2987 * child tasks to inherit that and Ctrl-C would not work.
2988 * What we want is for Ctrl-C to work in the exec()-ed
2989 * task, but being ignored by perf stat itself:
2990 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002991 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002992 if (!forever)
2993 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002994 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002995 signal(SIGALRM, skip_signal);
2996 signal(SIGABRT, skip_signal);
2997
Ingo Molnar42202dd2009-06-13 14:57:28 +02002998 status = 0;
Jiri Olsad97ae042018-08-30 08:32:36 +02002999 for (run_idx = 0; forever || run_idx < stat_config.run_count; run_idx++) {
3000 if (stat_config.run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02003001 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
3002 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02003003
Jiri Olsae55c14a2018-04-23 11:08:21 +02003004 status = run_perf_stat(argc, argv, run_idx);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003005 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02003006 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02003007 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003008 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02003009 }
3010
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003011 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02003012 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003013
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003014 if (STAT_RECORD) {
3015 /*
3016 * We synthesize the kernel mmap record just so that older tools
3017 * don't emit warnings about not being able to resolve symbols
3018 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3019 * a saner message about no samples being in the perf.data file.
3020 *
3021 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01003022 * in header.c at the moment 'perf stat record' gets introduced, which
3023 * is not really needed once we start adding the stat specific PERF_RECORD_
3024 * records, but the need to suppress the kptr_restrict messages in older
3025 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003026 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003027 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003028 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3029 process_synthesized_event,
3030 &perf_stat.session->machines.host);
3031 if (err) {
3032 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3033 "older tools may produce warnings about this file\n.");
3034 }
3035
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003036 if (!interval) {
3037 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3038 pr_err("failed to write stat round event\n");
3039 }
3040
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003041 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003042 perf_stat.session->header.data_size += perf_stat.bytes_written;
3043 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3044 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003045
3046 perf_session__delete(perf_stat.session);
3047 }
3048
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003049 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003050 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003051out:
Jiri Olsae55c14a2018-04-23 11:08:21 +02003052 free(walltime_run);
3053
Kan Liangdaefd0b2017-05-26 12:05:38 -07003054 if (smi_cost && smi_reset)
3055 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3056
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003057 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003058
3059 runtime_stat_delete(&stat_config);
3060
Ingo Molnar42202dd2009-06-13 14:57:28 +02003061 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003062}