blob: f340641fe63a4288104f23dd3dcee51b2dcd107f [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 Olsa82bf3112017-07-26 14:02:06 +0200405static bool perf_evsel__should_store_id(struct perf_evsel *counter)
406{
407 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
408}
409
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700410static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
411{
412 struct perf_evsel *c2, *leader;
413 bool is_open = true;
414
415 leader = evsel->leader;
416 pr_debug("Weak group for %s/%d failed\n",
417 leader->name, leader->nr_members);
418
419 /*
420 * for_each_group_member doesn't work here because it doesn't
421 * include the first entry.
422 */
423 evlist__for_each_entry(evsel_list, c2) {
424 if (c2 == evsel)
425 is_open = false;
426 if (c2->leader == leader) {
427 if (is_open)
428 perf_evsel__close(c2);
429 c2->leader = c2;
430 c2->nr_members = 0;
431 }
432 }
433 return leader;
434}
435
Jiri Olsae55c14a2018-04-23 11:08:21 +0200436static int __run_perf_stat(int argc, const char **argv, int run_idx)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200437{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200438 int interval = stat_config.interval;
yuzhoujiandb06a262018-01-29 10:25:22 +0100439 int times = stat_config.times;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100440 int timeout = stat_config.timeout;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300441 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200442 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100443 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100444 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100445 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200446 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300447 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100448 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600449 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200450
Stephane Eranian13370a92013-01-29 12:47:44 +0100451 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300452 ts.tv_sec = interval / USEC_PER_MSEC;
453 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100454 } else if (timeout) {
455 ts.tv_sec = timeout / USEC_PER_MSEC;
456 ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100457 } else {
458 ts.tv_sec = 1;
459 ts.tv_nsec = 0;
460 }
461
Liming Wang60666c62009-12-31 16:05:50 +0800462 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100463 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300464 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900465 perror("failed to prepare workload");
466 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800467 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900468 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000469 }
470
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200471 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300472 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200473
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300474 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300475try_again:
Jiri Olsad09cefd2018-08-30 08:32:17 +0200476 if (create_perf_stat_counter(counter, &stat_config, &target) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700477
478 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700479 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700480 counter->leader != counter &&
481 counter->weak_group) {
482 counter = perf_evsel__reset_weak_group(counter);
483 goto try_again;
484 }
485
David Ahern979987a2012-05-08 09:29:16 -0600486 /*
487 * PPC returns ENXIO for HW counters until 2.6.37
488 * (behavior changed with commit b0a873e).
489 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100490 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600491 errno == ENOENT || errno == EOPNOTSUPP ||
492 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900493 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600494 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300495 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600496 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400497
498 if ((counter->leader != counter) ||
499 !(counter->leader->nr_members > 1))
500 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300501 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900502 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300503 ui__warning("%s\n", msg);
504 goto try_again;
Jin Yaoab6c79b2018-01-16 23:43:08 +0800505 } else if (target__has_per_thread(&target) &&
506 evsel_list->threads &&
507 evsel_list->threads->err_thread != -1) {
508 /*
509 * For global --per-thread case, skip current
510 * error thread.
511 */
512 if (!thread_map__remove(evsel_list->threads,
513 evsel_list->threads->err_thread)) {
514 evsel_list->threads->err_thread = -1;
515 goto try_again;
516 }
517 }
Ingo Molnarede70292011-04-28 08:48:42 +0200518
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300519 perf_evsel__open_strerror(counter, &target,
520 errno, msg, sizeof(msg));
521 ui__error("%s\n", msg);
522
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200523 if (child_pid != -1)
524 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600525
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200526 return -1;
527 }
David Ahern2cee77c2011-05-30 08:55:59 -0600528 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100529
530 l = strlen(counter->unit);
531 if (l > unit_width)
532 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100533
Jiri Olsa82bf3112017-07-26 14:02:06 +0200534 if (perf_evsel__should_store_id(counter) &&
Jiri Olsa650d6222018-08-30 08:32:16 +0200535 perf_evsel__store_ids(counter, evsel_list))
Jiri Olsa2af46462015-11-05 15:40:49 +0100536 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300537 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200538
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300539 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300540 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300541 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300542 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100543 return -1;
544 }
545
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600546 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300547 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600548 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
549 str_error_r(errno, msg, sizeof(msg)));
550 return -1;
551 }
552
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100553 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100554 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100555
Jiri Olsa664c98d2015-11-05 15:40:50 +0100556 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100557 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100558 } else {
559 err = perf_session__write_header(perf_stat.session, evsel_list,
560 fd, false);
561 }
562
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100563 if (err < 0)
564 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100565
Jiri Olsa1c21e982018-08-30 08:32:21 +0200566 err = perf_stat_synthesize_config(&stat_config, NULL, evsel_list,
Jiri Olsac2c247f2018-08-30 08:32:22 +0200567 process_synthesized_event, is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100568 if (err < 0)
569 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100570 }
571
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200572 /*
573 * Enable counters and exec the command:
574 */
575 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100576 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200577
Liming Wang60666c62009-12-31 16:05:50 +0800578 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900579 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100580 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900581
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100582 if (interval || timeout) {
Stephane Eranian13370a92013-01-29 12:47:44 +0100583 while (!waitpid(child_pid, &status, WNOHANG)) {
584 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100585 if (timeout)
586 break;
Jiri Olsaba411a92015-06-26 11:29:24 +0200587 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100588 if (interval_count && !(--times))
589 break;
Stephane Eranian13370a92013-01-29 12:47:44 +0100590 }
591 }
Jiri Olsa0ce2da12018-06-05 14:13:13 +0200592 wait4(child_pid, &status, 0, &ru_data);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300593
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300594 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300595 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300596 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300597 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300598 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300599
Andi Kleen33e49ea2011-09-15 14:31:40 -0700600 if (WIFSIGNALED(status))
601 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800602 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100603 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100604 while (!done) {
605 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100606 if (timeout)
607 break;
yuzhoujiandb06a262018-01-29 10:25:22 +0100608 if (interval) {
Jiri Olsaba411a92015-06-26 11:29:24 +0200609 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100610 if (interval_count && !(--times))
611 break;
612 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100613 }
Liming Wang60666c62009-12-31 16:05:50 +0800614 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200615
Mark Rutland3df33ef2016-08-09 14:04:29 +0100616 disable_counters();
617
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200618 t1 = rdclock();
619
Jiri Olsae55c14a2018-04-23 11:08:21 +0200620 if (walltime_run_table)
621 walltime_run[run_idx] = t1 - t0;
622
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200623 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200624
Mark Rutland3df33ef2016-08-09 14:04:29 +0100625 /*
626 * Closing a group leader splits the group, and as we only disable
627 * group leaders, results in remaining events becoming enabled. To
628 * avoid arbitrary skew, we must read all counters before closing any
629 * group leaders.
630 */
631 read_counters();
632 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200633
Ingo Molnar42202dd2009-06-13 14:57:28 +0200634 return WEXITSTATUS(status);
635}
636
Jiri Olsae55c14a2018-04-23 11:08:21 +0200637static int run_perf_stat(int argc, const char **argv, int run_idx)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200638{
639 int ret;
640
641 if (pre_cmd) {
642 ret = system(pre_cmd);
643 if (ret)
644 return ret;
645 }
646
647 if (sync_run)
648 sync();
649
Jiri Olsae55c14a2018-04-23 11:08:21 +0200650 ret = __run_perf_stat(argc, argv, run_idx);
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200651 if (ret)
652 return ret;
653
654 if (post_cmd) {
655 ret = system(post_cmd);
656 if (ret)
657 return ret;
658 }
659
660 return ret;
661}
662
Andi Kleend73515c2015-03-11 07:16:27 -0700663static void print_running(u64 run, u64 ena)
664{
665 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200666 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700667 csv_sep,
668 run,
669 csv_sep,
670 ena ? 100.0 * run / ena : 100.0);
671 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200672 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700673 }
674}
675
Ingo Molnarf99844c2011-04-27 05:35:39 +0200676static void print_noise_pct(double total, double avg)
677{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800678 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200679
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700680 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200681 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600682 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200683 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200684}
685
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200686static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200687{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200688 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200689
Peter Zijlstra849abde2009-09-04 18:23:38 +0200690 if (run_count == 1)
691 return;
692
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300693 ps = evsel->stats;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200694 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200695}
696
Stephane Eranian12c08a92013-02-14 13:57:29 +0100697static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200698{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200699 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100700 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200701 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100702 cpu_map__id_to_socket(id),
703 csv_output ? 0 : -8,
704 cpu_map__id_to_cpu(id),
705 csv_sep,
706 csv_output ? 0 : 4,
707 nr,
708 csv_sep);
709 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100710 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200711 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100712 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100713 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100714 csv_sep,
715 csv_output ? 0 : 4,
716 nr,
717 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100718 break;
719 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200720 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200721 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100722 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100723 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200724 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200725 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200726 csv_output ? 0 : 16,
727 thread_map__comm(evsel->threads, id),
728 csv_output ? 0 : -8,
729 thread_map__pid(evsel->threads, id),
730 csv_sep);
731 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100732 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200733 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100734 default:
735 break;
736 }
737}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200738
Andi Kleen140aead2016-01-30 09:06:49 -0800739struct outstate {
740 FILE *fh;
741 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800742 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800743 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800744 int id, nr;
745 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800746};
747
748#define METRIC_LEN 35
749
750static void new_line_std(void *ctx)
751{
752 struct outstate *os = ctx;
753
754 os->newline = true;
755}
756
757static void do_new_line_std(struct outstate *os)
758{
759 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800760 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800761 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800762 if (stat_config.aggr_mode == AGGR_NONE)
763 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800764 fprintf(os->fh, " ");
765}
766
767static void print_metric_std(void *ctx, const char *color, const char *fmt,
768 const char *unit, double val)
769{
770 struct outstate *os = ctx;
771 FILE *out = os->fh;
772 int n;
773 bool newline = os->newline;
774
775 os->newline = false;
776
777 if (unit == NULL || fmt == NULL) {
778 fprintf(out, "%-*s", METRIC_LEN, "");
779 return;
780 }
781
782 if (newline)
783 do_new_line_std(os);
784
785 n = fprintf(out, " # ");
786 if (color)
787 n += color_fprintf(out, color, fmt, val);
788 else
789 n += fprintf(out, fmt, val);
790 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
791}
792
Andi Kleen92a61f62016-02-29 14:36:21 -0800793static void new_line_csv(void *ctx)
794{
795 struct outstate *os = ctx;
796 int i;
797
798 fputc('\n', os->fh);
799 if (os->prefix)
800 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800801 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800802 for (i = 0; i < os->nfields; i++)
803 fputs(csv_sep, os->fh);
804}
805
806static void print_metric_csv(void *ctx,
807 const char *color __maybe_unused,
808 const char *fmt, const char *unit, double val)
809{
810 struct outstate *os = ctx;
811 FILE *out = os->fh;
812 char buf[64], *vals, *ends;
813
814 if (unit == NULL || fmt == NULL) {
Ilya Pronin40c21892018-03-05 22:43:53 -0800815 fprintf(out, "%s%s", csv_sep, csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800816 return;
817 }
818 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900819 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800820 while (isdigit(*ends) || *ends == '.')
821 ends++;
822 *ends = 0;
823 while (isspace(*unit))
824 unit++;
825 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
826}
827
Andi Kleen54b50912016-03-03 15:57:36 -0800828/* Filter out some columns that don't work well in metrics only mode */
829
830static bool valid_only_metric(const char *unit)
831{
832 if (!unit)
833 return false;
834 if (strstr(unit, "/sec") ||
835 strstr(unit, "hz") ||
836 strstr(unit, "Hz") ||
837 strstr(unit, "CPUs utilized"))
838 return false;
839 return true;
840}
841
842static const char *fixunit(char *buf, struct perf_evsel *evsel,
843 const char *unit)
844{
845 if (!strncmp(unit, "of all", 6)) {
846 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
847 unit);
848 return buf;
849 }
850 return unit;
851}
852
853static void print_metric_only(void *ctx, const char *color, const char *fmt,
854 const char *unit, double val)
855{
856 struct outstate *os = ctx;
857 FILE *out = os->fh;
Jiri Olsaf5155722018-06-07 00:15:08 +0200858 char buf[1024], str[1024];
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200859 unsigned mlen = metric_only_len;
Andi Kleen54b50912016-03-03 15:57:36 -0800860
861 if (!valid_only_metric(unit))
862 return;
863 unit = fixunit(buf, os->evsel, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800864 if (mlen < strlen(unit))
865 mlen = strlen(unit) + 1;
Jiri Olsaf5155722018-06-07 00:15:08 +0200866
867 if (color)
868 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
869
870 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
871 fprintf(out, "%*s ", mlen, str);
Andi Kleen54b50912016-03-03 15:57:36 -0800872}
873
874static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
875 const char *fmt,
876 const char *unit, double val)
877{
878 struct outstate *os = ctx;
879 FILE *out = os->fh;
880 char buf[64], *vals, *ends;
881 char tbuf[1024];
882
883 if (!valid_only_metric(unit))
884 return;
885 unit = fixunit(tbuf, os->evsel, unit);
886 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900887 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -0800888 while (isdigit(*ends) || *ends == '.')
889 ends++;
890 *ends = 0;
891 fprintf(out, "%s%s", vals, csv_sep);
892}
893
894static void new_line_metric(void *ctx __maybe_unused)
895{
896}
897
898static void print_metric_header(void *ctx, const char *color __maybe_unused,
899 const char *fmt __maybe_unused,
900 const char *unit, double val __maybe_unused)
901{
902 struct outstate *os = ctx;
903 char tbuf[1024];
904
905 if (!valid_only_metric(unit))
906 return;
907 unit = fixunit(tbuf, os->evsel, unit);
908 if (csv_output)
909 fprintf(os->fh, "%s%s", unit, csv_sep);
910 else
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200911 fprintf(os->fh, "%*s ", metric_only_len, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800912}
913
Andi Kleen44d49a62016-02-29 14:36:22 -0800914static int first_shadow_cpu(struct perf_evsel *evsel, int id)
915{
916 int i;
917
918 if (!aggr_get_id)
919 return 0;
920
921 if (stat_config.aggr_mode == AGGR_NONE)
922 return id;
923
924 if (stat_config.aggr_mode == AGGR_GLOBAL)
925 return 0;
926
927 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
928 int cpu2 = perf_evsel__cpus(evsel)->map[i];
929
930 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
931 return cpu2;
932 }
933 return 0;
934}
935
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200936static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
937{
Jiri Olsa58215222015-07-21 14:31:24 +0200938 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200939 double sc = evsel->scale;
940 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200941
942 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -0700943 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200944 } else {
945 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -0700946 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200947 else
Andi Kleene3b03b62016-05-05 16:04:03 -0700948 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200949 }
950
951 aggr_printout(evsel, id, nr);
952
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200953 fprintf(output, fmt, avg, csv_sep);
954
955 if (evsel->unit)
956 fprintf(output, "%-*s%s",
957 csv_output ? 0 : unit_width,
958 evsel->unit, csv_sep);
959
960 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
961
962 if (evsel->cgrp)
963 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -0800964}
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200965
Kan Liang30060ea2018-04-24 11:20:11 -0700966static bool is_mixed_hw_group(struct perf_evsel *counter)
967{
968 struct perf_evlist *evlist = counter->evlist;
969 u32 pmu_type = counter->attr.type;
970 struct perf_evsel *pos;
971
972 if (counter->nr_members < 2)
973 return false;
974
975 evlist__for_each_entry(evlist, pos) {
976 /* software events can be part of any hardware group */
977 if (pos->attr.type == PERF_TYPE_SOFTWARE)
978 continue;
979 if (pmu_type == PERF_TYPE_SOFTWARE) {
980 pmu_type = pos->attr.type;
981 continue;
982 }
983 if (pmu_type != pos->attr.type)
984 return true;
985 }
986
987 return false;
988}
989
Andi Kleenf9483392016-01-30 09:06:50 -0800990static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +0800991 char *prefix, u64 run, u64 ena, double noise,
992 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -0800993{
Andi Kleen140aead2016-01-30 09:06:49 -0800994 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -0800995 struct outstate os = {
996 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -0800997 .prefix = prefix ? prefix : "",
998 .id = id,
999 .nr = nr,
1000 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001001 };
Andi Kleen140aead2016-01-30 09:06:49 -08001002 print_metric_t pm = print_metric_std;
1003 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001004
Andi Kleen54b50912016-03-03 15:57:36 -08001005 if (metric_only) {
1006 nl = new_line_metric;
1007 if (csv_output)
1008 pm = print_metric_only_csv;
1009 else
1010 pm = print_metric_only;
1011 } else
1012 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001013
Andi Kleen54b50912016-03-03 15:57:36 -08001014 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001015 static int aggr_fields[] = {
1016 [AGGR_GLOBAL] = 0,
1017 [AGGR_THREAD] = 1,
1018 [AGGR_NONE] = 1,
1019 [AGGR_SOCKET] = 2,
1020 [AGGR_CORE] = 2,
1021 };
1022
1023 pm = print_metric_csv;
1024 nl = new_line_csv;
1025 os.nfields = 3;
1026 os.nfields += aggr_fields[stat_config.aggr_mode];
1027 if (counter->cgrp)
1028 os.nfields++;
1029 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001030 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001031 if (metric_only) {
1032 pm(&os, NULL, "", "", 0);
1033 return;
1034 }
Andi Kleencb110f42016-01-30 09:06:51 -08001035 aggr_printout(counter, id, nr);
1036
1037 fprintf(stat_config.output, "%*s%s",
1038 csv_output ? 0 : 18,
1039 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1040 csv_sep);
1041
Kan Liang30060ea2018-04-24 11:20:11 -07001042 if (counter->supported) {
Borislav Petkov02d492e2017-02-07 01:40:05 +01001043 print_free_counters_hint = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001044 if (is_mixed_hw_group(counter))
1045 print_mixed_hw_group_error = 1;
1046 }
Borislav Petkov02d492e2017-02-07 01:40:05 +01001047
Andi Kleencb110f42016-01-30 09:06:51 -08001048 fprintf(stat_config.output, "%-*s%s",
1049 csv_output ? 0 : unit_width,
1050 counter->unit, csv_sep);
1051
1052 fprintf(stat_config.output, "%*s",
1053 csv_output ? 0 : -25,
1054 perf_evsel__name(counter));
1055
1056 if (counter->cgrp)
1057 fprintf(stat_config.output, "%s%s",
1058 csv_sep, counter->cgrp->name);
1059
Andi Kleen92a61f62016-02-29 14:36:21 -08001060 if (!csv_output)
1061 pm(&os, NULL, NULL, "", 0);
1062 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001063 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001064 if (csv_output)
1065 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001066 return;
1067 }
1068
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001069 if (!metric_only)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001070 abs_printout(id, nr, counter, uval);
1071
Andi Kleen140aead2016-01-30 09:06:49 -08001072 out.print_metric = pm;
1073 out.new_line = nl;
1074 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001075 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001076
Andi Kleen54b50912016-03-03 15:57:36 -08001077 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001078 print_noise(counter, noise);
1079 print_running(run, ena);
1080 }
1081
1082 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001083 first_shadow_cpu(counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001084 &out, &metric_events, st);
Andi Kleen54b50912016-03-03 15:57:36 -08001085 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001086 print_noise(counter, noise);
1087 print_running(run, ena);
1088 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001089}
1090
Andi Kleen44d49a62016-02-29 14:36:22 -08001091static void aggr_update_shadow(void)
1092{
1093 int cpu, s2, id, s;
1094 u64 val;
1095 struct perf_evsel *counter;
1096
1097 for (s = 0; s < aggr_map->nr; s++) {
1098 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001099 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001100 val = 0;
1101 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1102 s2 = aggr_get_id(evsel_list->cpus, cpu);
1103 if (s2 != id)
1104 continue;
1105 val += perf_counts(counter->counts, cpu, 0)->val;
1106 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001107 perf_stat__update_shadow_stats(counter, val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001108 first_shadow_cpu(counter, id),
1109 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001110 }
1111 }
1112}
1113
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001114static void uniquify_event_name(struct perf_evsel *counter)
1115{
1116 char *new_name;
1117 char *config;
1118
Kan Liang80ee8c52018-04-24 11:20:14 -07001119 if (counter->uniquified_name ||
1120 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001121 strlen(counter->pmu_name)))
1122 return;
1123
1124 config = strchr(counter->name, '/');
1125 if (config) {
1126 if (asprintf(&new_name,
1127 "%s%s", counter->pmu_name, config) > 0) {
1128 free(counter->name);
1129 counter->name = new_name;
1130 }
1131 } else {
1132 if (asprintf(&new_name,
1133 "%s [%s]", counter->name, counter->pmu_name) > 0) {
1134 free(counter->name);
1135 counter->name = new_name;
1136 }
1137 }
Kan Liang80ee8c52018-04-24 11:20:14 -07001138
1139 counter->uniquified_name = true;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001140}
1141
Andi Kleen430daf22017-03-20 13:17:00 -07001142static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001143 void (*cb)(struct perf_evsel *counter, void *data,
1144 bool first),
1145 void *data)
1146{
Andi Kleen430daf22017-03-20 13:17:00 -07001147 struct perf_evsel *alias;
1148
1149 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1150 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1151 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1152 alias->scale != counter->scale ||
1153 alias->cgrp != counter->cgrp ||
1154 strcmp(alias->unit, counter->unit) ||
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001155 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter))
Andi Kleen430daf22017-03-20 13:17:00 -07001156 break;
1157 alias->merged_stat = true;
1158 cb(alias, data, false);
1159 }
1160}
1161
1162static bool collect_data(struct perf_evsel *counter,
1163 void (*cb)(struct perf_evsel *counter, void *data,
1164 bool first),
1165 void *data)
1166{
1167 if (counter->merged_stat)
1168 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001169 cb(counter, data, true);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001170 if (no_merge)
1171 uniquify_event_name(counter);
1172 else if (counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001173 collect_all_aliases(counter, cb, data);
1174 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001175}
1176
1177struct aggr_data {
1178 u64 ena, run, val;
1179 int id;
1180 int nr;
1181 int cpu;
1182};
1183
1184static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1185{
1186 struct aggr_data *ad = data;
1187 int cpu, s2;
1188
1189 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1190 struct perf_counts_values *counts;
1191
1192 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1193 if (s2 != ad->id)
1194 continue;
1195 if (first)
1196 ad->nr++;
1197 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001198 /*
1199 * When any result is bad, make them all to give
1200 * consistent output in interval mode.
1201 */
1202 if (counts->ena == 0 || counts->run == 0 ||
1203 counter->counts->scaled == -1) {
1204 ad->ena = 0;
1205 ad->run = 0;
1206 break;
1207 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001208 ad->val += counts->val;
1209 ad->ena += counts->ena;
1210 ad->run += counts->run;
1211 }
1212}
1213
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001214static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001215{
Jiri Olsa58215222015-07-21 14:31:24 +02001216 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001217 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001218 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001219 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001220 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001221 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001222
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001223 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001224 return;
1225
Andi Kleen44d49a62016-02-29 14:36:22 -08001226 aggr_update_shadow();
1227
Andi Kleen54b50912016-03-03 15:57:36 -08001228 /*
1229 * With metric_only everything is on a single line.
1230 * Without each counter has its own line.
1231 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001232 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001233 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001234 if (prefix && metric_only)
1235 fprintf(output, "%s", prefix);
1236
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001237 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001238 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001239 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001240 if (is_duration_time(counter))
1241 continue;
1242
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001243 ad.val = ad.ena = ad.run = 0;
1244 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001245 if (!collect_data(counter, aggr_cb, &ad))
1246 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001247 nr = ad.nr;
1248 ena = ad.ena;
1249 run = ad.run;
1250 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001251 if (first && metric_only) {
1252 first = false;
1253 aggr_printout(counter, id, nr);
1254 }
1255 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001256 fprintf(output, "%s", prefix);
1257
Stephane Eranian410136f2013-11-12 17:58:49 +01001258 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001259 printout(id, nr, counter, uval, prefix, run, ena, 1.0,
1260 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001261 if (!metric_only)
1262 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001263 }
Andi Kleen54b50912016-03-03 15:57:36 -08001264 if (metric_only)
1265 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001266 }
1267}
1268
Jin Yao29734552017-12-05 22:03:11 +08001269static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001270{
Jin Yao29734552017-12-05 22:03:11 +08001271 return ((struct perf_aggr_thread_value *)b)->val -
1272 ((struct perf_aggr_thread_value *)a)->val;
1273}
1274
1275static struct perf_aggr_thread_value *sort_aggr_thread(
1276 struct perf_evsel *counter,
1277 int nthreads, int ncpus,
1278 int *ret)
1279{
1280 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001281 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001282 struct perf_aggr_thread_value *buf;
1283
1284 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1285 if (!buf)
1286 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001287
1288 for (thread = 0; thread < nthreads; thread++) {
1289 u64 ena = 0, run = 0, val = 0;
1290
1291 for (cpu = 0; cpu < ncpus; cpu++) {
1292 val += perf_counts(counter->counts, cpu, thread)->val;
1293 ena += perf_counts(counter->counts, cpu, thread)->ena;
1294 run += perf_counts(counter->counts, cpu, thread)->run;
1295 }
1296
Jin Yao29734552017-12-05 22:03:11 +08001297 uval = val * counter->scale;
1298
1299 /*
1300 * Skip value 0 when enabling --per-thread globally,
1301 * otherwise too many 0 output.
1302 */
1303 if (uval == 0.0 && target__has_per_thread(&target))
1304 continue;
1305
1306 buf[i].counter = counter;
1307 buf[i].id = thread;
1308 buf[i].uval = uval;
1309 buf[i].val = val;
1310 buf[i].run = run;
1311 buf[i].ena = ena;
1312 i++;
1313 }
1314
1315 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1316
1317 if (ret)
1318 *ret = i;
1319
1320 return buf;
1321}
1322
1323static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1324{
1325 FILE *output = stat_config.output;
1326 int nthreads = thread_map__nr(counter->threads);
1327 int ncpus = cpu_map__nr(counter->cpus);
1328 int thread, sorted_threads, id;
1329 struct perf_aggr_thread_value *buf;
1330
1331 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads);
1332 if (!buf) {
1333 perror("cannot sort aggr thread");
1334 return;
1335 }
1336
1337 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001338 if (prefix)
1339 fprintf(output, "%s", prefix);
1340
Jin Yao29734552017-12-05 22:03:11 +08001341 id = buf[thread].id;
Jin Yao14e72a22017-12-05 22:03:08 +08001342 if (stat_config.stats)
Jin Yao29734552017-12-05 22:03:11 +08001343 printout(id, 0, buf[thread].counter, buf[thread].uval,
1344 prefix, buf[thread].run, buf[thread].ena, 1.0,
1345 &stat_config.stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001346 else
Jin Yao29734552017-12-05 22:03:11 +08001347 printout(id, 0, buf[thread].counter, buf[thread].uval,
1348 prefix, buf[thread].run, buf[thread].ena, 1.0,
1349 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001350 fputc('\n', output);
1351 }
Jin Yao29734552017-12-05 22:03:11 +08001352
1353 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001354}
1355
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001356struct caggr_data {
1357 double avg, avg_enabled, avg_running;
1358};
1359
1360static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1361 bool first __maybe_unused)
1362{
1363 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001364 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001365
1366 cd->avg += avg_stats(&ps->res_stats[0]);
1367 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1368 cd->avg_running += avg_stats(&ps->res_stats[2]);
1369}
1370
Ingo Molnar42202dd2009-06-13 14:57:28 +02001371/*
1372 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001373 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001374 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001375static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001376{
Jiri Olsa58215222015-07-21 14:31:24 +02001377 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001378 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001379 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001380
Andi Kleen430daf22017-03-20 13:17:00 -07001381 if (!collect_data(counter, counter_aggr_cb, &cd))
1382 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001383
Andi Kleen54b50912016-03-03 15:57:36 -08001384 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001385 fprintf(output, "%s", prefix);
1386
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001387 uval = cd.avg * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001388 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
1389 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001390 if (!metric_only)
1391 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001392}
1393
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001394static void counter_cb(struct perf_evsel *counter, void *data,
1395 bool first __maybe_unused)
1396{
1397 struct aggr_data *ad = data;
1398
1399 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1400 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1401 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1402}
1403
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001404/*
1405 * Print out the results of a single counter:
1406 * does not use aggregated count in system-wide
1407 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001408static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001409{
Jiri Olsa58215222015-07-21 14:31:24 +02001410 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001411 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001412 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001413 int cpu;
1414
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001415 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001416 struct aggr_data ad = { .cpu = cpu };
1417
Andi Kleen430daf22017-03-20 13:17:00 -07001418 if (!collect_data(counter, counter_cb, &ad))
1419 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001420 val = ad.val;
1421 ena = ad.ena;
1422 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001423
1424 if (prefix)
1425 fprintf(output, "%s", prefix);
1426
Stephane Eranian410136f2013-11-12 17:58:49 +01001427 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001428 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1429 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001430
Stephane Eranian4aa90152011-08-15 22:22:33 +02001431 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001432 }
1433}
1434
Andi Kleen206cab62016-03-03 15:57:37 -08001435static void print_no_aggr_metric(char *prefix)
1436{
1437 int cpu;
1438 int nrcpus = 0;
1439 struct perf_evsel *counter;
1440 u64 ena, run, val;
1441 double uval;
1442
1443 nrcpus = evsel_list->cpus->nr;
1444 for (cpu = 0; cpu < nrcpus; cpu++) {
1445 bool first = true;
1446
1447 if (prefix)
1448 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001449 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001450 if (is_duration_time(counter))
1451 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001452 if (first) {
1453 aggr_printout(counter, cpu, 0);
1454 first = false;
1455 }
1456 val = perf_counts(counter->counts, cpu, 0)->val;
1457 ena = perf_counts(counter->counts, cpu, 0)->ena;
1458 run = perf_counts(counter->counts, cpu, 0)->run;
1459
1460 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001461 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1462 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001463 }
1464 fputc('\n', stat_config.output);
1465 }
1466}
1467
Andi Kleen54b50912016-03-03 15:57:36 -08001468static int aggr_header_lens[] = {
1469 [AGGR_CORE] = 18,
1470 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001471 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001472 [AGGR_THREAD] = 24,
1473 [AGGR_GLOBAL] = 0,
1474};
1475
Andi Kleenc51fd632016-05-24 12:52:39 -07001476static const char *aggr_header_csv[] = {
1477 [AGGR_CORE] = "core,cpus,",
1478 [AGGR_SOCKET] = "socket,cpus",
1479 [AGGR_NONE] = "cpu,",
1480 [AGGR_THREAD] = "comm-pid,",
1481 [AGGR_GLOBAL] = ""
1482};
1483
Andi Kleen41c8ca22016-05-24 12:52:38 -07001484static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001485{
1486 struct perf_stat_output_ctx out;
1487 struct perf_evsel *counter;
1488 struct outstate os = {
1489 .fh = stat_config.output
1490 };
1491
1492 if (prefix)
1493 fprintf(stat_config.output, "%s", prefix);
1494
Andi Kleen41c8ca22016-05-24 12:52:38 -07001495 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001496 fprintf(stat_config.output, "%*s",
1497 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001498 if (csv_output) {
1499 if (stat_config.interval)
1500 fputs("time,", stat_config.output);
1501 fputs(aggr_header_csv[stat_config.aggr_mode],
1502 stat_config.output);
1503 }
Andi Kleen54b50912016-03-03 15:57:36 -08001504
1505 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001506 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001507 if (is_duration_time(counter))
1508 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001509 os.evsel = counter;
1510 out.ctx = &os;
1511 out.print_metric = print_metric_header;
1512 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001513 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001514 os.evsel = counter;
1515 perf_stat__print_shadow_stats(counter, 0,
1516 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001517 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001518 &metric_events,
1519 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001520 }
1521 fputc('\n', stat_config.output);
1522}
1523
Jiri Olsad4f63a42015-06-26 11:29:26 +02001524static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001525{
Jiri Olsa58215222015-07-21 14:31:24 +02001526 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001527 static int num_print_interval;
1528
Jiri Olsa9660e082018-06-07 00:15:06 +02001529 if (interval_clear)
1530 puts(CONSOLE_CLEAR);
1531
Jiri Olsad4f63a42015-06-26 11:29:26 +02001532 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1533
Jiri Olsa9660e082018-06-07 00:15:06 +02001534 if ((num_print_interval == 0 && !csv_output) || interval_clear) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001535 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001536 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001537 fprintf(output, "# time socket cpus");
1538 if (!metric_only)
1539 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001540 break;
1541 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001542 fprintf(output, "# time core cpus");
1543 if (!metric_only)
1544 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001545 break;
1546 case AGGR_NONE:
Jiri Olsaf5155722018-06-07 00:15:08 +02001547 fprintf(output, "# time CPU ");
Andi Kleen41c8ca22016-05-24 12:52:38 -07001548 if (!metric_only)
1549 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001550 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001551 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001552 fprintf(output, "# time comm-pid");
1553 if (!metric_only)
1554 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001555 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001556 case AGGR_GLOBAL:
1557 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001558 fprintf(output, "# time");
1559 if (!metric_only)
1560 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001561 case AGGR_UNSET:
1562 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001563 }
1564 }
1565
Jiri Olsac818cc02018-07-02 15:42:02 +02001566 if ((num_print_interval == 0 || interval_clear) && metric_only)
Andi Kleen41c8ca22016-05-24 12:52:38 -07001567 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001568 if (++num_print_interval == 25)
1569 num_print_interval = 0;
1570}
1571
1572static void print_header(int argc, const char **argv)
1573{
Jiri Olsa58215222015-07-21 14:31:24 +02001574 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001575 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001576
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001577 fflush(stdout);
1578
Stephane Eraniand7470b62010-12-01 18:49:05 +02001579 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001580 fprintf(output, "\n");
1581 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001582 if (target.system_wide)
1583 fprintf(output, "\'system wide");
1584 else if (target.cpu_list)
1585 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001586 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001587 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1588 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001589 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001590 } else if (target.pid)
1591 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001592 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001593 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001594
Stephane Eranian4aa90152011-08-15 22:22:33 +02001595 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001596 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001597 fprintf(output, " (%d runs)", run_count);
1598 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001599 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001600}
1601
Jiri Olsabc22de92018-04-23 11:08:20 +02001602static int get_precision(double num)
1603{
1604 if (num > 1)
1605 return 0;
1606
1607 return lround(ceil(-log10(num)));
1608}
1609
Jiri Olsae55c14a2018-04-23 11:08:21 +02001610static void print_table(FILE *output, int precision, double avg)
1611{
1612 char tmp[64];
1613 int idx, indent = 0;
1614
1615 scnprintf(tmp, 64, " %17.*f", precision, avg);
1616 while (tmp[indent] == ' ')
1617 indent++;
1618
1619 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1620
1621 for (idx = 0; idx < run_count; idx++) {
1622 double run = (double) walltime_run[idx] / NSEC_PER_SEC;
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001623 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001624
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001625 fprintf(output, " %17.*f (%+.*f) ",
Jiri Olsae55c14a2018-04-23 11:08:21 +02001626 precision, run, precision, run - avg);
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001627
1628 for (h = 0; h < n; h++)
1629 fprintf(output, "#");
1630
1631 fprintf(output, "\n");
Jiri Olsae55c14a2018-04-23 11:08:21 +02001632 }
1633
1634 fprintf(output, "\n%*s# Final result:\n", indent, "");
1635}
1636
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001637static double timeval2double(struct timeval *t)
1638{
1639 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1640}
1641
Jiri Olsad4f63a42015-06-26 11:29:26 +02001642static void print_footer(void)
1643{
Jiri Olsabc22de92018-04-23 11:08:20 +02001644 double avg = avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsa58215222015-07-21 14:31:24 +02001645 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001646 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001647
Jiri Olsad4f63a42015-06-26 11:29:26 +02001648 if (!null_run)
1649 fprintf(output, "\n");
Jiri Olsabc22de92018-04-23 11:08:20 +02001650
1651 if (run_count == 1) {
1652 fprintf(output, " %17.9f seconds time elapsed", avg);
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001653
1654 if (ru_display) {
1655 double ru_utime = timeval2double(&ru_data.ru_utime);
1656 double ru_stime = timeval2double(&ru_data.ru_stime);
1657
1658 fprintf(output, "\n\n");
1659 fprintf(output, " %17.9f seconds user\n", ru_utime);
1660 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1661 }
Jiri Olsabc22de92018-04-23 11:08:20 +02001662 } else {
1663 double sd = stddev_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
1664 /*
1665 * Display at most 2 more significant
1666 * digits than the stddev inaccuracy.
1667 */
1668 int precision = get_precision(sd) + 2;
1669
Jiri Olsae55c14a2018-04-23 11:08:21 +02001670 if (walltime_run_table)
1671 print_table(output, precision, avg);
1672
Jiri Olsabc22de92018-04-23 11:08:20 +02001673 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1674 precision, avg, precision, sd);
1675
1676 print_noise_pct(sd, avg);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001677 }
1678 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001679
Andi Kleen918c7b062017-05-22 18:00:16 -07001680 if (print_free_counters_hint &&
1681 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1682 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001683 fprintf(output,
1684"Some events weren't counted. Try disabling the NMI watchdog:\n"
1685" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1686" perf stat ...\n"
1687" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001688
1689 if (print_mixed_hw_group_error)
1690 fprintf(output,
1691 "The events in group usually have to be from "
1692 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001693}
1694
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001695static void
1696perf_evlist__print_counters(struct perf_evlist *evlist,
Jiri Olsab64df7f2018-08-30 08:32:26 +02001697 struct perf_stat_config *config,
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001698 struct timespec *ts,
1699 int argc, const char **argv)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001700{
Jiri Olsab64df7f2018-08-30 08:32:26 +02001701 int interval = config->interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001702 struct perf_evsel *counter;
1703 char buf[64], *prefix = NULL;
1704
1705 if (interval)
1706 print_interval(prefix = buf, ts);
1707 else
1708 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001709
Andi Kleen54b50912016-03-03 15:57:36 -08001710 if (metric_only) {
1711 static int num_print_iv;
1712
Andi Kleen41c8ca22016-05-24 12:52:38 -07001713 if (num_print_iv == 0 && !interval)
1714 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001715 if (num_print_iv++ == 25)
1716 num_print_iv = 0;
Jiri Olsab64df7f2018-08-30 08:32:26 +02001717 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1718 fprintf(config->output, "%s", prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001719 }
1720
Jiri Olsab64df7f2018-08-30 08:32:26 +02001721 switch (config->aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001722 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001723 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001724 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001725 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001726 case AGGR_THREAD:
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001727 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001728 if (is_duration_time(counter))
1729 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001730 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001731 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001732 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001733 case AGGR_GLOBAL:
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001734 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001735 if (is_duration_time(counter))
1736 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001737 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001738 }
Andi Kleen54b50912016-03-03 15:57:36 -08001739 if (metric_only)
Jiri Olsab64df7f2018-08-30 08:32:26 +02001740 fputc('\n', config->output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001741 break;
1742 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001743 if (metric_only)
1744 print_no_aggr_metric(prefix);
1745 else {
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001746 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001747 if (is_duration_time(counter))
1748 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001749 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001750 }
Andi Kleen206cab62016-03-03 15:57:37 -08001751 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001752 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001753 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001754 default:
1755 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001756 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001757
Jiri Olsad4f63a42015-06-26 11:29:26 +02001758 if (!interval && !csv_output)
1759 print_footer();
1760
Jiri Olsab64df7f2018-08-30 08:32:26 +02001761 fflush(config->output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001762}
1763
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001764static void print_counters(struct timespec *ts, int argc, const char **argv)
1765{
Jiri Olsa01748202018-08-30 08:32:25 +02001766 /* Do not print anything if we record to the pipe. */
1767 if (STAT_RECORD && perf_stat.data.is_pipe)
1768 return;
1769
Jiri Olsab64df7f2018-08-30 08:32:26 +02001770 perf_evlist__print_counters(evsel_list, &stat_config,
1771 ts, argc, argv);
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001772}
1773
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001774static volatile int signr = -1;
1775
Ingo Molnar52425192009-05-26 09:17:18 +02001776static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001777{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001778 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001779 done = 1;
1780
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001781 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001782 /*
1783 * render child_pid harmless
1784 * won't send SIGTERM to a random
1785 * process in case of race condition
1786 * and fast PID recycling
1787 */
1788 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001789}
1790
1791static void sig_atexit(void)
1792{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001793 sigset_t set, oset;
1794
1795 /*
1796 * avoid race condition with SIGCHLD handler
1797 * in skip_signal() which is modifying child_pid
1798 * goal is to avoid send SIGTERM to a random
1799 * process
1800 */
1801 sigemptyset(&set);
1802 sigaddset(&set, SIGCHLD);
1803 sigprocmask(SIG_BLOCK, &set, &oset);
1804
Chris Wilson933da832009-10-04 01:35:01 +01001805 if (child_pid != -1)
1806 kill(child_pid, SIGTERM);
1807
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001808 sigprocmask(SIG_SETMASK, &oset, NULL);
1809
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001810 if (signr == -1)
1811 return;
1812
1813 signal(signr, SIG_DFL);
1814 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001815}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001816
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001817static int stat__set_big_num(const struct option *opt __maybe_unused,
1818 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001819{
1820 big_num_opt = unset ? 0 : 1;
1821 return 0;
1822}
1823
Andi Kleen44b1e602016-05-30 12:49:42 -03001824static int enable_metric_only(const struct option *opt __maybe_unused,
1825 const char *s __maybe_unused, int unset)
1826{
1827 force_metric_only = true;
1828 metric_only = !unset;
1829 return 0;
1830}
1831
Andi Kleenb18f3e32017-08-31 12:40:31 -07001832static int parse_metric_groups(const struct option *opt,
1833 const char *str,
1834 int unset __maybe_unused)
1835{
1836 return metricgroup__parse_groups(opt, str, &metric_events);
1837}
1838
Jiri Olsae0547312015-11-05 15:40:45 +01001839static const struct option stat_options[] = {
1840 OPT_BOOLEAN('T', "transaction", &transaction_run,
1841 "hardware transaction statistics"),
1842 OPT_CALLBACK('e', "event", &evsel_list, "event",
1843 "event selector. use 'perf list' to list available events",
1844 parse_events_option),
1845 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1846 "event filter", parse_filter),
Jiri Olsa5698f262018-08-30 08:32:12 +02001847 OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
Jiri Olsae0547312015-11-05 15:40:45 +01001848 "child tasks do not inherit counters"),
1849 OPT_STRING('p', "pid", &target.pid, "pid",
1850 "stat events on existing process id"),
1851 OPT_STRING('t', "tid", &target.tid, "tid",
1852 "stat events on existing thread id"),
1853 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1854 "system-wide collection from all CPUs"),
1855 OPT_BOOLEAN('g', "group", &group,
1856 "put the counters into a counter group"),
1857 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1858 OPT_INCR('v', "verbose", &verbose,
1859 "be more verbose (show counter open errors, etc)"),
1860 OPT_INTEGER('r', "repeat", &run_count,
1861 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsae55c14a2018-04-23 11:08:21 +02001862 OPT_BOOLEAN(0, "table", &walltime_run_table,
1863 "display details about each run (only with -r option)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001864 OPT_BOOLEAN('n', "null", &null_run,
1865 "null run - dont start any counters"),
1866 OPT_INCR('d', "detailed", &detailed_run,
1867 "detailed run - start a lot of events"),
1868 OPT_BOOLEAN('S', "sync", &sync_run,
1869 "call sync() before starting a run"),
1870 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1871 "print large numbers with thousands\' separators",
1872 stat__set_big_num),
1873 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1874 "list of cpus to monitor in system-wide"),
1875 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1876 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001877 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001878 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1879 "print counts with custom separator"),
1880 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1881 "monitor event in cgroup name only", parse_cgroups),
1882 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1883 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1884 OPT_INTEGER(0, "log-fd", &output_fd,
1885 "log output to fd, instead of stderr"),
1886 OPT_STRING(0, "pre", &pre_cmd, "command",
1887 "command to run prior to the measured command"),
1888 OPT_STRING(0, "post", &post_cmd, "command",
1889 "command to run after to the measured command"),
1890 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03001891 "print counts at regular interval in ms "
1892 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01001893 OPT_INTEGER(0, "interval-count", &stat_config.times,
1894 "print counts for fixed number of times"),
Jiri Olsa9660e082018-06-07 00:15:06 +02001895 OPT_BOOLEAN(0, "interval-clear", &interval_clear,
1896 "clear screen in between new interval"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01001897 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1898 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001899 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1900 "aggregate counts per processor socket", AGGR_SOCKET),
1901 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1902 "aggregate counts per physical processor core", AGGR_CORE),
1903 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1904 "aggregate counts per thread", AGGR_THREAD),
Jiri Olsa728c0ee2018-08-30 08:32:11 +02001905 OPT_UINTEGER('D', "delay", &stat_config.initial_delay,
Jiri Olsae0547312015-11-05 15:40:45 +01001906 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001907 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1908 "Only print computed metrics. No raw values", enable_metric_only),
1909 OPT_BOOLEAN(0, "topdown", &topdown_run,
1910 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001911 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1912 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001913 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1914 "monitor specified metrics or metric groups (separated by ,)",
1915 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01001916 OPT_END()
1917};
1918
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001919static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1920{
1921 return cpu_map__get_socket(map, cpu, NULL);
1922}
1923
1924static int perf_stat__get_core(struct cpu_map *map, int cpu)
1925{
1926 return cpu_map__get_core(map, cpu, NULL);
1927}
1928
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001929static int cpu_map__get_max(struct cpu_map *map)
1930{
1931 int i, max = -1;
1932
1933 for (i = 0; i < map->nr; i++) {
1934 if (map->map[i] > max)
1935 max = map->map[i];
1936 }
1937
1938 return max;
1939}
1940
1941static struct cpu_map *cpus_aggr_map;
1942
1943static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1944{
1945 int cpu;
1946
1947 if (idx >= map->nr)
1948 return -1;
1949
1950 cpu = map->map[idx];
1951
1952 if (cpus_aggr_map->map[cpu] == -1)
1953 cpus_aggr_map->map[cpu] = get_id(map, idx);
1954
1955 return cpus_aggr_map->map[cpu];
1956}
1957
1958static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1959{
1960 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1961}
1962
1963static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1964{
1965 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1966}
1967
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001968static int perf_stat_init_aggr_mode(void)
1969{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001970 int nr;
1971
Jiri Olsa421a50f2015-07-21 14:31:22 +02001972 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001973 case AGGR_SOCKET:
1974 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1975 perror("cannot build socket map");
1976 return -1;
1977 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001978 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001979 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001980 case AGGR_CORE:
1981 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1982 perror("cannot build core map");
1983 return -1;
1984 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001985 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001986 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001987 case AGGR_NONE:
1988 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001989 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001990 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001991 default:
1992 break;
1993 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001994
1995 /*
1996 * The evsel_list->cpus is the base we operate on,
1997 * taking the highest cpu number to be the size of
1998 * the aggregation translate cpumap.
1999 */
2000 nr = cpu_map__get_max(evsel_list->cpus);
2001 cpus_aggr_map = cpu_map__empty_new(nr + 1);
2002 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002003}
2004
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002005static void perf_stat__exit_aggr_mode(void)
2006{
2007 cpu_map__put(aggr_map);
2008 cpu_map__put(cpus_aggr_map);
2009 aggr_map = NULL;
2010 cpus_aggr_map = NULL;
2011}
2012
Jiri Olsa68d702f2015-11-05 15:40:58 +01002013static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2014{
2015 int cpu;
2016
2017 if (idx > map->nr)
2018 return -1;
2019
2020 cpu = map->map[idx];
2021
Jan Stancekda8a58b2017-02-17 12:10:26 +01002022 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002023 return -1;
2024
2025 return cpu;
2026}
2027
2028static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2029{
2030 struct perf_env *env = data;
2031 int cpu = perf_env__get_cpu(env, map, idx);
2032
2033 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2034}
2035
2036static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2037{
2038 struct perf_env *env = data;
2039 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2040
2041 if (cpu != -1) {
2042 int socket_id = env->cpu[cpu].socket_id;
2043
2044 /*
2045 * Encode socket in upper 16 bits
2046 * core_id is relative to socket, and
2047 * we need a global id. So we combine
2048 * socket + core id.
2049 */
2050 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2051 }
2052
2053 return core;
2054}
2055
2056static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2057 struct cpu_map **sockp)
2058{
2059 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2060}
2061
2062static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2063 struct cpu_map **corep)
2064{
2065 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2066}
2067
2068static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2069{
2070 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2071}
2072
2073static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2074{
2075 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2076}
2077
2078static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2079{
2080 struct perf_env *env = &st->session->header.env;
2081
2082 switch (stat_config.aggr_mode) {
2083 case AGGR_SOCKET:
2084 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2085 perror("cannot build socket map");
2086 return -1;
2087 }
2088 aggr_get_id = perf_stat__get_socket_file;
2089 break;
2090 case AGGR_CORE:
2091 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2092 perror("cannot build core map");
2093 return -1;
2094 }
2095 aggr_get_id = perf_stat__get_core_file;
2096 break;
2097 case AGGR_NONE:
2098 case AGGR_GLOBAL:
2099 case AGGR_THREAD:
2100 case AGGR_UNSET:
2101 default:
2102 break;
2103 }
2104
2105 return 0;
2106}
2107
Andi Kleen44b1e602016-05-30 12:49:42 -03002108static int topdown_filter_events(const char **attr, char **str, bool use_group)
2109{
2110 int off = 0;
2111 int i;
2112 int len = 0;
2113 char *s;
2114
2115 for (i = 0; attr[i]; i++) {
2116 if (pmu_have_event("cpu", attr[i])) {
2117 len += strlen(attr[i]) + 1;
2118 attr[i - off] = attr[i];
2119 } else
2120 off++;
2121 }
2122 attr[i - off] = NULL;
2123
2124 *str = malloc(len + 1 + 2);
2125 if (!*str)
2126 return -1;
2127 s = *str;
2128 if (i - off == 0) {
2129 *s = 0;
2130 return 0;
2131 }
2132 if (use_group)
2133 *s++ = '{';
2134 for (i = 0; attr[i]; i++) {
2135 strcpy(s, attr[i]);
2136 s += strlen(s);
2137 *s++ = ',';
2138 }
2139 if (use_group) {
2140 s[-1] = '}';
2141 *s = 0;
2142 } else
2143 s[-1] = 0;
2144 return 0;
2145}
2146
2147__weak bool arch_topdown_check_group(bool *warn)
2148{
2149 *warn = false;
2150 return false;
2151}
2152
2153__weak void arch_topdown_group_warn(void)
2154{
2155}
2156
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002157/*
2158 * Add default attributes, if there were no attributes specified or
2159 * if -d/--detailed, -d -d or -d -d -d is used:
2160 */
2161static int add_default_attributes(void)
2162{
Andi Kleen44b1e602016-05-30 12:49:42 -03002163 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002164 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002165
2166 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2167 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2168 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2169 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2170
2171 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002172};
2173 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002174 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002175};
2176 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002177 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002178};
2179 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002180 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2181 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2182 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2183
2184};
2185
2186/*
2187 * Detailed stats (-d), covering the L1 and last level data caches:
2188 */
2189 struct perf_event_attr detailed_attrs[] = {
2190
2191 { .type = PERF_TYPE_HW_CACHE,
2192 .config =
2193 PERF_COUNT_HW_CACHE_L1D << 0 |
2194 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2195 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2196
2197 { .type = PERF_TYPE_HW_CACHE,
2198 .config =
2199 PERF_COUNT_HW_CACHE_L1D << 0 |
2200 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2201 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2202
2203 { .type = PERF_TYPE_HW_CACHE,
2204 .config =
2205 PERF_COUNT_HW_CACHE_LL << 0 |
2206 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2207 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2208
2209 { .type = PERF_TYPE_HW_CACHE,
2210 .config =
2211 PERF_COUNT_HW_CACHE_LL << 0 |
2212 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2213 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2214};
2215
2216/*
2217 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2218 */
2219 struct perf_event_attr very_detailed_attrs[] = {
2220
2221 { .type = PERF_TYPE_HW_CACHE,
2222 .config =
2223 PERF_COUNT_HW_CACHE_L1I << 0 |
2224 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2225 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2226
2227 { .type = PERF_TYPE_HW_CACHE,
2228 .config =
2229 PERF_COUNT_HW_CACHE_L1I << 0 |
2230 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2231 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2232
2233 { .type = PERF_TYPE_HW_CACHE,
2234 .config =
2235 PERF_COUNT_HW_CACHE_DTLB << 0 |
2236 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2237 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2238
2239 { .type = PERF_TYPE_HW_CACHE,
2240 .config =
2241 PERF_COUNT_HW_CACHE_DTLB << 0 |
2242 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2243 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2244
2245 { .type = PERF_TYPE_HW_CACHE,
2246 .config =
2247 PERF_COUNT_HW_CACHE_ITLB << 0 |
2248 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2249 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2250
2251 { .type = PERF_TYPE_HW_CACHE,
2252 .config =
2253 PERF_COUNT_HW_CACHE_ITLB << 0 |
2254 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2255 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2256
2257};
2258
2259/*
2260 * Very, very detailed stats (-d -d -d), adding prefetch events:
2261 */
2262 struct perf_event_attr very_very_detailed_attrs[] = {
2263
2264 { .type = PERF_TYPE_HW_CACHE,
2265 .config =
2266 PERF_COUNT_HW_CACHE_L1D << 0 |
2267 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2268 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2269
2270 { .type = PERF_TYPE_HW_CACHE,
2271 .config =
2272 PERF_COUNT_HW_CACHE_L1D << 0 |
2273 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2274 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2275};
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002276 struct parse_events_error errinfo;
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002277
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002278 /* Set attrs if no event is selected and !null_run: */
2279 if (null_run)
2280 return 0;
2281
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002282 if (transaction_run) {
Thomas Richter742d92f2018-06-26 09:17:01 +02002283 /* Handle -T as -M transaction. Once platform specific metrics
2284 * support has been added to the json files, all archictures
2285 * will use this approach. To determine transaction support
2286 * on an architecture test for such a metric name.
2287 */
2288 if (metricgroup__has_metric("transaction")) {
2289 struct option opt = { .value = &evsel_list };
2290
2291 return metricgroup__parse_groups(&opt, "transaction",
2292 &metric_events);
2293 }
2294
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002295 if (pmu_have_event("cpu", "cycles-ct") &&
2296 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002297 err = parse_events(evsel_list, transaction_attrs,
2298 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002299 else
Thomas Richterfca323402018-03-08 15:57:35 +01002300 err = parse_events(evsel_list,
2301 transaction_limited_attrs,
2302 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002303 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002304 fprintf(stderr, "Cannot set up transaction events\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002305 parse_events_print_error(&errinfo, transaction_attrs);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002306 return -1;
2307 }
2308 return 0;
2309 }
2310
Kan Liangdaefd0b2017-05-26 12:05:38 -07002311 if (smi_cost) {
2312 int smi;
2313
2314 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2315 fprintf(stderr, "freeze_on_smi is not supported.\n");
2316 return -1;
2317 }
2318
2319 if (!smi) {
2320 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2321 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2322 return -1;
2323 }
2324 smi_reset = true;
2325 }
2326
2327 if (pmu_have_event("msr", "aperf") &&
2328 pmu_have_event("msr", "smi")) {
2329 if (!force_metric_only)
2330 metric_only = true;
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002331 err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002332 } else {
2333 fprintf(stderr, "To measure SMI cost, it needs "
2334 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002335 parse_events_print_error(&errinfo, smi_cost_attrs);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002336 return -1;
2337 }
2338 if (err) {
2339 fprintf(stderr, "Cannot set up SMI cost events\n");
2340 return -1;
2341 }
2342 return 0;
2343 }
2344
Andi Kleen44b1e602016-05-30 12:49:42 -03002345 if (topdown_run) {
2346 char *str = NULL;
2347 bool warn = false;
2348
2349 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2350 stat_config.aggr_mode != AGGR_CORE) {
2351 pr_err("top down event configuration requires --per-core mode\n");
2352 return -1;
2353 }
2354 stat_config.aggr_mode = AGGR_CORE;
2355 if (nr_cgroups || !target__has_cpu(&target)) {
2356 pr_err("top down event configuration requires system-wide mode (-a)\n");
2357 return -1;
2358 }
2359
2360 if (!force_metric_only)
2361 metric_only = true;
2362 if (topdown_filter_events(topdown_attrs, &str,
2363 arch_topdown_check_group(&warn)) < 0) {
2364 pr_err("Out of memory\n");
2365 return -1;
2366 }
2367 if (topdown_attrs[0] && str) {
2368 if (warn)
2369 arch_topdown_group_warn();
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002370 err = parse_events(evsel_list, str, &errinfo);
Andi Kleen44b1e602016-05-30 12:49:42 -03002371 if (err) {
2372 fprintf(stderr,
2373 "Cannot set up top down events %s: %d\n",
2374 str, err);
2375 free(str);
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002376 parse_events_print_error(&errinfo, str);
Andi Kleen44b1e602016-05-30 12:49:42 -03002377 return -1;
2378 }
2379 } else {
2380 fprintf(stderr, "System does not support topdown\n");
2381 return -1;
2382 }
2383 free(str);
2384 }
2385
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002386 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002387 if (target__has_cpu(&target))
2388 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2389
Andi Kleen9dec4472016-02-26 16:27:56 -08002390 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2391 return -1;
2392 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2393 if (perf_evlist__add_default_attrs(evsel_list,
2394 frontend_attrs) < 0)
2395 return -1;
2396 }
2397 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2398 if (perf_evlist__add_default_attrs(evsel_list,
2399 backend_attrs) < 0)
2400 return -1;
2401 }
2402 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002403 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002404 }
2405
2406 /* Detailed events get appended to the event list: */
2407
2408 if (detailed_run < 1)
2409 return 0;
2410
2411 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002412 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002413 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002414
2415 if (detailed_run < 2)
2416 return 0;
2417
2418 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002419 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002420 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002421
2422 if (detailed_run < 3)
2423 return 0;
2424
2425 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002426 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002427}
2428
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002429static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002430 "perf stat record [<options>]",
2431 NULL,
2432};
2433
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002434static void init_features(struct perf_session *session)
2435{
2436 int feat;
2437
2438 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2439 perf_header__set_feat(&session->header, feat);
2440
2441 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2442 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2443 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2444 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2445}
2446
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002447static int __cmd_record(int argc, const char **argv)
2448{
2449 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002450 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002451
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002452 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002453 PARSE_OPT_STOP_AT_NON_OPTION);
2454
2455 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002456 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002457
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002458 if (run_count != 1 || forever) {
2459 pr_err("Cannot use -r option with perf stat record.\n");
2460 return -1;
2461 }
2462
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002463 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002464 if (session == NULL) {
2465 pr_err("Perf session creation failed.\n");
2466 return -1;
2467 }
2468
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002469 init_features(session);
2470
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002471 session->evlist = evsel_list;
2472 perf_stat.session = session;
2473 perf_stat.record = true;
2474 return argc;
2475}
2476
Jiri Olsaa56f9392015-11-05 15:40:59 +01002477static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2478 union perf_event *event,
2479 struct perf_session *session)
2480{
Andi Kleene3b03b62016-05-05 16:04:03 -07002481 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002482 struct perf_evsel *counter;
2483 struct timespec tsh, *ts = NULL;
2484 const char **argv = session->header.env.cmdline_argv;
2485 int argc = session->header.env.nr_cmdline;
2486
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002487 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002488 perf_stat_process_counter(&stat_config, counter);
2489
Andi Kleene3b03b62016-05-05 16:04:03 -07002490 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2491 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002492
Andi Kleene3b03b62016-05-05 16:04:03 -07002493 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002494 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2495 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002496 ts = &tsh;
2497 }
2498
2499 print_counters(ts, argc, argv);
2500 return 0;
2501}
2502
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002503static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002504int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002505 union perf_event *event,
2506 struct perf_session *session __maybe_unused)
2507{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002508 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2509
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002510 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002511
Jiri Olsa89af4e02015-11-05 15:41:02 +01002512 if (cpu_map__empty(st->cpus)) {
2513 if (st->aggr_mode != AGGR_UNSET)
2514 pr_warning("warning: processing task data, aggregation mode not set\n");
2515 return 0;
2516 }
2517
2518 if (st->aggr_mode != AGGR_UNSET)
2519 stat_config.aggr_mode = st->aggr_mode;
2520
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002521 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002522 perf_stat_init_aggr_mode();
2523 else
2524 perf_stat_init_aggr_mode_file(st);
2525
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002526 return 0;
2527}
2528
Jiri Olsa1975d362015-11-05 15:40:56 +01002529static int set_maps(struct perf_stat *st)
2530{
2531 if (!st->cpus || !st->threads)
2532 return 0;
2533
2534 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2535 return -EINVAL;
2536
2537 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2538
2539 if (perf_evlist__alloc_stats(evsel_list, true))
2540 return -ENOMEM;
2541
2542 st->maps_allocated = true;
2543 return 0;
2544}
2545
2546static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002547int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002548 union perf_event *event,
2549 struct perf_session *session __maybe_unused)
2550{
2551 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2552
2553 if (st->threads) {
2554 pr_warning("Extra thread map event, ignoring.\n");
2555 return 0;
2556 }
2557
2558 st->threads = thread_map__new_event(&event->thread_map);
2559 if (!st->threads)
2560 return -ENOMEM;
2561
2562 return set_maps(st);
2563}
2564
2565static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002566int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002567 union perf_event *event,
2568 struct perf_session *session __maybe_unused)
2569{
2570 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2571 struct cpu_map *cpus;
2572
2573 if (st->cpus) {
2574 pr_warning("Extra cpu map event, ignoring.\n");
2575 return 0;
2576 }
2577
2578 cpus = cpu_map__new_data(&event->cpu_map.data);
2579 if (!cpus)
2580 return -ENOMEM;
2581
2582 st->cpus = cpus;
2583 return set_maps(st);
2584}
2585
Jin Yao56739442017-12-05 22:03:07 +08002586static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2587{
2588 int i;
2589
2590 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2591 if (!config->stats)
2592 return -1;
2593
2594 config->stats_num = nthreads;
2595
2596 for (i = 0; i < nthreads; i++)
2597 runtime_stat__init(&config->stats[i]);
2598
2599 return 0;
2600}
2601
2602static void runtime_stat_delete(struct perf_stat_config *config)
2603{
2604 int i;
2605
2606 if (!config->stats)
2607 return;
2608
2609 for (i = 0; i < config->stats_num; i++)
2610 runtime_stat__exit(&config->stats[i]);
2611
2612 free(config->stats);
2613}
2614
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002615static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002616 "perf stat report [<options>]",
2617 NULL,
2618};
2619
2620static struct perf_stat perf_stat = {
2621 .tool = {
2622 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002623 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002624 .thread_map = process_thread_map_event,
2625 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002626 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002627 .stat = perf_event__process_stat_event,
2628 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002629 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002630 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002631};
2632
2633static int __cmd_report(int argc, const char **argv)
2634{
2635 struct perf_session *session;
2636 const struct option options[] = {
2637 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002638 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2639 "aggregate counts per processor socket", AGGR_SOCKET),
2640 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2641 "aggregate counts per physical processor core", AGGR_CORE),
2642 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2643 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002644 OPT_END()
2645 };
2646 struct stat st;
2647 int ret;
2648
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002649 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002650
2651 if (!input_name || !strlen(input_name)) {
2652 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2653 input_name = "-";
2654 else
2655 input_name = "perf.data";
2656 }
2657
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002658 perf_stat.data.file.path = input_name;
2659 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002660
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002661 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002662 if (session == NULL)
2663 return -1;
2664
2665 perf_stat.session = session;
2666 stat_config.output = stderr;
2667 evsel_list = session->evlist;
2668
2669 ret = perf_session__process_events(session);
2670 if (ret)
2671 return ret;
2672
2673 perf_session__delete(session);
2674 return 0;
2675}
2676
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002677static void setup_system_wide(int forks)
2678{
2679 /*
2680 * Make system wide (-a) the default target if
2681 * no target was specified and one of following
2682 * conditions is met:
2683 *
2684 * - there's no workload specified
2685 * - there is workload specified but all requested
2686 * events are system wide events
2687 */
2688 if (!target__none(&target))
2689 return;
2690
2691 if (!forks)
2692 target.system_wide = true;
2693 else {
2694 struct perf_evsel *counter;
2695
2696 evlist__for_each_entry(evsel_list, counter) {
2697 if (!counter->system_wide)
2698 return;
2699 }
2700
2701 if (evsel_list->nr_entries)
2702 target.system_wide = true;
2703 }
2704}
2705
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002706int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002707{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002708 const char * const stat_usage[] = {
2709 "perf stat [<options>] [<command>]",
2710 NULL
2711 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002712 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002713 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002714 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002715 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002716 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002717
Stephane Eranian5af52b52010-05-18 15:00:01 +02002718 setlocale(LC_ALL, "");
2719
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002720 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002721 if (evsel_list == NULL)
2722 return -ENOMEM;
2723
Wang Nan1669e502016-02-19 11:43:58 +00002724 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002725 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2726 (const char **) stat_usage,
2727 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002728 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002729 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002730
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002731 if (csv_sep) {
2732 csv_output = true;
2733 if (!strcmp(csv_sep, "\\t"))
2734 csv_sep = "\t";
2735 } else
2736 csv_sep = DEFAULT_SEPARATOR;
2737
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002738 if (argc && !strncmp(argv[0], "rec", 3)) {
2739 argc = __cmd_record(argc, argv);
2740 if (argc < 0)
2741 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002742 } else if (argc && !strncmp(argv[0], "rep", 3))
2743 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002744
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002745 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002746 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002747
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002748 /*
2749 * For record command the -o is already taken care of.
2750 */
2751 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002752 output = NULL;
2753
Jim Cromie56f3bae2011-09-07 17:14:00 -06002754 if (output_name && output_fd) {
2755 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002756 parse_options_usage(stat_usage, stat_options, "o", 1);
2757 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002758 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002759 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002760
Andi Kleen54b50912016-03-03 15:57:36 -08002761 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2762 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2763 goto out;
2764 }
2765
Andi Kleen54b50912016-03-03 15:57:36 -08002766 if (metric_only && run_count > 1) {
2767 fprintf(stderr, "--metric-only is not supported with -r\n");
2768 goto out;
2769 }
2770
Jiri Olsae55c14a2018-04-23 11:08:21 +02002771 if (walltime_run_table && run_count <= 1) {
2772 fprintf(stderr, "--table is only supported with -r\n");
2773 parse_options_usage(stat_usage, stat_options, "r", 1);
2774 parse_options_usage(NULL, stat_options, "table", 0);
2775 goto out;
2776 }
2777
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002778 if (output_fd < 0) {
2779 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002780 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002781 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002782 }
2783
Stephane Eranian4aa90152011-08-15 22:22:33 +02002784 if (!output) {
2785 struct timespec tm;
2786 mode = append_file ? "a" : "w";
2787
2788 output = fopen(output_name, mode);
2789 if (!output) {
2790 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002791 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002792 }
2793 clock_gettime(CLOCK_REALTIME, &tm);
2794 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002795 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002796 mode = append_file ? "a" : "w";
2797 output = fdopen(output_fd, mode);
2798 if (!output) {
2799 perror("Failed opening logfd");
2800 return -errno;
2801 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002802 }
2803
Jiri Olsa58215222015-07-21 14:31:24 +02002804 stat_config.output = output;
2805
Stephane Eraniand7470b62010-12-01 18:49:05 +02002806 /*
2807 * let the spreadsheet do the pretty-printing
2808 */
2809 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002810 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002811 if (big_num_opt == 1) {
2812 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002813 parse_options_usage(stat_usage, stat_options, "B", 1);
2814 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002815 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002816 } else /* Nope, so disable big number formatting */
2817 big_num = false;
2818 } else if (big_num_opt == 0) /* User passed --no-big-num */
2819 big_num = false;
2820
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002821 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002822
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002823 /*
2824 * Display user/system times only for single
2825 * run and when there's specified tracee.
2826 */
2827 if ((run_count == 1) && target__none(&target))
2828 ru_display = true;
2829
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002830 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002831 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002832 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002833 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002834 } else if (run_count == 0) {
2835 forever = true;
2836 run_count = 1;
2837 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002838
Jiri Olsae55c14a2018-04-23 11:08:21 +02002839 if (walltime_run_table) {
2840 walltime_run = zalloc(run_count * sizeof(walltime_run[0]));
2841 if (!walltime_run) {
2842 pr_err("failed to setup -r option");
2843 goto out;
2844 }
2845 }
2846
Jin Yao1d9f8d12017-12-05 22:03:10 +08002847 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2848 !target__has_task(&target)) {
2849 if (!target.system_wide || target.cpu_list) {
2850 fprintf(stderr, "The --per-thread option is only "
2851 "available when monitoring via -p -t -a "
2852 "options or only --per-thread.\n");
2853 parse_options_usage(NULL, stat_options, "p", 1);
2854 parse_options_usage(NULL, stat_options, "t", 1);
2855 goto out;
2856 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002857 }
2858
2859 /*
2860 * no_aggr, cgroup are for system-wide only
2861 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2862 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002863 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2864 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002865 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002866 fprintf(stderr, "both cgroup and no-aggregation "
2867 "modes only available in system-wide mode\n");
2868
Jiri Olsae0547312015-11-05 15:40:45 +01002869 parse_options_usage(stat_usage, stat_options, "G", 1);
2870 parse_options_usage(NULL, stat_options, "A", 1);
2871 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002872 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002873 }
2874
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002875 if (add_default_attributes())
2876 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002877
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002878 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002879
Jin Yao1d9f8d12017-12-05 22:03:10 +08002880 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2881 target.per_thread = true;
2882
Namhyung Kim77a6f012012-05-07 14:09:04 +09002883 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002884 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002885 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002886 parse_options_usage(stat_usage, stat_options, "p", 1);
2887 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002888 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002889 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002890 parse_options_usage(stat_usage, stat_options, "C", 1);
2891 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002892 }
2893 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002894 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002895
2896 /*
2897 * Initialize thread_map with comm names,
2898 * so we could print it out on output.
2899 */
Jin Yao56739442017-12-05 22:03:07 +08002900 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002901 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08002902 if (target.system_wide) {
2903 if (runtime_stat_new(&stat_config,
2904 thread_map__nr(evsel_list->threads))) {
2905 goto out;
2906 }
2907 }
2908 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002909
yuzhoujiandb06a262018-01-29 10:25:22 +01002910 if (stat_config.times && interval)
2911 interval_count = true;
2912 else if (stat_config.times && !interval) {
2913 pr_err("interval-count option should be used together with "
2914 "interval-print.\n");
2915 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2916 parse_options_usage(stat_usage, stat_options, "I", 1);
2917 goto out;
2918 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002919
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002920 if (timeout && timeout < 100) {
2921 if (timeout < 10) {
2922 pr_err("timeout must be >= 10ms.\n");
2923 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2924 goto out;
2925 } else
2926 pr_warning("timeout < 100ms. "
2927 "The overhead percentage could be high in some cases. "
2928 "Please proceed with caution.\n");
2929 }
2930 if (timeout && interval) {
2931 pr_err("timeout option is not supported with interval-print.\n");
2932 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2933 parse_options_usage(stat_usage, stat_options, "I", 1);
2934 goto out;
2935 }
2936
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002937 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002938 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002939
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002940 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002941 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002942
Ingo Molnar58d7e992009-05-15 11:03:23 +02002943 /*
Jiri Olsa7d9ad162018-08-30 08:32:14 +02002944 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
2945 * while avoiding that older tools show confusing messages.
2946 *
2947 * However for pipe sessions we need to keep it zero,
2948 * because script's perf_evsel__check_attr is triggered
2949 * by attr->sample_type != 0, and we can't run it on
2950 * stat sessions.
2951 */
2952 stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
2953
2954 /*
Ingo Molnar58d7e992009-05-15 11:03:23 +02002955 * We dont want to block the signals - that would cause
2956 * child tasks to inherit that and Ctrl-C would not work.
2957 * What we want is for Ctrl-C to work in the exec()-ed
2958 * task, but being ignored by perf stat itself:
2959 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002960 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002961 if (!forever)
2962 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002963 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002964 signal(SIGALRM, skip_signal);
2965 signal(SIGABRT, skip_signal);
2966
Ingo Molnar42202dd2009-06-13 14:57:28 +02002967 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002968 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002969 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002970 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2971 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002972
Jiri Olsae55c14a2018-04-23 11:08:21 +02002973 status = run_perf_stat(argc, argv, run_idx);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002974 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002975 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002976 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002977 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002978 }
2979
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002980 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002981 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002982
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002983 if (STAT_RECORD) {
2984 /*
2985 * We synthesize the kernel mmap record just so that older tools
2986 * don't emit warnings about not being able to resolve symbols
2987 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2988 * a saner message about no samples being in the perf.data file.
2989 *
2990 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002991 * in header.c at the moment 'perf stat record' gets introduced, which
2992 * is not really needed once we start adding the stat specific PERF_RECORD_
2993 * records, but the need to suppress the kptr_restrict messages in older
2994 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002995 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002996 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002997 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2998 process_synthesized_event,
2999 &perf_stat.session->machines.host);
3000 if (err) {
3001 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3002 "older tools may produce warnings about this file\n.");
3003 }
3004
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003005 if (!interval) {
3006 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3007 pr_err("failed to write stat round event\n");
3008 }
3009
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003010 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003011 perf_stat.session->header.data_size += perf_stat.bytes_written;
3012 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3013 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003014
3015 perf_session__delete(perf_stat.session);
3016 }
3017
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003018 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003019 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003020out:
Jiri Olsae55c14a2018-04-23 11:08:21 +02003021 free(walltime_run);
3022
Kan Liangdaefd0b2017-05-26 12:05:38 -07003023 if (smi_cost && smi_reset)
3024 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3025
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003026 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003027
3028 runtime_stat_delete(&stat_config);
3029
Ingo Molnar42202dd2009-06-13 14:57:28 +02003030 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003031}