blob: 0a358c2e1a93ef73d96b6b58bbf5e684e2b2d826 [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 Olsa491073a2018-08-30 08:32:18 +0200405static int perf_stat_synthesize_config(bool attrs)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100406{
407 int err;
408
Jiri Olsa491073a2018-08-30 08:32:18 +0200409 if (attrs) {
Jiri Olsa318ec182018-08-30 08:32:15 +0200410 err = perf_event__synthesize_attrs(NULL, evsel_list,
Jiri Olsa664c98d2015-11-05 15:40:50 +0100411 process_synthesized_event);
412 if (err < 0) {
413 pr_err("Couldn't synthesize attrs.\n");
414 return err;
415 }
416 }
417
Andi Kleenbfd8f722017-11-17 13:42:58 -0800418 err = perf_event__synthesize_extra_attr(NULL,
419 evsel_list,
420 process_synthesized_event,
Jiri Olsa491073a2018-08-30 08:32:18 +0200421 attrs);
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100422
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100423 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
424 process_synthesized_event,
425 NULL);
426 if (err < 0) {
427 pr_err("Couldn't synthesize thread map.\n");
428 return err;
429 }
430
431 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
432 process_synthesized_event, NULL);
433 if (err < 0) {
434 pr_err("Couldn't synthesize thread map.\n");
435 return err;
436 }
437
438 err = perf_event__synthesize_stat_config(NULL, &stat_config,
439 process_synthesized_event, NULL);
440 if (err < 0) {
441 pr_err("Couldn't synthesize config.\n");
442 return err;
443 }
444
445 return 0;
446}
447
Jiri Olsa82bf3112017-07-26 14:02:06 +0200448static bool perf_evsel__should_store_id(struct perf_evsel *counter)
449{
450 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
451}
452
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700453static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
454{
455 struct perf_evsel *c2, *leader;
456 bool is_open = true;
457
458 leader = evsel->leader;
459 pr_debug("Weak group for %s/%d failed\n",
460 leader->name, leader->nr_members);
461
462 /*
463 * for_each_group_member doesn't work here because it doesn't
464 * include the first entry.
465 */
466 evlist__for_each_entry(evsel_list, c2) {
467 if (c2 == evsel)
468 is_open = false;
469 if (c2->leader == leader) {
470 if (is_open)
471 perf_evsel__close(c2);
472 c2->leader = c2;
473 c2->nr_members = 0;
474 }
475 }
476 return leader;
477}
478
Jiri Olsae55c14a2018-04-23 11:08:21 +0200479static int __run_perf_stat(int argc, const char **argv, int run_idx)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200480{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200481 int interval = stat_config.interval;
yuzhoujiandb06a262018-01-29 10:25:22 +0100482 int times = stat_config.times;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100483 int timeout = stat_config.timeout;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300484 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200485 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100486 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100487 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100488 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200489 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300490 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100491 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600492 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200493
Stephane Eranian13370a92013-01-29 12:47:44 +0100494 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300495 ts.tv_sec = interval / USEC_PER_MSEC;
496 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100497 } else if (timeout) {
498 ts.tv_sec = timeout / USEC_PER_MSEC;
499 ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100500 } else {
501 ts.tv_sec = 1;
502 ts.tv_nsec = 0;
503 }
504
Liming Wang60666c62009-12-31 16:05:50 +0800505 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100506 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300507 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900508 perror("failed to prepare workload");
509 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800510 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900511 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000512 }
513
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200514 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300515 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200516
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300517 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300518try_again:
Jiri Olsad09cefd2018-08-30 08:32:17 +0200519 if (create_perf_stat_counter(counter, &stat_config, &target) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700520
521 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700522 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700523 counter->leader != counter &&
524 counter->weak_group) {
525 counter = perf_evsel__reset_weak_group(counter);
526 goto try_again;
527 }
528
David Ahern979987a2012-05-08 09:29:16 -0600529 /*
530 * PPC returns ENXIO for HW counters until 2.6.37
531 * (behavior changed with commit b0a873e).
532 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100533 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600534 errno == ENOENT || errno == EOPNOTSUPP ||
535 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900536 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600537 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300538 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600539 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400540
541 if ((counter->leader != counter) ||
542 !(counter->leader->nr_members > 1))
543 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300544 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900545 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300546 ui__warning("%s\n", msg);
547 goto try_again;
Jin Yaoab6c79b2018-01-16 23:43:08 +0800548 } else if (target__has_per_thread(&target) &&
549 evsel_list->threads &&
550 evsel_list->threads->err_thread != -1) {
551 /*
552 * For global --per-thread case, skip current
553 * error thread.
554 */
555 if (!thread_map__remove(evsel_list->threads,
556 evsel_list->threads->err_thread)) {
557 evsel_list->threads->err_thread = -1;
558 goto try_again;
559 }
560 }
Ingo Molnarede70292011-04-28 08:48:42 +0200561
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300562 perf_evsel__open_strerror(counter, &target,
563 errno, msg, sizeof(msg));
564 ui__error("%s\n", msg);
565
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200566 if (child_pid != -1)
567 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600568
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200569 return -1;
570 }
David Ahern2cee77c2011-05-30 08:55:59 -0600571 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100572
573 l = strlen(counter->unit);
574 if (l > unit_width)
575 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100576
Jiri Olsa82bf3112017-07-26 14:02:06 +0200577 if (perf_evsel__should_store_id(counter) &&
Jiri Olsa650d6222018-08-30 08:32:16 +0200578 perf_evsel__store_ids(counter, evsel_list))
Jiri Olsa2af46462015-11-05 15:40:49 +0100579 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300580 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200581
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300582 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300583 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300584 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300585 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100586 return -1;
587 }
588
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600589 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300590 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600591 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
592 str_error_r(errno, msg, sizeof(msg)));
593 return -1;
594 }
595
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100596 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100597 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100598
Jiri Olsa664c98d2015-11-05 15:40:50 +0100599 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100600 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100601 } else {
602 err = perf_session__write_header(perf_stat.session, evsel_list,
603 fd, false);
604 }
605
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100606 if (err < 0)
607 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100608
Jiri Olsa664c98d2015-11-05 15:40:50 +0100609 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100610 if (err < 0)
611 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100612 }
613
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200614 /*
615 * Enable counters and exec the command:
616 */
617 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100618 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200619
Liming Wang60666c62009-12-31 16:05:50 +0800620 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900621 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100622 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900623
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100624 if (interval || timeout) {
Stephane Eranian13370a92013-01-29 12:47:44 +0100625 while (!waitpid(child_pid, &status, WNOHANG)) {
626 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100627 if (timeout)
628 break;
Jiri Olsaba411a92015-06-26 11:29:24 +0200629 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100630 if (interval_count && !(--times))
631 break;
Stephane Eranian13370a92013-01-29 12:47:44 +0100632 }
633 }
Jiri Olsa0ce2da12018-06-05 14:13:13 +0200634 wait4(child_pid, &status, 0, &ru_data);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300635
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300636 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300637 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300638 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300639 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300640 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300641
Andi Kleen33e49ea2011-09-15 14:31:40 -0700642 if (WIFSIGNALED(status))
643 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800644 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100645 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100646 while (!done) {
647 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100648 if (timeout)
649 break;
yuzhoujiandb06a262018-01-29 10:25:22 +0100650 if (interval) {
Jiri Olsaba411a92015-06-26 11:29:24 +0200651 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100652 if (interval_count && !(--times))
653 break;
654 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100655 }
Liming Wang60666c62009-12-31 16:05:50 +0800656 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200657
Mark Rutland3df33ef2016-08-09 14:04:29 +0100658 disable_counters();
659
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200660 t1 = rdclock();
661
Jiri Olsae55c14a2018-04-23 11:08:21 +0200662 if (walltime_run_table)
663 walltime_run[run_idx] = t1 - t0;
664
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200665 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200666
Mark Rutland3df33ef2016-08-09 14:04:29 +0100667 /*
668 * Closing a group leader splits the group, and as we only disable
669 * group leaders, results in remaining events becoming enabled. To
670 * avoid arbitrary skew, we must read all counters before closing any
671 * group leaders.
672 */
673 read_counters();
674 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200675
Ingo Molnar42202dd2009-06-13 14:57:28 +0200676 return WEXITSTATUS(status);
677}
678
Jiri Olsae55c14a2018-04-23 11:08:21 +0200679static int run_perf_stat(int argc, const char **argv, int run_idx)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200680{
681 int ret;
682
683 if (pre_cmd) {
684 ret = system(pre_cmd);
685 if (ret)
686 return ret;
687 }
688
689 if (sync_run)
690 sync();
691
Jiri Olsae55c14a2018-04-23 11:08:21 +0200692 ret = __run_perf_stat(argc, argv, run_idx);
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200693 if (ret)
694 return ret;
695
696 if (post_cmd) {
697 ret = system(post_cmd);
698 if (ret)
699 return ret;
700 }
701
702 return ret;
703}
704
Andi Kleend73515c2015-03-11 07:16:27 -0700705static void print_running(u64 run, u64 ena)
706{
707 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200708 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700709 csv_sep,
710 run,
711 csv_sep,
712 ena ? 100.0 * run / ena : 100.0);
713 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200714 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700715 }
716}
717
Ingo Molnarf99844c2011-04-27 05:35:39 +0200718static void print_noise_pct(double total, double avg)
719{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800720 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200721
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700722 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200723 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600724 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200725 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200726}
727
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200728static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200729{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200730 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200731
Peter Zijlstra849abde2009-09-04 18:23:38 +0200732 if (run_count == 1)
733 return;
734
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300735 ps = evsel->stats;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200736 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200737}
738
Stephane Eranian12c08a92013-02-14 13:57:29 +0100739static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200740{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200741 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100742 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200743 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100744 cpu_map__id_to_socket(id),
745 csv_output ? 0 : -8,
746 cpu_map__id_to_cpu(id),
747 csv_sep,
748 csv_output ? 0 : 4,
749 nr,
750 csv_sep);
751 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100752 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200753 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100754 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100755 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100756 csv_sep,
757 csv_output ? 0 : 4,
758 nr,
759 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100760 break;
761 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200762 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200763 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100764 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100765 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200766 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200767 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200768 csv_output ? 0 : 16,
769 thread_map__comm(evsel->threads, id),
770 csv_output ? 0 : -8,
771 thread_map__pid(evsel->threads, id),
772 csv_sep);
773 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100774 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200775 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100776 default:
777 break;
778 }
779}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200780
Andi Kleen140aead2016-01-30 09:06:49 -0800781struct outstate {
782 FILE *fh;
783 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800784 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800785 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800786 int id, nr;
787 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800788};
789
790#define METRIC_LEN 35
791
792static void new_line_std(void *ctx)
793{
794 struct outstate *os = ctx;
795
796 os->newline = true;
797}
798
799static void do_new_line_std(struct outstate *os)
800{
801 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800802 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800803 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800804 if (stat_config.aggr_mode == AGGR_NONE)
805 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800806 fprintf(os->fh, " ");
807}
808
809static void print_metric_std(void *ctx, const char *color, const char *fmt,
810 const char *unit, double val)
811{
812 struct outstate *os = ctx;
813 FILE *out = os->fh;
814 int n;
815 bool newline = os->newline;
816
817 os->newline = false;
818
819 if (unit == NULL || fmt == NULL) {
820 fprintf(out, "%-*s", METRIC_LEN, "");
821 return;
822 }
823
824 if (newline)
825 do_new_line_std(os);
826
827 n = fprintf(out, " # ");
828 if (color)
829 n += color_fprintf(out, color, fmt, val);
830 else
831 n += fprintf(out, fmt, val);
832 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
833}
834
Andi Kleen92a61f62016-02-29 14:36:21 -0800835static void new_line_csv(void *ctx)
836{
837 struct outstate *os = ctx;
838 int i;
839
840 fputc('\n', os->fh);
841 if (os->prefix)
842 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800843 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800844 for (i = 0; i < os->nfields; i++)
845 fputs(csv_sep, os->fh);
846}
847
848static void print_metric_csv(void *ctx,
849 const char *color __maybe_unused,
850 const char *fmt, const char *unit, double val)
851{
852 struct outstate *os = ctx;
853 FILE *out = os->fh;
854 char buf[64], *vals, *ends;
855
856 if (unit == NULL || fmt == NULL) {
Ilya Pronin40c21892018-03-05 22:43:53 -0800857 fprintf(out, "%s%s", csv_sep, csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800858 return;
859 }
860 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900861 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800862 while (isdigit(*ends) || *ends == '.')
863 ends++;
864 *ends = 0;
865 while (isspace(*unit))
866 unit++;
867 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
868}
869
Andi Kleen54b50912016-03-03 15:57:36 -0800870/* Filter out some columns that don't work well in metrics only mode */
871
872static bool valid_only_metric(const char *unit)
873{
874 if (!unit)
875 return false;
876 if (strstr(unit, "/sec") ||
877 strstr(unit, "hz") ||
878 strstr(unit, "Hz") ||
879 strstr(unit, "CPUs utilized"))
880 return false;
881 return true;
882}
883
884static const char *fixunit(char *buf, struct perf_evsel *evsel,
885 const char *unit)
886{
887 if (!strncmp(unit, "of all", 6)) {
888 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
889 unit);
890 return buf;
891 }
892 return unit;
893}
894
895static void print_metric_only(void *ctx, const char *color, const char *fmt,
896 const char *unit, double val)
897{
898 struct outstate *os = ctx;
899 FILE *out = os->fh;
Jiri Olsaf5155722018-06-07 00:15:08 +0200900 char buf[1024], str[1024];
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200901 unsigned mlen = metric_only_len;
Andi Kleen54b50912016-03-03 15:57:36 -0800902
903 if (!valid_only_metric(unit))
904 return;
905 unit = fixunit(buf, os->evsel, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800906 if (mlen < strlen(unit))
907 mlen = strlen(unit) + 1;
Jiri Olsaf5155722018-06-07 00:15:08 +0200908
909 if (color)
910 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
911
912 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
913 fprintf(out, "%*s ", mlen, str);
Andi Kleen54b50912016-03-03 15:57:36 -0800914}
915
916static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
917 const char *fmt,
918 const char *unit, double val)
919{
920 struct outstate *os = ctx;
921 FILE *out = os->fh;
922 char buf[64], *vals, *ends;
923 char tbuf[1024];
924
925 if (!valid_only_metric(unit))
926 return;
927 unit = fixunit(tbuf, os->evsel, unit);
928 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900929 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -0800930 while (isdigit(*ends) || *ends == '.')
931 ends++;
932 *ends = 0;
933 fprintf(out, "%s%s", vals, csv_sep);
934}
935
936static void new_line_metric(void *ctx __maybe_unused)
937{
938}
939
940static void print_metric_header(void *ctx, const char *color __maybe_unused,
941 const char *fmt __maybe_unused,
942 const char *unit, double val __maybe_unused)
943{
944 struct outstate *os = ctx;
945 char tbuf[1024];
946
947 if (!valid_only_metric(unit))
948 return;
949 unit = fixunit(tbuf, os->evsel, unit);
950 if (csv_output)
951 fprintf(os->fh, "%s%s", unit, csv_sep);
952 else
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200953 fprintf(os->fh, "%*s ", metric_only_len, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800954}
955
Andi Kleen44d49a62016-02-29 14:36:22 -0800956static int first_shadow_cpu(struct perf_evsel *evsel, int id)
957{
958 int i;
959
960 if (!aggr_get_id)
961 return 0;
962
963 if (stat_config.aggr_mode == AGGR_NONE)
964 return id;
965
966 if (stat_config.aggr_mode == AGGR_GLOBAL)
967 return 0;
968
969 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
970 int cpu2 = perf_evsel__cpus(evsel)->map[i];
971
972 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
973 return cpu2;
974 }
975 return 0;
976}
977
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200978static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
979{
Jiri Olsa58215222015-07-21 14:31:24 +0200980 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200981 double sc = evsel->scale;
982 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200983
984 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -0700985 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200986 } else {
987 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -0700988 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200989 else
Andi Kleene3b03b62016-05-05 16:04:03 -0700990 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200991 }
992
993 aggr_printout(evsel, id, nr);
994
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200995 fprintf(output, fmt, avg, csv_sep);
996
997 if (evsel->unit)
998 fprintf(output, "%-*s%s",
999 csv_output ? 0 : unit_width,
1000 evsel->unit, csv_sep);
1001
1002 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1003
1004 if (evsel->cgrp)
1005 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001006}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001007
Kan Liang30060ea2018-04-24 11:20:11 -07001008static bool is_mixed_hw_group(struct perf_evsel *counter)
1009{
1010 struct perf_evlist *evlist = counter->evlist;
1011 u32 pmu_type = counter->attr.type;
1012 struct perf_evsel *pos;
1013
1014 if (counter->nr_members < 2)
1015 return false;
1016
1017 evlist__for_each_entry(evlist, pos) {
1018 /* software events can be part of any hardware group */
1019 if (pos->attr.type == PERF_TYPE_SOFTWARE)
1020 continue;
1021 if (pmu_type == PERF_TYPE_SOFTWARE) {
1022 pmu_type = pos->attr.type;
1023 continue;
1024 }
1025 if (pmu_type != pos->attr.type)
1026 return true;
1027 }
1028
1029 return false;
1030}
1031
Andi Kleenf9483392016-01-30 09:06:50 -08001032static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +08001033 char *prefix, u64 run, u64 ena, double noise,
1034 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001035{
Andi Kleen140aead2016-01-30 09:06:49 -08001036 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001037 struct outstate os = {
1038 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001039 .prefix = prefix ? prefix : "",
1040 .id = id,
1041 .nr = nr,
1042 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001043 };
Andi Kleen140aead2016-01-30 09:06:49 -08001044 print_metric_t pm = print_metric_std;
1045 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001046
Andi Kleen54b50912016-03-03 15:57:36 -08001047 if (metric_only) {
1048 nl = new_line_metric;
1049 if (csv_output)
1050 pm = print_metric_only_csv;
1051 else
1052 pm = print_metric_only;
1053 } else
1054 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001055
Andi Kleen54b50912016-03-03 15:57:36 -08001056 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001057 static int aggr_fields[] = {
1058 [AGGR_GLOBAL] = 0,
1059 [AGGR_THREAD] = 1,
1060 [AGGR_NONE] = 1,
1061 [AGGR_SOCKET] = 2,
1062 [AGGR_CORE] = 2,
1063 };
1064
1065 pm = print_metric_csv;
1066 nl = new_line_csv;
1067 os.nfields = 3;
1068 os.nfields += aggr_fields[stat_config.aggr_mode];
1069 if (counter->cgrp)
1070 os.nfields++;
1071 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001072 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001073 if (metric_only) {
1074 pm(&os, NULL, "", "", 0);
1075 return;
1076 }
Andi Kleencb110f42016-01-30 09:06:51 -08001077 aggr_printout(counter, id, nr);
1078
1079 fprintf(stat_config.output, "%*s%s",
1080 csv_output ? 0 : 18,
1081 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1082 csv_sep);
1083
Kan Liang30060ea2018-04-24 11:20:11 -07001084 if (counter->supported) {
Borislav Petkov02d492e2017-02-07 01:40:05 +01001085 print_free_counters_hint = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001086 if (is_mixed_hw_group(counter))
1087 print_mixed_hw_group_error = 1;
1088 }
Borislav Petkov02d492e2017-02-07 01:40:05 +01001089
Andi Kleencb110f42016-01-30 09:06:51 -08001090 fprintf(stat_config.output, "%-*s%s",
1091 csv_output ? 0 : unit_width,
1092 counter->unit, csv_sep);
1093
1094 fprintf(stat_config.output, "%*s",
1095 csv_output ? 0 : -25,
1096 perf_evsel__name(counter));
1097
1098 if (counter->cgrp)
1099 fprintf(stat_config.output, "%s%s",
1100 csv_sep, counter->cgrp->name);
1101
Andi Kleen92a61f62016-02-29 14:36:21 -08001102 if (!csv_output)
1103 pm(&os, NULL, NULL, "", 0);
1104 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001105 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001106 if (csv_output)
1107 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001108 return;
1109 }
1110
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001111 if (!metric_only)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001112 abs_printout(id, nr, counter, uval);
1113
Andi Kleen140aead2016-01-30 09:06:49 -08001114 out.print_metric = pm;
1115 out.new_line = nl;
1116 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001117 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001118
Andi Kleen54b50912016-03-03 15:57:36 -08001119 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001120 print_noise(counter, noise);
1121 print_running(run, ena);
1122 }
1123
1124 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001125 first_shadow_cpu(counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001126 &out, &metric_events, st);
Andi Kleen54b50912016-03-03 15:57:36 -08001127 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001128 print_noise(counter, noise);
1129 print_running(run, ena);
1130 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001131}
1132
Andi Kleen44d49a62016-02-29 14:36:22 -08001133static void aggr_update_shadow(void)
1134{
1135 int cpu, s2, id, s;
1136 u64 val;
1137 struct perf_evsel *counter;
1138
1139 for (s = 0; s < aggr_map->nr; s++) {
1140 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001141 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001142 val = 0;
1143 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1144 s2 = aggr_get_id(evsel_list->cpus, cpu);
1145 if (s2 != id)
1146 continue;
1147 val += perf_counts(counter->counts, cpu, 0)->val;
1148 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001149 perf_stat__update_shadow_stats(counter, val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001150 first_shadow_cpu(counter, id),
1151 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001152 }
1153 }
1154}
1155
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001156static void uniquify_event_name(struct perf_evsel *counter)
1157{
1158 char *new_name;
1159 char *config;
1160
Kan Liang80ee8c52018-04-24 11:20:14 -07001161 if (counter->uniquified_name ||
1162 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001163 strlen(counter->pmu_name)))
1164 return;
1165
1166 config = strchr(counter->name, '/');
1167 if (config) {
1168 if (asprintf(&new_name,
1169 "%s%s", counter->pmu_name, config) > 0) {
1170 free(counter->name);
1171 counter->name = new_name;
1172 }
1173 } else {
1174 if (asprintf(&new_name,
1175 "%s [%s]", counter->name, counter->pmu_name) > 0) {
1176 free(counter->name);
1177 counter->name = new_name;
1178 }
1179 }
Kan Liang80ee8c52018-04-24 11:20:14 -07001180
1181 counter->uniquified_name = true;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001182}
1183
Andi Kleen430daf22017-03-20 13:17:00 -07001184static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001185 void (*cb)(struct perf_evsel *counter, void *data,
1186 bool first),
1187 void *data)
1188{
Andi Kleen430daf22017-03-20 13:17:00 -07001189 struct perf_evsel *alias;
1190
1191 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1192 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1193 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1194 alias->scale != counter->scale ||
1195 alias->cgrp != counter->cgrp ||
1196 strcmp(alias->unit, counter->unit) ||
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001197 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter))
Andi Kleen430daf22017-03-20 13:17:00 -07001198 break;
1199 alias->merged_stat = true;
1200 cb(alias, data, false);
1201 }
1202}
1203
1204static bool collect_data(struct perf_evsel *counter,
1205 void (*cb)(struct perf_evsel *counter, void *data,
1206 bool first),
1207 void *data)
1208{
1209 if (counter->merged_stat)
1210 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001211 cb(counter, data, true);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001212 if (no_merge)
1213 uniquify_event_name(counter);
1214 else if (counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001215 collect_all_aliases(counter, cb, data);
1216 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001217}
1218
1219struct aggr_data {
1220 u64 ena, run, val;
1221 int id;
1222 int nr;
1223 int cpu;
1224};
1225
1226static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1227{
1228 struct aggr_data *ad = data;
1229 int cpu, s2;
1230
1231 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1232 struct perf_counts_values *counts;
1233
1234 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1235 if (s2 != ad->id)
1236 continue;
1237 if (first)
1238 ad->nr++;
1239 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001240 /*
1241 * When any result is bad, make them all to give
1242 * consistent output in interval mode.
1243 */
1244 if (counts->ena == 0 || counts->run == 0 ||
1245 counter->counts->scaled == -1) {
1246 ad->ena = 0;
1247 ad->run = 0;
1248 break;
1249 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001250 ad->val += counts->val;
1251 ad->ena += counts->ena;
1252 ad->run += counts->run;
1253 }
1254}
1255
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001256static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001257{
Jiri Olsa58215222015-07-21 14:31:24 +02001258 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001259 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001260 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001261 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001262 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001263 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001264
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001265 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001266 return;
1267
Andi Kleen44d49a62016-02-29 14:36:22 -08001268 aggr_update_shadow();
1269
Andi Kleen54b50912016-03-03 15:57:36 -08001270 /*
1271 * With metric_only everything is on a single line.
1272 * Without each counter has its own line.
1273 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001274 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001275 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001276 if (prefix && metric_only)
1277 fprintf(output, "%s", prefix);
1278
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001279 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001280 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001281 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001282 if (is_duration_time(counter))
1283 continue;
1284
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001285 ad.val = ad.ena = ad.run = 0;
1286 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001287 if (!collect_data(counter, aggr_cb, &ad))
1288 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001289 nr = ad.nr;
1290 ena = ad.ena;
1291 run = ad.run;
1292 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001293 if (first && metric_only) {
1294 first = false;
1295 aggr_printout(counter, id, nr);
1296 }
1297 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001298 fprintf(output, "%s", prefix);
1299
Stephane Eranian410136f2013-11-12 17:58:49 +01001300 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001301 printout(id, nr, counter, uval, prefix, run, ena, 1.0,
1302 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001303 if (!metric_only)
1304 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001305 }
Andi Kleen54b50912016-03-03 15:57:36 -08001306 if (metric_only)
1307 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001308 }
1309}
1310
Jin Yao29734552017-12-05 22:03:11 +08001311static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001312{
Jin Yao29734552017-12-05 22:03:11 +08001313 return ((struct perf_aggr_thread_value *)b)->val -
1314 ((struct perf_aggr_thread_value *)a)->val;
1315}
1316
1317static struct perf_aggr_thread_value *sort_aggr_thread(
1318 struct perf_evsel *counter,
1319 int nthreads, int ncpus,
1320 int *ret)
1321{
1322 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001323 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001324 struct perf_aggr_thread_value *buf;
1325
1326 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1327 if (!buf)
1328 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001329
1330 for (thread = 0; thread < nthreads; thread++) {
1331 u64 ena = 0, run = 0, val = 0;
1332
1333 for (cpu = 0; cpu < ncpus; cpu++) {
1334 val += perf_counts(counter->counts, cpu, thread)->val;
1335 ena += perf_counts(counter->counts, cpu, thread)->ena;
1336 run += perf_counts(counter->counts, cpu, thread)->run;
1337 }
1338
Jin Yao29734552017-12-05 22:03:11 +08001339 uval = val * counter->scale;
1340
1341 /*
1342 * Skip value 0 when enabling --per-thread globally,
1343 * otherwise too many 0 output.
1344 */
1345 if (uval == 0.0 && target__has_per_thread(&target))
1346 continue;
1347
1348 buf[i].counter = counter;
1349 buf[i].id = thread;
1350 buf[i].uval = uval;
1351 buf[i].val = val;
1352 buf[i].run = run;
1353 buf[i].ena = ena;
1354 i++;
1355 }
1356
1357 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1358
1359 if (ret)
1360 *ret = i;
1361
1362 return buf;
1363}
1364
1365static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1366{
1367 FILE *output = stat_config.output;
1368 int nthreads = thread_map__nr(counter->threads);
1369 int ncpus = cpu_map__nr(counter->cpus);
1370 int thread, sorted_threads, id;
1371 struct perf_aggr_thread_value *buf;
1372
1373 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads);
1374 if (!buf) {
1375 perror("cannot sort aggr thread");
1376 return;
1377 }
1378
1379 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001380 if (prefix)
1381 fprintf(output, "%s", prefix);
1382
Jin Yao29734552017-12-05 22:03:11 +08001383 id = buf[thread].id;
Jin Yao14e72a22017-12-05 22:03:08 +08001384 if (stat_config.stats)
Jin Yao29734552017-12-05 22:03:11 +08001385 printout(id, 0, buf[thread].counter, buf[thread].uval,
1386 prefix, buf[thread].run, buf[thread].ena, 1.0,
1387 &stat_config.stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001388 else
Jin Yao29734552017-12-05 22:03:11 +08001389 printout(id, 0, buf[thread].counter, buf[thread].uval,
1390 prefix, buf[thread].run, buf[thread].ena, 1.0,
1391 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001392 fputc('\n', output);
1393 }
Jin Yao29734552017-12-05 22:03:11 +08001394
1395 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001396}
1397
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001398struct caggr_data {
1399 double avg, avg_enabled, avg_running;
1400};
1401
1402static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1403 bool first __maybe_unused)
1404{
1405 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001406 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001407
1408 cd->avg += avg_stats(&ps->res_stats[0]);
1409 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1410 cd->avg_running += avg_stats(&ps->res_stats[2]);
1411}
1412
Ingo Molnar42202dd2009-06-13 14:57:28 +02001413/*
1414 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001415 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001416 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001417static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001418{
Jiri Olsa58215222015-07-21 14:31:24 +02001419 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001420 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001421 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001422
Andi Kleen430daf22017-03-20 13:17:00 -07001423 if (!collect_data(counter, counter_aggr_cb, &cd))
1424 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001425
Andi Kleen54b50912016-03-03 15:57:36 -08001426 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001427 fprintf(output, "%s", prefix);
1428
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001429 uval = cd.avg * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001430 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
1431 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001432 if (!metric_only)
1433 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001434}
1435
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001436static void counter_cb(struct perf_evsel *counter, void *data,
1437 bool first __maybe_unused)
1438{
1439 struct aggr_data *ad = data;
1440
1441 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1442 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1443 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1444}
1445
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001446/*
1447 * Print out the results of a single counter:
1448 * does not use aggregated count in system-wide
1449 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001450static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001451{
Jiri Olsa58215222015-07-21 14:31:24 +02001452 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001453 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001454 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001455 int cpu;
1456
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001457 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001458 struct aggr_data ad = { .cpu = cpu };
1459
Andi Kleen430daf22017-03-20 13:17:00 -07001460 if (!collect_data(counter, counter_cb, &ad))
1461 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001462 val = ad.val;
1463 ena = ad.ena;
1464 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001465
1466 if (prefix)
1467 fprintf(output, "%s", prefix);
1468
Stephane Eranian410136f2013-11-12 17:58:49 +01001469 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001470 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1471 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001472
Stephane Eranian4aa90152011-08-15 22:22:33 +02001473 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001474 }
1475}
1476
Andi Kleen206cab62016-03-03 15:57:37 -08001477static void print_no_aggr_metric(char *prefix)
1478{
1479 int cpu;
1480 int nrcpus = 0;
1481 struct perf_evsel *counter;
1482 u64 ena, run, val;
1483 double uval;
1484
1485 nrcpus = evsel_list->cpus->nr;
1486 for (cpu = 0; cpu < nrcpus; cpu++) {
1487 bool first = true;
1488
1489 if (prefix)
1490 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001491 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001492 if (is_duration_time(counter))
1493 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001494 if (first) {
1495 aggr_printout(counter, cpu, 0);
1496 first = false;
1497 }
1498 val = perf_counts(counter->counts, cpu, 0)->val;
1499 ena = perf_counts(counter->counts, cpu, 0)->ena;
1500 run = perf_counts(counter->counts, cpu, 0)->run;
1501
1502 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001503 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1504 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001505 }
1506 fputc('\n', stat_config.output);
1507 }
1508}
1509
Andi Kleen54b50912016-03-03 15:57:36 -08001510static int aggr_header_lens[] = {
1511 [AGGR_CORE] = 18,
1512 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001513 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001514 [AGGR_THREAD] = 24,
1515 [AGGR_GLOBAL] = 0,
1516};
1517
Andi Kleenc51fd632016-05-24 12:52:39 -07001518static const char *aggr_header_csv[] = {
1519 [AGGR_CORE] = "core,cpus,",
1520 [AGGR_SOCKET] = "socket,cpus",
1521 [AGGR_NONE] = "cpu,",
1522 [AGGR_THREAD] = "comm-pid,",
1523 [AGGR_GLOBAL] = ""
1524};
1525
Andi Kleen41c8ca22016-05-24 12:52:38 -07001526static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001527{
1528 struct perf_stat_output_ctx out;
1529 struct perf_evsel *counter;
1530 struct outstate os = {
1531 .fh = stat_config.output
1532 };
1533
1534 if (prefix)
1535 fprintf(stat_config.output, "%s", prefix);
1536
Andi Kleen41c8ca22016-05-24 12:52:38 -07001537 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001538 fprintf(stat_config.output, "%*s",
1539 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001540 if (csv_output) {
1541 if (stat_config.interval)
1542 fputs("time,", stat_config.output);
1543 fputs(aggr_header_csv[stat_config.aggr_mode],
1544 stat_config.output);
1545 }
Andi Kleen54b50912016-03-03 15:57:36 -08001546
1547 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001548 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001549 if (is_duration_time(counter))
1550 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001551 os.evsel = counter;
1552 out.ctx = &os;
1553 out.print_metric = print_metric_header;
1554 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001555 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001556 os.evsel = counter;
1557 perf_stat__print_shadow_stats(counter, 0,
1558 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001559 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001560 &metric_events,
1561 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001562 }
1563 fputc('\n', stat_config.output);
1564}
1565
Jiri Olsad4f63a42015-06-26 11:29:26 +02001566static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001567{
Jiri Olsa58215222015-07-21 14:31:24 +02001568 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001569 static int num_print_interval;
1570
Jiri Olsa9660e082018-06-07 00:15:06 +02001571 if (interval_clear)
1572 puts(CONSOLE_CLEAR);
1573
Jiri Olsad4f63a42015-06-26 11:29:26 +02001574 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1575
Jiri Olsa9660e082018-06-07 00:15:06 +02001576 if ((num_print_interval == 0 && !csv_output) || interval_clear) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001577 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001578 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001579 fprintf(output, "# time socket cpus");
1580 if (!metric_only)
1581 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001582 break;
1583 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001584 fprintf(output, "# time core cpus");
1585 if (!metric_only)
1586 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001587 break;
1588 case AGGR_NONE:
Jiri Olsaf5155722018-06-07 00:15:08 +02001589 fprintf(output, "# time CPU ");
Andi Kleen41c8ca22016-05-24 12:52:38 -07001590 if (!metric_only)
1591 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001592 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001593 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001594 fprintf(output, "# time comm-pid");
1595 if (!metric_only)
1596 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001597 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001598 case AGGR_GLOBAL:
1599 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001600 fprintf(output, "# time");
1601 if (!metric_only)
1602 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001603 case AGGR_UNSET:
1604 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001605 }
1606 }
1607
Jiri Olsac818cc02018-07-02 15:42:02 +02001608 if ((num_print_interval == 0 || interval_clear) && metric_only)
Andi Kleen41c8ca22016-05-24 12:52:38 -07001609 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001610 if (++num_print_interval == 25)
1611 num_print_interval = 0;
1612}
1613
1614static void print_header(int argc, const char **argv)
1615{
Jiri Olsa58215222015-07-21 14:31:24 +02001616 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001617 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001618
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001619 fflush(stdout);
1620
Stephane Eraniand7470b62010-12-01 18:49:05 +02001621 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001622 fprintf(output, "\n");
1623 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001624 if (target.system_wide)
1625 fprintf(output, "\'system wide");
1626 else if (target.cpu_list)
1627 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001628 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001629 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1630 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001631 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001632 } else if (target.pid)
1633 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001634 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001635 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001636
Stephane Eranian4aa90152011-08-15 22:22:33 +02001637 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001638 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001639 fprintf(output, " (%d runs)", run_count);
1640 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001641 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001642}
1643
Jiri Olsabc22de92018-04-23 11:08:20 +02001644static int get_precision(double num)
1645{
1646 if (num > 1)
1647 return 0;
1648
1649 return lround(ceil(-log10(num)));
1650}
1651
Jiri Olsae55c14a2018-04-23 11:08:21 +02001652static void print_table(FILE *output, int precision, double avg)
1653{
1654 char tmp[64];
1655 int idx, indent = 0;
1656
1657 scnprintf(tmp, 64, " %17.*f", precision, avg);
1658 while (tmp[indent] == ' ')
1659 indent++;
1660
1661 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1662
1663 for (idx = 0; idx < run_count; idx++) {
1664 double run = (double) walltime_run[idx] / NSEC_PER_SEC;
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001665 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001666
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001667 fprintf(output, " %17.*f (%+.*f) ",
Jiri Olsae55c14a2018-04-23 11:08:21 +02001668 precision, run, precision, run - avg);
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001669
1670 for (h = 0; h < n; h++)
1671 fprintf(output, "#");
1672
1673 fprintf(output, "\n");
Jiri Olsae55c14a2018-04-23 11:08:21 +02001674 }
1675
1676 fprintf(output, "\n%*s# Final result:\n", indent, "");
1677}
1678
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001679static double timeval2double(struct timeval *t)
1680{
1681 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1682}
1683
Jiri Olsad4f63a42015-06-26 11:29:26 +02001684static void print_footer(void)
1685{
Jiri Olsabc22de92018-04-23 11:08:20 +02001686 double avg = avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsa58215222015-07-21 14:31:24 +02001687 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001688 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001689
Jiri Olsad4f63a42015-06-26 11:29:26 +02001690 if (!null_run)
1691 fprintf(output, "\n");
Jiri Olsabc22de92018-04-23 11:08:20 +02001692
1693 if (run_count == 1) {
1694 fprintf(output, " %17.9f seconds time elapsed", avg);
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001695
1696 if (ru_display) {
1697 double ru_utime = timeval2double(&ru_data.ru_utime);
1698 double ru_stime = timeval2double(&ru_data.ru_stime);
1699
1700 fprintf(output, "\n\n");
1701 fprintf(output, " %17.9f seconds user\n", ru_utime);
1702 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1703 }
Jiri Olsabc22de92018-04-23 11:08:20 +02001704 } else {
1705 double sd = stddev_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
1706 /*
1707 * Display at most 2 more significant
1708 * digits than the stddev inaccuracy.
1709 */
1710 int precision = get_precision(sd) + 2;
1711
Jiri Olsae55c14a2018-04-23 11:08:21 +02001712 if (walltime_run_table)
1713 print_table(output, precision, avg);
1714
Jiri Olsabc22de92018-04-23 11:08:20 +02001715 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1716 precision, avg, precision, sd);
1717
1718 print_noise_pct(sd, avg);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001719 }
1720 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001721
Andi Kleen918c7b062017-05-22 18:00:16 -07001722 if (print_free_counters_hint &&
1723 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1724 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001725 fprintf(output,
1726"Some events weren't counted. Try disabling the NMI watchdog:\n"
1727" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1728" perf stat ...\n"
1729" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001730
1731 if (print_mixed_hw_group_error)
1732 fprintf(output,
1733 "The events in group usually have to be from "
1734 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001735}
1736
1737static void print_counters(struct timespec *ts, int argc, const char **argv)
1738{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001739 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001740 struct perf_evsel *counter;
1741 char buf[64], *prefix = NULL;
1742
Jiri Olsa664c98d2015-11-05 15:40:50 +01001743 /* Do not print anything if we record to the pipe. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001744 if (STAT_RECORD && perf_stat.data.is_pipe)
Jiri Olsa664c98d2015-11-05 15:40:50 +01001745 return;
1746
Jiri Olsad4f63a42015-06-26 11:29:26 +02001747 if (interval)
1748 print_interval(prefix = buf, ts);
1749 else
1750 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001751
Andi Kleen54b50912016-03-03 15:57:36 -08001752 if (metric_only) {
1753 static int num_print_iv;
1754
Andi Kleen41c8ca22016-05-24 12:52:38 -07001755 if (num_print_iv == 0 && !interval)
1756 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001757 if (num_print_iv++ == 25)
1758 num_print_iv = 0;
1759 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1760 fprintf(stat_config.output, "%s", prefix);
1761 }
1762
Jiri Olsa421a50f2015-07-21 14:31:22 +02001763 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001764 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001765 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001766 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001767 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001768 case AGGR_THREAD:
Andi Kleene864c5c2017-08-31 12:40:35 -07001769 evlist__for_each_entry(evsel_list, counter) {
1770 if (is_duration_time(counter))
1771 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001772 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001773 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001774 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001775 case AGGR_GLOBAL:
Andi Kleene864c5c2017-08-31 12:40:35 -07001776 evlist__for_each_entry(evsel_list, counter) {
1777 if (is_duration_time(counter))
1778 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001779 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001780 }
Andi Kleen54b50912016-03-03 15:57:36 -08001781 if (metric_only)
1782 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001783 break;
1784 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001785 if (metric_only)
1786 print_no_aggr_metric(prefix);
1787 else {
Andi Kleene864c5c2017-08-31 12:40:35 -07001788 evlist__for_each_entry(evsel_list, counter) {
1789 if (is_duration_time(counter))
1790 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001791 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001792 }
Andi Kleen206cab62016-03-03 15:57:37 -08001793 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001794 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001795 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001796 default:
1797 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001798 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001799
Jiri Olsad4f63a42015-06-26 11:29:26 +02001800 if (!interval && !csv_output)
1801 print_footer();
1802
Jiri Olsa58215222015-07-21 14:31:24 +02001803 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001804}
1805
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001806static volatile int signr = -1;
1807
Ingo Molnar52425192009-05-26 09:17:18 +02001808static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001809{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001810 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001811 done = 1;
1812
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001813 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001814 /*
1815 * render child_pid harmless
1816 * won't send SIGTERM to a random
1817 * process in case of race condition
1818 * and fast PID recycling
1819 */
1820 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001821}
1822
1823static void sig_atexit(void)
1824{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001825 sigset_t set, oset;
1826
1827 /*
1828 * avoid race condition with SIGCHLD handler
1829 * in skip_signal() which is modifying child_pid
1830 * goal is to avoid send SIGTERM to a random
1831 * process
1832 */
1833 sigemptyset(&set);
1834 sigaddset(&set, SIGCHLD);
1835 sigprocmask(SIG_BLOCK, &set, &oset);
1836
Chris Wilson933da832009-10-04 01:35:01 +01001837 if (child_pid != -1)
1838 kill(child_pid, SIGTERM);
1839
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001840 sigprocmask(SIG_SETMASK, &oset, NULL);
1841
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001842 if (signr == -1)
1843 return;
1844
1845 signal(signr, SIG_DFL);
1846 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001847}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001848
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001849static int stat__set_big_num(const struct option *opt __maybe_unused,
1850 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001851{
1852 big_num_opt = unset ? 0 : 1;
1853 return 0;
1854}
1855
Andi Kleen44b1e602016-05-30 12:49:42 -03001856static int enable_metric_only(const struct option *opt __maybe_unused,
1857 const char *s __maybe_unused, int unset)
1858{
1859 force_metric_only = true;
1860 metric_only = !unset;
1861 return 0;
1862}
1863
Andi Kleenb18f3e32017-08-31 12:40:31 -07001864static int parse_metric_groups(const struct option *opt,
1865 const char *str,
1866 int unset __maybe_unused)
1867{
1868 return metricgroup__parse_groups(opt, str, &metric_events);
1869}
1870
Jiri Olsae0547312015-11-05 15:40:45 +01001871static const struct option stat_options[] = {
1872 OPT_BOOLEAN('T', "transaction", &transaction_run,
1873 "hardware transaction statistics"),
1874 OPT_CALLBACK('e', "event", &evsel_list, "event",
1875 "event selector. use 'perf list' to list available events",
1876 parse_events_option),
1877 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1878 "event filter", parse_filter),
Jiri Olsa5698f262018-08-30 08:32:12 +02001879 OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
Jiri Olsae0547312015-11-05 15:40:45 +01001880 "child tasks do not inherit counters"),
1881 OPT_STRING('p', "pid", &target.pid, "pid",
1882 "stat events on existing process id"),
1883 OPT_STRING('t', "tid", &target.tid, "tid",
1884 "stat events on existing thread id"),
1885 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1886 "system-wide collection from all CPUs"),
1887 OPT_BOOLEAN('g', "group", &group,
1888 "put the counters into a counter group"),
1889 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1890 OPT_INCR('v', "verbose", &verbose,
1891 "be more verbose (show counter open errors, etc)"),
1892 OPT_INTEGER('r', "repeat", &run_count,
1893 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsae55c14a2018-04-23 11:08:21 +02001894 OPT_BOOLEAN(0, "table", &walltime_run_table,
1895 "display details about each run (only with -r option)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001896 OPT_BOOLEAN('n', "null", &null_run,
1897 "null run - dont start any counters"),
1898 OPT_INCR('d', "detailed", &detailed_run,
1899 "detailed run - start a lot of events"),
1900 OPT_BOOLEAN('S', "sync", &sync_run,
1901 "call sync() before starting a run"),
1902 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1903 "print large numbers with thousands\' separators",
1904 stat__set_big_num),
1905 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1906 "list of cpus to monitor in system-wide"),
1907 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1908 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001909 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001910 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1911 "print counts with custom separator"),
1912 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1913 "monitor event in cgroup name only", parse_cgroups),
1914 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1915 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1916 OPT_INTEGER(0, "log-fd", &output_fd,
1917 "log output to fd, instead of stderr"),
1918 OPT_STRING(0, "pre", &pre_cmd, "command",
1919 "command to run prior to the measured command"),
1920 OPT_STRING(0, "post", &post_cmd, "command",
1921 "command to run after to the measured command"),
1922 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03001923 "print counts at regular interval in ms "
1924 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01001925 OPT_INTEGER(0, "interval-count", &stat_config.times,
1926 "print counts for fixed number of times"),
Jiri Olsa9660e082018-06-07 00:15:06 +02001927 OPT_BOOLEAN(0, "interval-clear", &interval_clear,
1928 "clear screen in between new interval"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01001929 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1930 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001931 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1932 "aggregate counts per processor socket", AGGR_SOCKET),
1933 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1934 "aggregate counts per physical processor core", AGGR_CORE),
1935 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1936 "aggregate counts per thread", AGGR_THREAD),
Jiri Olsa728c0ee2018-08-30 08:32:11 +02001937 OPT_UINTEGER('D', "delay", &stat_config.initial_delay,
Jiri Olsae0547312015-11-05 15:40:45 +01001938 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001939 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1940 "Only print computed metrics. No raw values", enable_metric_only),
1941 OPT_BOOLEAN(0, "topdown", &topdown_run,
1942 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001943 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1944 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001945 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1946 "monitor specified metrics or metric groups (separated by ,)",
1947 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01001948 OPT_END()
1949};
1950
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001951static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1952{
1953 return cpu_map__get_socket(map, cpu, NULL);
1954}
1955
1956static int perf_stat__get_core(struct cpu_map *map, int cpu)
1957{
1958 return cpu_map__get_core(map, cpu, NULL);
1959}
1960
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001961static int cpu_map__get_max(struct cpu_map *map)
1962{
1963 int i, max = -1;
1964
1965 for (i = 0; i < map->nr; i++) {
1966 if (map->map[i] > max)
1967 max = map->map[i];
1968 }
1969
1970 return max;
1971}
1972
1973static struct cpu_map *cpus_aggr_map;
1974
1975static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1976{
1977 int cpu;
1978
1979 if (idx >= map->nr)
1980 return -1;
1981
1982 cpu = map->map[idx];
1983
1984 if (cpus_aggr_map->map[cpu] == -1)
1985 cpus_aggr_map->map[cpu] = get_id(map, idx);
1986
1987 return cpus_aggr_map->map[cpu];
1988}
1989
1990static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1991{
1992 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1993}
1994
1995static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1996{
1997 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1998}
1999
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002000static int perf_stat_init_aggr_mode(void)
2001{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002002 int nr;
2003
Jiri Olsa421a50f2015-07-21 14:31:22 +02002004 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002005 case AGGR_SOCKET:
2006 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
2007 perror("cannot build socket map");
2008 return -1;
2009 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002010 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002011 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002012 case AGGR_CORE:
2013 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
2014 perror("cannot build core map");
2015 return -1;
2016 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002017 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002018 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002019 case AGGR_NONE:
2020 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002021 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002022 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002023 default:
2024 break;
2025 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002026
2027 /*
2028 * The evsel_list->cpus is the base we operate on,
2029 * taking the highest cpu number to be the size of
2030 * the aggregation translate cpumap.
2031 */
2032 nr = cpu_map__get_max(evsel_list->cpus);
2033 cpus_aggr_map = cpu_map__empty_new(nr + 1);
2034 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002035}
2036
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002037static void perf_stat__exit_aggr_mode(void)
2038{
2039 cpu_map__put(aggr_map);
2040 cpu_map__put(cpus_aggr_map);
2041 aggr_map = NULL;
2042 cpus_aggr_map = NULL;
2043}
2044
Jiri Olsa68d702f2015-11-05 15:40:58 +01002045static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2046{
2047 int cpu;
2048
2049 if (idx > map->nr)
2050 return -1;
2051
2052 cpu = map->map[idx];
2053
Jan Stancekda8a58b2017-02-17 12:10:26 +01002054 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002055 return -1;
2056
2057 return cpu;
2058}
2059
2060static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2061{
2062 struct perf_env *env = data;
2063 int cpu = perf_env__get_cpu(env, map, idx);
2064
2065 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2066}
2067
2068static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2069{
2070 struct perf_env *env = data;
2071 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2072
2073 if (cpu != -1) {
2074 int socket_id = env->cpu[cpu].socket_id;
2075
2076 /*
2077 * Encode socket in upper 16 bits
2078 * core_id is relative to socket, and
2079 * we need a global id. So we combine
2080 * socket + core id.
2081 */
2082 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2083 }
2084
2085 return core;
2086}
2087
2088static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2089 struct cpu_map **sockp)
2090{
2091 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2092}
2093
2094static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2095 struct cpu_map **corep)
2096{
2097 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2098}
2099
2100static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2101{
2102 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2103}
2104
2105static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2106{
2107 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2108}
2109
2110static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2111{
2112 struct perf_env *env = &st->session->header.env;
2113
2114 switch (stat_config.aggr_mode) {
2115 case AGGR_SOCKET:
2116 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2117 perror("cannot build socket map");
2118 return -1;
2119 }
2120 aggr_get_id = perf_stat__get_socket_file;
2121 break;
2122 case AGGR_CORE:
2123 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2124 perror("cannot build core map");
2125 return -1;
2126 }
2127 aggr_get_id = perf_stat__get_core_file;
2128 break;
2129 case AGGR_NONE:
2130 case AGGR_GLOBAL:
2131 case AGGR_THREAD:
2132 case AGGR_UNSET:
2133 default:
2134 break;
2135 }
2136
2137 return 0;
2138}
2139
Andi Kleen44b1e602016-05-30 12:49:42 -03002140static int topdown_filter_events(const char **attr, char **str, bool use_group)
2141{
2142 int off = 0;
2143 int i;
2144 int len = 0;
2145 char *s;
2146
2147 for (i = 0; attr[i]; i++) {
2148 if (pmu_have_event("cpu", attr[i])) {
2149 len += strlen(attr[i]) + 1;
2150 attr[i - off] = attr[i];
2151 } else
2152 off++;
2153 }
2154 attr[i - off] = NULL;
2155
2156 *str = malloc(len + 1 + 2);
2157 if (!*str)
2158 return -1;
2159 s = *str;
2160 if (i - off == 0) {
2161 *s = 0;
2162 return 0;
2163 }
2164 if (use_group)
2165 *s++ = '{';
2166 for (i = 0; attr[i]; i++) {
2167 strcpy(s, attr[i]);
2168 s += strlen(s);
2169 *s++ = ',';
2170 }
2171 if (use_group) {
2172 s[-1] = '}';
2173 *s = 0;
2174 } else
2175 s[-1] = 0;
2176 return 0;
2177}
2178
2179__weak bool arch_topdown_check_group(bool *warn)
2180{
2181 *warn = false;
2182 return false;
2183}
2184
2185__weak void arch_topdown_group_warn(void)
2186{
2187}
2188
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002189/*
2190 * Add default attributes, if there were no attributes specified or
2191 * if -d/--detailed, -d -d or -d -d -d is used:
2192 */
2193static int add_default_attributes(void)
2194{
Andi Kleen44b1e602016-05-30 12:49:42 -03002195 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002196 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002197
2198 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2199 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2200 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2201 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2202
2203 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002204};
2205 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002206 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002207};
2208 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002209 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002210};
2211 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002212 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2213 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2214 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2215
2216};
2217
2218/*
2219 * Detailed stats (-d), covering the L1 and last level data caches:
2220 */
2221 struct perf_event_attr detailed_attrs[] = {
2222
2223 { .type = PERF_TYPE_HW_CACHE,
2224 .config =
2225 PERF_COUNT_HW_CACHE_L1D << 0 |
2226 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2227 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2228
2229 { .type = PERF_TYPE_HW_CACHE,
2230 .config =
2231 PERF_COUNT_HW_CACHE_L1D << 0 |
2232 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2233 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2234
2235 { .type = PERF_TYPE_HW_CACHE,
2236 .config =
2237 PERF_COUNT_HW_CACHE_LL << 0 |
2238 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2239 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2240
2241 { .type = PERF_TYPE_HW_CACHE,
2242 .config =
2243 PERF_COUNT_HW_CACHE_LL << 0 |
2244 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2245 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2246};
2247
2248/*
2249 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2250 */
2251 struct perf_event_attr very_detailed_attrs[] = {
2252
2253 { .type = PERF_TYPE_HW_CACHE,
2254 .config =
2255 PERF_COUNT_HW_CACHE_L1I << 0 |
2256 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2257 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2258
2259 { .type = PERF_TYPE_HW_CACHE,
2260 .config =
2261 PERF_COUNT_HW_CACHE_L1I << 0 |
2262 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2263 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2264
2265 { .type = PERF_TYPE_HW_CACHE,
2266 .config =
2267 PERF_COUNT_HW_CACHE_DTLB << 0 |
2268 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2269 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2270
2271 { .type = PERF_TYPE_HW_CACHE,
2272 .config =
2273 PERF_COUNT_HW_CACHE_DTLB << 0 |
2274 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2275 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2276
2277 { .type = PERF_TYPE_HW_CACHE,
2278 .config =
2279 PERF_COUNT_HW_CACHE_ITLB << 0 |
2280 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2281 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2282
2283 { .type = PERF_TYPE_HW_CACHE,
2284 .config =
2285 PERF_COUNT_HW_CACHE_ITLB << 0 |
2286 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2287 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2288
2289};
2290
2291/*
2292 * Very, very detailed stats (-d -d -d), adding prefetch events:
2293 */
2294 struct perf_event_attr very_very_detailed_attrs[] = {
2295
2296 { .type = PERF_TYPE_HW_CACHE,
2297 .config =
2298 PERF_COUNT_HW_CACHE_L1D << 0 |
2299 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2300 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2301
2302 { .type = PERF_TYPE_HW_CACHE,
2303 .config =
2304 PERF_COUNT_HW_CACHE_L1D << 0 |
2305 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2306 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2307};
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002308 struct parse_events_error errinfo;
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002309
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002310 /* Set attrs if no event is selected and !null_run: */
2311 if (null_run)
2312 return 0;
2313
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002314 if (transaction_run) {
Thomas Richter742d92f2018-06-26 09:17:01 +02002315 /* Handle -T as -M transaction. Once platform specific metrics
2316 * support has been added to the json files, all archictures
2317 * will use this approach. To determine transaction support
2318 * on an architecture test for such a metric name.
2319 */
2320 if (metricgroup__has_metric("transaction")) {
2321 struct option opt = { .value = &evsel_list };
2322
2323 return metricgroup__parse_groups(&opt, "transaction",
2324 &metric_events);
2325 }
2326
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002327 if (pmu_have_event("cpu", "cycles-ct") &&
2328 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002329 err = parse_events(evsel_list, transaction_attrs,
2330 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002331 else
Thomas Richterfca323402018-03-08 15:57:35 +01002332 err = parse_events(evsel_list,
2333 transaction_limited_attrs,
2334 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002335 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002336 fprintf(stderr, "Cannot set up transaction events\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002337 parse_events_print_error(&errinfo, transaction_attrs);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002338 return -1;
2339 }
2340 return 0;
2341 }
2342
Kan Liangdaefd0b2017-05-26 12:05:38 -07002343 if (smi_cost) {
2344 int smi;
2345
2346 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2347 fprintf(stderr, "freeze_on_smi is not supported.\n");
2348 return -1;
2349 }
2350
2351 if (!smi) {
2352 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2353 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2354 return -1;
2355 }
2356 smi_reset = true;
2357 }
2358
2359 if (pmu_have_event("msr", "aperf") &&
2360 pmu_have_event("msr", "smi")) {
2361 if (!force_metric_only)
2362 metric_only = true;
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002363 err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002364 } else {
2365 fprintf(stderr, "To measure SMI cost, it needs "
2366 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002367 parse_events_print_error(&errinfo, smi_cost_attrs);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002368 return -1;
2369 }
2370 if (err) {
2371 fprintf(stderr, "Cannot set up SMI cost events\n");
2372 return -1;
2373 }
2374 return 0;
2375 }
2376
Andi Kleen44b1e602016-05-30 12:49:42 -03002377 if (topdown_run) {
2378 char *str = NULL;
2379 bool warn = false;
2380
2381 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2382 stat_config.aggr_mode != AGGR_CORE) {
2383 pr_err("top down event configuration requires --per-core mode\n");
2384 return -1;
2385 }
2386 stat_config.aggr_mode = AGGR_CORE;
2387 if (nr_cgroups || !target__has_cpu(&target)) {
2388 pr_err("top down event configuration requires system-wide mode (-a)\n");
2389 return -1;
2390 }
2391
2392 if (!force_metric_only)
2393 metric_only = true;
2394 if (topdown_filter_events(topdown_attrs, &str,
2395 arch_topdown_check_group(&warn)) < 0) {
2396 pr_err("Out of memory\n");
2397 return -1;
2398 }
2399 if (topdown_attrs[0] && str) {
2400 if (warn)
2401 arch_topdown_group_warn();
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002402 err = parse_events(evsel_list, str, &errinfo);
Andi Kleen44b1e602016-05-30 12:49:42 -03002403 if (err) {
2404 fprintf(stderr,
2405 "Cannot set up top down events %s: %d\n",
2406 str, err);
2407 free(str);
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002408 parse_events_print_error(&errinfo, str);
Andi Kleen44b1e602016-05-30 12:49:42 -03002409 return -1;
2410 }
2411 } else {
2412 fprintf(stderr, "System does not support topdown\n");
2413 return -1;
2414 }
2415 free(str);
2416 }
2417
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002418 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002419 if (target__has_cpu(&target))
2420 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2421
Andi Kleen9dec4472016-02-26 16:27:56 -08002422 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2423 return -1;
2424 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2425 if (perf_evlist__add_default_attrs(evsel_list,
2426 frontend_attrs) < 0)
2427 return -1;
2428 }
2429 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2430 if (perf_evlist__add_default_attrs(evsel_list,
2431 backend_attrs) < 0)
2432 return -1;
2433 }
2434 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002435 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002436 }
2437
2438 /* Detailed events get appended to the event list: */
2439
2440 if (detailed_run < 1)
2441 return 0;
2442
2443 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002444 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002445 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002446
2447 if (detailed_run < 2)
2448 return 0;
2449
2450 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002451 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002452 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002453
2454 if (detailed_run < 3)
2455 return 0;
2456
2457 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002458 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002459}
2460
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002461static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002462 "perf stat record [<options>]",
2463 NULL,
2464};
2465
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002466static void init_features(struct perf_session *session)
2467{
2468 int feat;
2469
2470 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2471 perf_header__set_feat(&session->header, feat);
2472
2473 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2474 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2475 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2476 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2477}
2478
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002479static int __cmd_record(int argc, const char **argv)
2480{
2481 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002482 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002483
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002484 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002485 PARSE_OPT_STOP_AT_NON_OPTION);
2486
2487 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002488 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002489
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002490 if (run_count != 1 || forever) {
2491 pr_err("Cannot use -r option with perf stat record.\n");
2492 return -1;
2493 }
2494
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002495 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002496 if (session == NULL) {
2497 pr_err("Perf session creation failed.\n");
2498 return -1;
2499 }
2500
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002501 init_features(session);
2502
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002503 session->evlist = evsel_list;
2504 perf_stat.session = session;
2505 perf_stat.record = true;
2506 return argc;
2507}
2508
Jiri Olsaa56f9392015-11-05 15:40:59 +01002509static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2510 union perf_event *event,
2511 struct perf_session *session)
2512{
Andi Kleene3b03b62016-05-05 16:04:03 -07002513 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002514 struct perf_evsel *counter;
2515 struct timespec tsh, *ts = NULL;
2516 const char **argv = session->header.env.cmdline_argv;
2517 int argc = session->header.env.nr_cmdline;
2518
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002519 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002520 perf_stat_process_counter(&stat_config, counter);
2521
Andi Kleene3b03b62016-05-05 16:04:03 -07002522 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2523 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002524
Andi Kleene3b03b62016-05-05 16:04:03 -07002525 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002526 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2527 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002528 ts = &tsh;
2529 }
2530
2531 print_counters(ts, argc, argv);
2532 return 0;
2533}
2534
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002535static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002536int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002537 union perf_event *event,
2538 struct perf_session *session __maybe_unused)
2539{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002540 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2541
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002542 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002543
Jiri Olsa89af4e02015-11-05 15:41:02 +01002544 if (cpu_map__empty(st->cpus)) {
2545 if (st->aggr_mode != AGGR_UNSET)
2546 pr_warning("warning: processing task data, aggregation mode not set\n");
2547 return 0;
2548 }
2549
2550 if (st->aggr_mode != AGGR_UNSET)
2551 stat_config.aggr_mode = st->aggr_mode;
2552
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002553 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002554 perf_stat_init_aggr_mode();
2555 else
2556 perf_stat_init_aggr_mode_file(st);
2557
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002558 return 0;
2559}
2560
Jiri Olsa1975d362015-11-05 15:40:56 +01002561static int set_maps(struct perf_stat *st)
2562{
2563 if (!st->cpus || !st->threads)
2564 return 0;
2565
2566 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2567 return -EINVAL;
2568
2569 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2570
2571 if (perf_evlist__alloc_stats(evsel_list, true))
2572 return -ENOMEM;
2573
2574 st->maps_allocated = true;
2575 return 0;
2576}
2577
2578static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002579int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002580 union perf_event *event,
2581 struct perf_session *session __maybe_unused)
2582{
2583 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2584
2585 if (st->threads) {
2586 pr_warning("Extra thread map event, ignoring.\n");
2587 return 0;
2588 }
2589
2590 st->threads = thread_map__new_event(&event->thread_map);
2591 if (!st->threads)
2592 return -ENOMEM;
2593
2594 return set_maps(st);
2595}
2596
2597static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002598int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002599 union perf_event *event,
2600 struct perf_session *session __maybe_unused)
2601{
2602 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2603 struct cpu_map *cpus;
2604
2605 if (st->cpus) {
2606 pr_warning("Extra cpu map event, ignoring.\n");
2607 return 0;
2608 }
2609
2610 cpus = cpu_map__new_data(&event->cpu_map.data);
2611 if (!cpus)
2612 return -ENOMEM;
2613
2614 st->cpus = cpus;
2615 return set_maps(st);
2616}
2617
Jin Yao56739442017-12-05 22:03:07 +08002618static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2619{
2620 int i;
2621
2622 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2623 if (!config->stats)
2624 return -1;
2625
2626 config->stats_num = nthreads;
2627
2628 for (i = 0; i < nthreads; i++)
2629 runtime_stat__init(&config->stats[i]);
2630
2631 return 0;
2632}
2633
2634static void runtime_stat_delete(struct perf_stat_config *config)
2635{
2636 int i;
2637
2638 if (!config->stats)
2639 return;
2640
2641 for (i = 0; i < config->stats_num; i++)
2642 runtime_stat__exit(&config->stats[i]);
2643
2644 free(config->stats);
2645}
2646
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002647static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002648 "perf stat report [<options>]",
2649 NULL,
2650};
2651
2652static struct perf_stat perf_stat = {
2653 .tool = {
2654 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002655 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002656 .thread_map = process_thread_map_event,
2657 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002658 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002659 .stat = perf_event__process_stat_event,
2660 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002661 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002662 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002663};
2664
2665static int __cmd_report(int argc, const char **argv)
2666{
2667 struct perf_session *session;
2668 const struct option options[] = {
2669 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002670 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2671 "aggregate counts per processor socket", AGGR_SOCKET),
2672 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2673 "aggregate counts per physical processor core", AGGR_CORE),
2674 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2675 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002676 OPT_END()
2677 };
2678 struct stat st;
2679 int ret;
2680
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002681 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002682
2683 if (!input_name || !strlen(input_name)) {
2684 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2685 input_name = "-";
2686 else
2687 input_name = "perf.data";
2688 }
2689
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002690 perf_stat.data.file.path = input_name;
2691 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002692
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002693 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002694 if (session == NULL)
2695 return -1;
2696
2697 perf_stat.session = session;
2698 stat_config.output = stderr;
2699 evsel_list = session->evlist;
2700
2701 ret = perf_session__process_events(session);
2702 if (ret)
2703 return ret;
2704
2705 perf_session__delete(session);
2706 return 0;
2707}
2708
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002709static void setup_system_wide(int forks)
2710{
2711 /*
2712 * Make system wide (-a) the default target if
2713 * no target was specified and one of following
2714 * conditions is met:
2715 *
2716 * - there's no workload specified
2717 * - there is workload specified but all requested
2718 * events are system wide events
2719 */
2720 if (!target__none(&target))
2721 return;
2722
2723 if (!forks)
2724 target.system_wide = true;
2725 else {
2726 struct perf_evsel *counter;
2727
2728 evlist__for_each_entry(evsel_list, counter) {
2729 if (!counter->system_wide)
2730 return;
2731 }
2732
2733 if (evsel_list->nr_entries)
2734 target.system_wide = true;
2735 }
2736}
2737
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002738int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002739{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002740 const char * const stat_usage[] = {
2741 "perf stat [<options>] [<command>]",
2742 NULL
2743 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002744 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002745 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002746 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002747 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002748 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002749
Stephane Eranian5af52b52010-05-18 15:00:01 +02002750 setlocale(LC_ALL, "");
2751
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002752 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002753 if (evsel_list == NULL)
2754 return -ENOMEM;
2755
Wang Nan1669e502016-02-19 11:43:58 +00002756 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002757 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2758 (const char **) stat_usage,
2759 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002760 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002761 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002762
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002763 if (csv_sep) {
2764 csv_output = true;
2765 if (!strcmp(csv_sep, "\\t"))
2766 csv_sep = "\t";
2767 } else
2768 csv_sep = DEFAULT_SEPARATOR;
2769
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002770 if (argc && !strncmp(argv[0], "rec", 3)) {
2771 argc = __cmd_record(argc, argv);
2772 if (argc < 0)
2773 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002774 } else if (argc && !strncmp(argv[0], "rep", 3))
2775 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002776
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002777 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002778 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002779
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002780 /*
2781 * For record command the -o is already taken care of.
2782 */
2783 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002784 output = NULL;
2785
Jim Cromie56f3bae2011-09-07 17:14:00 -06002786 if (output_name && output_fd) {
2787 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002788 parse_options_usage(stat_usage, stat_options, "o", 1);
2789 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002790 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002791 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002792
Andi Kleen54b50912016-03-03 15:57:36 -08002793 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2794 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2795 goto out;
2796 }
2797
Andi Kleen54b50912016-03-03 15:57:36 -08002798 if (metric_only && run_count > 1) {
2799 fprintf(stderr, "--metric-only is not supported with -r\n");
2800 goto out;
2801 }
2802
Jiri Olsae55c14a2018-04-23 11:08:21 +02002803 if (walltime_run_table && run_count <= 1) {
2804 fprintf(stderr, "--table is only supported with -r\n");
2805 parse_options_usage(stat_usage, stat_options, "r", 1);
2806 parse_options_usage(NULL, stat_options, "table", 0);
2807 goto out;
2808 }
2809
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002810 if (output_fd < 0) {
2811 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002812 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002813 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002814 }
2815
Stephane Eranian4aa90152011-08-15 22:22:33 +02002816 if (!output) {
2817 struct timespec tm;
2818 mode = append_file ? "a" : "w";
2819
2820 output = fopen(output_name, mode);
2821 if (!output) {
2822 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002823 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002824 }
2825 clock_gettime(CLOCK_REALTIME, &tm);
2826 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002827 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002828 mode = append_file ? "a" : "w";
2829 output = fdopen(output_fd, mode);
2830 if (!output) {
2831 perror("Failed opening logfd");
2832 return -errno;
2833 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002834 }
2835
Jiri Olsa58215222015-07-21 14:31:24 +02002836 stat_config.output = output;
2837
Stephane Eraniand7470b62010-12-01 18:49:05 +02002838 /*
2839 * let the spreadsheet do the pretty-printing
2840 */
2841 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002842 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002843 if (big_num_opt == 1) {
2844 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002845 parse_options_usage(stat_usage, stat_options, "B", 1);
2846 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002847 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002848 } else /* Nope, so disable big number formatting */
2849 big_num = false;
2850 } else if (big_num_opt == 0) /* User passed --no-big-num */
2851 big_num = false;
2852
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002853 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002854
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002855 /*
2856 * Display user/system times only for single
2857 * run and when there's specified tracee.
2858 */
2859 if ((run_count == 1) && target__none(&target))
2860 ru_display = true;
2861
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002862 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002863 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002864 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002865 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002866 } else if (run_count == 0) {
2867 forever = true;
2868 run_count = 1;
2869 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002870
Jiri Olsae55c14a2018-04-23 11:08:21 +02002871 if (walltime_run_table) {
2872 walltime_run = zalloc(run_count * sizeof(walltime_run[0]));
2873 if (!walltime_run) {
2874 pr_err("failed to setup -r option");
2875 goto out;
2876 }
2877 }
2878
Jin Yao1d9f8d12017-12-05 22:03:10 +08002879 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2880 !target__has_task(&target)) {
2881 if (!target.system_wide || target.cpu_list) {
2882 fprintf(stderr, "The --per-thread option is only "
2883 "available when monitoring via -p -t -a "
2884 "options or only --per-thread.\n");
2885 parse_options_usage(NULL, stat_options, "p", 1);
2886 parse_options_usage(NULL, stat_options, "t", 1);
2887 goto out;
2888 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002889 }
2890
2891 /*
2892 * no_aggr, cgroup are for system-wide only
2893 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2894 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002895 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2896 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002897 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002898 fprintf(stderr, "both cgroup and no-aggregation "
2899 "modes only available in system-wide mode\n");
2900
Jiri Olsae0547312015-11-05 15:40:45 +01002901 parse_options_usage(stat_usage, stat_options, "G", 1);
2902 parse_options_usage(NULL, stat_options, "A", 1);
2903 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002904 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002905 }
2906
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002907 if (add_default_attributes())
2908 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002909
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002910 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002911
Jin Yao1d9f8d12017-12-05 22:03:10 +08002912 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2913 target.per_thread = true;
2914
Namhyung Kim77a6f012012-05-07 14:09:04 +09002915 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002916 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002917 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002918 parse_options_usage(stat_usage, stat_options, "p", 1);
2919 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002920 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002921 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002922 parse_options_usage(stat_usage, stat_options, "C", 1);
2923 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002924 }
2925 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002926 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002927
2928 /*
2929 * Initialize thread_map with comm names,
2930 * so we could print it out on output.
2931 */
Jin Yao56739442017-12-05 22:03:07 +08002932 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002933 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08002934 if (target.system_wide) {
2935 if (runtime_stat_new(&stat_config,
2936 thread_map__nr(evsel_list->threads))) {
2937 goto out;
2938 }
2939 }
2940 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002941
yuzhoujiandb06a262018-01-29 10:25:22 +01002942 if (stat_config.times && interval)
2943 interval_count = true;
2944 else if (stat_config.times && !interval) {
2945 pr_err("interval-count option should be used together with "
2946 "interval-print.\n");
2947 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2948 parse_options_usage(stat_usage, stat_options, "I", 1);
2949 goto out;
2950 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002951
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002952 if (timeout && timeout < 100) {
2953 if (timeout < 10) {
2954 pr_err("timeout must be >= 10ms.\n");
2955 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2956 goto out;
2957 } else
2958 pr_warning("timeout < 100ms. "
2959 "The overhead percentage could be high in some cases. "
2960 "Please proceed with caution.\n");
2961 }
2962 if (timeout && interval) {
2963 pr_err("timeout option is not supported with interval-print.\n");
2964 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2965 parse_options_usage(stat_usage, stat_options, "I", 1);
2966 goto out;
2967 }
2968
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002969 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002970 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002971
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002972 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002973 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002974
Ingo Molnar58d7e992009-05-15 11:03:23 +02002975 /*
Jiri Olsa7d9ad162018-08-30 08:32:14 +02002976 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
2977 * while avoiding that older tools show confusing messages.
2978 *
2979 * However for pipe sessions we need to keep it zero,
2980 * because script's perf_evsel__check_attr is triggered
2981 * by attr->sample_type != 0, and we can't run it on
2982 * stat sessions.
2983 */
2984 stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
2985
2986 /*
Ingo Molnar58d7e992009-05-15 11:03:23 +02002987 * We dont want to block the signals - that would cause
2988 * child tasks to inherit that and Ctrl-C would not work.
2989 * What we want is for Ctrl-C to work in the exec()-ed
2990 * task, but being ignored by perf stat itself:
2991 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002992 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002993 if (!forever)
2994 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002995 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002996 signal(SIGALRM, skip_signal);
2997 signal(SIGABRT, skip_signal);
2998
Ingo Molnar42202dd2009-06-13 14:57:28 +02002999 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003000 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09003001 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02003002 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
3003 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02003004
Jiri Olsae55c14a2018-04-23 11:08:21 +02003005 status = run_perf_stat(argc, argv, run_idx);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003006 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02003007 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02003008 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003009 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02003010 }
3011
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003012 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02003013 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003014
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003015 if (STAT_RECORD) {
3016 /*
3017 * We synthesize the kernel mmap record just so that older tools
3018 * don't emit warnings about not being able to resolve symbols
3019 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3020 * a saner message about no samples being in the perf.data file.
3021 *
3022 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01003023 * in header.c at the moment 'perf stat record' gets introduced, which
3024 * is not really needed once we start adding the stat specific PERF_RECORD_
3025 * records, but the need to suppress the kptr_restrict messages in older
3026 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003027 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003028 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003029 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3030 process_synthesized_event,
3031 &perf_stat.session->machines.host);
3032 if (err) {
3033 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3034 "older tools may produce warnings about this file\n.");
3035 }
3036
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003037 if (!interval) {
3038 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3039 pr_err("failed to write stat round event\n");
3040 }
3041
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003042 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003043 perf_stat.session->header.data_size += perf_stat.bytes_written;
3044 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3045 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003046
3047 perf_session__delete(perf_stat.session);
3048 }
3049
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003050 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003051 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003052out:
Jiri Olsae55c14a2018-04-23 11:08:21 +02003053 free(walltime_run);
3054
Kan Liangdaefd0b2017-05-26 12:05:38 -07003055 if (smi_cost && smi_reset)
3056 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3057
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003058 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003059
3060 runtime_stat_delete(&stat_config);
3061
Ingo Molnar42202dd2009-06-13 14:57:28 +02003062 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003063}