blob: 096ccb25c11ff7786c6df0c24b695cd0eff5bec0 [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>
Jiri Olsa0ce2da12018-06-05 14:13:13 +020083#include <sys/time.h>
84#include <sys/resource.h>
85#include <sys/wait.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020086
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030087#include "sane_ctype.h"
88
Stephane Eraniand7470b62010-12-01 18:49:05 +020089#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060090#define CNTR_NOT_SUPPORTED "<not supported>"
91#define CNTR_NOT_COUNTED "<not counted>"
Kan Liangdaefd0b2017-05-26 12:05:38 -070092#define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
Stephane Eraniand7470b62010-12-01 18:49:05 +020093
Jiri Olsad4f63a42015-06-26 11:29:26 +020094static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010095
Andi Kleen4cabc3d2013-08-21 16:47:26 -070096/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020097static const char *transaction_attrs = {
98 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070099 "{"
100 "instructions,"
101 "cycles,"
102 "cpu/cycles-t/,"
103 "cpu/tx-start/,"
104 "cpu/el-start/,"
105 "cpu/cycles-ct/"
106 "}"
107};
108
109/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +0200110static const char * transaction_limited_attrs = {
111 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700112 "{"
113 "instructions,"
114 "cycles,"
115 "cpu/cycles-t/,"
116 "cpu/tx-start/"
117 "}"
118};
119
Andi Kleen44b1e602016-05-30 12:49:42 -0300120static const char * topdown_attrs[] = {
121 "topdown-total-slots",
122 "topdown-slots-retired",
123 "topdown-recovery-bubbles",
124 "topdown-fetch-bubbles",
125 "topdown-slots-issued",
126 NULL,
127};
128
Kan Liangdaefd0b2017-05-26 12:05:38 -0700129static const char *smi_cost_attrs = {
130 "{"
131 "msr/aperf/,"
132 "msr/smi/,"
133 "cycles"
134 "}"
135};
136
Robert Richter666e6d42012-04-05 18:26:27 +0200137static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200138
Andi Kleenb18f3e32017-08-31 12:40:31 -0700139static struct rblist metric_events;
140
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300141static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900142 .uid = UINT_MAX,
143};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530144
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100145typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
146
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530147static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200148static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200149static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000150static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200151static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700152static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300153static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700154static bool smi_cost = false;
155static bool smi_reset = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200156static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200157static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200158static const char *csv_sep = NULL;
159static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800160static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200161static const char *pre_cmd = NULL;
162static const char *post_cmd = NULL;
163static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700164static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100165static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500166static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800167static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300168static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700169static bool no_merge = false;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200170static bool walltime_run_table = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100171static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100172static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100173static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100174static bool append_file;
yuzhoujiandb06a262018-01-29 10:25:22 +0100175static bool interval_count;
Jiri Olsae0547312015-11-05 15:40:45 +0100176static const char *output_name;
177static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100178static int print_free_counters_hint;
Kan Liang30060ea2018-04-24 11:20:11 -0700179static int print_mixed_hw_group_error;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200180static u64 *walltime_run;
Jiri Olsa0ce2da12018-06-05 14:13:13 +0200181static bool ru_display = false;
182static struct rusage ru_data;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200183
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100184struct perf_stat {
185 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100186 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100187 struct perf_session *session;
188 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100189 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100190 bool maps_allocated;
191 struct cpu_map *cpus;
192 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100193 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100194};
195
196static struct perf_stat perf_stat;
197#define STAT_RECORD perf_stat.record
198
Liming Wang60666c62009-12-31 16:05:50 +0800199static volatile int done = 0;
200
Jiri Olsa421a50f2015-07-21 14:31:22 +0200201static struct perf_stat_config stat_config = {
202 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200203 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200204};
205
Andi Kleene864c5c2017-08-31 12:40:35 -0700206static bool is_duration_time(struct perf_evsel *evsel)
207{
208 return !strcmp(evsel->name, "duration_time");
209}
210
Stephane Eranian13370a92013-01-29 12:47:44 +0100211static inline void diff_timespec(struct timespec *r, struct timespec *a,
212 struct timespec *b)
213{
214 r->tv_sec = a->tv_sec - b->tv_sec;
215 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300216 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100217 r->tv_sec--;
218 } else {
219 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
220 }
221}
222
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200223static void perf_stat__reset_stats(void)
224{
Jin Yao56739442017-12-05 22:03:07 +0800225 int i;
226
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200227 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200228 perf_stat__reset_shadow_stats();
Jin Yao56739442017-12-05 22:03:07 +0800229
230 for (i = 0; i < stat_config.stats_num; i++)
231 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200232}
233
Jiri Olsacac21422012-11-12 18:34:00 +0100234static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200235{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200236 struct perf_event_attr *attr = &evsel->attr;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200237 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200238
Jiri Olsa82bf3112017-07-26 14:02:06 +0200239 if (stat_config.scale) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200240 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
241 PERF_FORMAT_TOTAL_TIME_RUNNING;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200242 }
243
244 /*
245 * The event is part of non trivial group, let's enable
246 * the group read (for leader) and ID retrieval for all
247 * members.
248 */
249 if (leader->nr_members > 1)
250 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200251
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200252 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300253
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100254 /*
255 * Some events get initialized with sample_(period/type) set,
256 * like tracepoints. Clear it up for counting.
257 */
258 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100259
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100260 /*
261 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
262 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100263 *
264 * However for pipe sessions we need to keep it zero,
265 * because script's perf_evsel__check_attr is triggered
266 * by attr->sample_type != 0, and we can't run it on
267 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100268 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100269 if (!(STAT_RECORD && perf_stat.data.is_pipe))
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100270 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100271
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100272 /*
273 * Disabling all counters initially, they will be enabled
274 * either manually by us or by kernel via enable_on_exec
275 * set later.
276 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100277 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100278 attr->disabled = 1;
279
Jiri Olsac8280ce2015-12-03 10:06:45 +0100280 /*
281 * In case of initial_delay we enable tracee
282 * events manually.
283 */
284 if (target__none(&target) && !initial_delay)
285 attr->enable_on_exec = 1;
286 }
287
Jin Yao1d9f8d12017-12-05 22:03:10 +0800288 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300289 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200290
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300291 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200292}
293
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200294/*
295 * Does the counter have nsecs as a unit?
296 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200297static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200298{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200299 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
300 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200301 return 1;
302
303 return 0;
304}
305
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100306static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100307 union perf_event *event,
308 struct perf_sample *sample __maybe_unused,
309 struct machine *machine __maybe_unused)
310{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100311 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100312 pr_err("failed to write perf data, error: %m\n");
313 return -1;
314 }
315
316 perf_stat.bytes_written += event->header.size;
317 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100318}
319
Jiri Olsa1975d362015-11-05 15:40:56 +0100320static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100321{
Jiri Olsa1975d362015-11-05 15:40:56 +0100322 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100323 process_synthesized_event,
324 NULL);
325}
326
327#define WRITE_STAT_ROUND_EVENT(time, interval) \
328 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
329
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100330#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
331
332static int
333perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
334 struct perf_counts_values *count)
335{
336 struct perf_sample_id *sid = SID(counter, cpu, thread);
337
338 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
339 process_synthesized_event, NULL);
340}
341
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200342/*
343 * Read out the results of a single counter:
344 * do not aggregate counts across CPUs in system-wide mode
345 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200346static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200347{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100348 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100349 int ncpus, cpu, thread;
350
Jin Yao1d9f8d12017-12-05 22:03:10 +0800351 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Mark Rutland00e727b2016-07-15 11:08:10 +0100352 ncpus = perf_evsel__nr_cpus(counter);
353 else
354 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200355
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000356 if (!counter->supported)
357 return -ENOENT;
358
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100359 if (counter->system_wide)
360 nthreads = 1;
361
362 for (thread = 0; thread < nthreads; thread++) {
363 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200364 struct perf_counts_values *count;
365
366 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200367
368 /*
369 * The leader's group read loads data into its group members
370 * (via perf_evsel__read_counter) and sets threir count->loaded.
371 */
372 if (!count->loaded &&
373 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700374 counter->counts->scaled = -1;
375 perf_counts(counter->counts, cpu, thread)->ena = 0;
376 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100377 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700378 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100379
Jiri Olsa82bf3112017-07-26 14:02:06 +0200380 count->loaded = false;
381
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100382 if (STAT_RECORD) {
383 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
384 pr_err("failed to write stat event\n");
385 return -1;
386 }
387 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700388
389 if (verbose > 1) {
390 fprintf(stat_config.output,
391 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
392 perf_evsel__name(counter),
393 cpu,
394 count->val, count->ena, count->run);
395 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100396 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200397 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200398
399 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200400}
401
Mark Rutland3df33ef2016-08-09 14:04:29 +0100402static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200403{
404 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700405 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200406
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300407 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700408 ret = read_counter(counter);
409 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700410 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200411
Stephane Eraniandb49a712017-04-12 11:23:01 -0700412 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200413 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200414 }
415}
416
Jiri Olsaba411a92015-06-26 11:29:24 +0200417static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100418{
Stephane Eranian13370a92013-01-29 12:47:44 +0100419 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100420
Mark Rutland3df33ef2016-08-09 14:04:29 +0100421 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100422
Stephane Eranian13370a92013-01-29 12:47:44 +0100423 clock_gettime(CLOCK_MONOTONIC, &ts);
424 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100425
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100426 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300427 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100428 pr_err("failed to write stat round event\n");
429 }
430
Andi Kleenb90f1332017-08-31 12:40:36 -0700431 init_stats(&walltime_nsecs_stats);
432 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200433 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100434}
435
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100436static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700437{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100438 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300439 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100440
441 /*
442 * We need to enable counters only if:
443 * - we don't have tracee (attaching to task or cpu)
444 * - we have initial delay configured
445 */
446 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100447 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700448}
449
Mark Rutland3df33ef2016-08-09 14:04:29 +0100450static void disable_counters(void)
451{
452 /*
453 * If we don't have tracee (attaching to task or cpu), counters may
454 * still be running. To get accurate group ratios, we must stop groups
455 * from counting before reading their constituent counters.
456 */
457 if (!target__none(&target))
458 perf_evlist__disable(evsel_list);
459}
460
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300461static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300462
463/*
464 * perf_evlist__prepare_workload will send a SIGUSR1
465 * if the fork fails, since we asked by setting its
466 * want_signal to true.
467 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300468static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
469 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300470{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300471 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300472}
473
Jiri Olsa664c98d2015-11-05 15:40:50 +0100474static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100475{
476 int err;
477
Jiri Olsa664c98d2015-11-05 15:40:50 +0100478 if (is_pipe) {
479 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
480 process_synthesized_event);
481 if (err < 0) {
482 pr_err("Couldn't synthesize attrs.\n");
483 return err;
484 }
485 }
486
Andi Kleenbfd8f722017-11-17 13:42:58 -0800487 err = perf_event__synthesize_extra_attr(NULL,
488 evsel_list,
489 process_synthesized_event,
490 is_pipe);
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100491
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100492 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
493 process_synthesized_event,
494 NULL);
495 if (err < 0) {
496 pr_err("Couldn't synthesize thread map.\n");
497 return err;
498 }
499
500 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
501 process_synthesized_event, NULL);
502 if (err < 0) {
503 pr_err("Couldn't synthesize thread map.\n");
504 return err;
505 }
506
507 err = perf_event__synthesize_stat_config(NULL, &stat_config,
508 process_synthesized_event, NULL);
509 if (err < 0) {
510 pr_err("Couldn't synthesize config.\n");
511 return err;
512 }
513
514 return 0;
515}
516
Jiri Olsa2af46462015-11-05 15:40:49 +0100517#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
518
Andi Kleen42811d52017-10-05 19:00:28 -0700519static int __store_counter_ids(struct perf_evsel *counter)
Jiri Olsa2af46462015-11-05 15:40:49 +0100520{
521 int cpu, thread;
522
Andi Kleen42811d52017-10-05 19:00:28 -0700523 for (cpu = 0; cpu < xyarray__max_x(counter->fd); cpu++) {
524 for (thread = 0; thread < xyarray__max_y(counter->fd);
525 thread++) {
Jiri Olsa2af46462015-11-05 15:40:49 +0100526 int fd = FD(counter, cpu, thread);
527
528 if (perf_evlist__id_add_fd(evsel_list, counter,
529 cpu, thread, fd) < 0)
530 return -1;
531 }
532 }
533
534 return 0;
535}
536
537static int store_counter_ids(struct perf_evsel *counter)
538{
539 struct cpu_map *cpus = counter->cpus;
540 struct thread_map *threads = counter->threads;
541
542 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
543 return -ENOMEM;
544
Andi Kleen42811d52017-10-05 19:00:28 -0700545 return __store_counter_ids(counter);
Jiri Olsa2af46462015-11-05 15:40:49 +0100546}
547
Jiri Olsa82bf3112017-07-26 14:02:06 +0200548static bool perf_evsel__should_store_id(struct perf_evsel *counter)
549{
550 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
551}
552
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700553static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
554{
555 struct perf_evsel *c2, *leader;
556 bool is_open = true;
557
558 leader = evsel->leader;
559 pr_debug("Weak group for %s/%d failed\n",
560 leader->name, leader->nr_members);
561
562 /*
563 * for_each_group_member doesn't work here because it doesn't
564 * include the first entry.
565 */
566 evlist__for_each_entry(evsel_list, c2) {
567 if (c2 == evsel)
568 is_open = false;
569 if (c2->leader == leader) {
570 if (is_open)
571 perf_evsel__close(c2);
572 c2->leader = c2;
573 c2->nr_members = 0;
574 }
575 }
576 return leader;
577}
578
Jiri Olsae55c14a2018-04-23 11:08:21 +0200579static int __run_perf_stat(int argc, const char **argv, int run_idx)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200580{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200581 int interval = stat_config.interval;
yuzhoujiandb06a262018-01-29 10:25:22 +0100582 int times = stat_config.times;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100583 int timeout = stat_config.timeout;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300584 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200585 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100586 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100587 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100588 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200589 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300590 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100591 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600592 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200593
Stephane Eranian13370a92013-01-29 12:47:44 +0100594 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300595 ts.tv_sec = interval / USEC_PER_MSEC;
596 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100597 } else if (timeout) {
598 ts.tv_sec = timeout / USEC_PER_MSEC;
599 ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100600 } else {
601 ts.tv_sec = 1;
602 ts.tv_nsec = 0;
603 }
604
Liming Wang60666c62009-12-31 16:05:50 +0800605 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100606 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300607 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900608 perror("failed to prepare workload");
609 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800610 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900611 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000612 }
613
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200614 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300615 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200616
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300617 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300618try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100619 if (create_perf_stat_counter(counter) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700620
621 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700622 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700623 counter->leader != counter &&
624 counter->weak_group) {
625 counter = perf_evsel__reset_weak_group(counter);
626 goto try_again;
627 }
628
David Ahern979987a2012-05-08 09:29:16 -0600629 /*
630 * PPC returns ENXIO for HW counters until 2.6.37
631 * (behavior changed with commit b0a873e).
632 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100633 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600634 errno == ENOENT || errno == EOPNOTSUPP ||
635 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900636 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600637 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300638 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600639 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400640
641 if ((counter->leader != counter) ||
642 !(counter->leader->nr_members > 1))
643 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300644 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900645 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300646 ui__warning("%s\n", msg);
647 goto try_again;
Jin Yaoab6c79b2018-01-16 23:43:08 +0800648 } else if (target__has_per_thread(&target) &&
649 evsel_list->threads &&
650 evsel_list->threads->err_thread != -1) {
651 /*
652 * For global --per-thread case, skip current
653 * error thread.
654 */
655 if (!thread_map__remove(evsel_list->threads,
656 evsel_list->threads->err_thread)) {
657 evsel_list->threads->err_thread = -1;
658 goto try_again;
659 }
660 }
Ingo Molnarede70292011-04-28 08:48:42 +0200661
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300662 perf_evsel__open_strerror(counter, &target,
663 errno, msg, sizeof(msg));
664 ui__error("%s\n", msg);
665
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200666 if (child_pid != -1)
667 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600668
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200669 return -1;
670 }
David Ahern2cee77c2011-05-30 08:55:59 -0600671 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100672
673 l = strlen(counter->unit);
674 if (l > unit_width)
675 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100676
Jiri Olsa82bf3112017-07-26 14:02:06 +0200677 if (perf_evsel__should_store_id(counter) &&
678 store_counter_ids(counter))
Jiri Olsa2af46462015-11-05 15:40:49 +0100679 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300680 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200681
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300682 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300683 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300684 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300685 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100686 return -1;
687 }
688
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600689 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300690 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600691 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
692 str_error_r(errno, msg, sizeof(msg)));
693 return -1;
694 }
695
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100696 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100697 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100698
Jiri Olsa664c98d2015-11-05 15:40:50 +0100699 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100700 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100701 } else {
702 err = perf_session__write_header(perf_stat.session, evsel_list,
703 fd, false);
704 }
705
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100706 if (err < 0)
707 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100708
Jiri Olsa664c98d2015-11-05 15:40:50 +0100709 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100710 if (err < 0)
711 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100712 }
713
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200714 /*
715 * Enable counters and exec the command:
716 */
717 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100718 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200719
Liming Wang60666c62009-12-31 16:05:50 +0800720 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900721 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100722 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900723
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100724 if (interval || timeout) {
Stephane Eranian13370a92013-01-29 12:47:44 +0100725 while (!waitpid(child_pid, &status, WNOHANG)) {
726 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100727 if (timeout)
728 break;
Jiri Olsaba411a92015-06-26 11:29:24 +0200729 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100730 if (interval_count && !(--times))
731 break;
Stephane Eranian13370a92013-01-29 12:47:44 +0100732 }
733 }
Jiri Olsa0ce2da12018-06-05 14:13:13 +0200734 wait4(child_pid, &status, 0, &ru_data);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300735
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300736 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300737 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300738 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300739 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300740 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300741
Andi Kleen33e49ea2011-09-15 14:31:40 -0700742 if (WIFSIGNALED(status))
743 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800744 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100745 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100746 while (!done) {
747 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100748 if (timeout)
749 break;
yuzhoujiandb06a262018-01-29 10:25:22 +0100750 if (interval) {
Jiri Olsaba411a92015-06-26 11:29:24 +0200751 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100752 if (interval_count && !(--times))
753 break;
754 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100755 }
Liming Wang60666c62009-12-31 16:05:50 +0800756 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200757
Mark Rutland3df33ef2016-08-09 14:04:29 +0100758 disable_counters();
759
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200760 t1 = rdclock();
761
Jiri Olsae55c14a2018-04-23 11:08:21 +0200762 if (walltime_run_table)
763 walltime_run[run_idx] = t1 - t0;
764
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200765 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200766
Mark Rutland3df33ef2016-08-09 14:04:29 +0100767 /*
768 * Closing a group leader splits the group, and as we only disable
769 * group leaders, results in remaining events becoming enabled. To
770 * avoid arbitrary skew, we must read all counters before closing any
771 * group leaders.
772 */
773 read_counters();
774 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200775
Ingo Molnar42202dd2009-06-13 14:57:28 +0200776 return WEXITSTATUS(status);
777}
778
Jiri Olsae55c14a2018-04-23 11:08:21 +0200779static int run_perf_stat(int argc, const char **argv, int run_idx)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200780{
781 int ret;
782
783 if (pre_cmd) {
784 ret = system(pre_cmd);
785 if (ret)
786 return ret;
787 }
788
789 if (sync_run)
790 sync();
791
Jiri Olsae55c14a2018-04-23 11:08:21 +0200792 ret = __run_perf_stat(argc, argv, run_idx);
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200793 if (ret)
794 return ret;
795
796 if (post_cmd) {
797 ret = system(post_cmd);
798 if (ret)
799 return ret;
800 }
801
802 return ret;
803}
804
Andi Kleend73515c2015-03-11 07:16:27 -0700805static void print_running(u64 run, u64 ena)
806{
807 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200808 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700809 csv_sep,
810 run,
811 csv_sep,
812 ena ? 100.0 * run / ena : 100.0);
813 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200814 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700815 }
816}
817
Ingo Molnarf99844c2011-04-27 05:35:39 +0200818static void print_noise_pct(double total, double avg)
819{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800820 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200821
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700822 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200823 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600824 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200825 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200826}
827
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200828static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200829{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200830 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200831
Peter Zijlstra849abde2009-09-04 18:23:38 +0200832 if (run_count == 1)
833 return;
834
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300835 ps = evsel->stats;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200836 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200837}
838
Stephane Eranian12c08a92013-02-14 13:57:29 +0100839static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200840{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200841 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100842 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200843 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100844 cpu_map__id_to_socket(id),
845 csv_output ? 0 : -8,
846 cpu_map__id_to_cpu(id),
847 csv_sep,
848 csv_output ? 0 : 4,
849 nr,
850 csv_sep);
851 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100852 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200853 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100854 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100855 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100856 csv_sep,
857 csv_output ? 0 : 4,
858 nr,
859 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100860 break;
861 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200862 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200863 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100864 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100865 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200866 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200867 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200868 csv_output ? 0 : 16,
869 thread_map__comm(evsel->threads, id),
870 csv_output ? 0 : -8,
871 thread_map__pid(evsel->threads, id),
872 csv_sep);
873 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100874 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200875 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100876 default:
877 break;
878 }
879}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200880
Andi Kleen140aead2016-01-30 09:06:49 -0800881struct outstate {
882 FILE *fh;
883 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800884 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800885 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800886 int id, nr;
887 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800888};
889
890#define METRIC_LEN 35
891
892static void new_line_std(void *ctx)
893{
894 struct outstate *os = ctx;
895
896 os->newline = true;
897}
898
899static void do_new_line_std(struct outstate *os)
900{
901 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800902 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800903 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800904 if (stat_config.aggr_mode == AGGR_NONE)
905 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800906 fprintf(os->fh, " ");
907}
908
909static void print_metric_std(void *ctx, const char *color, const char *fmt,
910 const char *unit, double val)
911{
912 struct outstate *os = ctx;
913 FILE *out = os->fh;
914 int n;
915 bool newline = os->newline;
916
917 os->newline = false;
918
919 if (unit == NULL || fmt == NULL) {
920 fprintf(out, "%-*s", METRIC_LEN, "");
921 return;
922 }
923
924 if (newline)
925 do_new_line_std(os);
926
927 n = fprintf(out, " # ");
928 if (color)
929 n += color_fprintf(out, color, fmt, val);
930 else
931 n += fprintf(out, fmt, val);
932 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
933}
934
Andi Kleen92a61f62016-02-29 14:36:21 -0800935static void new_line_csv(void *ctx)
936{
937 struct outstate *os = ctx;
938 int i;
939
940 fputc('\n', os->fh);
941 if (os->prefix)
942 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800943 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800944 for (i = 0; i < os->nfields; i++)
945 fputs(csv_sep, os->fh);
946}
947
948static void print_metric_csv(void *ctx,
949 const char *color __maybe_unused,
950 const char *fmt, const char *unit, double val)
951{
952 struct outstate *os = ctx;
953 FILE *out = os->fh;
954 char buf[64], *vals, *ends;
955
956 if (unit == NULL || fmt == NULL) {
Ilya Pronin40c21892018-03-05 22:43:53 -0800957 fprintf(out, "%s%s", csv_sep, csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800958 return;
959 }
960 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900961 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800962 while (isdigit(*ends) || *ends == '.')
963 ends++;
964 *ends = 0;
965 while (isspace(*unit))
966 unit++;
967 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
968}
969
Andi Kleen54b50912016-03-03 15:57:36 -0800970#define METRIC_ONLY_LEN 20
971
972/* Filter out some columns that don't work well in metrics only mode */
973
974static bool valid_only_metric(const char *unit)
975{
976 if (!unit)
977 return false;
978 if (strstr(unit, "/sec") ||
979 strstr(unit, "hz") ||
980 strstr(unit, "Hz") ||
981 strstr(unit, "CPUs utilized"))
982 return false;
983 return true;
984}
985
986static const char *fixunit(char *buf, struct perf_evsel *evsel,
987 const char *unit)
988{
989 if (!strncmp(unit, "of all", 6)) {
990 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
991 unit);
992 return buf;
993 }
994 return unit;
995}
996
997static void print_metric_only(void *ctx, const char *color, const char *fmt,
998 const char *unit, double val)
999{
1000 struct outstate *os = ctx;
1001 FILE *out = os->fh;
1002 int n;
1003 char buf[1024];
1004 unsigned mlen = METRIC_ONLY_LEN;
1005
1006 if (!valid_only_metric(unit))
1007 return;
1008 unit = fixunit(buf, os->evsel, unit);
1009 if (color)
1010 n = color_fprintf(out, color, fmt, val);
1011 else
1012 n = fprintf(out, fmt, val);
1013 if (n > METRIC_ONLY_LEN)
1014 n = METRIC_ONLY_LEN;
1015 if (mlen < strlen(unit))
1016 mlen = strlen(unit) + 1;
1017 fprintf(out, "%*s", mlen - n, "");
1018}
1019
1020static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
1021 const char *fmt,
1022 const char *unit, double val)
1023{
1024 struct outstate *os = ctx;
1025 FILE *out = os->fh;
1026 char buf[64], *vals, *ends;
1027 char tbuf[1024];
1028
1029 if (!valid_only_metric(unit))
1030 return;
1031 unit = fixunit(tbuf, os->evsel, unit);
1032 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +09001033 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -08001034 while (isdigit(*ends) || *ends == '.')
1035 ends++;
1036 *ends = 0;
1037 fprintf(out, "%s%s", vals, csv_sep);
1038}
1039
1040static void new_line_metric(void *ctx __maybe_unused)
1041{
1042}
1043
1044static void print_metric_header(void *ctx, const char *color __maybe_unused,
1045 const char *fmt __maybe_unused,
1046 const char *unit, double val __maybe_unused)
1047{
1048 struct outstate *os = ctx;
1049 char tbuf[1024];
1050
1051 if (!valid_only_metric(unit))
1052 return;
1053 unit = fixunit(tbuf, os->evsel, unit);
1054 if (csv_output)
1055 fprintf(os->fh, "%s%s", unit, csv_sep);
1056 else
1057 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
1058}
1059
Andi Kleenda88c7f2014-09-24 13:50:46 -07001060static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001061{
Jiri Olsa58215222015-07-21 14:31:24 +02001062 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001063 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +01001064 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -06001065 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001066
Stephane Eranian410136f2013-11-12 17:58:49 +01001067 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
1068 fmt_n = csv_output ? "%s" : "%-25s";
1069
Andi Kleenda88c7f2014-09-24 13:50:46 -07001070 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001071
David Ahern4bbe5a62013-09-28 14:28:00 -06001072 scnprintf(name, sizeof(name), "%s%s",
1073 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +01001074
1075 fprintf(output, fmt_v, msecs, csv_sep);
1076
1077 if (csv_output)
1078 fprintf(output, "%s%s", evsel->unit, csv_sep);
1079 else
1080 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1081
1082 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001083
Stephane Eranian023695d2011-02-14 11:20:01 +02001084 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001085 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001086}
1087
Andi Kleen44d49a62016-02-29 14:36:22 -08001088static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1089{
1090 int i;
1091
1092 if (!aggr_get_id)
1093 return 0;
1094
1095 if (stat_config.aggr_mode == AGGR_NONE)
1096 return id;
1097
1098 if (stat_config.aggr_mode == AGGR_GLOBAL)
1099 return 0;
1100
1101 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1102 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1103
1104 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1105 return cpu2;
1106 }
1107 return 0;
1108}
1109
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001110static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1111{
Jiri Olsa58215222015-07-21 14:31:24 +02001112 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001113 double sc = evsel->scale;
1114 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001115
1116 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001117 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001118 } else {
1119 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001120 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001121 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001122 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001123 }
1124
1125 aggr_printout(evsel, id, nr);
1126
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001127 fprintf(output, fmt, avg, csv_sep);
1128
1129 if (evsel->unit)
1130 fprintf(output, "%-*s%s",
1131 csv_output ? 0 : unit_width,
1132 evsel->unit, csv_sep);
1133
1134 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1135
1136 if (evsel->cgrp)
1137 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001138}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001139
Kan Liang30060ea2018-04-24 11:20:11 -07001140static bool is_mixed_hw_group(struct perf_evsel *counter)
1141{
1142 struct perf_evlist *evlist = counter->evlist;
1143 u32 pmu_type = counter->attr.type;
1144 struct perf_evsel *pos;
1145
1146 if (counter->nr_members < 2)
1147 return false;
1148
1149 evlist__for_each_entry(evlist, pos) {
1150 /* software events can be part of any hardware group */
1151 if (pos->attr.type == PERF_TYPE_SOFTWARE)
1152 continue;
1153 if (pmu_type == PERF_TYPE_SOFTWARE) {
1154 pmu_type = pos->attr.type;
1155 continue;
1156 }
1157 if (pmu_type != pos->attr.type)
1158 return true;
1159 }
1160
1161 return false;
1162}
1163
Andi Kleenf9483392016-01-30 09:06:50 -08001164static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +08001165 char *prefix, u64 run, u64 ena, double noise,
1166 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001167{
Andi Kleen140aead2016-01-30 09:06:49 -08001168 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001169 struct outstate os = {
1170 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001171 .prefix = prefix ? prefix : "",
1172 .id = id,
1173 .nr = nr,
1174 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001175 };
Andi Kleen140aead2016-01-30 09:06:49 -08001176 print_metric_t pm = print_metric_std;
1177 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001178
Andi Kleen54b50912016-03-03 15:57:36 -08001179 if (metric_only) {
1180 nl = new_line_metric;
1181 if (csv_output)
1182 pm = print_metric_only_csv;
1183 else
1184 pm = print_metric_only;
1185 } else
1186 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001187
Andi Kleen54b50912016-03-03 15:57:36 -08001188 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001189 static int aggr_fields[] = {
1190 [AGGR_GLOBAL] = 0,
1191 [AGGR_THREAD] = 1,
1192 [AGGR_NONE] = 1,
1193 [AGGR_SOCKET] = 2,
1194 [AGGR_CORE] = 2,
1195 };
1196
1197 pm = print_metric_csv;
1198 nl = new_line_csv;
1199 os.nfields = 3;
1200 os.nfields += aggr_fields[stat_config.aggr_mode];
1201 if (counter->cgrp)
1202 os.nfields++;
1203 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001204 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001205 if (metric_only) {
1206 pm(&os, NULL, "", "", 0);
1207 return;
1208 }
Andi Kleencb110f42016-01-30 09:06:51 -08001209 aggr_printout(counter, id, nr);
1210
1211 fprintf(stat_config.output, "%*s%s",
1212 csv_output ? 0 : 18,
1213 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1214 csv_sep);
1215
Kan Liang30060ea2018-04-24 11:20:11 -07001216 if (counter->supported) {
Borislav Petkov02d492e2017-02-07 01:40:05 +01001217 print_free_counters_hint = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001218 if (is_mixed_hw_group(counter))
1219 print_mixed_hw_group_error = 1;
1220 }
Borislav Petkov02d492e2017-02-07 01:40:05 +01001221
Andi Kleencb110f42016-01-30 09:06:51 -08001222 fprintf(stat_config.output, "%-*s%s",
1223 csv_output ? 0 : unit_width,
1224 counter->unit, csv_sep);
1225
1226 fprintf(stat_config.output, "%*s",
1227 csv_output ? 0 : -25,
1228 perf_evsel__name(counter));
1229
1230 if (counter->cgrp)
1231 fprintf(stat_config.output, "%s%s",
1232 csv_sep, counter->cgrp->name);
1233
Andi Kleen92a61f62016-02-29 14:36:21 -08001234 if (!csv_output)
1235 pm(&os, NULL, NULL, "", 0);
1236 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001237 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001238 if (csv_output)
1239 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001240 return;
1241 }
1242
Andi Kleen54b50912016-03-03 15:57:36 -08001243 if (metric_only)
1244 /* nothing */;
1245 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001246 nsec_printout(id, nr, counter, uval);
1247 else
1248 abs_printout(id, nr, counter, uval);
1249
Andi Kleen140aead2016-01-30 09:06:49 -08001250 out.print_metric = pm;
1251 out.new_line = nl;
1252 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001253 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001254
Andi Kleen54b50912016-03-03 15:57:36 -08001255 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001256 print_noise(counter, noise);
1257 print_running(run, ena);
1258 }
1259
1260 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001261 first_shadow_cpu(counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001262 &out, &metric_events, st);
Andi Kleen54b50912016-03-03 15:57:36 -08001263 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001264 print_noise(counter, noise);
1265 print_running(run, ena);
1266 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001267}
1268
Andi Kleen44d49a62016-02-29 14:36:22 -08001269static void aggr_update_shadow(void)
1270{
1271 int cpu, s2, id, s;
1272 u64 val;
1273 struct perf_evsel *counter;
1274
1275 for (s = 0; s < aggr_map->nr; s++) {
1276 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001277 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001278 val = 0;
1279 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1280 s2 = aggr_get_id(evsel_list->cpus, cpu);
1281 if (s2 != id)
1282 continue;
1283 val += perf_counts(counter->counts, cpu, 0)->val;
1284 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001285 perf_stat__update_shadow_stats(counter, val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001286 first_shadow_cpu(counter, id),
1287 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001288 }
1289 }
1290}
1291
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001292static void uniquify_event_name(struct perf_evsel *counter)
1293{
1294 char *new_name;
1295 char *config;
1296
Kan Liang80ee8c52018-04-24 11:20:14 -07001297 if (counter->uniquified_name ||
1298 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001299 strlen(counter->pmu_name)))
1300 return;
1301
1302 config = strchr(counter->name, '/');
1303 if (config) {
1304 if (asprintf(&new_name,
1305 "%s%s", counter->pmu_name, config) > 0) {
1306 free(counter->name);
1307 counter->name = new_name;
1308 }
1309 } else {
1310 if (asprintf(&new_name,
1311 "%s [%s]", counter->name, counter->pmu_name) > 0) {
1312 free(counter->name);
1313 counter->name = new_name;
1314 }
1315 }
Kan Liang80ee8c52018-04-24 11:20:14 -07001316
1317 counter->uniquified_name = true;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001318}
1319
Andi Kleen430daf22017-03-20 13:17:00 -07001320static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001321 void (*cb)(struct perf_evsel *counter, void *data,
1322 bool first),
1323 void *data)
1324{
Andi Kleen430daf22017-03-20 13:17:00 -07001325 struct perf_evsel *alias;
1326
1327 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1328 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1329 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1330 alias->scale != counter->scale ||
1331 alias->cgrp != counter->cgrp ||
1332 strcmp(alias->unit, counter->unit) ||
1333 nsec_counter(alias) != nsec_counter(counter))
1334 break;
1335 alias->merged_stat = true;
1336 cb(alias, data, false);
1337 }
1338}
1339
1340static bool collect_data(struct perf_evsel *counter,
1341 void (*cb)(struct perf_evsel *counter, void *data,
1342 bool first),
1343 void *data)
1344{
1345 if (counter->merged_stat)
1346 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001347 cb(counter, data, true);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001348 if (no_merge)
1349 uniquify_event_name(counter);
1350 else if (counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001351 collect_all_aliases(counter, cb, data);
1352 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001353}
1354
1355struct aggr_data {
1356 u64 ena, run, val;
1357 int id;
1358 int nr;
1359 int cpu;
1360};
1361
1362static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1363{
1364 struct aggr_data *ad = data;
1365 int cpu, s2;
1366
1367 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1368 struct perf_counts_values *counts;
1369
1370 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1371 if (s2 != ad->id)
1372 continue;
1373 if (first)
1374 ad->nr++;
1375 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001376 /*
1377 * When any result is bad, make them all to give
1378 * consistent output in interval mode.
1379 */
1380 if (counts->ena == 0 || counts->run == 0 ||
1381 counter->counts->scaled == -1) {
1382 ad->ena = 0;
1383 ad->run = 0;
1384 break;
1385 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001386 ad->val += counts->val;
1387 ad->ena += counts->ena;
1388 ad->run += counts->run;
1389 }
1390}
1391
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001392static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001393{
Jiri Olsa58215222015-07-21 14:31:24 +02001394 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001395 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001396 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001397 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001398 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001399 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001400
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001401 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001402 return;
1403
Andi Kleen44d49a62016-02-29 14:36:22 -08001404 aggr_update_shadow();
1405
Andi Kleen54b50912016-03-03 15:57:36 -08001406 /*
1407 * With metric_only everything is on a single line.
1408 * Without each counter has its own line.
1409 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001410 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001411 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001412 if (prefix && metric_only)
1413 fprintf(output, "%s", prefix);
1414
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001415 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001416 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001417 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001418 if (is_duration_time(counter))
1419 continue;
1420
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001421 ad.val = ad.ena = ad.run = 0;
1422 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001423 if (!collect_data(counter, aggr_cb, &ad))
1424 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001425 nr = ad.nr;
1426 ena = ad.ena;
1427 run = ad.run;
1428 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001429 if (first && metric_only) {
1430 first = false;
1431 aggr_printout(counter, id, nr);
1432 }
1433 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001434 fprintf(output, "%s", prefix);
1435
Stephane Eranian410136f2013-11-12 17:58:49 +01001436 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001437 printout(id, nr, counter, uval, prefix, run, ena, 1.0,
1438 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001439 if (!metric_only)
1440 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001441 }
Andi Kleen54b50912016-03-03 15:57:36 -08001442 if (metric_only)
1443 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001444 }
1445}
1446
Jin Yao29734552017-12-05 22:03:11 +08001447static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001448{
Jin Yao29734552017-12-05 22:03:11 +08001449 return ((struct perf_aggr_thread_value *)b)->val -
1450 ((struct perf_aggr_thread_value *)a)->val;
1451}
1452
1453static struct perf_aggr_thread_value *sort_aggr_thread(
1454 struct perf_evsel *counter,
1455 int nthreads, int ncpus,
1456 int *ret)
1457{
1458 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001459 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001460 struct perf_aggr_thread_value *buf;
1461
1462 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1463 if (!buf)
1464 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001465
1466 for (thread = 0; thread < nthreads; thread++) {
1467 u64 ena = 0, run = 0, val = 0;
1468
1469 for (cpu = 0; cpu < ncpus; cpu++) {
1470 val += perf_counts(counter->counts, cpu, thread)->val;
1471 ena += perf_counts(counter->counts, cpu, thread)->ena;
1472 run += perf_counts(counter->counts, cpu, thread)->run;
1473 }
1474
Jin Yao29734552017-12-05 22:03:11 +08001475 uval = val * counter->scale;
1476
1477 /*
1478 * Skip value 0 when enabling --per-thread globally,
1479 * otherwise too many 0 output.
1480 */
1481 if (uval == 0.0 && target__has_per_thread(&target))
1482 continue;
1483
1484 buf[i].counter = counter;
1485 buf[i].id = thread;
1486 buf[i].uval = uval;
1487 buf[i].val = val;
1488 buf[i].run = run;
1489 buf[i].ena = ena;
1490 i++;
1491 }
1492
1493 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1494
1495 if (ret)
1496 *ret = i;
1497
1498 return buf;
1499}
1500
1501static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1502{
1503 FILE *output = stat_config.output;
1504 int nthreads = thread_map__nr(counter->threads);
1505 int ncpus = cpu_map__nr(counter->cpus);
1506 int thread, sorted_threads, id;
1507 struct perf_aggr_thread_value *buf;
1508
1509 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads);
1510 if (!buf) {
1511 perror("cannot sort aggr thread");
1512 return;
1513 }
1514
1515 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001516 if (prefix)
1517 fprintf(output, "%s", prefix);
1518
Jin Yao29734552017-12-05 22:03:11 +08001519 id = buf[thread].id;
Jin Yao14e72a22017-12-05 22:03:08 +08001520 if (stat_config.stats)
Jin Yao29734552017-12-05 22:03:11 +08001521 printout(id, 0, buf[thread].counter, buf[thread].uval,
1522 prefix, buf[thread].run, buf[thread].ena, 1.0,
1523 &stat_config.stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001524 else
Jin Yao29734552017-12-05 22:03:11 +08001525 printout(id, 0, buf[thread].counter, buf[thread].uval,
1526 prefix, buf[thread].run, buf[thread].ena, 1.0,
1527 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001528 fputc('\n', output);
1529 }
Jin Yao29734552017-12-05 22:03:11 +08001530
1531 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001532}
1533
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001534struct caggr_data {
1535 double avg, avg_enabled, avg_running;
1536};
1537
1538static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1539 bool first __maybe_unused)
1540{
1541 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001542 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001543
1544 cd->avg += avg_stats(&ps->res_stats[0]);
1545 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1546 cd->avg_running += avg_stats(&ps->res_stats[2]);
1547}
1548
Ingo Molnar42202dd2009-06-13 14:57:28 +02001549/*
1550 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001551 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001552 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001553static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001554{
Jiri Olsa58215222015-07-21 14:31:24 +02001555 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001556 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001557 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001558
Andi Kleen430daf22017-03-20 13:17:00 -07001559 if (!collect_data(counter, counter_aggr_cb, &cd))
1560 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001561
Andi Kleen54b50912016-03-03 15:57:36 -08001562 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001563 fprintf(output, "%s", prefix);
1564
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001565 uval = cd.avg * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001566 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
1567 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001568 if (!metric_only)
1569 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001570}
1571
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001572static void counter_cb(struct perf_evsel *counter, void *data,
1573 bool first __maybe_unused)
1574{
1575 struct aggr_data *ad = data;
1576
1577 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1578 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1579 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1580}
1581
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001582/*
1583 * Print out the results of a single counter:
1584 * does not use aggregated count in system-wide
1585 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001586static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001587{
Jiri Olsa58215222015-07-21 14:31:24 +02001588 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001589 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001590 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001591 int cpu;
1592
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001593 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001594 struct aggr_data ad = { .cpu = cpu };
1595
Andi Kleen430daf22017-03-20 13:17:00 -07001596 if (!collect_data(counter, counter_cb, &ad))
1597 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001598 val = ad.val;
1599 ena = ad.ena;
1600 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001601
1602 if (prefix)
1603 fprintf(output, "%s", prefix);
1604
Stephane Eranian410136f2013-11-12 17:58:49 +01001605 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001606 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1607 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001608
Stephane Eranian4aa90152011-08-15 22:22:33 +02001609 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001610 }
1611}
1612
Andi Kleen206cab62016-03-03 15:57:37 -08001613static void print_no_aggr_metric(char *prefix)
1614{
1615 int cpu;
1616 int nrcpus = 0;
1617 struct perf_evsel *counter;
1618 u64 ena, run, val;
1619 double uval;
1620
1621 nrcpus = evsel_list->cpus->nr;
1622 for (cpu = 0; cpu < nrcpus; cpu++) {
1623 bool first = true;
1624
1625 if (prefix)
1626 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001627 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001628 if (is_duration_time(counter))
1629 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001630 if (first) {
1631 aggr_printout(counter, cpu, 0);
1632 first = false;
1633 }
1634 val = perf_counts(counter->counts, cpu, 0)->val;
1635 ena = perf_counts(counter->counts, cpu, 0)->ena;
1636 run = perf_counts(counter->counts, cpu, 0)->run;
1637
1638 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001639 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1640 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001641 }
1642 fputc('\n', stat_config.output);
1643 }
1644}
1645
Andi Kleen54b50912016-03-03 15:57:36 -08001646static int aggr_header_lens[] = {
1647 [AGGR_CORE] = 18,
1648 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001649 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001650 [AGGR_THREAD] = 24,
1651 [AGGR_GLOBAL] = 0,
1652};
1653
Andi Kleenc51fd632016-05-24 12:52:39 -07001654static const char *aggr_header_csv[] = {
1655 [AGGR_CORE] = "core,cpus,",
1656 [AGGR_SOCKET] = "socket,cpus",
1657 [AGGR_NONE] = "cpu,",
1658 [AGGR_THREAD] = "comm-pid,",
1659 [AGGR_GLOBAL] = ""
1660};
1661
Andi Kleen41c8ca22016-05-24 12:52:38 -07001662static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001663{
1664 struct perf_stat_output_ctx out;
1665 struct perf_evsel *counter;
1666 struct outstate os = {
1667 .fh = stat_config.output
1668 };
1669
1670 if (prefix)
1671 fprintf(stat_config.output, "%s", prefix);
1672
Andi Kleen41c8ca22016-05-24 12:52:38 -07001673 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001674 fprintf(stat_config.output, "%*s",
1675 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001676 if (csv_output) {
1677 if (stat_config.interval)
1678 fputs("time,", stat_config.output);
1679 fputs(aggr_header_csv[stat_config.aggr_mode],
1680 stat_config.output);
1681 }
Andi Kleen54b50912016-03-03 15:57:36 -08001682
1683 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001684 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001685 if (is_duration_time(counter))
1686 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001687 os.evsel = counter;
1688 out.ctx = &os;
1689 out.print_metric = print_metric_header;
1690 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001691 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001692 os.evsel = counter;
1693 perf_stat__print_shadow_stats(counter, 0,
1694 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001695 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001696 &metric_events,
1697 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001698 }
1699 fputc('\n', stat_config.output);
1700}
1701
Jiri Olsad4f63a42015-06-26 11:29:26 +02001702static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001703{
Jiri Olsa58215222015-07-21 14:31:24 +02001704 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001705 static int num_print_interval;
1706
1707 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1708
Andi Kleen41c8ca22016-05-24 12:52:38 -07001709 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001710 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001711 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001712 fprintf(output, "# time socket 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_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001717 fprintf(output, "# time core cpus");
1718 if (!metric_only)
1719 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001720 break;
1721 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001722 fprintf(output, "# time CPU");
1723 if (!metric_only)
1724 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001725 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001726 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001727 fprintf(output, "# time comm-pid");
1728 if (!metric_only)
1729 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001730 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001731 case AGGR_GLOBAL:
1732 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001733 fprintf(output, "# time");
1734 if (!metric_only)
1735 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001736 case AGGR_UNSET:
1737 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001738 }
1739 }
1740
Andi Kleen41c8ca22016-05-24 12:52:38 -07001741 if (num_print_interval == 0 && metric_only)
1742 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001743 if (++num_print_interval == 25)
1744 num_print_interval = 0;
1745}
1746
1747static void print_header(int argc, const char **argv)
1748{
Jiri Olsa58215222015-07-21 14:31:24 +02001749 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001750 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001751
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001752 fflush(stdout);
1753
Stephane Eraniand7470b62010-12-01 18:49:05 +02001754 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001755 fprintf(output, "\n");
1756 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001757 if (target.system_wide)
1758 fprintf(output, "\'system wide");
1759 else if (target.cpu_list)
1760 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001761 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001762 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1763 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001764 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001765 } else if (target.pid)
1766 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001767 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001768 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001769
Stephane Eranian4aa90152011-08-15 22:22:33 +02001770 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001771 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001772 fprintf(output, " (%d runs)", run_count);
1773 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001774 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001775}
1776
Jiri Olsabc22de92018-04-23 11:08:20 +02001777static int get_precision(double num)
1778{
1779 if (num > 1)
1780 return 0;
1781
1782 return lround(ceil(-log10(num)));
1783}
1784
Jiri Olsae55c14a2018-04-23 11:08:21 +02001785static void print_table(FILE *output, int precision, double avg)
1786{
1787 char tmp[64];
1788 int idx, indent = 0;
1789
1790 scnprintf(tmp, 64, " %17.*f", precision, avg);
1791 while (tmp[indent] == ' ')
1792 indent++;
1793
1794 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1795
1796 for (idx = 0; idx < run_count; idx++) {
1797 double run = (double) walltime_run[idx] / NSEC_PER_SEC;
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001798 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001799
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001800 fprintf(output, " %17.*f (%+.*f) ",
Jiri Olsae55c14a2018-04-23 11:08:21 +02001801 precision, run, precision, run - avg);
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001802
1803 for (h = 0; h < n; h++)
1804 fprintf(output, "#");
1805
1806 fprintf(output, "\n");
Jiri Olsae55c14a2018-04-23 11:08:21 +02001807 }
1808
1809 fprintf(output, "\n%*s# Final result:\n", indent, "");
1810}
1811
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001812static double timeval2double(struct timeval *t)
1813{
1814 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1815}
1816
Jiri Olsad4f63a42015-06-26 11:29:26 +02001817static void print_footer(void)
1818{
Jiri Olsabc22de92018-04-23 11:08:20 +02001819 double avg = avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsa58215222015-07-21 14:31:24 +02001820 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001821 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001822
Jiri Olsad4f63a42015-06-26 11:29:26 +02001823 if (!null_run)
1824 fprintf(output, "\n");
Jiri Olsabc22de92018-04-23 11:08:20 +02001825
1826 if (run_count == 1) {
1827 fprintf(output, " %17.9f seconds time elapsed", avg);
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001828
1829 if (ru_display) {
1830 double ru_utime = timeval2double(&ru_data.ru_utime);
1831 double ru_stime = timeval2double(&ru_data.ru_stime);
1832
1833 fprintf(output, "\n\n");
1834 fprintf(output, " %17.9f seconds user\n", ru_utime);
1835 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1836 }
Jiri Olsabc22de92018-04-23 11:08:20 +02001837 } else {
1838 double sd = stddev_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
1839 /*
1840 * Display at most 2 more significant
1841 * digits than the stddev inaccuracy.
1842 */
1843 int precision = get_precision(sd) + 2;
1844
Jiri Olsae55c14a2018-04-23 11:08:21 +02001845 if (walltime_run_table)
1846 print_table(output, precision, avg);
1847
Jiri Olsabc22de92018-04-23 11:08:20 +02001848 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1849 precision, avg, precision, sd);
1850
1851 print_noise_pct(sd, avg);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001852 }
1853 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001854
Andi Kleen918c7b062017-05-22 18:00:16 -07001855 if (print_free_counters_hint &&
1856 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1857 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001858 fprintf(output,
1859"Some events weren't counted. Try disabling the NMI watchdog:\n"
1860" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1861" perf stat ...\n"
1862" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001863
1864 if (print_mixed_hw_group_error)
1865 fprintf(output,
1866 "The events in group usually have to be from "
1867 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001868}
1869
1870static void print_counters(struct timespec *ts, int argc, const char **argv)
1871{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001872 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001873 struct perf_evsel *counter;
1874 char buf[64], *prefix = NULL;
1875
Jiri Olsa664c98d2015-11-05 15:40:50 +01001876 /* Do not print anything if we record to the pipe. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001877 if (STAT_RECORD && perf_stat.data.is_pipe)
Jiri Olsa664c98d2015-11-05 15:40:50 +01001878 return;
1879
Jiri Olsad4f63a42015-06-26 11:29:26 +02001880 if (interval)
1881 print_interval(prefix = buf, ts);
1882 else
1883 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001884
Andi Kleen54b50912016-03-03 15:57:36 -08001885 if (metric_only) {
1886 static int num_print_iv;
1887
Andi Kleen41c8ca22016-05-24 12:52:38 -07001888 if (num_print_iv == 0 && !interval)
1889 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001890 if (num_print_iv++ == 25)
1891 num_print_iv = 0;
1892 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1893 fprintf(stat_config.output, "%s", prefix);
1894 }
1895
Jiri Olsa421a50f2015-07-21 14:31:22 +02001896 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001897 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001898 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001899 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001900 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001901 case AGGR_THREAD:
Andi Kleene864c5c2017-08-31 12:40:35 -07001902 evlist__for_each_entry(evsel_list, counter) {
1903 if (is_duration_time(counter))
1904 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001905 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001906 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001907 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001908 case AGGR_GLOBAL:
Andi Kleene864c5c2017-08-31 12:40:35 -07001909 evlist__for_each_entry(evsel_list, counter) {
1910 if (is_duration_time(counter))
1911 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001912 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001913 }
Andi Kleen54b50912016-03-03 15:57:36 -08001914 if (metric_only)
1915 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001916 break;
1917 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001918 if (metric_only)
1919 print_no_aggr_metric(prefix);
1920 else {
Andi Kleene864c5c2017-08-31 12:40:35 -07001921 evlist__for_each_entry(evsel_list, counter) {
1922 if (is_duration_time(counter))
1923 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001924 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001925 }
Andi Kleen206cab62016-03-03 15:57:37 -08001926 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001927 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001928 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001929 default:
1930 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001931 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001932
Jiri Olsad4f63a42015-06-26 11:29:26 +02001933 if (!interval && !csv_output)
1934 print_footer();
1935
Jiri Olsa58215222015-07-21 14:31:24 +02001936 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001937}
1938
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001939static volatile int signr = -1;
1940
Ingo Molnar52425192009-05-26 09:17:18 +02001941static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001942{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001943 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001944 done = 1;
1945
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001946 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001947 /*
1948 * render child_pid harmless
1949 * won't send SIGTERM to a random
1950 * process in case of race condition
1951 * and fast PID recycling
1952 */
1953 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001954}
1955
1956static void sig_atexit(void)
1957{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001958 sigset_t set, oset;
1959
1960 /*
1961 * avoid race condition with SIGCHLD handler
1962 * in skip_signal() which is modifying child_pid
1963 * goal is to avoid send SIGTERM to a random
1964 * process
1965 */
1966 sigemptyset(&set);
1967 sigaddset(&set, SIGCHLD);
1968 sigprocmask(SIG_BLOCK, &set, &oset);
1969
Chris Wilson933da832009-10-04 01:35:01 +01001970 if (child_pid != -1)
1971 kill(child_pid, SIGTERM);
1972
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001973 sigprocmask(SIG_SETMASK, &oset, NULL);
1974
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001975 if (signr == -1)
1976 return;
1977
1978 signal(signr, SIG_DFL);
1979 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001980}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001981
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001982static int stat__set_big_num(const struct option *opt __maybe_unused,
1983 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001984{
1985 big_num_opt = unset ? 0 : 1;
1986 return 0;
1987}
1988
Andi Kleen44b1e602016-05-30 12:49:42 -03001989static int enable_metric_only(const struct option *opt __maybe_unused,
1990 const char *s __maybe_unused, int unset)
1991{
1992 force_metric_only = true;
1993 metric_only = !unset;
1994 return 0;
1995}
1996
Andi Kleenb18f3e32017-08-31 12:40:31 -07001997static int parse_metric_groups(const struct option *opt,
1998 const char *str,
1999 int unset __maybe_unused)
2000{
2001 return metricgroup__parse_groups(opt, str, &metric_events);
2002}
2003
Jiri Olsae0547312015-11-05 15:40:45 +01002004static const struct option stat_options[] = {
2005 OPT_BOOLEAN('T', "transaction", &transaction_run,
2006 "hardware transaction statistics"),
2007 OPT_CALLBACK('e', "event", &evsel_list, "event",
2008 "event selector. use 'perf list' to list available events",
2009 parse_events_option),
2010 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
2011 "event filter", parse_filter),
2012 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
2013 "child tasks do not inherit counters"),
2014 OPT_STRING('p', "pid", &target.pid, "pid",
2015 "stat events on existing process id"),
2016 OPT_STRING('t', "tid", &target.tid, "tid",
2017 "stat events on existing thread id"),
2018 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
2019 "system-wide collection from all CPUs"),
2020 OPT_BOOLEAN('g', "group", &group,
2021 "put the counters into a counter group"),
2022 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
2023 OPT_INCR('v', "verbose", &verbose,
2024 "be more verbose (show counter open errors, etc)"),
2025 OPT_INTEGER('r', "repeat", &run_count,
2026 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsae55c14a2018-04-23 11:08:21 +02002027 OPT_BOOLEAN(0, "table", &walltime_run_table,
2028 "display details about each run (only with -r option)"),
Jiri Olsae0547312015-11-05 15:40:45 +01002029 OPT_BOOLEAN('n', "null", &null_run,
2030 "null run - dont start any counters"),
2031 OPT_INCR('d', "detailed", &detailed_run,
2032 "detailed run - start a lot of events"),
2033 OPT_BOOLEAN('S', "sync", &sync_run,
2034 "call sync() before starting a run"),
2035 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
2036 "print large numbers with thousands\' separators",
2037 stat__set_big_num),
2038 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
2039 "list of cpus to monitor in system-wide"),
2040 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
2041 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07002042 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01002043 OPT_STRING('x', "field-separator", &csv_sep, "separator",
2044 "print counts with custom separator"),
2045 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
2046 "monitor event in cgroup name only", parse_cgroups),
2047 OPT_STRING('o', "output", &output_name, "file", "output file name"),
2048 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
2049 OPT_INTEGER(0, "log-fd", &output_fd,
2050 "log output to fd, instead of stderr"),
2051 OPT_STRING(0, "pre", &pre_cmd, "command",
2052 "command to run prior to the measured command"),
2053 OPT_STRING(0, "post", &post_cmd, "command",
2054 "command to run after to the measured command"),
2055 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03002056 "print counts at regular interval in ms "
2057 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01002058 OPT_INTEGER(0, "interval-count", &stat_config.times,
2059 "print counts for fixed number of times"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002060 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
2061 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01002062 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
2063 "aggregate counts per processor socket", AGGR_SOCKET),
2064 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
2065 "aggregate counts per physical processor core", AGGR_CORE),
2066 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
2067 "aggregate counts per thread", AGGR_THREAD),
2068 OPT_UINTEGER('D', "delay", &initial_delay,
2069 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03002070 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
2071 "Only print computed metrics. No raw values", enable_metric_only),
2072 OPT_BOOLEAN(0, "topdown", &topdown_run,
2073 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07002074 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
2075 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07002076 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
2077 "monitor specified metrics or metric groups (separated by ,)",
2078 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01002079 OPT_END()
2080};
2081
Jiri Olsa1fe7a302015-10-16 12:41:15 +02002082static int perf_stat__get_socket(struct cpu_map *map, int cpu)
2083{
2084 return cpu_map__get_socket(map, cpu, NULL);
2085}
2086
2087static int perf_stat__get_core(struct cpu_map *map, int cpu)
2088{
2089 return cpu_map__get_core(map, cpu, NULL);
2090}
2091
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002092static int cpu_map__get_max(struct cpu_map *map)
2093{
2094 int i, max = -1;
2095
2096 for (i = 0; i < map->nr; i++) {
2097 if (map->map[i] > max)
2098 max = map->map[i];
2099 }
2100
2101 return max;
2102}
2103
2104static struct cpu_map *cpus_aggr_map;
2105
2106static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
2107{
2108 int cpu;
2109
2110 if (idx >= map->nr)
2111 return -1;
2112
2113 cpu = map->map[idx];
2114
2115 if (cpus_aggr_map->map[cpu] == -1)
2116 cpus_aggr_map->map[cpu] = get_id(map, idx);
2117
2118 return cpus_aggr_map->map[cpu];
2119}
2120
2121static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
2122{
2123 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
2124}
2125
2126static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
2127{
2128 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
2129}
2130
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002131static int perf_stat_init_aggr_mode(void)
2132{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002133 int nr;
2134
Jiri Olsa421a50f2015-07-21 14:31:22 +02002135 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002136 case AGGR_SOCKET:
2137 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
2138 perror("cannot build socket map");
2139 return -1;
2140 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002141 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002142 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002143 case AGGR_CORE:
2144 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
2145 perror("cannot build core map");
2146 return -1;
2147 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002148 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002149 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002150 case AGGR_NONE:
2151 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002152 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002153 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002154 default:
2155 break;
2156 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002157
2158 /*
2159 * The evsel_list->cpus is the base we operate on,
2160 * taking the highest cpu number to be the size of
2161 * the aggregation translate cpumap.
2162 */
2163 nr = cpu_map__get_max(evsel_list->cpus);
2164 cpus_aggr_map = cpu_map__empty_new(nr + 1);
2165 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002166}
2167
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002168static void perf_stat__exit_aggr_mode(void)
2169{
2170 cpu_map__put(aggr_map);
2171 cpu_map__put(cpus_aggr_map);
2172 aggr_map = NULL;
2173 cpus_aggr_map = NULL;
2174}
2175
Jiri Olsa68d702f2015-11-05 15:40:58 +01002176static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2177{
2178 int cpu;
2179
2180 if (idx > map->nr)
2181 return -1;
2182
2183 cpu = map->map[idx];
2184
Jan Stancekda8a58b2017-02-17 12:10:26 +01002185 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002186 return -1;
2187
2188 return cpu;
2189}
2190
2191static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2192{
2193 struct perf_env *env = data;
2194 int cpu = perf_env__get_cpu(env, map, idx);
2195
2196 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2197}
2198
2199static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2200{
2201 struct perf_env *env = data;
2202 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2203
2204 if (cpu != -1) {
2205 int socket_id = env->cpu[cpu].socket_id;
2206
2207 /*
2208 * Encode socket in upper 16 bits
2209 * core_id is relative to socket, and
2210 * we need a global id. So we combine
2211 * socket + core id.
2212 */
2213 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2214 }
2215
2216 return core;
2217}
2218
2219static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2220 struct cpu_map **sockp)
2221{
2222 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2223}
2224
2225static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2226 struct cpu_map **corep)
2227{
2228 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2229}
2230
2231static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2232{
2233 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2234}
2235
2236static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2237{
2238 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2239}
2240
2241static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2242{
2243 struct perf_env *env = &st->session->header.env;
2244
2245 switch (stat_config.aggr_mode) {
2246 case AGGR_SOCKET:
2247 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2248 perror("cannot build socket map");
2249 return -1;
2250 }
2251 aggr_get_id = perf_stat__get_socket_file;
2252 break;
2253 case AGGR_CORE:
2254 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2255 perror("cannot build core map");
2256 return -1;
2257 }
2258 aggr_get_id = perf_stat__get_core_file;
2259 break;
2260 case AGGR_NONE:
2261 case AGGR_GLOBAL:
2262 case AGGR_THREAD:
2263 case AGGR_UNSET:
2264 default:
2265 break;
2266 }
2267
2268 return 0;
2269}
2270
Andi Kleen44b1e602016-05-30 12:49:42 -03002271static int topdown_filter_events(const char **attr, char **str, bool use_group)
2272{
2273 int off = 0;
2274 int i;
2275 int len = 0;
2276 char *s;
2277
2278 for (i = 0; attr[i]; i++) {
2279 if (pmu_have_event("cpu", attr[i])) {
2280 len += strlen(attr[i]) + 1;
2281 attr[i - off] = attr[i];
2282 } else
2283 off++;
2284 }
2285 attr[i - off] = NULL;
2286
2287 *str = malloc(len + 1 + 2);
2288 if (!*str)
2289 return -1;
2290 s = *str;
2291 if (i - off == 0) {
2292 *s = 0;
2293 return 0;
2294 }
2295 if (use_group)
2296 *s++ = '{';
2297 for (i = 0; attr[i]; i++) {
2298 strcpy(s, attr[i]);
2299 s += strlen(s);
2300 *s++ = ',';
2301 }
2302 if (use_group) {
2303 s[-1] = '}';
2304 *s = 0;
2305 } else
2306 s[-1] = 0;
2307 return 0;
2308}
2309
2310__weak bool arch_topdown_check_group(bool *warn)
2311{
2312 *warn = false;
2313 return false;
2314}
2315
2316__weak void arch_topdown_group_warn(void)
2317{
2318}
2319
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002320/*
2321 * Add default attributes, if there were no attributes specified or
2322 * if -d/--detailed, -d -d or -d -d -d is used:
2323 */
2324static int add_default_attributes(void)
2325{
Andi Kleen44b1e602016-05-30 12:49:42 -03002326 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002327 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002328
2329 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2330 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2331 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2332 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2333
2334 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002335};
2336 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002337 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002338};
2339 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002340 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002341};
2342 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002343 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2344 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2345 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2346
2347};
2348
2349/*
2350 * Detailed stats (-d), covering the L1 and last level data caches:
2351 */
2352 struct perf_event_attr detailed_attrs[] = {
2353
2354 { .type = PERF_TYPE_HW_CACHE,
2355 .config =
2356 PERF_COUNT_HW_CACHE_L1D << 0 |
2357 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2358 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2359
2360 { .type = PERF_TYPE_HW_CACHE,
2361 .config =
2362 PERF_COUNT_HW_CACHE_L1D << 0 |
2363 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2364 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2365
2366 { .type = PERF_TYPE_HW_CACHE,
2367 .config =
2368 PERF_COUNT_HW_CACHE_LL << 0 |
2369 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2370 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2371
2372 { .type = PERF_TYPE_HW_CACHE,
2373 .config =
2374 PERF_COUNT_HW_CACHE_LL << 0 |
2375 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2376 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2377};
2378
2379/*
2380 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2381 */
2382 struct perf_event_attr very_detailed_attrs[] = {
2383
2384 { .type = PERF_TYPE_HW_CACHE,
2385 .config =
2386 PERF_COUNT_HW_CACHE_L1I << 0 |
2387 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2388 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2389
2390 { .type = PERF_TYPE_HW_CACHE,
2391 .config =
2392 PERF_COUNT_HW_CACHE_L1I << 0 |
2393 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2394 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2395
2396 { .type = PERF_TYPE_HW_CACHE,
2397 .config =
2398 PERF_COUNT_HW_CACHE_DTLB << 0 |
2399 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2400 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2401
2402 { .type = PERF_TYPE_HW_CACHE,
2403 .config =
2404 PERF_COUNT_HW_CACHE_DTLB << 0 |
2405 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2406 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2407
2408 { .type = PERF_TYPE_HW_CACHE,
2409 .config =
2410 PERF_COUNT_HW_CACHE_ITLB << 0 |
2411 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2412 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2413
2414 { .type = PERF_TYPE_HW_CACHE,
2415 .config =
2416 PERF_COUNT_HW_CACHE_ITLB << 0 |
2417 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2418 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2419
2420};
2421
2422/*
2423 * Very, very detailed stats (-d -d -d), adding prefetch events:
2424 */
2425 struct perf_event_attr very_very_detailed_attrs[] = {
2426
2427 { .type = PERF_TYPE_HW_CACHE,
2428 .config =
2429 PERF_COUNT_HW_CACHE_L1D << 0 |
2430 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2431 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2432
2433 { .type = PERF_TYPE_HW_CACHE,
2434 .config =
2435 PERF_COUNT_HW_CACHE_L1D << 0 |
2436 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2437 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2438};
2439
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002440 /* Set attrs if no event is selected and !null_run: */
2441 if (null_run)
2442 return 0;
2443
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002444 if (transaction_run) {
Thomas Richterfca323402018-03-08 15:57:35 +01002445 struct parse_events_error errinfo;
2446
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002447 if (pmu_have_event("cpu", "cycles-ct") &&
2448 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002449 err = parse_events(evsel_list, transaction_attrs,
2450 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002451 else
Thomas Richterfca323402018-03-08 15:57:35 +01002452 err = parse_events(evsel_list,
2453 transaction_limited_attrs,
2454 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002455 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002456 fprintf(stderr, "Cannot set up transaction events\n");
2457 return -1;
2458 }
2459 return 0;
2460 }
2461
Kan Liangdaefd0b2017-05-26 12:05:38 -07002462 if (smi_cost) {
2463 int smi;
2464
2465 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2466 fprintf(stderr, "freeze_on_smi is not supported.\n");
2467 return -1;
2468 }
2469
2470 if (!smi) {
2471 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2472 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2473 return -1;
2474 }
2475 smi_reset = true;
2476 }
2477
2478 if (pmu_have_event("msr", "aperf") &&
2479 pmu_have_event("msr", "smi")) {
2480 if (!force_metric_only)
2481 metric_only = true;
2482 err = parse_events(evsel_list, smi_cost_attrs, NULL);
2483 } else {
2484 fprintf(stderr, "To measure SMI cost, it needs "
2485 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2486 return -1;
2487 }
2488 if (err) {
2489 fprintf(stderr, "Cannot set up SMI cost events\n");
2490 return -1;
2491 }
2492 return 0;
2493 }
2494
Andi Kleen44b1e602016-05-30 12:49:42 -03002495 if (topdown_run) {
2496 char *str = NULL;
2497 bool warn = false;
2498
2499 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2500 stat_config.aggr_mode != AGGR_CORE) {
2501 pr_err("top down event configuration requires --per-core mode\n");
2502 return -1;
2503 }
2504 stat_config.aggr_mode = AGGR_CORE;
2505 if (nr_cgroups || !target__has_cpu(&target)) {
2506 pr_err("top down event configuration requires system-wide mode (-a)\n");
2507 return -1;
2508 }
2509
2510 if (!force_metric_only)
2511 metric_only = true;
2512 if (topdown_filter_events(topdown_attrs, &str,
2513 arch_topdown_check_group(&warn)) < 0) {
2514 pr_err("Out of memory\n");
2515 return -1;
2516 }
2517 if (topdown_attrs[0] && str) {
2518 if (warn)
2519 arch_topdown_group_warn();
2520 err = parse_events(evsel_list, str, NULL);
2521 if (err) {
2522 fprintf(stderr,
2523 "Cannot set up top down events %s: %d\n",
2524 str, err);
2525 free(str);
2526 return -1;
2527 }
2528 } else {
2529 fprintf(stderr, "System does not support topdown\n");
2530 return -1;
2531 }
2532 free(str);
2533 }
2534
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002535 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002536 if (target__has_cpu(&target))
2537 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2538
Andi Kleen9dec4472016-02-26 16:27:56 -08002539 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2540 return -1;
2541 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2542 if (perf_evlist__add_default_attrs(evsel_list,
2543 frontend_attrs) < 0)
2544 return -1;
2545 }
2546 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2547 if (perf_evlist__add_default_attrs(evsel_list,
2548 backend_attrs) < 0)
2549 return -1;
2550 }
2551 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002552 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002553 }
2554
2555 /* Detailed events get appended to the event list: */
2556
2557 if (detailed_run < 1)
2558 return 0;
2559
2560 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002561 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002562 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002563
2564 if (detailed_run < 2)
2565 return 0;
2566
2567 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002568 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002569 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002570
2571 if (detailed_run < 3)
2572 return 0;
2573
2574 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002575 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002576}
2577
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002578static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002579 "perf stat record [<options>]",
2580 NULL,
2581};
2582
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002583static void init_features(struct perf_session *session)
2584{
2585 int feat;
2586
2587 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2588 perf_header__set_feat(&session->header, feat);
2589
2590 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2591 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2592 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2593 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2594}
2595
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002596static int __cmd_record(int argc, const char **argv)
2597{
2598 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002599 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002600
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002601 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002602 PARSE_OPT_STOP_AT_NON_OPTION);
2603
2604 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002605 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002606
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002607 if (run_count != 1 || forever) {
2608 pr_err("Cannot use -r option with perf stat record.\n");
2609 return -1;
2610 }
2611
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002612 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002613 if (session == NULL) {
2614 pr_err("Perf session creation failed.\n");
2615 return -1;
2616 }
2617
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002618 init_features(session);
2619
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002620 session->evlist = evsel_list;
2621 perf_stat.session = session;
2622 perf_stat.record = true;
2623 return argc;
2624}
2625
Jiri Olsaa56f9392015-11-05 15:40:59 +01002626static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2627 union perf_event *event,
2628 struct perf_session *session)
2629{
Andi Kleene3b03b62016-05-05 16:04:03 -07002630 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002631 struct perf_evsel *counter;
2632 struct timespec tsh, *ts = NULL;
2633 const char **argv = session->header.env.cmdline_argv;
2634 int argc = session->header.env.nr_cmdline;
2635
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002636 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002637 perf_stat_process_counter(&stat_config, counter);
2638
Andi Kleene3b03b62016-05-05 16:04:03 -07002639 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2640 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002641
Andi Kleene3b03b62016-05-05 16:04:03 -07002642 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002643 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2644 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002645 ts = &tsh;
2646 }
2647
2648 print_counters(ts, argc, argv);
2649 return 0;
2650}
2651
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002652static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002653int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002654 union perf_event *event,
2655 struct perf_session *session __maybe_unused)
2656{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002657 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2658
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002659 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002660
Jiri Olsa89af4e02015-11-05 15:41:02 +01002661 if (cpu_map__empty(st->cpus)) {
2662 if (st->aggr_mode != AGGR_UNSET)
2663 pr_warning("warning: processing task data, aggregation mode not set\n");
2664 return 0;
2665 }
2666
2667 if (st->aggr_mode != AGGR_UNSET)
2668 stat_config.aggr_mode = st->aggr_mode;
2669
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002670 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002671 perf_stat_init_aggr_mode();
2672 else
2673 perf_stat_init_aggr_mode_file(st);
2674
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002675 return 0;
2676}
2677
Jiri Olsa1975d362015-11-05 15:40:56 +01002678static int set_maps(struct perf_stat *st)
2679{
2680 if (!st->cpus || !st->threads)
2681 return 0;
2682
2683 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2684 return -EINVAL;
2685
2686 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2687
2688 if (perf_evlist__alloc_stats(evsel_list, true))
2689 return -ENOMEM;
2690
2691 st->maps_allocated = true;
2692 return 0;
2693}
2694
2695static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002696int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002697 union perf_event *event,
2698 struct perf_session *session __maybe_unused)
2699{
2700 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2701
2702 if (st->threads) {
2703 pr_warning("Extra thread map event, ignoring.\n");
2704 return 0;
2705 }
2706
2707 st->threads = thread_map__new_event(&event->thread_map);
2708 if (!st->threads)
2709 return -ENOMEM;
2710
2711 return set_maps(st);
2712}
2713
2714static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002715int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002716 union perf_event *event,
2717 struct perf_session *session __maybe_unused)
2718{
2719 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2720 struct cpu_map *cpus;
2721
2722 if (st->cpus) {
2723 pr_warning("Extra cpu map event, ignoring.\n");
2724 return 0;
2725 }
2726
2727 cpus = cpu_map__new_data(&event->cpu_map.data);
2728 if (!cpus)
2729 return -ENOMEM;
2730
2731 st->cpus = cpus;
2732 return set_maps(st);
2733}
2734
Jin Yao56739442017-12-05 22:03:07 +08002735static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2736{
2737 int i;
2738
2739 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2740 if (!config->stats)
2741 return -1;
2742
2743 config->stats_num = nthreads;
2744
2745 for (i = 0; i < nthreads; i++)
2746 runtime_stat__init(&config->stats[i]);
2747
2748 return 0;
2749}
2750
2751static void runtime_stat_delete(struct perf_stat_config *config)
2752{
2753 int i;
2754
2755 if (!config->stats)
2756 return;
2757
2758 for (i = 0; i < config->stats_num; i++)
2759 runtime_stat__exit(&config->stats[i]);
2760
2761 free(config->stats);
2762}
2763
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002764static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002765 "perf stat report [<options>]",
2766 NULL,
2767};
2768
2769static struct perf_stat perf_stat = {
2770 .tool = {
2771 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002772 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002773 .thread_map = process_thread_map_event,
2774 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002775 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002776 .stat = perf_event__process_stat_event,
2777 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002778 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002779 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002780};
2781
2782static int __cmd_report(int argc, const char **argv)
2783{
2784 struct perf_session *session;
2785 const struct option options[] = {
2786 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002787 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2788 "aggregate counts per processor socket", AGGR_SOCKET),
2789 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2790 "aggregate counts per physical processor core", AGGR_CORE),
2791 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2792 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002793 OPT_END()
2794 };
2795 struct stat st;
2796 int ret;
2797
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002798 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002799
2800 if (!input_name || !strlen(input_name)) {
2801 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2802 input_name = "-";
2803 else
2804 input_name = "perf.data";
2805 }
2806
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002807 perf_stat.data.file.path = input_name;
2808 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002809
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002810 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002811 if (session == NULL)
2812 return -1;
2813
2814 perf_stat.session = session;
2815 stat_config.output = stderr;
2816 evsel_list = session->evlist;
2817
2818 ret = perf_session__process_events(session);
2819 if (ret)
2820 return ret;
2821
2822 perf_session__delete(session);
2823 return 0;
2824}
2825
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002826static void setup_system_wide(int forks)
2827{
2828 /*
2829 * Make system wide (-a) the default target if
2830 * no target was specified and one of following
2831 * conditions is met:
2832 *
2833 * - there's no workload specified
2834 * - there is workload specified but all requested
2835 * events are system wide events
2836 */
2837 if (!target__none(&target))
2838 return;
2839
2840 if (!forks)
2841 target.system_wide = true;
2842 else {
2843 struct perf_evsel *counter;
2844
2845 evlist__for_each_entry(evsel_list, counter) {
2846 if (!counter->system_wide)
2847 return;
2848 }
2849
2850 if (evsel_list->nr_entries)
2851 target.system_wide = true;
2852 }
2853}
2854
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002855int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002856{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002857 const char * const stat_usage[] = {
2858 "perf stat [<options>] [<command>]",
2859 NULL
2860 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002861 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002862 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002863 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002864 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002865 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002866
Stephane Eranian5af52b52010-05-18 15:00:01 +02002867 setlocale(LC_ALL, "");
2868
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002869 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002870 if (evsel_list == NULL)
2871 return -ENOMEM;
2872
Wang Nan1669e502016-02-19 11:43:58 +00002873 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002874 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2875 (const char **) stat_usage,
2876 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002877 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002878 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002879
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002880 if (csv_sep) {
2881 csv_output = true;
2882 if (!strcmp(csv_sep, "\\t"))
2883 csv_sep = "\t";
2884 } else
2885 csv_sep = DEFAULT_SEPARATOR;
2886
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002887 if (argc && !strncmp(argv[0], "rec", 3)) {
2888 argc = __cmd_record(argc, argv);
2889 if (argc < 0)
2890 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002891 } else if (argc && !strncmp(argv[0], "rep", 3))
2892 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002893
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002894 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002895 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002896
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002897 /*
2898 * For record command the -o is already taken care of.
2899 */
2900 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002901 output = NULL;
2902
Jim Cromie56f3bae2011-09-07 17:14:00 -06002903 if (output_name && output_fd) {
2904 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002905 parse_options_usage(stat_usage, stat_options, "o", 1);
2906 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002907 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002908 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002909
Andi Kleen54b50912016-03-03 15:57:36 -08002910 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2911 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2912 goto out;
2913 }
2914
Andi Kleen54b50912016-03-03 15:57:36 -08002915 if (metric_only && run_count > 1) {
2916 fprintf(stderr, "--metric-only is not supported with -r\n");
2917 goto out;
2918 }
2919
Jiri Olsae55c14a2018-04-23 11:08:21 +02002920 if (walltime_run_table && run_count <= 1) {
2921 fprintf(stderr, "--table is only supported with -r\n");
2922 parse_options_usage(stat_usage, stat_options, "r", 1);
2923 parse_options_usage(NULL, stat_options, "table", 0);
2924 goto out;
2925 }
2926
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002927 if (output_fd < 0) {
2928 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002929 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002930 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002931 }
2932
Stephane Eranian4aa90152011-08-15 22:22:33 +02002933 if (!output) {
2934 struct timespec tm;
2935 mode = append_file ? "a" : "w";
2936
2937 output = fopen(output_name, mode);
2938 if (!output) {
2939 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002940 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002941 }
2942 clock_gettime(CLOCK_REALTIME, &tm);
2943 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002944 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002945 mode = append_file ? "a" : "w";
2946 output = fdopen(output_fd, mode);
2947 if (!output) {
2948 perror("Failed opening logfd");
2949 return -errno;
2950 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002951 }
2952
Jiri Olsa58215222015-07-21 14:31:24 +02002953 stat_config.output = output;
2954
Stephane Eraniand7470b62010-12-01 18:49:05 +02002955 /*
2956 * let the spreadsheet do the pretty-printing
2957 */
2958 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002959 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002960 if (big_num_opt == 1) {
2961 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002962 parse_options_usage(stat_usage, stat_options, "B", 1);
2963 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002964 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002965 } else /* Nope, so disable big number formatting */
2966 big_num = false;
2967 } else if (big_num_opt == 0) /* User passed --no-big-num */
2968 big_num = false;
2969
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002970 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002971
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002972 /*
2973 * Display user/system times only for single
2974 * run and when there's specified tracee.
2975 */
2976 if ((run_count == 1) && target__none(&target))
2977 ru_display = true;
2978
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002979 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002980 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002981 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002982 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002983 } else if (run_count == 0) {
2984 forever = true;
2985 run_count = 1;
2986 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002987
Jiri Olsae55c14a2018-04-23 11:08:21 +02002988 if (walltime_run_table) {
2989 walltime_run = zalloc(run_count * sizeof(walltime_run[0]));
2990 if (!walltime_run) {
2991 pr_err("failed to setup -r option");
2992 goto out;
2993 }
2994 }
2995
Jin Yao1d9f8d12017-12-05 22:03:10 +08002996 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2997 !target__has_task(&target)) {
2998 if (!target.system_wide || target.cpu_list) {
2999 fprintf(stderr, "The --per-thread option is only "
3000 "available when monitoring via -p -t -a "
3001 "options or only --per-thread.\n");
3002 parse_options_usage(NULL, stat_options, "p", 1);
3003 parse_options_usage(NULL, stat_options, "t", 1);
3004 goto out;
3005 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02003006 }
3007
3008 /*
3009 * no_aggr, cgroup are for system-wide only
3010 * --per-thread is aggregated per thread, we dont mix it with cpu mode
3011 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02003012 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
3013 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003014 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02003015 fprintf(stderr, "both cgroup and no-aggregation "
3016 "modes only available in system-wide mode\n");
3017
Jiri Olsae0547312015-11-05 15:40:45 +01003018 parse_options_usage(stat_usage, stat_options, "G", 1);
3019 parse_options_usage(NULL, stat_options, "A", 1);
3020 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09003021 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01003022 }
3023
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02003024 if (add_default_attributes())
3025 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003026
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003027 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02003028
Jin Yao1d9f8d12017-12-05 22:03:10 +08003029 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
3030 target.per_thread = true;
3031
Namhyung Kim77a6f012012-05-07 14:09:04 +09003032 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003033 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09003034 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01003035 parse_options_usage(stat_usage, stat_options, "p", 1);
3036 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003037 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09003038 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01003039 parse_options_usage(stat_usage, stat_options, "C", 1);
3040 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09003041 }
3042 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02003043 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02003044
3045 /*
3046 * Initialize thread_map with comm names,
3047 * so we could print it out on output.
3048 */
Jin Yao56739442017-12-05 22:03:07 +08003049 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02003050 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08003051 if (target.system_wide) {
3052 if (runtime_stat_new(&stat_config,
3053 thread_map__nr(evsel_list->threads))) {
3054 goto out;
3055 }
3056 }
3057 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02003058
yuzhoujiandb06a262018-01-29 10:25:22 +01003059 if (stat_config.times && interval)
3060 interval_count = true;
3061 else if (stat_config.times && !interval) {
3062 pr_err("interval-count option should be used together with "
3063 "interval-print.\n");
3064 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
3065 parse_options_usage(stat_usage, stat_options, "I", 1);
3066 goto out;
3067 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02003068
yuzhoujianf1f8ad52018-01-29 10:25:23 +01003069 if (timeout && timeout < 100) {
3070 if (timeout < 10) {
3071 pr_err("timeout must be >= 10ms.\n");
3072 parse_options_usage(stat_usage, stat_options, "timeout", 0);
3073 goto out;
3074 } else
3075 pr_warning("timeout < 100ms. "
3076 "The overhead percentage could be high in some cases. "
3077 "Please proceed with caution.\n");
3078 }
3079 if (timeout && interval) {
3080 pr_err("timeout option is not supported with interval-print.\n");
3081 parse_options_usage(stat_usage, stat_options, "timeout", 0);
3082 parse_options_usage(stat_usage, stat_options, "I", 1);
3083 goto out;
3084 }
3085
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003086 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003087 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03003088
Stephane Eranian86ee6e12013-02-14 13:57:27 +01003089 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003090 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01003091
Ingo Molnar58d7e992009-05-15 11:03:23 +02003092 /*
3093 * We dont want to block the signals - that would cause
3094 * child tasks to inherit that and Ctrl-C would not work.
3095 * What we want is for Ctrl-C to work in the exec()-ed
3096 * task, but being ignored by perf stat itself:
3097 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02003098 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003099 if (!forever)
3100 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01003101 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02003102 signal(SIGALRM, skip_signal);
3103 signal(SIGABRT, skip_signal);
3104
Ingo Molnar42202dd2009-06-13 14:57:28 +02003105 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003106 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09003107 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02003108 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
3109 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02003110
Jiri Olsae55c14a2018-04-23 11:08:21 +02003111 status = run_perf_stat(argc, argv, run_idx);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003112 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02003113 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02003114 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003115 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02003116 }
3117
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003118 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02003119 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003120
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003121 if (STAT_RECORD) {
3122 /*
3123 * We synthesize the kernel mmap record just so that older tools
3124 * don't emit warnings about not being able to resolve symbols
3125 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3126 * a saner message about no samples being in the perf.data file.
3127 *
3128 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01003129 * in header.c at the moment 'perf stat record' gets introduced, which
3130 * is not really needed once we start adding the stat specific PERF_RECORD_
3131 * records, but the need to suppress the kptr_restrict messages in older
3132 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003133 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003134 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003135 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3136 process_synthesized_event,
3137 &perf_stat.session->machines.host);
3138 if (err) {
3139 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3140 "older tools may produce warnings about this file\n.");
3141 }
3142
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003143 if (!interval) {
3144 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3145 pr_err("failed to write stat round event\n");
3146 }
3147
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003148 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003149 perf_stat.session->header.data_size += perf_stat.bytes_written;
3150 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3151 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003152
3153 perf_session__delete(perf_stat.session);
3154 }
3155
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003156 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003157 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003158out:
Jiri Olsae55c14a2018-04-23 11:08:21 +02003159 free(walltime_run);
3160
Kan Liangdaefd0b2017-05-26 12:05:38 -07003161 if (smi_cost && smi_reset)
3162 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3163
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003164 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003165
3166 runtime_stat_delete(&stat_config);
3167
Ingo Molnar42202dd2009-06-13 14:57:28 +02003168 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003169}