blob: a4f662a462c66c2112db0a1947d21d5d97d2431e [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 Olsaba6039b62015-11-05 15:40:55 +010068#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020069
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030070#include <linux/time64.h>
Andi Kleen44b1e602016-05-30 12:49:42 -030071#include <api/fs/fs.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030072#include <errno.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030073#include <signal.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020074#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020075#include <sys/prctl.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030076#include <inttypes.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020077#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070078#include <math.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030079#include <sys/types.h>
80#include <sys/stat.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030081#include <sys/wait.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030082#include <unistd.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020083
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030084#include "sane_ctype.h"
85
Stephane Eraniand7470b62010-12-01 18:49:05 +020086#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060087#define CNTR_NOT_SUPPORTED "<not supported>"
88#define CNTR_NOT_COUNTED "<not counted>"
Kan Liangdaefd0b2017-05-26 12:05:38 -070089#define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
Stephane Eraniand7470b62010-12-01 18:49:05 +020090
Jiri Olsad4f63a42015-06-26 11:29:26 +020091static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010092
Andi Kleen4cabc3d2013-08-21 16:47:26 -070093/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020094static const char *transaction_attrs = {
95 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070096 "{"
97 "instructions,"
98 "cycles,"
99 "cpu/cycles-t/,"
100 "cpu/tx-start/,"
101 "cpu/el-start/,"
102 "cpu/cycles-ct/"
103 "}"
104};
105
106/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +0200107static const char * transaction_limited_attrs = {
108 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700109 "{"
110 "instructions,"
111 "cycles,"
112 "cpu/cycles-t/,"
113 "cpu/tx-start/"
114 "}"
115};
116
Andi Kleen44b1e602016-05-30 12:49:42 -0300117static const char * topdown_attrs[] = {
118 "topdown-total-slots",
119 "topdown-slots-retired",
120 "topdown-recovery-bubbles",
121 "topdown-fetch-bubbles",
122 "topdown-slots-issued",
123 NULL,
124};
125
Kan Liangdaefd0b2017-05-26 12:05:38 -0700126static const char *smi_cost_attrs = {
127 "{"
128 "msr/aperf/,"
129 "msr/smi/,"
130 "cycles"
131 "}"
132};
133
Robert Richter666e6d42012-04-05 18:26:27 +0200134static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200135
Andi Kleenb18f3e32017-08-31 12:40:31 -0700136static struct rblist metric_events;
137
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300138static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900139 .uid = UINT_MAX,
140};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530141
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100142typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
143
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530144static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200145static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200146static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000147static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200148static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700149static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300150static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700151static bool smi_cost = false;
152static bool smi_reset = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200153static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200154static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200155static const char *csv_sep = NULL;
156static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800157static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200158static const char *pre_cmd = NULL;
159static const char *post_cmd = NULL;
160static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700161static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100162static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500163static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800164static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300165static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700166static bool no_merge = false;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200167static bool walltime_run_table = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100168static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100169static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100170static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100171static bool append_file;
yuzhoujiandb06a262018-01-29 10:25:22 +0100172static bool interval_count;
Jiri Olsae0547312015-11-05 15:40:45 +0100173static const char *output_name;
174static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100175static int print_free_counters_hint;
Kan Liang30060ea2018-04-24 11:20:11 -0700176static int print_mixed_hw_group_error;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200177static u64 *walltime_run;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200178
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100179struct perf_stat {
180 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100181 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100182 struct perf_session *session;
183 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100184 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100185 bool maps_allocated;
186 struct cpu_map *cpus;
187 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100188 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100189};
190
191static struct perf_stat perf_stat;
192#define STAT_RECORD perf_stat.record
193
Liming Wang60666c62009-12-31 16:05:50 +0800194static volatile int done = 0;
195
Jiri Olsa421a50f2015-07-21 14:31:22 +0200196static struct perf_stat_config stat_config = {
197 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200198 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200199};
200
Andi Kleene864c5c2017-08-31 12:40:35 -0700201static bool is_duration_time(struct perf_evsel *evsel)
202{
203 return !strcmp(evsel->name, "duration_time");
204}
205
Stephane Eranian13370a92013-01-29 12:47:44 +0100206static inline void diff_timespec(struct timespec *r, struct timespec *a,
207 struct timespec *b)
208{
209 r->tv_sec = a->tv_sec - b->tv_sec;
210 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300211 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100212 r->tv_sec--;
213 } else {
214 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
215 }
216}
217
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200218static void perf_stat__reset_stats(void)
219{
Jin Yao56739442017-12-05 22:03:07 +0800220 int i;
221
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200222 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200223 perf_stat__reset_shadow_stats();
Jin Yao56739442017-12-05 22:03:07 +0800224
225 for (i = 0; i < stat_config.stats_num; i++)
226 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200227}
228
Jiri Olsacac21422012-11-12 18:34:00 +0100229static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200230{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200231 struct perf_event_attr *attr = &evsel->attr;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200232 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200233
Jiri Olsa82bf3112017-07-26 14:02:06 +0200234 if (stat_config.scale) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200235 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
236 PERF_FORMAT_TOTAL_TIME_RUNNING;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200237 }
238
239 /*
240 * The event is part of non trivial group, let's enable
241 * the group read (for leader) and ID retrieval for all
242 * members.
243 */
244 if (leader->nr_members > 1)
245 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200246
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200247 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300248
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100249 /*
250 * Some events get initialized with sample_(period/type) set,
251 * like tracepoints. Clear it up for counting.
252 */
253 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100254
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100255 /*
256 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
257 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100258 *
259 * However for pipe sessions we need to keep it zero,
260 * because script's perf_evsel__check_attr is triggered
261 * by attr->sample_type != 0, and we can't run it on
262 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100263 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100264 if (!(STAT_RECORD && perf_stat.data.is_pipe))
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100265 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100266
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100267 /*
268 * Disabling all counters initially, they will be enabled
269 * either manually by us or by kernel via enable_on_exec
270 * set later.
271 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100272 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100273 attr->disabled = 1;
274
Jiri Olsac8280ce2015-12-03 10:06:45 +0100275 /*
276 * In case of initial_delay we enable tracee
277 * events manually.
278 */
279 if (target__none(&target) && !initial_delay)
280 attr->enable_on_exec = 1;
281 }
282
Jin Yao1d9f8d12017-12-05 22:03:10 +0800283 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300284 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200285
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300286 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200287}
288
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200289/*
290 * Does the counter have nsecs as a unit?
291 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200292static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200293{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200294 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
295 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200296 return 1;
297
298 return 0;
299}
300
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100301static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100302 union perf_event *event,
303 struct perf_sample *sample __maybe_unused,
304 struct machine *machine __maybe_unused)
305{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100306 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100307 pr_err("failed to write perf data, error: %m\n");
308 return -1;
309 }
310
311 perf_stat.bytes_written += event->header.size;
312 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100313}
314
Jiri Olsa1975d362015-11-05 15:40:56 +0100315static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100316{
Jiri Olsa1975d362015-11-05 15:40:56 +0100317 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100318 process_synthesized_event,
319 NULL);
320}
321
322#define WRITE_STAT_ROUND_EVENT(time, interval) \
323 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
324
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100325#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
326
327static int
328perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
329 struct perf_counts_values *count)
330{
331 struct perf_sample_id *sid = SID(counter, cpu, thread);
332
333 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
334 process_synthesized_event, NULL);
335}
336
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200337/*
338 * Read out the results of a single counter:
339 * do not aggregate counts across CPUs in system-wide mode
340 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200341static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200342{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100343 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100344 int ncpus, cpu, thread;
345
Jin Yao1d9f8d12017-12-05 22:03:10 +0800346 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Mark Rutland00e727b2016-07-15 11:08:10 +0100347 ncpus = perf_evsel__nr_cpus(counter);
348 else
349 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200350
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000351 if (!counter->supported)
352 return -ENOENT;
353
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100354 if (counter->system_wide)
355 nthreads = 1;
356
357 for (thread = 0; thread < nthreads; thread++) {
358 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200359 struct perf_counts_values *count;
360
361 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200362
363 /*
364 * The leader's group read loads data into its group members
365 * (via perf_evsel__read_counter) and sets threir count->loaded.
366 */
367 if (!count->loaded &&
368 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700369 counter->counts->scaled = -1;
370 perf_counts(counter->counts, cpu, thread)->ena = 0;
371 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100372 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700373 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100374
Jiri Olsa82bf3112017-07-26 14:02:06 +0200375 count->loaded = false;
376
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100377 if (STAT_RECORD) {
378 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
379 pr_err("failed to write stat event\n");
380 return -1;
381 }
382 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700383
384 if (verbose > 1) {
385 fprintf(stat_config.output,
386 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
387 perf_evsel__name(counter),
388 cpu,
389 count->val, count->ena, count->run);
390 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100391 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200392 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200393
394 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200395}
396
Mark Rutland3df33ef2016-08-09 14:04:29 +0100397static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200398{
399 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700400 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200401
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300402 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700403 ret = read_counter(counter);
404 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700405 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200406
Stephane Eraniandb49a712017-04-12 11:23:01 -0700407 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200408 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200409 }
410}
411
Jiri Olsaba411a92015-06-26 11:29:24 +0200412static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100413{
Stephane Eranian13370a92013-01-29 12:47:44 +0100414 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100415
Mark Rutland3df33ef2016-08-09 14:04:29 +0100416 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100417
Stephane Eranian13370a92013-01-29 12:47:44 +0100418 clock_gettime(CLOCK_MONOTONIC, &ts);
419 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100420
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100421 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300422 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100423 pr_err("failed to write stat round event\n");
424 }
425
Andi Kleenb90f1332017-08-31 12:40:36 -0700426 init_stats(&walltime_nsecs_stats);
427 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200428 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100429}
430
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100431static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700432{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100433 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300434 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100435
436 /*
437 * We need to enable counters only if:
438 * - we don't have tracee (attaching to task or cpu)
439 * - we have initial delay configured
440 */
441 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100442 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700443}
444
Mark Rutland3df33ef2016-08-09 14:04:29 +0100445static void disable_counters(void)
446{
447 /*
448 * If we don't have tracee (attaching to task or cpu), counters may
449 * still be running. To get accurate group ratios, we must stop groups
450 * from counting before reading their constituent counters.
451 */
452 if (!target__none(&target))
453 perf_evlist__disable(evsel_list);
454}
455
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300456static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300457
458/*
459 * perf_evlist__prepare_workload will send a SIGUSR1
460 * if the fork fails, since we asked by setting its
461 * want_signal to true.
462 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300463static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
464 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300465{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300466 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300467}
468
Jiri Olsa664c98d2015-11-05 15:40:50 +0100469static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100470{
471 int err;
472
Jiri Olsa664c98d2015-11-05 15:40:50 +0100473 if (is_pipe) {
474 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
475 process_synthesized_event);
476 if (err < 0) {
477 pr_err("Couldn't synthesize attrs.\n");
478 return err;
479 }
480 }
481
Andi Kleenbfd8f722017-11-17 13:42:58 -0800482 err = perf_event__synthesize_extra_attr(NULL,
483 evsel_list,
484 process_synthesized_event,
485 is_pipe);
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100486
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100487 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
488 process_synthesized_event,
489 NULL);
490 if (err < 0) {
491 pr_err("Couldn't synthesize thread map.\n");
492 return err;
493 }
494
495 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
496 process_synthesized_event, NULL);
497 if (err < 0) {
498 pr_err("Couldn't synthesize thread map.\n");
499 return err;
500 }
501
502 err = perf_event__synthesize_stat_config(NULL, &stat_config,
503 process_synthesized_event, NULL);
504 if (err < 0) {
505 pr_err("Couldn't synthesize config.\n");
506 return err;
507 }
508
509 return 0;
510}
511
Jiri Olsa2af46462015-11-05 15:40:49 +0100512#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
513
Andi Kleen42811d52017-10-05 19:00:28 -0700514static int __store_counter_ids(struct perf_evsel *counter)
Jiri Olsa2af46462015-11-05 15:40:49 +0100515{
516 int cpu, thread;
517
Andi Kleen42811d52017-10-05 19:00:28 -0700518 for (cpu = 0; cpu < xyarray__max_x(counter->fd); cpu++) {
519 for (thread = 0; thread < xyarray__max_y(counter->fd);
520 thread++) {
Jiri Olsa2af46462015-11-05 15:40:49 +0100521 int fd = FD(counter, cpu, thread);
522
523 if (perf_evlist__id_add_fd(evsel_list, counter,
524 cpu, thread, fd) < 0)
525 return -1;
526 }
527 }
528
529 return 0;
530}
531
532static int store_counter_ids(struct perf_evsel *counter)
533{
534 struct cpu_map *cpus = counter->cpus;
535 struct thread_map *threads = counter->threads;
536
537 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
538 return -ENOMEM;
539
Andi Kleen42811d52017-10-05 19:00:28 -0700540 return __store_counter_ids(counter);
Jiri Olsa2af46462015-11-05 15:40:49 +0100541}
542
Jiri Olsa82bf3112017-07-26 14:02:06 +0200543static bool perf_evsel__should_store_id(struct perf_evsel *counter)
544{
545 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
546}
547
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700548static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
549{
550 struct perf_evsel *c2, *leader;
551 bool is_open = true;
552
553 leader = evsel->leader;
554 pr_debug("Weak group for %s/%d failed\n",
555 leader->name, leader->nr_members);
556
557 /*
558 * for_each_group_member doesn't work here because it doesn't
559 * include the first entry.
560 */
561 evlist__for_each_entry(evsel_list, c2) {
562 if (c2 == evsel)
563 is_open = false;
564 if (c2->leader == leader) {
565 if (is_open)
566 perf_evsel__close(c2);
567 c2->leader = c2;
568 c2->nr_members = 0;
569 }
570 }
571 return leader;
572}
573
Jiri Olsae55c14a2018-04-23 11:08:21 +0200574static int __run_perf_stat(int argc, const char **argv, int run_idx)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200575{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200576 int interval = stat_config.interval;
yuzhoujiandb06a262018-01-29 10:25:22 +0100577 int times = stat_config.times;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100578 int timeout = stat_config.timeout;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300579 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200580 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100581 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100582 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100583 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200584 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300585 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100586 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600587 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200588
Stephane Eranian13370a92013-01-29 12:47:44 +0100589 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300590 ts.tv_sec = interval / USEC_PER_MSEC;
591 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100592 } else if (timeout) {
593 ts.tv_sec = timeout / USEC_PER_MSEC;
594 ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100595 } else {
596 ts.tv_sec = 1;
597 ts.tv_nsec = 0;
598 }
599
Liming Wang60666c62009-12-31 16:05:50 +0800600 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100601 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300602 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900603 perror("failed to prepare workload");
604 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800605 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900606 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000607 }
608
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200609 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300610 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200611
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300612 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300613try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100614 if (create_perf_stat_counter(counter) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700615
616 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700617 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700618 counter->leader != counter &&
619 counter->weak_group) {
620 counter = perf_evsel__reset_weak_group(counter);
621 goto try_again;
622 }
623
David Ahern979987a2012-05-08 09:29:16 -0600624 /*
625 * PPC returns ENXIO for HW counters until 2.6.37
626 * (behavior changed with commit b0a873e).
627 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100628 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600629 errno == ENOENT || errno == EOPNOTSUPP ||
630 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900631 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600632 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300633 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600634 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400635
636 if ((counter->leader != counter) ||
637 !(counter->leader->nr_members > 1))
638 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300639 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900640 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300641 ui__warning("%s\n", msg);
642 goto try_again;
Jin Yaoab6c79b2018-01-16 23:43:08 +0800643 } else if (target__has_per_thread(&target) &&
644 evsel_list->threads &&
645 evsel_list->threads->err_thread != -1) {
646 /*
647 * For global --per-thread case, skip current
648 * error thread.
649 */
650 if (!thread_map__remove(evsel_list->threads,
651 evsel_list->threads->err_thread)) {
652 evsel_list->threads->err_thread = -1;
653 goto try_again;
654 }
655 }
Ingo Molnarede70292011-04-28 08:48:42 +0200656
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300657 perf_evsel__open_strerror(counter, &target,
658 errno, msg, sizeof(msg));
659 ui__error("%s\n", msg);
660
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200661 if (child_pid != -1)
662 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600663
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200664 return -1;
665 }
David Ahern2cee77c2011-05-30 08:55:59 -0600666 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100667
668 l = strlen(counter->unit);
669 if (l > unit_width)
670 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100671
Jiri Olsa82bf3112017-07-26 14:02:06 +0200672 if (perf_evsel__should_store_id(counter) &&
673 store_counter_ids(counter))
Jiri Olsa2af46462015-11-05 15:40:49 +0100674 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300675 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200676
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300677 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300678 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300679 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300680 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100681 return -1;
682 }
683
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600684 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300685 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600686 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
687 str_error_r(errno, msg, sizeof(msg)));
688 return -1;
689 }
690
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100691 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100692 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100693
Jiri Olsa664c98d2015-11-05 15:40:50 +0100694 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100695 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100696 } else {
697 err = perf_session__write_header(perf_stat.session, evsel_list,
698 fd, false);
699 }
700
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100701 if (err < 0)
702 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100703
Jiri Olsa664c98d2015-11-05 15:40:50 +0100704 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100705 if (err < 0)
706 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100707 }
708
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200709 /*
710 * Enable counters and exec the command:
711 */
712 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100713 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200714
Liming Wang60666c62009-12-31 16:05:50 +0800715 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900716 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100717 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900718
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100719 if (interval || timeout) {
Stephane Eranian13370a92013-01-29 12:47:44 +0100720 while (!waitpid(child_pid, &status, WNOHANG)) {
721 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100722 if (timeout)
723 break;
Jiri Olsaba411a92015-06-26 11:29:24 +0200724 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100725 if (interval_count && !(--times))
726 break;
Stephane Eranian13370a92013-01-29 12:47:44 +0100727 }
728 }
Milian Wolffdfc9eec2017-09-12 17:25:23 +0200729 waitpid(child_pid, &status, 0);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300730
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300731 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300732 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300733 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300734 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300735 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300736
Andi Kleen33e49ea2011-09-15 14:31:40 -0700737 if (WIFSIGNALED(status))
738 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800739 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100740 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100741 while (!done) {
742 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100743 if (timeout)
744 break;
yuzhoujiandb06a262018-01-29 10:25:22 +0100745 if (interval) {
Jiri Olsaba411a92015-06-26 11:29:24 +0200746 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100747 if (interval_count && !(--times))
748 break;
749 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100750 }
Liming Wang60666c62009-12-31 16:05:50 +0800751 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200752
Mark Rutland3df33ef2016-08-09 14:04:29 +0100753 disable_counters();
754
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200755 t1 = rdclock();
756
Jiri Olsae55c14a2018-04-23 11:08:21 +0200757 if (walltime_run_table)
758 walltime_run[run_idx] = t1 - t0;
759
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200760 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200761
Mark Rutland3df33ef2016-08-09 14:04:29 +0100762 /*
763 * Closing a group leader splits the group, and as we only disable
764 * group leaders, results in remaining events becoming enabled. To
765 * avoid arbitrary skew, we must read all counters before closing any
766 * group leaders.
767 */
768 read_counters();
769 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200770
Ingo Molnar42202dd2009-06-13 14:57:28 +0200771 return WEXITSTATUS(status);
772}
773
Jiri Olsae55c14a2018-04-23 11:08:21 +0200774static int run_perf_stat(int argc, const char **argv, int run_idx)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200775{
776 int ret;
777
778 if (pre_cmd) {
779 ret = system(pre_cmd);
780 if (ret)
781 return ret;
782 }
783
784 if (sync_run)
785 sync();
786
Jiri Olsae55c14a2018-04-23 11:08:21 +0200787 ret = __run_perf_stat(argc, argv, run_idx);
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200788 if (ret)
789 return ret;
790
791 if (post_cmd) {
792 ret = system(post_cmd);
793 if (ret)
794 return ret;
795 }
796
797 return ret;
798}
799
Andi Kleend73515c2015-03-11 07:16:27 -0700800static void print_running(u64 run, u64 ena)
801{
802 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200803 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700804 csv_sep,
805 run,
806 csv_sep,
807 ena ? 100.0 * run / ena : 100.0);
808 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200809 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700810 }
811}
812
Ingo Molnarf99844c2011-04-27 05:35:39 +0200813static void print_noise_pct(double total, double avg)
814{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800815 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200816
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700817 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200818 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600819 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200820 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200821}
822
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200823static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200824{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200825 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200826
Peter Zijlstra849abde2009-09-04 18:23:38 +0200827 if (run_count == 1)
828 return;
829
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300830 ps = evsel->stats;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200831 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200832}
833
Stephane Eranian12c08a92013-02-14 13:57:29 +0100834static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200835{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200836 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100837 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200838 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100839 cpu_map__id_to_socket(id),
840 csv_output ? 0 : -8,
841 cpu_map__id_to_cpu(id),
842 csv_sep,
843 csv_output ? 0 : 4,
844 nr,
845 csv_sep);
846 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100847 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200848 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100849 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100850 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100851 csv_sep,
852 csv_output ? 0 : 4,
853 nr,
854 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100855 break;
856 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200857 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200858 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100859 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100860 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200861 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200862 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200863 csv_output ? 0 : 16,
864 thread_map__comm(evsel->threads, id),
865 csv_output ? 0 : -8,
866 thread_map__pid(evsel->threads, id),
867 csv_sep);
868 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100869 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200870 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100871 default:
872 break;
873 }
874}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200875
Andi Kleen140aead2016-01-30 09:06:49 -0800876struct outstate {
877 FILE *fh;
878 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800879 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800880 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800881 int id, nr;
882 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800883};
884
885#define METRIC_LEN 35
886
887static void new_line_std(void *ctx)
888{
889 struct outstate *os = ctx;
890
891 os->newline = true;
892}
893
894static void do_new_line_std(struct outstate *os)
895{
896 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800897 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800898 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800899 if (stat_config.aggr_mode == AGGR_NONE)
900 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800901 fprintf(os->fh, " ");
902}
903
904static void print_metric_std(void *ctx, const char *color, const char *fmt,
905 const char *unit, double val)
906{
907 struct outstate *os = ctx;
908 FILE *out = os->fh;
909 int n;
910 bool newline = os->newline;
911
912 os->newline = false;
913
914 if (unit == NULL || fmt == NULL) {
915 fprintf(out, "%-*s", METRIC_LEN, "");
916 return;
917 }
918
919 if (newline)
920 do_new_line_std(os);
921
922 n = fprintf(out, " # ");
923 if (color)
924 n += color_fprintf(out, color, fmt, val);
925 else
926 n += fprintf(out, fmt, val);
927 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
928}
929
Andi Kleen92a61f62016-02-29 14:36:21 -0800930static void new_line_csv(void *ctx)
931{
932 struct outstate *os = ctx;
933 int i;
934
935 fputc('\n', os->fh);
936 if (os->prefix)
937 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800938 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800939 for (i = 0; i < os->nfields; i++)
940 fputs(csv_sep, os->fh);
941}
942
943static void print_metric_csv(void *ctx,
944 const char *color __maybe_unused,
945 const char *fmt, const char *unit, double val)
946{
947 struct outstate *os = ctx;
948 FILE *out = os->fh;
949 char buf[64], *vals, *ends;
950
951 if (unit == NULL || fmt == NULL) {
Ilya Pronin40c21892018-03-05 22:43:53 -0800952 fprintf(out, "%s%s", csv_sep, csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800953 return;
954 }
955 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900956 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800957 while (isdigit(*ends) || *ends == '.')
958 ends++;
959 *ends = 0;
960 while (isspace(*unit))
961 unit++;
962 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
963}
964
Andi Kleen54b50912016-03-03 15:57:36 -0800965#define METRIC_ONLY_LEN 20
966
967/* Filter out some columns that don't work well in metrics only mode */
968
969static bool valid_only_metric(const char *unit)
970{
971 if (!unit)
972 return false;
973 if (strstr(unit, "/sec") ||
974 strstr(unit, "hz") ||
975 strstr(unit, "Hz") ||
976 strstr(unit, "CPUs utilized"))
977 return false;
978 return true;
979}
980
981static const char *fixunit(char *buf, struct perf_evsel *evsel,
982 const char *unit)
983{
984 if (!strncmp(unit, "of all", 6)) {
985 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
986 unit);
987 return buf;
988 }
989 return unit;
990}
991
992static void print_metric_only(void *ctx, const char *color, const char *fmt,
993 const char *unit, double val)
994{
995 struct outstate *os = ctx;
996 FILE *out = os->fh;
997 int n;
998 char buf[1024];
999 unsigned mlen = METRIC_ONLY_LEN;
1000
1001 if (!valid_only_metric(unit))
1002 return;
1003 unit = fixunit(buf, os->evsel, unit);
1004 if (color)
1005 n = color_fprintf(out, color, fmt, val);
1006 else
1007 n = fprintf(out, fmt, val);
1008 if (n > METRIC_ONLY_LEN)
1009 n = METRIC_ONLY_LEN;
1010 if (mlen < strlen(unit))
1011 mlen = strlen(unit) + 1;
1012 fprintf(out, "%*s", mlen - n, "");
1013}
1014
1015static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
1016 const char *fmt,
1017 const char *unit, double val)
1018{
1019 struct outstate *os = ctx;
1020 FILE *out = os->fh;
1021 char buf[64], *vals, *ends;
1022 char tbuf[1024];
1023
1024 if (!valid_only_metric(unit))
1025 return;
1026 unit = fixunit(tbuf, os->evsel, unit);
1027 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +09001028 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -08001029 while (isdigit(*ends) || *ends == '.')
1030 ends++;
1031 *ends = 0;
1032 fprintf(out, "%s%s", vals, csv_sep);
1033}
1034
1035static void new_line_metric(void *ctx __maybe_unused)
1036{
1037}
1038
1039static void print_metric_header(void *ctx, const char *color __maybe_unused,
1040 const char *fmt __maybe_unused,
1041 const char *unit, double val __maybe_unused)
1042{
1043 struct outstate *os = ctx;
1044 char tbuf[1024];
1045
1046 if (!valid_only_metric(unit))
1047 return;
1048 unit = fixunit(tbuf, os->evsel, unit);
1049 if (csv_output)
1050 fprintf(os->fh, "%s%s", unit, csv_sep);
1051 else
1052 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
1053}
1054
Andi Kleenda88c7f2014-09-24 13:50:46 -07001055static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001056{
Jiri Olsa58215222015-07-21 14:31:24 +02001057 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001058 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +01001059 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -06001060 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001061
Stephane Eranian410136f2013-11-12 17:58:49 +01001062 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
1063 fmt_n = csv_output ? "%s" : "%-25s";
1064
Andi Kleenda88c7f2014-09-24 13:50:46 -07001065 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001066
David Ahern4bbe5a62013-09-28 14:28:00 -06001067 scnprintf(name, sizeof(name), "%s%s",
1068 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +01001069
1070 fprintf(output, fmt_v, msecs, csv_sep);
1071
1072 if (csv_output)
1073 fprintf(output, "%s%s", evsel->unit, csv_sep);
1074 else
1075 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1076
1077 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001078
Stephane Eranian023695d2011-02-14 11:20:01 +02001079 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001080 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001081}
1082
Andi Kleen44d49a62016-02-29 14:36:22 -08001083static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1084{
1085 int i;
1086
1087 if (!aggr_get_id)
1088 return 0;
1089
1090 if (stat_config.aggr_mode == AGGR_NONE)
1091 return id;
1092
1093 if (stat_config.aggr_mode == AGGR_GLOBAL)
1094 return 0;
1095
1096 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1097 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1098
1099 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1100 return cpu2;
1101 }
1102 return 0;
1103}
1104
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001105static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1106{
Jiri Olsa58215222015-07-21 14:31:24 +02001107 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001108 double sc = evsel->scale;
1109 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001110
1111 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001112 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001113 } else {
1114 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001115 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001116 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001117 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001118 }
1119
1120 aggr_printout(evsel, id, nr);
1121
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001122 fprintf(output, fmt, avg, csv_sep);
1123
1124 if (evsel->unit)
1125 fprintf(output, "%-*s%s",
1126 csv_output ? 0 : unit_width,
1127 evsel->unit, csv_sep);
1128
1129 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1130
1131 if (evsel->cgrp)
1132 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001133}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001134
Kan Liang30060ea2018-04-24 11:20:11 -07001135static bool is_mixed_hw_group(struct perf_evsel *counter)
1136{
1137 struct perf_evlist *evlist = counter->evlist;
1138 u32 pmu_type = counter->attr.type;
1139 struct perf_evsel *pos;
1140
1141 if (counter->nr_members < 2)
1142 return false;
1143
1144 evlist__for_each_entry(evlist, pos) {
1145 /* software events can be part of any hardware group */
1146 if (pos->attr.type == PERF_TYPE_SOFTWARE)
1147 continue;
1148 if (pmu_type == PERF_TYPE_SOFTWARE) {
1149 pmu_type = pos->attr.type;
1150 continue;
1151 }
1152 if (pmu_type != pos->attr.type)
1153 return true;
1154 }
1155
1156 return false;
1157}
1158
Andi Kleenf9483392016-01-30 09:06:50 -08001159static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +08001160 char *prefix, u64 run, u64 ena, double noise,
1161 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001162{
Andi Kleen140aead2016-01-30 09:06:49 -08001163 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001164 struct outstate os = {
1165 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001166 .prefix = prefix ? prefix : "",
1167 .id = id,
1168 .nr = nr,
1169 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001170 };
Andi Kleen140aead2016-01-30 09:06:49 -08001171 print_metric_t pm = print_metric_std;
1172 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001173
Andi Kleen54b50912016-03-03 15:57:36 -08001174 if (metric_only) {
1175 nl = new_line_metric;
1176 if (csv_output)
1177 pm = print_metric_only_csv;
1178 else
1179 pm = print_metric_only;
1180 } else
1181 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001182
Andi Kleen54b50912016-03-03 15:57:36 -08001183 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001184 static int aggr_fields[] = {
1185 [AGGR_GLOBAL] = 0,
1186 [AGGR_THREAD] = 1,
1187 [AGGR_NONE] = 1,
1188 [AGGR_SOCKET] = 2,
1189 [AGGR_CORE] = 2,
1190 };
1191
1192 pm = print_metric_csv;
1193 nl = new_line_csv;
1194 os.nfields = 3;
1195 os.nfields += aggr_fields[stat_config.aggr_mode];
1196 if (counter->cgrp)
1197 os.nfields++;
1198 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001199 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001200 if (metric_only) {
1201 pm(&os, NULL, "", "", 0);
1202 return;
1203 }
Andi Kleencb110f42016-01-30 09:06:51 -08001204 aggr_printout(counter, id, nr);
1205
1206 fprintf(stat_config.output, "%*s%s",
1207 csv_output ? 0 : 18,
1208 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1209 csv_sep);
1210
Kan Liang30060ea2018-04-24 11:20:11 -07001211 if (counter->supported) {
Borislav Petkov02d492e2017-02-07 01:40:05 +01001212 print_free_counters_hint = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001213 if (is_mixed_hw_group(counter))
1214 print_mixed_hw_group_error = 1;
1215 }
Borislav Petkov02d492e2017-02-07 01:40:05 +01001216
Andi Kleencb110f42016-01-30 09:06:51 -08001217 fprintf(stat_config.output, "%-*s%s",
1218 csv_output ? 0 : unit_width,
1219 counter->unit, csv_sep);
1220
1221 fprintf(stat_config.output, "%*s",
1222 csv_output ? 0 : -25,
1223 perf_evsel__name(counter));
1224
1225 if (counter->cgrp)
1226 fprintf(stat_config.output, "%s%s",
1227 csv_sep, counter->cgrp->name);
1228
Andi Kleen92a61f62016-02-29 14:36:21 -08001229 if (!csv_output)
1230 pm(&os, NULL, NULL, "", 0);
1231 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001232 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001233 if (csv_output)
1234 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001235 return;
1236 }
1237
Andi Kleen54b50912016-03-03 15:57:36 -08001238 if (metric_only)
1239 /* nothing */;
1240 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001241 nsec_printout(id, nr, counter, uval);
1242 else
1243 abs_printout(id, nr, counter, uval);
1244
Andi Kleen140aead2016-01-30 09:06:49 -08001245 out.print_metric = pm;
1246 out.new_line = nl;
1247 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001248 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001249
Andi Kleen54b50912016-03-03 15:57:36 -08001250 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001251 print_noise(counter, noise);
1252 print_running(run, ena);
1253 }
1254
1255 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001256 first_shadow_cpu(counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001257 &out, &metric_events, st);
Andi Kleen54b50912016-03-03 15:57:36 -08001258 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001259 print_noise(counter, noise);
1260 print_running(run, ena);
1261 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001262}
1263
Andi Kleen44d49a62016-02-29 14:36:22 -08001264static void aggr_update_shadow(void)
1265{
1266 int cpu, s2, id, s;
1267 u64 val;
1268 struct perf_evsel *counter;
1269
1270 for (s = 0; s < aggr_map->nr; s++) {
1271 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001272 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001273 val = 0;
1274 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1275 s2 = aggr_get_id(evsel_list->cpus, cpu);
1276 if (s2 != id)
1277 continue;
1278 val += perf_counts(counter->counts, cpu, 0)->val;
1279 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001280 perf_stat__update_shadow_stats(counter, val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001281 first_shadow_cpu(counter, id),
1282 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001283 }
1284 }
1285}
1286
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001287static void uniquify_event_name(struct perf_evsel *counter)
1288{
1289 char *new_name;
1290 char *config;
1291
Kan Liang80ee8c52018-04-24 11:20:14 -07001292 if (counter->uniquified_name ||
1293 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001294 strlen(counter->pmu_name)))
1295 return;
1296
1297 config = strchr(counter->name, '/');
1298 if (config) {
1299 if (asprintf(&new_name,
1300 "%s%s", counter->pmu_name, config) > 0) {
1301 free(counter->name);
1302 counter->name = new_name;
1303 }
1304 } else {
1305 if (asprintf(&new_name,
1306 "%s [%s]", counter->name, counter->pmu_name) > 0) {
1307 free(counter->name);
1308 counter->name = new_name;
1309 }
1310 }
Kan Liang80ee8c52018-04-24 11:20:14 -07001311
1312 counter->uniquified_name = true;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001313}
1314
Andi Kleen430daf22017-03-20 13:17:00 -07001315static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001316 void (*cb)(struct perf_evsel *counter, void *data,
1317 bool first),
1318 void *data)
1319{
Andi Kleen430daf22017-03-20 13:17:00 -07001320 struct perf_evsel *alias;
1321
1322 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1323 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1324 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1325 alias->scale != counter->scale ||
1326 alias->cgrp != counter->cgrp ||
1327 strcmp(alias->unit, counter->unit) ||
1328 nsec_counter(alias) != nsec_counter(counter))
1329 break;
1330 alias->merged_stat = true;
1331 cb(alias, data, false);
1332 }
1333}
1334
1335static bool collect_data(struct perf_evsel *counter,
1336 void (*cb)(struct perf_evsel *counter, void *data,
1337 bool first),
1338 void *data)
1339{
1340 if (counter->merged_stat)
1341 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001342 cb(counter, data, true);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001343 if (no_merge)
1344 uniquify_event_name(counter);
1345 else if (counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001346 collect_all_aliases(counter, cb, data);
1347 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001348}
1349
1350struct aggr_data {
1351 u64 ena, run, val;
1352 int id;
1353 int nr;
1354 int cpu;
1355};
1356
1357static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1358{
1359 struct aggr_data *ad = data;
1360 int cpu, s2;
1361
1362 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1363 struct perf_counts_values *counts;
1364
1365 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1366 if (s2 != ad->id)
1367 continue;
1368 if (first)
1369 ad->nr++;
1370 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001371 /*
1372 * When any result is bad, make them all to give
1373 * consistent output in interval mode.
1374 */
1375 if (counts->ena == 0 || counts->run == 0 ||
1376 counter->counts->scaled == -1) {
1377 ad->ena = 0;
1378 ad->run = 0;
1379 break;
1380 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001381 ad->val += counts->val;
1382 ad->ena += counts->ena;
1383 ad->run += counts->run;
1384 }
1385}
1386
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001387static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001388{
Jiri Olsa58215222015-07-21 14:31:24 +02001389 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001390 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001391 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001392 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001393 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001394 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001395
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001396 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001397 return;
1398
Andi Kleen44d49a62016-02-29 14:36:22 -08001399 aggr_update_shadow();
1400
Andi Kleen54b50912016-03-03 15:57:36 -08001401 /*
1402 * With metric_only everything is on a single line.
1403 * Without each counter has its own line.
1404 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001405 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001406 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001407 if (prefix && metric_only)
1408 fprintf(output, "%s", prefix);
1409
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001410 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001411 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001412 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001413 if (is_duration_time(counter))
1414 continue;
1415
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001416 ad.val = ad.ena = ad.run = 0;
1417 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001418 if (!collect_data(counter, aggr_cb, &ad))
1419 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001420 nr = ad.nr;
1421 ena = ad.ena;
1422 run = ad.run;
1423 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001424 if (first && metric_only) {
1425 first = false;
1426 aggr_printout(counter, id, nr);
1427 }
1428 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001429 fprintf(output, "%s", prefix);
1430
Stephane Eranian410136f2013-11-12 17:58:49 +01001431 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001432 printout(id, nr, counter, uval, prefix, run, ena, 1.0,
1433 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001434 if (!metric_only)
1435 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001436 }
Andi Kleen54b50912016-03-03 15:57:36 -08001437 if (metric_only)
1438 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001439 }
1440}
1441
Jin Yao29734552017-12-05 22:03:11 +08001442static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001443{
Jin Yao29734552017-12-05 22:03:11 +08001444 return ((struct perf_aggr_thread_value *)b)->val -
1445 ((struct perf_aggr_thread_value *)a)->val;
1446}
1447
1448static struct perf_aggr_thread_value *sort_aggr_thread(
1449 struct perf_evsel *counter,
1450 int nthreads, int ncpus,
1451 int *ret)
1452{
1453 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001454 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001455 struct perf_aggr_thread_value *buf;
1456
1457 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1458 if (!buf)
1459 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001460
1461 for (thread = 0; thread < nthreads; thread++) {
1462 u64 ena = 0, run = 0, val = 0;
1463
1464 for (cpu = 0; cpu < ncpus; cpu++) {
1465 val += perf_counts(counter->counts, cpu, thread)->val;
1466 ena += perf_counts(counter->counts, cpu, thread)->ena;
1467 run += perf_counts(counter->counts, cpu, thread)->run;
1468 }
1469
Jin Yao29734552017-12-05 22:03:11 +08001470 uval = val * counter->scale;
1471
1472 /*
1473 * Skip value 0 when enabling --per-thread globally,
1474 * otherwise too many 0 output.
1475 */
1476 if (uval == 0.0 && target__has_per_thread(&target))
1477 continue;
1478
1479 buf[i].counter = counter;
1480 buf[i].id = thread;
1481 buf[i].uval = uval;
1482 buf[i].val = val;
1483 buf[i].run = run;
1484 buf[i].ena = ena;
1485 i++;
1486 }
1487
1488 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1489
1490 if (ret)
1491 *ret = i;
1492
1493 return buf;
1494}
1495
1496static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1497{
1498 FILE *output = stat_config.output;
1499 int nthreads = thread_map__nr(counter->threads);
1500 int ncpus = cpu_map__nr(counter->cpus);
1501 int thread, sorted_threads, id;
1502 struct perf_aggr_thread_value *buf;
1503
1504 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads);
1505 if (!buf) {
1506 perror("cannot sort aggr thread");
1507 return;
1508 }
1509
1510 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001511 if (prefix)
1512 fprintf(output, "%s", prefix);
1513
Jin Yao29734552017-12-05 22:03:11 +08001514 id = buf[thread].id;
Jin Yao14e72a22017-12-05 22:03:08 +08001515 if (stat_config.stats)
Jin Yao29734552017-12-05 22:03:11 +08001516 printout(id, 0, buf[thread].counter, buf[thread].uval,
1517 prefix, buf[thread].run, buf[thread].ena, 1.0,
1518 &stat_config.stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001519 else
Jin Yao29734552017-12-05 22:03:11 +08001520 printout(id, 0, buf[thread].counter, buf[thread].uval,
1521 prefix, buf[thread].run, buf[thread].ena, 1.0,
1522 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001523 fputc('\n', output);
1524 }
Jin Yao29734552017-12-05 22:03:11 +08001525
1526 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001527}
1528
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001529struct caggr_data {
1530 double avg, avg_enabled, avg_running;
1531};
1532
1533static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1534 bool first __maybe_unused)
1535{
1536 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001537 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001538
1539 cd->avg += avg_stats(&ps->res_stats[0]);
1540 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1541 cd->avg_running += avg_stats(&ps->res_stats[2]);
1542}
1543
Ingo Molnar42202dd2009-06-13 14:57:28 +02001544/*
1545 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001546 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001547 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001548static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001549{
Jiri Olsa58215222015-07-21 14:31:24 +02001550 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001551 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001552 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001553
Andi Kleen430daf22017-03-20 13:17:00 -07001554 if (!collect_data(counter, counter_aggr_cb, &cd))
1555 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001556
Andi Kleen54b50912016-03-03 15:57:36 -08001557 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001558 fprintf(output, "%s", prefix);
1559
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001560 uval = cd.avg * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001561 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
1562 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001563 if (!metric_only)
1564 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001565}
1566
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001567static void counter_cb(struct perf_evsel *counter, void *data,
1568 bool first __maybe_unused)
1569{
1570 struct aggr_data *ad = data;
1571
1572 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1573 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1574 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1575}
1576
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001577/*
1578 * Print out the results of a single counter:
1579 * does not use aggregated count in system-wide
1580 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001581static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001582{
Jiri Olsa58215222015-07-21 14:31:24 +02001583 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001584 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001585 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001586 int cpu;
1587
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001588 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001589 struct aggr_data ad = { .cpu = cpu };
1590
Andi Kleen430daf22017-03-20 13:17:00 -07001591 if (!collect_data(counter, counter_cb, &ad))
1592 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001593 val = ad.val;
1594 ena = ad.ena;
1595 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001596
1597 if (prefix)
1598 fprintf(output, "%s", prefix);
1599
Stephane Eranian410136f2013-11-12 17:58:49 +01001600 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001601 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1602 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001603
Stephane Eranian4aa90152011-08-15 22:22:33 +02001604 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001605 }
1606}
1607
Andi Kleen206cab62016-03-03 15:57:37 -08001608static void print_no_aggr_metric(char *prefix)
1609{
1610 int cpu;
1611 int nrcpus = 0;
1612 struct perf_evsel *counter;
1613 u64 ena, run, val;
1614 double uval;
1615
1616 nrcpus = evsel_list->cpus->nr;
1617 for (cpu = 0; cpu < nrcpus; cpu++) {
1618 bool first = true;
1619
1620 if (prefix)
1621 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001622 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001623 if (is_duration_time(counter))
1624 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001625 if (first) {
1626 aggr_printout(counter, cpu, 0);
1627 first = false;
1628 }
1629 val = perf_counts(counter->counts, cpu, 0)->val;
1630 ena = perf_counts(counter->counts, cpu, 0)->ena;
1631 run = perf_counts(counter->counts, cpu, 0)->run;
1632
1633 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001634 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1635 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001636 }
1637 fputc('\n', stat_config.output);
1638 }
1639}
1640
Andi Kleen54b50912016-03-03 15:57:36 -08001641static int aggr_header_lens[] = {
1642 [AGGR_CORE] = 18,
1643 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001644 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001645 [AGGR_THREAD] = 24,
1646 [AGGR_GLOBAL] = 0,
1647};
1648
Andi Kleenc51fd632016-05-24 12:52:39 -07001649static const char *aggr_header_csv[] = {
1650 [AGGR_CORE] = "core,cpus,",
1651 [AGGR_SOCKET] = "socket,cpus",
1652 [AGGR_NONE] = "cpu,",
1653 [AGGR_THREAD] = "comm-pid,",
1654 [AGGR_GLOBAL] = ""
1655};
1656
Andi Kleen41c8ca22016-05-24 12:52:38 -07001657static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001658{
1659 struct perf_stat_output_ctx out;
1660 struct perf_evsel *counter;
1661 struct outstate os = {
1662 .fh = stat_config.output
1663 };
1664
1665 if (prefix)
1666 fprintf(stat_config.output, "%s", prefix);
1667
Andi Kleen41c8ca22016-05-24 12:52:38 -07001668 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001669 fprintf(stat_config.output, "%*s",
1670 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001671 if (csv_output) {
1672 if (stat_config.interval)
1673 fputs("time,", stat_config.output);
1674 fputs(aggr_header_csv[stat_config.aggr_mode],
1675 stat_config.output);
1676 }
Andi Kleen54b50912016-03-03 15:57:36 -08001677
1678 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001679 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001680 if (is_duration_time(counter))
1681 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001682 os.evsel = counter;
1683 out.ctx = &os;
1684 out.print_metric = print_metric_header;
1685 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001686 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001687 os.evsel = counter;
1688 perf_stat__print_shadow_stats(counter, 0,
1689 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001690 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001691 &metric_events,
1692 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001693 }
1694 fputc('\n', stat_config.output);
1695}
1696
Jiri Olsad4f63a42015-06-26 11:29:26 +02001697static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001698{
Jiri Olsa58215222015-07-21 14:31:24 +02001699 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001700 static int num_print_interval;
1701
1702 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1703
Andi Kleen41c8ca22016-05-24 12:52:38 -07001704 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001705 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001706 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001707 fprintf(output, "# time socket cpus");
1708 if (!metric_only)
1709 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001710 break;
1711 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001712 fprintf(output, "# time core cpus");
1713 if (!metric_only)
1714 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001715 break;
1716 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001717 fprintf(output, "# time CPU");
1718 if (!metric_only)
1719 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001720 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001721 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001722 fprintf(output, "# time comm-pid");
1723 if (!metric_only)
1724 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001725 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001726 case AGGR_GLOBAL:
1727 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001728 fprintf(output, "# time");
1729 if (!metric_only)
1730 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001731 case AGGR_UNSET:
1732 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001733 }
1734 }
1735
Andi Kleen41c8ca22016-05-24 12:52:38 -07001736 if (num_print_interval == 0 && metric_only)
1737 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001738 if (++num_print_interval == 25)
1739 num_print_interval = 0;
1740}
1741
1742static void print_header(int argc, const char **argv)
1743{
Jiri Olsa58215222015-07-21 14:31:24 +02001744 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001745 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001746
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001747 fflush(stdout);
1748
Stephane Eraniand7470b62010-12-01 18:49:05 +02001749 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001750 fprintf(output, "\n");
1751 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001752 if (target.system_wide)
1753 fprintf(output, "\'system wide");
1754 else if (target.cpu_list)
1755 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001756 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001757 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1758 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001759 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001760 } else if (target.pid)
1761 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001762 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001763 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001764
Stephane Eranian4aa90152011-08-15 22:22:33 +02001765 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001766 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001767 fprintf(output, " (%d runs)", run_count);
1768 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001769 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001770}
1771
Jiri Olsabc22de92018-04-23 11:08:20 +02001772static int get_precision(double num)
1773{
1774 if (num > 1)
1775 return 0;
1776
1777 return lround(ceil(-log10(num)));
1778}
1779
Jiri Olsae55c14a2018-04-23 11:08:21 +02001780static void print_table(FILE *output, int precision, double avg)
1781{
1782 char tmp[64];
1783 int idx, indent = 0;
1784
1785 scnprintf(tmp, 64, " %17.*f", precision, avg);
1786 while (tmp[indent] == ' ')
1787 indent++;
1788
1789 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1790
1791 for (idx = 0; idx < run_count; idx++) {
1792 double run = (double) walltime_run[idx] / NSEC_PER_SEC;
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001793 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001794
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001795 fprintf(output, " %17.*f (%+.*f) ",
Jiri Olsae55c14a2018-04-23 11:08:21 +02001796 precision, run, precision, run - avg);
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001797
1798 for (h = 0; h < n; h++)
1799 fprintf(output, "#");
1800
1801 fprintf(output, "\n");
Jiri Olsae55c14a2018-04-23 11:08:21 +02001802 }
1803
1804 fprintf(output, "\n%*s# Final result:\n", indent, "");
1805}
1806
Jiri Olsad4f63a42015-06-26 11:29:26 +02001807static void print_footer(void)
1808{
Jiri Olsabc22de92018-04-23 11:08:20 +02001809 double avg = avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsa58215222015-07-21 14:31:24 +02001810 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001811 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001812
Jiri Olsad4f63a42015-06-26 11:29:26 +02001813 if (!null_run)
1814 fprintf(output, "\n");
Jiri Olsabc22de92018-04-23 11:08:20 +02001815
1816 if (run_count == 1) {
1817 fprintf(output, " %17.9f seconds time elapsed", avg);
1818 } else {
1819 double sd = stddev_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
1820 /*
1821 * Display at most 2 more significant
1822 * digits than the stddev inaccuracy.
1823 */
1824 int precision = get_precision(sd) + 2;
1825
Jiri Olsae55c14a2018-04-23 11:08:21 +02001826 if (walltime_run_table)
1827 print_table(output, precision, avg);
1828
Jiri Olsabc22de92018-04-23 11:08:20 +02001829 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1830 precision, avg, precision, sd);
1831
1832 print_noise_pct(sd, avg);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001833 }
1834 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001835
Andi Kleen918c7b062017-05-22 18:00:16 -07001836 if (print_free_counters_hint &&
1837 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1838 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001839 fprintf(output,
1840"Some events weren't counted. Try disabling the NMI watchdog:\n"
1841" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1842" perf stat ...\n"
1843" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001844
1845 if (print_mixed_hw_group_error)
1846 fprintf(output,
1847 "The events in group usually have to be from "
1848 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001849}
1850
1851static void print_counters(struct timespec *ts, int argc, const char **argv)
1852{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001853 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001854 struct perf_evsel *counter;
1855 char buf[64], *prefix = NULL;
1856
Jiri Olsa664c98d2015-11-05 15:40:50 +01001857 /* Do not print anything if we record to the pipe. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001858 if (STAT_RECORD && perf_stat.data.is_pipe)
Jiri Olsa664c98d2015-11-05 15:40:50 +01001859 return;
1860
Jiri Olsad4f63a42015-06-26 11:29:26 +02001861 if (interval)
1862 print_interval(prefix = buf, ts);
1863 else
1864 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001865
Andi Kleen54b50912016-03-03 15:57:36 -08001866 if (metric_only) {
1867 static int num_print_iv;
1868
Andi Kleen41c8ca22016-05-24 12:52:38 -07001869 if (num_print_iv == 0 && !interval)
1870 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001871 if (num_print_iv++ == 25)
1872 num_print_iv = 0;
1873 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1874 fprintf(stat_config.output, "%s", prefix);
1875 }
1876
Jiri Olsa421a50f2015-07-21 14:31:22 +02001877 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001878 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001879 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001880 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001881 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001882 case AGGR_THREAD:
Andi Kleene864c5c2017-08-31 12:40:35 -07001883 evlist__for_each_entry(evsel_list, counter) {
1884 if (is_duration_time(counter))
1885 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001886 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001887 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001888 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001889 case AGGR_GLOBAL:
Andi Kleene864c5c2017-08-31 12:40:35 -07001890 evlist__for_each_entry(evsel_list, counter) {
1891 if (is_duration_time(counter))
1892 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001893 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001894 }
Andi Kleen54b50912016-03-03 15:57:36 -08001895 if (metric_only)
1896 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001897 break;
1898 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001899 if (metric_only)
1900 print_no_aggr_metric(prefix);
1901 else {
Andi Kleene864c5c2017-08-31 12:40:35 -07001902 evlist__for_each_entry(evsel_list, counter) {
1903 if (is_duration_time(counter))
1904 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001905 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001906 }
Andi Kleen206cab62016-03-03 15:57:37 -08001907 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001908 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001909 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001910 default:
1911 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001912 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001913
Jiri Olsad4f63a42015-06-26 11:29:26 +02001914 if (!interval && !csv_output)
1915 print_footer();
1916
Jiri Olsa58215222015-07-21 14:31:24 +02001917 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001918}
1919
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001920static volatile int signr = -1;
1921
Ingo Molnar52425192009-05-26 09:17:18 +02001922static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001923{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001924 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001925 done = 1;
1926
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001927 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001928 /*
1929 * render child_pid harmless
1930 * won't send SIGTERM to a random
1931 * process in case of race condition
1932 * and fast PID recycling
1933 */
1934 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001935}
1936
1937static void sig_atexit(void)
1938{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001939 sigset_t set, oset;
1940
1941 /*
1942 * avoid race condition with SIGCHLD handler
1943 * in skip_signal() which is modifying child_pid
1944 * goal is to avoid send SIGTERM to a random
1945 * process
1946 */
1947 sigemptyset(&set);
1948 sigaddset(&set, SIGCHLD);
1949 sigprocmask(SIG_BLOCK, &set, &oset);
1950
Chris Wilson933da832009-10-04 01:35:01 +01001951 if (child_pid != -1)
1952 kill(child_pid, SIGTERM);
1953
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001954 sigprocmask(SIG_SETMASK, &oset, NULL);
1955
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001956 if (signr == -1)
1957 return;
1958
1959 signal(signr, SIG_DFL);
1960 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001961}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001962
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001963static int stat__set_big_num(const struct option *opt __maybe_unused,
1964 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001965{
1966 big_num_opt = unset ? 0 : 1;
1967 return 0;
1968}
1969
Andi Kleen44b1e602016-05-30 12:49:42 -03001970static int enable_metric_only(const struct option *opt __maybe_unused,
1971 const char *s __maybe_unused, int unset)
1972{
1973 force_metric_only = true;
1974 metric_only = !unset;
1975 return 0;
1976}
1977
Andi Kleenb18f3e32017-08-31 12:40:31 -07001978static int parse_metric_groups(const struct option *opt,
1979 const char *str,
1980 int unset __maybe_unused)
1981{
1982 return metricgroup__parse_groups(opt, str, &metric_events);
1983}
1984
Jiri Olsae0547312015-11-05 15:40:45 +01001985static const struct option stat_options[] = {
1986 OPT_BOOLEAN('T', "transaction", &transaction_run,
1987 "hardware transaction statistics"),
1988 OPT_CALLBACK('e', "event", &evsel_list, "event",
1989 "event selector. use 'perf list' to list available events",
1990 parse_events_option),
1991 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1992 "event filter", parse_filter),
1993 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1994 "child tasks do not inherit counters"),
1995 OPT_STRING('p', "pid", &target.pid, "pid",
1996 "stat events on existing process id"),
1997 OPT_STRING('t', "tid", &target.tid, "tid",
1998 "stat events on existing thread id"),
1999 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
2000 "system-wide collection from all CPUs"),
2001 OPT_BOOLEAN('g', "group", &group,
2002 "put the counters into a counter group"),
2003 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
2004 OPT_INCR('v', "verbose", &verbose,
2005 "be more verbose (show counter open errors, etc)"),
2006 OPT_INTEGER('r', "repeat", &run_count,
2007 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsae55c14a2018-04-23 11:08:21 +02002008 OPT_BOOLEAN(0, "table", &walltime_run_table,
2009 "display details about each run (only with -r option)"),
Jiri Olsae0547312015-11-05 15:40:45 +01002010 OPT_BOOLEAN('n', "null", &null_run,
2011 "null run - dont start any counters"),
2012 OPT_INCR('d', "detailed", &detailed_run,
2013 "detailed run - start a lot of events"),
2014 OPT_BOOLEAN('S', "sync", &sync_run,
2015 "call sync() before starting a run"),
2016 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
2017 "print large numbers with thousands\' separators",
2018 stat__set_big_num),
2019 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
2020 "list of cpus to monitor in system-wide"),
2021 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
2022 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07002023 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01002024 OPT_STRING('x', "field-separator", &csv_sep, "separator",
2025 "print counts with custom separator"),
2026 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
2027 "monitor event in cgroup name only", parse_cgroups),
2028 OPT_STRING('o', "output", &output_name, "file", "output file name"),
2029 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
2030 OPT_INTEGER(0, "log-fd", &output_fd,
2031 "log output to fd, instead of stderr"),
2032 OPT_STRING(0, "pre", &pre_cmd, "command",
2033 "command to run prior to the measured command"),
2034 OPT_STRING(0, "post", &post_cmd, "command",
2035 "command to run after to the measured command"),
2036 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03002037 "print counts at regular interval in ms "
2038 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01002039 OPT_INTEGER(0, "interval-count", &stat_config.times,
2040 "print counts for fixed number of times"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002041 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
2042 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01002043 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
2044 "aggregate counts per processor socket", AGGR_SOCKET),
2045 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
2046 "aggregate counts per physical processor core", AGGR_CORE),
2047 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
2048 "aggregate counts per thread", AGGR_THREAD),
2049 OPT_UINTEGER('D', "delay", &initial_delay,
2050 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03002051 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
2052 "Only print computed metrics. No raw values", enable_metric_only),
2053 OPT_BOOLEAN(0, "topdown", &topdown_run,
2054 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07002055 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
2056 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07002057 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
2058 "monitor specified metrics or metric groups (separated by ,)",
2059 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01002060 OPT_END()
2061};
2062
Jiri Olsa1fe7a302015-10-16 12:41:15 +02002063static int perf_stat__get_socket(struct cpu_map *map, int cpu)
2064{
2065 return cpu_map__get_socket(map, cpu, NULL);
2066}
2067
2068static int perf_stat__get_core(struct cpu_map *map, int cpu)
2069{
2070 return cpu_map__get_core(map, cpu, NULL);
2071}
2072
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002073static int cpu_map__get_max(struct cpu_map *map)
2074{
2075 int i, max = -1;
2076
2077 for (i = 0; i < map->nr; i++) {
2078 if (map->map[i] > max)
2079 max = map->map[i];
2080 }
2081
2082 return max;
2083}
2084
2085static struct cpu_map *cpus_aggr_map;
2086
2087static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
2088{
2089 int cpu;
2090
2091 if (idx >= map->nr)
2092 return -1;
2093
2094 cpu = map->map[idx];
2095
2096 if (cpus_aggr_map->map[cpu] == -1)
2097 cpus_aggr_map->map[cpu] = get_id(map, idx);
2098
2099 return cpus_aggr_map->map[cpu];
2100}
2101
2102static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
2103{
2104 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
2105}
2106
2107static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
2108{
2109 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
2110}
2111
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002112static int perf_stat_init_aggr_mode(void)
2113{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002114 int nr;
2115
Jiri Olsa421a50f2015-07-21 14:31:22 +02002116 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002117 case AGGR_SOCKET:
2118 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
2119 perror("cannot build socket map");
2120 return -1;
2121 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002122 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002123 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002124 case AGGR_CORE:
2125 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
2126 perror("cannot build core map");
2127 return -1;
2128 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002129 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002130 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002131 case AGGR_NONE:
2132 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002133 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002134 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002135 default:
2136 break;
2137 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002138
2139 /*
2140 * The evsel_list->cpus is the base we operate on,
2141 * taking the highest cpu number to be the size of
2142 * the aggregation translate cpumap.
2143 */
2144 nr = cpu_map__get_max(evsel_list->cpus);
2145 cpus_aggr_map = cpu_map__empty_new(nr + 1);
2146 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002147}
2148
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002149static void perf_stat__exit_aggr_mode(void)
2150{
2151 cpu_map__put(aggr_map);
2152 cpu_map__put(cpus_aggr_map);
2153 aggr_map = NULL;
2154 cpus_aggr_map = NULL;
2155}
2156
Jiri Olsa68d702f2015-11-05 15:40:58 +01002157static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2158{
2159 int cpu;
2160
2161 if (idx > map->nr)
2162 return -1;
2163
2164 cpu = map->map[idx];
2165
Jan Stancekda8a58b2017-02-17 12:10:26 +01002166 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002167 return -1;
2168
2169 return cpu;
2170}
2171
2172static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2173{
2174 struct perf_env *env = data;
2175 int cpu = perf_env__get_cpu(env, map, idx);
2176
2177 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2178}
2179
2180static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2181{
2182 struct perf_env *env = data;
2183 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2184
2185 if (cpu != -1) {
2186 int socket_id = env->cpu[cpu].socket_id;
2187
2188 /*
2189 * Encode socket in upper 16 bits
2190 * core_id is relative to socket, and
2191 * we need a global id. So we combine
2192 * socket + core id.
2193 */
2194 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2195 }
2196
2197 return core;
2198}
2199
2200static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2201 struct cpu_map **sockp)
2202{
2203 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2204}
2205
2206static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2207 struct cpu_map **corep)
2208{
2209 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2210}
2211
2212static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2213{
2214 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2215}
2216
2217static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2218{
2219 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2220}
2221
2222static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2223{
2224 struct perf_env *env = &st->session->header.env;
2225
2226 switch (stat_config.aggr_mode) {
2227 case AGGR_SOCKET:
2228 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2229 perror("cannot build socket map");
2230 return -1;
2231 }
2232 aggr_get_id = perf_stat__get_socket_file;
2233 break;
2234 case AGGR_CORE:
2235 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2236 perror("cannot build core map");
2237 return -1;
2238 }
2239 aggr_get_id = perf_stat__get_core_file;
2240 break;
2241 case AGGR_NONE:
2242 case AGGR_GLOBAL:
2243 case AGGR_THREAD:
2244 case AGGR_UNSET:
2245 default:
2246 break;
2247 }
2248
2249 return 0;
2250}
2251
Andi Kleen44b1e602016-05-30 12:49:42 -03002252static int topdown_filter_events(const char **attr, char **str, bool use_group)
2253{
2254 int off = 0;
2255 int i;
2256 int len = 0;
2257 char *s;
2258
2259 for (i = 0; attr[i]; i++) {
2260 if (pmu_have_event("cpu", attr[i])) {
2261 len += strlen(attr[i]) + 1;
2262 attr[i - off] = attr[i];
2263 } else
2264 off++;
2265 }
2266 attr[i - off] = NULL;
2267
2268 *str = malloc(len + 1 + 2);
2269 if (!*str)
2270 return -1;
2271 s = *str;
2272 if (i - off == 0) {
2273 *s = 0;
2274 return 0;
2275 }
2276 if (use_group)
2277 *s++ = '{';
2278 for (i = 0; attr[i]; i++) {
2279 strcpy(s, attr[i]);
2280 s += strlen(s);
2281 *s++ = ',';
2282 }
2283 if (use_group) {
2284 s[-1] = '}';
2285 *s = 0;
2286 } else
2287 s[-1] = 0;
2288 return 0;
2289}
2290
2291__weak bool arch_topdown_check_group(bool *warn)
2292{
2293 *warn = false;
2294 return false;
2295}
2296
2297__weak void arch_topdown_group_warn(void)
2298{
2299}
2300
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002301/*
2302 * Add default attributes, if there were no attributes specified or
2303 * if -d/--detailed, -d -d or -d -d -d is used:
2304 */
2305static int add_default_attributes(void)
2306{
Andi Kleen44b1e602016-05-30 12:49:42 -03002307 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002308 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002309
2310 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2311 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2312 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2313 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2314
2315 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002316};
2317 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002318 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002319};
2320 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002321 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002322};
2323 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002324 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2325 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2326 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2327
2328};
2329
2330/*
2331 * Detailed stats (-d), covering the L1 and last level data caches:
2332 */
2333 struct perf_event_attr detailed_attrs[] = {
2334
2335 { .type = PERF_TYPE_HW_CACHE,
2336 .config =
2337 PERF_COUNT_HW_CACHE_L1D << 0 |
2338 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2339 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2340
2341 { .type = PERF_TYPE_HW_CACHE,
2342 .config =
2343 PERF_COUNT_HW_CACHE_L1D << 0 |
2344 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2345 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2346
2347 { .type = PERF_TYPE_HW_CACHE,
2348 .config =
2349 PERF_COUNT_HW_CACHE_LL << 0 |
2350 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2351 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2352
2353 { .type = PERF_TYPE_HW_CACHE,
2354 .config =
2355 PERF_COUNT_HW_CACHE_LL << 0 |
2356 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2357 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2358};
2359
2360/*
2361 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2362 */
2363 struct perf_event_attr very_detailed_attrs[] = {
2364
2365 { .type = PERF_TYPE_HW_CACHE,
2366 .config =
2367 PERF_COUNT_HW_CACHE_L1I << 0 |
2368 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2369 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2370
2371 { .type = PERF_TYPE_HW_CACHE,
2372 .config =
2373 PERF_COUNT_HW_CACHE_L1I << 0 |
2374 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2375 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2376
2377 { .type = PERF_TYPE_HW_CACHE,
2378 .config =
2379 PERF_COUNT_HW_CACHE_DTLB << 0 |
2380 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2381 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2382
2383 { .type = PERF_TYPE_HW_CACHE,
2384 .config =
2385 PERF_COUNT_HW_CACHE_DTLB << 0 |
2386 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2387 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2388
2389 { .type = PERF_TYPE_HW_CACHE,
2390 .config =
2391 PERF_COUNT_HW_CACHE_ITLB << 0 |
2392 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2393 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2394
2395 { .type = PERF_TYPE_HW_CACHE,
2396 .config =
2397 PERF_COUNT_HW_CACHE_ITLB << 0 |
2398 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2399 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2400
2401};
2402
2403/*
2404 * Very, very detailed stats (-d -d -d), adding prefetch events:
2405 */
2406 struct perf_event_attr very_very_detailed_attrs[] = {
2407
2408 { .type = PERF_TYPE_HW_CACHE,
2409 .config =
2410 PERF_COUNT_HW_CACHE_L1D << 0 |
2411 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2412 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2413
2414 { .type = PERF_TYPE_HW_CACHE,
2415 .config =
2416 PERF_COUNT_HW_CACHE_L1D << 0 |
2417 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2418 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2419};
2420
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002421 /* Set attrs if no event is selected and !null_run: */
2422 if (null_run)
2423 return 0;
2424
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002425 if (transaction_run) {
Thomas Richterfca323402018-03-08 15:57:35 +01002426 struct parse_events_error errinfo;
2427
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002428 if (pmu_have_event("cpu", "cycles-ct") &&
2429 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002430 err = parse_events(evsel_list, transaction_attrs,
2431 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002432 else
Thomas Richterfca323402018-03-08 15:57:35 +01002433 err = parse_events(evsel_list,
2434 transaction_limited_attrs,
2435 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002436 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002437 fprintf(stderr, "Cannot set up transaction events\n");
2438 return -1;
2439 }
2440 return 0;
2441 }
2442
Kan Liangdaefd0b2017-05-26 12:05:38 -07002443 if (smi_cost) {
2444 int smi;
2445
2446 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2447 fprintf(stderr, "freeze_on_smi is not supported.\n");
2448 return -1;
2449 }
2450
2451 if (!smi) {
2452 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2453 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2454 return -1;
2455 }
2456 smi_reset = true;
2457 }
2458
2459 if (pmu_have_event("msr", "aperf") &&
2460 pmu_have_event("msr", "smi")) {
2461 if (!force_metric_only)
2462 metric_only = true;
2463 err = parse_events(evsel_list, smi_cost_attrs, NULL);
2464 } else {
2465 fprintf(stderr, "To measure SMI cost, it needs "
2466 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2467 return -1;
2468 }
2469 if (err) {
2470 fprintf(stderr, "Cannot set up SMI cost events\n");
2471 return -1;
2472 }
2473 return 0;
2474 }
2475
Andi Kleen44b1e602016-05-30 12:49:42 -03002476 if (topdown_run) {
2477 char *str = NULL;
2478 bool warn = false;
2479
2480 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2481 stat_config.aggr_mode != AGGR_CORE) {
2482 pr_err("top down event configuration requires --per-core mode\n");
2483 return -1;
2484 }
2485 stat_config.aggr_mode = AGGR_CORE;
2486 if (nr_cgroups || !target__has_cpu(&target)) {
2487 pr_err("top down event configuration requires system-wide mode (-a)\n");
2488 return -1;
2489 }
2490
2491 if (!force_metric_only)
2492 metric_only = true;
2493 if (topdown_filter_events(topdown_attrs, &str,
2494 arch_topdown_check_group(&warn)) < 0) {
2495 pr_err("Out of memory\n");
2496 return -1;
2497 }
2498 if (topdown_attrs[0] && str) {
2499 if (warn)
2500 arch_topdown_group_warn();
2501 err = parse_events(evsel_list, str, NULL);
2502 if (err) {
2503 fprintf(stderr,
2504 "Cannot set up top down events %s: %d\n",
2505 str, err);
2506 free(str);
2507 return -1;
2508 }
2509 } else {
2510 fprintf(stderr, "System does not support topdown\n");
2511 return -1;
2512 }
2513 free(str);
2514 }
2515
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002516 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002517 if (target__has_cpu(&target))
2518 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2519
Andi Kleen9dec4472016-02-26 16:27:56 -08002520 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2521 return -1;
2522 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2523 if (perf_evlist__add_default_attrs(evsel_list,
2524 frontend_attrs) < 0)
2525 return -1;
2526 }
2527 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2528 if (perf_evlist__add_default_attrs(evsel_list,
2529 backend_attrs) < 0)
2530 return -1;
2531 }
2532 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002533 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002534 }
2535
2536 /* Detailed events get appended to the event list: */
2537
2538 if (detailed_run < 1)
2539 return 0;
2540
2541 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002542 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002543 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002544
2545 if (detailed_run < 2)
2546 return 0;
2547
2548 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002549 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002550 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002551
2552 if (detailed_run < 3)
2553 return 0;
2554
2555 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002556 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002557}
2558
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002559static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002560 "perf stat record [<options>]",
2561 NULL,
2562};
2563
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002564static void init_features(struct perf_session *session)
2565{
2566 int feat;
2567
2568 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2569 perf_header__set_feat(&session->header, feat);
2570
2571 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2572 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2573 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2574 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2575}
2576
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002577static int __cmd_record(int argc, const char **argv)
2578{
2579 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002580 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002581
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002582 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002583 PARSE_OPT_STOP_AT_NON_OPTION);
2584
2585 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002586 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002587
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002588 if (run_count != 1 || forever) {
2589 pr_err("Cannot use -r option with perf stat record.\n");
2590 return -1;
2591 }
2592
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002593 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002594 if (session == NULL) {
2595 pr_err("Perf session creation failed.\n");
2596 return -1;
2597 }
2598
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002599 init_features(session);
2600
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002601 session->evlist = evsel_list;
2602 perf_stat.session = session;
2603 perf_stat.record = true;
2604 return argc;
2605}
2606
Jiri Olsaa56f9392015-11-05 15:40:59 +01002607static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2608 union perf_event *event,
2609 struct perf_session *session)
2610{
Andi Kleene3b03b62016-05-05 16:04:03 -07002611 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002612 struct perf_evsel *counter;
2613 struct timespec tsh, *ts = NULL;
2614 const char **argv = session->header.env.cmdline_argv;
2615 int argc = session->header.env.nr_cmdline;
2616
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002617 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002618 perf_stat_process_counter(&stat_config, counter);
2619
Andi Kleene3b03b62016-05-05 16:04:03 -07002620 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2621 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002622
Andi Kleene3b03b62016-05-05 16:04:03 -07002623 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002624 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2625 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002626 ts = &tsh;
2627 }
2628
2629 print_counters(ts, argc, argv);
2630 return 0;
2631}
2632
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002633static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002634int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002635 union perf_event *event,
2636 struct perf_session *session __maybe_unused)
2637{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002638 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2639
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002640 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002641
Jiri Olsa89af4e02015-11-05 15:41:02 +01002642 if (cpu_map__empty(st->cpus)) {
2643 if (st->aggr_mode != AGGR_UNSET)
2644 pr_warning("warning: processing task data, aggregation mode not set\n");
2645 return 0;
2646 }
2647
2648 if (st->aggr_mode != AGGR_UNSET)
2649 stat_config.aggr_mode = st->aggr_mode;
2650
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002651 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002652 perf_stat_init_aggr_mode();
2653 else
2654 perf_stat_init_aggr_mode_file(st);
2655
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002656 return 0;
2657}
2658
Jiri Olsa1975d362015-11-05 15:40:56 +01002659static int set_maps(struct perf_stat *st)
2660{
2661 if (!st->cpus || !st->threads)
2662 return 0;
2663
2664 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2665 return -EINVAL;
2666
2667 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2668
2669 if (perf_evlist__alloc_stats(evsel_list, true))
2670 return -ENOMEM;
2671
2672 st->maps_allocated = true;
2673 return 0;
2674}
2675
2676static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002677int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002678 union perf_event *event,
2679 struct perf_session *session __maybe_unused)
2680{
2681 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2682
2683 if (st->threads) {
2684 pr_warning("Extra thread map event, ignoring.\n");
2685 return 0;
2686 }
2687
2688 st->threads = thread_map__new_event(&event->thread_map);
2689 if (!st->threads)
2690 return -ENOMEM;
2691
2692 return set_maps(st);
2693}
2694
2695static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002696int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002697 union perf_event *event,
2698 struct perf_session *session __maybe_unused)
2699{
2700 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2701 struct cpu_map *cpus;
2702
2703 if (st->cpus) {
2704 pr_warning("Extra cpu map event, ignoring.\n");
2705 return 0;
2706 }
2707
2708 cpus = cpu_map__new_data(&event->cpu_map.data);
2709 if (!cpus)
2710 return -ENOMEM;
2711
2712 st->cpus = cpus;
2713 return set_maps(st);
2714}
2715
Jin Yao56739442017-12-05 22:03:07 +08002716static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2717{
2718 int i;
2719
2720 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2721 if (!config->stats)
2722 return -1;
2723
2724 config->stats_num = nthreads;
2725
2726 for (i = 0; i < nthreads; i++)
2727 runtime_stat__init(&config->stats[i]);
2728
2729 return 0;
2730}
2731
2732static void runtime_stat_delete(struct perf_stat_config *config)
2733{
2734 int i;
2735
2736 if (!config->stats)
2737 return;
2738
2739 for (i = 0; i < config->stats_num; i++)
2740 runtime_stat__exit(&config->stats[i]);
2741
2742 free(config->stats);
2743}
2744
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002745static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002746 "perf stat report [<options>]",
2747 NULL,
2748};
2749
2750static struct perf_stat perf_stat = {
2751 .tool = {
2752 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002753 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002754 .thread_map = process_thread_map_event,
2755 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002756 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002757 .stat = perf_event__process_stat_event,
2758 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002759 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002760 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002761};
2762
2763static int __cmd_report(int argc, const char **argv)
2764{
2765 struct perf_session *session;
2766 const struct option options[] = {
2767 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002768 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2769 "aggregate counts per processor socket", AGGR_SOCKET),
2770 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2771 "aggregate counts per physical processor core", AGGR_CORE),
2772 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2773 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002774 OPT_END()
2775 };
2776 struct stat st;
2777 int ret;
2778
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002779 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002780
2781 if (!input_name || !strlen(input_name)) {
2782 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2783 input_name = "-";
2784 else
2785 input_name = "perf.data";
2786 }
2787
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002788 perf_stat.data.file.path = input_name;
2789 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002790
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002791 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002792 if (session == NULL)
2793 return -1;
2794
2795 perf_stat.session = session;
2796 stat_config.output = stderr;
2797 evsel_list = session->evlist;
2798
2799 ret = perf_session__process_events(session);
2800 if (ret)
2801 return ret;
2802
2803 perf_session__delete(session);
2804 return 0;
2805}
2806
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002807static void setup_system_wide(int forks)
2808{
2809 /*
2810 * Make system wide (-a) the default target if
2811 * no target was specified and one of following
2812 * conditions is met:
2813 *
2814 * - there's no workload specified
2815 * - there is workload specified but all requested
2816 * events are system wide events
2817 */
2818 if (!target__none(&target))
2819 return;
2820
2821 if (!forks)
2822 target.system_wide = true;
2823 else {
2824 struct perf_evsel *counter;
2825
2826 evlist__for_each_entry(evsel_list, counter) {
2827 if (!counter->system_wide)
2828 return;
2829 }
2830
2831 if (evsel_list->nr_entries)
2832 target.system_wide = true;
2833 }
2834}
2835
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002836int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002837{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002838 const char * const stat_usage[] = {
2839 "perf stat [<options>] [<command>]",
2840 NULL
2841 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002842 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002843 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002844 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002845 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002846 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002847
Stephane Eranian5af52b52010-05-18 15:00:01 +02002848 setlocale(LC_ALL, "");
2849
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002850 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002851 if (evsel_list == NULL)
2852 return -ENOMEM;
2853
Wang Nan1669e502016-02-19 11:43:58 +00002854 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002855 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2856 (const char **) stat_usage,
2857 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002858 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002859 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002860
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002861 if (csv_sep) {
2862 csv_output = true;
2863 if (!strcmp(csv_sep, "\\t"))
2864 csv_sep = "\t";
2865 } else
2866 csv_sep = DEFAULT_SEPARATOR;
2867
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002868 if (argc && !strncmp(argv[0], "rec", 3)) {
2869 argc = __cmd_record(argc, argv);
2870 if (argc < 0)
2871 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002872 } else if (argc && !strncmp(argv[0], "rep", 3))
2873 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002874
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002875 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002876 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002877
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002878 /*
2879 * For record command the -o is already taken care of.
2880 */
2881 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002882 output = NULL;
2883
Jim Cromie56f3bae2011-09-07 17:14:00 -06002884 if (output_name && output_fd) {
2885 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002886 parse_options_usage(stat_usage, stat_options, "o", 1);
2887 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002888 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002889 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002890
Andi Kleen54b50912016-03-03 15:57:36 -08002891 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2892 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2893 goto out;
2894 }
2895
Andi Kleen54b50912016-03-03 15:57:36 -08002896 if (metric_only && run_count > 1) {
2897 fprintf(stderr, "--metric-only is not supported with -r\n");
2898 goto out;
2899 }
2900
Jiri Olsae55c14a2018-04-23 11:08:21 +02002901 if (walltime_run_table && run_count <= 1) {
2902 fprintf(stderr, "--table is only supported with -r\n");
2903 parse_options_usage(stat_usage, stat_options, "r", 1);
2904 parse_options_usage(NULL, stat_options, "table", 0);
2905 goto out;
2906 }
2907
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002908 if (output_fd < 0) {
2909 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002910 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002911 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002912 }
2913
Stephane Eranian4aa90152011-08-15 22:22:33 +02002914 if (!output) {
2915 struct timespec tm;
2916 mode = append_file ? "a" : "w";
2917
2918 output = fopen(output_name, mode);
2919 if (!output) {
2920 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002921 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002922 }
2923 clock_gettime(CLOCK_REALTIME, &tm);
2924 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002925 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002926 mode = append_file ? "a" : "w";
2927 output = fdopen(output_fd, mode);
2928 if (!output) {
2929 perror("Failed opening logfd");
2930 return -errno;
2931 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002932 }
2933
Jiri Olsa58215222015-07-21 14:31:24 +02002934 stat_config.output = output;
2935
Stephane Eraniand7470b62010-12-01 18:49:05 +02002936 /*
2937 * let the spreadsheet do the pretty-printing
2938 */
2939 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002940 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002941 if (big_num_opt == 1) {
2942 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002943 parse_options_usage(stat_usage, stat_options, "B", 1);
2944 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002945 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002946 } else /* Nope, so disable big number formatting */
2947 big_num = false;
2948 } else if (big_num_opt == 0) /* User passed --no-big-num */
2949 big_num = false;
2950
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002951 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002952
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002953 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002954 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002955 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002956 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002957 } else if (run_count == 0) {
2958 forever = true;
2959 run_count = 1;
2960 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002961
Jiri Olsae55c14a2018-04-23 11:08:21 +02002962 if (walltime_run_table) {
2963 walltime_run = zalloc(run_count * sizeof(walltime_run[0]));
2964 if (!walltime_run) {
2965 pr_err("failed to setup -r option");
2966 goto out;
2967 }
2968 }
2969
Jin Yao1d9f8d12017-12-05 22:03:10 +08002970 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2971 !target__has_task(&target)) {
2972 if (!target.system_wide || target.cpu_list) {
2973 fprintf(stderr, "The --per-thread option is only "
2974 "available when monitoring via -p -t -a "
2975 "options or only --per-thread.\n");
2976 parse_options_usage(NULL, stat_options, "p", 1);
2977 parse_options_usage(NULL, stat_options, "t", 1);
2978 goto out;
2979 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002980 }
2981
2982 /*
2983 * no_aggr, cgroup are for system-wide only
2984 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2985 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002986 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2987 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002988 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002989 fprintf(stderr, "both cgroup and no-aggregation "
2990 "modes only available in system-wide mode\n");
2991
Jiri Olsae0547312015-11-05 15:40:45 +01002992 parse_options_usage(stat_usage, stat_options, "G", 1);
2993 parse_options_usage(NULL, stat_options, "A", 1);
2994 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002995 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002996 }
2997
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002998 if (add_default_attributes())
2999 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003000
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003001 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02003002
Jin Yao1d9f8d12017-12-05 22:03:10 +08003003 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
3004 target.per_thread = true;
3005
Namhyung Kim77a6f012012-05-07 14:09:04 +09003006 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003007 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09003008 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01003009 parse_options_usage(stat_usage, stat_options, "p", 1);
3010 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003011 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09003012 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01003013 parse_options_usage(stat_usage, stat_options, "C", 1);
3014 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09003015 }
3016 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02003017 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02003018
3019 /*
3020 * Initialize thread_map with comm names,
3021 * so we could print it out on output.
3022 */
Jin Yao56739442017-12-05 22:03:07 +08003023 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02003024 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08003025 if (target.system_wide) {
3026 if (runtime_stat_new(&stat_config,
3027 thread_map__nr(evsel_list->threads))) {
3028 goto out;
3029 }
3030 }
3031 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02003032
yuzhoujiandb06a262018-01-29 10:25:22 +01003033 if (stat_config.times && interval)
3034 interval_count = true;
3035 else if (stat_config.times && !interval) {
3036 pr_err("interval-count option should be used together with "
3037 "interval-print.\n");
3038 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
3039 parse_options_usage(stat_usage, stat_options, "I", 1);
3040 goto out;
3041 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02003042
yuzhoujianf1f8ad52018-01-29 10:25:23 +01003043 if (timeout && timeout < 100) {
3044 if (timeout < 10) {
3045 pr_err("timeout must be >= 10ms.\n");
3046 parse_options_usage(stat_usage, stat_options, "timeout", 0);
3047 goto out;
3048 } else
3049 pr_warning("timeout < 100ms. "
3050 "The overhead percentage could be high in some cases. "
3051 "Please proceed with caution.\n");
3052 }
3053 if (timeout && interval) {
3054 pr_err("timeout option is not supported with interval-print.\n");
3055 parse_options_usage(stat_usage, stat_options, "timeout", 0);
3056 parse_options_usage(stat_usage, stat_options, "I", 1);
3057 goto out;
3058 }
3059
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003060 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003061 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03003062
Stephane Eranian86ee6e12013-02-14 13:57:27 +01003063 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003064 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01003065
Ingo Molnar58d7e992009-05-15 11:03:23 +02003066 /*
3067 * We dont want to block the signals - that would cause
3068 * child tasks to inherit that and Ctrl-C would not work.
3069 * What we want is for Ctrl-C to work in the exec()-ed
3070 * task, but being ignored by perf stat itself:
3071 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02003072 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003073 if (!forever)
3074 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01003075 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02003076 signal(SIGALRM, skip_signal);
3077 signal(SIGABRT, skip_signal);
3078
Ingo Molnar42202dd2009-06-13 14:57:28 +02003079 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003080 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09003081 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02003082 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
3083 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02003084
Jiri Olsae55c14a2018-04-23 11:08:21 +02003085 status = run_perf_stat(argc, argv, run_idx);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003086 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02003087 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02003088 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003089 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02003090 }
3091
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003092 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02003093 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003094
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003095 if (STAT_RECORD) {
3096 /*
3097 * We synthesize the kernel mmap record just so that older tools
3098 * don't emit warnings about not being able to resolve symbols
3099 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3100 * a saner message about no samples being in the perf.data file.
3101 *
3102 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01003103 * in header.c at the moment 'perf stat record' gets introduced, which
3104 * is not really needed once we start adding the stat specific PERF_RECORD_
3105 * records, but the need to suppress the kptr_restrict messages in older
3106 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003107 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003108 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003109 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3110 process_synthesized_event,
3111 &perf_stat.session->machines.host);
3112 if (err) {
3113 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3114 "older tools may produce warnings about this file\n.");
3115 }
3116
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003117 if (!interval) {
3118 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3119 pr_err("failed to write stat round event\n");
3120 }
3121
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003122 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003123 perf_stat.session->header.data_size += perf_stat.bytes_written;
3124 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3125 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003126
3127 perf_session__delete(perf_stat.session);
3128 }
3129
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003130 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003131 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003132out:
Jiri Olsae55c14a2018-04-23 11:08:21 +02003133 free(walltime_run);
3134
Kan Liangdaefd0b2017-05-26 12:05:38 -07003135 if (smi_cost && smi_reset)
3136 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3137
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003138 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003139
3140 runtime_stat_delete(&stat_config);
3141
Ingo Molnar42202dd2009-06-13 14:57:28 +02003142 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003143}