blob: 528f85146b59dcbd091b9ef8d4800e69f3581025 [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
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530150static int run_count = 1;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200151static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000152static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200153static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700154static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300155static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700156static bool smi_cost = false;
157static bool smi_reset = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200158static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200159static int big_num_opt = -1;
Lin Ming43bece72011-08-17 18:42:07 +0800160static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200161static const char *pre_cmd = NULL;
162static const char *post_cmd = NULL;
163static bool sync_run = false;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500164static bool forever = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300165static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700166static bool no_merge = false;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200167static bool walltime_run_table = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100168static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100169static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100170static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100171static bool append_file;
yuzhoujiandb06a262018-01-29 10:25:22 +0100172static bool interval_count;
Jiri Olsae0547312015-11-05 15:40:45 +0100173static const char *output_name;
174static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100175static int print_free_counters_hint;
Kan Liang30060ea2018-04-24 11:20:11 -0700176static int print_mixed_hw_group_error;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200177static u64 *walltime_run;
Jiri Olsa0ce2da12018-06-05 14:13:13 +0200178static bool ru_display = false;
179static struct rusage ru_data;
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200180static unsigned int metric_only_len = METRIC_ONLY_LEN;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200181
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100182struct perf_stat {
183 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100184 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100185 struct perf_session *session;
186 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100187 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100188 bool maps_allocated;
189 struct cpu_map *cpus;
190 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100191 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100192};
193
194static struct perf_stat perf_stat;
195#define STAT_RECORD perf_stat.record
196
Liming Wang60666c62009-12-31 16:05:50 +0800197static volatile int done = 0;
198
Jiri Olsa421a50f2015-07-21 14:31:22 +0200199static struct perf_stat_config stat_config = {
200 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200201 .scale = true,
Jiri Olsadf4f7b42018-08-30 08:32:32 +0200202 .unit_width = 4, /* strlen("unit") */
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
Peter Zijlstra849abde2009-09-04 18:23:38 +0200689 if (run_count == 1)
690 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 Olsa6ca9a082018-08-30 08:32:28 +0200857static void print_metric_only(struct perf_stat_config *config __maybe_unused,
858 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 Olsac1a1f5d2018-06-07 00:15:09 +0200864 unsigned mlen = 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 Olsa6ca9a082018-08-30 08:32:28 +0200905static void print_metric_header(struct perf_stat_config *config __maybe_unused,
906 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 Olsac1a1f5d2018-06-07 00:15:09 +0200919 fprintf(os->fh, "%*s ", metric_only_len, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800920}
921
Andi Kleen44d49a62016-02-29 14:36:22 -0800922static int first_shadow_cpu(struct perf_evsel *evsel, int id)
923{
924 int i;
925
926 if (!aggr_get_id)
927 return 0;
928
929 if (stat_config.aggr_mode == AGGR_NONE)
930 return id;
931
932 if (stat_config.aggr_mode == AGGR_GLOBAL)
933 return 0;
934
935 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
936 int cpu2 = perf_evsel__cpus(evsel)->map[i];
937
938 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
939 return cpu2;
940 }
941 return 0;
942}
943
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200944static void abs_printout(struct perf_stat_config *config,
945 int id, int nr, struct perf_evsel *evsel, double avg)
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200946{
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200947 FILE *output = config->output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200948 double sc = evsel->scale;
949 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200950
Jiri Olsafa7070a2018-08-30 08:32:29 +0200951 if (config->csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -0700952 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200953 } else {
954 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -0700955 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200956 else
Andi Kleene3b03b62016-05-05 16:04:03 -0700957 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200958 }
959
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200960 aggr_printout(config, evsel, id, nr);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200961
Jiri Olsafa7070a2018-08-30 08:32:29 +0200962 fprintf(output, fmt, avg, config->csv_sep);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200963
964 if (evsel->unit)
965 fprintf(output, "%-*s%s",
Jiri Olsadf4f7b42018-08-30 08:32:32 +0200966 config->csv_output ? 0 : config->unit_width,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200967 evsel->unit, config->csv_sep);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200968
Jiri Olsafa7070a2018-08-30 08:32:29 +0200969 fprintf(output, "%-*s", config->csv_output ? 0 : 25, perf_evsel__name(evsel));
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200970
971 if (evsel->cgrp)
Jiri Olsafa7070a2018-08-30 08:32:29 +0200972 fprintf(output, "%s%s", config->csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -0800973}
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200974
Kan Liang30060ea2018-04-24 11:20:11 -0700975static bool is_mixed_hw_group(struct perf_evsel *counter)
976{
977 struct perf_evlist *evlist = counter->evlist;
978 u32 pmu_type = counter->attr.type;
979 struct perf_evsel *pos;
980
981 if (counter->nr_members < 2)
982 return false;
983
984 evlist__for_each_entry(evlist, pos) {
985 /* software events can be part of any hardware group */
986 if (pos->attr.type == PERF_TYPE_SOFTWARE)
987 continue;
988 if (pmu_type == PERF_TYPE_SOFTWARE) {
989 pmu_type = pos->attr.type;
990 continue;
991 }
992 if (pmu_type != pos->attr.type)
993 return true;
994 }
995
996 return false;
997}
998
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200999static void printout(struct perf_stat_config *config, int id, int nr,
1000 struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +08001001 char *prefix, u64 run, u64 ena, double noise,
1002 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001003{
Andi Kleen140aead2016-01-30 09:06:49 -08001004 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001005 struct outstate os = {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001006 .fh = config->output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001007 .prefix = prefix ? prefix : "",
1008 .id = id,
1009 .nr = nr,
1010 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001011 };
Andi Kleen140aead2016-01-30 09:06:49 -08001012 print_metric_t pm = print_metric_std;
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001013 new_line_t nl;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001014
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001015 if (config->metric_only) {
Andi Kleen54b50912016-03-03 15:57:36 -08001016 nl = new_line_metric;
Jiri Olsafa7070a2018-08-30 08:32:29 +02001017 if (config->csv_output)
Andi Kleen54b50912016-03-03 15:57:36 -08001018 pm = print_metric_only_csv;
1019 else
1020 pm = print_metric_only;
1021 } else
1022 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001023
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001024 if (config->csv_output && !config->metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001025 static int aggr_fields[] = {
1026 [AGGR_GLOBAL] = 0,
1027 [AGGR_THREAD] = 1,
1028 [AGGR_NONE] = 1,
1029 [AGGR_SOCKET] = 2,
1030 [AGGR_CORE] = 2,
1031 };
1032
1033 pm = print_metric_csv;
1034 nl = new_line_csv;
1035 os.nfields = 3;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001036 os.nfields += aggr_fields[config->aggr_mode];
Andi Kleen92a61f62016-02-29 14:36:21 -08001037 if (counter->cgrp)
1038 os.nfields++;
1039 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001040 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001041 if (config->metric_only) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001042 pm(config, &os, NULL, "", "", 0);
Andi Kleen54b50912016-03-03 15:57:36 -08001043 return;
1044 }
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001045 aggr_printout(config, counter, id, nr);
Andi Kleencb110f42016-01-30 09:06:51 -08001046
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001047 fprintf(config->output, "%*s%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +02001048 config->csv_output ? 0 : 18,
Andi Kleencb110f42016-01-30 09:06:51 -08001049 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Jiri Olsafa7070a2018-08-30 08:32:29 +02001050 config->csv_sep);
Andi Kleencb110f42016-01-30 09:06:51 -08001051
Kan Liang30060ea2018-04-24 11:20:11 -07001052 if (counter->supported) {
Borislav Petkov02d492e2017-02-07 01:40:05 +01001053 print_free_counters_hint = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001054 if (is_mixed_hw_group(counter))
1055 print_mixed_hw_group_error = 1;
1056 }
Borislav Petkov02d492e2017-02-07 01:40:05 +01001057
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001058 fprintf(config->output, "%-*s%s",
Jiri Olsadf4f7b42018-08-30 08:32:32 +02001059 config->csv_output ? 0 : config->unit_width,
Jiri Olsafa7070a2018-08-30 08:32:29 +02001060 counter->unit, config->csv_sep);
Andi Kleencb110f42016-01-30 09:06:51 -08001061
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001062 fprintf(config->output, "%*s",
Jiri Olsafa7070a2018-08-30 08:32:29 +02001063 config->csv_output ? 0 : -25,
Andi Kleencb110f42016-01-30 09:06:51 -08001064 perf_evsel__name(counter));
1065
1066 if (counter->cgrp)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001067 fprintf(config->output, "%s%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +02001068 config->csv_sep, counter->cgrp->name);
Andi Kleencb110f42016-01-30 09:06:51 -08001069
Jiri Olsafa7070a2018-08-30 08:32:29 +02001070 if (!config->csv_output)
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001071 pm(config, &os, NULL, NULL, "", 0);
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001072 print_noise(config, counter, noise);
1073 print_running(config, run, ena);
Jiri Olsafa7070a2018-08-30 08:32:29 +02001074 if (config->csv_output)
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001075 pm(config, &os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001076 return;
1077 }
1078
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001079 if (!config->metric_only)
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001080 abs_printout(config, id, nr, counter, uval);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001081
Andi Kleen140aead2016-01-30 09:06:49 -08001082 out.print_metric = pm;
1083 out.new_line = nl;
1084 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001085 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001086
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001087 if (config->csv_output && !config->metric_only) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001088 print_noise(config, counter, noise);
1089 print_running(config, run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001090 }
1091
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001092 perf_stat__print_shadow_stats(config, counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001093 first_shadow_cpu(counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001094 &out, &metric_events, st);
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001095 if (!config->csv_output && !config->metric_only) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001096 print_noise(config, counter, noise);
1097 print_running(config, run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001098 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001099}
1100
Andi Kleen44d49a62016-02-29 14:36:22 -08001101static void aggr_update_shadow(void)
1102{
1103 int cpu, s2, id, s;
1104 u64 val;
1105 struct perf_evsel *counter;
1106
1107 for (s = 0; s < aggr_map->nr; s++) {
1108 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001109 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001110 val = 0;
1111 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1112 s2 = aggr_get_id(evsel_list->cpus, cpu);
1113 if (s2 != id)
1114 continue;
1115 val += perf_counts(counter->counts, cpu, 0)->val;
1116 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001117 perf_stat__update_shadow_stats(counter, val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001118 first_shadow_cpu(counter, id),
1119 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001120 }
1121 }
1122}
1123
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001124static void uniquify_event_name(struct perf_evsel *counter)
1125{
1126 char *new_name;
1127 char *config;
1128
Kan Liang80ee8c52018-04-24 11:20:14 -07001129 if (counter->uniquified_name ||
1130 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001131 strlen(counter->pmu_name)))
1132 return;
1133
1134 config = strchr(counter->name, '/');
1135 if (config) {
1136 if (asprintf(&new_name,
1137 "%s%s", counter->pmu_name, config) > 0) {
1138 free(counter->name);
1139 counter->name = new_name;
1140 }
1141 } else {
1142 if (asprintf(&new_name,
1143 "%s [%s]", counter->name, counter->pmu_name) > 0) {
1144 free(counter->name);
1145 counter->name = new_name;
1146 }
1147 }
Kan Liang80ee8c52018-04-24 11:20:14 -07001148
1149 counter->uniquified_name = true;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001150}
1151
Andi Kleen430daf22017-03-20 13:17:00 -07001152static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001153 void (*cb)(struct perf_evsel *counter, void *data,
1154 bool first),
1155 void *data)
1156{
Andi Kleen430daf22017-03-20 13:17:00 -07001157 struct perf_evsel *alias;
1158
1159 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1160 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1161 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1162 alias->scale != counter->scale ||
1163 alias->cgrp != counter->cgrp ||
1164 strcmp(alias->unit, counter->unit) ||
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001165 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter))
Andi Kleen430daf22017-03-20 13:17:00 -07001166 break;
1167 alias->merged_stat = true;
1168 cb(alias, data, false);
1169 }
1170}
1171
1172static bool collect_data(struct perf_evsel *counter,
1173 void (*cb)(struct perf_evsel *counter, void *data,
1174 bool first),
1175 void *data)
1176{
1177 if (counter->merged_stat)
1178 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001179 cb(counter, data, true);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001180 if (no_merge)
1181 uniquify_event_name(counter);
1182 else if (counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001183 collect_all_aliases(counter, cb, data);
1184 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001185}
1186
1187struct aggr_data {
1188 u64 ena, run, val;
1189 int id;
1190 int nr;
1191 int cpu;
1192};
1193
1194static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1195{
1196 struct aggr_data *ad = data;
1197 int cpu, s2;
1198
1199 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1200 struct perf_counts_values *counts;
1201
1202 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1203 if (s2 != ad->id)
1204 continue;
1205 if (first)
1206 ad->nr++;
1207 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001208 /*
1209 * When any result is bad, make them all to give
1210 * consistent output in interval mode.
1211 */
1212 if (counts->ena == 0 || counts->run == 0 ||
1213 counter->counts->scaled == -1) {
1214 ad->ena = 0;
1215 ad->run = 0;
1216 break;
1217 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001218 ad->val += counts->val;
1219 ad->ena += counts->ena;
1220 ad->run += counts->run;
1221 }
1222}
1223
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001224static void print_aggr(struct perf_stat_config *config,
1225 char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001226{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001227 bool metric_only = config->metric_only;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001228 FILE *output = config->output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001229 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001230 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001231 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001232 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001233 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001234
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001235 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001236 return;
1237
Andi Kleen44d49a62016-02-29 14:36:22 -08001238 aggr_update_shadow();
1239
Andi Kleen54b50912016-03-03 15:57:36 -08001240 /*
1241 * With metric_only everything is on a single line.
1242 * Without each counter has its own line.
1243 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001244 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001245 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001246 if (prefix && metric_only)
1247 fprintf(output, "%s", prefix);
1248
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001249 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001250 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001251 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001252 if (is_duration_time(counter))
1253 continue;
1254
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001255 ad.val = ad.ena = ad.run = 0;
1256 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001257 if (!collect_data(counter, aggr_cb, &ad))
1258 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001259 nr = ad.nr;
1260 ena = ad.ena;
1261 run = ad.run;
1262 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001263 if (first && metric_only) {
1264 first = false;
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001265 aggr_printout(config, counter, id, nr);
Andi Kleen54b50912016-03-03 15:57:36 -08001266 }
1267 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001268 fprintf(output, "%s", prefix);
1269
Stephane Eranian410136f2013-11-12 17:58:49 +01001270 uval = val * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001271 printout(config, id, nr, counter, uval, prefix,
1272 run, ena, 1.0, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001273 if (!metric_only)
1274 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001275 }
Andi Kleen54b50912016-03-03 15:57:36 -08001276 if (metric_only)
1277 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001278 }
1279}
1280
Jin Yao29734552017-12-05 22:03:11 +08001281static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001282{
Jin Yao29734552017-12-05 22:03:11 +08001283 return ((struct perf_aggr_thread_value *)b)->val -
1284 ((struct perf_aggr_thread_value *)a)->val;
1285}
1286
1287static struct perf_aggr_thread_value *sort_aggr_thread(
1288 struct perf_evsel *counter,
1289 int nthreads, int ncpus,
1290 int *ret)
1291{
1292 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001293 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001294 struct perf_aggr_thread_value *buf;
1295
1296 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1297 if (!buf)
1298 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001299
1300 for (thread = 0; thread < nthreads; thread++) {
1301 u64 ena = 0, run = 0, val = 0;
1302
1303 for (cpu = 0; cpu < ncpus; cpu++) {
1304 val += perf_counts(counter->counts, cpu, thread)->val;
1305 ena += perf_counts(counter->counts, cpu, thread)->ena;
1306 run += perf_counts(counter->counts, cpu, thread)->run;
1307 }
1308
Jin Yao29734552017-12-05 22:03:11 +08001309 uval = val * counter->scale;
1310
1311 /*
1312 * Skip value 0 when enabling --per-thread globally,
1313 * otherwise too many 0 output.
1314 */
1315 if (uval == 0.0 && target__has_per_thread(&target))
1316 continue;
1317
1318 buf[i].counter = counter;
1319 buf[i].id = thread;
1320 buf[i].uval = uval;
1321 buf[i].val = val;
1322 buf[i].run = run;
1323 buf[i].ena = ena;
1324 i++;
1325 }
1326
1327 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1328
1329 if (ret)
1330 *ret = i;
1331
1332 return buf;
1333}
1334
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001335static void print_aggr_thread(struct perf_stat_config *config,
1336 struct perf_evsel *counter, char *prefix)
Jin Yao29734552017-12-05 22:03:11 +08001337{
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001338 FILE *output = config->output;
Jin Yao29734552017-12-05 22:03:11 +08001339 int nthreads = thread_map__nr(counter->threads);
1340 int ncpus = cpu_map__nr(counter->cpus);
1341 int thread, sorted_threads, id;
1342 struct perf_aggr_thread_value *buf;
1343
1344 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads);
1345 if (!buf) {
1346 perror("cannot sort aggr thread");
1347 return;
1348 }
1349
1350 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001351 if (prefix)
1352 fprintf(output, "%s", prefix);
1353
Jin Yao29734552017-12-05 22:03:11 +08001354 id = buf[thread].id;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001355 if (config->stats)
1356 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
Jin Yao29734552017-12-05 22:03:11 +08001357 prefix, buf[thread].run, buf[thread].ena, 1.0,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001358 &config->stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001359 else
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001360 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
Jin Yao29734552017-12-05 22:03:11 +08001361 prefix, buf[thread].run, buf[thread].ena, 1.0,
1362 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001363 fputc('\n', output);
1364 }
Jin Yao29734552017-12-05 22:03:11 +08001365
1366 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001367}
1368
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001369struct caggr_data {
1370 double avg, avg_enabled, avg_running;
1371};
1372
1373static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1374 bool first __maybe_unused)
1375{
1376 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001377 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001378
1379 cd->avg += avg_stats(&ps->res_stats[0]);
1380 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1381 cd->avg_running += avg_stats(&ps->res_stats[2]);
1382}
1383
Ingo Molnar42202dd2009-06-13 14:57:28 +02001384/*
1385 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001386 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001387 */
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001388static void print_counter_aggr(struct perf_stat_config *config,
1389 struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001390{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001391 bool metric_only = config->metric_only;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001392 FILE *output = config->output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001393 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001394 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001395
Andi Kleen430daf22017-03-20 13:17:00 -07001396 if (!collect_data(counter, counter_aggr_cb, &cd))
1397 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001398
Andi Kleen54b50912016-03-03 15:57:36 -08001399 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001400 fprintf(output, "%s", prefix);
1401
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001402 uval = cd.avg * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001403 printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
Jin Yaoe0128b32017-12-05 22:03:05 +08001404 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001405 if (!metric_only)
1406 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001407}
1408
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001409static void counter_cb(struct perf_evsel *counter, void *data,
1410 bool first __maybe_unused)
1411{
1412 struct aggr_data *ad = data;
1413
1414 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1415 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1416 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1417}
1418
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001419/*
1420 * Print out the results of a single counter:
1421 * does not use aggregated count in system-wide
1422 */
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001423static void print_counter(struct perf_stat_config *config,
1424 struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001425{
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001426 FILE *output = config->output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001427 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001428 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001429 int cpu;
1430
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001431 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001432 struct aggr_data ad = { .cpu = cpu };
1433
Andi Kleen430daf22017-03-20 13:17:00 -07001434 if (!collect_data(counter, counter_cb, &ad))
1435 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001436 val = ad.val;
1437 ena = ad.ena;
1438 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001439
1440 if (prefix)
1441 fprintf(output, "%s", prefix);
1442
Stephane Eranian410136f2013-11-12 17:58:49 +01001443 uval = val * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001444 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
Jin Yaoe0128b32017-12-05 22:03:05 +08001445 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001446
Stephane Eranian4aa90152011-08-15 22:22:33 +02001447 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001448 }
1449}
1450
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001451static void print_no_aggr_metric(struct perf_stat_config *config,
1452 char *prefix)
Andi Kleen206cab62016-03-03 15:57:37 -08001453{
1454 int cpu;
1455 int nrcpus = 0;
1456 struct perf_evsel *counter;
1457 u64 ena, run, val;
1458 double uval;
1459
1460 nrcpus = evsel_list->cpus->nr;
1461 for (cpu = 0; cpu < nrcpus; cpu++) {
1462 bool first = true;
1463
1464 if (prefix)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001465 fputs(prefix, config->output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001466 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001467 if (is_duration_time(counter))
1468 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001469 if (first) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001470 aggr_printout(config, counter, cpu, 0);
Andi Kleen206cab62016-03-03 15:57:37 -08001471 first = false;
1472 }
1473 val = perf_counts(counter->counts, cpu, 0)->val;
1474 ena = perf_counts(counter->counts, cpu, 0)->ena;
1475 run = perf_counts(counter->counts, cpu, 0)->run;
1476
1477 uval = val * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001478 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
Jin Yaoe0128b32017-12-05 22:03:05 +08001479 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001480 }
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001481 fputc('\n', config->output);
Andi Kleen206cab62016-03-03 15:57:37 -08001482 }
1483}
1484
Andi Kleen54b50912016-03-03 15:57:36 -08001485static int aggr_header_lens[] = {
1486 [AGGR_CORE] = 18,
1487 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001488 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001489 [AGGR_THREAD] = 24,
1490 [AGGR_GLOBAL] = 0,
1491};
1492
Andi Kleenc51fd632016-05-24 12:52:39 -07001493static const char *aggr_header_csv[] = {
1494 [AGGR_CORE] = "core,cpus,",
1495 [AGGR_SOCKET] = "socket,cpus",
1496 [AGGR_NONE] = "cpu,",
1497 [AGGR_THREAD] = "comm-pid,",
1498 [AGGR_GLOBAL] = ""
1499};
1500
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001501static void print_metric_headers(struct perf_stat_config *config,
1502 const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001503{
1504 struct perf_stat_output_ctx out;
1505 struct perf_evsel *counter;
1506 struct outstate os = {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001507 .fh = config->output
Andi Kleen54b50912016-03-03 15:57:36 -08001508 };
1509
1510 if (prefix)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001511 fprintf(config->output, "%s", prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001512
Jiri Olsafa7070a2018-08-30 08:32:29 +02001513 if (!config->csv_output && !no_indent)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001514 fprintf(config->output, "%*s",
1515 aggr_header_lens[config->aggr_mode], "");
Jiri Olsafa7070a2018-08-30 08:32:29 +02001516 if (config->csv_output) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001517 if (config->interval)
1518 fputs("time,", config->output);
1519 fputs(aggr_header_csv[config->aggr_mode], config->output);
Andi Kleenc51fd632016-05-24 12:52:39 -07001520 }
Andi Kleen54b50912016-03-03 15:57:36 -08001521
1522 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001523 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001524 if (is_duration_time(counter))
1525 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001526 os.evsel = counter;
1527 out.ctx = &os;
1528 out.print_metric = print_metric_header;
1529 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001530 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001531 os.evsel = counter;
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001532 perf_stat__print_shadow_stats(config, counter, 0,
Andi Kleen54b50912016-03-03 15:57:36 -08001533 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001534 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001535 &metric_events,
1536 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001537 }
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001538 fputc('\n', config->output);
Andi Kleen54b50912016-03-03 15:57:36 -08001539}
1540
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001541static void print_interval(struct perf_stat_config *config,
1542 char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001543{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001544 bool metric_only = config->metric_only;
Jiri Olsadf4f7b42018-08-30 08:32:32 +02001545 unsigned int unit_width = config->unit_width;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001546 FILE *output = config->output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001547 static int num_print_interval;
1548
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001549 if (config->interval_clear)
Jiri Olsa9660e082018-06-07 00:15:06 +02001550 puts(CONSOLE_CLEAR);
1551
Jiri Olsafa7070a2018-08-30 08:32:29 +02001552 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001553
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001554 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001555 switch (config->aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001556 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001557 fprintf(output, "# time socket cpus");
1558 if (!metric_only)
1559 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001560 break;
1561 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001562 fprintf(output, "# time core cpus");
1563 if (!metric_only)
1564 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001565 break;
1566 case AGGR_NONE:
Jiri Olsaf5155722018-06-07 00:15:08 +02001567 fprintf(output, "# time CPU ");
Andi Kleen41c8ca22016-05-24 12:52:38 -07001568 if (!metric_only)
1569 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001570 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001571 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001572 fprintf(output, "# time comm-pid");
1573 if (!metric_only)
1574 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001575 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001576 case AGGR_GLOBAL:
1577 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001578 fprintf(output, "# time");
1579 if (!metric_only)
1580 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001581 case AGGR_UNSET:
1582 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001583 }
1584 }
1585
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001586 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001587 print_metric_headers(config, " ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001588 if (++num_print_interval == 25)
1589 num_print_interval = 0;
1590}
1591
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001592static void print_header(struct perf_stat_config *config,
Jiri Olsac512e0e2018-08-30 08:32:33 +02001593 struct target *_target,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001594 int argc, const char **argv)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001595{
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001596 FILE *output = config->output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001597 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001598
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001599 fflush(stdout);
1600
Jiri Olsafa7070a2018-08-30 08:32:29 +02001601 if (!config->csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001602 fprintf(output, "\n");
1603 fprintf(output, " Performance counter stats for ");
Jiri Olsac512e0e2018-08-30 08:32:33 +02001604 if (_target->system_wide)
David Ahern62d3b612013-09-28 14:27:58 -06001605 fprintf(output, "\'system wide");
Jiri Olsac512e0e2018-08-30 08:32:33 +02001606 else if (_target->cpu_list)
1607 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1608 else if (!target__has_task(_target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001609 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1610 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001611 fprintf(output, " %s", argv[i]);
Jiri Olsac512e0e2018-08-30 08:32:33 +02001612 } else if (_target->pid)
1613 fprintf(output, "process id \'%s", _target->pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001614 else
Jiri Olsac512e0e2018-08-30 08:32:33 +02001615 fprintf(output, "thread id \'%s", _target->tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001616
Stephane Eranian4aa90152011-08-15 22:22:33 +02001617 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001618 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001619 fprintf(output, " (%d runs)", run_count);
1620 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001621 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001622}
1623
Jiri Olsabc22de92018-04-23 11:08:20 +02001624static int get_precision(double num)
1625{
1626 if (num > 1)
1627 return 0;
1628
1629 return lround(ceil(-log10(num)));
1630}
1631
Jiri Olsae55c14a2018-04-23 11:08:21 +02001632static void print_table(FILE *output, int precision, double avg)
1633{
1634 char tmp[64];
1635 int idx, indent = 0;
1636
1637 scnprintf(tmp, 64, " %17.*f", precision, avg);
1638 while (tmp[indent] == ' ')
1639 indent++;
1640
1641 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1642
1643 for (idx = 0; idx < run_count; idx++) {
1644 double run = (double) walltime_run[idx] / NSEC_PER_SEC;
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001645 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001646
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001647 fprintf(output, " %17.*f (%+.*f) ",
Jiri Olsae55c14a2018-04-23 11:08:21 +02001648 precision, run, precision, run - avg);
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001649
1650 for (h = 0; h < n; h++)
1651 fprintf(output, "#");
1652
1653 fprintf(output, "\n");
Jiri Olsae55c14a2018-04-23 11:08:21 +02001654 }
1655
1656 fprintf(output, "\n%*s# Final result:\n", indent, "");
1657}
1658
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001659static double timeval2double(struct timeval *t)
1660{
1661 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1662}
1663
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001664static void print_footer(struct perf_stat_config *config)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001665{
Jiri Olsabc22de92018-04-23 11:08:20 +02001666 double avg = avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001667 FILE *output = config->output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001668 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001669
Jiri Olsad4f63a42015-06-26 11:29:26 +02001670 if (!null_run)
1671 fprintf(output, "\n");
Jiri Olsabc22de92018-04-23 11:08:20 +02001672
1673 if (run_count == 1) {
1674 fprintf(output, " %17.9f seconds time elapsed", avg);
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001675
1676 if (ru_display) {
1677 double ru_utime = timeval2double(&ru_data.ru_utime);
1678 double ru_stime = timeval2double(&ru_data.ru_stime);
1679
1680 fprintf(output, "\n\n");
1681 fprintf(output, " %17.9f seconds user\n", ru_utime);
1682 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1683 }
Jiri Olsabc22de92018-04-23 11:08:20 +02001684 } else {
1685 double sd = stddev_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
1686 /*
1687 * Display at most 2 more significant
1688 * digits than the stddev inaccuracy.
1689 */
1690 int precision = get_precision(sd) + 2;
1691
Jiri Olsae55c14a2018-04-23 11:08:21 +02001692 if (walltime_run_table)
1693 print_table(output, precision, avg);
1694
Jiri Olsabc22de92018-04-23 11:08:20 +02001695 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1696 precision, avg, precision, sd);
1697
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001698 print_noise_pct(config, sd, avg);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001699 }
1700 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001701
Andi Kleen918c7b062017-05-22 18:00:16 -07001702 if (print_free_counters_hint &&
1703 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1704 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001705 fprintf(output,
1706"Some events weren't counted. Try disabling the NMI watchdog:\n"
1707" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1708" perf stat ...\n"
1709" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001710
1711 if (print_mixed_hw_group_error)
1712 fprintf(output,
1713 "The events in group usually have to be from "
1714 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001715}
1716
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001717static void
1718perf_evlist__print_counters(struct perf_evlist *evlist,
Jiri Olsab64df7f2018-08-30 08:32:26 +02001719 struct perf_stat_config *config,
Jiri Olsac512e0e2018-08-30 08:32:33 +02001720 struct target *_target,
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001721 struct timespec *ts,
1722 int argc, const char **argv)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001723{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001724 bool metric_only = config->metric_only;
Jiri Olsab64df7f2018-08-30 08:32:26 +02001725 int interval = config->interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001726 struct perf_evsel *counter;
1727 char buf[64], *prefix = NULL;
1728
1729 if (interval)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001730 print_interval(config, prefix = buf, ts);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001731 else
Jiri Olsac512e0e2018-08-30 08:32:33 +02001732 print_header(config, _target, argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001733
Andi Kleen54b50912016-03-03 15:57:36 -08001734 if (metric_only) {
1735 static int num_print_iv;
1736
Andi Kleen41c8ca22016-05-24 12:52:38 -07001737 if (num_print_iv == 0 && !interval)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001738 print_metric_headers(config, prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001739 if (num_print_iv++ == 25)
1740 num_print_iv = 0;
Jiri Olsab64df7f2018-08-30 08:32:26 +02001741 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1742 fprintf(config->output, "%s", prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001743 }
1744
Jiri Olsab64df7f2018-08-30 08:32:26 +02001745 switch (config->aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001746 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001747 case AGGR_SOCKET:
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001748 print_aggr(config, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001749 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001750 case AGGR_THREAD:
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001751 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001752 if (is_duration_time(counter))
1753 continue;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001754 print_aggr_thread(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001755 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001756 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001757 case AGGR_GLOBAL:
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_counter_aggr(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001762 }
Andi Kleen54b50912016-03-03 15:57:36 -08001763 if (metric_only)
Jiri Olsab64df7f2018-08-30 08:32:26 +02001764 fputc('\n', config->output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001765 break;
1766 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001767 if (metric_only)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001768 print_no_aggr_metric(config, prefix);
Andi Kleen206cab62016-03-03 15:57:37 -08001769 else {
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001770 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001771 if (is_duration_time(counter))
1772 continue;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001773 print_counter(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001774 }
Andi Kleen206cab62016-03-03 15:57:37 -08001775 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001776 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001777 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001778 default:
1779 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001780 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001781
Jiri Olsafa7070a2018-08-30 08:32:29 +02001782 if (!interval && !config->csv_output)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001783 print_footer(config);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001784
Jiri Olsab64df7f2018-08-30 08:32:26 +02001785 fflush(config->output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001786}
1787
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001788static void print_counters(struct timespec *ts, int argc, const char **argv)
1789{
Jiri Olsa01748202018-08-30 08:32:25 +02001790 /* Do not print anything if we record to the pipe. */
1791 if (STAT_RECORD && perf_stat.data.is_pipe)
1792 return;
1793
Jiri Olsac512e0e2018-08-30 08:32:33 +02001794 perf_evlist__print_counters(evsel_list, &stat_config, &target,
Jiri Olsab64df7f2018-08-30 08:32:26 +02001795 ts, argc, argv);
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001796}
1797
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001798static volatile int signr = -1;
1799
Ingo Molnar52425192009-05-26 09:17:18 +02001800static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001801{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001802 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001803 done = 1;
1804
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001805 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001806 /*
1807 * render child_pid harmless
1808 * won't send SIGTERM to a random
1809 * process in case of race condition
1810 * and fast PID recycling
1811 */
1812 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001813}
1814
1815static void sig_atexit(void)
1816{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001817 sigset_t set, oset;
1818
1819 /*
1820 * avoid race condition with SIGCHLD handler
1821 * in skip_signal() which is modifying child_pid
1822 * goal is to avoid send SIGTERM to a random
1823 * process
1824 */
1825 sigemptyset(&set);
1826 sigaddset(&set, SIGCHLD);
1827 sigprocmask(SIG_BLOCK, &set, &oset);
1828
Chris Wilson933da832009-10-04 01:35:01 +01001829 if (child_pid != -1)
1830 kill(child_pid, SIGTERM);
1831
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001832 sigprocmask(SIG_SETMASK, &oset, NULL);
1833
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001834 if (signr == -1)
1835 return;
1836
1837 signal(signr, SIG_DFL);
1838 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001839}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001840
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001841static int stat__set_big_num(const struct option *opt __maybe_unused,
1842 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001843{
1844 big_num_opt = unset ? 0 : 1;
1845 return 0;
1846}
1847
Andi Kleen44b1e602016-05-30 12:49:42 -03001848static int enable_metric_only(const struct option *opt __maybe_unused,
1849 const char *s __maybe_unused, int unset)
1850{
1851 force_metric_only = true;
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001852 stat_config.metric_only = !unset;
Andi Kleen44b1e602016-05-30 12:49:42 -03001853 return 0;
1854}
1855
Andi Kleenb18f3e32017-08-31 12:40:31 -07001856static int parse_metric_groups(const struct option *opt,
1857 const char *str,
1858 int unset __maybe_unused)
1859{
1860 return metricgroup__parse_groups(opt, str, &metric_events);
1861}
1862
Jiri Olsae0547312015-11-05 15:40:45 +01001863static const struct option stat_options[] = {
1864 OPT_BOOLEAN('T', "transaction", &transaction_run,
1865 "hardware transaction statistics"),
1866 OPT_CALLBACK('e', "event", &evsel_list, "event",
1867 "event selector. use 'perf list' to list available events",
1868 parse_events_option),
1869 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1870 "event filter", parse_filter),
Jiri Olsa5698f262018-08-30 08:32:12 +02001871 OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
Jiri Olsae0547312015-11-05 15:40:45 +01001872 "child tasks do not inherit counters"),
1873 OPT_STRING('p', "pid", &target.pid, "pid",
1874 "stat events on existing process id"),
1875 OPT_STRING('t', "tid", &target.tid, "tid",
1876 "stat events on existing thread id"),
1877 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1878 "system-wide collection from all CPUs"),
1879 OPT_BOOLEAN('g', "group", &group,
1880 "put the counters into a counter group"),
1881 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1882 OPT_INCR('v', "verbose", &verbose,
1883 "be more verbose (show counter open errors, etc)"),
1884 OPT_INTEGER('r', "repeat", &run_count,
1885 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsae55c14a2018-04-23 11:08:21 +02001886 OPT_BOOLEAN(0, "table", &walltime_run_table,
1887 "display details about each run (only with -r option)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001888 OPT_BOOLEAN('n', "null", &null_run,
1889 "null run - dont start any counters"),
1890 OPT_INCR('d', "detailed", &detailed_run,
1891 "detailed run - start a lot of events"),
1892 OPT_BOOLEAN('S', "sync", &sync_run,
1893 "call sync() before starting a run"),
1894 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1895 "print large numbers with thousands\' separators",
1896 stat__set_big_num),
1897 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1898 "list of cpus to monitor in system-wide"),
1899 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1900 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001901 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsafa7070a2018-08-30 08:32:29 +02001902 OPT_STRING('x', "field-separator", &stat_config.csv_sep, "separator",
Jiri Olsae0547312015-11-05 15:40:45 +01001903 "print counts with custom separator"),
1904 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1905 "monitor event in cgroup name only", parse_cgroups),
1906 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1907 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1908 OPT_INTEGER(0, "log-fd", &output_fd,
1909 "log output to fd, instead of stderr"),
1910 OPT_STRING(0, "pre", &pre_cmd, "command",
1911 "command to run prior to the measured command"),
1912 OPT_STRING(0, "post", &post_cmd, "command",
1913 "command to run after to the measured command"),
1914 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03001915 "print counts at regular interval in ms "
1916 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01001917 OPT_INTEGER(0, "interval-count", &stat_config.times,
1918 "print counts for fixed number of times"),
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001919 OPT_BOOLEAN(0, "interval-clear", &stat_config.interval_clear,
Jiri Olsa9660e082018-06-07 00:15:06 +02001920 "clear screen in between new interval"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01001921 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1922 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001923 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1924 "aggregate counts per processor socket", AGGR_SOCKET),
1925 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1926 "aggregate counts per physical processor core", AGGR_CORE),
1927 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1928 "aggregate counts per thread", AGGR_THREAD),
Jiri Olsa728c0ee2018-08-30 08:32:11 +02001929 OPT_UINTEGER('D', "delay", &stat_config.initial_delay,
Jiri Olsae0547312015-11-05 15:40:45 +01001930 "ms to wait before starting measurement after program start"),
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001931 OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL,
Andi Kleen44b1e602016-05-30 12:49:42 -03001932 "Only print computed metrics. No raw values", enable_metric_only),
1933 OPT_BOOLEAN(0, "topdown", &topdown_run,
1934 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001935 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1936 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001937 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1938 "monitor specified metrics or metric groups (separated by ,)",
1939 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01001940 OPT_END()
1941};
1942
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001943static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1944{
1945 return cpu_map__get_socket(map, cpu, NULL);
1946}
1947
1948static int perf_stat__get_core(struct cpu_map *map, int cpu)
1949{
1950 return cpu_map__get_core(map, cpu, NULL);
1951}
1952
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001953static int cpu_map__get_max(struct cpu_map *map)
1954{
1955 int i, max = -1;
1956
1957 for (i = 0; i < map->nr; i++) {
1958 if (map->map[i] > max)
1959 max = map->map[i];
1960 }
1961
1962 return max;
1963}
1964
1965static struct cpu_map *cpus_aggr_map;
1966
1967static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1968{
1969 int cpu;
1970
1971 if (idx >= map->nr)
1972 return -1;
1973
1974 cpu = map->map[idx];
1975
1976 if (cpus_aggr_map->map[cpu] == -1)
1977 cpus_aggr_map->map[cpu] = get_id(map, idx);
1978
1979 return cpus_aggr_map->map[cpu];
1980}
1981
1982static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1983{
1984 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1985}
1986
1987static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1988{
1989 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1990}
1991
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001992static int perf_stat_init_aggr_mode(void)
1993{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001994 int nr;
1995
Jiri Olsa421a50f2015-07-21 14:31:22 +02001996 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001997 case AGGR_SOCKET:
1998 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1999 perror("cannot build socket map");
2000 return -1;
2001 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002002 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002003 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002004 case AGGR_CORE:
2005 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
2006 perror("cannot build core map");
2007 return -1;
2008 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002009 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002010 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002011 case AGGR_NONE:
2012 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002013 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002014 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002015 default:
2016 break;
2017 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002018
2019 /*
2020 * The evsel_list->cpus is the base we operate on,
2021 * taking the highest cpu number to be the size of
2022 * the aggregation translate cpumap.
2023 */
2024 nr = cpu_map__get_max(evsel_list->cpus);
2025 cpus_aggr_map = cpu_map__empty_new(nr + 1);
2026 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002027}
2028
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002029static void perf_stat__exit_aggr_mode(void)
2030{
2031 cpu_map__put(aggr_map);
2032 cpu_map__put(cpus_aggr_map);
2033 aggr_map = NULL;
2034 cpus_aggr_map = NULL;
2035}
2036
Jiri Olsa68d702f2015-11-05 15:40:58 +01002037static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2038{
2039 int cpu;
2040
2041 if (idx > map->nr)
2042 return -1;
2043
2044 cpu = map->map[idx];
2045
Jan Stancekda8a58b2017-02-17 12:10:26 +01002046 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002047 return -1;
2048
2049 return cpu;
2050}
2051
2052static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2053{
2054 struct perf_env *env = data;
2055 int cpu = perf_env__get_cpu(env, map, idx);
2056
2057 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2058}
2059
2060static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2061{
2062 struct perf_env *env = data;
2063 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2064
2065 if (cpu != -1) {
2066 int socket_id = env->cpu[cpu].socket_id;
2067
2068 /*
2069 * Encode socket in upper 16 bits
2070 * core_id is relative to socket, and
2071 * we need a global id. So we combine
2072 * socket + core id.
2073 */
2074 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2075 }
2076
2077 return core;
2078}
2079
2080static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2081 struct cpu_map **sockp)
2082{
2083 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2084}
2085
2086static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2087 struct cpu_map **corep)
2088{
2089 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2090}
2091
2092static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2093{
2094 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2095}
2096
2097static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2098{
2099 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2100}
2101
2102static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2103{
2104 struct perf_env *env = &st->session->header.env;
2105
2106 switch (stat_config.aggr_mode) {
2107 case AGGR_SOCKET:
2108 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2109 perror("cannot build socket map");
2110 return -1;
2111 }
2112 aggr_get_id = perf_stat__get_socket_file;
2113 break;
2114 case AGGR_CORE:
2115 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2116 perror("cannot build core map");
2117 return -1;
2118 }
2119 aggr_get_id = perf_stat__get_core_file;
2120 break;
2121 case AGGR_NONE:
2122 case AGGR_GLOBAL:
2123 case AGGR_THREAD:
2124 case AGGR_UNSET:
2125 default:
2126 break;
2127 }
2128
2129 return 0;
2130}
2131
Andi Kleen44b1e602016-05-30 12:49:42 -03002132static int topdown_filter_events(const char **attr, char **str, bool use_group)
2133{
2134 int off = 0;
2135 int i;
2136 int len = 0;
2137 char *s;
2138
2139 for (i = 0; attr[i]; i++) {
2140 if (pmu_have_event("cpu", attr[i])) {
2141 len += strlen(attr[i]) + 1;
2142 attr[i - off] = attr[i];
2143 } else
2144 off++;
2145 }
2146 attr[i - off] = NULL;
2147
2148 *str = malloc(len + 1 + 2);
2149 if (!*str)
2150 return -1;
2151 s = *str;
2152 if (i - off == 0) {
2153 *s = 0;
2154 return 0;
2155 }
2156 if (use_group)
2157 *s++ = '{';
2158 for (i = 0; attr[i]; i++) {
2159 strcpy(s, attr[i]);
2160 s += strlen(s);
2161 *s++ = ',';
2162 }
2163 if (use_group) {
2164 s[-1] = '}';
2165 *s = 0;
2166 } else
2167 s[-1] = 0;
2168 return 0;
2169}
2170
2171__weak bool arch_topdown_check_group(bool *warn)
2172{
2173 *warn = false;
2174 return false;
2175}
2176
2177__weak void arch_topdown_group_warn(void)
2178{
2179}
2180
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002181/*
2182 * Add default attributes, if there were no attributes specified or
2183 * if -d/--detailed, -d -d or -d -d -d is used:
2184 */
2185static int add_default_attributes(void)
2186{
Andi Kleen44b1e602016-05-30 12:49:42 -03002187 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002188 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002189
2190 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2191 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2192 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2193 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2194
2195 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002196};
2197 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002198 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002199};
2200 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002201 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002202};
2203 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002204 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2205 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2206 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2207
2208};
2209
2210/*
2211 * Detailed stats (-d), covering the L1 and last level data caches:
2212 */
2213 struct perf_event_attr detailed_attrs[] = {
2214
2215 { .type = PERF_TYPE_HW_CACHE,
2216 .config =
2217 PERF_COUNT_HW_CACHE_L1D << 0 |
2218 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2219 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2220
2221 { .type = PERF_TYPE_HW_CACHE,
2222 .config =
2223 PERF_COUNT_HW_CACHE_L1D << 0 |
2224 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2225 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2226
2227 { .type = PERF_TYPE_HW_CACHE,
2228 .config =
2229 PERF_COUNT_HW_CACHE_LL << 0 |
2230 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2231 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2232
2233 { .type = PERF_TYPE_HW_CACHE,
2234 .config =
2235 PERF_COUNT_HW_CACHE_LL << 0 |
2236 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2237 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2238};
2239
2240/*
2241 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2242 */
2243 struct perf_event_attr very_detailed_attrs[] = {
2244
2245 { .type = PERF_TYPE_HW_CACHE,
2246 .config =
2247 PERF_COUNT_HW_CACHE_L1I << 0 |
2248 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2249 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2250
2251 { .type = PERF_TYPE_HW_CACHE,
2252 .config =
2253 PERF_COUNT_HW_CACHE_L1I << 0 |
2254 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2255 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2256
2257 { .type = PERF_TYPE_HW_CACHE,
2258 .config =
2259 PERF_COUNT_HW_CACHE_DTLB << 0 |
2260 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2261 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2262
2263 { .type = PERF_TYPE_HW_CACHE,
2264 .config =
2265 PERF_COUNT_HW_CACHE_DTLB << 0 |
2266 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2267 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2268
2269 { .type = PERF_TYPE_HW_CACHE,
2270 .config =
2271 PERF_COUNT_HW_CACHE_ITLB << 0 |
2272 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2273 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2274
2275 { .type = PERF_TYPE_HW_CACHE,
2276 .config =
2277 PERF_COUNT_HW_CACHE_ITLB << 0 |
2278 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2279 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2280
2281};
2282
2283/*
2284 * Very, very detailed stats (-d -d -d), adding prefetch events:
2285 */
2286 struct perf_event_attr very_very_detailed_attrs[] = {
2287
2288 { .type = PERF_TYPE_HW_CACHE,
2289 .config =
2290 PERF_COUNT_HW_CACHE_L1D << 0 |
2291 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2292 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2293
2294 { .type = PERF_TYPE_HW_CACHE,
2295 .config =
2296 PERF_COUNT_HW_CACHE_L1D << 0 |
2297 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2298 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2299};
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002300 struct parse_events_error errinfo;
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002301
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002302 /* Set attrs if no event is selected and !null_run: */
2303 if (null_run)
2304 return 0;
2305
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002306 if (transaction_run) {
Thomas Richter742d92f2018-06-26 09:17:01 +02002307 /* Handle -T as -M transaction. Once platform specific metrics
2308 * support has been added to the json files, all archictures
2309 * will use this approach. To determine transaction support
2310 * on an architecture test for such a metric name.
2311 */
2312 if (metricgroup__has_metric("transaction")) {
2313 struct option opt = { .value = &evsel_list };
2314
2315 return metricgroup__parse_groups(&opt, "transaction",
2316 &metric_events);
2317 }
2318
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002319 if (pmu_have_event("cpu", "cycles-ct") &&
2320 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002321 err = parse_events(evsel_list, transaction_attrs,
2322 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002323 else
Thomas Richterfca323402018-03-08 15:57:35 +01002324 err = parse_events(evsel_list,
2325 transaction_limited_attrs,
2326 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002327 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002328 fprintf(stderr, "Cannot set up transaction events\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002329 parse_events_print_error(&errinfo, transaction_attrs);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002330 return -1;
2331 }
2332 return 0;
2333 }
2334
Kan Liangdaefd0b2017-05-26 12:05:38 -07002335 if (smi_cost) {
2336 int smi;
2337
2338 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2339 fprintf(stderr, "freeze_on_smi is not supported.\n");
2340 return -1;
2341 }
2342
2343 if (!smi) {
2344 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2345 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2346 return -1;
2347 }
2348 smi_reset = true;
2349 }
2350
2351 if (pmu_have_event("msr", "aperf") &&
2352 pmu_have_event("msr", "smi")) {
2353 if (!force_metric_only)
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002354 stat_config.metric_only = true;
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002355 err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002356 } else {
2357 fprintf(stderr, "To measure SMI cost, it needs "
2358 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002359 parse_events_print_error(&errinfo, smi_cost_attrs);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002360 return -1;
2361 }
2362 if (err) {
2363 fprintf(stderr, "Cannot set up SMI cost events\n");
2364 return -1;
2365 }
2366 return 0;
2367 }
2368
Andi Kleen44b1e602016-05-30 12:49:42 -03002369 if (topdown_run) {
2370 char *str = NULL;
2371 bool warn = false;
2372
2373 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2374 stat_config.aggr_mode != AGGR_CORE) {
2375 pr_err("top down event configuration requires --per-core mode\n");
2376 return -1;
2377 }
2378 stat_config.aggr_mode = AGGR_CORE;
2379 if (nr_cgroups || !target__has_cpu(&target)) {
2380 pr_err("top down event configuration requires system-wide mode (-a)\n");
2381 return -1;
2382 }
2383
2384 if (!force_metric_only)
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002385 stat_config.metric_only = true;
Andi Kleen44b1e602016-05-30 12:49:42 -03002386 if (topdown_filter_events(topdown_attrs, &str,
2387 arch_topdown_check_group(&warn)) < 0) {
2388 pr_err("Out of memory\n");
2389 return -1;
2390 }
2391 if (topdown_attrs[0] && str) {
2392 if (warn)
2393 arch_topdown_group_warn();
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002394 err = parse_events(evsel_list, str, &errinfo);
Andi Kleen44b1e602016-05-30 12:49:42 -03002395 if (err) {
2396 fprintf(stderr,
2397 "Cannot set up top down events %s: %d\n",
2398 str, err);
2399 free(str);
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002400 parse_events_print_error(&errinfo, str);
Andi Kleen44b1e602016-05-30 12:49:42 -03002401 return -1;
2402 }
2403 } else {
2404 fprintf(stderr, "System does not support topdown\n");
2405 return -1;
2406 }
2407 free(str);
2408 }
2409
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002410 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002411 if (target__has_cpu(&target))
2412 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2413
Andi Kleen9dec4472016-02-26 16:27:56 -08002414 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2415 return -1;
2416 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2417 if (perf_evlist__add_default_attrs(evsel_list,
2418 frontend_attrs) < 0)
2419 return -1;
2420 }
2421 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2422 if (perf_evlist__add_default_attrs(evsel_list,
2423 backend_attrs) < 0)
2424 return -1;
2425 }
2426 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002427 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002428 }
2429
2430 /* Detailed events get appended to the event list: */
2431
2432 if (detailed_run < 1)
2433 return 0;
2434
2435 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002436 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002437 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002438
2439 if (detailed_run < 2)
2440 return 0;
2441
2442 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002443 if (perf_evlist__add_default_attrs(evsel_list, very_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 < 3)
2447 return 0;
2448
2449 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002450 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002451}
2452
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002453static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002454 "perf stat record [<options>]",
2455 NULL,
2456};
2457
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002458static void init_features(struct perf_session *session)
2459{
2460 int feat;
2461
2462 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2463 perf_header__set_feat(&session->header, feat);
2464
2465 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2466 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2467 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2468 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2469}
2470
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002471static int __cmd_record(int argc, const char **argv)
2472{
2473 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002474 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002475
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002476 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002477 PARSE_OPT_STOP_AT_NON_OPTION);
2478
2479 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002480 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002481
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002482 if (run_count != 1 || forever) {
2483 pr_err("Cannot use -r option with perf stat record.\n");
2484 return -1;
2485 }
2486
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002487 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002488 if (session == NULL) {
2489 pr_err("Perf session creation failed.\n");
2490 return -1;
2491 }
2492
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002493 init_features(session);
2494
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002495 session->evlist = evsel_list;
2496 perf_stat.session = session;
2497 perf_stat.record = true;
2498 return argc;
2499}
2500
Jiri Olsaa56f9392015-11-05 15:40:59 +01002501static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2502 union perf_event *event,
2503 struct perf_session *session)
2504{
Andi Kleene3b03b62016-05-05 16:04:03 -07002505 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002506 struct perf_evsel *counter;
2507 struct timespec tsh, *ts = NULL;
2508 const char **argv = session->header.env.cmdline_argv;
2509 int argc = session->header.env.nr_cmdline;
2510
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002511 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002512 perf_stat_process_counter(&stat_config, counter);
2513
Andi Kleene3b03b62016-05-05 16:04:03 -07002514 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2515 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002516
Andi Kleene3b03b62016-05-05 16:04:03 -07002517 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002518 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2519 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002520 ts = &tsh;
2521 }
2522
2523 print_counters(ts, argc, argv);
2524 return 0;
2525}
2526
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002527static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002528int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002529 union perf_event *event,
2530 struct perf_session *session __maybe_unused)
2531{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002532 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2533
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002534 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002535
Jiri Olsa89af4e02015-11-05 15:41:02 +01002536 if (cpu_map__empty(st->cpus)) {
2537 if (st->aggr_mode != AGGR_UNSET)
2538 pr_warning("warning: processing task data, aggregation mode not set\n");
2539 return 0;
2540 }
2541
2542 if (st->aggr_mode != AGGR_UNSET)
2543 stat_config.aggr_mode = st->aggr_mode;
2544
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002545 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002546 perf_stat_init_aggr_mode();
2547 else
2548 perf_stat_init_aggr_mode_file(st);
2549
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002550 return 0;
2551}
2552
Jiri Olsa1975d362015-11-05 15:40:56 +01002553static int set_maps(struct perf_stat *st)
2554{
2555 if (!st->cpus || !st->threads)
2556 return 0;
2557
2558 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2559 return -EINVAL;
2560
2561 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2562
2563 if (perf_evlist__alloc_stats(evsel_list, true))
2564 return -ENOMEM;
2565
2566 st->maps_allocated = true;
2567 return 0;
2568}
2569
2570static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002571int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002572 union perf_event *event,
2573 struct perf_session *session __maybe_unused)
2574{
2575 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2576
2577 if (st->threads) {
2578 pr_warning("Extra thread map event, ignoring.\n");
2579 return 0;
2580 }
2581
2582 st->threads = thread_map__new_event(&event->thread_map);
2583 if (!st->threads)
2584 return -ENOMEM;
2585
2586 return set_maps(st);
2587}
2588
2589static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002590int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002591 union perf_event *event,
2592 struct perf_session *session __maybe_unused)
2593{
2594 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2595 struct cpu_map *cpus;
2596
2597 if (st->cpus) {
2598 pr_warning("Extra cpu map event, ignoring.\n");
2599 return 0;
2600 }
2601
2602 cpus = cpu_map__new_data(&event->cpu_map.data);
2603 if (!cpus)
2604 return -ENOMEM;
2605
2606 st->cpus = cpus;
2607 return set_maps(st);
2608}
2609
Jin Yao56739442017-12-05 22:03:07 +08002610static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2611{
2612 int i;
2613
2614 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2615 if (!config->stats)
2616 return -1;
2617
2618 config->stats_num = nthreads;
2619
2620 for (i = 0; i < nthreads; i++)
2621 runtime_stat__init(&config->stats[i]);
2622
2623 return 0;
2624}
2625
2626static void runtime_stat_delete(struct perf_stat_config *config)
2627{
2628 int i;
2629
2630 if (!config->stats)
2631 return;
2632
2633 for (i = 0; i < config->stats_num; i++)
2634 runtime_stat__exit(&config->stats[i]);
2635
2636 free(config->stats);
2637}
2638
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002639static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002640 "perf stat report [<options>]",
2641 NULL,
2642};
2643
2644static struct perf_stat perf_stat = {
2645 .tool = {
2646 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002647 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002648 .thread_map = process_thread_map_event,
2649 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002650 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002651 .stat = perf_event__process_stat_event,
2652 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002653 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002654 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002655};
2656
2657static int __cmd_report(int argc, const char **argv)
2658{
2659 struct perf_session *session;
2660 const struct option options[] = {
2661 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002662 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2663 "aggregate counts per processor socket", AGGR_SOCKET),
2664 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2665 "aggregate counts per physical processor core", AGGR_CORE),
2666 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2667 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002668 OPT_END()
2669 };
2670 struct stat st;
2671 int ret;
2672
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002673 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002674
2675 if (!input_name || !strlen(input_name)) {
2676 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2677 input_name = "-";
2678 else
2679 input_name = "perf.data";
2680 }
2681
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002682 perf_stat.data.file.path = input_name;
2683 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002684
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002685 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002686 if (session == NULL)
2687 return -1;
2688
2689 perf_stat.session = session;
2690 stat_config.output = stderr;
2691 evsel_list = session->evlist;
2692
2693 ret = perf_session__process_events(session);
2694 if (ret)
2695 return ret;
2696
2697 perf_session__delete(session);
2698 return 0;
2699}
2700
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002701static void setup_system_wide(int forks)
2702{
2703 /*
2704 * Make system wide (-a) the default target if
2705 * no target was specified and one of following
2706 * conditions is met:
2707 *
2708 * - there's no workload specified
2709 * - there is workload specified but all requested
2710 * events are system wide events
2711 */
2712 if (!target__none(&target))
2713 return;
2714
2715 if (!forks)
2716 target.system_wide = true;
2717 else {
2718 struct perf_evsel *counter;
2719
2720 evlist__for_each_entry(evsel_list, counter) {
2721 if (!counter->system_wide)
2722 return;
2723 }
2724
2725 if (evsel_list->nr_entries)
2726 target.system_wide = true;
2727 }
2728}
2729
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002730int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002731{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002732 const char * const stat_usage[] = {
2733 "perf stat [<options>] [<command>]",
2734 NULL
2735 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002736 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002737 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002738 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002739 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002740 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002741
Stephane Eranian5af52b52010-05-18 15:00:01 +02002742 setlocale(LC_ALL, "");
2743
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002744 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002745 if (evsel_list == NULL)
2746 return -ENOMEM;
2747
Wang Nan1669e502016-02-19 11:43:58 +00002748 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002749 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2750 (const char **) stat_usage,
2751 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002752 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002753 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002754
Jiri Olsafa7070a2018-08-30 08:32:29 +02002755 if (stat_config.csv_sep) {
2756 stat_config.csv_output = true;
2757 if (!strcmp(stat_config.csv_sep, "\\t"))
2758 stat_config.csv_sep = "\t";
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002759 } else
Jiri Olsafa7070a2018-08-30 08:32:29 +02002760 stat_config.csv_sep = DEFAULT_SEPARATOR;
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002761
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002762 if (argc && !strncmp(argv[0], "rec", 3)) {
2763 argc = __cmd_record(argc, argv);
2764 if (argc < 0)
2765 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002766 } else if (argc && !strncmp(argv[0], "rep", 3))
2767 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002768
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002769 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002770 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002771
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002772 /*
2773 * For record command the -o is already taken care of.
2774 */
2775 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002776 output = NULL;
2777
Jim Cromie56f3bae2011-09-07 17:14:00 -06002778 if (output_name && output_fd) {
2779 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002780 parse_options_usage(stat_usage, stat_options, "o", 1);
2781 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002782 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002783 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002784
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002785 if (stat_config.metric_only && stat_config.aggr_mode == AGGR_THREAD) {
Andi Kleen54b50912016-03-03 15:57:36 -08002786 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2787 goto out;
2788 }
2789
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002790 if (stat_config.metric_only && run_count > 1) {
Andi Kleen54b50912016-03-03 15:57:36 -08002791 fprintf(stderr, "--metric-only is not supported with -r\n");
2792 goto out;
2793 }
2794
Jiri Olsae55c14a2018-04-23 11:08:21 +02002795 if (walltime_run_table && run_count <= 1) {
2796 fprintf(stderr, "--table is only supported with -r\n");
2797 parse_options_usage(stat_usage, stat_options, "r", 1);
2798 parse_options_usage(NULL, stat_options, "table", 0);
2799 goto out;
2800 }
2801
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002802 if (output_fd < 0) {
2803 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002804 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002805 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002806 }
2807
Stephane Eranian4aa90152011-08-15 22:22:33 +02002808 if (!output) {
2809 struct timespec tm;
2810 mode = append_file ? "a" : "w";
2811
2812 output = fopen(output_name, mode);
2813 if (!output) {
2814 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002815 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002816 }
2817 clock_gettime(CLOCK_REALTIME, &tm);
2818 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002819 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002820 mode = append_file ? "a" : "w";
2821 output = fdopen(output_fd, mode);
2822 if (!output) {
2823 perror("Failed opening logfd");
2824 return -errno;
2825 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002826 }
2827
Jiri Olsa58215222015-07-21 14:31:24 +02002828 stat_config.output = output;
2829
Stephane Eraniand7470b62010-12-01 18:49:05 +02002830 /*
2831 * let the spreadsheet do the pretty-printing
2832 */
Jiri Olsafa7070a2018-08-30 08:32:29 +02002833 if (stat_config.csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002834 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002835 if (big_num_opt == 1) {
2836 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002837 parse_options_usage(stat_usage, stat_options, "B", 1);
2838 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002839 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002840 } else /* Nope, so disable big number formatting */
2841 big_num = false;
2842 } else if (big_num_opt == 0) /* User passed --no-big-num */
2843 big_num = false;
2844
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002845 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002846
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002847 /*
2848 * Display user/system times only for single
2849 * run and when there's specified tracee.
2850 */
2851 if ((run_count == 1) && target__none(&target))
2852 ru_display = true;
2853
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002854 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002855 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002856 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002857 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002858 } else if (run_count == 0) {
2859 forever = true;
2860 run_count = 1;
2861 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002862
Jiri Olsae55c14a2018-04-23 11:08:21 +02002863 if (walltime_run_table) {
2864 walltime_run = zalloc(run_count * sizeof(walltime_run[0]));
2865 if (!walltime_run) {
2866 pr_err("failed to setup -r option");
2867 goto out;
2868 }
2869 }
2870
Jin Yao1d9f8d12017-12-05 22:03:10 +08002871 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2872 !target__has_task(&target)) {
2873 if (!target.system_wide || target.cpu_list) {
2874 fprintf(stderr, "The --per-thread option is only "
2875 "available when monitoring via -p -t -a "
2876 "options or only --per-thread.\n");
2877 parse_options_usage(NULL, stat_options, "p", 1);
2878 parse_options_usage(NULL, stat_options, "t", 1);
2879 goto out;
2880 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002881 }
2882
2883 /*
2884 * no_aggr, cgroup are for system-wide only
2885 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2886 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002887 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2888 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002889 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002890 fprintf(stderr, "both cgroup and no-aggregation "
2891 "modes only available in system-wide mode\n");
2892
Jiri Olsae0547312015-11-05 15:40:45 +01002893 parse_options_usage(stat_usage, stat_options, "G", 1);
2894 parse_options_usage(NULL, stat_options, "A", 1);
2895 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002896 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002897 }
2898
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002899 if (add_default_attributes())
2900 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002901
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002902 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002903
Jin Yao1d9f8d12017-12-05 22:03:10 +08002904 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2905 target.per_thread = true;
2906
Namhyung Kim77a6f012012-05-07 14:09:04 +09002907 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002908 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002909 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002910 parse_options_usage(stat_usage, stat_options, "p", 1);
2911 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002912 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002913 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002914 parse_options_usage(stat_usage, stat_options, "C", 1);
2915 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002916 }
2917 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002918 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002919
2920 /*
2921 * Initialize thread_map with comm names,
2922 * so we could print it out on output.
2923 */
Jin Yao56739442017-12-05 22:03:07 +08002924 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002925 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08002926 if (target.system_wide) {
2927 if (runtime_stat_new(&stat_config,
2928 thread_map__nr(evsel_list->threads))) {
2929 goto out;
2930 }
2931 }
2932 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002933
yuzhoujiandb06a262018-01-29 10:25:22 +01002934 if (stat_config.times && interval)
2935 interval_count = true;
2936 else if (stat_config.times && !interval) {
2937 pr_err("interval-count option should be used together with "
2938 "interval-print.\n");
2939 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2940 parse_options_usage(stat_usage, stat_options, "I", 1);
2941 goto out;
2942 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002943
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002944 if (timeout && timeout < 100) {
2945 if (timeout < 10) {
2946 pr_err("timeout must be >= 10ms.\n");
2947 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2948 goto out;
2949 } else
2950 pr_warning("timeout < 100ms. "
2951 "The overhead percentage could be high in some cases. "
2952 "Please proceed with caution.\n");
2953 }
2954 if (timeout && interval) {
2955 pr_err("timeout option is not supported with interval-print.\n");
2956 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2957 parse_options_usage(stat_usage, stat_options, "I", 1);
2958 goto out;
2959 }
2960
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002961 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002962 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002963
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002964 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002965 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002966
Ingo Molnar58d7e992009-05-15 11:03:23 +02002967 /*
Jiri Olsa7d9ad162018-08-30 08:32:14 +02002968 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
2969 * while avoiding that older tools show confusing messages.
2970 *
2971 * However for pipe sessions we need to keep it zero,
2972 * because script's perf_evsel__check_attr is triggered
2973 * by attr->sample_type != 0, and we can't run it on
2974 * stat sessions.
2975 */
2976 stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
2977
2978 /*
Ingo Molnar58d7e992009-05-15 11:03:23 +02002979 * We dont want to block the signals - that would cause
2980 * child tasks to inherit that and Ctrl-C would not work.
2981 * What we want is for Ctrl-C to work in the exec()-ed
2982 * task, but being ignored by perf stat itself:
2983 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002984 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002985 if (!forever)
2986 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002987 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002988 signal(SIGALRM, skip_signal);
2989 signal(SIGABRT, skip_signal);
2990
Ingo Molnar42202dd2009-06-13 14:57:28 +02002991 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002992 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002993 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002994 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2995 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002996
Jiri Olsae55c14a2018-04-23 11:08:21 +02002997 status = run_perf_stat(argc, argv, run_idx);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002998 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002999 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02003000 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003001 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02003002 }
3003
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003004 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02003005 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003006
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003007 if (STAT_RECORD) {
3008 /*
3009 * We synthesize the kernel mmap record just so that older tools
3010 * don't emit warnings about not being able to resolve symbols
3011 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3012 * a saner message about no samples being in the perf.data file.
3013 *
3014 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01003015 * in header.c at the moment 'perf stat record' gets introduced, which
3016 * is not really needed once we start adding the stat specific PERF_RECORD_
3017 * records, but the need to suppress the kptr_restrict messages in older
3018 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003019 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003020 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003021 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3022 process_synthesized_event,
3023 &perf_stat.session->machines.host);
3024 if (err) {
3025 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3026 "older tools may produce warnings about this file\n.");
3027 }
3028
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003029 if (!interval) {
3030 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3031 pr_err("failed to write stat round event\n");
3032 }
3033
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003034 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003035 perf_stat.session->header.data_size += perf_stat.bytes_written;
3036 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3037 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003038
3039 perf_session__delete(perf_stat.session);
3040 }
3041
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003042 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003043 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003044out:
Jiri Olsae55c14a2018-04-23 11:08:21 +02003045 free(walltime_run);
3046
Kan Liangdaefd0b2017-05-26 12:05:38 -07003047 if (smi_cost && smi_reset)
3048 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3049
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003050 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003051
3052 runtime_stat_delete(&stat_config);
3053
Ingo Molnar42202dd2009-06-13 14:57:28 +02003054 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003055}