blob: f4ddedc8619b82338e7b48b95b21c7117a2a94d4 [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
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300140static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900141 .uid = UINT_MAX,
142};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530143
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200144#define METRIC_ONLY_LEN 20
145
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200146static volatile pid_t child_pid = -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200147static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700148static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300149static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700150static bool smi_cost = false;
151static bool smi_reset = false;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200152static int big_num_opt = -1;
Lin Ming43bece72011-08-17 18:42:07 +0800153static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200154static const char *pre_cmd = NULL;
155static const char *post_cmd = NULL;
156static bool sync_run = false;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500157static bool forever = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300158static bool force_metric_only = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100159static struct timespec ref_time;
Jiri Olsae0547312015-11-05 15:40:45 +0100160static bool append_file;
yuzhoujiandb06a262018-01-29 10:25:22 +0100161static bool interval_count;
Jiri Olsae0547312015-11-05 15:40:45 +0100162static const char *output_name;
163static int output_fd;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200164
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100165struct perf_stat {
166 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100167 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100168 struct perf_session *session;
169 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100170 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100171 bool maps_allocated;
172 struct cpu_map *cpus;
173 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100174 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100175};
176
177static struct perf_stat perf_stat;
178#define STAT_RECORD perf_stat.record
179
Liming Wang60666c62009-12-31 16:05:50 +0800180static volatile int done = 0;
181
Jiri Olsa421a50f2015-07-21 14:31:22 +0200182static struct perf_stat_config stat_config = {
Jiri Olsa26893a62018-08-30 08:32:40 +0200183 .aggr_mode = AGGR_GLOBAL,
184 .scale = true,
185 .unit_width = 4, /* strlen("unit") */
186 .run_count = 1,
187 .metric_only_len = METRIC_ONLY_LEN,
188 .walltime_nsecs_stats = &walltime_nsecs_stats,
Jiri Olsa34ff0862018-08-30 08:32:47 +0200189 .big_num = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200190};
191
Andi Kleene864c5c2017-08-31 12:40:35 -0700192static bool is_duration_time(struct perf_evsel *evsel)
193{
194 return !strcmp(evsel->name, "duration_time");
195}
196
Stephane Eranian13370a92013-01-29 12:47:44 +0100197static inline void diff_timespec(struct timespec *r, struct timespec *a,
198 struct timespec *b)
199{
200 r->tv_sec = a->tv_sec - b->tv_sec;
201 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300202 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100203 r->tv_sec--;
204 } else {
205 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
206 }
207}
208
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200209static void perf_stat__reset_stats(void)
210{
Jin Yao56739442017-12-05 22:03:07 +0800211 int i;
212
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200213 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200214 perf_stat__reset_shadow_stats();
Jin Yao56739442017-12-05 22:03:07 +0800215
216 for (i = 0; i < stat_config.stats_num; i++)
217 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200218}
219
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100220static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100221 union perf_event *event,
222 struct perf_sample *sample __maybe_unused,
223 struct machine *machine __maybe_unused)
224{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100225 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100226 pr_err("failed to write perf data, error: %m\n");
227 return -1;
228 }
229
230 perf_stat.bytes_written += event->header.size;
231 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100232}
233
Jiri Olsa1975d362015-11-05 15:40:56 +0100234static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100235{
Jiri Olsa1975d362015-11-05 15:40:56 +0100236 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100237 process_synthesized_event,
238 NULL);
239}
240
241#define WRITE_STAT_ROUND_EVENT(time, interval) \
242 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
243
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100244#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
245
246static int
247perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
248 struct perf_counts_values *count)
249{
250 struct perf_sample_id *sid = SID(counter, cpu, thread);
251
252 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
253 process_synthesized_event, NULL);
254}
255
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200256/*
257 * Read out the results of a single counter:
258 * do not aggregate counts across CPUs in system-wide mode
259 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200260static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200261{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100262 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100263 int ncpus, cpu, thread;
264
Jin Yao1d9f8d12017-12-05 22:03:10 +0800265 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Mark Rutland00e727b2016-07-15 11:08:10 +0100266 ncpus = perf_evsel__nr_cpus(counter);
267 else
268 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200269
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000270 if (!counter->supported)
271 return -ENOENT;
272
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100273 if (counter->system_wide)
274 nthreads = 1;
275
276 for (thread = 0; thread < nthreads; thread++) {
277 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200278 struct perf_counts_values *count;
279
280 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200281
282 /*
283 * The leader's group read loads data into its group members
284 * (via perf_evsel__read_counter) and sets threir count->loaded.
285 */
286 if (!count->loaded &&
287 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700288 counter->counts->scaled = -1;
289 perf_counts(counter->counts, cpu, thread)->ena = 0;
290 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100291 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700292 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100293
Jiri Olsa82bf3112017-07-26 14:02:06 +0200294 count->loaded = false;
295
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100296 if (STAT_RECORD) {
297 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
298 pr_err("failed to write stat event\n");
299 return -1;
300 }
301 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700302
303 if (verbose > 1) {
304 fprintf(stat_config.output,
305 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
306 perf_evsel__name(counter),
307 cpu,
308 count->val, count->ena, count->run);
309 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100310 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200311 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200312
313 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200314}
315
Mark Rutland3df33ef2016-08-09 14:04:29 +0100316static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200317{
318 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700319 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200320
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300321 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700322 ret = read_counter(counter);
323 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700324 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200325
Stephane Eraniandb49a712017-04-12 11:23:01 -0700326 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200327 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200328 }
329}
330
Jiri Olsaba411a92015-06-26 11:29:24 +0200331static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100332{
Stephane Eranian13370a92013-01-29 12:47:44 +0100333 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100334
Mark Rutland3df33ef2016-08-09 14:04:29 +0100335 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100336
Stephane Eranian13370a92013-01-29 12:47:44 +0100337 clock_gettime(CLOCK_MONOTONIC, &ts);
338 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100339
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100340 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300341 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100342 pr_err("failed to write stat round event\n");
343 }
344
Andi Kleenb90f1332017-08-31 12:40:36 -0700345 init_stats(&walltime_nsecs_stats);
346 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200347 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100348}
349
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100350static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700351{
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200352 if (stat_config.initial_delay)
353 usleep(stat_config.initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100354
355 /*
356 * We need to enable counters only if:
357 * - we don't have tracee (attaching to task or cpu)
358 * - we have initial delay configured
359 */
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200360 if (!target__none(&target) || stat_config.initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100361 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700362}
363
Mark Rutland3df33ef2016-08-09 14:04:29 +0100364static void disable_counters(void)
365{
366 /*
367 * If we don't have tracee (attaching to task or cpu), counters may
368 * still be running. To get accurate group ratios, we must stop groups
369 * from counting before reading their constituent counters.
370 */
371 if (!target__none(&target))
372 perf_evlist__disable(evsel_list);
373}
374
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300375static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300376
377/*
378 * perf_evlist__prepare_workload will send a SIGUSR1
379 * if the fork fails, since we asked by setting its
380 * want_signal to true.
381 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300382static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
383 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300384{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300385 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300386}
387
Jiri Olsa82bf3112017-07-26 14:02:06 +0200388static bool perf_evsel__should_store_id(struct perf_evsel *counter)
389{
390 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
391}
392
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700393static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
394{
395 struct perf_evsel *c2, *leader;
396 bool is_open = true;
397
398 leader = evsel->leader;
399 pr_debug("Weak group for %s/%d failed\n",
400 leader->name, leader->nr_members);
401
402 /*
403 * for_each_group_member doesn't work here because it doesn't
404 * include the first entry.
405 */
406 evlist__for_each_entry(evsel_list, c2) {
407 if (c2 == evsel)
408 is_open = false;
409 if (c2->leader == leader) {
410 if (is_open)
411 perf_evsel__close(c2);
412 c2->leader = c2;
413 c2->nr_members = 0;
414 }
415 }
416 return leader;
417}
418
Jiri Olsae55c14a2018-04-23 11:08:21 +0200419static int __run_perf_stat(int argc, const char **argv, int run_idx)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200420{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200421 int interval = stat_config.interval;
yuzhoujiandb06a262018-01-29 10:25:22 +0100422 int times = stat_config.times;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100423 int timeout = stat_config.timeout;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300424 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200425 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100426 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100427 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100428 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200429 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300430 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100431 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600432 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200433
Stephane Eranian13370a92013-01-29 12:47:44 +0100434 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300435 ts.tv_sec = interval / USEC_PER_MSEC;
436 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100437 } else if (timeout) {
438 ts.tv_sec = timeout / USEC_PER_MSEC;
439 ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100440 } else {
441 ts.tv_sec = 1;
442 ts.tv_nsec = 0;
443 }
444
Liming Wang60666c62009-12-31 16:05:50 +0800445 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100446 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300447 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900448 perror("failed to prepare workload");
449 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800450 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900451 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000452 }
453
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200454 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300455 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200456
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300457 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300458try_again:
Jiri Olsad09cefd2018-08-30 08:32:17 +0200459 if (create_perf_stat_counter(counter, &stat_config, &target) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700460
461 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700462 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700463 counter->leader != counter &&
464 counter->weak_group) {
465 counter = perf_evsel__reset_weak_group(counter);
466 goto try_again;
467 }
468
David Ahern979987a2012-05-08 09:29:16 -0600469 /*
470 * PPC returns ENXIO for HW counters until 2.6.37
471 * (behavior changed with commit b0a873e).
472 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100473 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600474 errno == ENOENT || errno == EOPNOTSUPP ||
475 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900476 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600477 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300478 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600479 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400480
481 if ((counter->leader != counter) ||
482 !(counter->leader->nr_members > 1))
483 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300484 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900485 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300486 ui__warning("%s\n", msg);
487 goto try_again;
Jin Yaoab6c79b2018-01-16 23:43:08 +0800488 } else if (target__has_per_thread(&target) &&
489 evsel_list->threads &&
490 evsel_list->threads->err_thread != -1) {
491 /*
492 * For global --per-thread case, skip current
493 * error thread.
494 */
495 if (!thread_map__remove(evsel_list->threads,
496 evsel_list->threads->err_thread)) {
497 evsel_list->threads->err_thread = -1;
498 goto try_again;
499 }
500 }
Ingo Molnarede70292011-04-28 08:48:42 +0200501
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300502 perf_evsel__open_strerror(counter, &target,
503 errno, msg, sizeof(msg));
504 ui__error("%s\n", msg);
505
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200506 if (child_pid != -1)
507 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600508
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200509 return -1;
510 }
David Ahern2cee77c2011-05-30 08:55:59 -0600511 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100512
513 l = strlen(counter->unit);
Jiri Olsadf4f7b42018-08-30 08:32:32 +0200514 if (l > stat_config.unit_width)
515 stat_config.unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100516
Jiri Olsa82bf3112017-07-26 14:02:06 +0200517 if (perf_evsel__should_store_id(counter) &&
Jiri Olsa650d6222018-08-30 08:32:16 +0200518 perf_evsel__store_ids(counter, evsel_list))
Jiri Olsa2af46462015-11-05 15:40:49 +0100519 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300520 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200521
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300522 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300523 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300524 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300525 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100526 return -1;
527 }
528
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600529 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300530 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600531 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
532 str_error_r(errno, msg, sizeof(msg)));
533 return -1;
534 }
535
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100536 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100537 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100538
Jiri Olsa664c98d2015-11-05 15:40:50 +0100539 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100540 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100541 } else {
542 err = perf_session__write_header(perf_stat.session, evsel_list,
543 fd, false);
544 }
545
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100546 if (err < 0)
547 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100548
Jiri Olsa1c21e982018-08-30 08:32:21 +0200549 err = perf_stat_synthesize_config(&stat_config, NULL, evsel_list,
Jiri Olsac2c247f2018-08-30 08:32:22 +0200550 process_synthesized_event, is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100551 if (err < 0)
552 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100553 }
554
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200555 /*
556 * Enable counters and exec the command:
557 */
558 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100559 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200560
Liming Wang60666c62009-12-31 16:05:50 +0800561 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900562 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100563 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900564
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100565 if (interval || timeout) {
Stephane Eranian13370a92013-01-29 12:47:44 +0100566 while (!waitpid(child_pid, &status, WNOHANG)) {
567 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100568 if (timeout)
569 break;
Jiri Olsaba411a92015-06-26 11:29:24 +0200570 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100571 if (interval_count && !(--times))
572 break;
Stephane Eranian13370a92013-01-29 12:47:44 +0100573 }
574 }
Jiri Olsa8897a892018-08-30 08:32:44 +0200575 wait4(child_pid, &status, 0, &stat_config.ru_data);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300576
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300577 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300578 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300579 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300580 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300581 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300582
Andi Kleen33e49ea2011-09-15 14:31:40 -0700583 if (WIFSIGNALED(status))
584 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800585 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100586 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100587 while (!done) {
588 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100589 if (timeout)
590 break;
yuzhoujiandb06a262018-01-29 10:25:22 +0100591 if (interval) {
Jiri Olsaba411a92015-06-26 11:29:24 +0200592 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100593 if (interval_count && !(--times))
594 break;
595 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100596 }
Liming Wang60666c62009-12-31 16:05:50 +0800597 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200598
Mark Rutland3df33ef2016-08-09 14:04:29 +0100599 disable_counters();
600
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200601 t1 = rdclock();
602
Jiri Olsa54ac0b12018-08-30 08:32:50 +0200603 if (stat_config.walltime_run_table)
604 stat_config.walltime_run[run_idx] = t1 - t0;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200605
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200606 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200607
Mark Rutland3df33ef2016-08-09 14:04:29 +0100608 /*
609 * Closing a group leader splits the group, and as we only disable
610 * group leaders, results in remaining events becoming enabled. To
611 * avoid arbitrary skew, we must read all counters before closing any
612 * group leaders.
613 */
614 read_counters();
615 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200616
Ingo Molnar42202dd2009-06-13 14:57:28 +0200617 return WEXITSTATUS(status);
618}
619
Jiri Olsae55c14a2018-04-23 11:08:21 +0200620static int run_perf_stat(int argc, const char **argv, int run_idx)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200621{
622 int ret;
623
624 if (pre_cmd) {
625 ret = system(pre_cmd);
626 if (ret)
627 return ret;
628 }
629
630 if (sync_run)
631 sync();
632
Jiri Olsae55c14a2018-04-23 11:08:21 +0200633 ret = __run_perf_stat(argc, argv, run_idx);
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200634 if (ret)
635 return ret;
636
637 if (post_cmd) {
638 ret = system(post_cmd);
639 if (ret)
640 return ret;
641 }
642
643 return ret;
644}
645
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200646static void print_running(struct perf_stat_config *config,
647 u64 run, u64 ena)
Andi Kleend73515c2015-03-11 07:16:27 -0700648{
Jiri Olsafa7070a2018-08-30 08:32:29 +0200649 if (config->csv_output) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200650 fprintf(config->output, "%s%" PRIu64 "%s%.2f",
Jiri Olsafa7070a2018-08-30 08:32:29 +0200651 config->csv_sep,
Andi Kleend73515c2015-03-11 07:16:27 -0700652 run,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200653 config->csv_sep,
Andi Kleend73515c2015-03-11 07:16:27 -0700654 ena ? 100.0 * run / ena : 100.0);
655 } else if (run != ena) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200656 fprintf(config->output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700657 }
658}
659
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200660static void print_noise_pct(struct perf_stat_config *config,
661 double total, double avg)
Ingo Molnarf99844c2011-04-27 05:35:39 +0200662{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800663 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200664
Jiri Olsafa7070a2018-08-30 08:32:29 +0200665 if (config->csv_output)
666 fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600667 else if (pct)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200668 fprintf(config->output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200669}
670
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200671static void print_noise(struct perf_stat_config *config,
672 struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200673{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200674 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200675
Jiri Olsad97ae042018-08-30 08:32:36 +0200676 if (config->run_count == 1)
Peter Zijlstra849abde2009-09-04 18:23:38 +0200677 return;
678
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300679 ps = evsel->stats;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200680 print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200681}
682
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200683static void aggr_printout(struct perf_stat_config *config,
684 struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200685{
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200686 switch (config->aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100687 case AGGR_CORE:
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200688 fprintf(config->output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100689 cpu_map__id_to_socket(id),
Jiri Olsafa7070a2018-08-30 08:32:29 +0200690 config->csv_output ? 0 : -8,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100691 cpu_map__id_to_cpu(id),
Jiri Olsafa7070a2018-08-30 08:32:29 +0200692 config->csv_sep,
693 config->csv_output ? 0 : 4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100694 nr,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200695 config->csv_sep);
Stephane Eranian12c08a92013-02-14 13:57:29 +0100696 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100697 case AGGR_SOCKET:
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200698 fprintf(config->output, "S%*d%s%*d%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +0200699 config->csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100700 id,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200701 config->csv_sep,
702 config->csv_output ? 0 : 4,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100703 nr,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200704 config->csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100705 break;
706 case AGGR_NONE:
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200707 fprintf(config->output, "CPU%*d%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +0200708 config->csv_output ? 0 : -4,
709 perf_evsel__cpus(evsel)->map[id], config->csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100710 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200711 case AGGR_THREAD:
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200712 fprintf(config->output, "%*s-%*d%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +0200713 config->csv_output ? 0 : 16,
Jiri Olsa32b8af82015-06-26 11:29:27 +0200714 thread_map__comm(evsel->threads, id),
Jiri Olsafa7070a2018-08-30 08:32:29 +0200715 config->csv_output ? 0 : -8,
Jiri Olsa32b8af82015-06-26 11:29:27 +0200716 thread_map__pid(evsel->threads, id),
Jiri Olsafa7070a2018-08-30 08:32:29 +0200717 config->csv_sep);
Jiri Olsa32b8af82015-06-26 11:29:27 +0200718 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100719 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200720 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100721 default:
722 break;
723 }
724}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200725
Andi Kleen140aead2016-01-30 09:06:49 -0800726struct outstate {
727 FILE *fh;
728 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800729 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800730 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800731 int id, nr;
732 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800733};
734
735#define METRIC_LEN 35
736
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200737static void new_line_std(struct perf_stat_config *config __maybe_unused,
738 void *ctx)
Andi Kleen140aead2016-01-30 09:06:49 -0800739{
740 struct outstate *os = ctx;
741
742 os->newline = true;
743}
744
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200745static void do_new_line_std(struct perf_stat_config *config,
746 struct outstate *os)
Andi Kleen140aead2016-01-30 09:06:49 -0800747{
748 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800749 fputs(os->prefix, os->fh);
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200750 aggr_printout(config, os->evsel, os->id, os->nr);
751 if (config->aggr_mode == AGGR_NONE)
Andi Kleen140aead2016-01-30 09:06:49 -0800752 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800753 fprintf(os->fh, " ");
754}
755
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200756static void print_metric_std(struct perf_stat_config *config,
757 void *ctx, const char *color, const char *fmt,
Andi Kleen140aead2016-01-30 09:06:49 -0800758 const char *unit, double val)
759{
760 struct outstate *os = ctx;
761 FILE *out = os->fh;
762 int n;
763 bool newline = os->newline;
764
765 os->newline = false;
766
767 if (unit == NULL || fmt == NULL) {
768 fprintf(out, "%-*s", METRIC_LEN, "");
769 return;
770 }
771
772 if (newline)
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200773 do_new_line_std(config, os);
Andi Kleen140aead2016-01-30 09:06:49 -0800774
775 n = fprintf(out, " # ");
776 if (color)
777 n += color_fprintf(out, color, fmt, val);
778 else
779 n += fprintf(out, fmt, val);
780 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
781}
782
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200783static void new_line_csv(struct perf_stat_config *config, void *ctx)
Andi Kleen92a61f62016-02-29 14:36:21 -0800784{
785 struct outstate *os = ctx;
786 int i;
787
788 fputc('\n', os->fh);
789 if (os->prefix)
Jiri Olsafa7070a2018-08-30 08:32:29 +0200790 fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200791 aggr_printout(config, os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800792 for (i = 0; i < os->nfields; i++)
Jiri Olsafa7070a2018-08-30 08:32:29 +0200793 fputs(config->csv_sep, os->fh);
Andi Kleen92a61f62016-02-29 14:36:21 -0800794}
795
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200796static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
797 void *ctx,
Andi Kleen92a61f62016-02-29 14:36:21 -0800798 const char *color __maybe_unused,
799 const char *fmt, const char *unit, double val)
800{
801 struct outstate *os = ctx;
802 FILE *out = os->fh;
803 char buf[64], *vals, *ends;
804
805 if (unit == NULL || fmt == NULL) {
Jiri Olsafa7070a2018-08-30 08:32:29 +0200806 fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800807 return;
808 }
809 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900810 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800811 while (isdigit(*ends) || *ends == '.')
812 ends++;
813 *ends = 0;
814 while (isspace(*unit))
815 unit++;
Jiri Olsafa7070a2018-08-30 08:32:29 +0200816 fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, unit);
Andi Kleen92a61f62016-02-29 14:36:21 -0800817}
818
Andi Kleen54b50912016-03-03 15:57:36 -0800819/* Filter out some columns that don't work well in metrics only mode */
820
821static bool valid_only_metric(const char *unit)
822{
823 if (!unit)
824 return false;
825 if (strstr(unit, "/sec") ||
826 strstr(unit, "hz") ||
827 strstr(unit, "Hz") ||
828 strstr(unit, "CPUs utilized"))
829 return false;
830 return true;
831}
832
833static const char *fixunit(char *buf, struct perf_evsel *evsel,
834 const char *unit)
835{
836 if (!strncmp(unit, "of all", 6)) {
837 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
838 unit);
839 return buf;
840 }
841 return unit;
842}
843
Jiri Olsaee1760e2018-08-30 08:32:37 +0200844static void print_metric_only(struct perf_stat_config *config,
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200845 void *ctx, const char *color, const char *fmt,
Andi Kleen54b50912016-03-03 15:57:36 -0800846 const char *unit, double val)
847{
848 struct outstate *os = ctx;
849 FILE *out = os->fh;
Jiri Olsaf5155722018-06-07 00:15:08 +0200850 char buf[1024], str[1024];
Jiri Olsaee1760e2018-08-30 08:32:37 +0200851 unsigned mlen = config->metric_only_len;
Andi Kleen54b50912016-03-03 15:57:36 -0800852
853 if (!valid_only_metric(unit))
854 return;
855 unit = fixunit(buf, os->evsel, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800856 if (mlen < strlen(unit))
857 mlen = strlen(unit) + 1;
Jiri Olsaf5155722018-06-07 00:15:08 +0200858
859 if (color)
860 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
861
862 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
863 fprintf(out, "%*s ", mlen, str);
Andi Kleen54b50912016-03-03 15:57:36 -0800864}
865
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200866static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
867 void *ctx, const char *color __maybe_unused,
Andi Kleen54b50912016-03-03 15:57:36 -0800868 const char *fmt,
869 const char *unit, double val)
870{
871 struct outstate *os = ctx;
872 FILE *out = os->fh;
873 char buf[64], *vals, *ends;
874 char tbuf[1024];
875
876 if (!valid_only_metric(unit))
877 return;
878 unit = fixunit(tbuf, os->evsel, unit);
879 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900880 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -0800881 while (isdigit(*ends) || *ends == '.')
882 ends++;
883 *ends = 0;
Jiri Olsafa7070a2018-08-30 08:32:29 +0200884 fprintf(out, "%s%s", vals, config->csv_sep);
Andi Kleen54b50912016-03-03 15:57:36 -0800885}
886
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200887static void new_line_metric(struct perf_stat_config *config __maybe_unused,
888 void *ctx __maybe_unused)
Andi Kleen54b50912016-03-03 15:57:36 -0800889{
890}
891
Jiri Olsaee1760e2018-08-30 08:32:37 +0200892static void print_metric_header(struct perf_stat_config *config,
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200893 void *ctx, const char *color __maybe_unused,
Andi Kleen54b50912016-03-03 15:57:36 -0800894 const char *fmt __maybe_unused,
895 const char *unit, double val __maybe_unused)
896{
897 struct outstate *os = ctx;
898 char tbuf[1024];
899
900 if (!valid_only_metric(unit))
901 return;
902 unit = fixunit(tbuf, os->evsel, unit);
Jiri Olsafa7070a2018-08-30 08:32:29 +0200903 if (config->csv_output)
904 fprintf(os->fh, "%s%s", unit, config->csv_sep);
Andi Kleen54b50912016-03-03 15:57:36 -0800905 else
Jiri Olsaee1760e2018-08-30 08:32:37 +0200906 fprintf(os->fh, "%*s ", config->metric_only_len, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800907}
908
Jiri Olsaae2d7da2018-08-30 08:32:38 +0200909static int first_shadow_cpu(struct perf_stat_config *config,
910 struct perf_evsel *evsel, int id)
Andi Kleen44d49a62016-02-29 14:36:22 -0800911{
Jiri Olsaa138af62018-08-30 08:32:46 +0200912 struct perf_evlist *evlist = evsel->evlist;
Andi Kleen44d49a62016-02-29 14:36:22 -0800913 int i;
914
Jiri Olsa6f6b6592018-08-30 08:32:45 +0200915 if (!config->aggr_get_id)
Andi Kleen44d49a62016-02-29 14:36:22 -0800916 return 0;
917
Jiri Olsaae2d7da2018-08-30 08:32:38 +0200918 if (config->aggr_mode == AGGR_NONE)
Andi Kleen44d49a62016-02-29 14:36:22 -0800919 return id;
920
Jiri Olsaae2d7da2018-08-30 08:32:38 +0200921 if (config->aggr_mode == AGGR_GLOBAL)
Andi Kleen44d49a62016-02-29 14:36:22 -0800922 return 0;
923
924 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
925 int cpu2 = perf_evsel__cpus(evsel)->map[i];
926
Jiri Olsaa138af62018-08-30 08:32:46 +0200927 if (config->aggr_get_id(config, evlist->cpus, cpu2) == id)
Andi Kleen44d49a62016-02-29 14:36:22 -0800928 return cpu2;
929 }
930 return 0;
931}
932
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200933static void abs_printout(struct perf_stat_config *config,
934 int id, int nr, struct perf_evsel *evsel, double avg)
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200935{
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200936 FILE *output = config->output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200937 double sc = evsel->scale;
938 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200939
Jiri Olsafa7070a2018-08-30 08:32:29 +0200940 if (config->csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -0700941 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200942 } else {
Jiri Olsa34ff0862018-08-30 08:32:47 +0200943 if (config->big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -0700944 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200945 else
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 }
948
Jiri Olsa6ca9a082018-08-30 08:32:28 +0200949 aggr_printout(config, evsel, id, nr);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200950
Jiri Olsafa7070a2018-08-30 08:32:29 +0200951 fprintf(output, fmt, avg, config->csv_sep);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200952
953 if (evsel->unit)
954 fprintf(output, "%-*s%s",
Jiri Olsadf4f7b42018-08-30 08:32:32 +0200955 config->csv_output ? 0 : config->unit_width,
Jiri Olsafa7070a2018-08-30 08:32:29 +0200956 evsel->unit, config->csv_sep);
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200957
Jiri Olsafa7070a2018-08-30 08:32:29 +0200958 fprintf(output, "%-*s", config->csv_output ? 0 : 25, perf_evsel__name(evsel));
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200959
960 if (evsel->cgrp)
Jiri Olsafa7070a2018-08-30 08:32:29 +0200961 fprintf(output, "%s%s", config->csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -0800962}
Jiri Olsa556b1fb2015-06-03 16:25:56 +0200963
Kan Liang30060ea2018-04-24 11:20:11 -0700964static bool is_mixed_hw_group(struct perf_evsel *counter)
965{
966 struct perf_evlist *evlist = counter->evlist;
967 u32 pmu_type = counter->attr.type;
968 struct perf_evsel *pos;
969
970 if (counter->nr_members < 2)
971 return false;
972
973 evlist__for_each_entry(evlist, pos) {
974 /* software events can be part of any hardware group */
975 if (pos->attr.type == PERF_TYPE_SOFTWARE)
976 continue;
977 if (pmu_type == PERF_TYPE_SOFTWARE) {
978 pmu_type = pos->attr.type;
979 continue;
980 }
981 if (pmu_type != pos->attr.type)
982 return true;
983 }
984
985 return false;
986}
987
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200988static void printout(struct perf_stat_config *config, int id, int nr,
989 struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +0800990 char *prefix, u64 run, u64 ena, double noise,
991 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -0800992{
Andi Kleen140aead2016-01-30 09:06:49 -0800993 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -0800994 struct outstate os = {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +0200995 .fh = config->output,
Andi Kleen44d49a62016-02-29 14:36:22 -0800996 .prefix = prefix ? prefix : "",
997 .id = id,
998 .nr = nr,
999 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001000 };
Andi Kleen140aead2016-01-30 09:06:49 -08001001 print_metric_t pm = print_metric_std;
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001002 new_line_t nl;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001003
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001004 if (config->metric_only) {
Andi Kleen54b50912016-03-03 15:57:36 -08001005 nl = new_line_metric;
Jiri Olsafa7070a2018-08-30 08:32:29 +02001006 if (config->csv_output)
Andi Kleen54b50912016-03-03 15:57:36 -08001007 pm = print_metric_only_csv;
1008 else
1009 pm = print_metric_only;
1010 } else
1011 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001012
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001013 if (config->csv_output && !config->metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001014 static int aggr_fields[] = {
1015 [AGGR_GLOBAL] = 0,
1016 [AGGR_THREAD] = 1,
1017 [AGGR_NONE] = 1,
1018 [AGGR_SOCKET] = 2,
1019 [AGGR_CORE] = 2,
1020 };
1021
1022 pm = print_metric_csv;
1023 nl = new_line_csv;
1024 os.nfields = 3;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001025 os.nfields += aggr_fields[config->aggr_mode];
Andi Kleen92a61f62016-02-29 14:36:21 -08001026 if (counter->cgrp)
1027 os.nfields++;
1028 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001029 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001030 if (config->metric_only) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001031 pm(config, &os, NULL, "", "", 0);
Andi Kleen54b50912016-03-03 15:57:36 -08001032 return;
1033 }
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001034 aggr_printout(config, counter, id, nr);
Andi Kleencb110f42016-01-30 09:06:51 -08001035
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001036 fprintf(config->output, "%*s%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +02001037 config->csv_output ? 0 : 18,
Andi Kleencb110f42016-01-30 09:06:51 -08001038 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Jiri Olsafa7070a2018-08-30 08:32:29 +02001039 config->csv_sep);
Andi Kleencb110f42016-01-30 09:06:51 -08001040
Kan Liang30060ea2018-04-24 11:20:11 -07001041 if (counter->supported) {
Jiri Olsa31084122018-08-30 08:32:42 +02001042 config->print_free_counters_hint = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001043 if (is_mixed_hw_group(counter))
Jiri Olsa3b3cd9a2018-08-30 08:32:43 +02001044 config->print_mixed_hw_group_error = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001045 }
Borislav Petkov02d492e2017-02-07 01:40:05 +01001046
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001047 fprintf(config->output, "%-*s%s",
Jiri Olsadf4f7b42018-08-30 08:32:32 +02001048 config->csv_output ? 0 : config->unit_width,
Jiri Olsafa7070a2018-08-30 08:32:29 +02001049 counter->unit, config->csv_sep);
Andi Kleencb110f42016-01-30 09:06:51 -08001050
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001051 fprintf(config->output, "%*s",
Jiri Olsafa7070a2018-08-30 08:32:29 +02001052 config->csv_output ? 0 : -25,
Andi Kleencb110f42016-01-30 09:06:51 -08001053 perf_evsel__name(counter));
1054
1055 if (counter->cgrp)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001056 fprintf(config->output, "%s%s",
Jiri Olsafa7070a2018-08-30 08:32:29 +02001057 config->csv_sep, counter->cgrp->name);
Andi Kleencb110f42016-01-30 09:06:51 -08001058
Jiri Olsafa7070a2018-08-30 08:32:29 +02001059 if (!config->csv_output)
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001060 pm(config, &os, NULL, NULL, "", 0);
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001061 print_noise(config, counter, noise);
1062 print_running(config, run, ena);
Jiri Olsafa7070a2018-08-30 08:32:29 +02001063 if (config->csv_output)
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001064 pm(config, &os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001065 return;
1066 }
1067
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001068 if (!config->metric_only)
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001069 abs_printout(config, id, nr, counter, uval);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001070
Andi Kleen140aead2016-01-30 09:06:49 -08001071 out.print_metric = pm;
1072 out.new_line = nl;
1073 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001074 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001075
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001076 if (config->csv_output && !config->metric_only) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001077 print_noise(config, counter, noise);
1078 print_running(config, run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001079 }
1080
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001081 perf_stat__print_shadow_stats(config, counter, uval,
Jiri Olsaae2d7da2018-08-30 08:32:38 +02001082 first_shadow_cpu(config, counter, id),
Jiri Olsad0192fd2018-08-30 08:32:51 +02001083 &out, &config->metric_events, st);
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001084 if (!config->csv_output && !config->metric_only) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001085 print_noise(config, counter, noise);
1086 print_running(config, run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001087 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001088}
1089
Jiri Olsa77e0faf2018-08-30 08:32:39 +02001090static void aggr_update_shadow(struct perf_stat_config *config,
1091 struct perf_evlist *evlist)
Andi Kleen44d49a62016-02-29 14:36:22 -08001092{
1093 int cpu, s2, id, s;
1094 u64 val;
1095 struct perf_evsel *counter;
1096
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001097 for (s = 0; s < config->aggr_map->nr; s++) {
1098 id = config->aggr_map->map[s];
Jiri Olsa77e0faf2018-08-30 08:32:39 +02001099 evlist__for_each_entry(evlist, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001100 val = 0;
1101 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Jiri Olsaa138af62018-08-30 08:32:46 +02001102 s2 = config->aggr_get_id(config, evlist->cpus, cpu);
Andi Kleen44d49a62016-02-29 14:36:22 -08001103 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,
Jiri Olsaae2d7da2018-08-30 08:32:38 +02001108 first_shadow_cpu(config, counter, id),
Jin Yao1fcd0392017-12-05 22:03:04 +08001109 &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
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001142static void collect_all_aliases(struct perf_stat_config *config, struct perf_evsel *counter,
1143 void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001144 bool first),
1145 void *data)
1146{
Jiri Olsa0c538a92018-08-30 08:32:35 +02001147 struct perf_evlist *evlist = counter->evlist;
Andi Kleen430daf22017-03-20 13:17:00 -07001148 struct perf_evsel *alias;
1149
Jiri Olsa0c538a92018-08-30 08:32:35 +02001150 alias = list_prepare_entry(counter, &(evlist->entries), node);
1151 list_for_each_entry_continue (alias, &evlist->entries, node) {
Andi Kleen430daf22017-03-20 13:17:00 -07001152 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1153 alias->scale != counter->scale ||
1154 alias->cgrp != counter->cgrp ||
1155 strcmp(alias->unit, counter->unit) ||
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001156 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter))
Andi Kleen430daf22017-03-20 13:17:00 -07001157 break;
1158 alias->merged_stat = true;
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001159 cb(config, alias, data, false);
Andi Kleen430daf22017-03-20 13:17:00 -07001160 }
1161}
1162
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001163static bool collect_data(struct perf_stat_config *config, struct perf_evsel *counter,
1164 void (*cb)(struct perf_stat_config *config, struct perf_evsel *counter, void *data,
Andi Kleen430daf22017-03-20 13:17:00 -07001165 bool first),
1166 void *data)
1167{
1168 if (counter->merged_stat)
1169 return false;
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001170 cb(config, counter, data, true);
Jiri Olsafdee3352018-08-30 08:32:48 +02001171 if (config->no_merge)
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001172 uniquify_event_name(counter);
1173 else if (counter->auto_merge_stats)
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001174 collect_all_aliases(config, counter, cb, data);
Andi Kleen430daf22017-03-20 13:17:00 -07001175 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001176}
1177
1178struct aggr_data {
1179 u64 ena, run, val;
1180 int id;
1181 int nr;
1182 int cpu;
1183};
1184
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001185static void aggr_cb(struct perf_stat_config *config,
1186 struct perf_evsel *counter, void *data, bool first)
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001187{
1188 struct aggr_data *ad = data;
1189 int cpu, s2;
1190
1191 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1192 struct perf_counts_values *counts;
1193
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001194 s2 = config->aggr_get_id(config, perf_evsel__cpus(counter), cpu);
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001195 if (s2 != ad->id)
1196 continue;
1197 if (first)
1198 ad->nr++;
1199 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001200 /*
1201 * When any result is bad, make them all to give
1202 * consistent output in interval mode.
1203 */
1204 if (counts->ena == 0 || counts->run == 0 ||
1205 counter->counts->scaled == -1) {
1206 ad->ena = 0;
1207 ad->run = 0;
1208 break;
1209 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001210 ad->val += counts->val;
1211 ad->ena += counts->ena;
1212 ad->run += counts->run;
1213 }
1214}
1215
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001216static void print_aggr(struct perf_stat_config *config,
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001217 struct perf_evlist *evlist,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001218 char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001219{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001220 bool metric_only = config->metric_only;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001221 FILE *output = config->output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001222 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001223 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001224 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001225 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001226 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001227
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001228 if (!(config->aggr_map || config->aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001229 return;
1230
Jiri Olsa77e0faf2018-08-30 08:32:39 +02001231 aggr_update_shadow(config, evlist);
Andi Kleen44d49a62016-02-29 14:36:22 -08001232
Andi Kleen54b50912016-03-03 15:57:36 -08001233 /*
1234 * With metric_only everything is on a single line.
1235 * Without each counter has its own line.
1236 */
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001237 for (s = 0; s < config->aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001238 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001239 if (prefix && metric_only)
1240 fprintf(output, "%s", prefix);
1241
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001242 ad.id = id = config->aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001243 first = true;
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001244 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001245 if (is_duration_time(counter))
1246 continue;
1247
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001248 ad.val = ad.ena = ad.run = 0;
1249 ad.nr = 0;
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001250 if (!collect_data(config, counter, aggr_cb, &ad))
Andi Kleen430daf22017-03-20 13:17:00 -07001251 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001252 nr = ad.nr;
1253 ena = ad.ena;
1254 run = ad.run;
1255 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001256 if (first && metric_only) {
1257 first = false;
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001258 aggr_printout(config, counter, id, nr);
Andi Kleen54b50912016-03-03 15:57:36 -08001259 }
1260 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001261 fprintf(output, "%s", prefix);
1262
Stephane Eranian410136f2013-11-12 17:58:49 +01001263 uval = val * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001264 printout(config, id, nr, counter, uval, prefix,
1265 run, ena, 1.0, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001266 if (!metric_only)
1267 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001268 }
Andi Kleen54b50912016-03-03 15:57:36 -08001269 if (metric_only)
1270 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001271 }
1272}
1273
Jin Yao29734552017-12-05 22:03:11 +08001274static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001275{
Jin Yao29734552017-12-05 22:03:11 +08001276 return ((struct perf_aggr_thread_value *)b)->val -
1277 ((struct perf_aggr_thread_value *)a)->val;
1278}
1279
1280static struct perf_aggr_thread_value *sort_aggr_thread(
1281 struct perf_evsel *counter,
1282 int nthreads, int ncpus,
Jiri Olsabe54d592018-08-30 08:32:49 +02001283 int *ret,
1284 struct target *_target)
Jin Yao29734552017-12-05 22:03:11 +08001285{
1286 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001287 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001288 struct perf_aggr_thread_value *buf;
1289
1290 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1291 if (!buf)
1292 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001293
1294 for (thread = 0; thread < nthreads; thread++) {
1295 u64 ena = 0, run = 0, val = 0;
1296
1297 for (cpu = 0; cpu < ncpus; cpu++) {
1298 val += perf_counts(counter->counts, cpu, thread)->val;
1299 ena += perf_counts(counter->counts, cpu, thread)->ena;
1300 run += perf_counts(counter->counts, cpu, thread)->run;
1301 }
1302
Jin Yao29734552017-12-05 22:03:11 +08001303 uval = val * counter->scale;
1304
1305 /*
1306 * Skip value 0 when enabling --per-thread globally,
1307 * otherwise too many 0 output.
1308 */
Jiri Olsabe54d592018-08-30 08:32:49 +02001309 if (uval == 0.0 && target__has_per_thread(_target))
Jin Yao29734552017-12-05 22:03:11 +08001310 continue;
1311
1312 buf[i].counter = counter;
1313 buf[i].id = thread;
1314 buf[i].uval = uval;
1315 buf[i].val = val;
1316 buf[i].run = run;
1317 buf[i].ena = ena;
1318 i++;
1319 }
1320
1321 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1322
1323 if (ret)
1324 *ret = i;
1325
1326 return buf;
1327}
1328
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001329static void print_aggr_thread(struct perf_stat_config *config,
Jiri Olsabe54d592018-08-30 08:32:49 +02001330 struct target *_target,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001331 struct perf_evsel *counter, char *prefix)
Jin Yao29734552017-12-05 22:03:11 +08001332{
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001333 FILE *output = config->output;
Jin Yao29734552017-12-05 22:03:11 +08001334 int nthreads = thread_map__nr(counter->threads);
1335 int ncpus = cpu_map__nr(counter->cpus);
1336 int thread, sorted_threads, id;
1337 struct perf_aggr_thread_value *buf;
1338
Jiri Olsabe54d592018-08-30 08:32:49 +02001339 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
Jin Yao29734552017-12-05 22:03:11 +08001340 if (!buf) {
1341 perror("cannot sort aggr thread");
1342 return;
1343 }
1344
1345 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001346 if (prefix)
1347 fprintf(output, "%s", prefix);
1348
Jin Yao29734552017-12-05 22:03:11 +08001349 id = buf[thread].id;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001350 if (config->stats)
1351 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
Jin Yao29734552017-12-05 22:03:11 +08001352 prefix, buf[thread].run, buf[thread].ena, 1.0,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001353 &config->stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001354 else
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001355 printout(config, id, 0, buf[thread].counter, buf[thread].uval,
Jin Yao29734552017-12-05 22:03:11 +08001356 prefix, buf[thread].run, buf[thread].ena, 1.0,
1357 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001358 fputc('\n', output);
1359 }
Jin Yao29734552017-12-05 22:03:11 +08001360
1361 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001362}
1363
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001364struct caggr_data {
1365 double avg, avg_enabled, avg_running;
1366};
1367
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001368static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
1369 struct perf_evsel *counter, void *data,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001370 bool first __maybe_unused)
1371{
1372 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001373 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001374
1375 cd->avg += avg_stats(&ps->res_stats[0]);
1376 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1377 cd->avg_running += avg_stats(&ps->res_stats[2]);
1378}
1379
Ingo Molnar42202dd2009-06-13 14:57:28 +02001380/*
1381 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001382 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001383 */
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001384static void print_counter_aggr(struct perf_stat_config *config,
1385 struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001386{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001387 bool metric_only = config->metric_only;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001388 FILE *output = config->output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001389 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001390 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001391
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001392 if (!collect_data(config, counter, counter_aggr_cb, &cd))
Andi Kleen430daf22017-03-20 13:17:00 -07001393 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001394
Andi Kleen54b50912016-03-03 15:57:36 -08001395 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001396 fprintf(output, "%s", prefix);
1397
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001398 uval = cd.avg * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001399 printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
Jin Yaoe0128b32017-12-05 22:03:05 +08001400 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001401 if (!metric_only)
1402 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001403}
1404
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001405static void counter_cb(struct perf_stat_config *config __maybe_unused,
1406 struct perf_evsel *counter, void *data,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001407 bool first __maybe_unused)
1408{
1409 struct aggr_data *ad = data;
1410
1411 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1412 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1413 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1414}
1415
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001416/*
1417 * Print out the results of a single counter:
1418 * does not use aggregated count in system-wide
1419 */
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001420static void print_counter(struct perf_stat_config *config,
1421 struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001422{
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001423 FILE *output = config->output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001424 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001425 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001426 int cpu;
1427
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001428 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001429 struct aggr_data ad = { .cpu = cpu };
1430
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001431 if (!collect_data(config, counter, counter_cb, &ad))
Andi Kleen430daf22017-03-20 13:17:00 -07001432 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001433 val = ad.val;
1434 ena = ad.ena;
1435 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001436
1437 if (prefix)
1438 fprintf(output, "%s", prefix);
1439
Stephane Eranian410136f2013-11-12 17:58:49 +01001440 uval = val * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001441 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
Jin Yaoe0128b32017-12-05 22:03:05 +08001442 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001443
Stephane Eranian4aa90152011-08-15 22:22:33 +02001444 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001445 }
1446}
1447
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001448static void print_no_aggr_metric(struct perf_stat_config *config,
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001449 struct perf_evlist *evlist,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001450 char *prefix)
Andi Kleen206cab62016-03-03 15:57:37 -08001451{
1452 int cpu;
1453 int nrcpus = 0;
1454 struct perf_evsel *counter;
1455 u64 ena, run, val;
1456 double uval;
1457
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001458 nrcpus = evlist->cpus->nr;
Andi Kleen206cab62016-03-03 15:57:37 -08001459 for (cpu = 0; cpu < nrcpus; cpu++) {
1460 bool first = true;
1461
1462 if (prefix)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001463 fputs(prefix, config->output);
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001464 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001465 if (is_duration_time(counter))
1466 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001467 if (first) {
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001468 aggr_printout(config, counter, cpu, 0);
Andi Kleen206cab62016-03-03 15:57:37 -08001469 first = false;
1470 }
1471 val = perf_counts(counter->counts, cpu, 0)->val;
1472 ena = perf_counts(counter->counts, cpu, 0)->ena;
1473 run = perf_counts(counter->counts, cpu, 0)->run;
1474
1475 uval = val * counter->scale;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001476 printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
Jin Yaoe0128b32017-12-05 22:03:05 +08001477 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001478 }
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001479 fputc('\n', config->output);
Andi Kleen206cab62016-03-03 15:57:37 -08001480 }
1481}
1482
Andi Kleen54b50912016-03-03 15:57:36 -08001483static int aggr_header_lens[] = {
1484 [AGGR_CORE] = 18,
1485 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001486 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001487 [AGGR_THREAD] = 24,
1488 [AGGR_GLOBAL] = 0,
1489};
1490
Andi Kleenc51fd632016-05-24 12:52:39 -07001491static const char *aggr_header_csv[] = {
1492 [AGGR_CORE] = "core,cpus,",
1493 [AGGR_SOCKET] = "socket,cpus",
1494 [AGGR_NONE] = "cpu,",
1495 [AGGR_THREAD] = "comm-pid,",
1496 [AGGR_GLOBAL] = ""
1497};
1498
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001499static void print_metric_headers(struct perf_stat_config *config,
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001500 struct perf_evlist *evlist,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001501 const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001502{
1503 struct perf_stat_output_ctx out;
1504 struct perf_evsel *counter;
1505 struct outstate os = {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001506 .fh = config->output
Andi Kleen54b50912016-03-03 15:57:36 -08001507 };
1508
1509 if (prefix)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001510 fprintf(config->output, "%s", prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001511
Jiri Olsafa7070a2018-08-30 08:32:29 +02001512 if (!config->csv_output && !no_indent)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001513 fprintf(config->output, "%*s",
1514 aggr_header_lens[config->aggr_mode], "");
Jiri Olsafa7070a2018-08-30 08:32:29 +02001515 if (config->csv_output) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001516 if (config->interval)
1517 fputs("time,", config->output);
1518 fputs(aggr_header_csv[config->aggr_mode], config->output);
Andi Kleenc51fd632016-05-24 12:52:39 -07001519 }
Andi Kleen54b50912016-03-03 15:57:36 -08001520
1521 /* Print metrics headers only */
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001522 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001523 if (is_duration_time(counter))
1524 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001525 os.evsel = counter;
1526 out.ctx = &os;
1527 out.print_metric = print_metric_header;
1528 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001529 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001530 os.evsel = counter;
Jiri Olsa6ca9a082018-08-30 08:32:28 +02001531 perf_stat__print_shadow_stats(config, counter, 0,
Andi Kleen54b50912016-03-03 15:57:36 -08001532 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001533 &out,
Jiri Olsad0192fd2018-08-30 08:32:51 +02001534 &config->metric_events,
Jin Yaoe0128b32017-12-05 22:03:05 +08001535 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001536 }
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001537 fputc('\n', config->output);
Andi Kleen54b50912016-03-03 15:57:36 -08001538}
1539
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001540static void print_interval(struct perf_stat_config *config,
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001541 struct perf_evlist *evlist,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001542 char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001543{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001544 bool metric_only = config->metric_only;
Jiri Olsadf4f7b42018-08-30 08:32:32 +02001545 unsigned int unit_width = config->unit_width;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001546 FILE *output = config->output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001547 static int num_print_interval;
1548
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001549 if (config->interval_clear)
Jiri Olsa9660e082018-06-07 00:15:06 +02001550 puts(CONSOLE_CLEAR);
1551
Jiri Olsafa7070a2018-08-30 08:32:29 +02001552 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001553
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001554 if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001555 switch (config->aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001556 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001557 fprintf(output, "# time socket cpus");
1558 if (!metric_only)
1559 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001560 break;
1561 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001562 fprintf(output, "# time core cpus");
1563 if (!metric_only)
1564 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001565 break;
1566 case AGGR_NONE:
Jiri Olsaf5155722018-06-07 00:15:08 +02001567 fprintf(output, "# time CPU ");
Andi Kleen41c8ca22016-05-24 12:52:38 -07001568 if (!metric_only)
1569 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001570 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001571 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001572 fprintf(output, "# time comm-pid");
1573 if (!metric_only)
1574 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001575 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001576 case AGGR_GLOBAL:
1577 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001578 fprintf(output, "# time");
1579 if (!metric_only)
1580 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001581 case AGGR_UNSET:
1582 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001583 }
1584 }
1585
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001586 if ((num_print_interval == 0 || config->interval_clear) && metric_only)
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001587 print_metric_headers(config, evlist, " ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001588 if (++num_print_interval == 25)
1589 num_print_interval = 0;
1590}
1591
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001592static void print_header(struct perf_stat_config *config,
Jiri Olsac512e0e2018-08-30 08:32:33 +02001593 struct target *_target,
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001594 int argc, const char **argv)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001595{
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001596 FILE *output = config->output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001597 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001598
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001599 fflush(stdout);
1600
Jiri Olsafa7070a2018-08-30 08:32:29 +02001601 if (!config->csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001602 fprintf(output, "\n");
1603 fprintf(output, " Performance counter stats for ");
Jiri Olsac512e0e2018-08-30 08:32:33 +02001604 if (_target->system_wide)
David Ahern62d3b612013-09-28 14:27:58 -06001605 fprintf(output, "\'system wide");
Jiri Olsac512e0e2018-08-30 08:32:33 +02001606 else if (_target->cpu_list)
1607 fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1608 else if (!target__has_task(_target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001609 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1610 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001611 fprintf(output, " %s", argv[i]);
Jiri Olsac512e0e2018-08-30 08:32:33 +02001612 } else if (_target->pid)
1613 fprintf(output, "process id \'%s", _target->pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001614 else
Jiri Olsac512e0e2018-08-30 08:32:33 +02001615 fprintf(output, "thread id \'%s", _target->tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001616
Stephane Eranian4aa90152011-08-15 22:22:33 +02001617 fprintf(output, "\'");
Jiri Olsad97ae042018-08-30 08:32:36 +02001618 if (config->run_count > 1)
1619 fprintf(output, " (%d runs)", config->run_count);
Stephane Eranian4aa90152011-08-15 22:22:33 +02001620 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001621 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001622}
1623
Jiri Olsabc22de92018-04-23 11:08:20 +02001624static int get_precision(double num)
1625{
1626 if (num > 1)
1627 return 0;
1628
1629 return lround(ceil(-log10(num)));
1630}
1631
Jiri Olsad97ae042018-08-30 08:32:36 +02001632static void print_table(struct perf_stat_config *config,
1633 FILE *output, int precision, double avg)
Jiri Olsae55c14a2018-04-23 11:08:21 +02001634{
1635 char tmp[64];
1636 int idx, indent = 0;
1637
1638 scnprintf(tmp, 64, " %17.*f", precision, avg);
1639 while (tmp[indent] == ' ')
1640 indent++;
1641
1642 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1643
Jiri Olsad97ae042018-08-30 08:32:36 +02001644 for (idx = 0; idx < config->run_count; idx++) {
Jiri Olsa54ac0b12018-08-30 08:32:50 +02001645 double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001646 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001647
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001648 fprintf(output, " %17.*f (%+.*f) ",
Jiri Olsae55c14a2018-04-23 11:08:21 +02001649 precision, run, precision, run - avg);
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001650
1651 for (h = 0; h < n; h++)
1652 fprintf(output, "#");
1653
1654 fprintf(output, "\n");
Jiri Olsae55c14a2018-04-23 11:08:21 +02001655 }
1656
1657 fprintf(output, "\n%*s# Final result:\n", indent, "");
1658}
1659
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001660static double timeval2double(struct timeval *t)
1661{
1662 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1663}
1664
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001665static void print_footer(struct perf_stat_config *config)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001666{
Jiri Olsa26893a62018-08-30 08:32:40 +02001667 double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001668 FILE *output = config->output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001669 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001670
Jiri Olsaaea0dca2018-08-30 08:32:41 +02001671 if (!config->null_run)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001672 fprintf(output, "\n");
Jiri Olsabc22de92018-04-23 11:08:20 +02001673
Jiri Olsad97ae042018-08-30 08:32:36 +02001674 if (config->run_count == 1) {
Jiri Olsabc22de92018-04-23 11:08:20 +02001675 fprintf(output, " %17.9f seconds time elapsed", avg);
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001676
Jiri Olsa8897a892018-08-30 08:32:44 +02001677 if (config->ru_display) {
1678 double ru_utime = timeval2double(&config->ru_data.ru_utime);
1679 double ru_stime = timeval2double(&config->ru_data.ru_stime);
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001680
1681 fprintf(output, "\n\n");
1682 fprintf(output, " %17.9f seconds user\n", ru_utime);
1683 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1684 }
Jiri Olsabc22de92018-04-23 11:08:20 +02001685 } else {
Jiri Olsa26893a62018-08-30 08:32:40 +02001686 double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsabc22de92018-04-23 11:08:20 +02001687 /*
1688 * Display at most 2 more significant
1689 * digits than the stddev inaccuracy.
1690 */
1691 int precision = get_precision(sd) + 2;
1692
Jiri Olsa54ac0b12018-08-30 08:32:50 +02001693 if (config->walltime_run_table)
Jiri Olsad97ae042018-08-30 08:32:36 +02001694 print_table(config, output, precision, avg);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001695
Jiri Olsabc22de92018-04-23 11:08:20 +02001696 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1697 precision, avg, precision, sd);
1698
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001699 print_noise_pct(config, sd, avg);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001700 }
1701 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001702
Jiri Olsa31084122018-08-30 08:32:42 +02001703 if (config->print_free_counters_hint &&
Andi Kleen918c7b062017-05-22 18:00:16 -07001704 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1705 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001706 fprintf(output,
1707"Some events weren't counted. Try disabling the NMI watchdog:\n"
1708" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1709" perf stat ...\n"
1710" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001711
Jiri Olsa3b3cd9a2018-08-30 08:32:43 +02001712 if (config->print_mixed_hw_group_error)
Kan Liang30060ea2018-04-24 11:20:11 -07001713 fprintf(output,
1714 "The events in group usually have to be from "
1715 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001716}
1717
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001718static void
1719perf_evlist__print_counters(struct perf_evlist *evlist,
Jiri Olsab64df7f2018-08-30 08:32:26 +02001720 struct perf_stat_config *config,
Jiri Olsac512e0e2018-08-30 08:32:33 +02001721 struct target *_target,
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001722 struct timespec *ts,
1723 int argc, const char **argv)
Jiri Olsad4f63a42015-06-26 11:29:26 +02001724{
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001725 bool metric_only = config->metric_only;
Jiri Olsab64df7f2018-08-30 08:32:26 +02001726 int interval = config->interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001727 struct perf_evsel *counter;
1728 char buf[64], *prefix = NULL;
1729
1730 if (interval)
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001731 print_interval(config, evlist, prefix = buf, ts);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001732 else
Jiri Olsac512e0e2018-08-30 08:32:33 +02001733 print_header(config, _target, argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001734
Andi Kleen54b50912016-03-03 15:57:36 -08001735 if (metric_only) {
1736 static int num_print_iv;
1737
Andi Kleen41c8ca22016-05-24 12:52:38 -07001738 if (num_print_iv == 0 && !interval)
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001739 print_metric_headers(config, evlist, prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001740 if (num_print_iv++ == 25)
1741 num_print_iv = 0;
Jiri Olsab64df7f2018-08-30 08:32:26 +02001742 if (config->aggr_mode == AGGR_GLOBAL && prefix)
1743 fprintf(config->output, "%s", prefix);
Andi Kleen54b50912016-03-03 15:57:36 -08001744 }
1745
Jiri Olsab64df7f2018-08-30 08:32:26 +02001746 switch (config->aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001747 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001748 case AGGR_SOCKET:
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001749 print_aggr(config, evlist, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001750 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001751 case AGGR_THREAD:
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001752 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001753 if (is_duration_time(counter))
1754 continue;
Jiri Olsabe54d592018-08-30 08:32:49 +02001755 print_aggr_thread(config, _target, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001756 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001757 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001758 case AGGR_GLOBAL:
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001759 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001760 if (is_duration_time(counter))
1761 continue;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001762 print_counter_aggr(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001763 }
Andi Kleen54b50912016-03-03 15:57:36 -08001764 if (metric_only)
Jiri Olsab64df7f2018-08-30 08:32:26 +02001765 fputc('\n', config->output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001766 break;
1767 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001768 if (metric_only)
Jiri Olsabc0bcda2018-08-30 08:32:34 +02001769 print_no_aggr_metric(config, evlist, prefix);
Andi Kleen206cab62016-03-03 15:57:37 -08001770 else {
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001771 evlist__for_each_entry(evlist, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001772 if (is_duration_time(counter))
1773 continue;
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001774 print_counter(config, counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001775 }
Andi Kleen206cab62016-03-03 15:57:37 -08001776 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001777 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001778 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001779 default:
1780 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001781 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001782
Jiri Olsafa7070a2018-08-30 08:32:29 +02001783 if (!interval && !config->csv_output)
Jiri Olsaf3ca50e2018-08-30 08:32:27 +02001784 print_footer(config);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001785
Jiri Olsab64df7f2018-08-30 08:32:26 +02001786 fflush(config->output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001787}
1788
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001789static void print_counters(struct timespec *ts, int argc, const char **argv)
1790{
Jiri Olsa01748202018-08-30 08:32:25 +02001791 /* Do not print anything if we record to the pipe. */
1792 if (STAT_RECORD && perf_stat.data.is_pipe)
1793 return;
1794
Jiri Olsac512e0e2018-08-30 08:32:33 +02001795 perf_evlist__print_counters(evsel_list, &stat_config, &target,
Jiri Olsab64df7f2018-08-30 08:32:26 +02001796 ts, argc, argv);
Jiri Olsaa5a9eac2018-08-30 08:32:24 +02001797}
1798
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001799static volatile int signr = -1;
1800
Ingo Molnar52425192009-05-26 09:17:18 +02001801static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001802{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001803 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001804 done = 1;
1805
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001806 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001807 /*
1808 * render child_pid harmless
1809 * won't send SIGTERM to a random
1810 * process in case of race condition
1811 * and fast PID recycling
1812 */
1813 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001814}
1815
1816static void sig_atexit(void)
1817{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001818 sigset_t set, oset;
1819
1820 /*
1821 * avoid race condition with SIGCHLD handler
1822 * in skip_signal() which is modifying child_pid
1823 * goal is to avoid send SIGTERM to a random
1824 * process
1825 */
1826 sigemptyset(&set);
1827 sigaddset(&set, SIGCHLD);
1828 sigprocmask(SIG_BLOCK, &set, &oset);
1829
Chris Wilson933da832009-10-04 01:35:01 +01001830 if (child_pid != -1)
1831 kill(child_pid, SIGTERM);
1832
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001833 sigprocmask(SIG_SETMASK, &oset, NULL);
1834
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001835 if (signr == -1)
1836 return;
1837
1838 signal(signr, SIG_DFL);
1839 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001840}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001841
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001842static int stat__set_big_num(const struct option *opt __maybe_unused,
1843 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001844{
1845 big_num_opt = unset ? 0 : 1;
1846 return 0;
1847}
1848
Andi Kleen44b1e602016-05-30 12:49:42 -03001849static int enable_metric_only(const struct option *opt __maybe_unused,
1850 const char *s __maybe_unused, int unset)
1851{
1852 force_metric_only = true;
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001853 stat_config.metric_only = !unset;
Andi Kleen44b1e602016-05-30 12:49:42 -03001854 return 0;
1855}
1856
Andi Kleenb18f3e32017-08-31 12:40:31 -07001857static int parse_metric_groups(const struct option *opt,
1858 const char *str,
1859 int unset __maybe_unused)
1860{
Jiri Olsad0192fd2018-08-30 08:32:51 +02001861 return metricgroup__parse_groups(opt, str, &stat_config.metric_events);
Andi Kleenb18f3e32017-08-31 12:40:31 -07001862}
1863
Jiri Olsae0547312015-11-05 15:40:45 +01001864static const struct option stat_options[] = {
1865 OPT_BOOLEAN('T', "transaction", &transaction_run,
1866 "hardware transaction statistics"),
1867 OPT_CALLBACK('e', "event", &evsel_list, "event",
1868 "event selector. use 'perf list' to list available events",
1869 parse_events_option),
1870 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1871 "event filter", parse_filter),
Jiri Olsa5698f262018-08-30 08:32:12 +02001872 OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
Jiri Olsae0547312015-11-05 15:40:45 +01001873 "child tasks do not inherit counters"),
1874 OPT_STRING('p', "pid", &target.pid, "pid",
1875 "stat events on existing process id"),
1876 OPT_STRING('t', "tid", &target.tid, "tid",
1877 "stat events on existing thread id"),
1878 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1879 "system-wide collection from all CPUs"),
1880 OPT_BOOLEAN('g', "group", &group,
1881 "put the counters into a counter group"),
1882 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1883 OPT_INCR('v', "verbose", &verbose,
1884 "be more verbose (show counter open errors, etc)"),
Jiri Olsad97ae042018-08-30 08:32:36 +02001885 OPT_INTEGER('r', "repeat", &stat_config.run_count,
Jiri Olsae0547312015-11-05 15:40:45 +01001886 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsa54ac0b12018-08-30 08:32:50 +02001887 OPT_BOOLEAN(0, "table", &stat_config.walltime_run_table,
Jiri Olsae55c14a2018-04-23 11:08:21 +02001888 "display details about each run (only with -r option)"),
Jiri Olsaaea0dca2018-08-30 08:32:41 +02001889 OPT_BOOLEAN('n', "null", &stat_config.null_run,
Jiri Olsae0547312015-11-05 15:40:45 +01001890 "null run - dont start any counters"),
1891 OPT_INCR('d', "detailed", &detailed_run,
1892 "detailed run - start a lot of events"),
1893 OPT_BOOLEAN('S', "sync", &sync_run,
1894 "call sync() before starting a run"),
1895 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1896 "print large numbers with thousands\' separators",
1897 stat__set_big_num),
1898 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1899 "list of cpus to monitor in system-wide"),
1900 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1901 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsafdee3352018-08-30 08:32:48 +02001902 OPT_BOOLEAN(0, "no-merge", &stat_config.no_merge, "Do not merge identical named events"),
Jiri Olsafa7070a2018-08-30 08:32:29 +02001903 OPT_STRING('x', "field-separator", &stat_config.csv_sep, "separator",
Jiri Olsae0547312015-11-05 15:40:45 +01001904 "print counts with custom separator"),
1905 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1906 "monitor event in cgroup name only", parse_cgroups),
1907 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1908 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1909 OPT_INTEGER(0, "log-fd", &output_fd,
1910 "log output to fd, instead of stderr"),
1911 OPT_STRING(0, "pre", &pre_cmd, "command",
1912 "command to run prior to the measured command"),
1913 OPT_STRING(0, "post", &post_cmd, "command",
1914 "command to run after to the measured command"),
1915 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03001916 "print counts at regular interval in ms "
1917 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01001918 OPT_INTEGER(0, "interval-count", &stat_config.times,
1919 "print counts for fixed number of times"),
Jiri Olsa132c6ba2018-08-30 08:32:30 +02001920 OPT_BOOLEAN(0, "interval-clear", &stat_config.interval_clear,
Jiri Olsa9660e082018-06-07 00:15:06 +02001921 "clear screen in between new interval"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01001922 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1923 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001924 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1925 "aggregate counts per processor socket", AGGR_SOCKET),
1926 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1927 "aggregate counts per physical processor core", AGGR_CORE),
1928 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1929 "aggregate counts per thread", AGGR_THREAD),
Jiri Olsa728c0ee2018-08-30 08:32:11 +02001930 OPT_UINTEGER('D', "delay", &stat_config.initial_delay,
Jiri Olsae0547312015-11-05 15:40:45 +01001931 "ms to wait before starting measurement after program start"),
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02001932 OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL,
Andi Kleen44b1e602016-05-30 12:49:42 -03001933 "Only print computed metrics. No raw values", enable_metric_only),
1934 OPT_BOOLEAN(0, "topdown", &topdown_run,
1935 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001936 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1937 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001938 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1939 "monitor specified metrics or metric groups (separated by ,)",
1940 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01001941 OPT_END()
1942};
1943
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001944static int perf_stat__get_socket(struct perf_stat_config *config __maybe_unused,
1945 struct cpu_map *map, int cpu)
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001946{
1947 return cpu_map__get_socket(map, cpu, NULL);
1948}
1949
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001950static int perf_stat__get_core(struct perf_stat_config *config __maybe_unused,
1951 struct cpu_map *map, int cpu)
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001952{
1953 return cpu_map__get_core(map, cpu, NULL);
1954}
1955
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001956static int cpu_map__get_max(struct cpu_map *map)
1957{
1958 int i, max = -1;
1959
1960 for (i = 0; i < map->nr; i++) {
1961 if (map->map[i] > max)
1962 max = map->map[i];
1963 }
1964
1965 return max;
1966}
1967
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001968static int perf_stat__get_aggr(struct perf_stat_config *config,
1969 aggr_get_id_t get_id, struct cpu_map *map, int idx)
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001970{
1971 int cpu;
1972
1973 if (idx >= map->nr)
1974 return -1;
1975
1976 cpu = map->map[idx];
1977
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001978 if (config->cpus_aggr_map->map[cpu] == -1)
1979 config->cpus_aggr_map->map[cpu] = get_id(config, map, idx);
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001980
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001981 return config->cpus_aggr_map->map[cpu];
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001982}
1983
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001984static int perf_stat__get_socket_cached(struct perf_stat_config *config,
1985 struct cpu_map *map, int idx)
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001986{
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001987 return perf_stat__get_aggr(config, perf_stat__get_socket, map, idx);
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001988}
1989
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001990static int perf_stat__get_core_cached(struct perf_stat_config *config,
1991 struct cpu_map *map, int idx)
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001992{
Jiri Olsa6f6b6592018-08-30 08:32:45 +02001993 return perf_stat__get_aggr(config, perf_stat__get_core, map, idx);
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001994}
1995
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001996static int perf_stat_init_aggr_mode(void)
1997{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001998 int nr;
1999
Jiri Olsa421a50f2015-07-21 14:31:22 +02002000 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002001 case AGGR_SOCKET:
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002002 if (cpu_map__build_socket_map(evsel_list->cpus, &stat_config.aggr_map)) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002003 perror("cannot build socket map");
2004 return -1;
2005 }
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002006 stat_config.aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002007 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002008 case AGGR_CORE:
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002009 if (cpu_map__build_core_map(evsel_list->cpus, &stat_config.aggr_map)) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01002010 perror("cannot build core map");
2011 return -1;
2012 }
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002013 stat_config.aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002014 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002015 case AGGR_NONE:
2016 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002017 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002018 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002019 default:
2020 break;
2021 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002022
2023 /*
2024 * The evsel_list->cpus is the base we operate on,
2025 * taking the highest cpu number to be the size of
2026 * the aggregation translate cpumap.
2027 */
2028 nr = cpu_map__get_max(evsel_list->cpus);
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002029 stat_config.cpus_aggr_map = cpu_map__empty_new(nr + 1);
2030 return stat_config.cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002031}
2032
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002033static void perf_stat__exit_aggr_mode(void)
2034{
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002035 cpu_map__put(stat_config.aggr_map);
2036 cpu_map__put(stat_config.cpus_aggr_map);
2037 stat_config.aggr_map = NULL;
2038 stat_config.cpus_aggr_map = NULL;
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002039}
2040
Jiri Olsa68d702f2015-11-05 15:40:58 +01002041static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2042{
2043 int cpu;
2044
2045 if (idx > map->nr)
2046 return -1;
2047
2048 cpu = map->map[idx];
2049
Jan Stancekda8a58b2017-02-17 12:10:26 +01002050 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002051 return -1;
2052
2053 return cpu;
2054}
2055
2056static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2057{
2058 struct perf_env *env = data;
2059 int cpu = perf_env__get_cpu(env, map, idx);
2060
2061 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2062}
2063
2064static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2065{
2066 struct perf_env *env = data;
2067 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2068
2069 if (cpu != -1) {
2070 int socket_id = env->cpu[cpu].socket_id;
2071
2072 /*
2073 * Encode socket in upper 16 bits
2074 * core_id is relative to socket, and
2075 * we need a global id. So we combine
2076 * socket + core id.
2077 */
2078 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2079 }
2080
2081 return core;
2082}
2083
2084static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2085 struct cpu_map **sockp)
2086{
2087 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2088}
2089
2090static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2091 struct cpu_map **corep)
2092{
2093 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2094}
2095
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002096static int perf_stat__get_socket_file(struct perf_stat_config *config __maybe_unused,
2097 struct cpu_map *map, int idx)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002098{
2099 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2100}
2101
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002102static int perf_stat__get_core_file(struct perf_stat_config *config __maybe_unused,
2103 struct cpu_map *map, int idx)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002104{
2105 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2106}
2107
2108static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2109{
2110 struct perf_env *env = &st->session->header.env;
2111
2112 switch (stat_config.aggr_mode) {
2113 case AGGR_SOCKET:
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002114 if (perf_env__build_socket_map(env, evsel_list->cpus, &stat_config.aggr_map)) {
Jiri Olsa68d702f2015-11-05 15:40:58 +01002115 perror("cannot build socket map");
2116 return -1;
2117 }
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002118 stat_config.aggr_get_id = perf_stat__get_socket_file;
Jiri Olsa68d702f2015-11-05 15:40:58 +01002119 break;
2120 case AGGR_CORE:
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002121 if (perf_env__build_core_map(env, evsel_list->cpus, &stat_config.aggr_map)) {
Jiri Olsa68d702f2015-11-05 15:40:58 +01002122 perror("cannot build core map");
2123 return -1;
2124 }
Jiri Olsa6f6b6592018-08-30 08:32:45 +02002125 stat_config.aggr_get_id = perf_stat__get_core_file;
Jiri Olsa68d702f2015-11-05 15:40:58 +01002126 break;
2127 case AGGR_NONE:
2128 case AGGR_GLOBAL:
2129 case AGGR_THREAD:
2130 case AGGR_UNSET:
2131 default:
2132 break;
2133 }
2134
2135 return 0;
2136}
2137
Andi Kleen44b1e602016-05-30 12:49:42 -03002138static int topdown_filter_events(const char **attr, char **str, bool use_group)
2139{
2140 int off = 0;
2141 int i;
2142 int len = 0;
2143 char *s;
2144
2145 for (i = 0; attr[i]; i++) {
2146 if (pmu_have_event("cpu", attr[i])) {
2147 len += strlen(attr[i]) + 1;
2148 attr[i - off] = attr[i];
2149 } else
2150 off++;
2151 }
2152 attr[i - off] = NULL;
2153
2154 *str = malloc(len + 1 + 2);
2155 if (!*str)
2156 return -1;
2157 s = *str;
2158 if (i - off == 0) {
2159 *s = 0;
2160 return 0;
2161 }
2162 if (use_group)
2163 *s++ = '{';
2164 for (i = 0; attr[i]; i++) {
2165 strcpy(s, attr[i]);
2166 s += strlen(s);
2167 *s++ = ',';
2168 }
2169 if (use_group) {
2170 s[-1] = '}';
2171 *s = 0;
2172 } else
2173 s[-1] = 0;
2174 return 0;
2175}
2176
2177__weak bool arch_topdown_check_group(bool *warn)
2178{
2179 *warn = false;
2180 return false;
2181}
2182
2183__weak void arch_topdown_group_warn(void)
2184{
2185}
2186
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002187/*
2188 * Add default attributes, if there were no attributes specified or
2189 * if -d/--detailed, -d -d or -d -d -d is used:
2190 */
2191static int add_default_attributes(void)
2192{
Andi Kleen44b1e602016-05-30 12:49:42 -03002193 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002194 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002195
2196 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2197 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2198 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2199 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2200
2201 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002202};
2203 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002204 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002205};
2206 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002207 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002208};
2209 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002210 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2211 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2212 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2213
2214};
2215
2216/*
2217 * Detailed stats (-d), covering the L1 and last level data caches:
2218 */
2219 struct perf_event_attr detailed_attrs[] = {
2220
2221 { .type = PERF_TYPE_HW_CACHE,
2222 .config =
2223 PERF_COUNT_HW_CACHE_L1D << 0 |
2224 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2225 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2226
2227 { .type = PERF_TYPE_HW_CACHE,
2228 .config =
2229 PERF_COUNT_HW_CACHE_L1D << 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_LL << 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_LL << 0 |
2242 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2243 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2244};
2245
2246/*
2247 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2248 */
2249 struct perf_event_attr very_detailed_attrs[] = {
2250
2251 { .type = PERF_TYPE_HW_CACHE,
2252 .config =
2253 PERF_COUNT_HW_CACHE_L1I << 0 |
2254 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2255 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2256
2257 { .type = PERF_TYPE_HW_CACHE,
2258 .config =
2259 PERF_COUNT_HW_CACHE_L1I << 0 |
2260 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2261 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2262
2263 { .type = PERF_TYPE_HW_CACHE,
2264 .config =
2265 PERF_COUNT_HW_CACHE_DTLB << 0 |
2266 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2267 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2268
2269 { .type = PERF_TYPE_HW_CACHE,
2270 .config =
2271 PERF_COUNT_HW_CACHE_DTLB << 0 |
2272 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2273 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2274
2275 { .type = PERF_TYPE_HW_CACHE,
2276 .config =
2277 PERF_COUNT_HW_CACHE_ITLB << 0 |
2278 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2279 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2280
2281 { .type = PERF_TYPE_HW_CACHE,
2282 .config =
2283 PERF_COUNT_HW_CACHE_ITLB << 0 |
2284 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2285 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2286
2287};
2288
2289/*
2290 * Very, very detailed stats (-d -d -d), adding prefetch events:
2291 */
2292 struct perf_event_attr very_very_detailed_attrs[] = {
2293
2294 { .type = PERF_TYPE_HW_CACHE,
2295 .config =
2296 PERF_COUNT_HW_CACHE_L1D << 0 |
2297 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2298 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2299
2300 { .type = PERF_TYPE_HW_CACHE,
2301 .config =
2302 PERF_COUNT_HW_CACHE_L1D << 0 |
2303 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2304 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2305};
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002306 struct parse_events_error errinfo;
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002307
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002308 /* Set attrs if no event is selected and !null_run: */
Jiri Olsaaea0dca2018-08-30 08:32:41 +02002309 if (stat_config.null_run)
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002310 return 0;
2311
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002312 if (transaction_run) {
Thomas Richter742d92f2018-06-26 09:17:01 +02002313 /* Handle -T as -M transaction. Once platform specific metrics
2314 * support has been added to the json files, all archictures
2315 * will use this approach. To determine transaction support
2316 * on an architecture test for such a metric name.
2317 */
2318 if (metricgroup__has_metric("transaction")) {
2319 struct option opt = { .value = &evsel_list };
2320
2321 return metricgroup__parse_groups(&opt, "transaction",
Jiri Olsad0192fd2018-08-30 08:32:51 +02002322 &stat_config.metric_events);
Thomas Richter742d92f2018-06-26 09:17:01 +02002323 }
2324
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002325 if (pmu_have_event("cpu", "cycles-ct") &&
2326 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002327 err = parse_events(evsel_list, transaction_attrs,
2328 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002329 else
Thomas Richterfca323402018-03-08 15:57:35 +01002330 err = parse_events(evsel_list,
2331 transaction_limited_attrs,
2332 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002333 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002334 fprintf(stderr, "Cannot set up transaction events\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002335 parse_events_print_error(&errinfo, transaction_attrs);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002336 return -1;
2337 }
2338 return 0;
2339 }
2340
Kan Liangdaefd0b2017-05-26 12:05:38 -07002341 if (smi_cost) {
2342 int smi;
2343
2344 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2345 fprintf(stderr, "freeze_on_smi is not supported.\n");
2346 return -1;
2347 }
2348
2349 if (!smi) {
2350 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2351 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2352 return -1;
2353 }
2354 smi_reset = true;
2355 }
2356
2357 if (pmu_have_event("msr", "aperf") &&
2358 pmu_have_event("msr", "smi")) {
2359 if (!force_metric_only)
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002360 stat_config.metric_only = true;
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002361 err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002362 } else {
2363 fprintf(stderr, "To measure SMI cost, it needs "
2364 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002365 parse_events_print_error(&errinfo, smi_cost_attrs);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002366 return -1;
2367 }
2368 if (err) {
2369 fprintf(stderr, "Cannot set up SMI cost events\n");
2370 return -1;
2371 }
2372 return 0;
2373 }
2374
Andi Kleen44b1e602016-05-30 12:49:42 -03002375 if (topdown_run) {
2376 char *str = NULL;
2377 bool warn = false;
2378
2379 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2380 stat_config.aggr_mode != AGGR_CORE) {
2381 pr_err("top down event configuration requires --per-core mode\n");
2382 return -1;
2383 }
2384 stat_config.aggr_mode = AGGR_CORE;
2385 if (nr_cgroups || !target__has_cpu(&target)) {
2386 pr_err("top down event configuration requires system-wide mode (-a)\n");
2387 return -1;
2388 }
2389
2390 if (!force_metric_only)
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002391 stat_config.metric_only = true;
Andi Kleen44b1e602016-05-30 12:49:42 -03002392 if (topdown_filter_events(topdown_attrs, &str,
2393 arch_topdown_check_group(&warn)) < 0) {
2394 pr_err("Out of memory\n");
2395 return -1;
2396 }
2397 if (topdown_attrs[0] && str) {
2398 if (warn)
2399 arch_topdown_group_warn();
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002400 err = parse_events(evsel_list, str, &errinfo);
Andi Kleen44b1e602016-05-30 12:49:42 -03002401 if (err) {
2402 fprintf(stderr,
2403 "Cannot set up top down events %s: %d\n",
2404 str, err);
2405 free(str);
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002406 parse_events_print_error(&errinfo, str);
Andi Kleen44b1e602016-05-30 12:49:42 -03002407 return -1;
2408 }
2409 } else {
2410 fprintf(stderr, "System does not support topdown\n");
2411 return -1;
2412 }
2413 free(str);
2414 }
2415
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002416 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002417 if (target__has_cpu(&target))
2418 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2419
Andi Kleen9dec4472016-02-26 16:27:56 -08002420 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2421 return -1;
2422 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2423 if (perf_evlist__add_default_attrs(evsel_list,
2424 frontend_attrs) < 0)
2425 return -1;
2426 }
2427 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2428 if (perf_evlist__add_default_attrs(evsel_list,
2429 backend_attrs) < 0)
2430 return -1;
2431 }
2432 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002433 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002434 }
2435
2436 /* Detailed events get appended to the event list: */
2437
2438 if (detailed_run < 1)
2439 return 0;
2440
2441 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002442 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002443 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002444
2445 if (detailed_run < 2)
2446 return 0;
2447
2448 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002449 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002450 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002451
2452 if (detailed_run < 3)
2453 return 0;
2454
2455 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002456 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002457}
2458
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002459static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002460 "perf stat record [<options>]",
2461 NULL,
2462};
2463
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002464static void init_features(struct perf_session *session)
2465{
2466 int feat;
2467
2468 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2469 perf_header__set_feat(&session->header, feat);
2470
2471 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2472 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2473 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2474 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2475}
2476
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002477static int __cmd_record(int argc, const char **argv)
2478{
2479 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002480 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002481
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002482 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002483 PARSE_OPT_STOP_AT_NON_OPTION);
2484
2485 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002486 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002487
Jiri Olsad97ae042018-08-30 08:32:36 +02002488 if (stat_config.run_count != 1 || forever) {
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002489 pr_err("Cannot use -r option with perf stat record.\n");
2490 return -1;
2491 }
2492
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002493 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002494 if (session == NULL) {
2495 pr_err("Perf session creation failed.\n");
2496 return -1;
2497 }
2498
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002499 init_features(session);
2500
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002501 session->evlist = evsel_list;
2502 perf_stat.session = session;
2503 perf_stat.record = true;
2504 return argc;
2505}
2506
Jiri Olsaa56f9392015-11-05 15:40:59 +01002507static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2508 union perf_event *event,
2509 struct perf_session *session)
2510{
Andi Kleene3b03b62016-05-05 16:04:03 -07002511 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002512 struct perf_evsel *counter;
2513 struct timespec tsh, *ts = NULL;
2514 const char **argv = session->header.env.cmdline_argv;
2515 int argc = session->header.env.nr_cmdline;
2516
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002517 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002518 perf_stat_process_counter(&stat_config, counter);
2519
Andi Kleene3b03b62016-05-05 16:04:03 -07002520 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2521 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002522
Andi Kleene3b03b62016-05-05 16:04:03 -07002523 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002524 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2525 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002526 ts = &tsh;
2527 }
2528
2529 print_counters(ts, argc, argv);
2530 return 0;
2531}
2532
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002533static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002534int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002535 union perf_event *event,
2536 struct perf_session *session __maybe_unused)
2537{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002538 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2539
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002540 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002541
Jiri Olsa89af4e02015-11-05 15:41:02 +01002542 if (cpu_map__empty(st->cpus)) {
2543 if (st->aggr_mode != AGGR_UNSET)
2544 pr_warning("warning: processing task data, aggregation mode not set\n");
2545 return 0;
2546 }
2547
2548 if (st->aggr_mode != AGGR_UNSET)
2549 stat_config.aggr_mode = st->aggr_mode;
2550
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002551 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002552 perf_stat_init_aggr_mode();
2553 else
2554 perf_stat_init_aggr_mode_file(st);
2555
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002556 return 0;
2557}
2558
Jiri Olsa1975d362015-11-05 15:40:56 +01002559static int set_maps(struct perf_stat *st)
2560{
2561 if (!st->cpus || !st->threads)
2562 return 0;
2563
2564 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2565 return -EINVAL;
2566
2567 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2568
2569 if (perf_evlist__alloc_stats(evsel_list, true))
2570 return -ENOMEM;
2571
2572 st->maps_allocated = true;
2573 return 0;
2574}
2575
2576static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002577int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002578 union perf_event *event,
2579 struct perf_session *session __maybe_unused)
2580{
2581 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2582
2583 if (st->threads) {
2584 pr_warning("Extra thread map event, ignoring.\n");
2585 return 0;
2586 }
2587
2588 st->threads = thread_map__new_event(&event->thread_map);
2589 if (!st->threads)
2590 return -ENOMEM;
2591
2592 return set_maps(st);
2593}
2594
2595static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002596int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002597 union perf_event *event,
2598 struct perf_session *session __maybe_unused)
2599{
2600 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2601 struct cpu_map *cpus;
2602
2603 if (st->cpus) {
2604 pr_warning("Extra cpu map event, ignoring.\n");
2605 return 0;
2606 }
2607
2608 cpus = cpu_map__new_data(&event->cpu_map.data);
2609 if (!cpus)
2610 return -ENOMEM;
2611
2612 st->cpus = cpus;
2613 return set_maps(st);
2614}
2615
Jin Yao56739442017-12-05 22:03:07 +08002616static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2617{
2618 int i;
2619
2620 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2621 if (!config->stats)
2622 return -1;
2623
2624 config->stats_num = nthreads;
2625
2626 for (i = 0; i < nthreads; i++)
2627 runtime_stat__init(&config->stats[i]);
2628
2629 return 0;
2630}
2631
2632static void runtime_stat_delete(struct perf_stat_config *config)
2633{
2634 int i;
2635
2636 if (!config->stats)
2637 return;
2638
2639 for (i = 0; i < config->stats_num; i++)
2640 runtime_stat__exit(&config->stats[i]);
2641
2642 free(config->stats);
2643}
2644
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002645static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002646 "perf stat report [<options>]",
2647 NULL,
2648};
2649
2650static struct perf_stat perf_stat = {
2651 .tool = {
2652 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002653 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002654 .thread_map = process_thread_map_event,
2655 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002656 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002657 .stat = perf_event__process_stat_event,
2658 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002659 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002660 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002661};
2662
2663static int __cmd_report(int argc, const char **argv)
2664{
2665 struct perf_session *session;
2666 const struct option options[] = {
2667 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002668 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2669 "aggregate counts per processor socket", AGGR_SOCKET),
2670 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2671 "aggregate counts per physical processor core", AGGR_CORE),
2672 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2673 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002674 OPT_END()
2675 };
2676 struct stat st;
2677 int ret;
2678
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002679 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002680
2681 if (!input_name || !strlen(input_name)) {
2682 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2683 input_name = "-";
2684 else
2685 input_name = "perf.data";
2686 }
2687
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002688 perf_stat.data.file.path = input_name;
2689 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002690
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002691 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002692 if (session == NULL)
2693 return -1;
2694
2695 perf_stat.session = session;
2696 stat_config.output = stderr;
2697 evsel_list = session->evlist;
2698
2699 ret = perf_session__process_events(session);
2700 if (ret)
2701 return ret;
2702
2703 perf_session__delete(session);
2704 return 0;
2705}
2706
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002707static void setup_system_wide(int forks)
2708{
2709 /*
2710 * Make system wide (-a) the default target if
2711 * no target was specified and one of following
2712 * conditions is met:
2713 *
2714 * - there's no workload specified
2715 * - there is workload specified but all requested
2716 * events are system wide events
2717 */
2718 if (!target__none(&target))
2719 return;
2720
2721 if (!forks)
2722 target.system_wide = true;
2723 else {
2724 struct perf_evsel *counter;
2725
2726 evlist__for_each_entry(evsel_list, counter) {
2727 if (!counter->system_wide)
2728 return;
2729 }
2730
2731 if (evsel_list->nr_entries)
2732 target.system_wide = true;
2733 }
2734}
2735
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002736int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002737{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002738 const char * const stat_usage[] = {
2739 "perf stat [<options>] [<command>]",
2740 NULL
2741 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002742 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002743 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002744 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002745 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002746 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002747
Stephane Eranian5af52b52010-05-18 15:00:01 +02002748 setlocale(LC_ALL, "");
2749
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002750 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002751 if (evsel_list == NULL)
2752 return -ENOMEM;
2753
Wang Nan1669e502016-02-19 11:43:58 +00002754 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002755 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2756 (const char **) stat_usage,
2757 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002758 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002759 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002760
Jiri Olsafa7070a2018-08-30 08:32:29 +02002761 if (stat_config.csv_sep) {
2762 stat_config.csv_output = true;
2763 if (!strcmp(stat_config.csv_sep, "\\t"))
2764 stat_config.csv_sep = "\t";
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002765 } else
Jiri Olsafa7070a2018-08-30 08:32:29 +02002766 stat_config.csv_sep = DEFAULT_SEPARATOR;
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002767
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002768 if (argc && !strncmp(argv[0], "rec", 3)) {
2769 argc = __cmd_record(argc, argv);
2770 if (argc < 0)
2771 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002772 } else if (argc && !strncmp(argv[0], "rep", 3))
2773 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002774
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002775 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002776 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002777
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002778 /*
2779 * For record command the -o is already taken care of.
2780 */
2781 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002782 output = NULL;
2783
Jim Cromie56f3bae2011-09-07 17:14:00 -06002784 if (output_name && output_fd) {
2785 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002786 parse_options_usage(stat_usage, stat_options, "o", 1);
2787 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002788 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002789 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002790
Jiri Olsa0ce5aa02018-08-30 08:32:31 +02002791 if (stat_config.metric_only && stat_config.aggr_mode == AGGR_THREAD) {
Andi Kleen54b50912016-03-03 15:57:36 -08002792 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2793 goto out;
2794 }
2795
Jiri Olsad97ae042018-08-30 08:32:36 +02002796 if (stat_config.metric_only && stat_config.run_count > 1) {
Andi Kleen54b50912016-03-03 15:57:36 -08002797 fprintf(stderr, "--metric-only is not supported with -r\n");
2798 goto out;
2799 }
2800
Jiri Olsa54ac0b12018-08-30 08:32:50 +02002801 if (stat_config.walltime_run_table && stat_config.run_count <= 1) {
Jiri Olsae55c14a2018-04-23 11:08:21 +02002802 fprintf(stderr, "--table is only supported with -r\n");
2803 parse_options_usage(stat_usage, stat_options, "r", 1);
2804 parse_options_usage(NULL, stat_options, "table", 0);
2805 goto out;
2806 }
2807
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002808 if (output_fd < 0) {
2809 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002810 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002811 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002812 }
2813
Stephane Eranian4aa90152011-08-15 22:22:33 +02002814 if (!output) {
2815 struct timespec tm;
2816 mode = append_file ? "a" : "w";
2817
2818 output = fopen(output_name, mode);
2819 if (!output) {
2820 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002821 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002822 }
2823 clock_gettime(CLOCK_REALTIME, &tm);
2824 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002825 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002826 mode = append_file ? "a" : "w";
2827 output = fdopen(output_fd, mode);
2828 if (!output) {
2829 perror("Failed opening logfd");
2830 return -errno;
2831 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002832 }
2833
Jiri Olsa58215222015-07-21 14:31:24 +02002834 stat_config.output = output;
2835
Stephane Eraniand7470b62010-12-01 18:49:05 +02002836 /*
2837 * let the spreadsheet do the pretty-printing
2838 */
Jiri Olsafa7070a2018-08-30 08:32:29 +02002839 if (stat_config.csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002840 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002841 if (big_num_opt == 1) {
2842 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002843 parse_options_usage(stat_usage, stat_options, "B", 1);
2844 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002845 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002846 } else /* Nope, so disable big number formatting */
Jiri Olsa34ff0862018-08-30 08:32:47 +02002847 stat_config.big_num = false;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002848 } else if (big_num_opt == 0) /* User passed --no-big-num */
Jiri Olsa34ff0862018-08-30 08:32:47 +02002849 stat_config.big_num = false;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002850
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002851 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002852
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002853 /*
2854 * Display user/system times only for single
2855 * run and when there's specified tracee.
2856 */
Jiri Olsad97ae042018-08-30 08:32:36 +02002857 if ((stat_config.run_count == 1) && target__none(&target))
Jiri Olsa8897a892018-08-30 08:32:44 +02002858 stat_config.ru_display = true;
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002859
Jiri Olsad97ae042018-08-30 08:32:36 +02002860 if (stat_config.run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002861 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002862 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002863 goto out;
Jiri Olsad97ae042018-08-30 08:32:36 +02002864 } else if (stat_config.run_count == 0) {
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002865 forever = true;
Jiri Olsad97ae042018-08-30 08:32:36 +02002866 stat_config.run_count = 1;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002867 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002868
Jiri Olsa54ac0b12018-08-30 08:32:50 +02002869 if (stat_config.walltime_run_table) {
2870 stat_config.walltime_run = zalloc(stat_config.run_count * sizeof(stat_config.walltime_run[0]));
2871 if (!stat_config.walltime_run) {
Jiri Olsae55c14a2018-04-23 11:08:21 +02002872 pr_err("failed to setup -r option");
2873 goto out;
2874 }
2875 }
2876
Jin Yao1d9f8d12017-12-05 22:03:10 +08002877 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2878 !target__has_task(&target)) {
2879 if (!target.system_wide || target.cpu_list) {
2880 fprintf(stderr, "The --per-thread option is only "
2881 "available when monitoring via -p -t -a "
2882 "options or only --per-thread.\n");
2883 parse_options_usage(NULL, stat_options, "p", 1);
2884 parse_options_usage(NULL, stat_options, "t", 1);
2885 goto out;
2886 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002887 }
2888
2889 /*
2890 * no_aggr, cgroup are for system-wide only
2891 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2892 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002893 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2894 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002895 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002896 fprintf(stderr, "both cgroup and no-aggregation "
2897 "modes only available in system-wide mode\n");
2898
Jiri Olsae0547312015-11-05 15:40:45 +01002899 parse_options_usage(stat_usage, stat_options, "G", 1);
2900 parse_options_usage(NULL, stat_options, "A", 1);
2901 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002902 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002903 }
2904
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002905 if (add_default_attributes())
2906 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002907
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002908 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002909
Jin Yao1d9f8d12017-12-05 22:03:10 +08002910 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2911 target.per_thread = true;
2912
Namhyung Kim77a6f012012-05-07 14:09:04 +09002913 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002914 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002915 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002916 parse_options_usage(stat_usage, stat_options, "p", 1);
2917 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002918 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002919 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002920 parse_options_usage(stat_usage, stat_options, "C", 1);
2921 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002922 }
2923 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002924 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002925
2926 /*
2927 * Initialize thread_map with comm names,
2928 * so we could print it out on output.
2929 */
Jin Yao56739442017-12-05 22:03:07 +08002930 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002931 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08002932 if (target.system_wide) {
2933 if (runtime_stat_new(&stat_config,
2934 thread_map__nr(evsel_list->threads))) {
2935 goto out;
2936 }
2937 }
2938 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002939
yuzhoujiandb06a262018-01-29 10:25:22 +01002940 if (stat_config.times && interval)
2941 interval_count = true;
2942 else if (stat_config.times && !interval) {
2943 pr_err("interval-count option should be used together with "
2944 "interval-print.\n");
2945 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2946 parse_options_usage(stat_usage, stat_options, "I", 1);
2947 goto out;
2948 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002949
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002950 if (timeout && timeout < 100) {
2951 if (timeout < 10) {
2952 pr_err("timeout must be >= 10ms.\n");
2953 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2954 goto out;
2955 } else
2956 pr_warning("timeout < 100ms. "
2957 "The overhead percentage could be high in some cases. "
2958 "Please proceed with caution.\n");
2959 }
2960 if (timeout && interval) {
2961 pr_err("timeout option is not supported with interval-print.\n");
2962 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2963 parse_options_usage(stat_usage, stat_options, "I", 1);
2964 goto out;
2965 }
2966
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002967 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002968 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002969
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002970 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002971 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002972
Ingo Molnar58d7e992009-05-15 11:03:23 +02002973 /*
Jiri Olsa7d9ad162018-08-30 08:32:14 +02002974 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
2975 * while avoiding that older tools show confusing messages.
2976 *
2977 * However for pipe sessions we need to keep it zero,
2978 * because script's perf_evsel__check_attr is triggered
2979 * by attr->sample_type != 0, and we can't run it on
2980 * stat sessions.
2981 */
2982 stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
2983
2984 /*
Ingo Molnar58d7e992009-05-15 11:03:23 +02002985 * We dont want to block the signals - that would cause
2986 * child tasks to inherit that and Ctrl-C would not work.
2987 * What we want is for Ctrl-C to work in the exec()-ed
2988 * task, but being ignored by perf stat itself:
2989 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002990 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002991 if (!forever)
2992 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002993 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002994 signal(SIGALRM, skip_signal);
2995 signal(SIGABRT, skip_signal);
2996
Ingo Molnar42202dd2009-06-13 14:57:28 +02002997 status = 0;
Jiri Olsad97ae042018-08-30 08:32:36 +02002998 for (run_idx = 0; forever || run_idx < stat_config.run_count; run_idx++) {
2999 if (stat_config.run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02003000 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
3001 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02003002
Jiri Olsae55c14a2018-04-23 11:08:21 +02003003 status = run_perf_stat(argc, argv, run_idx);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003004 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02003005 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02003006 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003007 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02003008 }
3009
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003010 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02003011 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003012
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003013 if (STAT_RECORD) {
3014 /*
3015 * We synthesize the kernel mmap record just so that older tools
3016 * don't emit warnings about not being able to resolve symbols
3017 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3018 * a saner message about no samples being in the perf.data file.
3019 *
3020 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01003021 * in header.c at the moment 'perf stat record' gets introduced, which
3022 * is not really needed once we start adding the stat specific PERF_RECORD_
3023 * records, but the need to suppress the kptr_restrict messages in older
3024 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003025 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003026 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003027 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3028 process_synthesized_event,
3029 &perf_stat.session->machines.host);
3030 if (err) {
3031 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3032 "older tools may produce warnings about this file\n.");
3033 }
3034
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003035 if (!interval) {
3036 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3037 pr_err("failed to write stat round event\n");
3038 }
3039
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003040 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003041 perf_stat.session->header.data_size += perf_stat.bytes_written;
3042 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3043 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003044
3045 perf_session__delete(perf_stat.session);
3046 }
3047
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003048 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003049 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003050out:
Jiri Olsa54ac0b12018-08-30 08:32:50 +02003051 free(stat_config.walltime_run);
Jiri Olsae55c14a2018-04-23 11:08:21 +02003052
Kan Liangdaefd0b2017-05-26 12:05:38 -07003053 if (smi_cost && smi_reset)
3054 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3055
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003056 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003057
3058 runtime_stat_delete(&stat_config);
3059
Ingo Molnar42202dd2009-06-13 14:57:28 +02003060 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003061}