blob: 72553937c010d855abc47daf1fe60cafced7d460 [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;
1793
1794 fprintf(output, " %17.*f (%+.*f)\n",
1795 precision, run, precision, run - avg);
1796 }
1797
1798 fprintf(output, "\n%*s# Final result:\n", indent, "");
1799}
1800
Jiri Olsad4f63a42015-06-26 11:29:26 +02001801static void print_footer(void)
1802{
Jiri Olsabc22de92018-04-23 11:08:20 +02001803 double avg = avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsa58215222015-07-21 14:31:24 +02001804 FILE *output = stat_config.output;
Andi Kleen918c7b02017-05-22 18:00:16 -07001805 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001806
Jiri Olsad4f63a42015-06-26 11:29:26 +02001807 if (!null_run)
1808 fprintf(output, "\n");
Jiri Olsabc22de92018-04-23 11:08:20 +02001809
1810 if (run_count == 1) {
1811 fprintf(output, " %17.9f seconds time elapsed", avg);
1812 } else {
1813 double sd = stddev_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
1814 /*
1815 * Display at most 2 more significant
1816 * digits than the stddev inaccuracy.
1817 */
1818 int precision = get_precision(sd) + 2;
1819
Jiri Olsae55c14a2018-04-23 11:08:21 +02001820 if (walltime_run_table)
1821 print_table(output, precision, avg);
1822
Jiri Olsabc22de92018-04-23 11:08:20 +02001823 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1824 precision, avg, precision, sd);
1825
1826 print_noise_pct(sd, avg);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001827 }
1828 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001829
Andi Kleen918c7b02017-05-22 18:00:16 -07001830 if (print_free_counters_hint &&
1831 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1832 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001833 fprintf(output,
1834"Some events weren't counted. Try disabling the NMI watchdog:\n"
1835" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1836" perf stat ...\n"
1837" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001838
1839 if (print_mixed_hw_group_error)
1840 fprintf(output,
1841 "The events in group usually have to be from "
1842 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001843}
1844
1845static void print_counters(struct timespec *ts, int argc, const char **argv)
1846{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001847 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001848 struct perf_evsel *counter;
1849 char buf[64], *prefix = NULL;
1850
Jiri Olsa664c98d2015-11-05 15:40:50 +01001851 /* Do not print anything if we record to the pipe. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001852 if (STAT_RECORD && perf_stat.data.is_pipe)
Jiri Olsa664c98d2015-11-05 15:40:50 +01001853 return;
1854
Jiri Olsad4f63a42015-06-26 11:29:26 +02001855 if (interval)
1856 print_interval(prefix = buf, ts);
1857 else
1858 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001859
Andi Kleen54b50912016-03-03 15:57:36 -08001860 if (metric_only) {
1861 static int num_print_iv;
1862
Andi Kleen41c8ca22016-05-24 12:52:38 -07001863 if (num_print_iv == 0 && !interval)
1864 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001865 if (num_print_iv++ == 25)
1866 num_print_iv = 0;
1867 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1868 fprintf(stat_config.output, "%s", prefix);
1869 }
1870
Jiri Olsa421a50f2015-07-21 14:31:22 +02001871 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001872 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001873 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001874 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001875 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001876 case AGGR_THREAD:
Andi Kleene864c5c2017-08-31 12:40:35 -07001877 evlist__for_each_entry(evsel_list, counter) {
1878 if (is_duration_time(counter))
1879 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001880 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001881 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001882 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001883 case AGGR_GLOBAL:
Andi Kleene864c5c2017-08-31 12:40:35 -07001884 evlist__for_each_entry(evsel_list, counter) {
1885 if (is_duration_time(counter))
1886 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001887 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001888 }
Andi Kleen54b50912016-03-03 15:57:36 -08001889 if (metric_only)
1890 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001891 break;
1892 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001893 if (metric_only)
1894 print_no_aggr_metric(prefix);
1895 else {
Andi Kleene864c5c2017-08-31 12:40:35 -07001896 evlist__for_each_entry(evsel_list, counter) {
1897 if (is_duration_time(counter))
1898 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001899 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001900 }
Andi Kleen206cab62016-03-03 15:57:37 -08001901 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001902 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001903 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001904 default:
1905 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001906 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001907
Jiri Olsad4f63a42015-06-26 11:29:26 +02001908 if (!interval && !csv_output)
1909 print_footer();
1910
Jiri Olsa58215222015-07-21 14:31:24 +02001911 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001912}
1913
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001914static volatile int signr = -1;
1915
Ingo Molnar52425192009-05-26 09:17:18 +02001916static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001917{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001918 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001919 done = 1;
1920
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001921 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001922 /*
1923 * render child_pid harmless
1924 * won't send SIGTERM to a random
1925 * process in case of race condition
1926 * and fast PID recycling
1927 */
1928 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001929}
1930
1931static void sig_atexit(void)
1932{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001933 sigset_t set, oset;
1934
1935 /*
1936 * avoid race condition with SIGCHLD handler
1937 * in skip_signal() which is modifying child_pid
1938 * goal is to avoid send SIGTERM to a random
1939 * process
1940 */
1941 sigemptyset(&set);
1942 sigaddset(&set, SIGCHLD);
1943 sigprocmask(SIG_BLOCK, &set, &oset);
1944
Chris Wilson933da832009-10-04 01:35:01 +01001945 if (child_pid != -1)
1946 kill(child_pid, SIGTERM);
1947
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001948 sigprocmask(SIG_SETMASK, &oset, NULL);
1949
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001950 if (signr == -1)
1951 return;
1952
1953 signal(signr, SIG_DFL);
1954 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001955}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001956
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001957static int stat__set_big_num(const struct option *opt __maybe_unused,
1958 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001959{
1960 big_num_opt = unset ? 0 : 1;
1961 return 0;
1962}
1963
Andi Kleen44b1e602016-05-30 12:49:42 -03001964static int enable_metric_only(const struct option *opt __maybe_unused,
1965 const char *s __maybe_unused, int unset)
1966{
1967 force_metric_only = true;
1968 metric_only = !unset;
1969 return 0;
1970}
1971
Andi Kleenb18f3e32017-08-31 12:40:31 -07001972static int parse_metric_groups(const struct option *opt,
1973 const char *str,
1974 int unset __maybe_unused)
1975{
1976 return metricgroup__parse_groups(opt, str, &metric_events);
1977}
1978
Jiri Olsae0547312015-11-05 15:40:45 +01001979static const struct option stat_options[] = {
1980 OPT_BOOLEAN('T', "transaction", &transaction_run,
1981 "hardware transaction statistics"),
1982 OPT_CALLBACK('e', "event", &evsel_list, "event",
1983 "event selector. use 'perf list' to list available events",
1984 parse_events_option),
1985 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1986 "event filter", parse_filter),
1987 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1988 "child tasks do not inherit counters"),
1989 OPT_STRING('p', "pid", &target.pid, "pid",
1990 "stat events on existing process id"),
1991 OPT_STRING('t', "tid", &target.tid, "tid",
1992 "stat events on existing thread id"),
1993 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1994 "system-wide collection from all CPUs"),
1995 OPT_BOOLEAN('g', "group", &group,
1996 "put the counters into a counter group"),
1997 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1998 OPT_INCR('v', "verbose", &verbose,
1999 "be more verbose (show counter open errors, etc)"),
2000 OPT_INTEGER('r', "repeat", &run_count,
2001 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsae55c14a2018-04-23 11:08:21 +02002002 OPT_BOOLEAN(0, "table", &walltime_run_table,
2003 "display details about each run (only with -r option)"),
Jiri Olsae0547312015-11-05 15:40:45 +01002004 OPT_BOOLEAN('n', "null", &null_run,
2005 "null run - dont start any counters"),
2006 OPT_INCR('d', "detailed", &detailed_run,
2007 "detailed run - start a lot of events"),
2008 OPT_BOOLEAN('S', "sync", &sync_run,
2009 "call sync() before starting a run"),
2010 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
2011 "print large numbers with thousands\' separators",
2012 stat__set_big_num),
2013 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
2014 "list of cpus to monitor in system-wide"),
2015 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
2016 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07002017 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01002018 OPT_STRING('x', "field-separator", &csv_sep, "separator",
2019 "print counts with custom separator"),
2020 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
2021 "monitor event in cgroup name only", parse_cgroups),
2022 OPT_STRING('o', "output", &output_name, "file", "output file name"),
2023 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
2024 OPT_INTEGER(0, "log-fd", &output_fd,
2025 "log output to fd, instead of stderr"),
2026 OPT_STRING(0, "pre", &pre_cmd, "command",
2027 "command to run prior to the measured command"),
2028 OPT_STRING(0, "post", &post_cmd, "command",
2029 "command to run after to the measured command"),
2030 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03002031 "print counts at regular interval in ms "
2032 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01002033 OPT_INTEGER(0, "interval-count", &stat_config.times,
2034 "print counts for fixed number of times"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002035 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
2036 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01002037 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
2038 "aggregate counts per processor socket", AGGR_SOCKET),
2039 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
2040 "aggregate counts per physical processor core", AGGR_CORE),
2041 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
2042 "aggregate counts per thread", AGGR_THREAD),
2043 OPT_UINTEGER('D', "delay", &initial_delay,
2044 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03002045 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
2046 "Only print computed metrics. No raw values", enable_metric_only),
2047 OPT_BOOLEAN(0, "topdown", &topdown_run,
2048 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07002049 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
2050 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07002051 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
2052 "monitor specified metrics or metric groups (separated by ,)",
2053 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01002054 OPT_END()
2055};
2056
Jiri Olsa1fe7a302015-10-16 12:41:15 +02002057static int perf_stat__get_socket(struct cpu_map *map, int cpu)
2058{
2059 return cpu_map__get_socket(map, cpu, NULL);
2060}
2061
2062static int perf_stat__get_core(struct cpu_map *map, int cpu)
2063{
2064 return cpu_map__get_core(map, cpu, NULL);
2065}
2066
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002067static int cpu_map__get_max(struct cpu_map *map)
2068{
2069 int i, max = -1;
2070
2071 for (i = 0; i < map->nr; i++) {
2072 if (map->map[i] > max)
2073 max = map->map[i];
2074 }
2075
2076 return max;
2077}
2078
2079static struct cpu_map *cpus_aggr_map;
2080
2081static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
2082{
2083 int cpu;
2084
2085 if (idx >= map->nr)
2086 return -1;
2087
2088 cpu = map->map[idx];
2089
2090 if (cpus_aggr_map->map[cpu] == -1)
2091 cpus_aggr_map->map[cpu] = get_id(map, idx);
2092
2093 return cpus_aggr_map->map[cpu];
2094}
2095
2096static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
2097{
2098 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
2099}
2100
2101static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
2102{
2103 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
2104}
2105
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002106static int perf_stat_init_aggr_mode(void)
2107{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002108 int nr;
2109
Jiri Olsa421a50f2015-07-21 14:31:22 +02002110 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002111 case AGGR_SOCKET:
2112 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
2113 perror("cannot build socket map");
2114 return -1;
2115 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002116 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002117 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002118 case AGGR_CORE:
2119 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
2120 perror("cannot build core map");
2121 return -1;
2122 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002123 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002124 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002125 case AGGR_NONE:
2126 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002127 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002128 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002129 default:
2130 break;
2131 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002132
2133 /*
2134 * The evsel_list->cpus is the base we operate on,
2135 * taking the highest cpu number to be the size of
2136 * the aggregation translate cpumap.
2137 */
2138 nr = cpu_map__get_max(evsel_list->cpus);
2139 cpus_aggr_map = cpu_map__empty_new(nr + 1);
2140 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002141}
2142
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002143static void perf_stat__exit_aggr_mode(void)
2144{
2145 cpu_map__put(aggr_map);
2146 cpu_map__put(cpus_aggr_map);
2147 aggr_map = NULL;
2148 cpus_aggr_map = NULL;
2149}
2150
Jiri Olsa68d702f2015-11-05 15:40:58 +01002151static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2152{
2153 int cpu;
2154
2155 if (idx > map->nr)
2156 return -1;
2157
2158 cpu = map->map[idx];
2159
Jan Stancekda8a58b2017-02-17 12:10:26 +01002160 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002161 return -1;
2162
2163 return cpu;
2164}
2165
2166static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2167{
2168 struct perf_env *env = data;
2169 int cpu = perf_env__get_cpu(env, map, idx);
2170
2171 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2172}
2173
2174static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2175{
2176 struct perf_env *env = data;
2177 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2178
2179 if (cpu != -1) {
2180 int socket_id = env->cpu[cpu].socket_id;
2181
2182 /*
2183 * Encode socket in upper 16 bits
2184 * core_id is relative to socket, and
2185 * we need a global id. So we combine
2186 * socket + core id.
2187 */
2188 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2189 }
2190
2191 return core;
2192}
2193
2194static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2195 struct cpu_map **sockp)
2196{
2197 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2198}
2199
2200static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2201 struct cpu_map **corep)
2202{
2203 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2204}
2205
2206static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2207{
2208 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2209}
2210
2211static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2212{
2213 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2214}
2215
2216static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2217{
2218 struct perf_env *env = &st->session->header.env;
2219
2220 switch (stat_config.aggr_mode) {
2221 case AGGR_SOCKET:
2222 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2223 perror("cannot build socket map");
2224 return -1;
2225 }
2226 aggr_get_id = perf_stat__get_socket_file;
2227 break;
2228 case AGGR_CORE:
2229 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2230 perror("cannot build core map");
2231 return -1;
2232 }
2233 aggr_get_id = perf_stat__get_core_file;
2234 break;
2235 case AGGR_NONE:
2236 case AGGR_GLOBAL:
2237 case AGGR_THREAD:
2238 case AGGR_UNSET:
2239 default:
2240 break;
2241 }
2242
2243 return 0;
2244}
2245
Andi Kleen44b1e602016-05-30 12:49:42 -03002246static int topdown_filter_events(const char **attr, char **str, bool use_group)
2247{
2248 int off = 0;
2249 int i;
2250 int len = 0;
2251 char *s;
2252
2253 for (i = 0; attr[i]; i++) {
2254 if (pmu_have_event("cpu", attr[i])) {
2255 len += strlen(attr[i]) + 1;
2256 attr[i - off] = attr[i];
2257 } else
2258 off++;
2259 }
2260 attr[i - off] = NULL;
2261
2262 *str = malloc(len + 1 + 2);
2263 if (!*str)
2264 return -1;
2265 s = *str;
2266 if (i - off == 0) {
2267 *s = 0;
2268 return 0;
2269 }
2270 if (use_group)
2271 *s++ = '{';
2272 for (i = 0; attr[i]; i++) {
2273 strcpy(s, attr[i]);
2274 s += strlen(s);
2275 *s++ = ',';
2276 }
2277 if (use_group) {
2278 s[-1] = '}';
2279 *s = 0;
2280 } else
2281 s[-1] = 0;
2282 return 0;
2283}
2284
2285__weak bool arch_topdown_check_group(bool *warn)
2286{
2287 *warn = false;
2288 return false;
2289}
2290
2291__weak void arch_topdown_group_warn(void)
2292{
2293}
2294
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002295/*
2296 * Add default attributes, if there were no attributes specified or
2297 * if -d/--detailed, -d -d or -d -d -d is used:
2298 */
2299static int add_default_attributes(void)
2300{
Andi Kleen44b1e602016-05-30 12:49:42 -03002301 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002302 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002303
2304 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2305 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2306 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2307 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2308
2309 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002310};
2311 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002312 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002313};
2314 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002315 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002316};
2317 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002318 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2319 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2320 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2321
2322};
2323
2324/*
2325 * Detailed stats (-d), covering the L1 and last level data caches:
2326 */
2327 struct perf_event_attr detailed_attrs[] = {
2328
2329 { .type = PERF_TYPE_HW_CACHE,
2330 .config =
2331 PERF_COUNT_HW_CACHE_L1D << 0 |
2332 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2333 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
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_MISS << 16) },
2340
2341 { .type = PERF_TYPE_HW_CACHE,
2342 .config =
2343 PERF_COUNT_HW_CACHE_LL << 0 |
2344 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2345 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 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_MISS << 16) },
2352};
2353
2354/*
2355 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2356 */
2357 struct perf_event_attr very_detailed_attrs[] = {
2358
2359 { .type = PERF_TYPE_HW_CACHE,
2360 .config =
2361 PERF_COUNT_HW_CACHE_L1I << 0 |
2362 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2363 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
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_MISS << 16) },
2370
2371 { .type = PERF_TYPE_HW_CACHE,
2372 .config =
2373 PERF_COUNT_HW_CACHE_DTLB << 0 |
2374 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2375 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 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_MISS << 16) },
2382
2383 { .type = PERF_TYPE_HW_CACHE,
2384 .config =
2385 PERF_COUNT_HW_CACHE_ITLB << 0 |
2386 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2387 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 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_MISS << 16) },
2394
2395};
2396
2397/*
2398 * Very, very detailed stats (-d -d -d), adding prefetch events:
2399 */
2400 struct perf_event_attr very_very_detailed_attrs[] = {
2401
2402 { .type = PERF_TYPE_HW_CACHE,
2403 .config =
2404 PERF_COUNT_HW_CACHE_L1D << 0 |
2405 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2406 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
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_MISS << 16) },
2413};
2414
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002415 /* Set attrs if no event is selected and !null_run: */
2416 if (null_run)
2417 return 0;
2418
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002419 if (transaction_run) {
Thomas Richterfca323402018-03-08 15:57:35 +01002420 struct parse_events_error errinfo;
2421
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002422 if (pmu_have_event("cpu", "cycles-ct") &&
2423 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002424 err = parse_events(evsel_list, transaction_attrs,
2425 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002426 else
Thomas Richterfca323402018-03-08 15:57:35 +01002427 err = parse_events(evsel_list,
2428 transaction_limited_attrs,
2429 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002430 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002431 fprintf(stderr, "Cannot set up transaction events\n");
2432 return -1;
2433 }
2434 return 0;
2435 }
2436
Kan Liangdaefd0b2017-05-26 12:05:38 -07002437 if (smi_cost) {
2438 int smi;
2439
2440 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2441 fprintf(stderr, "freeze_on_smi is not supported.\n");
2442 return -1;
2443 }
2444
2445 if (!smi) {
2446 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2447 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2448 return -1;
2449 }
2450 smi_reset = true;
2451 }
2452
2453 if (pmu_have_event("msr", "aperf") &&
2454 pmu_have_event("msr", "smi")) {
2455 if (!force_metric_only)
2456 metric_only = true;
2457 err = parse_events(evsel_list, smi_cost_attrs, NULL);
2458 } else {
2459 fprintf(stderr, "To measure SMI cost, it needs "
2460 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2461 return -1;
2462 }
2463 if (err) {
2464 fprintf(stderr, "Cannot set up SMI cost events\n");
2465 return -1;
2466 }
2467 return 0;
2468 }
2469
Andi Kleen44b1e602016-05-30 12:49:42 -03002470 if (topdown_run) {
2471 char *str = NULL;
2472 bool warn = false;
2473
2474 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2475 stat_config.aggr_mode != AGGR_CORE) {
2476 pr_err("top down event configuration requires --per-core mode\n");
2477 return -1;
2478 }
2479 stat_config.aggr_mode = AGGR_CORE;
2480 if (nr_cgroups || !target__has_cpu(&target)) {
2481 pr_err("top down event configuration requires system-wide mode (-a)\n");
2482 return -1;
2483 }
2484
2485 if (!force_metric_only)
2486 metric_only = true;
2487 if (topdown_filter_events(topdown_attrs, &str,
2488 arch_topdown_check_group(&warn)) < 0) {
2489 pr_err("Out of memory\n");
2490 return -1;
2491 }
2492 if (topdown_attrs[0] && str) {
2493 if (warn)
2494 arch_topdown_group_warn();
2495 err = parse_events(evsel_list, str, NULL);
2496 if (err) {
2497 fprintf(stderr,
2498 "Cannot set up top down events %s: %d\n",
2499 str, err);
2500 free(str);
2501 return -1;
2502 }
2503 } else {
2504 fprintf(stderr, "System does not support topdown\n");
2505 return -1;
2506 }
2507 free(str);
2508 }
2509
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002510 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002511 if (target__has_cpu(&target))
2512 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2513
Andi Kleen9dec4472016-02-26 16:27:56 -08002514 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2515 return -1;
2516 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2517 if (perf_evlist__add_default_attrs(evsel_list,
2518 frontend_attrs) < 0)
2519 return -1;
2520 }
2521 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2522 if (perf_evlist__add_default_attrs(evsel_list,
2523 backend_attrs) < 0)
2524 return -1;
2525 }
2526 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002527 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002528 }
2529
2530 /* Detailed events get appended to the event list: */
2531
2532 if (detailed_run < 1)
2533 return 0;
2534
2535 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002536 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002537 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002538
2539 if (detailed_run < 2)
2540 return 0;
2541
2542 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002543 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002544 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002545
2546 if (detailed_run < 3)
2547 return 0;
2548
2549 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002550 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002551}
2552
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002553static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002554 "perf stat record [<options>]",
2555 NULL,
2556};
2557
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002558static void init_features(struct perf_session *session)
2559{
2560 int feat;
2561
2562 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2563 perf_header__set_feat(&session->header, feat);
2564
2565 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2566 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2567 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2568 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2569}
2570
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002571static int __cmd_record(int argc, const char **argv)
2572{
2573 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002574 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002575
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002576 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002577 PARSE_OPT_STOP_AT_NON_OPTION);
2578
2579 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002580 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002581
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002582 if (run_count != 1 || forever) {
2583 pr_err("Cannot use -r option with perf stat record.\n");
2584 return -1;
2585 }
2586
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002587 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002588 if (session == NULL) {
2589 pr_err("Perf session creation failed.\n");
2590 return -1;
2591 }
2592
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002593 init_features(session);
2594
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002595 session->evlist = evsel_list;
2596 perf_stat.session = session;
2597 perf_stat.record = true;
2598 return argc;
2599}
2600
Jiri Olsaa56f9392015-11-05 15:40:59 +01002601static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2602 union perf_event *event,
2603 struct perf_session *session)
2604{
Andi Kleene3b03b62016-05-05 16:04:03 -07002605 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002606 struct perf_evsel *counter;
2607 struct timespec tsh, *ts = NULL;
2608 const char **argv = session->header.env.cmdline_argv;
2609 int argc = session->header.env.nr_cmdline;
2610
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002611 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002612 perf_stat_process_counter(&stat_config, counter);
2613
Andi Kleene3b03b62016-05-05 16:04:03 -07002614 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2615 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002616
Andi Kleene3b03b62016-05-05 16:04:03 -07002617 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002618 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2619 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002620 ts = &tsh;
2621 }
2622
2623 print_counters(ts, argc, argv);
2624 return 0;
2625}
2626
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002627static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002628int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002629 union perf_event *event,
2630 struct perf_session *session __maybe_unused)
2631{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002632 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2633
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002634 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002635
Jiri Olsa89af4e02015-11-05 15:41:02 +01002636 if (cpu_map__empty(st->cpus)) {
2637 if (st->aggr_mode != AGGR_UNSET)
2638 pr_warning("warning: processing task data, aggregation mode not set\n");
2639 return 0;
2640 }
2641
2642 if (st->aggr_mode != AGGR_UNSET)
2643 stat_config.aggr_mode = st->aggr_mode;
2644
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002645 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002646 perf_stat_init_aggr_mode();
2647 else
2648 perf_stat_init_aggr_mode_file(st);
2649
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002650 return 0;
2651}
2652
Jiri Olsa1975d362015-11-05 15:40:56 +01002653static int set_maps(struct perf_stat *st)
2654{
2655 if (!st->cpus || !st->threads)
2656 return 0;
2657
2658 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2659 return -EINVAL;
2660
2661 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2662
2663 if (perf_evlist__alloc_stats(evsel_list, true))
2664 return -ENOMEM;
2665
2666 st->maps_allocated = true;
2667 return 0;
2668}
2669
2670static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002671int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002672 union perf_event *event,
2673 struct perf_session *session __maybe_unused)
2674{
2675 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2676
2677 if (st->threads) {
2678 pr_warning("Extra thread map event, ignoring.\n");
2679 return 0;
2680 }
2681
2682 st->threads = thread_map__new_event(&event->thread_map);
2683 if (!st->threads)
2684 return -ENOMEM;
2685
2686 return set_maps(st);
2687}
2688
2689static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002690int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002691 union perf_event *event,
2692 struct perf_session *session __maybe_unused)
2693{
2694 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2695 struct cpu_map *cpus;
2696
2697 if (st->cpus) {
2698 pr_warning("Extra cpu map event, ignoring.\n");
2699 return 0;
2700 }
2701
2702 cpus = cpu_map__new_data(&event->cpu_map.data);
2703 if (!cpus)
2704 return -ENOMEM;
2705
2706 st->cpus = cpus;
2707 return set_maps(st);
2708}
2709
Jin Yao56739442017-12-05 22:03:07 +08002710static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2711{
2712 int i;
2713
2714 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2715 if (!config->stats)
2716 return -1;
2717
2718 config->stats_num = nthreads;
2719
2720 for (i = 0; i < nthreads; i++)
2721 runtime_stat__init(&config->stats[i]);
2722
2723 return 0;
2724}
2725
2726static void runtime_stat_delete(struct perf_stat_config *config)
2727{
2728 int i;
2729
2730 if (!config->stats)
2731 return;
2732
2733 for (i = 0; i < config->stats_num; i++)
2734 runtime_stat__exit(&config->stats[i]);
2735
2736 free(config->stats);
2737}
2738
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002739static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002740 "perf stat report [<options>]",
2741 NULL,
2742};
2743
2744static struct perf_stat perf_stat = {
2745 .tool = {
2746 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002747 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002748 .thread_map = process_thread_map_event,
2749 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002750 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002751 .stat = perf_event__process_stat_event,
2752 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002753 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002754 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002755};
2756
2757static int __cmd_report(int argc, const char **argv)
2758{
2759 struct perf_session *session;
2760 const struct option options[] = {
2761 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002762 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2763 "aggregate counts per processor socket", AGGR_SOCKET),
2764 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2765 "aggregate counts per physical processor core", AGGR_CORE),
2766 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2767 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002768 OPT_END()
2769 };
2770 struct stat st;
2771 int ret;
2772
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002773 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002774
2775 if (!input_name || !strlen(input_name)) {
2776 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2777 input_name = "-";
2778 else
2779 input_name = "perf.data";
2780 }
2781
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002782 perf_stat.data.file.path = input_name;
2783 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002784
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002785 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002786 if (session == NULL)
2787 return -1;
2788
2789 perf_stat.session = session;
2790 stat_config.output = stderr;
2791 evsel_list = session->evlist;
2792
2793 ret = perf_session__process_events(session);
2794 if (ret)
2795 return ret;
2796
2797 perf_session__delete(session);
2798 return 0;
2799}
2800
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002801static void setup_system_wide(int forks)
2802{
2803 /*
2804 * Make system wide (-a) the default target if
2805 * no target was specified and one of following
2806 * conditions is met:
2807 *
2808 * - there's no workload specified
2809 * - there is workload specified but all requested
2810 * events are system wide events
2811 */
2812 if (!target__none(&target))
2813 return;
2814
2815 if (!forks)
2816 target.system_wide = true;
2817 else {
2818 struct perf_evsel *counter;
2819
2820 evlist__for_each_entry(evsel_list, counter) {
2821 if (!counter->system_wide)
2822 return;
2823 }
2824
2825 if (evsel_list->nr_entries)
2826 target.system_wide = true;
2827 }
2828}
2829
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002830int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002831{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002832 const char * const stat_usage[] = {
2833 "perf stat [<options>] [<command>]",
2834 NULL
2835 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002836 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002837 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002838 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002839 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002840 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002841
Stephane Eranian5af52b52010-05-18 15:00:01 +02002842 setlocale(LC_ALL, "");
2843
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002844 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002845 if (evsel_list == NULL)
2846 return -ENOMEM;
2847
Wang Nan1669e502016-02-19 11:43:58 +00002848 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002849 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2850 (const char **) stat_usage,
2851 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002852 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002853 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002854
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002855 if (csv_sep) {
2856 csv_output = true;
2857 if (!strcmp(csv_sep, "\\t"))
2858 csv_sep = "\t";
2859 } else
2860 csv_sep = DEFAULT_SEPARATOR;
2861
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002862 if (argc && !strncmp(argv[0], "rec", 3)) {
2863 argc = __cmd_record(argc, argv);
2864 if (argc < 0)
2865 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002866 } else if (argc && !strncmp(argv[0], "rep", 3))
2867 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002868
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002869 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002870 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002871
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002872 /*
2873 * For record command the -o is already taken care of.
2874 */
2875 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002876 output = NULL;
2877
Jim Cromie56f3bae2011-09-07 17:14:00 -06002878 if (output_name && output_fd) {
2879 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002880 parse_options_usage(stat_usage, stat_options, "o", 1);
2881 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002882 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002883 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002884
Andi Kleen54b50912016-03-03 15:57:36 -08002885 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2886 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2887 goto out;
2888 }
2889
Andi Kleen54b50912016-03-03 15:57:36 -08002890 if (metric_only && run_count > 1) {
2891 fprintf(stderr, "--metric-only is not supported with -r\n");
2892 goto out;
2893 }
2894
Jiri Olsae55c14a2018-04-23 11:08:21 +02002895 if (walltime_run_table && run_count <= 1) {
2896 fprintf(stderr, "--table is only supported with -r\n");
2897 parse_options_usage(stat_usage, stat_options, "r", 1);
2898 parse_options_usage(NULL, stat_options, "table", 0);
2899 goto out;
2900 }
2901
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002902 if (output_fd < 0) {
2903 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002904 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002905 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002906 }
2907
Stephane Eranian4aa90152011-08-15 22:22:33 +02002908 if (!output) {
2909 struct timespec tm;
2910 mode = append_file ? "a" : "w";
2911
2912 output = fopen(output_name, mode);
2913 if (!output) {
2914 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002915 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002916 }
2917 clock_gettime(CLOCK_REALTIME, &tm);
2918 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002919 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002920 mode = append_file ? "a" : "w";
2921 output = fdopen(output_fd, mode);
2922 if (!output) {
2923 perror("Failed opening logfd");
2924 return -errno;
2925 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002926 }
2927
Jiri Olsa58215222015-07-21 14:31:24 +02002928 stat_config.output = output;
2929
Stephane Eraniand7470b62010-12-01 18:49:05 +02002930 /*
2931 * let the spreadsheet do the pretty-printing
2932 */
2933 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002934 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002935 if (big_num_opt == 1) {
2936 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002937 parse_options_usage(stat_usage, stat_options, "B", 1);
2938 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002939 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002940 } else /* Nope, so disable big number formatting */
2941 big_num = false;
2942 } else if (big_num_opt == 0) /* User passed --no-big-num */
2943 big_num = false;
2944
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002945 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002946
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002947 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002948 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002949 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002950 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002951 } else if (run_count == 0) {
2952 forever = true;
2953 run_count = 1;
2954 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002955
Jiri Olsae55c14a2018-04-23 11:08:21 +02002956 if (walltime_run_table) {
2957 walltime_run = zalloc(run_count * sizeof(walltime_run[0]));
2958 if (!walltime_run) {
2959 pr_err("failed to setup -r option");
2960 goto out;
2961 }
2962 }
2963
Jin Yao1d9f8d12017-12-05 22:03:10 +08002964 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2965 !target__has_task(&target)) {
2966 if (!target.system_wide || target.cpu_list) {
2967 fprintf(stderr, "The --per-thread option is only "
2968 "available when monitoring via -p -t -a "
2969 "options or only --per-thread.\n");
2970 parse_options_usage(NULL, stat_options, "p", 1);
2971 parse_options_usage(NULL, stat_options, "t", 1);
2972 goto out;
2973 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002974 }
2975
2976 /*
2977 * no_aggr, cgroup are for system-wide only
2978 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2979 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002980 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2981 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002982 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002983 fprintf(stderr, "both cgroup and no-aggregation "
2984 "modes only available in system-wide mode\n");
2985
Jiri Olsae0547312015-11-05 15:40:45 +01002986 parse_options_usage(stat_usage, stat_options, "G", 1);
2987 parse_options_usage(NULL, stat_options, "A", 1);
2988 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002989 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002990 }
2991
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002992 if (add_default_attributes())
2993 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002994
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002995 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002996
Jin Yao1d9f8d12017-12-05 22:03:10 +08002997 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2998 target.per_thread = true;
2999
Namhyung Kim77a6f012012-05-07 14:09:04 +09003000 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003001 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09003002 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01003003 parse_options_usage(stat_usage, stat_options, "p", 1);
3004 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003005 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09003006 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01003007 parse_options_usage(stat_usage, stat_options, "C", 1);
3008 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09003009 }
3010 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02003011 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02003012
3013 /*
3014 * Initialize thread_map with comm names,
3015 * so we could print it out on output.
3016 */
Jin Yao56739442017-12-05 22:03:07 +08003017 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02003018 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08003019 if (target.system_wide) {
3020 if (runtime_stat_new(&stat_config,
3021 thread_map__nr(evsel_list->threads))) {
3022 goto out;
3023 }
3024 }
3025 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02003026
yuzhoujiandb06a262018-01-29 10:25:22 +01003027 if (stat_config.times && interval)
3028 interval_count = true;
3029 else if (stat_config.times && !interval) {
3030 pr_err("interval-count option should be used together with "
3031 "interval-print.\n");
3032 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
3033 parse_options_usage(stat_usage, stat_options, "I", 1);
3034 goto out;
3035 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02003036
yuzhoujianf1f8ad52018-01-29 10:25:23 +01003037 if (timeout && timeout < 100) {
3038 if (timeout < 10) {
3039 pr_err("timeout must be >= 10ms.\n");
3040 parse_options_usage(stat_usage, stat_options, "timeout", 0);
3041 goto out;
3042 } else
3043 pr_warning("timeout < 100ms. "
3044 "The overhead percentage could be high in some cases. "
3045 "Please proceed with caution.\n");
3046 }
3047 if (timeout && interval) {
3048 pr_err("timeout option is not supported with interval-print.\n");
3049 parse_options_usage(stat_usage, stat_options, "timeout", 0);
3050 parse_options_usage(stat_usage, stat_options, "I", 1);
3051 goto out;
3052 }
3053
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003054 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003055 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03003056
Stephane Eranian86ee6e12013-02-14 13:57:27 +01003057 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003058 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01003059
Ingo Molnar58d7e992009-05-15 11:03:23 +02003060 /*
3061 * We dont want to block the signals - that would cause
3062 * child tasks to inherit that and Ctrl-C would not work.
3063 * What we want is for Ctrl-C to work in the exec()-ed
3064 * task, but being ignored by perf stat itself:
3065 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02003066 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003067 if (!forever)
3068 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01003069 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02003070 signal(SIGALRM, skip_signal);
3071 signal(SIGABRT, skip_signal);
3072
Ingo Molnar42202dd2009-06-13 14:57:28 +02003073 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003074 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09003075 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02003076 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
3077 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02003078
Jiri Olsae55c14a2018-04-23 11:08:21 +02003079 status = run_perf_stat(argc, argv, run_idx);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003080 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02003081 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02003082 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003083 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02003084 }
3085
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003086 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02003087 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003088
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003089 if (STAT_RECORD) {
3090 /*
3091 * We synthesize the kernel mmap record just so that older tools
3092 * don't emit warnings about not being able to resolve symbols
3093 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3094 * a saner message about no samples being in the perf.data file.
3095 *
3096 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01003097 * in header.c at the moment 'perf stat record' gets introduced, which
3098 * is not really needed once we start adding the stat specific PERF_RECORD_
3099 * records, but the need to suppress the kptr_restrict messages in older
3100 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003101 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003102 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003103 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3104 process_synthesized_event,
3105 &perf_stat.session->machines.host);
3106 if (err) {
3107 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3108 "older tools may produce warnings about this file\n.");
3109 }
3110
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003111 if (!interval) {
3112 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3113 pr_err("failed to write stat round event\n");
3114 }
3115
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003116 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003117 perf_stat.session->header.data_size += perf_stat.bytes_written;
3118 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3119 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003120
3121 perf_session__delete(perf_stat.session);
3122 }
3123
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003124 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003125 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003126out:
Jiri Olsae55c14a2018-04-23 11:08:21 +02003127 free(walltime_run);
3128
Kan Liangdaefd0b2017-05-26 12:05:38 -07003129 if (smi_cost && smi_reset)
3130 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3131
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003132 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003133
3134 runtime_stat_delete(&stat_config);
3135
Ingo Molnar42202dd2009-06-13 14:57:28 +02003136 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003137}