blob: 24171aa6c41faa2619d4d294ab30611e70788a78 [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,
1593 int argc, const char **argv)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001594{
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001595 FILE *output = config->output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001596 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001597
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001598 fflush(stdout);
1599
Jiri Olsafa7070a2018-08-30 08:32:29 +02001600 if (!config->csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001601 fprintf(output, "\n");
1602 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001603 if (target.system_wide)
1604 fprintf(output, "\'system wide");
1605 else if (target.cpu_list)
1606 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001607 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001608 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1609 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001610 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001611 } else if (target.pid)
1612 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001613 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001614 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001615
Stephane Eranian4aa90152011-08-15 22:22:33 +02001616 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001617 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001618 fprintf(output, " (%d runs)", run_count);
1619 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001620 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001621}
1622
Jiri Olsabc22de92018-04-23 11:08:20 +02001623static int get_precision(double num)
1624{
1625 if (num > 1)
1626 return 0;
1627
1628 return lround(ceil(-log10(num)));
1629}
1630
Jiri Olsae55c14a2018-04-23 11:08:21 +02001631static void print_table(FILE *output, int precision, double avg)
1632{
1633 char tmp[64];
1634 int idx, indent = 0;
1635
1636 scnprintf(tmp, 64, " %17.*f", precision, avg);
1637 while (tmp[indent] == ' ')
1638 indent++;
1639
1640 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1641
1642 for (idx = 0; idx < run_count; idx++) {
1643 double run = (double) walltime_run[idx] / NSEC_PER_SEC;
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001644 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001645
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001646 fprintf(output, " %17.*f (%+.*f) ",
Jiri Olsae55c14a2018-04-23 11:08:21 +02001647 precision, run, precision, run - avg);
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001648
1649 for (h = 0; h < n; h++)
1650 fprintf(output, "#");
1651
1652 fprintf(output, "\n");
Jiri Olsae55c14a2018-04-23 11:08:21 +02001653 }
1654
1655 fprintf(output, "\n%*s# Final result:\n", indent, "");
1656}
1657
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001658static double timeval2double(struct timeval *t)
1659{
1660 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1661}
1662
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001663static void print_footer(struct perf_stat_config *config)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001664{
Jiri Olsabc22de92018-04-23 11:08:20 +02001665 double avg = avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001666 FILE *output = config->output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001667 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001668
Jiri Olsad4f63a42015-06-26 11:29:26 +02001669 if (!null_run)
1670 fprintf(output, "\n");
Jiri Olsabc22de92018-04-23 11:08:20 +02001671
1672 if (run_count == 1) {
1673 fprintf(output, " %17.9f seconds time elapsed", avg);
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001674
1675 if (ru_display) {
1676 double ru_utime = timeval2double(&ru_data.ru_utime);
1677 double ru_stime = timeval2double(&ru_data.ru_stime);
1678
1679 fprintf(output, "\n\n");
1680 fprintf(output, " %17.9f seconds user\n", ru_utime);
1681 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1682 }
Jiri Olsabc22de92018-04-23 11:08:20 +02001683 } else {
1684 double sd = stddev_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
1685 /*
1686 * Display at most 2 more significant
1687 * digits than the stddev inaccuracy.
1688 */
1689 int precision = get_precision(sd) + 2;
1690
Jiri Olsae55c14a2018-04-23 11:08:21 +02001691 if (walltime_run_table)
1692 print_table(output, precision, avg);
1693
Jiri Olsabc22de92018-04-23 11:08:20 +02001694 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1695 precision, avg, precision, sd);
1696
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001697 print_noise_pct(config, sd, avg);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001698 }
1699 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001700
Andi Kleen918c7b062017-05-22 18:00:16 -07001701 if (print_free_counters_hint &&
1702 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1703 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001704 fprintf(output,
1705"Some events weren't counted. Try disabling the NMI watchdog:\n"
1706" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1707" perf stat ...\n"
1708" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001709
1710 if (print_mixed_hw_group_error)
1711 fprintf(output,
1712 "The events in group usually have to be from "
1713 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001714}
1715
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001716static void
1717perf_evlist__print_counters(struct perf_evlist *evlist,
Jiri Olsab64df7f2018-08-30 08:32:26 +02001718 struct perf_stat_config *config,
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001719 struct timespec *ts,
1720 int argc, const char **argv)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001721{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001722 bool metric_only = config->metric_only;
Jiri Olsab64df7f2018-08-30 08:32:26 +02001723 int interval = config->interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001724 struct perf_evsel *counter;
1725 char buf[64], *prefix = NULL;
1726
1727 if (interval)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001728 print_interval(config, prefix = buf, ts);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001729 else
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001730 print_header(config, argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001731
Andi Kleen54b50912016-03-03 15:57:36 -08001732 if (metric_only) {
1733 static int num_print_iv;
1734
Andi Kleen41c8ca22016-05-24 12:52:38 -07001735 if (num_print_iv == 0 && !interval)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001736 print_metric_headers(config, prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001737 if (num_print_iv++ == 25)
1738 num_print_iv = 0;
Jiri Olsab64df7f2018-08-30 08:32:26 +02001739 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1740 fprintf(config->output, "%s", prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001741 }
1742
Jiri Olsab64df7f2018-08-30 08:32:26 +02001743 switch (config->aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001744 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001745 case AGGR_SOCKET:
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001746 print_aggr(config, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001747 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001748 case AGGR_THREAD:
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001749 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001750 if (is_duration_time(counter))
1751 continue;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001752 print_aggr_thread(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001753 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001754 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001755 case AGGR_GLOBAL:
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001756 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001757 if (is_duration_time(counter))
1758 continue;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001759 print_counter_aggr(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001760 }
Andi Kleen54b50912016-03-03 15:57:36 -08001761 if (metric_only)
Jiri Olsab64df7f2018-08-30 08:32:26 +02001762 fputc('\n', config->output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001763 break;
1764 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001765 if (metric_only)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001766 print_no_aggr_metric(config, prefix);
Andi Kleen206cab62016-03-03 15:57:37 -08001767 else {
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001768 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001769 if (is_duration_time(counter))
1770 continue;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001771 print_counter(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001772 }
Andi Kleen206cab62016-03-03 15:57:37 -08001773 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001774 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001775 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001776 default:
1777 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001778 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001779
Jiri Olsafa7070a2018-08-30 08:32:29 +02001780 if (!interval && !config->csv_output)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001781 print_footer(config);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001782
Jiri Olsab64df7f2018-08-30 08:32:26 +02001783 fflush(config->output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001784}
1785
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001786static void print_counters(struct timespec *ts, int argc, const char **argv)
1787{
Jiri Olsa01748202018-08-30 08:32:25 +02001788 /* Do not print anything if we record to the pipe. */
1789 if (STAT_RECORD && perf_stat.data.is_pipe)
1790 return;
1791
Jiri Olsab64df7f2018-08-30 08:32:26 +02001792 perf_evlist__print_counters(evsel_list, &stat_config,
1793 ts, argc, argv);
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001794}
1795
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001796static volatile int signr = -1;
1797
Ingo Molnar52425192009-05-26 09:17:18 +02001798static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001799{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001800 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001801 done = 1;
1802
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001803 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001804 /*
1805 * render child_pid harmless
1806 * won't send SIGTERM to a random
1807 * process in case of race condition
1808 * and fast PID recycling
1809 */
1810 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001811}
1812
1813static void sig_atexit(void)
1814{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001815 sigset_t set, oset;
1816
1817 /*
1818 * avoid race condition with SIGCHLD handler
1819 * in skip_signal() which is modifying child_pid
1820 * goal is to avoid send SIGTERM to a random
1821 * process
1822 */
1823 sigemptyset(&set);
1824 sigaddset(&set, SIGCHLD);
1825 sigprocmask(SIG_BLOCK, &set, &oset);
1826
Chris Wilson933da832009-10-04 01:35:01 +01001827 if (child_pid != -1)
1828 kill(child_pid, SIGTERM);
1829
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001830 sigprocmask(SIG_SETMASK, &oset, NULL);
1831
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001832 if (signr == -1)
1833 return;
1834
1835 signal(signr, SIG_DFL);
1836 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001837}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001838
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001839static int stat__set_big_num(const struct option *opt __maybe_unused,
1840 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001841{
1842 big_num_opt = unset ? 0 : 1;
1843 return 0;
1844}
1845
Andi Kleen44b1e602016-05-30 12:49:42 -03001846static int enable_metric_only(const struct option *opt __maybe_unused,
1847 const char *s __maybe_unused, int unset)
1848{
1849 force_metric_only = true;
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001850 stat_config.metric_only = !unset;
Andi Kleen44b1e602016-05-30 12:49:42 -03001851 return 0;
1852}
1853
Andi Kleenb18f3e32017-08-31 12:40:31 -07001854static int parse_metric_groups(const struct option *opt,
1855 const char *str,
1856 int unset __maybe_unused)
1857{
1858 return metricgroup__parse_groups(opt, str, &metric_events);
1859}
1860
Jiri Olsae0547312015-11-05 15:40:45 +01001861static const struct option stat_options[] = {
1862 OPT_BOOLEAN('T', "transaction", &transaction_run,
1863 "hardware transaction statistics"),
1864 OPT_CALLBACK('e', "event", &evsel_list, "event",
1865 "event selector. use 'perf list' to list available events",
1866 parse_events_option),
1867 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1868 "event filter", parse_filter),
Jiri Olsa5698f262018-08-30 08:32:12 +02001869 OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
Jiri Olsae0547312015-11-05 15:40:45 +01001870 "child tasks do not inherit counters"),
1871 OPT_STRING('p', "pid", &target.pid, "pid",
1872 "stat events on existing process id"),
1873 OPT_STRING('t', "tid", &target.tid, "tid",
1874 "stat events on existing thread id"),
1875 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1876 "system-wide collection from all CPUs"),
1877 OPT_BOOLEAN('g', "group", &group,
1878 "put the counters into a counter group"),
1879 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1880 OPT_INCR('v', "verbose", &verbose,
1881 "be more verbose (show counter open errors, etc)"),
1882 OPT_INTEGER('r', "repeat", &run_count,
1883 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsae55c14a2018-04-23 11:08:21 +02001884 OPT_BOOLEAN(0, "table", &walltime_run_table,
1885 "display details about each run (only with -r option)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001886 OPT_BOOLEAN('n', "null", &null_run,
1887 "null run - dont start any counters"),
1888 OPT_INCR('d', "detailed", &detailed_run,
1889 "detailed run - start a lot of events"),
1890 OPT_BOOLEAN('S', "sync", &sync_run,
1891 "call sync() before starting a run"),
1892 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1893 "print large numbers with thousands\' separators",
1894 stat__set_big_num),
1895 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1896 "list of cpus to monitor in system-wide"),
1897 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1898 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001899 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsafa7070a2018-08-30 08:32:29 +02001900 OPT_STRING('x', "field-separator", &stat_config.csv_sep, "separator",
Jiri Olsae0547312015-11-05 15:40:45 +01001901 "print counts with custom separator"),
1902 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1903 "monitor event in cgroup name only", parse_cgroups),
1904 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1905 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1906 OPT_INTEGER(0, "log-fd", &output_fd,
1907 "log output to fd, instead of stderr"),
1908 OPT_STRING(0, "pre", &pre_cmd, "command",
1909 "command to run prior to the measured command"),
1910 OPT_STRING(0, "post", &post_cmd, "command",
1911 "command to run after to the measured command"),
1912 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03001913 "print counts at regular interval in ms "
1914 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01001915 OPT_INTEGER(0, "interval-count", &stat_config.times,
1916 "print counts for fixed number of times"),
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001917 OPT_BOOLEAN(0, "interval-clear", &stat_config.interval_clear,
Jiri Olsa9660e082018-06-07 00:15:06 +02001918 "clear screen in between new interval"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01001919 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1920 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001921 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1922 "aggregate counts per processor socket", AGGR_SOCKET),
1923 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1924 "aggregate counts per physical processor core", AGGR_CORE),
1925 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1926 "aggregate counts per thread", AGGR_THREAD),
Jiri Olsa728c0ee2018-08-30 08:32:11 +02001927 OPT_UINTEGER('D', "delay", &stat_config.initial_delay,
Jiri Olsae0547312015-11-05 15:40:45 +01001928 "ms to wait before starting measurement after program start"),
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001929 OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL,
Andi Kleen44b1e602016-05-30 12:49:42 -03001930 "Only print computed metrics. No raw values", enable_metric_only),
1931 OPT_BOOLEAN(0, "topdown", &topdown_run,
1932 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001933 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1934 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001935 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1936 "monitor specified metrics or metric groups (separated by ,)",
1937 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01001938 OPT_END()
1939};
1940
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001941static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1942{
1943 return cpu_map__get_socket(map, cpu, NULL);
1944}
1945
1946static int perf_stat__get_core(struct cpu_map *map, int cpu)
1947{
1948 return cpu_map__get_core(map, cpu, NULL);
1949}
1950
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001951static int cpu_map__get_max(struct cpu_map *map)
1952{
1953 int i, max = -1;
1954
1955 for (i = 0; i < map->nr; i++) {
1956 if (map->map[i] > max)
1957 max = map->map[i];
1958 }
1959
1960 return max;
1961}
1962
1963static struct cpu_map *cpus_aggr_map;
1964
1965static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1966{
1967 int cpu;
1968
1969 if (idx >= map->nr)
1970 return -1;
1971
1972 cpu = map->map[idx];
1973
1974 if (cpus_aggr_map->map[cpu] == -1)
1975 cpus_aggr_map->map[cpu] = get_id(map, idx);
1976
1977 return cpus_aggr_map->map[cpu];
1978}
1979
1980static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1981{
1982 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1983}
1984
1985static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1986{
1987 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1988}
1989
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001990static int perf_stat_init_aggr_mode(void)
1991{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001992 int nr;
1993
Jiri Olsa421a50f2015-07-21 14:31:22 +02001994 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001995 case AGGR_SOCKET:
1996 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1997 perror("cannot build socket map");
1998 return -1;
1999 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002000 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002001 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002002 case AGGR_CORE:
2003 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
2004 perror("cannot build core map");
2005 return -1;
2006 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002007 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002008 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002009 case AGGR_NONE:
2010 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002011 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002012 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002013 default:
2014 break;
2015 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002016
2017 /*
2018 * The evsel_list->cpus is the base we operate on,
2019 * taking the highest cpu number to be the size of
2020 * the aggregation translate cpumap.
2021 */
2022 nr = cpu_map__get_max(evsel_list->cpus);
2023 cpus_aggr_map = cpu_map__empty_new(nr + 1);
2024 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002025}
2026
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002027static void perf_stat__exit_aggr_mode(void)
2028{
2029 cpu_map__put(aggr_map);
2030 cpu_map__put(cpus_aggr_map);
2031 aggr_map = NULL;
2032 cpus_aggr_map = NULL;
2033}
2034
Jiri Olsa68d702f2015-11-05 15:40:58 +01002035static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2036{
2037 int cpu;
2038
2039 if (idx > map->nr)
2040 return -1;
2041
2042 cpu = map->map[idx];
2043
Jan Stancekda8a58b2017-02-17 12:10:26 +01002044 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002045 return -1;
2046
2047 return cpu;
2048}
2049
2050static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2051{
2052 struct perf_env *env = data;
2053 int cpu = perf_env__get_cpu(env, map, idx);
2054
2055 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2056}
2057
2058static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2059{
2060 struct perf_env *env = data;
2061 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2062
2063 if (cpu != -1) {
2064 int socket_id = env->cpu[cpu].socket_id;
2065
2066 /*
2067 * Encode socket in upper 16 bits
2068 * core_id is relative to socket, and
2069 * we need a global id. So we combine
2070 * socket + core id.
2071 */
2072 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2073 }
2074
2075 return core;
2076}
2077
2078static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2079 struct cpu_map **sockp)
2080{
2081 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2082}
2083
2084static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2085 struct cpu_map **corep)
2086{
2087 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2088}
2089
2090static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2091{
2092 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2093}
2094
2095static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2096{
2097 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2098}
2099
2100static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2101{
2102 struct perf_env *env = &st->session->header.env;
2103
2104 switch (stat_config.aggr_mode) {
2105 case AGGR_SOCKET:
2106 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2107 perror("cannot build socket map");
2108 return -1;
2109 }
2110 aggr_get_id = perf_stat__get_socket_file;
2111 break;
2112 case AGGR_CORE:
2113 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2114 perror("cannot build core map");
2115 return -1;
2116 }
2117 aggr_get_id = perf_stat__get_core_file;
2118 break;
2119 case AGGR_NONE:
2120 case AGGR_GLOBAL:
2121 case AGGR_THREAD:
2122 case AGGR_UNSET:
2123 default:
2124 break;
2125 }
2126
2127 return 0;
2128}
2129
Andi Kleen44b1e602016-05-30 12:49:42 -03002130static int topdown_filter_events(const char **attr, char **str, bool use_group)
2131{
2132 int off = 0;
2133 int i;
2134 int len = 0;
2135 char *s;
2136
2137 for (i = 0; attr[i]; i++) {
2138 if (pmu_have_event("cpu", attr[i])) {
2139 len += strlen(attr[i]) + 1;
2140 attr[i - off] = attr[i];
2141 } else
2142 off++;
2143 }
2144 attr[i - off] = NULL;
2145
2146 *str = malloc(len + 1 + 2);
2147 if (!*str)
2148 return -1;
2149 s = *str;
2150 if (i - off == 0) {
2151 *s = 0;
2152 return 0;
2153 }
2154 if (use_group)
2155 *s++ = '{';
2156 for (i = 0; attr[i]; i++) {
2157 strcpy(s, attr[i]);
2158 s += strlen(s);
2159 *s++ = ',';
2160 }
2161 if (use_group) {
2162 s[-1] = '}';
2163 *s = 0;
2164 } else
2165 s[-1] = 0;
2166 return 0;
2167}
2168
2169__weak bool arch_topdown_check_group(bool *warn)
2170{
2171 *warn = false;
2172 return false;
2173}
2174
2175__weak void arch_topdown_group_warn(void)
2176{
2177}
2178
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002179/*
2180 * Add default attributes, if there were no attributes specified or
2181 * if -d/--detailed, -d -d or -d -d -d is used:
2182 */
2183static int add_default_attributes(void)
2184{
Andi Kleen44b1e602016-05-30 12:49:42 -03002185 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002186 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002187
2188 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2189 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2190 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2191 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2192
2193 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002194};
2195 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002196 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002197};
2198 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002199 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002200};
2201 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002202 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2203 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2204 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2205
2206};
2207
2208/*
2209 * Detailed stats (-d), covering the L1 and last level data caches:
2210 */
2211 struct perf_event_attr detailed_attrs[] = {
2212
2213 { .type = PERF_TYPE_HW_CACHE,
2214 .config =
2215 PERF_COUNT_HW_CACHE_L1D << 0 |
2216 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2217 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2218
2219 { .type = PERF_TYPE_HW_CACHE,
2220 .config =
2221 PERF_COUNT_HW_CACHE_L1D << 0 |
2222 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2223 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2224
2225 { .type = PERF_TYPE_HW_CACHE,
2226 .config =
2227 PERF_COUNT_HW_CACHE_LL << 0 |
2228 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2229 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2230
2231 { .type = PERF_TYPE_HW_CACHE,
2232 .config =
2233 PERF_COUNT_HW_CACHE_LL << 0 |
2234 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2235 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2236};
2237
2238/*
2239 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2240 */
2241 struct perf_event_attr very_detailed_attrs[] = {
2242
2243 { .type = PERF_TYPE_HW_CACHE,
2244 .config =
2245 PERF_COUNT_HW_CACHE_L1I << 0 |
2246 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2247 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2248
2249 { .type = PERF_TYPE_HW_CACHE,
2250 .config =
2251 PERF_COUNT_HW_CACHE_L1I << 0 |
2252 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2253 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2254
2255 { .type = PERF_TYPE_HW_CACHE,
2256 .config =
2257 PERF_COUNT_HW_CACHE_DTLB << 0 |
2258 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2259 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2260
2261 { .type = PERF_TYPE_HW_CACHE,
2262 .config =
2263 PERF_COUNT_HW_CACHE_DTLB << 0 |
2264 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2265 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2266
2267 { .type = PERF_TYPE_HW_CACHE,
2268 .config =
2269 PERF_COUNT_HW_CACHE_ITLB << 0 |
2270 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2271 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2272
2273 { .type = PERF_TYPE_HW_CACHE,
2274 .config =
2275 PERF_COUNT_HW_CACHE_ITLB << 0 |
2276 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2277 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2278
2279};
2280
2281/*
2282 * Very, very detailed stats (-d -d -d), adding prefetch events:
2283 */
2284 struct perf_event_attr very_very_detailed_attrs[] = {
2285
2286 { .type = PERF_TYPE_HW_CACHE,
2287 .config =
2288 PERF_COUNT_HW_CACHE_L1D << 0 |
2289 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2290 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2291
2292 { .type = PERF_TYPE_HW_CACHE,
2293 .config =
2294 PERF_COUNT_HW_CACHE_L1D << 0 |
2295 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2296 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2297};
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002298 struct parse_events_error errinfo;
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002299
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002300 /* Set attrs if no event is selected and !null_run: */
2301 if (null_run)
2302 return 0;
2303
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002304 if (transaction_run) {
Thomas Richter742d92f2018-06-26 09:17:01 +02002305 /* Handle -T as -M transaction. Once platform specific metrics
2306 * support has been added to the json files, all archictures
2307 * will use this approach. To determine transaction support
2308 * on an architecture test for such a metric name.
2309 */
2310 if (metricgroup__has_metric("transaction")) {
2311 struct option opt = { .value = &evsel_list };
2312
2313 return metricgroup__parse_groups(&opt, "transaction",
2314 &metric_events);
2315 }
2316
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002317 if (pmu_have_event("cpu", "cycles-ct") &&
2318 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002319 err = parse_events(evsel_list, transaction_attrs,
2320 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002321 else
Thomas Richterfca323402018-03-08 15:57:35 +01002322 err = parse_events(evsel_list,
2323 transaction_limited_attrs,
2324 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002325 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002326 fprintf(stderr, "Cannot set up transaction events\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002327 parse_events_print_error(&errinfo, transaction_attrs);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002328 return -1;
2329 }
2330 return 0;
2331 }
2332
Kan Liangdaefd0b2017-05-26 12:05:38 -07002333 if (smi_cost) {
2334 int smi;
2335
2336 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2337 fprintf(stderr, "freeze_on_smi is not supported.\n");
2338 return -1;
2339 }
2340
2341 if (!smi) {
2342 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2343 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2344 return -1;
2345 }
2346 smi_reset = true;
2347 }
2348
2349 if (pmu_have_event("msr", "aperf") &&
2350 pmu_have_event("msr", "smi")) {
2351 if (!force_metric_only)
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002352 stat_config.metric_only = true;
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002353 err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002354 } else {
2355 fprintf(stderr, "To measure SMI cost, it needs "
2356 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002357 parse_events_print_error(&errinfo, smi_cost_attrs);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002358 return -1;
2359 }
2360 if (err) {
2361 fprintf(stderr, "Cannot set up SMI cost events\n");
2362 return -1;
2363 }
2364 return 0;
2365 }
2366
Andi Kleen44b1e602016-05-30 12:49:42 -03002367 if (topdown_run) {
2368 char *str = NULL;
2369 bool warn = false;
2370
2371 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2372 stat_config.aggr_mode != AGGR_CORE) {
2373 pr_err("top down event configuration requires --per-core mode\n");
2374 return -1;
2375 }
2376 stat_config.aggr_mode = AGGR_CORE;
2377 if (nr_cgroups || !target__has_cpu(&target)) {
2378 pr_err("top down event configuration requires system-wide mode (-a)\n");
2379 return -1;
2380 }
2381
2382 if (!force_metric_only)
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002383 stat_config.metric_only = true;
Andi Kleen44b1e602016-05-30 12:49:42 -03002384 if (topdown_filter_events(topdown_attrs, &str,
2385 arch_topdown_check_group(&warn)) < 0) {
2386 pr_err("Out of memory\n");
2387 return -1;
2388 }
2389 if (topdown_attrs[0] && str) {
2390 if (warn)
2391 arch_topdown_group_warn();
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002392 err = parse_events(evsel_list, str, &errinfo);
Andi Kleen44b1e602016-05-30 12:49:42 -03002393 if (err) {
2394 fprintf(stderr,
2395 "Cannot set up top down events %s: %d\n",
2396 str, err);
2397 free(str);
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002398 parse_events_print_error(&errinfo, str);
Andi Kleen44b1e602016-05-30 12:49:42 -03002399 return -1;
2400 }
2401 } else {
2402 fprintf(stderr, "System does not support topdown\n");
2403 return -1;
2404 }
2405 free(str);
2406 }
2407
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002408 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002409 if (target__has_cpu(&target))
2410 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2411
Andi Kleen9dec4472016-02-26 16:27:56 -08002412 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2413 return -1;
2414 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2415 if (perf_evlist__add_default_attrs(evsel_list,
2416 frontend_attrs) < 0)
2417 return -1;
2418 }
2419 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2420 if (perf_evlist__add_default_attrs(evsel_list,
2421 backend_attrs) < 0)
2422 return -1;
2423 }
2424 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002425 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002426 }
2427
2428 /* Detailed events get appended to the event list: */
2429
2430 if (detailed_run < 1)
2431 return 0;
2432
2433 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002434 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002435 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002436
2437 if (detailed_run < 2)
2438 return 0;
2439
2440 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002441 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002442 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002443
2444 if (detailed_run < 3)
2445 return 0;
2446
2447 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002448 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002449}
2450
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002451static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002452 "perf stat record [<options>]",
2453 NULL,
2454};
2455
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002456static void init_features(struct perf_session *session)
2457{
2458 int feat;
2459
2460 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2461 perf_header__set_feat(&session->header, feat);
2462
2463 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2464 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2465 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2466 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2467}
2468
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002469static int __cmd_record(int argc, const char **argv)
2470{
2471 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002472 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002473
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002474 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002475 PARSE_OPT_STOP_AT_NON_OPTION);
2476
2477 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002478 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002479
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002480 if (run_count != 1 || forever) {
2481 pr_err("Cannot use -r option with perf stat record.\n");
2482 return -1;
2483 }
2484
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002485 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002486 if (session == NULL) {
2487 pr_err("Perf session creation failed.\n");
2488 return -1;
2489 }
2490
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002491 init_features(session);
2492
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002493 session->evlist = evsel_list;
2494 perf_stat.session = session;
2495 perf_stat.record = true;
2496 return argc;
2497}
2498
Jiri Olsaa56f9392015-11-05 15:40:59 +01002499static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2500 union perf_event *event,
2501 struct perf_session *session)
2502{
Andi Kleene3b03b62016-05-05 16:04:03 -07002503 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002504 struct perf_evsel *counter;
2505 struct timespec tsh, *ts = NULL;
2506 const char **argv = session->header.env.cmdline_argv;
2507 int argc = session->header.env.nr_cmdline;
2508
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002509 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002510 perf_stat_process_counter(&stat_config, counter);
2511
Andi Kleene3b03b62016-05-05 16:04:03 -07002512 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2513 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002514
Andi Kleene3b03b62016-05-05 16:04:03 -07002515 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002516 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2517 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002518 ts = &tsh;
2519 }
2520
2521 print_counters(ts, argc, argv);
2522 return 0;
2523}
2524
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002525static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002526int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002527 union perf_event *event,
2528 struct perf_session *session __maybe_unused)
2529{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002530 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2531
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002532 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002533
Jiri Olsa89af4e02015-11-05 15:41:02 +01002534 if (cpu_map__empty(st->cpus)) {
2535 if (st->aggr_mode != AGGR_UNSET)
2536 pr_warning("warning: processing task data, aggregation mode not set\n");
2537 return 0;
2538 }
2539
2540 if (st->aggr_mode != AGGR_UNSET)
2541 stat_config.aggr_mode = st->aggr_mode;
2542
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002543 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002544 perf_stat_init_aggr_mode();
2545 else
2546 perf_stat_init_aggr_mode_file(st);
2547
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002548 return 0;
2549}
2550
Jiri Olsa1975d362015-11-05 15:40:56 +01002551static int set_maps(struct perf_stat *st)
2552{
2553 if (!st->cpus || !st->threads)
2554 return 0;
2555
2556 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2557 return -EINVAL;
2558
2559 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2560
2561 if (perf_evlist__alloc_stats(evsel_list, true))
2562 return -ENOMEM;
2563
2564 st->maps_allocated = true;
2565 return 0;
2566}
2567
2568static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002569int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002570 union perf_event *event,
2571 struct perf_session *session __maybe_unused)
2572{
2573 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2574
2575 if (st->threads) {
2576 pr_warning("Extra thread map event, ignoring.\n");
2577 return 0;
2578 }
2579
2580 st->threads = thread_map__new_event(&event->thread_map);
2581 if (!st->threads)
2582 return -ENOMEM;
2583
2584 return set_maps(st);
2585}
2586
2587static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002588int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002589 union perf_event *event,
2590 struct perf_session *session __maybe_unused)
2591{
2592 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2593 struct cpu_map *cpus;
2594
2595 if (st->cpus) {
2596 pr_warning("Extra cpu map event, ignoring.\n");
2597 return 0;
2598 }
2599
2600 cpus = cpu_map__new_data(&event->cpu_map.data);
2601 if (!cpus)
2602 return -ENOMEM;
2603
2604 st->cpus = cpus;
2605 return set_maps(st);
2606}
2607
Jin Yao56739442017-12-05 22:03:07 +08002608static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2609{
2610 int i;
2611
2612 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2613 if (!config->stats)
2614 return -1;
2615
2616 config->stats_num = nthreads;
2617
2618 for (i = 0; i < nthreads; i++)
2619 runtime_stat__init(&config->stats[i]);
2620
2621 return 0;
2622}
2623
2624static void runtime_stat_delete(struct perf_stat_config *config)
2625{
2626 int i;
2627
2628 if (!config->stats)
2629 return;
2630
2631 for (i = 0; i < config->stats_num; i++)
2632 runtime_stat__exit(&config->stats[i]);
2633
2634 free(config->stats);
2635}
2636
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002637static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002638 "perf stat report [<options>]",
2639 NULL,
2640};
2641
2642static struct perf_stat perf_stat = {
2643 .tool = {
2644 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002645 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002646 .thread_map = process_thread_map_event,
2647 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002648 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002649 .stat = perf_event__process_stat_event,
2650 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002651 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002652 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002653};
2654
2655static int __cmd_report(int argc, const char **argv)
2656{
2657 struct perf_session *session;
2658 const struct option options[] = {
2659 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002660 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2661 "aggregate counts per processor socket", AGGR_SOCKET),
2662 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2663 "aggregate counts per physical processor core", AGGR_CORE),
2664 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2665 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002666 OPT_END()
2667 };
2668 struct stat st;
2669 int ret;
2670
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002671 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002672
2673 if (!input_name || !strlen(input_name)) {
2674 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2675 input_name = "-";
2676 else
2677 input_name = "perf.data";
2678 }
2679
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002680 perf_stat.data.file.path = input_name;
2681 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002682
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002683 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002684 if (session == NULL)
2685 return -1;
2686
2687 perf_stat.session = session;
2688 stat_config.output = stderr;
2689 evsel_list = session->evlist;
2690
2691 ret = perf_session__process_events(session);
2692 if (ret)
2693 return ret;
2694
2695 perf_session__delete(session);
2696 return 0;
2697}
2698
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002699static void setup_system_wide(int forks)
2700{
2701 /*
2702 * Make system wide (-a) the default target if
2703 * no target was specified and one of following
2704 * conditions is met:
2705 *
2706 * - there's no workload specified
2707 * - there is workload specified but all requested
2708 * events are system wide events
2709 */
2710 if (!target__none(&target))
2711 return;
2712
2713 if (!forks)
2714 target.system_wide = true;
2715 else {
2716 struct perf_evsel *counter;
2717
2718 evlist__for_each_entry(evsel_list, counter) {
2719 if (!counter->system_wide)
2720 return;
2721 }
2722
2723 if (evsel_list->nr_entries)
2724 target.system_wide = true;
2725 }
2726}
2727
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002728int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002729{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002730 const char * const stat_usage[] = {
2731 "perf stat [<options>] [<command>]",
2732 NULL
2733 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002734 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002735 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002736 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002737 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002738 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002739
Stephane Eranian5af52b52010-05-18 15:00:01 +02002740 setlocale(LC_ALL, "");
2741
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002742 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002743 if (evsel_list == NULL)
2744 return -ENOMEM;
2745
Wang Nan1669e502016-02-19 11:43:58 +00002746 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002747 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2748 (const char **) stat_usage,
2749 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002750 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002751 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002752
Jiri Olsafa7070a2018-08-30 08:32:29 +02002753 if (stat_config.csv_sep) {
2754 stat_config.csv_output = true;
2755 if (!strcmp(stat_config.csv_sep, "\\t"))
2756 stat_config.csv_sep = "\t";
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002757 } else
Jiri Olsafa7070a2018-08-30 08:32:29 +02002758 stat_config.csv_sep = DEFAULT_SEPARATOR;
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002759
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002760 if (argc && !strncmp(argv[0], "rec", 3)) {
2761 argc = __cmd_record(argc, argv);
2762 if (argc < 0)
2763 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002764 } else if (argc && !strncmp(argv[0], "rep", 3))
2765 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002766
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002767 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002768 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002769
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002770 /*
2771 * For record command the -o is already taken care of.
2772 */
2773 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002774 output = NULL;
2775
Jim Cromie56f3bae2011-09-07 17:14:00 -06002776 if (output_name && output_fd) {
2777 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002778 parse_options_usage(stat_usage, stat_options, "o", 1);
2779 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002780 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002781 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002782
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002783 if (stat_config.metric_only && stat_config.aggr_mode == AGGR_THREAD) {
Andi Kleen54b50912016-03-03 15:57:36 -08002784 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2785 goto out;
2786 }
2787
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002788 if (stat_config.metric_only && run_count > 1) {
Andi Kleen54b50912016-03-03 15:57:36 -08002789 fprintf(stderr, "--metric-only is not supported with -r\n");
2790 goto out;
2791 }
2792
Jiri Olsae55c14a2018-04-23 11:08:21 +02002793 if (walltime_run_table && run_count <= 1) {
2794 fprintf(stderr, "--table is only supported with -r\n");
2795 parse_options_usage(stat_usage, stat_options, "r", 1);
2796 parse_options_usage(NULL, stat_options, "table", 0);
2797 goto out;
2798 }
2799
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002800 if (output_fd < 0) {
2801 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002802 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002803 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002804 }
2805
Stephane Eranian4aa90152011-08-15 22:22:33 +02002806 if (!output) {
2807 struct timespec tm;
2808 mode = append_file ? "a" : "w";
2809
2810 output = fopen(output_name, mode);
2811 if (!output) {
2812 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002813 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002814 }
2815 clock_gettime(CLOCK_REALTIME, &tm);
2816 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002817 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002818 mode = append_file ? "a" : "w";
2819 output = fdopen(output_fd, mode);
2820 if (!output) {
2821 perror("Failed opening logfd");
2822 return -errno;
2823 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002824 }
2825
Jiri Olsa58215222015-07-21 14:31:24 +02002826 stat_config.output = output;
2827
Stephane Eraniand7470b62010-12-01 18:49:05 +02002828 /*
2829 * let the spreadsheet do the pretty-printing
2830 */
Jiri Olsafa7070a2018-08-30 08:32:29 +02002831 if (stat_config.csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002832 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002833 if (big_num_opt == 1) {
2834 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002835 parse_options_usage(stat_usage, stat_options, "B", 1);
2836 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002837 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002838 } else /* Nope, so disable big number formatting */
2839 big_num = false;
2840 } else if (big_num_opt == 0) /* User passed --no-big-num */
2841 big_num = false;
2842
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002843 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002844
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002845 /*
2846 * Display user/system times only for single
2847 * run and when there's specified tracee.
2848 */
2849 if ((run_count == 1) && target__none(&target))
2850 ru_display = true;
2851
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002852 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002853 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002854 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002855 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002856 } else if (run_count == 0) {
2857 forever = true;
2858 run_count = 1;
2859 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002860
Jiri Olsae55c14a2018-04-23 11:08:21 +02002861 if (walltime_run_table) {
2862 walltime_run = zalloc(run_count * sizeof(walltime_run[0]));
2863 if (!walltime_run) {
2864 pr_err("failed to setup -r option");
2865 goto out;
2866 }
2867 }
2868
Jin Yao1d9f8d12017-12-05 22:03:10 +08002869 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2870 !target__has_task(&target)) {
2871 if (!target.system_wide || target.cpu_list) {
2872 fprintf(stderr, "The --per-thread option is only "
2873 "available when monitoring via -p -t -a "
2874 "options or only --per-thread.\n");
2875 parse_options_usage(NULL, stat_options, "p", 1);
2876 parse_options_usage(NULL, stat_options, "t", 1);
2877 goto out;
2878 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002879 }
2880
2881 /*
2882 * no_aggr, cgroup are for system-wide only
2883 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2884 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002885 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2886 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002887 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002888 fprintf(stderr, "both cgroup and no-aggregation "
2889 "modes only available in system-wide mode\n");
2890
Jiri Olsae0547312015-11-05 15:40:45 +01002891 parse_options_usage(stat_usage, stat_options, "G", 1);
2892 parse_options_usage(NULL, stat_options, "A", 1);
2893 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002894 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002895 }
2896
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002897 if (add_default_attributes())
2898 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002899
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002900 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002901
Jin Yao1d9f8d12017-12-05 22:03:10 +08002902 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2903 target.per_thread = true;
2904
Namhyung Kim77a6f012012-05-07 14:09:04 +09002905 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002906 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002907 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002908 parse_options_usage(stat_usage, stat_options, "p", 1);
2909 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002910 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002911 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002912 parse_options_usage(stat_usage, stat_options, "C", 1);
2913 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002914 }
2915 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002916 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002917
2918 /*
2919 * Initialize thread_map with comm names,
2920 * so we could print it out on output.
2921 */
Jin Yao56739442017-12-05 22:03:07 +08002922 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002923 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08002924 if (target.system_wide) {
2925 if (runtime_stat_new(&stat_config,
2926 thread_map__nr(evsel_list->threads))) {
2927 goto out;
2928 }
2929 }
2930 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002931
yuzhoujiandb06a262018-01-29 10:25:22 +01002932 if (stat_config.times && interval)
2933 interval_count = true;
2934 else if (stat_config.times && !interval) {
2935 pr_err("interval-count option should be used together with "
2936 "interval-print.\n");
2937 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2938 parse_options_usage(stat_usage, stat_options, "I", 1);
2939 goto out;
2940 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002941
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002942 if (timeout && timeout < 100) {
2943 if (timeout < 10) {
2944 pr_err("timeout must be >= 10ms.\n");
2945 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2946 goto out;
2947 } else
2948 pr_warning("timeout < 100ms. "
2949 "The overhead percentage could be high in some cases. "
2950 "Please proceed with caution.\n");
2951 }
2952 if (timeout && interval) {
2953 pr_err("timeout option is not supported with interval-print.\n");
2954 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2955 parse_options_usage(stat_usage, stat_options, "I", 1);
2956 goto out;
2957 }
2958
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002959 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002960 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002961
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002962 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002963 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002964
Ingo Molnar58d7e992009-05-15 11:03:23 +02002965 /*
Jiri Olsa7d9ad162018-08-30 08:32:14 +02002966 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
2967 * while avoiding that older tools show confusing messages.
2968 *
2969 * However for pipe sessions we need to keep it zero,
2970 * because script's perf_evsel__check_attr is triggered
2971 * by attr->sample_type != 0, and we can't run it on
2972 * stat sessions.
2973 */
2974 stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
2975
2976 /*
Ingo Molnar58d7e992009-05-15 11:03:23 +02002977 * We dont want to block the signals - that would cause
2978 * child tasks to inherit that and Ctrl-C would not work.
2979 * What we want is for Ctrl-C to work in the exec()-ed
2980 * task, but being ignored by perf stat itself:
2981 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002982 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002983 if (!forever)
2984 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002985 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002986 signal(SIGALRM, skip_signal);
2987 signal(SIGABRT, skip_signal);
2988
Ingo Molnar42202dd2009-06-13 14:57:28 +02002989 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002990 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002991 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002992 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2993 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002994
Jiri Olsae55c14a2018-04-23 11:08:21 +02002995 status = run_perf_stat(argc, argv, run_idx);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002996 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002997 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002998 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002999 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02003000 }
3001
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003002 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02003003 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003004
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003005 if (STAT_RECORD) {
3006 /*
3007 * We synthesize the kernel mmap record just so that older tools
3008 * don't emit warnings about not being able to resolve symbols
3009 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3010 * a saner message about no samples being in the perf.data file.
3011 *
3012 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01003013 * in header.c at the moment 'perf stat record' gets introduced, which
3014 * is not really needed once we start adding the stat specific PERF_RECORD_
3015 * records, but the need to suppress the kptr_restrict messages in older
3016 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003017 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003018 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003019 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3020 process_synthesized_event,
3021 &perf_stat.session->machines.host);
3022 if (err) {
3023 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3024 "older tools may produce warnings about this file\n.");
3025 }
3026
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003027 if (!interval) {
3028 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3029 pr_err("failed to write stat round event\n");
3030 }
3031
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003032 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003033 perf_stat.session->header.data_size += perf_stat.bytes_written;
3034 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3035 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003036
3037 perf_session__delete(perf_stat.session);
3038 }
3039
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003040 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003041 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003042out:
Jiri Olsae55c14a2018-04-23 11:08:21 +02003043 free(walltime_run);
3044
Kan Liangdaefd0b2017-05-26 12:05:38 -07003045 if (smi_cost && smi_reset)
3046 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3047
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003048 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003049
3050 runtime_stat_delete(&stat_config);
3051
Ingo Molnar42202dd2009-06-13 14:57:28 +02003052 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003053}