blob: d0d19a5ffa854d53592d5b23fa2b9aa1adfecb93 [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;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200160static const char *csv_sep = NULL;
161static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800162static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200163static const char *pre_cmd = NULL;
164static const char *post_cmd = NULL;
165static bool sync_run = false;
Stephane Eranian410136f2013-11-12 17:58:49 +0100166static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500167static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800168static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300169static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700170static bool no_merge = false;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200171static bool walltime_run_table = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100172static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100173static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100174static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100175static bool append_file;
yuzhoujiandb06a262018-01-29 10:25:22 +0100176static bool interval_count;
Jiri Olsa9660e082018-06-07 00:15:06 +0200177static bool interval_clear;
Jiri Olsae0547312015-11-05 15:40:45 +0100178static const char *output_name;
179static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100180static int print_free_counters_hint;
Kan Liang30060ea2018-04-24 11:20:11 -0700181static int print_mixed_hw_group_error;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200182static u64 *walltime_run;
Jiri Olsa0ce2da12018-06-05 14:13:13 +0200183static bool ru_display = false;
184static struct rusage ru_data;
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200185static unsigned int metric_only_len = METRIC_ONLY_LEN;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200186
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100187struct perf_stat {
188 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100189 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100190 struct perf_session *session;
191 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100192 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100193 bool maps_allocated;
194 struct cpu_map *cpus;
195 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100196 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100197};
198
199static struct perf_stat perf_stat;
200#define STAT_RECORD perf_stat.record
201
Liming Wang60666c62009-12-31 16:05:50 +0800202static volatile int done = 0;
203
Jiri Olsa421a50f2015-07-21 14:31:22 +0200204static struct perf_stat_config stat_config = {
205 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200206 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200207};
208
Andi Kleene864c5c2017-08-31 12:40:35 -0700209static bool is_duration_time(struct perf_evsel *evsel)
210{
211 return !strcmp(evsel->name, "duration_time");
212}
213
Stephane Eranian13370a92013-01-29 12:47:44 +0100214static inline void diff_timespec(struct timespec *r, struct timespec *a,
215 struct timespec *b)
216{
217 r->tv_sec = a->tv_sec - b->tv_sec;
218 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300219 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100220 r->tv_sec--;
221 } else {
222 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
223 }
224}
225
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200226static void perf_stat__reset_stats(void)
227{
Jin Yao56739442017-12-05 22:03:07 +0800228 int i;
229
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200230 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200231 perf_stat__reset_shadow_stats();
Jin Yao56739442017-12-05 22:03:07 +0800232
233 for (i = 0; i < stat_config.stats_num; i++)
234 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200235}
236
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100237static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100238 union perf_event *event,
239 struct perf_sample *sample __maybe_unused,
240 struct machine *machine __maybe_unused)
241{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100242 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100243 pr_err("failed to write perf data, error: %m\n");
244 return -1;
245 }
246
247 perf_stat.bytes_written += event->header.size;
248 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100249}
250
Jiri Olsa1975d362015-11-05 15:40:56 +0100251static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100252{
Jiri Olsa1975d362015-11-05 15:40:56 +0100253 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100254 process_synthesized_event,
255 NULL);
256}
257
258#define WRITE_STAT_ROUND_EVENT(time, interval) \
259 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
260
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100261#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
262
263static int
264perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
265 struct perf_counts_values *count)
266{
267 struct perf_sample_id *sid = SID(counter, cpu, thread);
268
269 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
270 process_synthesized_event, NULL);
271}
272
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200273/*
274 * Read out the results of a single counter:
275 * do not aggregate counts across CPUs in system-wide mode
276 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200277static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200278{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100279 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100280 int ncpus, cpu, thread;
281
Jin Yao1d9f8d12017-12-05 22:03:10 +0800282 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Mark Rutland00e727b2016-07-15 11:08:10 +0100283 ncpus = perf_evsel__nr_cpus(counter);
284 else
285 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200286
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000287 if (!counter->supported)
288 return -ENOENT;
289
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100290 if (counter->system_wide)
291 nthreads = 1;
292
293 for (thread = 0; thread < nthreads; thread++) {
294 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200295 struct perf_counts_values *count;
296
297 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200298
299 /*
300 * The leader's group read loads data into its group members
301 * (via perf_evsel__read_counter) and sets threir count->loaded.
302 */
303 if (!count->loaded &&
304 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700305 counter->counts->scaled = -1;
306 perf_counts(counter->counts, cpu, thread)->ena = 0;
307 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100308 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700309 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100310
Jiri Olsa82bf3112017-07-26 14:02:06 +0200311 count->loaded = false;
312
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100313 if (STAT_RECORD) {
314 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
315 pr_err("failed to write stat event\n");
316 return -1;
317 }
318 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700319
320 if (verbose > 1) {
321 fprintf(stat_config.output,
322 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
323 perf_evsel__name(counter),
324 cpu,
325 count->val, count->ena, count->run);
326 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100327 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200328 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200329
330 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200331}
332
Mark Rutland3df33ef2016-08-09 14:04:29 +0100333static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200334{
335 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700336 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200337
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300338 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700339 ret = read_counter(counter);
340 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700341 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200342
Stephane Eraniandb49a712017-04-12 11:23:01 -0700343 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200344 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200345 }
346}
347
Jiri Olsaba411a92015-06-26 11:29:24 +0200348static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100349{
Stephane Eranian13370a92013-01-29 12:47:44 +0100350 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100351
Mark Rutland3df33ef2016-08-09 14:04:29 +0100352 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100353
Stephane Eranian13370a92013-01-29 12:47:44 +0100354 clock_gettime(CLOCK_MONOTONIC, &ts);
355 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100356
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100357 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300358 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100359 pr_err("failed to write stat round event\n");
360 }
361
Andi Kleenb90f1332017-08-31 12:40:36 -0700362 init_stats(&walltime_nsecs_stats);
363 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200364 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100365}
366
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100367static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700368{
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200369 if (stat_config.initial_delay)
370 usleep(stat_config.initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100371
372 /*
373 * We need to enable counters only if:
374 * - we don't have tracee (attaching to task or cpu)
375 * - we have initial delay configured
376 */
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200377 if (!target__none(&target) || stat_config.initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100378 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700379}
380
Mark Rutland3df33ef2016-08-09 14:04:29 +0100381static void disable_counters(void)
382{
383 /*
384 * If we don't have tracee (attaching to task or cpu), counters may
385 * still be running. To get accurate group ratios, we must stop groups
386 * from counting before reading their constituent counters.
387 */
388 if (!target__none(&target))
389 perf_evlist__disable(evsel_list);
390}
391
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300392static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300393
394/*
395 * perf_evlist__prepare_workload will send a SIGUSR1
396 * if the fork fails, since we asked by setting its
397 * want_signal to true.
398 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300399static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
400 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300401{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300402 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300403}
404
Jiri Olsa73d586c2018-08-30 08:32:19 +0200405static int perf_stat_synthesize_config(struct perf_stat_config *config,
406 bool attrs)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100407{
408 int err;
409
Jiri Olsa491073a2018-08-30 08:32:18 +0200410 if (attrs) {
Jiri Olsa318ec182018-08-30 08:32:15 +0200411 err = perf_event__synthesize_attrs(NULL, evsel_list,
Jiri Olsa664c98d2015-11-05 15:40:50 +0100412 process_synthesized_event);
413 if (err < 0) {
414 pr_err("Couldn't synthesize attrs.\n");
415 return err;
416 }
417 }
418
Andi Kleenbfd8f722017-11-17 13:42:58 -0800419 err = perf_event__synthesize_extra_attr(NULL,
420 evsel_list,
421 process_synthesized_event,
Jiri Olsa491073a2018-08-30 08:32:18 +0200422 attrs);
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100423
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100424 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
425 process_synthesized_event,
426 NULL);
427 if (err < 0) {
428 pr_err("Couldn't synthesize thread map.\n");
429 return err;
430 }
431
432 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
433 process_synthesized_event, NULL);
434 if (err < 0) {
435 pr_err("Couldn't synthesize thread map.\n");
436 return err;
437 }
438
Jiri Olsa73d586c2018-08-30 08:32:19 +0200439 err = perf_event__synthesize_stat_config(NULL, config,
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100440 process_synthesized_event, NULL);
441 if (err < 0) {
442 pr_err("Couldn't synthesize config.\n");
443 return err;
444 }
445
446 return 0;
447}
448
Jiri Olsa82bf3112017-07-26 14:02:06 +0200449static bool perf_evsel__should_store_id(struct perf_evsel *counter)
450{
451 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
452}
453
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700454static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
455{
456 struct perf_evsel *c2, *leader;
457 bool is_open = true;
458
459 leader = evsel->leader;
460 pr_debug("Weak group for %s/%d failed\n",
461 leader->name, leader->nr_members);
462
463 /*
464 * for_each_group_member doesn't work here because it doesn't
465 * include the first entry.
466 */
467 evlist__for_each_entry(evsel_list, c2) {
468 if (c2 == evsel)
469 is_open = false;
470 if (c2->leader == leader) {
471 if (is_open)
472 perf_evsel__close(c2);
473 c2->leader = c2;
474 c2->nr_members = 0;
475 }
476 }
477 return leader;
478}
479
Jiri Olsae55c14a2018-04-23 11:08:21 +0200480static int __run_perf_stat(int argc, const char **argv, int run_idx)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200481{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200482 int interval = stat_config.interval;
yuzhoujiandb06a262018-01-29 10:25:22 +0100483 int times = stat_config.times;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100484 int timeout = stat_config.timeout;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300485 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200486 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100487 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100488 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100489 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200490 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300491 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100492 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600493 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200494
Stephane Eranian13370a92013-01-29 12:47:44 +0100495 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300496 ts.tv_sec = interval / USEC_PER_MSEC;
497 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100498 } else if (timeout) {
499 ts.tv_sec = timeout / USEC_PER_MSEC;
500 ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100501 } else {
502 ts.tv_sec = 1;
503 ts.tv_nsec = 0;
504 }
505
Liming Wang60666c62009-12-31 16:05:50 +0800506 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100507 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300508 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900509 perror("failed to prepare workload");
510 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800511 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900512 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000513 }
514
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200515 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300516 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200517
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300518 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300519try_again:
Jiri Olsad09cefd2018-08-30 08:32:17 +0200520 if (create_perf_stat_counter(counter, &stat_config, &target) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700521
522 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700523 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700524 counter->leader != counter &&
525 counter->weak_group) {
526 counter = perf_evsel__reset_weak_group(counter);
527 goto try_again;
528 }
529
David Ahern979987a2012-05-08 09:29:16 -0600530 /*
531 * PPC returns ENXIO for HW counters until 2.6.37
532 * (behavior changed with commit b0a873e).
533 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100534 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600535 errno == ENOENT || errno == EOPNOTSUPP ||
536 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900537 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600538 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300539 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600540 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400541
542 if ((counter->leader != counter) ||
543 !(counter->leader->nr_members > 1))
544 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300545 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900546 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300547 ui__warning("%s\n", msg);
548 goto try_again;
Jin Yaoab6c79b2018-01-16 23:43:08 +0800549 } else if (target__has_per_thread(&target) &&
550 evsel_list->threads &&
551 evsel_list->threads->err_thread != -1) {
552 /*
553 * For global --per-thread case, skip current
554 * error thread.
555 */
556 if (!thread_map__remove(evsel_list->threads,
557 evsel_list->threads->err_thread)) {
558 evsel_list->threads->err_thread = -1;
559 goto try_again;
560 }
561 }
Ingo Molnarede70292011-04-28 08:48:42 +0200562
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300563 perf_evsel__open_strerror(counter, &target,
564 errno, msg, sizeof(msg));
565 ui__error("%s\n", msg);
566
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200567 if (child_pid != -1)
568 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600569
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200570 return -1;
571 }
David Ahern2cee77c2011-05-30 08:55:59 -0600572 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100573
574 l = strlen(counter->unit);
575 if (l > unit_width)
576 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100577
Jiri Olsa82bf3112017-07-26 14:02:06 +0200578 if (perf_evsel__should_store_id(counter) &&
Jiri Olsa650d6222018-08-30 08:32:16 +0200579 perf_evsel__store_ids(counter, evsel_list))
Jiri Olsa2af46462015-11-05 15:40:49 +0100580 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300581 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200582
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300583 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300584 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300585 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300586 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100587 return -1;
588 }
589
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600590 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300591 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600592 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
593 str_error_r(errno, msg, sizeof(msg)));
594 return -1;
595 }
596
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100597 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100598 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100599
Jiri Olsa664c98d2015-11-05 15:40:50 +0100600 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100601 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100602 } else {
603 err = perf_session__write_header(perf_stat.session, evsel_list,
604 fd, false);
605 }
606
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100607 if (err < 0)
608 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100609
Jiri Olsa73d586c2018-08-30 08:32:19 +0200610 err = perf_stat_synthesize_config(&stat_config, is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100611 if (err < 0)
612 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100613 }
614
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200615 /*
616 * Enable counters and exec the command:
617 */
618 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100619 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200620
Liming Wang60666c62009-12-31 16:05:50 +0800621 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900622 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100623 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900624
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100625 if (interval || timeout) {
Stephane Eranian13370a92013-01-29 12:47:44 +0100626 while (!waitpid(child_pid, &status, WNOHANG)) {
627 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100628 if (timeout)
629 break;
Jiri Olsaba411a92015-06-26 11:29:24 +0200630 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100631 if (interval_count && !(--times))
632 break;
Stephane Eranian13370a92013-01-29 12:47:44 +0100633 }
634 }
Jiri Olsa0ce2da12018-06-05 14:13:13 +0200635 wait4(child_pid, &status, 0, &ru_data);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300636
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300637 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300638 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300639 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300640 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300641 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300642
Andi Kleen33e49ea2011-09-15 14:31:40 -0700643 if (WIFSIGNALED(status))
644 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800645 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100646 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100647 while (!done) {
648 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100649 if (timeout)
650 break;
yuzhoujiandb06a262018-01-29 10:25:22 +0100651 if (interval) {
Jiri Olsaba411a92015-06-26 11:29:24 +0200652 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100653 if (interval_count && !(--times))
654 break;
655 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100656 }
Liming Wang60666c62009-12-31 16:05:50 +0800657 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200658
Mark Rutland3df33ef2016-08-09 14:04:29 +0100659 disable_counters();
660
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200661 t1 = rdclock();
662
Jiri Olsae55c14a2018-04-23 11:08:21 +0200663 if (walltime_run_table)
664 walltime_run[run_idx] = t1 - t0;
665
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200666 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200667
Mark Rutland3df33ef2016-08-09 14:04:29 +0100668 /*
669 * Closing a group leader splits the group, and as we only disable
670 * group leaders, results in remaining events becoming enabled. To
671 * avoid arbitrary skew, we must read all counters before closing any
672 * group leaders.
673 */
674 read_counters();
675 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200676
Ingo Molnar42202dd2009-06-13 14:57:28 +0200677 return WEXITSTATUS(status);
678}
679
Jiri Olsae55c14a2018-04-23 11:08:21 +0200680static int run_perf_stat(int argc, const char **argv, int run_idx)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200681{
682 int ret;
683
684 if (pre_cmd) {
685 ret = system(pre_cmd);
686 if (ret)
687 return ret;
688 }
689
690 if (sync_run)
691 sync();
692
Jiri Olsae55c14a2018-04-23 11:08:21 +0200693 ret = __run_perf_stat(argc, argv, run_idx);
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200694 if (ret)
695 return ret;
696
697 if (post_cmd) {
698 ret = system(post_cmd);
699 if (ret)
700 return ret;
701 }
702
703 return ret;
704}
705
Andi Kleend73515c2015-03-11 07:16:27 -0700706static void print_running(u64 run, u64 ena)
707{
708 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200709 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700710 csv_sep,
711 run,
712 csv_sep,
713 ena ? 100.0 * run / ena : 100.0);
714 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200715 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700716 }
717}
718
Ingo Molnarf99844c2011-04-27 05:35:39 +0200719static void print_noise_pct(double total, double avg)
720{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800721 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200722
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700723 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200724 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600725 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200726 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200727}
728
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200729static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200730{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200731 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200732
Peter Zijlstra849abde2009-09-04 18:23:38 +0200733 if (run_count == 1)
734 return;
735
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300736 ps = evsel->stats;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200737 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200738}
739
Stephane Eranian12c08a92013-02-14 13:57:29 +0100740static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200741{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200742 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100743 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200744 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100745 cpu_map__id_to_socket(id),
746 csv_output ? 0 : -8,
747 cpu_map__id_to_cpu(id),
748 csv_sep,
749 csv_output ? 0 : 4,
750 nr,
751 csv_sep);
752 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100753 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200754 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100755 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100756 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100757 csv_sep,
758 csv_output ? 0 : 4,
759 nr,
760 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100761 break;
762 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200763 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200764 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100765 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100766 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200767 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200768 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200769 csv_output ? 0 : 16,
770 thread_map__comm(evsel->threads, id),
771 csv_output ? 0 : -8,
772 thread_map__pid(evsel->threads, id),
773 csv_sep);
774 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100775 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200776 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100777 default:
778 break;
779 }
780}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200781
Andi Kleen140aead2016-01-30 09:06:49 -0800782struct outstate {
783 FILE *fh;
784 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800785 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800786 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800787 int id, nr;
788 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800789};
790
791#define METRIC_LEN 35
792
793static void new_line_std(void *ctx)
794{
795 struct outstate *os = ctx;
796
797 os->newline = true;
798}
799
800static void do_new_line_std(struct outstate *os)
801{
802 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800803 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800804 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800805 if (stat_config.aggr_mode == AGGR_NONE)
806 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800807 fprintf(os->fh, " ");
808}
809
810static void print_metric_std(void *ctx, const char *color, const char *fmt,
811 const char *unit, double val)
812{
813 struct outstate *os = ctx;
814 FILE *out = os->fh;
815 int n;
816 bool newline = os->newline;
817
818 os->newline = false;
819
820 if (unit == NULL || fmt == NULL) {
821 fprintf(out, "%-*s", METRIC_LEN, "");
822 return;
823 }
824
825 if (newline)
826 do_new_line_std(os);
827
828 n = fprintf(out, " # ");
829 if (color)
830 n += color_fprintf(out, color, fmt, val);
831 else
832 n += fprintf(out, fmt, val);
833 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
834}
835
Andi Kleen92a61f62016-02-29 14:36:21 -0800836static void new_line_csv(void *ctx)
837{
838 struct outstate *os = ctx;
839 int i;
840
841 fputc('\n', os->fh);
842 if (os->prefix)
843 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800844 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800845 for (i = 0; i < os->nfields; i++)
846 fputs(csv_sep, os->fh);
847}
848
849static void print_metric_csv(void *ctx,
850 const char *color __maybe_unused,
851 const char *fmt, const char *unit, double val)
852{
853 struct outstate *os = ctx;
854 FILE *out = os->fh;
855 char buf[64], *vals, *ends;
856
857 if (unit == NULL || fmt == NULL) {
Ilya Pronin40c21892018-03-05 22:43:53 -0800858 fprintf(out, "%s%s", csv_sep, csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800859 return;
860 }
861 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900862 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800863 while (isdigit(*ends) || *ends == '.')
864 ends++;
865 *ends = 0;
866 while (isspace(*unit))
867 unit++;
868 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
869}
870
Andi Kleen54b50912016-03-03 15:57:36 -0800871/* Filter out some columns that don't work well in metrics only mode */
872
873static bool valid_only_metric(const char *unit)
874{
875 if (!unit)
876 return false;
877 if (strstr(unit, "/sec") ||
878 strstr(unit, "hz") ||
879 strstr(unit, "Hz") ||
880 strstr(unit, "CPUs utilized"))
881 return false;
882 return true;
883}
884
885static const char *fixunit(char *buf, struct perf_evsel *evsel,
886 const char *unit)
887{
888 if (!strncmp(unit, "of all", 6)) {
889 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
890 unit);
891 return buf;
892 }
893 return unit;
894}
895
896static void print_metric_only(void *ctx, const char *color, const char *fmt,
897 const char *unit, double val)
898{
899 struct outstate *os = ctx;
900 FILE *out = os->fh;
Jiri Olsaf5155722018-06-07 00:15:08 +0200901 char buf[1024], str[1024];
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200902 unsigned mlen = metric_only_len;
Andi Kleen54b50912016-03-03 15:57:36 -0800903
904 if (!valid_only_metric(unit))
905 return;
906 unit = fixunit(buf, os->evsel, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800907 if (mlen < strlen(unit))
908 mlen = strlen(unit) + 1;
Jiri Olsaf5155722018-06-07 00:15:08 +0200909
910 if (color)
911 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
912
913 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
914 fprintf(out, "%*s ", mlen, str);
Andi Kleen54b50912016-03-03 15:57:36 -0800915}
916
917static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
918 const char *fmt,
919 const char *unit, double val)
920{
921 struct outstate *os = ctx;
922 FILE *out = os->fh;
923 char buf[64], *vals, *ends;
924 char tbuf[1024];
925
926 if (!valid_only_metric(unit))
927 return;
928 unit = fixunit(tbuf, os->evsel, unit);
929 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900930 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -0800931 while (isdigit(*ends) || *ends == '.')
932 ends++;
933 *ends = 0;
934 fprintf(out, "%s%s", vals, csv_sep);
935}
936
937static void new_line_metric(void *ctx __maybe_unused)
938{
939}
940
941static void print_metric_header(void *ctx, const char *color __maybe_unused,
942 const char *fmt __maybe_unused,
943 const char *unit, double val __maybe_unused)
944{
945 struct outstate *os = ctx;
946 char tbuf[1024];
947
948 if (!valid_only_metric(unit))
949 return;
950 unit = fixunit(tbuf, os->evsel, unit);
951 if (csv_output)
952 fprintf(os->fh, "%s%s", unit, csv_sep);
953 else
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200954 fprintf(os->fh, "%*s ", metric_only_len, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800955}
956
Andi Kleen44d49a62016-02-29 14:36:22 -0800957static int first_shadow_cpu(struct perf_evsel *evsel, int id)
958{
959 int i;
960
961 if (!aggr_get_id)
962 return 0;
963
964 if (stat_config.aggr_mode == AGGR_NONE)
965 return id;
966
967 if (stat_config.aggr_mode == AGGR_GLOBAL)
968 return 0;
969
970 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
971 int cpu2 = perf_evsel__cpus(evsel)->map[i];
972
973 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
974 return cpu2;
975 }
976 return 0;
977}
978
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200979static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
980{
Jiri Olsa58215222015-07-21 14:31:24 +0200981 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200982 double sc = evsel->scale;
983 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200984
985 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -0700986 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200987 } else {
988 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -0700989 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200990 else
Andi Kleene3b03b62016-05-05 16:04:03 -0700991 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200992 }
993
994 aggr_printout(evsel, id, nr);
995
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200996 fprintf(output, fmt, avg, csv_sep);
997
998 if (evsel->unit)
999 fprintf(output, "%-*s%s",
1000 csv_output ? 0 : unit_width,
1001 evsel->unit, csv_sep);
1002
1003 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1004
1005 if (evsel->cgrp)
1006 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001007}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001008
Kan Liang30060ea2018-04-24 11:20:11 -07001009static bool is_mixed_hw_group(struct perf_evsel *counter)
1010{
1011 struct perf_evlist *evlist = counter->evlist;
1012 u32 pmu_type = counter->attr.type;
1013 struct perf_evsel *pos;
1014
1015 if (counter->nr_members < 2)
1016 return false;
1017
1018 evlist__for_each_entry(evlist, pos) {
1019 /* software events can be part of any hardware group */
1020 if (pos->attr.type == PERF_TYPE_SOFTWARE)
1021 continue;
1022 if (pmu_type == PERF_TYPE_SOFTWARE) {
1023 pmu_type = pos->attr.type;
1024 continue;
1025 }
1026 if (pmu_type != pos->attr.type)
1027 return true;
1028 }
1029
1030 return false;
1031}
1032
Andi Kleenf9483392016-01-30 09:06:50 -08001033static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +08001034 char *prefix, u64 run, u64 ena, double noise,
1035 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001036{
Andi Kleen140aead2016-01-30 09:06:49 -08001037 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001038 struct outstate os = {
1039 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001040 .prefix = prefix ? prefix : "",
1041 .id = id,
1042 .nr = nr,
1043 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001044 };
Andi Kleen140aead2016-01-30 09:06:49 -08001045 print_metric_t pm = print_metric_std;
1046 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001047
Andi Kleen54b50912016-03-03 15:57:36 -08001048 if (metric_only) {
1049 nl = new_line_metric;
1050 if (csv_output)
1051 pm = print_metric_only_csv;
1052 else
1053 pm = print_metric_only;
1054 } else
1055 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001056
Andi Kleen54b50912016-03-03 15:57:36 -08001057 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001058 static int aggr_fields[] = {
1059 [AGGR_GLOBAL] = 0,
1060 [AGGR_THREAD] = 1,
1061 [AGGR_NONE] = 1,
1062 [AGGR_SOCKET] = 2,
1063 [AGGR_CORE] = 2,
1064 };
1065
1066 pm = print_metric_csv;
1067 nl = new_line_csv;
1068 os.nfields = 3;
1069 os.nfields += aggr_fields[stat_config.aggr_mode];
1070 if (counter->cgrp)
1071 os.nfields++;
1072 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001073 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001074 if (metric_only) {
1075 pm(&os, NULL, "", "", 0);
1076 return;
1077 }
Andi Kleencb110f42016-01-30 09:06:51 -08001078 aggr_printout(counter, id, nr);
1079
1080 fprintf(stat_config.output, "%*s%s",
1081 csv_output ? 0 : 18,
1082 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1083 csv_sep);
1084
Kan Liang30060ea2018-04-24 11:20:11 -07001085 if (counter->supported) {
Borislav Petkov02d492e2017-02-07 01:40:05 +01001086 print_free_counters_hint = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001087 if (is_mixed_hw_group(counter))
1088 print_mixed_hw_group_error = 1;
1089 }
Borislav Petkov02d492e2017-02-07 01:40:05 +01001090
Andi Kleencb110f42016-01-30 09:06:51 -08001091 fprintf(stat_config.output, "%-*s%s",
1092 csv_output ? 0 : unit_width,
1093 counter->unit, csv_sep);
1094
1095 fprintf(stat_config.output, "%*s",
1096 csv_output ? 0 : -25,
1097 perf_evsel__name(counter));
1098
1099 if (counter->cgrp)
1100 fprintf(stat_config.output, "%s%s",
1101 csv_sep, counter->cgrp->name);
1102
Andi Kleen92a61f62016-02-29 14:36:21 -08001103 if (!csv_output)
1104 pm(&os, NULL, NULL, "", 0);
1105 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001106 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001107 if (csv_output)
1108 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001109 return;
1110 }
1111
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001112 if (!metric_only)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001113 abs_printout(id, nr, counter, uval);
1114
Andi Kleen140aead2016-01-30 09:06:49 -08001115 out.print_metric = pm;
1116 out.new_line = nl;
1117 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001118 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001119
Andi Kleen54b50912016-03-03 15:57:36 -08001120 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001121 print_noise(counter, noise);
1122 print_running(run, ena);
1123 }
1124
1125 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001126 first_shadow_cpu(counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001127 &out, &metric_events, st);
Andi Kleen54b50912016-03-03 15:57:36 -08001128 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001129 print_noise(counter, noise);
1130 print_running(run, ena);
1131 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001132}
1133
Andi Kleen44d49a62016-02-29 14:36:22 -08001134static void aggr_update_shadow(void)
1135{
1136 int cpu, s2, id, s;
1137 u64 val;
1138 struct perf_evsel *counter;
1139
1140 for (s = 0; s < aggr_map->nr; s++) {
1141 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001142 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001143 val = 0;
1144 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1145 s2 = aggr_get_id(evsel_list->cpus, cpu);
1146 if (s2 != id)
1147 continue;
1148 val += perf_counts(counter->counts, cpu, 0)->val;
1149 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001150 perf_stat__update_shadow_stats(counter, val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001151 first_shadow_cpu(counter, id),
1152 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001153 }
1154 }
1155}
1156
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001157static void uniquify_event_name(struct perf_evsel *counter)
1158{
1159 char *new_name;
1160 char *config;
1161
Kan Liang80ee8c52018-04-24 11:20:14 -07001162 if (counter->uniquified_name ||
1163 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001164 strlen(counter->pmu_name)))
1165 return;
1166
1167 config = strchr(counter->name, '/');
1168 if (config) {
1169 if (asprintf(&new_name,
1170 "%s%s", counter->pmu_name, config) > 0) {
1171 free(counter->name);
1172 counter->name = new_name;
1173 }
1174 } else {
1175 if (asprintf(&new_name,
1176 "%s [%s]", counter->name, counter->pmu_name) > 0) {
1177 free(counter->name);
1178 counter->name = new_name;
1179 }
1180 }
Kan Liang80ee8c52018-04-24 11:20:14 -07001181
1182 counter->uniquified_name = true;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001183}
1184
Andi Kleen430daf22017-03-20 13:17:00 -07001185static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001186 void (*cb)(struct perf_evsel *counter, void *data,
1187 bool first),
1188 void *data)
1189{
Andi Kleen430daf22017-03-20 13:17:00 -07001190 struct perf_evsel *alias;
1191
1192 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1193 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1194 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1195 alias->scale != counter->scale ||
1196 alias->cgrp != counter->cgrp ||
1197 strcmp(alias->unit, counter->unit) ||
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001198 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter))
Andi Kleen430daf22017-03-20 13:17:00 -07001199 break;
1200 alias->merged_stat = true;
1201 cb(alias, data, false);
1202 }
1203}
1204
1205static bool collect_data(struct perf_evsel *counter,
1206 void (*cb)(struct perf_evsel *counter, void *data,
1207 bool first),
1208 void *data)
1209{
1210 if (counter->merged_stat)
1211 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001212 cb(counter, data, true);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001213 if (no_merge)
1214 uniquify_event_name(counter);
1215 else if (counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001216 collect_all_aliases(counter, cb, data);
1217 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001218}
1219
1220struct aggr_data {
1221 u64 ena, run, val;
1222 int id;
1223 int nr;
1224 int cpu;
1225};
1226
1227static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1228{
1229 struct aggr_data *ad = data;
1230 int cpu, s2;
1231
1232 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1233 struct perf_counts_values *counts;
1234
1235 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1236 if (s2 != ad->id)
1237 continue;
1238 if (first)
1239 ad->nr++;
1240 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001241 /*
1242 * When any result is bad, make them all to give
1243 * consistent output in interval mode.
1244 */
1245 if (counts->ena == 0 || counts->run == 0 ||
1246 counter->counts->scaled == -1) {
1247 ad->ena = 0;
1248 ad->run = 0;
1249 break;
1250 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001251 ad->val += counts->val;
1252 ad->ena += counts->ena;
1253 ad->run += counts->run;
1254 }
1255}
1256
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001257static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001258{
Jiri Olsa58215222015-07-21 14:31:24 +02001259 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001260 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001261 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001262 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001263 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001264 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001265
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001266 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001267 return;
1268
Andi Kleen44d49a62016-02-29 14:36:22 -08001269 aggr_update_shadow();
1270
Andi Kleen54b50912016-03-03 15:57:36 -08001271 /*
1272 * With metric_only everything is on a single line.
1273 * Without each counter has its own line.
1274 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001275 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001276 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001277 if (prefix && metric_only)
1278 fprintf(output, "%s", prefix);
1279
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001280 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001281 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001282 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001283 if (is_duration_time(counter))
1284 continue;
1285
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001286 ad.val = ad.ena = ad.run = 0;
1287 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001288 if (!collect_data(counter, aggr_cb, &ad))
1289 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001290 nr = ad.nr;
1291 ena = ad.ena;
1292 run = ad.run;
1293 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001294 if (first && metric_only) {
1295 first = false;
1296 aggr_printout(counter, id, nr);
1297 }
1298 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001299 fprintf(output, "%s", prefix);
1300
Stephane Eranian410136f2013-11-12 17:58:49 +01001301 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001302 printout(id, nr, counter, uval, prefix, run, ena, 1.0,
1303 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001304 if (!metric_only)
1305 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001306 }
Andi Kleen54b50912016-03-03 15:57:36 -08001307 if (metric_only)
1308 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001309 }
1310}
1311
Jin Yao29734552017-12-05 22:03:11 +08001312static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001313{
Jin Yao29734552017-12-05 22:03:11 +08001314 return ((struct perf_aggr_thread_value *)b)->val -
1315 ((struct perf_aggr_thread_value *)a)->val;
1316}
1317
1318static struct perf_aggr_thread_value *sort_aggr_thread(
1319 struct perf_evsel *counter,
1320 int nthreads, int ncpus,
1321 int *ret)
1322{
1323 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001324 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001325 struct perf_aggr_thread_value *buf;
1326
1327 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1328 if (!buf)
1329 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001330
1331 for (thread = 0; thread < nthreads; thread++) {
1332 u64 ena = 0, run = 0, val = 0;
1333
1334 for (cpu = 0; cpu < ncpus; cpu++) {
1335 val += perf_counts(counter->counts, cpu, thread)->val;
1336 ena += perf_counts(counter->counts, cpu, thread)->ena;
1337 run += perf_counts(counter->counts, cpu, thread)->run;
1338 }
1339
Jin Yao29734552017-12-05 22:03:11 +08001340 uval = val * counter->scale;
1341
1342 /*
1343 * Skip value 0 when enabling --per-thread globally,
1344 * otherwise too many 0 output.
1345 */
1346 if (uval == 0.0 && target__has_per_thread(&target))
1347 continue;
1348
1349 buf[i].counter = counter;
1350 buf[i].id = thread;
1351 buf[i].uval = uval;
1352 buf[i].val = val;
1353 buf[i].run = run;
1354 buf[i].ena = ena;
1355 i++;
1356 }
1357
1358 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1359
1360 if (ret)
1361 *ret = i;
1362
1363 return buf;
1364}
1365
1366static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1367{
1368 FILE *output = stat_config.output;
1369 int nthreads = thread_map__nr(counter->threads);
1370 int ncpus = cpu_map__nr(counter->cpus);
1371 int thread, sorted_threads, id;
1372 struct perf_aggr_thread_value *buf;
1373
1374 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads);
1375 if (!buf) {
1376 perror("cannot sort aggr thread");
1377 return;
1378 }
1379
1380 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001381 if (prefix)
1382 fprintf(output, "%s", prefix);
1383
Jin Yao29734552017-12-05 22:03:11 +08001384 id = buf[thread].id;
Jin Yao14e72a22017-12-05 22:03:08 +08001385 if (stat_config.stats)
Jin Yao29734552017-12-05 22:03:11 +08001386 printout(id, 0, buf[thread].counter, buf[thread].uval,
1387 prefix, buf[thread].run, buf[thread].ena, 1.0,
1388 &stat_config.stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001389 else
Jin Yao29734552017-12-05 22:03:11 +08001390 printout(id, 0, buf[thread].counter, buf[thread].uval,
1391 prefix, buf[thread].run, buf[thread].ena, 1.0,
1392 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001393 fputc('\n', output);
1394 }
Jin Yao29734552017-12-05 22:03:11 +08001395
1396 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001397}
1398
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001399struct caggr_data {
1400 double avg, avg_enabled, avg_running;
1401};
1402
1403static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1404 bool first __maybe_unused)
1405{
1406 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001407 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001408
1409 cd->avg += avg_stats(&ps->res_stats[0]);
1410 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1411 cd->avg_running += avg_stats(&ps->res_stats[2]);
1412}
1413
Ingo Molnar42202dd2009-06-13 14:57:28 +02001414/*
1415 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001416 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001417 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001418static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001419{
Jiri Olsa58215222015-07-21 14:31:24 +02001420 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001421 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001422 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001423
Andi Kleen430daf22017-03-20 13:17:00 -07001424 if (!collect_data(counter, counter_aggr_cb, &cd))
1425 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001426
Andi Kleen54b50912016-03-03 15:57:36 -08001427 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001428 fprintf(output, "%s", prefix);
1429
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001430 uval = cd.avg * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001431 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
1432 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001433 if (!metric_only)
1434 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001435}
1436
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001437static void counter_cb(struct perf_evsel *counter, void *data,
1438 bool first __maybe_unused)
1439{
1440 struct aggr_data *ad = data;
1441
1442 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1443 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1444 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1445}
1446
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001447/*
1448 * Print out the results of a single counter:
1449 * does not use aggregated count in system-wide
1450 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001451static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001452{
Jiri Olsa58215222015-07-21 14:31:24 +02001453 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001454 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001455 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001456 int cpu;
1457
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001458 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001459 struct aggr_data ad = { .cpu = cpu };
1460
Andi Kleen430daf22017-03-20 13:17:00 -07001461 if (!collect_data(counter, counter_cb, &ad))
1462 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001463 val = ad.val;
1464 ena = ad.ena;
1465 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001466
1467 if (prefix)
1468 fprintf(output, "%s", prefix);
1469
Stephane Eranian410136f2013-11-12 17:58:49 +01001470 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001471 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1472 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001473
Stephane Eranian4aa90152011-08-15 22:22:33 +02001474 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001475 }
1476}
1477
Andi Kleen206cab62016-03-03 15:57:37 -08001478static void print_no_aggr_metric(char *prefix)
1479{
1480 int cpu;
1481 int nrcpus = 0;
1482 struct perf_evsel *counter;
1483 u64 ena, run, val;
1484 double uval;
1485
1486 nrcpus = evsel_list->cpus->nr;
1487 for (cpu = 0; cpu < nrcpus; cpu++) {
1488 bool first = true;
1489
1490 if (prefix)
1491 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001492 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001493 if (is_duration_time(counter))
1494 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001495 if (first) {
1496 aggr_printout(counter, cpu, 0);
1497 first = false;
1498 }
1499 val = perf_counts(counter->counts, cpu, 0)->val;
1500 ena = perf_counts(counter->counts, cpu, 0)->ena;
1501 run = perf_counts(counter->counts, cpu, 0)->run;
1502
1503 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001504 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1505 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001506 }
1507 fputc('\n', stat_config.output);
1508 }
1509}
1510
Andi Kleen54b50912016-03-03 15:57:36 -08001511static int aggr_header_lens[] = {
1512 [AGGR_CORE] = 18,
1513 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001514 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001515 [AGGR_THREAD] = 24,
1516 [AGGR_GLOBAL] = 0,
1517};
1518
Andi Kleenc51fd632016-05-24 12:52:39 -07001519static const char *aggr_header_csv[] = {
1520 [AGGR_CORE] = "core,cpus,",
1521 [AGGR_SOCKET] = "socket,cpus",
1522 [AGGR_NONE] = "cpu,",
1523 [AGGR_THREAD] = "comm-pid,",
1524 [AGGR_GLOBAL] = ""
1525};
1526
Andi Kleen41c8ca22016-05-24 12:52:38 -07001527static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001528{
1529 struct perf_stat_output_ctx out;
1530 struct perf_evsel *counter;
1531 struct outstate os = {
1532 .fh = stat_config.output
1533 };
1534
1535 if (prefix)
1536 fprintf(stat_config.output, "%s", prefix);
1537
Andi Kleen41c8ca22016-05-24 12:52:38 -07001538 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001539 fprintf(stat_config.output, "%*s",
1540 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001541 if (csv_output) {
1542 if (stat_config.interval)
1543 fputs("time,", stat_config.output);
1544 fputs(aggr_header_csv[stat_config.aggr_mode],
1545 stat_config.output);
1546 }
Andi Kleen54b50912016-03-03 15:57:36 -08001547
1548 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001549 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001550 if (is_duration_time(counter))
1551 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001552 os.evsel = counter;
1553 out.ctx = &os;
1554 out.print_metric = print_metric_header;
1555 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001556 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001557 os.evsel = counter;
1558 perf_stat__print_shadow_stats(counter, 0,
1559 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001560 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001561 &metric_events,
1562 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001563 }
1564 fputc('\n', stat_config.output);
1565}
1566
Jiri Olsad4f63a42015-06-26 11:29:26 +02001567static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001568{
Jiri Olsa58215222015-07-21 14:31:24 +02001569 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001570 static int num_print_interval;
1571
Jiri Olsa9660e082018-06-07 00:15:06 +02001572 if (interval_clear)
1573 puts(CONSOLE_CLEAR);
1574
Jiri Olsad4f63a42015-06-26 11:29:26 +02001575 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1576
Jiri Olsa9660e082018-06-07 00:15:06 +02001577 if ((num_print_interval == 0 && !csv_output) || interval_clear) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001578 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001579 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001580 fprintf(output, "# time socket cpus");
1581 if (!metric_only)
1582 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001583 break;
1584 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001585 fprintf(output, "# time core cpus");
1586 if (!metric_only)
1587 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001588 break;
1589 case AGGR_NONE:
Jiri Olsaf5155722018-06-07 00:15:08 +02001590 fprintf(output, "# time CPU ");
Andi Kleen41c8ca22016-05-24 12:52:38 -07001591 if (!metric_only)
1592 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001593 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001594 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001595 fprintf(output, "# time comm-pid");
1596 if (!metric_only)
1597 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001598 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001599 case AGGR_GLOBAL:
1600 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001601 fprintf(output, "# time");
1602 if (!metric_only)
1603 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001604 case AGGR_UNSET:
1605 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001606 }
1607 }
1608
Jiri Olsac818cc02018-07-02 15:42:02 +02001609 if ((num_print_interval == 0 || interval_clear) && metric_only)
Andi Kleen41c8ca22016-05-24 12:52:38 -07001610 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001611 if (++num_print_interval == 25)
1612 num_print_interval = 0;
1613}
1614
1615static void print_header(int argc, const char **argv)
1616{
Jiri Olsa58215222015-07-21 14:31:24 +02001617 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001618 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001619
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001620 fflush(stdout);
1621
Stephane Eraniand7470b62010-12-01 18:49:05 +02001622 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001623 fprintf(output, "\n");
1624 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001625 if (target.system_wide)
1626 fprintf(output, "\'system wide");
1627 else if (target.cpu_list)
1628 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001629 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001630 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1631 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001632 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001633 } else if (target.pid)
1634 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001635 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001636 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001637
Stephane Eranian4aa90152011-08-15 22:22:33 +02001638 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001639 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001640 fprintf(output, " (%d runs)", run_count);
1641 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001642 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001643}
1644
Jiri Olsabc22de92018-04-23 11:08:20 +02001645static int get_precision(double num)
1646{
1647 if (num > 1)
1648 return 0;
1649
1650 return lround(ceil(-log10(num)));
1651}
1652
Jiri Olsae55c14a2018-04-23 11:08:21 +02001653static void print_table(FILE *output, int precision, double avg)
1654{
1655 char tmp[64];
1656 int idx, indent = 0;
1657
1658 scnprintf(tmp, 64, " %17.*f", precision, avg);
1659 while (tmp[indent] == ' ')
1660 indent++;
1661
1662 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1663
1664 for (idx = 0; idx < run_count; idx++) {
1665 double run = (double) walltime_run[idx] / NSEC_PER_SEC;
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001666 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001667
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001668 fprintf(output, " %17.*f (%+.*f) ",
Jiri Olsae55c14a2018-04-23 11:08:21 +02001669 precision, run, precision, run - avg);
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001670
1671 for (h = 0; h < n; h++)
1672 fprintf(output, "#");
1673
1674 fprintf(output, "\n");
Jiri Olsae55c14a2018-04-23 11:08:21 +02001675 }
1676
1677 fprintf(output, "\n%*s# Final result:\n", indent, "");
1678}
1679
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001680static double timeval2double(struct timeval *t)
1681{
1682 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1683}
1684
Jiri Olsad4f63a42015-06-26 11:29:26 +02001685static void print_footer(void)
1686{
Jiri Olsabc22de92018-04-23 11:08:20 +02001687 double avg = avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsa58215222015-07-21 14:31:24 +02001688 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001689 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001690
Jiri Olsad4f63a42015-06-26 11:29:26 +02001691 if (!null_run)
1692 fprintf(output, "\n");
Jiri Olsabc22de92018-04-23 11:08:20 +02001693
1694 if (run_count == 1) {
1695 fprintf(output, " %17.9f seconds time elapsed", avg);
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001696
1697 if (ru_display) {
1698 double ru_utime = timeval2double(&ru_data.ru_utime);
1699 double ru_stime = timeval2double(&ru_data.ru_stime);
1700
1701 fprintf(output, "\n\n");
1702 fprintf(output, " %17.9f seconds user\n", ru_utime);
1703 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1704 }
Jiri Olsabc22de92018-04-23 11:08:20 +02001705 } else {
1706 double sd = stddev_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
1707 /*
1708 * Display at most 2 more significant
1709 * digits than the stddev inaccuracy.
1710 */
1711 int precision = get_precision(sd) + 2;
1712
Jiri Olsae55c14a2018-04-23 11:08:21 +02001713 if (walltime_run_table)
1714 print_table(output, precision, avg);
1715
Jiri Olsabc22de92018-04-23 11:08:20 +02001716 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1717 precision, avg, precision, sd);
1718
1719 print_noise_pct(sd, avg);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001720 }
1721 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001722
Andi Kleen918c7b062017-05-22 18:00:16 -07001723 if (print_free_counters_hint &&
1724 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1725 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001726 fprintf(output,
1727"Some events weren't counted. Try disabling the NMI watchdog:\n"
1728" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1729" perf stat ...\n"
1730" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001731
1732 if (print_mixed_hw_group_error)
1733 fprintf(output,
1734 "The events in group usually have to be from "
1735 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001736}
1737
1738static void print_counters(struct timespec *ts, int argc, const char **argv)
1739{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001740 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001741 struct perf_evsel *counter;
1742 char buf[64], *prefix = NULL;
1743
Jiri Olsa664c98d2015-11-05 15:40:50 +01001744 /* Do not print anything if we record to the pipe. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001745 if (STAT_RECORD && perf_stat.data.is_pipe)
Jiri Olsa664c98d2015-11-05 15:40:50 +01001746 return;
1747
Jiri Olsad4f63a42015-06-26 11:29:26 +02001748 if (interval)
1749 print_interval(prefix = buf, ts);
1750 else
1751 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001752
Andi Kleen54b50912016-03-03 15:57:36 -08001753 if (metric_only) {
1754 static int num_print_iv;
1755
Andi Kleen41c8ca22016-05-24 12:52:38 -07001756 if (num_print_iv == 0 && !interval)
1757 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001758 if (num_print_iv++ == 25)
1759 num_print_iv = 0;
1760 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1761 fprintf(stat_config.output, "%s", prefix);
1762 }
1763
Jiri Olsa421a50f2015-07-21 14:31:22 +02001764 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001765 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001766 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001767 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001768 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001769 case AGGR_THREAD:
Andi Kleene864c5c2017-08-31 12:40:35 -07001770 evlist__for_each_entry(evsel_list, counter) {
1771 if (is_duration_time(counter))
1772 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001773 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001774 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001775 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001776 case AGGR_GLOBAL:
Andi Kleene864c5c2017-08-31 12:40:35 -07001777 evlist__for_each_entry(evsel_list, counter) {
1778 if (is_duration_time(counter))
1779 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001780 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001781 }
Andi Kleen54b50912016-03-03 15:57:36 -08001782 if (metric_only)
1783 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001784 break;
1785 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001786 if (metric_only)
1787 print_no_aggr_metric(prefix);
1788 else {
Andi Kleene864c5c2017-08-31 12:40:35 -07001789 evlist__for_each_entry(evsel_list, counter) {
1790 if (is_duration_time(counter))
1791 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001792 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001793 }
Andi Kleen206cab62016-03-03 15:57:37 -08001794 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001795 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001796 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001797 default:
1798 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001799 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001800
Jiri Olsad4f63a42015-06-26 11:29:26 +02001801 if (!interval && !csv_output)
1802 print_footer();
1803
Jiri Olsa58215222015-07-21 14:31:24 +02001804 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001805}
1806
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001807static volatile int signr = -1;
1808
Ingo Molnar52425192009-05-26 09:17:18 +02001809static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001810{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001811 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001812 done = 1;
1813
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001814 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001815 /*
1816 * render child_pid harmless
1817 * won't send SIGTERM to a random
1818 * process in case of race condition
1819 * and fast PID recycling
1820 */
1821 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001822}
1823
1824static void sig_atexit(void)
1825{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001826 sigset_t set, oset;
1827
1828 /*
1829 * avoid race condition with SIGCHLD handler
1830 * in skip_signal() which is modifying child_pid
1831 * goal is to avoid send SIGTERM to a random
1832 * process
1833 */
1834 sigemptyset(&set);
1835 sigaddset(&set, SIGCHLD);
1836 sigprocmask(SIG_BLOCK, &set, &oset);
1837
Chris Wilson933da832009-10-04 01:35:01 +01001838 if (child_pid != -1)
1839 kill(child_pid, SIGTERM);
1840
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001841 sigprocmask(SIG_SETMASK, &oset, NULL);
1842
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001843 if (signr == -1)
1844 return;
1845
1846 signal(signr, SIG_DFL);
1847 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001848}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001849
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001850static int stat__set_big_num(const struct option *opt __maybe_unused,
1851 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001852{
1853 big_num_opt = unset ? 0 : 1;
1854 return 0;
1855}
1856
Andi Kleen44b1e602016-05-30 12:49:42 -03001857static int enable_metric_only(const struct option *opt __maybe_unused,
1858 const char *s __maybe_unused, int unset)
1859{
1860 force_metric_only = true;
1861 metric_only = !unset;
1862 return 0;
1863}
1864
Andi Kleenb18f3e32017-08-31 12:40:31 -07001865static int parse_metric_groups(const struct option *opt,
1866 const char *str,
1867 int unset __maybe_unused)
1868{
1869 return metricgroup__parse_groups(opt, str, &metric_events);
1870}
1871
Jiri Olsae0547312015-11-05 15:40:45 +01001872static const struct option stat_options[] = {
1873 OPT_BOOLEAN('T', "transaction", &transaction_run,
1874 "hardware transaction statistics"),
1875 OPT_CALLBACK('e', "event", &evsel_list, "event",
1876 "event selector. use 'perf list' to list available events",
1877 parse_events_option),
1878 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1879 "event filter", parse_filter),
Jiri Olsa5698f262018-08-30 08:32:12 +02001880 OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
Jiri Olsae0547312015-11-05 15:40:45 +01001881 "child tasks do not inherit counters"),
1882 OPT_STRING('p', "pid", &target.pid, "pid",
1883 "stat events on existing process id"),
1884 OPT_STRING('t', "tid", &target.tid, "tid",
1885 "stat events on existing thread id"),
1886 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1887 "system-wide collection from all CPUs"),
1888 OPT_BOOLEAN('g', "group", &group,
1889 "put the counters into a counter group"),
1890 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1891 OPT_INCR('v', "verbose", &verbose,
1892 "be more verbose (show counter open errors, etc)"),
1893 OPT_INTEGER('r', "repeat", &run_count,
1894 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsae55c14a2018-04-23 11:08:21 +02001895 OPT_BOOLEAN(0, "table", &walltime_run_table,
1896 "display details about each run (only with -r option)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001897 OPT_BOOLEAN('n', "null", &null_run,
1898 "null run - dont start any counters"),
1899 OPT_INCR('d', "detailed", &detailed_run,
1900 "detailed run - start a lot of events"),
1901 OPT_BOOLEAN('S', "sync", &sync_run,
1902 "call sync() before starting a run"),
1903 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1904 "print large numbers with thousands\' separators",
1905 stat__set_big_num),
1906 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1907 "list of cpus to monitor in system-wide"),
1908 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1909 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001910 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001911 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1912 "print counts with custom separator"),
1913 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1914 "monitor event in cgroup name only", parse_cgroups),
1915 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1916 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1917 OPT_INTEGER(0, "log-fd", &output_fd,
1918 "log output to fd, instead of stderr"),
1919 OPT_STRING(0, "pre", &pre_cmd, "command",
1920 "command to run prior to the measured command"),
1921 OPT_STRING(0, "post", &post_cmd, "command",
1922 "command to run after to the measured command"),
1923 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03001924 "print counts at regular interval in ms "
1925 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01001926 OPT_INTEGER(0, "interval-count", &stat_config.times,
1927 "print counts for fixed number of times"),
Jiri Olsa9660e082018-06-07 00:15:06 +02001928 OPT_BOOLEAN(0, "interval-clear", &interval_clear,
1929 "clear screen in between new interval"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01001930 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1931 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001932 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1933 "aggregate counts per processor socket", AGGR_SOCKET),
1934 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1935 "aggregate counts per physical processor core", AGGR_CORE),
1936 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1937 "aggregate counts per thread", AGGR_THREAD),
Jiri Olsa728c0ee2018-08-30 08:32:11 +02001938 OPT_UINTEGER('D', "delay", &stat_config.initial_delay,
Jiri Olsae0547312015-11-05 15:40:45 +01001939 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001940 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1941 "Only print computed metrics. No raw values", enable_metric_only),
1942 OPT_BOOLEAN(0, "topdown", &topdown_run,
1943 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001944 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1945 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001946 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1947 "monitor specified metrics or metric groups (separated by ,)",
1948 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01001949 OPT_END()
1950};
1951
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001952static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1953{
1954 return cpu_map__get_socket(map, cpu, NULL);
1955}
1956
1957static int perf_stat__get_core(struct cpu_map *map, int cpu)
1958{
1959 return cpu_map__get_core(map, cpu, NULL);
1960}
1961
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001962static int cpu_map__get_max(struct cpu_map *map)
1963{
1964 int i, max = -1;
1965
1966 for (i = 0; i < map->nr; i++) {
1967 if (map->map[i] > max)
1968 max = map->map[i];
1969 }
1970
1971 return max;
1972}
1973
1974static struct cpu_map *cpus_aggr_map;
1975
1976static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1977{
1978 int cpu;
1979
1980 if (idx >= map->nr)
1981 return -1;
1982
1983 cpu = map->map[idx];
1984
1985 if (cpus_aggr_map->map[cpu] == -1)
1986 cpus_aggr_map->map[cpu] = get_id(map, idx);
1987
1988 return cpus_aggr_map->map[cpu];
1989}
1990
1991static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1992{
1993 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1994}
1995
1996static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1997{
1998 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1999}
2000
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002001static int perf_stat_init_aggr_mode(void)
2002{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002003 int nr;
2004
Jiri Olsa421a50f2015-07-21 14:31:22 +02002005 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002006 case AGGR_SOCKET:
2007 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
2008 perror("cannot build socket map");
2009 return -1;
2010 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002011 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002012 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002013 case AGGR_CORE:
2014 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
2015 perror("cannot build core map");
2016 return -1;
2017 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002018 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002019 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002020 case AGGR_NONE:
2021 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002022 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002023 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002024 default:
2025 break;
2026 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002027
2028 /*
2029 * The evsel_list->cpus is the base we operate on,
2030 * taking the highest cpu number to be the size of
2031 * the aggregation translate cpumap.
2032 */
2033 nr = cpu_map__get_max(evsel_list->cpus);
2034 cpus_aggr_map = cpu_map__empty_new(nr + 1);
2035 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002036}
2037
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002038static void perf_stat__exit_aggr_mode(void)
2039{
2040 cpu_map__put(aggr_map);
2041 cpu_map__put(cpus_aggr_map);
2042 aggr_map = NULL;
2043 cpus_aggr_map = NULL;
2044}
2045
Jiri Olsa68d702f2015-11-05 15:40:58 +01002046static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2047{
2048 int cpu;
2049
2050 if (idx > map->nr)
2051 return -1;
2052
2053 cpu = map->map[idx];
2054
Jan Stancekda8a58b2017-02-17 12:10:26 +01002055 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002056 return -1;
2057
2058 return cpu;
2059}
2060
2061static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2062{
2063 struct perf_env *env = data;
2064 int cpu = perf_env__get_cpu(env, map, idx);
2065
2066 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2067}
2068
2069static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2070{
2071 struct perf_env *env = data;
2072 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2073
2074 if (cpu != -1) {
2075 int socket_id = env->cpu[cpu].socket_id;
2076
2077 /*
2078 * Encode socket in upper 16 bits
2079 * core_id is relative to socket, and
2080 * we need a global id. So we combine
2081 * socket + core id.
2082 */
2083 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2084 }
2085
2086 return core;
2087}
2088
2089static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2090 struct cpu_map **sockp)
2091{
2092 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2093}
2094
2095static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2096 struct cpu_map **corep)
2097{
2098 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2099}
2100
2101static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2102{
2103 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2104}
2105
2106static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2107{
2108 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2109}
2110
2111static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2112{
2113 struct perf_env *env = &st->session->header.env;
2114
2115 switch (stat_config.aggr_mode) {
2116 case AGGR_SOCKET:
2117 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2118 perror("cannot build socket map");
2119 return -1;
2120 }
2121 aggr_get_id = perf_stat__get_socket_file;
2122 break;
2123 case AGGR_CORE:
2124 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2125 perror("cannot build core map");
2126 return -1;
2127 }
2128 aggr_get_id = perf_stat__get_core_file;
2129 break;
2130 case AGGR_NONE:
2131 case AGGR_GLOBAL:
2132 case AGGR_THREAD:
2133 case AGGR_UNSET:
2134 default:
2135 break;
2136 }
2137
2138 return 0;
2139}
2140
Andi Kleen44b1e602016-05-30 12:49:42 -03002141static int topdown_filter_events(const char **attr, char **str, bool use_group)
2142{
2143 int off = 0;
2144 int i;
2145 int len = 0;
2146 char *s;
2147
2148 for (i = 0; attr[i]; i++) {
2149 if (pmu_have_event("cpu", attr[i])) {
2150 len += strlen(attr[i]) + 1;
2151 attr[i - off] = attr[i];
2152 } else
2153 off++;
2154 }
2155 attr[i - off] = NULL;
2156
2157 *str = malloc(len + 1 + 2);
2158 if (!*str)
2159 return -1;
2160 s = *str;
2161 if (i - off == 0) {
2162 *s = 0;
2163 return 0;
2164 }
2165 if (use_group)
2166 *s++ = '{';
2167 for (i = 0; attr[i]; i++) {
2168 strcpy(s, attr[i]);
2169 s += strlen(s);
2170 *s++ = ',';
2171 }
2172 if (use_group) {
2173 s[-1] = '}';
2174 *s = 0;
2175 } else
2176 s[-1] = 0;
2177 return 0;
2178}
2179
2180__weak bool arch_topdown_check_group(bool *warn)
2181{
2182 *warn = false;
2183 return false;
2184}
2185
2186__weak void arch_topdown_group_warn(void)
2187{
2188}
2189
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002190/*
2191 * Add default attributes, if there were no attributes specified or
2192 * if -d/--detailed, -d -d or -d -d -d is used:
2193 */
2194static int add_default_attributes(void)
2195{
Andi Kleen44b1e602016-05-30 12:49:42 -03002196 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002197 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002198
2199 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2200 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2201 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2202 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2203
2204 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002205};
2206 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002207 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002208};
2209 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002210 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002211};
2212 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002213 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2214 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2215 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2216
2217};
2218
2219/*
2220 * Detailed stats (-d), covering the L1 and last level data caches:
2221 */
2222 struct perf_event_attr detailed_attrs[] = {
2223
2224 { .type = PERF_TYPE_HW_CACHE,
2225 .config =
2226 PERF_COUNT_HW_CACHE_L1D << 0 |
2227 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2228 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2229
2230 { .type = PERF_TYPE_HW_CACHE,
2231 .config =
2232 PERF_COUNT_HW_CACHE_L1D << 0 |
2233 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2234 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2235
2236 { .type = PERF_TYPE_HW_CACHE,
2237 .config =
2238 PERF_COUNT_HW_CACHE_LL << 0 |
2239 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2240 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2241
2242 { .type = PERF_TYPE_HW_CACHE,
2243 .config =
2244 PERF_COUNT_HW_CACHE_LL << 0 |
2245 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2246 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2247};
2248
2249/*
2250 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2251 */
2252 struct perf_event_attr very_detailed_attrs[] = {
2253
2254 { .type = PERF_TYPE_HW_CACHE,
2255 .config =
2256 PERF_COUNT_HW_CACHE_L1I << 0 |
2257 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2258 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2259
2260 { .type = PERF_TYPE_HW_CACHE,
2261 .config =
2262 PERF_COUNT_HW_CACHE_L1I << 0 |
2263 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2264 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2265
2266 { .type = PERF_TYPE_HW_CACHE,
2267 .config =
2268 PERF_COUNT_HW_CACHE_DTLB << 0 |
2269 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2270 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2271
2272 { .type = PERF_TYPE_HW_CACHE,
2273 .config =
2274 PERF_COUNT_HW_CACHE_DTLB << 0 |
2275 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2276 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2277
2278 { .type = PERF_TYPE_HW_CACHE,
2279 .config =
2280 PERF_COUNT_HW_CACHE_ITLB << 0 |
2281 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2282 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2283
2284 { .type = PERF_TYPE_HW_CACHE,
2285 .config =
2286 PERF_COUNT_HW_CACHE_ITLB << 0 |
2287 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2288 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2289
2290};
2291
2292/*
2293 * Very, very detailed stats (-d -d -d), adding prefetch events:
2294 */
2295 struct perf_event_attr very_very_detailed_attrs[] = {
2296
2297 { .type = PERF_TYPE_HW_CACHE,
2298 .config =
2299 PERF_COUNT_HW_CACHE_L1D << 0 |
2300 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2301 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2302
2303 { .type = PERF_TYPE_HW_CACHE,
2304 .config =
2305 PERF_COUNT_HW_CACHE_L1D << 0 |
2306 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2307 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2308};
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002309 struct parse_events_error errinfo;
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002310
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002311 /* Set attrs if no event is selected and !null_run: */
2312 if (null_run)
2313 return 0;
2314
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002315 if (transaction_run) {
Thomas Richter742d92f2018-06-26 09:17:01 +02002316 /* Handle -T as -M transaction. Once platform specific metrics
2317 * support has been added to the json files, all archictures
2318 * will use this approach. To determine transaction support
2319 * on an architecture test for such a metric name.
2320 */
2321 if (metricgroup__has_metric("transaction")) {
2322 struct option opt = { .value = &evsel_list };
2323
2324 return metricgroup__parse_groups(&opt, "transaction",
2325 &metric_events);
2326 }
2327
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002328 if (pmu_have_event("cpu", "cycles-ct") &&
2329 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002330 err = parse_events(evsel_list, transaction_attrs,
2331 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002332 else
Thomas Richterfca323402018-03-08 15:57:35 +01002333 err = parse_events(evsel_list,
2334 transaction_limited_attrs,
2335 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002336 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002337 fprintf(stderr, "Cannot set up transaction events\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002338 parse_events_print_error(&errinfo, transaction_attrs);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002339 return -1;
2340 }
2341 return 0;
2342 }
2343
Kan Liangdaefd0b2017-05-26 12:05:38 -07002344 if (smi_cost) {
2345 int smi;
2346
2347 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2348 fprintf(stderr, "freeze_on_smi is not supported.\n");
2349 return -1;
2350 }
2351
2352 if (!smi) {
2353 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2354 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2355 return -1;
2356 }
2357 smi_reset = true;
2358 }
2359
2360 if (pmu_have_event("msr", "aperf") &&
2361 pmu_have_event("msr", "smi")) {
2362 if (!force_metric_only)
2363 metric_only = true;
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002364 err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002365 } else {
2366 fprintf(stderr, "To measure SMI cost, it needs "
2367 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002368 parse_events_print_error(&errinfo, smi_cost_attrs);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002369 return -1;
2370 }
2371 if (err) {
2372 fprintf(stderr, "Cannot set up SMI cost events\n");
2373 return -1;
2374 }
2375 return 0;
2376 }
2377
Andi Kleen44b1e602016-05-30 12:49:42 -03002378 if (topdown_run) {
2379 char *str = NULL;
2380 bool warn = false;
2381
2382 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2383 stat_config.aggr_mode != AGGR_CORE) {
2384 pr_err("top down event configuration requires --per-core mode\n");
2385 return -1;
2386 }
2387 stat_config.aggr_mode = AGGR_CORE;
2388 if (nr_cgroups || !target__has_cpu(&target)) {
2389 pr_err("top down event configuration requires system-wide mode (-a)\n");
2390 return -1;
2391 }
2392
2393 if (!force_metric_only)
2394 metric_only = true;
2395 if (topdown_filter_events(topdown_attrs, &str,
2396 arch_topdown_check_group(&warn)) < 0) {
2397 pr_err("Out of memory\n");
2398 return -1;
2399 }
2400 if (topdown_attrs[0] && str) {
2401 if (warn)
2402 arch_topdown_group_warn();
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002403 err = parse_events(evsel_list, str, &errinfo);
Andi Kleen44b1e602016-05-30 12:49:42 -03002404 if (err) {
2405 fprintf(stderr,
2406 "Cannot set up top down events %s: %d\n",
2407 str, err);
2408 free(str);
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002409 parse_events_print_error(&errinfo, str);
Andi Kleen44b1e602016-05-30 12:49:42 -03002410 return -1;
2411 }
2412 } else {
2413 fprintf(stderr, "System does not support topdown\n");
2414 return -1;
2415 }
2416 free(str);
2417 }
2418
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002419 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002420 if (target__has_cpu(&target))
2421 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2422
Andi Kleen9dec4472016-02-26 16:27:56 -08002423 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2424 return -1;
2425 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2426 if (perf_evlist__add_default_attrs(evsel_list,
2427 frontend_attrs) < 0)
2428 return -1;
2429 }
2430 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2431 if (perf_evlist__add_default_attrs(evsel_list,
2432 backend_attrs) < 0)
2433 return -1;
2434 }
2435 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002436 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002437 }
2438
2439 /* Detailed events get appended to the event list: */
2440
2441 if (detailed_run < 1)
2442 return 0;
2443
2444 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002445 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002446 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002447
2448 if (detailed_run < 2)
2449 return 0;
2450
2451 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002452 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002453 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002454
2455 if (detailed_run < 3)
2456 return 0;
2457
2458 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002459 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002460}
2461
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002462static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002463 "perf stat record [<options>]",
2464 NULL,
2465};
2466
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002467static void init_features(struct perf_session *session)
2468{
2469 int feat;
2470
2471 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2472 perf_header__set_feat(&session->header, feat);
2473
2474 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2475 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2476 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2477 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2478}
2479
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002480static int __cmd_record(int argc, const char **argv)
2481{
2482 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002483 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002484
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002485 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002486 PARSE_OPT_STOP_AT_NON_OPTION);
2487
2488 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002489 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002490
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002491 if (run_count != 1 || forever) {
2492 pr_err("Cannot use -r option with perf stat record.\n");
2493 return -1;
2494 }
2495
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002496 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002497 if (session == NULL) {
2498 pr_err("Perf session creation failed.\n");
2499 return -1;
2500 }
2501
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002502 init_features(session);
2503
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002504 session->evlist = evsel_list;
2505 perf_stat.session = session;
2506 perf_stat.record = true;
2507 return argc;
2508}
2509
Jiri Olsaa56f9392015-11-05 15:40:59 +01002510static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2511 union perf_event *event,
2512 struct perf_session *session)
2513{
Andi Kleene3b03b62016-05-05 16:04:03 -07002514 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002515 struct perf_evsel *counter;
2516 struct timespec tsh, *ts = NULL;
2517 const char **argv = session->header.env.cmdline_argv;
2518 int argc = session->header.env.nr_cmdline;
2519
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002520 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002521 perf_stat_process_counter(&stat_config, counter);
2522
Andi Kleene3b03b62016-05-05 16:04:03 -07002523 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2524 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002525
Andi Kleene3b03b62016-05-05 16:04:03 -07002526 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002527 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2528 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002529 ts = &tsh;
2530 }
2531
2532 print_counters(ts, argc, argv);
2533 return 0;
2534}
2535
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002536static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002537int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002538 union perf_event *event,
2539 struct perf_session *session __maybe_unused)
2540{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002541 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2542
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002543 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002544
Jiri Olsa89af4e02015-11-05 15:41:02 +01002545 if (cpu_map__empty(st->cpus)) {
2546 if (st->aggr_mode != AGGR_UNSET)
2547 pr_warning("warning: processing task data, aggregation mode not set\n");
2548 return 0;
2549 }
2550
2551 if (st->aggr_mode != AGGR_UNSET)
2552 stat_config.aggr_mode = st->aggr_mode;
2553
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002554 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002555 perf_stat_init_aggr_mode();
2556 else
2557 perf_stat_init_aggr_mode_file(st);
2558
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002559 return 0;
2560}
2561
Jiri Olsa1975d362015-11-05 15:40:56 +01002562static int set_maps(struct perf_stat *st)
2563{
2564 if (!st->cpus || !st->threads)
2565 return 0;
2566
2567 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2568 return -EINVAL;
2569
2570 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2571
2572 if (perf_evlist__alloc_stats(evsel_list, true))
2573 return -ENOMEM;
2574
2575 st->maps_allocated = true;
2576 return 0;
2577}
2578
2579static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002580int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002581 union perf_event *event,
2582 struct perf_session *session __maybe_unused)
2583{
2584 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2585
2586 if (st->threads) {
2587 pr_warning("Extra thread map event, ignoring.\n");
2588 return 0;
2589 }
2590
2591 st->threads = thread_map__new_event(&event->thread_map);
2592 if (!st->threads)
2593 return -ENOMEM;
2594
2595 return set_maps(st);
2596}
2597
2598static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002599int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002600 union perf_event *event,
2601 struct perf_session *session __maybe_unused)
2602{
2603 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2604 struct cpu_map *cpus;
2605
2606 if (st->cpus) {
2607 pr_warning("Extra cpu map event, ignoring.\n");
2608 return 0;
2609 }
2610
2611 cpus = cpu_map__new_data(&event->cpu_map.data);
2612 if (!cpus)
2613 return -ENOMEM;
2614
2615 st->cpus = cpus;
2616 return set_maps(st);
2617}
2618
Jin Yao56739442017-12-05 22:03:07 +08002619static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2620{
2621 int i;
2622
2623 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2624 if (!config->stats)
2625 return -1;
2626
2627 config->stats_num = nthreads;
2628
2629 for (i = 0; i < nthreads; i++)
2630 runtime_stat__init(&config->stats[i]);
2631
2632 return 0;
2633}
2634
2635static void runtime_stat_delete(struct perf_stat_config *config)
2636{
2637 int i;
2638
2639 if (!config->stats)
2640 return;
2641
2642 for (i = 0; i < config->stats_num; i++)
2643 runtime_stat__exit(&config->stats[i]);
2644
2645 free(config->stats);
2646}
2647
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002648static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002649 "perf stat report [<options>]",
2650 NULL,
2651};
2652
2653static struct perf_stat perf_stat = {
2654 .tool = {
2655 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002656 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002657 .thread_map = process_thread_map_event,
2658 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002659 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002660 .stat = perf_event__process_stat_event,
2661 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002662 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002663 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002664};
2665
2666static int __cmd_report(int argc, const char **argv)
2667{
2668 struct perf_session *session;
2669 const struct option options[] = {
2670 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002671 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2672 "aggregate counts per processor socket", AGGR_SOCKET),
2673 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2674 "aggregate counts per physical processor core", AGGR_CORE),
2675 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2676 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002677 OPT_END()
2678 };
2679 struct stat st;
2680 int ret;
2681
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002682 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002683
2684 if (!input_name || !strlen(input_name)) {
2685 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2686 input_name = "-";
2687 else
2688 input_name = "perf.data";
2689 }
2690
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002691 perf_stat.data.file.path = input_name;
2692 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002693
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002694 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002695 if (session == NULL)
2696 return -1;
2697
2698 perf_stat.session = session;
2699 stat_config.output = stderr;
2700 evsel_list = session->evlist;
2701
2702 ret = perf_session__process_events(session);
2703 if (ret)
2704 return ret;
2705
2706 perf_session__delete(session);
2707 return 0;
2708}
2709
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002710static void setup_system_wide(int forks)
2711{
2712 /*
2713 * Make system wide (-a) the default target if
2714 * no target was specified and one of following
2715 * conditions is met:
2716 *
2717 * - there's no workload specified
2718 * - there is workload specified but all requested
2719 * events are system wide events
2720 */
2721 if (!target__none(&target))
2722 return;
2723
2724 if (!forks)
2725 target.system_wide = true;
2726 else {
2727 struct perf_evsel *counter;
2728
2729 evlist__for_each_entry(evsel_list, counter) {
2730 if (!counter->system_wide)
2731 return;
2732 }
2733
2734 if (evsel_list->nr_entries)
2735 target.system_wide = true;
2736 }
2737}
2738
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002739int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002740{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002741 const char * const stat_usage[] = {
2742 "perf stat [<options>] [<command>]",
2743 NULL
2744 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002745 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002746 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002747 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002748 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002749 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002750
Stephane Eranian5af52b52010-05-18 15:00:01 +02002751 setlocale(LC_ALL, "");
2752
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002753 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002754 if (evsel_list == NULL)
2755 return -ENOMEM;
2756
Wang Nan1669e502016-02-19 11:43:58 +00002757 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002758 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2759 (const char **) stat_usage,
2760 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002761 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002762 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002763
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002764 if (csv_sep) {
2765 csv_output = true;
2766 if (!strcmp(csv_sep, "\\t"))
2767 csv_sep = "\t";
2768 } else
2769 csv_sep = DEFAULT_SEPARATOR;
2770
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002771 if (argc && !strncmp(argv[0], "rec", 3)) {
2772 argc = __cmd_record(argc, argv);
2773 if (argc < 0)
2774 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002775 } else if (argc && !strncmp(argv[0], "rep", 3))
2776 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002777
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002778 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002779 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002780
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002781 /*
2782 * For record command the -o is already taken care of.
2783 */
2784 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002785 output = NULL;
2786
Jim Cromie56f3bae2011-09-07 17:14:00 -06002787 if (output_name && output_fd) {
2788 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002789 parse_options_usage(stat_usage, stat_options, "o", 1);
2790 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002791 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002792 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002793
Andi Kleen54b50912016-03-03 15:57:36 -08002794 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2795 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2796 goto out;
2797 }
2798
Andi Kleen54b50912016-03-03 15:57:36 -08002799 if (metric_only && run_count > 1) {
2800 fprintf(stderr, "--metric-only is not supported with -r\n");
2801 goto out;
2802 }
2803
Jiri Olsae55c14a2018-04-23 11:08:21 +02002804 if (walltime_run_table && run_count <= 1) {
2805 fprintf(stderr, "--table is only supported with -r\n");
2806 parse_options_usage(stat_usage, stat_options, "r", 1);
2807 parse_options_usage(NULL, stat_options, "table", 0);
2808 goto out;
2809 }
2810
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002811 if (output_fd < 0) {
2812 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002813 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002814 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002815 }
2816
Stephane Eranian4aa90152011-08-15 22:22:33 +02002817 if (!output) {
2818 struct timespec tm;
2819 mode = append_file ? "a" : "w";
2820
2821 output = fopen(output_name, mode);
2822 if (!output) {
2823 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002824 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002825 }
2826 clock_gettime(CLOCK_REALTIME, &tm);
2827 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002828 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002829 mode = append_file ? "a" : "w";
2830 output = fdopen(output_fd, mode);
2831 if (!output) {
2832 perror("Failed opening logfd");
2833 return -errno;
2834 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002835 }
2836
Jiri Olsa58215222015-07-21 14:31:24 +02002837 stat_config.output = output;
2838
Stephane Eraniand7470b62010-12-01 18:49:05 +02002839 /*
2840 * let the spreadsheet do the pretty-printing
2841 */
2842 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002843 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002844 if (big_num_opt == 1) {
2845 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002846 parse_options_usage(stat_usage, stat_options, "B", 1);
2847 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002848 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002849 } else /* Nope, so disable big number formatting */
2850 big_num = false;
2851 } else if (big_num_opt == 0) /* User passed --no-big-num */
2852 big_num = false;
2853
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002854 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002855
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002856 /*
2857 * Display user/system times only for single
2858 * run and when there's specified tracee.
2859 */
2860 if ((run_count == 1) && target__none(&target))
2861 ru_display = true;
2862
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002863 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002864 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002865 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002866 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002867 } else if (run_count == 0) {
2868 forever = true;
2869 run_count = 1;
2870 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002871
Jiri Olsae55c14a2018-04-23 11:08:21 +02002872 if (walltime_run_table) {
2873 walltime_run = zalloc(run_count * sizeof(walltime_run[0]));
2874 if (!walltime_run) {
2875 pr_err("failed to setup -r option");
2876 goto out;
2877 }
2878 }
2879
Jin Yao1d9f8d12017-12-05 22:03:10 +08002880 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2881 !target__has_task(&target)) {
2882 if (!target.system_wide || target.cpu_list) {
2883 fprintf(stderr, "The --per-thread option is only "
2884 "available when monitoring via -p -t -a "
2885 "options or only --per-thread.\n");
2886 parse_options_usage(NULL, stat_options, "p", 1);
2887 parse_options_usage(NULL, stat_options, "t", 1);
2888 goto out;
2889 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002890 }
2891
2892 /*
2893 * no_aggr, cgroup are for system-wide only
2894 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2895 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002896 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2897 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002898 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002899 fprintf(stderr, "both cgroup and no-aggregation "
2900 "modes only available in system-wide mode\n");
2901
Jiri Olsae0547312015-11-05 15:40:45 +01002902 parse_options_usage(stat_usage, stat_options, "G", 1);
2903 parse_options_usage(NULL, stat_options, "A", 1);
2904 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002905 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002906 }
2907
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002908 if (add_default_attributes())
2909 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002910
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002911 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002912
Jin Yao1d9f8d12017-12-05 22:03:10 +08002913 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2914 target.per_thread = true;
2915
Namhyung Kim77a6f012012-05-07 14:09:04 +09002916 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002917 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002918 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002919 parse_options_usage(stat_usage, stat_options, "p", 1);
2920 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002921 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002922 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002923 parse_options_usage(stat_usage, stat_options, "C", 1);
2924 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002925 }
2926 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002927 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002928
2929 /*
2930 * Initialize thread_map with comm names,
2931 * so we could print it out on output.
2932 */
Jin Yao56739442017-12-05 22:03:07 +08002933 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002934 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08002935 if (target.system_wide) {
2936 if (runtime_stat_new(&stat_config,
2937 thread_map__nr(evsel_list->threads))) {
2938 goto out;
2939 }
2940 }
2941 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002942
yuzhoujiandb06a262018-01-29 10:25:22 +01002943 if (stat_config.times && interval)
2944 interval_count = true;
2945 else if (stat_config.times && !interval) {
2946 pr_err("interval-count option should be used together with "
2947 "interval-print.\n");
2948 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2949 parse_options_usage(stat_usage, stat_options, "I", 1);
2950 goto out;
2951 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002952
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002953 if (timeout && timeout < 100) {
2954 if (timeout < 10) {
2955 pr_err("timeout must be >= 10ms.\n");
2956 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2957 goto out;
2958 } else
2959 pr_warning("timeout < 100ms. "
2960 "The overhead percentage could be high in some cases. "
2961 "Please proceed with caution.\n");
2962 }
2963 if (timeout && interval) {
2964 pr_err("timeout option is not supported with interval-print.\n");
2965 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2966 parse_options_usage(stat_usage, stat_options, "I", 1);
2967 goto out;
2968 }
2969
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002970 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002971 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002972
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002973 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002974 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002975
Ingo Molnar58d7e992009-05-15 11:03:23 +02002976 /*
Jiri Olsa7d9ad162018-08-30 08:32:14 +02002977 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
2978 * while avoiding that older tools show confusing messages.
2979 *
2980 * However for pipe sessions we need to keep it zero,
2981 * because script's perf_evsel__check_attr is triggered
2982 * by attr->sample_type != 0, and we can't run it on
2983 * stat sessions.
2984 */
2985 stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
2986
2987 /*
Ingo Molnar58d7e992009-05-15 11:03:23 +02002988 * We dont want to block the signals - that would cause
2989 * child tasks to inherit that and Ctrl-C would not work.
2990 * What we want is for Ctrl-C to work in the exec()-ed
2991 * task, but being ignored by perf stat itself:
2992 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002993 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002994 if (!forever)
2995 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002996 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002997 signal(SIGALRM, skip_signal);
2998 signal(SIGABRT, skip_signal);
2999
Ingo Molnar42202dd2009-06-13 14:57:28 +02003000 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003001 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09003002 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02003003 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
3004 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02003005
Jiri Olsae55c14a2018-04-23 11:08:21 +02003006 status = run_perf_stat(argc, argv, run_idx);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003007 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02003008 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02003009 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003010 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02003011 }
3012
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003013 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02003014 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003015
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003016 if (STAT_RECORD) {
3017 /*
3018 * We synthesize the kernel mmap record just so that older tools
3019 * don't emit warnings about not being able to resolve symbols
3020 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3021 * a saner message about no samples being in the perf.data file.
3022 *
3023 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01003024 * in header.c at the moment 'perf stat record' gets introduced, which
3025 * is not really needed once we start adding the stat specific PERF_RECORD_
3026 * records, but the need to suppress the kptr_restrict messages in older
3027 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003028 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003029 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003030 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3031 process_synthesized_event,
3032 &perf_stat.session->machines.host);
3033 if (err) {
3034 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3035 "older tools may produce warnings about this file\n.");
3036 }
3037
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003038 if (!interval) {
3039 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3040 pr_err("failed to write stat round event\n");
3041 }
3042
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003043 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003044 perf_stat.session->header.data_size += perf_stat.bytes_written;
3045 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3046 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003047
3048 perf_session__delete(perf_stat.session);
3049 }
3050
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003051 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003052 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003053out:
Jiri Olsae55c14a2018-04-23 11:08:21 +02003054 free(walltime_run);
3055
Kan Liangdaefd0b2017-05-26 12:05:38 -07003056 if (smi_cost && smi_reset)
3057 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3058
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003059 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003060
3061 runtime_stat_delete(&stat_config);
3062
Ingo Molnar42202dd2009-06-13 14:57:28 +02003063 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003064}