blob: 48c88f568fe10008643f02b7c2c84084a679f5f2 [file] [log] [blame]
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-stat.c
3 *
4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
6 *
7 * Sample output:
Ingo Molnarddcacfa2009-04-20 15:37:32 +02008
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02009 $ perf stat ./hackbench 10
Ingo Molnarddcacfa2009-04-20 15:37:32 +020010
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020011 Time: 0.118
Ingo Molnarddcacfa2009-04-20 15:37:32 +020012
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020013 Performance counter stats for './hackbench 10':
Ingo Molnarddcacfa2009-04-20 15:37:32 +020014
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020015 1708.761321 task-clock # 11.037 CPUs utilized
16 41,190 context-switches # 0.024 M/sec
17 6,735 CPU-migrations # 0.004 M/sec
18 17,318 page-faults # 0.010 M/sec
19 5,205,202,243 cycles # 3.046 GHz
20 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
21 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
22 2,603,501,247 instructions # 0.50 insns per cycle
23 # 1.48 stalled cycles per insn
24 484,357,498 branches # 283.455 M/sec
25 6,388,934 branch-misses # 1.32% of all branches
26
27 0.154822978 seconds time elapsed
Ingo Molnarddcacfa2009-04-20 15:37:32 +020028
Ingo Molnar52425192009-05-26 09:17:18 +020029 *
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020030 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
Ingo Molnar52425192009-05-26 09:17:18 +020031 *
32 * Improvements and fixes by:
33 *
34 * Arjan van de Ven <arjan@linux.intel.com>
35 * Yanmin Zhang <yanmin.zhang@intel.com>
36 * Wu Fengguang <fengguang.wu@intel.com>
37 * Mike Galbraith <efault@gmx.de>
38 * Paul Mackerras <paulus@samba.org>
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +053039 * Jaswinder Singh Rajput <jaswinder@kernel.org>
Ingo Molnar52425192009-05-26 09:17:18 +020040 *
41 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnarddcacfa2009-04-20 15:37:32 +020042 */
43
Peter Zijlstra1a482f32009-05-23 18:28:58 +020044#include "perf.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +020045#include "builtin.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030046#include "util/cgroup.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020047#include "util/util.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060048#include <subcmd/parse-options.h>
Ingo Molnar52425192009-05-26 09:17:18 +020049#include "util/parse-events.h"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070050#include "util/pmu.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020051#include "util/event.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020052#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020053#include "util/evsel.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020054#include "util/debug.h"
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -060055#include "util/drv_configs.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020056#include "util/color.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080057#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080058#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110059#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030060#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020061#include "util/thread_map.h"
Jiri Olsad8095602015-08-07 12:51:03 +020062#include "util/counts.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030063#include "util/group.h"
Jiri Olsa4979d0c2015-11-05 15:40:46 +010064#include "util/session.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010065#include "util/tool.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030066#include "util/string2.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070067#include "util/metricgroup.h"
Jiri Olsa9660e082018-06-07 00:15:06 +020068#include "util/top.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010069#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020070
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030071#include <linux/time64.h>
Andi Kleen44b1e602016-05-30 12:49:42 -030072#include <api/fs/fs.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030073#include <errno.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030074#include <signal.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020075#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020076#include <sys/prctl.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030077#include <inttypes.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020078#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070079#include <math.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030080#include <sys/types.h>
81#include <sys/stat.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030082#include <sys/wait.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030083#include <unistd.h>
Jiri Olsa0ce2da12018-06-05 14:13:13 +020084#include <sys/time.h>
85#include <sys/resource.h>
86#include <sys/wait.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020087
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030088#include "sane_ctype.h"
89
Stephane Eraniand7470b62010-12-01 18:49:05 +020090#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060091#define CNTR_NOT_SUPPORTED "<not supported>"
92#define CNTR_NOT_COUNTED "<not counted>"
Kan Liangdaefd0b2017-05-26 12:05:38 -070093#define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
Stephane Eraniand7470b62010-12-01 18:49:05 +020094
Jiri Olsad4f63a42015-06-26 11:29:26 +020095static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010096
Andi Kleen4cabc3d2013-08-21 16:47:26 -070097/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020098static const char *transaction_attrs = {
99 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700100 "{"
101 "instructions,"
102 "cycles,"
103 "cpu/cycles-t/,"
104 "cpu/tx-start/,"
105 "cpu/el-start/,"
106 "cpu/cycles-ct/"
107 "}"
108};
109
110/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +0200111static const char * transaction_limited_attrs = {
112 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700113 "{"
114 "instructions,"
115 "cycles,"
116 "cpu/cycles-t/,"
117 "cpu/tx-start/"
118 "}"
119};
120
Andi Kleen44b1e602016-05-30 12:49:42 -0300121static const char * topdown_attrs[] = {
122 "topdown-total-slots",
123 "topdown-slots-retired",
124 "topdown-recovery-bubbles",
125 "topdown-fetch-bubbles",
126 "topdown-slots-issued",
127 NULL,
128};
129
Kan Liangdaefd0b2017-05-26 12:05:38 -0700130static const char *smi_cost_attrs = {
131 "{"
132 "msr/aperf/,"
133 "msr/smi/,"
134 "cycles"
135 "}"
136};
137
Robert Richter666e6d42012-04-05 18:26:27 +0200138static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200139
Andi Kleenb18f3e32017-08-31 12:40:31 -0700140static struct rblist metric_events;
141
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300142static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900143 .uid = UINT_MAX,
144};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530145
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100146typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
147
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200148#define METRIC_ONLY_LEN 20
149
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530150static int run_count = 1;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200151static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000152static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200153static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700154static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300155static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700156static bool smi_cost = false;
157static bool smi_reset = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200158static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200159static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200160static const char *csv_sep = NULL;
161static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800162static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200163static const char *pre_cmd = NULL;
164static const char *post_cmd = NULL;
165static bool sync_run = false;
Stephane Eranian410136f2013-11-12 17:58:49 +0100166static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500167static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800168static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300169static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700170static bool no_merge = false;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200171static bool walltime_run_table = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100172static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100173static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100174static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100175static bool append_file;
yuzhoujiandb06a262018-01-29 10:25:22 +0100176static bool interval_count;
Jiri Olsa9660e082018-06-07 00:15:06 +0200177static bool interval_clear;
Jiri Olsae0547312015-11-05 15:40:45 +0100178static const char *output_name;
179static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100180static int print_free_counters_hint;
Kan Liang30060ea2018-04-24 11:20:11 -0700181static int print_mixed_hw_group_error;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200182static u64 *walltime_run;
Jiri Olsa0ce2da12018-06-05 14:13:13 +0200183static bool ru_display = false;
184static struct rusage ru_data;
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200185static unsigned int metric_only_len = METRIC_ONLY_LEN;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200186
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100187struct perf_stat {
188 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100189 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100190 struct perf_session *session;
191 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100192 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100193 bool maps_allocated;
194 struct cpu_map *cpus;
195 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100196 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100197};
198
199static struct perf_stat perf_stat;
200#define STAT_RECORD perf_stat.record
201
Liming Wang60666c62009-12-31 16:05:50 +0800202static volatile int done = 0;
203
Jiri Olsa421a50f2015-07-21 14:31:22 +0200204static struct perf_stat_config stat_config = {
205 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200206 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200207};
208
Andi Kleene864c5c2017-08-31 12:40:35 -0700209static bool is_duration_time(struct perf_evsel *evsel)
210{
211 return !strcmp(evsel->name, "duration_time");
212}
213
Stephane Eranian13370a92013-01-29 12:47:44 +0100214static inline void diff_timespec(struct timespec *r, struct timespec *a,
215 struct timespec *b)
216{
217 r->tv_sec = a->tv_sec - b->tv_sec;
218 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300219 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100220 r->tv_sec--;
221 } else {
222 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
223 }
224}
225
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200226static void perf_stat__reset_stats(void)
227{
Jin Yao56739442017-12-05 22:03:07 +0800228 int i;
229
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200230 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200231 perf_stat__reset_shadow_stats();
Jin Yao56739442017-12-05 22:03:07 +0800232
233 for (i = 0; i < stat_config.stats_num; i++)
234 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200235}
236
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200237static int create_perf_stat_counter(struct perf_evsel *evsel,
238 struct perf_stat_config *config)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200239{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200240 struct perf_event_attr *attr = &evsel->attr;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200241 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200242
Jiri Olsa35386232018-08-30 08:32:13 +0200243 if (config->scale) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200244 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
245 PERF_FORMAT_TOTAL_TIME_RUNNING;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200246 }
247
248 /*
249 * The event is part of non trivial group, let's enable
250 * the group read (for leader) and ID retrieval for all
251 * members.
252 */
253 if (leader->nr_members > 1)
254 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200255
Jiri Olsa5698f262018-08-30 08:32:12 +0200256 attr->inherit = !config->no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300257
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100258 /*
259 * Some events get initialized with sample_(period/type) set,
260 * like tracepoints. Clear it up for counting.
261 */
262 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100263
Jiri Olsa7d9ad162018-08-30 08:32:14 +0200264 if (config->identifier)
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100265 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100266
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100267 /*
268 * Disabling all counters initially, they will be enabled
269 * either manually by us or by kernel via enable_on_exec
270 * set later.
271 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100272 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100273 attr->disabled = 1;
274
Jiri Olsac8280ce2015-12-03 10:06:45 +0100275 /*
276 * In case of initial_delay we enable tracee
277 * events manually.
278 */
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200279 if (target__none(&target) && !config->initial_delay)
Jiri Olsac8280ce2015-12-03 10:06:45 +0100280 attr->enable_on_exec = 1;
281 }
282
Jin Yao1d9f8d12017-12-05 22:03:10 +0800283 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300284 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200285
Jiri Olsad50ed0c2018-08-30 08:32:10 +0200286 return perf_evsel__open_per_thread(evsel, evsel->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200287}
288
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100289static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100290 union perf_event *event,
291 struct perf_sample *sample __maybe_unused,
292 struct machine *machine __maybe_unused)
293{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100294 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100295 pr_err("failed to write perf data, error: %m\n");
296 return -1;
297 }
298
299 perf_stat.bytes_written += event->header.size;
300 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100301}
302
Jiri Olsa1975d362015-11-05 15:40:56 +0100303static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100304{
Jiri Olsa1975d362015-11-05 15:40:56 +0100305 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100306 process_synthesized_event,
307 NULL);
308}
309
310#define WRITE_STAT_ROUND_EVENT(time, interval) \
311 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
312
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100313#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
314
315static int
316perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
317 struct perf_counts_values *count)
318{
319 struct perf_sample_id *sid = SID(counter, cpu, thread);
320
321 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
322 process_synthesized_event, NULL);
323}
324
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200325/*
326 * Read out the results of a single counter:
327 * do not aggregate counts across CPUs in system-wide mode
328 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200329static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200330{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100331 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100332 int ncpus, cpu, thread;
333
Jin Yao1d9f8d12017-12-05 22:03:10 +0800334 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Mark Rutland00e727b2016-07-15 11:08:10 +0100335 ncpus = perf_evsel__nr_cpus(counter);
336 else
337 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200338
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000339 if (!counter->supported)
340 return -ENOENT;
341
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100342 if (counter->system_wide)
343 nthreads = 1;
344
345 for (thread = 0; thread < nthreads; thread++) {
346 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200347 struct perf_counts_values *count;
348
349 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200350
351 /*
352 * The leader's group read loads data into its group members
353 * (via perf_evsel__read_counter) and sets threir count->loaded.
354 */
355 if (!count->loaded &&
356 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700357 counter->counts->scaled = -1;
358 perf_counts(counter->counts, cpu, thread)->ena = 0;
359 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100360 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700361 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100362
Jiri Olsa82bf3112017-07-26 14:02:06 +0200363 count->loaded = false;
364
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100365 if (STAT_RECORD) {
366 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
367 pr_err("failed to write stat event\n");
368 return -1;
369 }
370 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700371
372 if (verbose > 1) {
373 fprintf(stat_config.output,
374 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
375 perf_evsel__name(counter),
376 cpu,
377 count->val, count->ena, count->run);
378 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100379 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200380 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200381
382 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200383}
384
Mark Rutland3df33ef2016-08-09 14:04:29 +0100385static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200386{
387 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700388 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200389
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300390 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700391 ret = read_counter(counter);
392 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700393 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200394
Stephane Eraniandb49a712017-04-12 11:23:01 -0700395 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200396 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200397 }
398}
399
Jiri Olsaba411a92015-06-26 11:29:24 +0200400static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100401{
Stephane Eranian13370a92013-01-29 12:47:44 +0100402 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100403
Mark Rutland3df33ef2016-08-09 14:04:29 +0100404 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100405
Stephane Eranian13370a92013-01-29 12:47:44 +0100406 clock_gettime(CLOCK_MONOTONIC, &ts);
407 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100408
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100409 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300410 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100411 pr_err("failed to write stat round event\n");
412 }
413
Andi Kleenb90f1332017-08-31 12:40:36 -0700414 init_stats(&walltime_nsecs_stats);
415 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200416 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100417}
418
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100419static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700420{
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200421 if (stat_config.initial_delay)
422 usleep(stat_config.initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100423
424 /*
425 * We need to enable counters only if:
426 * - we don't have tracee (attaching to task or cpu)
427 * - we have initial delay configured
428 */
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200429 if (!target__none(&target) || stat_config.initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100430 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700431}
432
Mark Rutland3df33ef2016-08-09 14:04:29 +0100433static void disable_counters(void)
434{
435 /*
436 * If we don't have tracee (attaching to task or cpu), counters may
437 * still be running. To get accurate group ratios, we must stop groups
438 * from counting before reading their constituent counters.
439 */
440 if (!target__none(&target))
441 perf_evlist__disable(evsel_list);
442}
443
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300444static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300445
446/*
447 * perf_evlist__prepare_workload will send a SIGUSR1
448 * if the fork fails, since we asked by setting its
449 * want_signal to true.
450 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300451static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
452 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300453{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300454 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300455}
456
Jiri Olsa664c98d2015-11-05 15:40:50 +0100457static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100458{
459 int err;
460
Jiri Olsa664c98d2015-11-05 15:40:50 +0100461 if (is_pipe) {
462 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
463 process_synthesized_event);
464 if (err < 0) {
465 pr_err("Couldn't synthesize attrs.\n");
466 return err;
467 }
468 }
469
Andi Kleenbfd8f722017-11-17 13:42:58 -0800470 err = perf_event__synthesize_extra_attr(NULL,
471 evsel_list,
472 process_synthesized_event,
473 is_pipe);
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100474
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100475 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
476 process_synthesized_event,
477 NULL);
478 if (err < 0) {
479 pr_err("Couldn't synthesize thread map.\n");
480 return err;
481 }
482
483 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
484 process_synthesized_event, NULL);
485 if (err < 0) {
486 pr_err("Couldn't synthesize thread map.\n");
487 return err;
488 }
489
490 err = perf_event__synthesize_stat_config(NULL, &stat_config,
491 process_synthesized_event, NULL);
492 if (err < 0) {
493 pr_err("Couldn't synthesize config.\n");
494 return err;
495 }
496
497 return 0;
498}
499
Jiri Olsa2af46462015-11-05 15:40:49 +0100500#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
501
Andi Kleen42811d52017-10-05 19:00:28 -0700502static int __store_counter_ids(struct perf_evsel *counter)
Jiri Olsa2af46462015-11-05 15:40:49 +0100503{
504 int cpu, thread;
505
Andi Kleen42811d52017-10-05 19:00:28 -0700506 for (cpu = 0; cpu < xyarray__max_x(counter->fd); cpu++) {
507 for (thread = 0; thread < xyarray__max_y(counter->fd);
508 thread++) {
Jiri Olsa2af46462015-11-05 15:40:49 +0100509 int fd = FD(counter, cpu, thread);
510
511 if (perf_evlist__id_add_fd(evsel_list, counter,
512 cpu, thread, fd) < 0)
513 return -1;
514 }
515 }
516
517 return 0;
518}
519
520static int store_counter_ids(struct perf_evsel *counter)
521{
522 struct cpu_map *cpus = counter->cpus;
523 struct thread_map *threads = counter->threads;
524
525 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
526 return -ENOMEM;
527
Andi Kleen42811d52017-10-05 19:00:28 -0700528 return __store_counter_ids(counter);
Jiri Olsa2af46462015-11-05 15:40:49 +0100529}
530
Jiri Olsa82bf3112017-07-26 14:02:06 +0200531static bool perf_evsel__should_store_id(struct perf_evsel *counter)
532{
533 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
534}
535
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700536static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
537{
538 struct perf_evsel *c2, *leader;
539 bool is_open = true;
540
541 leader = evsel->leader;
542 pr_debug("Weak group for %s/%d failed\n",
543 leader->name, leader->nr_members);
544
545 /*
546 * for_each_group_member doesn't work here because it doesn't
547 * include the first entry.
548 */
549 evlist__for_each_entry(evsel_list, c2) {
550 if (c2 == evsel)
551 is_open = false;
552 if (c2->leader == leader) {
553 if (is_open)
554 perf_evsel__close(c2);
555 c2->leader = c2;
556 c2->nr_members = 0;
557 }
558 }
559 return leader;
560}
561
Jiri Olsae55c14a2018-04-23 11:08:21 +0200562static int __run_perf_stat(int argc, const char **argv, int run_idx)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200563{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200564 int interval = stat_config.interval;
yuzhoujiandb06a262018-01-29 10:25:22 +0100565 int times = stat_config.times;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100566 int timeout = stat_config.timeout;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300567 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200568 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100569 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100570 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100571 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200572 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300573 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100574 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600575 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200576
Stephane Eranian13370a92013-01-29 12:47:44 +0100577 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300578 ts.tv_sec = interval / USEC_PER_MSEC;
579 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100580 } else if (timeout) {
581 ts.tv_sec = timeout / USEC_PER_MSEC;
582 ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100583 } else {
584 ts.tv_sec = 1;
585 ts.tv_nsec = 0;
586 }
587
Liming Wang60666c62009-12-31 16:05:50 +0800588 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100589 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300590 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900591 perror("failed to prepare workload");
592 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800593 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900594 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000595 }
596
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200597 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300598 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200599
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300600 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300601try_again:
Jiri Olsa728c0ee2018-08-30 08:32:11 +0200602 if (create_perf_stat_counter(counter, &stat_config) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700603
604 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700605 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700606 counter->leader != counter &&
607 counter->weak_group) {
608 counter = perf_evsel__reset_weak_group(counter);
609 goto try_again;
610 }
611
David Ahern979987a2012-05-08 09:29:16 -0600612 /*
613 * PPC returns ENXIO for HW counters until 2.6.37
614 * (behavior changed with commit b0a873e).
615 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100616 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600617 errno == ENOENT || errno == EOPNOTSUPP ||
618 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900619 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600620 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300621 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600622 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400623
624 if ((counter->leader != counter) ||
625 !(counter->leader->nr_members > 1))
626 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300627 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900628 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300629 ui__warning("%s\n", msg);
630 goto try_again;
Jin Yaoab6c79b2018-01-16 23:43:08 +0800631 } else if (target__has_per_thread(&target) &&
632 evsel_list->threads &&
633 evsel_list->threads->err_thread != -1) {
634 /*
635 * For global --per-thread case, skip current
636 * error thread.
637 */
638 if (!thread_map__remove(evsel_list->threads,
639 evsel_list->threads->err_thread)) {
640 evsel_list->threads->err_thread = -1;
641 goto try_again;
642 }
643 }
Ingo Molnarede70292011-04-28 08:48:42 +0200644
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300645 perf_evsel__open_strerror(counter, &target,
646 errno, msg, sizeof(msg));
647 ui__error("%s\n", msg);
648
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200649 if (child_pid != -1)
650 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600651
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200652 return -1;
653 }
David Ahern2cee77c2011-05-30 08:55:59 -0600654 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100655
656 l = strlen(counter->unit);
657 if (l > unit_width)
658 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100659
Jiri Olsa82bf3112017-07-26 14:02:06 +0200660 if (perf_evsel__should_store_id(counter) &&
661 store_counter_ids(counter))
Jiri Olsa2af46462015-11-05 15:40:49 +0100662 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300663 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200664
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300665 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300666 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300667 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300668 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100669 return -1;
670 }
671
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600672 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300673 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600674 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
675 str_error_r(errno, msg, sizeof(msg)));
676 return -1;
677 }
678
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100679 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100680 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100681
Jiri Olsa664c98d2015-11-05 15:40:50 +0100682 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100683 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100684 } else {
685 err = perf_session__write_header(perf_stat.session, evsel_list,
686 fd, false);
687 }
688
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100689 if (err < 0)
690 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100691
Jiri Olsa664c98d2015-11-05 15:40:50 +0100692 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100693 if (err < 0)
694 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100695 }
696
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200697 /*
698 * Enable counters and exec the command:
699 */
700 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100701 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200702
Liming Wang60666c62009-12-31 16:05:50 +0800703 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900704 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100705 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900706
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100707 if (interval || timeout) {
Stephane Eranian13370a92013-01-29 12:47:44 +0100708 while (!waitpid(child_pid, &status, WNOHANG)) {
709 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100710 if (timeout)
711 break;
Jiri Olsaba411a92015-06-26 11:29:24 +0200712 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100713 if (interval_count && !(--times))
714 break;
Stephane Eranian13370a92013-01-29 12:47:44 +0100715 }
716 }
Jiri Olsa0ce2da12018-06-05 14:13:13 +0200717 wait4(child_pid, &status, 0, &ru_data);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300718
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300719 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300720 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300721 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300722 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300723 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300724
Andi Kleen33e49ea2011-09-15 14:31:40 -0700725 if (WIFSIGNALED(status))
726 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800727 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100728 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100729 while (!done) {
730 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100731 if (timeout)
732 break;
yuzhoujiandb06a262018-01-29 10:25:22 +0100733 if (interval) {
Jiri Olsaba411a92015-06-26 11:29:24 +0200734 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100735 if (interval_count && !(--times))
736 break;
737 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100738 }
Liming Wang60666c62009-12-31 16:05:50 +0800739 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200740
Mark Rutland3df33ef2016-08-09 14:04:29 +0100741 disable_counters();
742
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200743 t1 = rdclock();
744
Jiri Olsae55c14a2018-04-23 11:08:21 +0200745 if (walltime_run_table)
746 walltime_run[run_idx] = t1 - t0;
747
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200748 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200749
Mark Rutland3df33ef2016-08-09 14:04:29 +0100750 /*
751 * Closing a group leader splits the group, and as we only disable
752 * group leaders, results in remaining events becoming enabled. To
753 * avoid arbitrary skew, we must read all counters before closing any
754 * group leaders.
755 */
756 read_counters();
757 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200758
Ingo Molnar42202dd2009-06-13 14:57:28 +0200759 return WEXITSTATUS(status);
760}
761
Jiri Olsae55c14a2018-04-23 11:08:21 +0200762static int run_perf_stat(int argc, const char **argv, int run_idx)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200763{
764 int ret;
765
766 if (pre_cmd) {
767 ret = system(pre_cmd);
768 if (ret)
769 return ret;
770 }
771
772 if (sync_run)
773 sync();
774
Jiri Olsae55c14a2018-04-23 11:08:21 +0200775 ret = __run_perf_stat(argc, argv, run_idx);
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200776 if (ret)
777 return ret;
778
779 if (post_cmd) {
780 ret = system(post_cmd);
781 if (ret)
782 return ret;
783 }
784
785 return ret;
786}
787
Andi Kleend73515c2015-03-11 07:16:27 -0700788static void print_running(u64 run, u64 ena)
789{
790 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200791 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700792 csv_sep,
793 run,
794 csv_sep,
795 ena ? 100.0 * run / ena : 100.0);
796 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200797 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700798 }
799}
800
Ingo Molnarf99844c2011-04-27 05:35:39 +0200801static void print_noise_pct(double total, double avg)
802{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800803 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200804
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700805 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200806 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600807 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200808 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200809}
810
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200811static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200812{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200813 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200814
Peter Zijlstra849abde2009-09-04 18:23:38 +0200815 if (run_count == 1)
816 return;
817
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300818 ps = evsel->stats;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200819 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200820}
821
Stephane Eranian12c08a92013-02-14 13:57:29 +0100822static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200823{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200824 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100825 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200826 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100827 cpu_map__id_to_socket(id),
828 csv_output ? 0 : -8,
829 cpu_map__id_to_cpu(id),
830 csv_sep,
831 csv_output ? 0 : 4,
832 nr,
833 csv_sep);
834 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100835 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200836 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100837 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100838 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100839 csv_sep,
840 csv_output ? 0 : 4,
841 nr,
842 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100843 break;
844 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200845 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200846 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100847 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100848 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200849 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200850 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200851 csv_output ? 0 : 16,
852 thread_map__comm(evsel->threads, id),
853 csv_output ? 0 : -8,
854 thread_map__pid(evsel->threads, id),
855 csv_sep);
856 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100857 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200858 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100859 default:
860 break;
861 }
862}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200863
Andi Kleen140aead2016-01-30 09:06:49 -0800864struct outstate {
865 FILE *fh;
866 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800867 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800868 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800869 int id, nr;
870 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800871};
872
873#define METRIC_LEN 35
874
875static void new_line_std(void *ctx)
876{
877 struct outstate *os = ctx;
878
879 os->newline = true;
880}
881
882static void do_new_line_std(struct outstate *os)
883{
884 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800885 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800886 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800887 if (stat_config.aggr_mode == AGGR_NONE)
888 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800889 fprintf(os->fh, " ");
890}
891
892static void print_metric_std(void *ctx, const char *color, const char *fmt,
893 const char *unit, double val)
894{
895 struct outstate *os = ctx;
896 FILE *out = os->fh;
897 int n;
898 bool newline = os->newline;
899
900 os->newline = false;
901
902 if (unit == NULL || fmt == NULL) {
903 fprintf(out, "%-*s", METRIC_LEN, "");
904 return;
905 }
906
907 if (newline)
908 do_new_line_std(os);
909
910 n = fprintf(out, " # ");
911 if (color)
912 n += color_fprintf(out, color, fmt, val);
913 else
914 n += fprintf(out, fmt, val);
915 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
916}
917
Andi Kleen92a61f62016-02-29 14:36:21 -0800918static void new_line_csv(void *ctx)
919{
920 struct outstate *os = ctx;
921 int i;
922
923 fputc('\n', os->fh);
924 if (os->prefix)
925 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800926 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800927 for (i = 0; i < os->nfields; i++)
928 fputs(csv_sep, os->fh);
929}
930
931static void print_metric_csv(void *ctx,
932 const char *color __maybe_unused,
933 const char *fmt, const char *unit, double val)
934{
935 struct outstate *os = ctx;
936 FILE *out = os->fh;
937 char buf[64], *vals, *ends;
938
939 if (unit == NULL || fmt == NULL) {
Ilya Pronin40c21892018-03-05 22:43:53 -0800940 fprintf(out, "%s%s", csv_sep, csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800941 return;
942 }
943 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900944 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800945 while (isdigit(*ends) || *ends == '.')
946 ends++;
947 *ends = 0;
948 while (isspace(*unit))
949 unit++;
950 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
951}
952
Andi Kleen54b50912016-03-03 15:57:36 -0800953/* Filter out some columns that don't work well in metrics only mode */
954
955static bool valid_only_metric(const char *unit)
956{
957 if (!unit)
958 return false;
959 if (strstr(unit, "/sec") ||
960 strstr(unit, "hz") ||
961 strstr(unit, "Hz") ||
962 strstr(unit, "CPUs utilized"))
963 return false;
964 return true;
965}
966
967static const char *fixunit(char *buf, struct perf_evsel *evsel,
968 const char *unit)
969{
970 if (!strncmp(unit, "of all", 6)) {
971 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
972 unit);
973 return buf;
974 }
975 return unit;
976}
977
978static void print_metric_only(void *ctx, const char *color, const char *fmt,
979 const char *unit, double val)
980{
981 struct outstate *os = ctx;
982 FILE *out = os->fh;
Jiri Olsaf5155722018-06-07 00:15:08 +0200983 char buf[1024], str[1024];
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200984 unsigned mlen = metric_only_len;
Andi Kleen54b50912016-03-03 15:57:36 -0800985
986 if (!valid_only_metric(unit))
987 return;
988 unit = fixunit(buf, os->evsel, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800989 if (mlen < strlen(unit))
990 mlen = strlen(unit) + 1;
Jiri Olsaf5155722018-06-07 00:15:08 +0200991
992 if (color)
993 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
994
995 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
996 fprintf(out, "%*s ", mlen, str);
Andi Kleen54b50912016-03-03 15:57:36 -0800997}
998
999static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
1000 const char *fmt,
1001 const char *unit, double val)
1002{
1003 struct outstate *os = ctx;
1004 FILE *out = os->fh;
1005 char buf[64], *vals, *ends;
1006 char tbuf[1024];
1007
1008 if (!valid_only_metric(unit))
1009 return;
1010 unit = fixunit(tbuf, os->evsel, unit);
1011 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +09001012 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -08001013 while (isdigit(*ends) || *ends == '.')
1014 ends++;
1015 *ends = 0;
1016 fprintf(out, "%s%s", vals, csv_sep);
1017}
1018
1019static void new_line_metric(void *ctx __maybe_unused)
1020{
1021}
1022
1023static void print_metric_header(void *ctx, const char *color __maybe_unused,
1024 const char *fmt __maybe_unused,
1025 const char *unit, double val __maybe_unused)
1026{
1027 struct outstate *os = ctx;
1028 char tbuf[1024];
1029
1030 if (!valid_only_metric(unit))
1031 return;
1032 unit = fixunit(tbuf, os->evsel, unit);
1033 if (csv_output)
1034 fprintf(os->fh, "%s%s", unit, csv_sep);
1035 else
Jiri Olsac1a1f5d2018-06-07 00:15:09 +02001036 fprintf(os->fh, "%*s ", metric_only_len, unit);
Andi Kleen54b50912016-03-03 15:57:36 -08001037}
1038
Andi Kleen44d49a62016-02-29 14:36:22 -08001039static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1040{
1041 int i;
1042
1043 if (!aggr_get_id)
1044 return 0;
1045
1046 if (stat_config.aggr_mode == AGGR_NONE)
1047 return id;
1048
1049 if (stat_config.aggr_mode == AGGR_GLOBAL)
1050 return 0;
1051
1052 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1053 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1054
1055 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1056 return cpu2;
1057 }
1058 return 0;
1059}
1060
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001061static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1062{
Jiri Olsa58215222015-07-21 14:31:24 +02001063 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001064 double sc = evsel->scale;
1065 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001066
1067 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001068 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001069 } else {
1070 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001071 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001072 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001073 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001074 }
1075
1076 aggr_printout(evsel, id, nr);
1077
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001078 fprintf(output, fmt, avg, csv_sep);
1079
1080 if (evsel->unit)
1081 fprintf(output, "%-*s%s",
1082 csv_output ? 0 : unit_width,
1083 evsel->unit, csv_sep);
1084
1085 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1086
1087 if (evsel->cgrp)
1088 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001089}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001090
Kan Liang30060ea2018-04-24 11:20:11 -07001091static bool is_mixed_hw_group(struct perf_evsel *counter)
1092{
1093 struct perf_evlist *evlist = counter->evlist;
1094 u32 pmu_type = counter->attr.type;
1095 struct perf_evsel *pos;
1096
1097 if (counter->nr_members < 2)
1098 return false;
1099
1100 evlist__for_each_entry(evlist, pos) {
1101 /* software events can be part of any hardware group */
1102 if (pos->attr.type == PERF_TYPE_SOFTWARE)
1103 continue;
1104 if (pmu_type == PERF_TYPE_SOFTWARE) {
1105 pmu_type = pos->attr.type;
1106 continue;
1107 }
1108 if (pmu_type != pos->attr.type)
1109 return true;
1110 }
1111
1112 return false;
1113}
1114
Andi Kleenf9483392016-01-30 09:06:50 -08001115static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +08001116 char *prefix, u64 run, u64 ena, double noise,
1117 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001118{
Andi Kleen140aead2016-01-30 09:06:49 -08001119 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001120 struct outstate os = {
1121 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001122 .prefix = prefix ? prefix : "",
1123 .id = id,
1124 .nr = nr,
1125 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001126 };
Andi Kleen140aead2016-01-30 09:06:49 -08001127 print_metric_t pm = print_metric_std;
1128 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001129
Andi Kleen54b50912016-03-03 15:57:36 -08001130 if (metric_only) {
1131 nl = new_line_metric;
1132 if (csv_output)
1133 pm = print_metric_only_csv;
1134 else
1135 pm = print_metric_only;
1136 } else
1137 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001138
Andi Kleen54b50912016-03-03 15:57:36 -08001139 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001140 static int aggr_fields[] = {
1141 [AGGR_GLOBAL] = 0,
1142 [AGGR_THREAD] = 1,
1143 [AGGR_NONE] = 1,
1144 [AGGR_SOCKET] = 2,
1145 [AGGR_CORE] = 2,
1146 };
1147
1148 pm = print_metric_csv;
1149 nl = new_line_csv;
1150 os.nfields = 3;
1151 os.nfields += aggr_fields[stat_config.aggr_mode];
1152 if (counter->cgrp)
1153 os.nfields++;
1154 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001155 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001156 if (metric_only) {
1157 pm(&os, NULL, "", "", 0);
1158 return;
1159 }
Andi Kleencb110f42016-01-30 09:06:51 -08001160 aggr_printout(counter, id, nr);
1161
1162 fprintf(stat_config.output, "%*s%s",
1163 csv_output ? 0 : 18,
1164 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1165 csv_sep);
1166
Kan Liang30060ea2018-04-24 11:20:11 -07001167 if (counter->supported) {
Borislav Petkov02d492e2017-02-07 01:40:05 +01001168 print_free_counters_hint = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001169 if (is_mixed_hw_group(counter))
1170 print_mixed_hw_group_error = 1;
1171 }
Borislav Petkov02d492e2017-02-07 01:40:05 +01001172
Andi Kleencb110f42016-01-30 09:06:51 -08001173 fprintf(stat_config.output, "%-*s%s",
1174 csv_output ? 0 : unit_width,
1175 counter->unit, csv_sep);
1176
1177 fprintf(stat_config.output, "%*s",
1178 csv_output ? 0 : -25,
1179 perf_evsel__name(counter));
1180
1181 if (counter->cgrp)
1182 fprintf(stat_config.output, "%s%s",
1183 csv_sep, counter->cgrp->name);
1184
Andi Kleen92a61f62016-02-29 14:36:21 -08001185 if (!csv_output)
1186 pm(&os, NULL, NULL, "", 0);
1187 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001188 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001189 if (csv_output)
1190 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001191 return;
1192 }
1193
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001194 if (!metric_only)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001195 abs_printout(id, nr, counter, uval);
1196
Andi Kleen140aead2016-01-30 09:06:49 -08001197 out.print_metric = pm;
1198 out.new_line = nl;
1199 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001200 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001201
Andi Kleen54b50912016-03-03 15:57:36 -08001202 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001203 print_noise(counter, noise);
1204 print_running(run, ena);
1205 }
1206
1207 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001208 first_shadow_cpu(counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001209 &out, &metric_events, st);
Andi Kleen54b50912016-03-03 15:57:36 -08001210 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001211 print_noise(counter, noise);
1212 print_running(run, ena);
1213 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001214}
1215
Andi Kleen44d49a62016-02-29 14:36:22 -08001216static void aggr_update_shadow(void)
1217{
1218 int cpu, s2, id, s;
1219 u64 val;
1220 struct perf_evsel *counter;
1221
1222 for (s = 0; s < aggr_map->nr; s++) {
1223 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001224 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001225 val = 0;
1226 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1227 s2 = aggr_get_id(evsel_list->cpus, cpu);
1228 if (s2 != id)
1229 continue;
1230 val += perf_counts(counter->counts, cpu, 0)->val;
1231 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001232 perf_stat__update_shadow_stats(counter, val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001233 first_shadow_cpu(counter, id),
1234 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001235 }
1236 }
1237}
1238
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001239static void uniquify_event_name(struct perf_evsel *counter)
1240{
1241 char *new_name;
1242 char *config;
1243
Kan Liang80ee8c52018-04-24 11:20:14 -07001244 if (counter->uniquified_name ||
1245 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001246 strlen(counter->pmu_name)))
1247 return;
1248
1249 config = strchr(counter->name, '/');
1250 if (config) {
1251 if (asprintf(&new_name,
1252 "%s%s", counter->pmu_name, config) > 0) {
1253 free(counter->name);
1254 counter->name = new_name;
1255 }
1256 } else {
1257 if (asprintf(&new_name,
1258 "%s [%s]", counter->name, counter->pmu_name) > 0) {
1259 free(counter->name);
1260 counter->name = new_name;
1261 }
1262 }
Kan Liang80ee8c52018-04-24 11:20:14 -07001263
1264 counter->uniquified_name = true;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001265}
1266
Andi Kleen430daf22017-03-20 13:17:00 -07001267static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001268 void (*cb)(struct perf_evsel *counter, void *data,
1269 bool first),
1270 void *data)
1271{
Andi Kleen430daf22017-03-20 13:17:00 -07001272 struct perf_evsel *alias;
1273
1274 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1275 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1276 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1277 alias->scale != counter->scale ||
1278 alias->cgrp != counter->cgrp ||
1279 strcmp(alias->unit, counter->unit) ||
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001280 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter))
Andi Kleen430daf22017-03-20 13:17:00 -07001281 break;
1282 alias->merged_stat = true;
1283 cb(alias, data, false);
1284 }
1285}
1286
1287static bool collect_data(struct perf_evsel *counter,
1288 void (*cb)(struct perf_evsel *counter, void *data,
1289 bool first),
1290 void *data)
1291{
1292 if (counter->merged_stat)
1293 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001294 cb(counter, data, true);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001295 if (no_merge)
1296 uniquify_event_name(counter);
1297 else if (counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001298 collect_all_aliases(counter, cb, data);
1299 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001300}
1301
1302struct aggr_data {
1303 u64 ena, run, val;
1304 int id;
1305 int nr;
1306 int cpu;
1307};
1308
1309static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1310{
1311 struct aggr_data *ad = data;
1312 int cpu, s2;
1313
1314 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1315 struct perf_counts_values *counts;
1316
1317 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1318 if (s2 != ad->id)
1319 continue;
1320 if (first)
1321 ad->nr++;
1322 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001323 /*
1324 * When any result is bad, make them all to give
1325 * consistent output in interval mode.
1326 */
1327 if (counts->ena == 0 || counts->run == 0 ||
1328 counter->counts->scaled == -1) {
1329 ad->ena = 0;
1330 ad->run = 0;
1331 break;
1332 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001333 ad->val += counts->val;
1334 ad->ena += counts->ena;
1335 ad->run += counts->run;
1336 }
1337}
1338
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001339static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001340{
Jiri Olsa58215222015-07-21 14:31:24 +02001341 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001342 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001343 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001344 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001345 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001346 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001347
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001348 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001349 return;
1350
Andi Kleen44d49a62016-02-29 14:36:22 -08001351 aggr_update_shadow();
1352
Andi Kleen54b50912016-03-03 15:57:36 -08001353 /*
1354 * With metric_only everything is on a single line.
1355 * Without each counter has its own line.
1356 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001357 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001358 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001359 if (prefix && metric_only)
1360 fprintf(output, "%s", prefix);
1361
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001362 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001363 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001364 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001365 if (is_duration_time(counter))
1366 continue;
1367
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001368 ad.val = ad.ena = ad.run = 0;
1369 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001370 if (!collect_data(counter, aggr_cb, &ad))
1371 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001372 nr = ad.nr;
1373 ena = ad.ena;
1374 run = ad.run;
1375 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001376 if (first && metric_only) {
1377 first = false;
1378 aggr_printout(counter, id, nr);
1379 }
1380 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001381 fprintf(output, "%s", prefix);
1382
Stephane Eranian410136f2013-11-12 17:58:49 +01001383 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001384 printout(id, nr, counter, uval, prefix, run, ena, 1.0,
1385 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001386 if (!metric_only)
1387 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001388 }
Andi Kleen54b50912016-03-03 15:57:36 -08001389 if (metric_only)
1390 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001391 }
1392}
1393
Jin Yao29734552017-12-05 22:03:11 +08001394static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001395{
Jin Yao29734552017-12-05 22:03:11 +08001396 return ((struct perf_aggr_thread_value *)b)->val -
1397 ((struct perf_aggr_thread_value *)a)->val;
1398}
1399
1400static struct perf_aggr_thread_value *sort_aggr_thread(
1401 struct perf_evsel *counter,
1402 int nthreads, int ncpus,
1403 int *ret)
1404{
1405 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001406 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001407 struct perf_aggr_thread_value *buf;
1408
1409 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1410 if (!buf)
1411 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001412
1413 for (thread = 0; thread < nthreads; thread++) {
1414 u64 ena = 0, run = 0, val = 0;
1415
1416 for (cpu = 0; cpu < ncpus; cpu++) {
1417 val += perf_counts(counter->counts, cpu, thread)->val;
1418 ena += perf_counts(counter->counts, cpu, thread)->ena;
1419 run += perf_counts(counter->counts, cpu, thread)->run;
1420 }
1421
Jin Yao29734552017-12-05 22:03:11 +08001422 uval = val * counter->scale;
1423
1424 /*
1425 * Skip value 0 when enabling --per-thread globally,
1426 * otherwise too many 0 output.
1427 */
1428 if (uval == 0.0 && target__has_per_thread(&target))
1429 continue;
1430
1431 buf[i].counter = counter;
1432 buf[i].id = thread;
1433 buf[i].uval = uval;
1434 buf[i].val = val;
1435 buf[i].run = run;
1436 buf[i].ena = ena;
1437 i++;
1438 }
1439
1440 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1441
1442 if (ret)
1443 *ret = i;
1444
1445 return buf;
1446}
1447
1448static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1449{
1450 FILE *output = stat_config.output;
1451 int nthreads = thread_map__nr(counter->threads);
1452 int ncpus = cpu_map__nr(counter->cpus);
1453 int thread, sorted_threads, id;
1454 struct perf_aggr_thread_value *buf;
1455
1456 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads);
1457 if (!buf) {
1458 perror("cannot sort aggr thread");
1459 return;
1460 }
1461
1462 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001463 if (prefix)
1464 fprintf(output, "%s", prefix);
1465
Jin Yao29734552017-12-05 22:03:11 +08001466 id = buf[thread].id;
Jin Yao14e72a22017-12-05 22:03:08 +08001467 if (stat_config.stats)
Jin Yao29734552017-12-05 22:03:11 +08001468 printout(id, 0, buf[thread].counter, buf[thread].uval,
1469 prefix, buf[thread].run, buf[thread].ena, 1.0,
1470 &stat_config.stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001471 else
Jin Yao29734552017-12-05 22:03:11 +08001472 printout(id, 0, buf[thread].counter, buf[thread].uval,
1473 prefix, buf[thread].run, buf[thread].ena, 1.0,
1474 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001475 fputc('\n', output);
1476 }
Jin Yao29734552017-12-05 22:03:11 +08001477
1478 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001479}
1480
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001481struct caggr_data {
1482 double avg, avg_enabled, avg_running;
1483};
1484
1485static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1486 bool first __maybe_unused)
1487{
1488 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001489 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001490
1491 cd->avg += avg_stats(&ps->res_stats[0]);
1492 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1493 cd->avg_running += avg_stats(&ps->res_stats[2]);
1494}
1495
Ingo Molnar42202dd2009-06-13 14:57:28 +02001496/*
1497 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001498 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001499 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001500static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001501{
Jiri Olsa58215222015-07-21 14:31:24 +02001502 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001503 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001504 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001505
Andi Kleen430daf22017-03-20 13:17:00 -07001506 if (!collect_data(counter, counter_aggr_cb, &cd))
1507 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001508
Andi Kleen54b50912016-03-03 15:57:36 -08001509 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001510 fprintf(output, "%s", prefix);
1511
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001512 uval = cd.avg * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001513 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
1514 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001515 if (!metric_only)
1516 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001517}
1518
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001519static void counter_cb(struct perf_evsel *counter, void *data,
1520 bool first __maybe_unused)
1521{
1522 struct aggr_data *ad = data;
1523
1524 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1525 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1526 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1527}
1528
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001529/*
1530 * Print out the results of a single counter:
1531 * does not use aggregated count in system-wide
1532 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001533static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001534{
Jiri Olsa58215222015-07-21 14:31:24 +02001535 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001536 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001537 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001538 int cpu;
1539
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001540 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001541 struct aggr_data ad = { .cpu = cpu };
1542
Andi Kleen430daf22017-03-20 13:17:00 -07001543 if (!collect_data(counter, counter_cb, &ad))
1544 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001545 val = ad.val;
1546 ena = ad.ena;
1547 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001548
1549 if (prefix)
1550 fprintf(output, "%s", prefix);
1551
Stephane Eranian410136f2013-11-12 17:58:49 +01001552 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001553 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1554 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001555
Stephane Eranian4aa90152011-08-15 22:22:33 +02001556 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001557 }
1558}
1559
Andi Kleen206cab62016-03-03 15:57:37 -08001560static void print_no_aggr_metric(char *prefix)
1561{
1562 int cpu;
1563 int nrcpus = 0;
1564 struct perf_evsel *counter;
1565 u64 ena, run, val;
1566 double uval;
1567
1568 nrcpus = evsel_list->cpus->nr;
1569 for (cpu = 0; cpu < nrcpus; cpu++) {
1570 bool first = true;
1571
1572 if (prefix)
1573 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001574 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001575 if (is_duration_time(counter))
1576 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001577 if (first) {
1578 aggr_printout(counter, cpu, 0);
1579 first = false;
1580 }
1581 val = perf_counts(counter->counts, cpu, 0)->val;
1582 ena = perf_counts(counter->counts, cpu, 0)->ena;
1583 run = perf_counts(counter->counts, cpu, 0)->run;
1584
1585 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001586 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1587 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001588 }
1589 fputc('\n', stat_config.output);
1590 }
1591}
1592
Andi Kleen54b50912016-03-03 15:57:36 -08001593static int aggr_header_lens[] = {
1594 [AGGR_CORE] = 18,
1595 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001596 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001597 [AGGR_THREAD] = 24,
1598 [AGGR_GLOBAL] = 0,
1599};
1600
Andi Kleenc51fd632016-05-24 12:52:39 -07001601static const char *aggr_header_csv[] = {
1602 [AGGR_CORE] = "core,cpus,",
1603 [AGGR_SOCKET] = "socket,cpus",
1604 [AGGR_NONE] = "cpu,",
1605 [AGGR_THREAD] = "comm-pid,",
1606 [AGGR_GLOBAL] = ""
1607};
1608
Andi Kleen41c8ca22016-05-24 12:52:38 -07001609static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001610{
1611 struct perf_stat_output_ctx out;
1612 struct perf_evsel *counter;
1613 struct outstate os = {
1614 .fh = stat_config.output
1615 };
1616
1617 if (prefix)
1618 fprintf(stat_config.output, "%s", prefix);
1619
Andi Kleen41c8ca22016-05-24 12:52:38 -07001620 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001621 fprintf(stat_config.output, "%*s",
1622 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001623 if (csv_output) {
1624 if (stat_config.interval)
1625 fputs("time,", stat_config.output);
1626 fputs(aggr_header_csv[stat_config.aggr_mode],
1627 stat_config.output);
1628 }
Andi Kleen54b50912016-03-03 15:57:36 -08001629
1630 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001631 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001632 if (is_duration_time(counter))
1633 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001634 os.evsel = counter;
1635 out.ctx = &os;
1636 out.print_metric = print_metric_header;
1637 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001638 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001639 os.evsel = counter;
1640 perf_stat__print_shadow_stats(counter, 0,
1641 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001642 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001643 &metric_events,
1644 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001645 }
1646 fputc('\n', stat_config.output);
1647}
1648
Jiri Olsad4f63a42015-06-26 11:29:26 +02001649static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001650{
Jiri Olsa58215222015-07-21 14:31:24 +02001651 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001652 static int num_print_interval;
1653
Jiri Olsa9660e082018-06-07 00:15:06 +02001654 if (interval_clear)
1655 puts(CONSOLE_CLEAR);
1656
Jiri Olsad4f63a42015-06-26 11:29:26 +02001657 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1658
Jiri Olsa9660e082018-06-07 00:15:06 +02001659 if ((num_print_interval == 0 && !csv_output) || interval_clear) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001660 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001661 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001662 fprintf(output, "# time socket cpus");
1663 if (!metric_only)
1664 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001665 break;
1666 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001667 fprintf(output, "# time core cpus");
1668 if (!metric_only)
1669 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001670 break;
1671 case AGGR_NONE:
Jiri Olsaf5155722018-06-07 00:15:08 +02001672 fprintf(output, "# time CPU ");
Andi Kleen41c8ca22016-05-24 12:52:38 -07001673 if (!metric_only)
1674 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001675 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001676 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001677 fprintf(output, "# time comm-pid");
1678 if (!metric_only)
1679 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001680 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001681 case AGGR_GLOBAL:
1682 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001683 fprintf(output, "# time");
1684 if (!metric_only)
1685 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001686 case AGGR_UNSET:
1687 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001688 }
1689 }
1690
Jiri Olsac818cc02018-07-02 15:42:02 +02001691 if ((num_print_interval == 0 || interval_clear) && metric_only)
Andi Kleen41c8ca22016-05-24 12:52:38 -07001692 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001693 if (++num_print_interval == 25)
1694 num_print_interval = 0;
1695}
1696
1697static void print_header(int argc, const char **argv)
1698{
Jiri Olsa58215222015-07-21 14:31:24 +02001699 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001700 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001701
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001702 fflush(stdout);
1703
Stephane Eraniand7470b62010-12-01 18:49:05 +02001704 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001705 fprintf(output, "\n");
1706 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001707 if (target.system_wide)
1708 fprintf(output, "\'system wide");
1709 else if (target.cpu_list)
1710 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001711 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001712 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1713 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001714 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001715 } else if (target.pid)
1716 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001717 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001718 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001719
Stephane Eranian4aa90152011-08-15 22:22:33 +02001720 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001721 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001722 fprintf(output, " (%d runs)", run_count);
1723 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001724 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001725}
1726
Jiri Olsabc22de92018-04-23 11:08:20 +02001727static int get_precision(double num)
1728{
1729 if (num > 1)
1730 return 0;
1731
1732 return lround(ceil(-log10(num)));
1733}
1734
Jiri Olsae55c14a2018-04-23 11:08:21 +02001735static void print_table(FILE *output, int precision, double avg)
1736{
1737 char tmp[64];
1738 int idx, indent = 0;
1739
1740 scnprintf(tmp, 64, " %17.*f", precision, avg);
1741 while (tmp[indent] == ' ')
1742 indent++;
1743
1744 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1745
1746 for (idx = 0; idx < run_count; idx++) {
1747 double run = (double) walltime_run[idx] / NSEC_PER_SEC;
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001748 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001749
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001750 fprintf(output, " %17.*f (%+.*f) ",
Jiri Olsae55c14a2018-04-23 11:08:21 +02001751 precision, run, precision, run - avg);
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001752
1753 for (h = 0; h < n; h++)
1754 fprintf(output, "#");
1755
1756 fprintf(output, "\n");
Jiri Olsae55c14a2018-04-23 11:08:21 +02001757 }
1758
1759 fprintf(output, "\n%*s# Final result:\n", indent, "");
1760}
1761
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001762static double timeval2double(struct timeval *t)
1763{
1764 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1765}
1766
Jiri Olsad4f63a42015-06-26 11:29:26 +02001767static void print_footer(void)
1768{
Jiri Olsabc22de92018-04-23 11:08:20 +02001769 double avg = avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsa58215222015-07-21 14:31:24 +02001770 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001771 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001772
Jiri Olsad4f63a42015-06-26 11:29:26 +02001773 if (!null_run)
1774 fprintf(output, "\n");
Jiri Olsabc22de92018-04-23 11:08:20 +02001775
1776 if (run_count == 1) {
1777 fprintf(output, " %17.9f seconds time elapsed", avg);
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001778
1779 if (ru_display) {
1780 double ru_utime = timeval2double(&ru_data.ru_utime);
1781 double ru_stime = timeval2double(&ru_data.ru_stime);
1782
1783 fprintf(output, "\n\n");
1784 fprintf(output, " %17.9f seconds user\n", ru_utime);
1785 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1786 }
Jiri Olsabc22de92018-04-23 11:08:20 +02001787 } else {
1788 double sd = stddev_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
1789 /*
1790 * Display at most 2 more significant
1791 * digits than the stddev inaccuracy.
1792 */
1793 int precision = get_precision(sd) + 2;
1794
Jiri Olsae55c14a2018-04-23 11:08:21 +02001795 if (walltime_run_table)
1796 print_table(output, precision, avg);
1797
Jiri Olsabc22de92018-04-23 11:08:20 +02001798 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1799 precision, avg, precision, sd);
1800
1801 print_noise_pct(sd, avg);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001802 }
1803 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001804
Andi Kleen918c7b062017-05-22 18:00:16 -07001805 if (print_free_counters_hint &&
1806 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1807 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001808 fprintf(output,
1809"Some events weren't counted. Try disabling the NMI watchdog:\n"
1810" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1811" perf stat ...\n"
1812" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001813
1814 if (print_mixed_hw_group_error)
1815 fprintf(output,
1816 "The events in group usually have to be from "
1817 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001818}
1819
1820static void print_counters(struct timespec *ts, int argc, const char **argv)
1821{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001822 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001823 struct perf_evsel *counter;
1824 char buf[64], *prefix = NULL;
1825
Jiri Olsa664c98d2015-11-05 15:40:50 +01001826 /* Do not print anything if we record to the pipe. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001827 if (STAT_RECORD && perf_stat.data.is_pipe)
Jiri Olsa664c98d2015-11-05 15:40:50 +01001828 return;
1829
Jiri Olsad4f63a42015-06-26 11:29:26 +02001830 if (interval)
1831 print_interval(prefix = buf, ts);
1832 else
1833 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001834
Andi Kleen54b50912016-03-03 15:57:36 -08001835 if (metric_only) {
1836 static int num_print_iv;
1837
Andi Kleen41c8ca22016-05-24 12:52:38 -07001838 if (num_print_iv == 0 && !interval)
1839 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001840 if (num_print_iv++ == 25)
1841 num_print_iv = 0;
1842 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1843 fprintf(stat_config.output, "%s", prefix);
1844 }
1845
Jiri Olsa421a50f2015-07-21 14:31:22 +02001846 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001847 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001848 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001849 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001850 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001851 case AGGR_THREAD:
Andi Kleene864c5c2017-08-31 12:40:35 -07001852 evlist__for_each_entry(evsel_list, counter) {
1853 if (is_duration_time(counter))
1854 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001855 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001856 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001857 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001858 case AGGR_GLOBAL:
Andi Kleene864c5c2017-08-31 12:40:35 -07001859 evlist__for_each_entry(evsel_list, counter) {
1860 if (is_duration_time(counter))
1861 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001862 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001863 }
Andi Kleen54b50912016-03-03 15:57:36 -08001864 if (metric_only)
1865 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001866 break;
1867 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001868 if (metric_only)
1869 print_no_aggr_metric(prefix);
1870 else {
Andi Kleene864c5c2017-08-31 12:40:35 -07001871 evlist__for_each_entry(evsel_list, counter) {
1872 if (is_duration_time(counter))
1873 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001874 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001875 }
Andi Kleen206cab62016-03-03 15:57:37 -08001876 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001877 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001878 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001879 default:
1880 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001881 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001882
Jiri Olsad4f63a42015-06-26 11:29:26 +02001883 if (!interval && !csv_output)
1884 print_footer();
1885
Jiri Olsa58215222015-07-21 14:31:24 +02001886 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001887}
1888
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001889static volatile int signr = -1;
1890
Ingo Molnar52425192009-05-26 09:17:18 +02001891static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001892{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001893 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001894 done = 1;
1895
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001896 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001897 /*
1898 * render child_pid harmless
1899 * won't send SIGTERM to a random
1900 * process in case of race condition
1901 * and fast PID recycling
1902 */
1903 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001904}
1905
1906static void sig_atexit(void)
1907{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001908 sigset_t set, oset;
1909
1910 /*
1911 * avoid race condition with SIGCHLD handler
1912 * in skip_signal() which is modifying child_pid
1913 * goal is to avoid send SIGTERM to a random
1914 * process
1915 */
1916 sigemptyset(&set);
1917 sigaddset(&set, SIGCHLD);
1918 sigprocmask(SIG_BLOCK, &set, &oset);
1919
Chris Wilson933da832009-10-04 01:35:01 +01001920 if (child_pid != -1)
1921 kill(child_pid, SIGTERM);
1922
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001923 sigprocmask(SIG_SETMASK, &oset, NULL);
1924
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001925 if (signr == -1)
1926 return;
1927
1928 signal(signr, SIG_DFL);
1929 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001930}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001931
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001932static int stat__set_big_num(const struct option *opt __maybe_unused,
1933 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001934{
1935 big_num_opt = unset ? 0 : 1;
1936 return 0;
1937}
1938
Andi Kleen44b1e602016-05-30 12:49:42 -03001939static int enable_metric_only(const struct option *opt __maybe_unused,
1940 const char *s __maybe_unused, int unset)
1941{
1942 force_metric_only = true;
1943 metric_only = !unset;
1944 return 0;
1945}
1946
Andi Kleenb18f3e32017-08-31 12:40:31 -07001947static int parse_metric_groups(const struct option *opt,
1948 const char *str,
1949 int unset __maybe_unused)
1950{
1951 return metricgroup__parse_groups(opt, str, &metric_events);
1952}
1953
Jiri Olsae0547312015-11-05 15:40:45 +01001954static const struct option stat_options[] = {
1955 OPT_BOOLEAN('T', "transaction", &transaction_run,
1956 "hardware transaction statistics"),
1957 OPT_CALLBACK('e', "event", &evsel_list, "event",
1958 "event selector. use 'perf list' to list available events",
1959 parse_events_option),
1960 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1961 "event filter", parse_filter),
Jiri Olsa5698f262018-08-30 08:32:12 +02001962 OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
Jiri Olsae0547312015-11-05 15:40:45 +01001963 "child tasks do not inherit counters"),
1964 OPT_STRING('p', "pid", &target.pid, "pid",
1965 "stat events on existing process id"),
1966 OPT_STRING('t', "tid", &target.tid, "tid",
1967 "stat events on existing thread id"),
1968 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1969 "system-wide collection from all CPUs"),
1970 OPT_BOOLEAN('g', "group", &group,
1971 "put the counters into a counter group"),
1972 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1973 OPT_INCR('v', "verbose", &verbose,
1974 "be more verbose (show counter open errors, etc)"),
1975 OPT_INTEGER('r', "repeat", &run_count,
1976 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsae55c14a2018-04-23 11:08:21 +02001977 OPT_BOOLEAN(0, "table", &walltime_run_table,
1978 "display details about each run (only with -r option)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001979 OPT_BOOLEAN('n', "null", &null_run,
1980 "null run - dont start any counters"),
1981 OPT_INCR('d', "detailed", &detailed_run,
1982 "detailed run - start a lot of events"),
1983 OPT_BOOLEAN('S', "sync", &sync_run,
1984 "call sync() before starting a run"),
1985 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1986 "print large numbers with thousands\' separators",
1987 stat__set_big_num),
1988 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1989 "list of cpus to monitor in system-wide"),
1990 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1991 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001992 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001993 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1994 "print counts with custom separator"),
1995 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1996 "monitor event in cgroup name only", parse_cgroups),
1997 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1998 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1999 OPT_INTEGER(0, "log-fd", &output_fd,
2000 "log output to fd, instead of stderr"),
2001 OPT_STRING(0, "pre", &pre_cmd, "command",
2002 "command to run prior to the measured command"),
2003 OPT_STRING(0, "post", &post_cmd, "command",
2004 "command to run after to the measured command"),
2005 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03002006 "print counts at regular interval in ms "
2007 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01002008 OPT_INTEGER(0, "interval-count", &stat_config.times,
2009 "print counts for fixed number of times"),
Jiri Olsa9660e082018-06-07 00:15:06 +02002010 OPT_BOOLEAN(0, "interval-clear", &interval_clear,
2011 "clear screen in between new interval"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002012 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
2013 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01002014 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
2015 "aggregate counts per processor socket", AGGR_SOCKET),
2016 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
2017 "aggregate counts per physical processor core", AGGR_CORE),
2018 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
2019 "aggregate counts per thread", AGGR_THREAD),
Jiri Olsa728c0ee2018-08-30 08:32:11 +02002020 OPT_UINTEGER('D', "delay", &stat_config.initial_delay,
Jiri Olsae0547312015-11-05 15:40:45 +01002021 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03002022 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
2023 "Only print computed metrics. No raw values", enable_metric_only),
2024 OPT_BOOLEAN(0, "topdown", &topdown_run,
2025 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07002026 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
2027 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07002028 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
2029 "monitor specified metrics or metric groups (separated by ,)",
2030 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01002031 OPT_END()
2032};
2033
Jiri Olsa1fe7a302015-10-16 12:41:15 +02002034static int perf_stat__get_socket(struct cpu_map *map, int cpu)
2035{
2036 return cpu_map__get_socket(map, cpu, NULL);
2037}
2038
2039static int perf_stat__get_core(struct cpu_map *map, int cpu)
2040{
2041 return cpu_map__get_core(map, cpu, NULL);
2042}
2043
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002044static int cpu_map__get_max(struct cpu_map *map)
2045{
2046 int i, max = -1;
2047
2048 for (i = 0; i < map->nr; i++) {
2049 if (map->map[i] > max)
2050 max = map->map[i];
2051 }
2052
2053 return max;
2054}
2055
2056static struct cpu_map *cpus_aggr_map;
2057
2058static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
2059{
2060 int cpu;
2061
2062 if (idx >= map->nr)
2063 return -1;
2064
2065 cpu = map->map[idx];
2066
2067 if (cpus_aggr_map->map[cpu] == -1)
2068 cpus_aggr_map->map[cpu] = get_id(map, idx);
2069
2070 return cpus_aggr_map->map[cpu];
2071}
2072
2073static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
2074{
2075 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
2076}
2077
2078static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
2079{
2080 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
2081}
2082
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002083static int perf_stat_init_aggr_mode(void)
2084{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002085 int nr;
2086
Jiri Olsa421a50f2015-07-21 14:31:22 +02002087 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002088 case AGGR_SOCKET:
2089 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
2090 perror("cannot build socket map");
2091 return -1;
2092 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002093 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002094 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002095 case AGGR_CORE:
2096 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
2097 perror("cannot build core map");
2098 return -1;
2099 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002100 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002101 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002102 case AGGR_NONE:
2103 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002104 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002105 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002106 default:
2107 break;
2108 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002109
2110 /*
2111 * The evsel_list->cpus is the base we operate on,
2112 * taking the highest cpu number to be the size of
2113 * the aggregation translate cpumap.
2114 */
2115 nr = cpu_map__get_max(evsel_list->cpus);
2116 cpus_aggr_map = cpu_map__empty_new(nr + 1);
2117 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002118}
2119
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002120static void perf_stat__exit_aggr_mode(void)
2121{
2122 cpu_map__put(aggr_map);
2123 cpu_map__put(cpus_aggr_map);
2124 aggr_map = NULL;
2125 cpus_aggr_map = NULL;
2126}
2127
Jiri Olsa68d702f2015-11-05 15:40:58 +01002128static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2129{
2130 int cpu;
2131
2132 if (idx > map->nr)
2133 return -1;
2134
2135 cpu = map->map[idx];
2136
Jan Stancekda8a58b2017-02-17 12:10:26 +01002137 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002138 return -1;
2139
2140 return cpu;
2141}
2142
2143static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2144{
2145 struct perf_env *env = data;
2146 int cpu = perf_env__get_cpu(env, map, idx);
2147
2148 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2149}
2150
2151static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2152{
2153 struct perf_env *env = data;
2154 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2155
2156 if (cpu != -1) {
2157 int socket_id = env->cpu[cpu].socket_id;
2158
2159 /*
2160 * Encode socket in upper 16 bits
2161 * core_id is relative to socket, and
2162 * we need a global id. So we combine
2163 * socket + core id.
2164 */
2165 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2166 }
2167
2168 return core;
2169}
2170
2171static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2172 struct cpu_map **sockp)
2173{
2174 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2175}
2176
2177static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2178 struct cpu_map **corep)
2179{
2180 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2181}
2182
2183static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2184{
2185 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2186}
2187
2188static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2189{
2190 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2191}
2192
2193static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2194{
2195 struct perf_env *env = &st->session->header.env;
2196
2197 switch (stat_config.aggr_mode) {
2198 case AGGR_SOCKET:
2199 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2200 perror("cannot build socket map");
2201 return -1;
2202 }
2203 aggr_get_id = perf_stat__get_socket_file;
2204 break;
2205 case AGGR_CORE:
2206 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2207 perror("cannot build core map");
2208 return -1;
2209 }
2210 aggr_get_id = perf_stat__get_core_file;
2211 break;
2212 case AGGR_NONE:
2213 case AGGR_GLOBAL:
2214 case AGGR_THREAD:
2215 case AGGR_UNSET:
2216 default:
2217 break;
2218 }
2219
2220 return 0;
2221}
2222
Andi Kleen44b1e602016-05-30 12:49:42 -03002223static int topdown_filter_events(const char **attr, char **str, bool use_group)
2224{
2225 int off = 0;
2226 int i;
2227 int len = 0;
2228 char *s;
2229
2230 for (i = 0; attr[i]; i++) {
2231 if (pmu_have_event("cpu", attr[i])) {
2232 len += strlen(attr[i]) + 1;
2233 attr[i - off] = attr[i];
2234 } else
2235 off++;
2236 }
2237 attr[i - off] = NULL;
2238
2239 *str = malloc(len + 1 + 2);
2240 if (!*str)
2241 return -1;
2242 s = *str;
2243 if (i - off == 0) {
2244 *s = 0;
2245 return 0;
2246 }
2247 if (use_group)
2248 *s++ = '{';
2249 for (i = 0; attr[i]; i++) {
2250 strcpy(s, attr[i]);
2251 s += strlen(s);
2252 *s++ = ',';
2253 }
2254 if (use_group) {
2255 s[-1] = '}';
2256 *s = 0;
2257 } else
2258 s[-1] = 0;
2259 return 0;
2260}
2261
2262__weak bool arch_topdown_check_group(bool *warn)
2263{
2264 *warn = false;
2265 return false;
2266}
2267
2268__weak void arch_topdown_group_warn(void)
2269{
2270}
2271
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002272/*
2273 * Add default attributes, if there were no attributes specified or
2274 * if -d/--detailed, -d -d or -d -d -d is used:
2275 */
2276static int add_default_attributes(void)
2277{
Andi Kleen44b1e602016-05-30 12:49:42 -03002278 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002279 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002280
2281 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2282 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2283 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2284 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2285
2286 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002287};
2288 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002289 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002290};
2291 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002292 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002293};
2294 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002295 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2296 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2297 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2298
2299};
2300
2301/*
2302 * Detailed stats (-d), covering the L1 and last level data caches:
2303 */
2304 struct perf_event_attr detailed_attrs[] = {
2305
2306 { .type = PERF_TYPE_HW_CACHE,
2307 .config =
2308 PERF_COUNT_HW_CACHE_L1D << 0 |
2309 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2310 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2311
2312 { .type = PERF_TYPE_HW_CACHE,
2313 .config =
2314 PERF_COUNT_HW_CACHE_L1D << 0 |
2315 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2316 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2317
2318 { .type = PERF_TYPE_HW_CACHE,
2319 .config =
2320 PERF_COUNT_HW_CACHE_LL << 0 |
2321 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2322 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2323
2324 { .type = PERF_TYPE_HW_CACHE,
2325 .config =
2326 PERF_COUNT_HW_CACHE_LL << 0 |
2327 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2328 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2329};
2330
2331/*
2332 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2333 */
2334 struct perf_event_attr very_detailed_attrs[] = {
2335
2336 { .type = PERF_TYPE_HW_CACHE,
2337 .config =
2338 PERF_COUNT_HW_CACHE_L1I << 0 |
2339 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2340 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2341
2342 { .type = PERF_TYPE_HW_CACHE,
2343 .config =
2344 PERF_COUNT_HW_CACHE_L1I << 0 |
2345 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2346 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2347
2348 { .type = PERF_TYPE_HW_CACHE,
2349 .config =
2350 PERF_COUNT_HW_CACHE_DTLB << 0 |
2351 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2352 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2353
2354 { .type = PERF_TYPE_HW_CACHE,
2355 .config =
2356 PERF_COUNT_HW_CACHE_DTLB << 0 |
2357 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2358 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2359
2360 { .type = PERF_TYPE_HW_CACHE,
2361 .config =
2362 PERF_COUNT_HW_CACHE_ITLB << 0 |
2363 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2364 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2365
2366 { .type = PERF_TYPE_HW_CACHE,
2367 .config =
2368 PERF_COUNT_HW_CACHE_ITLB << 0 |
2369 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2370 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2371
2372};
2373
2374/*
2375 * Very, very detailed stats (-d -d -d), adding prefetch events:
2376 */
2377 struct perf_event_attr very_very_detailed_attrs[] = {
2378
2379 { .type = PERF_TYPE_HW_CACHE,
2380 .config =
2381 PERF_COUNT_HW_CACHE_L1D << 0 |
2382 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2383 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2384
2385 { .type = PERF_TYPE_HW_CACHE,
2386 .config =
2387 PERF_COUNT_HW_CACHE_L1D << 0 |
2388 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2389 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2390};
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002391 struct parse_events_error errinfo;
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002392
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002393 /* Set attrs if no event is selected and !null_run: */
2394 if (null_run)
2395 return 0;
2396
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002397 if (transaction_run) {
Thomas Richter742d92f2018-06-26 09:17:01 +02002398 /* Handle -T as -M transaction. Once platform specific metrics
2399 * support has been added to the json files, all archictures
2400 * will use this approach. To determine transaction support
2401 * on an architecture test for such a metric name.
2402 */
2403 if (metricgroup__has_metric("transaction")) {
2404 struct option opt = { .value = &evsel_list };
2405
2406 return metricgroup__parse_groups(&opt, "transaction",
2407 &metric_events);
2408 }
2409
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002410 if (pmu_have_event("cpu", "cycles-ct") &&
2411 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002412 err = parse_events(evsel_list, transaction_attrs,
2413 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002414 else
Thomas Richterfca323402018-03-08 15:57:35 +01002415 err = parse_events(evsel_list,
2416 transaction_limited_attrs,
2417 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002418 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002419 fprintf(stderr, "Cannot set up transaction events\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002420 parse_events_print_error(&errinfo, transaction_attrs);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002421 return -1;
2422 }
2423 return 0;
2424 }
2425
Kan Liangdaefd0b2017-05-26 12:05:38 -07002426 if (smi_cost) {
2427 int smi;
2428
2429 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2430 fprintf(stderr, "freeze_on_smi is not supported.\n");
2431 return -1;
2432 }
2433
2434 if (!smi) {
2435 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2436 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2437 return -1;
2438 }
2439 smi_reset = true;
2440 }
2441
2442 if (pmu_have_event("msr", "aperf") &&
2443 pmu_have_event("msr", "smi")) {
2444 if (!force_metric_only)
2445 metric_only = true;
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002446 err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002447 } else {
2448 fprintf(stderr, "To measure SMI cost, it needs "
2449 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002450 parse_events_print_error(&errinfo, smi_cost_attrs);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002451 return -1;
2452 }
2453 if (err) {
2454 fprintf(stderr, "Cannot set up SMI cost events\n");
2455 return -1;
2456 }
2457 return 0;
2458 }
2459
Andi Kleen44b1e602016-05-30 12:49:42 -03002460 if (topdown_run) {
2461 char *str = NULL;
2462 bool warn = false;
2463
2464 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2465 stat_config.aggr_mode != AGGR_CORE) {
2466 pr_err("top down event configuration requires --per-core mode\n");
2467 return -1;
2468 }
2469 stat_config.aggr_mode = AGGR_CORE;
2470 if (nr_cgroups || !target__has_cpu(&target)) {
2471 pr_err("top down event configuration requires system-wide mode (-a)\n");
2472 return -1;
2473 }
2474
2475 if (!force_metric_only)
2476 metric_only = true;
2477 if (topdown_filter_events(topdown_attrs, &str,
2478 arch_topdown_check_group(&warn)) < 0) {
2479 pr_err("Out of memory\n");
2480 return -1;
2481 }
2482 if (topdown_attrs[0] && str) {
2483 if (warn)
2484 arch_topdown_group_warn();
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002485 err = parse_events(evsel_list, str, &errinfo);
Andi Kleen44b1e602016-05-30 12:49:42 -03002486 if (err) {
2487 fprintf(stderr,
2488 "Cannot set up top down events %s: %d\n",
2489 str, err);
2490 free(str);
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002491 parse_events_print_error(&errinfo, str);
Andi Kleen44b1e602016-05-30 12:49:42 -03002492 return -1;
2493 }
2494 } else {
2495 fprintf(stderr, "System does not support topdown\n");
2496 return -1;
2497 }
2498 free(str);
2499 }
2500
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002501 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002502 if (target__has_cpu(&target))
2503 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2504
Andi Kleen9dec4472016-02-26 16:27:56 -08002505 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2506 return -1;
2507 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2508 if (perf_evlist__add_default_attrs(evsel_list,
2509 frontend_attrs) < 0)
2510 return -1;
2511 }
2512 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2513 if (perf_evlist__add_default_attrs(evsel_list,
2514 backend_attrs) < 0)
2515 return -1;
2516 }
2517 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002518 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002519 }
2520
2521 /* Detailed events get appended to the event list: */
2522
2523 if (detailed_run < 1)
2524 return 0;
2525
2526 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002527 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002528 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002529
2530 if (detailed_run < 2)
2531 return 0;
2532
2533 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002534 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002535 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002536
2537 if (detailed_run < 3)
2538 return 0;
2539
2540 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002541 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002542}
2543
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002544static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002545 "perf stat record [<options>]",
2546 NULL,
2547};
2548
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002549static void init_features(struct perf_session *session)
2550{
2551 int feat;
2552
2553 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2554 perf_header__set_feat(&session->header, feat);
2555
2556 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2557 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2558 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2559 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2560}
2561
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002562static int __cmd_record(int argc, const char **argv)
2563{
2564 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002565 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002566
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002567 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002568 PARSE_OPT_STOP_AT_NON_OPTION);
2569
2570 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002571 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002572
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002573 if (run_count != 1 || forever) {
2574 pr_err("Cannot use -r option with perf stat record.\n");
2575 return -1;
2576 }
2577
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002578 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002579 if (session == NULL) {
2580 pr_err("Perf session creation failed.\n");
2581 return -1;
2582 }
2583
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002584 init_features(session);
2585
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002586 session->evlist = evsel_list;
2587 perf_stat.session = session;
2588 perf_stat.record = true;
2589 return argc;
2590}
2591
Jiri Olsaa56f9392015-11-05 15:40:59 +01002592static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2593 union perf_event *event,
2594 struct perf_session *session)
2595{
Andi Kleene3b03b62016-05-05 16:04:03 -07002596 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002597 struct perf_evsel *counter;
2598 struct timespec tsh, *ts = NULL;
2599 const char **argv = session->header.env.cmdline_argv;
2600 int argc = session->header.env.nr_cmdline;
2601
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002602 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002603 perf_stat_process_counter(&stat_config, counter);
2604
Andi Kleene3b03b62016-05-05 16:04:03 -07002605 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2606 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002607
Andi Kleene3b03b62016-05-05 16:04:03 -07002608 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002609 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2610 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002611 ts = &tsh;
2612 }
2613
2614 print_counters(ts, argc, argv);
2615 return 0;
2616}
2617
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002618static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002619int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002620 union perf_event *event,
2621 struct perf_session *session __maybe_unused)
2622{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002623 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2624
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002625 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002626
Jiri Olsa89af4e02015-11-05 15:41:02 +01002627 if (cpu_map__empty(st->cpus)) {
2628 if (st->aggr_mode != AGGR_UNSET)
2629 pr_warning("warning: processing task data, aggregation mode not set\n");
2630 return 0;
2631 }
2632
2633 if (st->aggr_mode != AGGR_UNSET)
2634 stat_config.aggr_mode = st->aggr_mode;
2635
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002636 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002637 perf_stat_init_aggr_mode();
2638 else
2639 perf_stat_init_aggr_mode_file(st);
2640
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002641 return 0;
2642}
2643
Jiri Olsa1975d362015-11-05 15:40:56 +01002644static int set_maps(struct perf_stat *st)
2645{
2646 if (!st->cpus || !st->threads)
2647 return 0;
2648
2649 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2650 return -EINVAL;
2651
2652 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2653
2654 if (perf_evlist__alloc_stats(evsel_list, true))
2655 return -ENOMEM;
2656
2657 st->maps_allocated = true;
2658 return 0;
2659}
2660
2661static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002662int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002663 union perf_event *event,
2664 struct perf_session *session __maybe_unused)
2665{
2666 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2667
2668 if (st->threads) {
2669 pr_warning("Extra thread map event, ignoring.\n");
2670 return 0;
2671 }
2672
2673 st->threads = thread_map__new_event(&event->thread_map);
2674 if (!st->threads)
2675 return -ENOMEM;
2676
2677 return set_maps(st);
2678}
2679
2680static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002681int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002682 union perf_event *event,
2683 struct perf_session *session __maybe_unused)
2684{
2685 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2686 struct cpu_map *cpus;
2687
2688 if (st->cpus) {
2689 pr_warning("Extra cpu map event, ignoring.\n");
2690 return 0;
2691 }
2692
2693 cpus = cpu_map__new_data(&event->cpu_map.data);
2694 if (!cpus)
2695 return -ENOMEM;
2696
2697 st->cpus = cpus;
2698 return set_maps(st);
2699}
2700
Jin Yao56739442017-12-05 22:03:07 +08002701static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2702{
2703 int i;
2704
2705 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2706 if (!config->stats)
2707 return -1;
2708
2709 config->stats_num = nthreads;
2710
2711 for (i = 0; i < nthreads; i++)
2712 runtime_stat__init(&config->stats[i]);
2713
2714 return 0;
2715}
2716
2717static void runtime_stat_delete(struct perf_stat_config *config)
2718{
2719 int i;
2720
2721 if (!config->stats)
2722 return;
2723
2724 for (i = 0; i < config->stats_num; i++)
2725 runtime_stat__exit(&config->stats[i]);
2726
2727 free(config->stats);
2728}
2729
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002730static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002731 "perf stat report [<options>]",
2732 NULL,
2733};
2734
2735static struct perf_stat perf_stat = {
2736 .tool = {
2737 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002738 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002739 .thread_map = process_thread_map_event,
2740 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002741 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002742 .stat = perf_event__process_stat_event,
2743 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002744 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002745 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002746};
2747
2748static int __cmd_report(int argc, const char **argv)
2749{
2750 struct perf_session *session;
2751 const struct option options[] = {
2752 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002753 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2754 "aggregate counts per processor socket", AGGR_SOCKET),
2755 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2756 "aggregate counts per physical processor core", AGGR_CORE),
2757 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2758 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002759 OPT_END()
2760 };
2761 struct stat st;
2762 int ret;
2763
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002764 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002765
2766 if (!input_name || !strlen(input_name)) {
2767 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2768 input_name = "-";
2769 else
2770 input_name = "perf.data";
2771 }
2772
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002773 perf_stat.data.file.path = input_name;
2774 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002775
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002776 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002777 if (session == NULL)
2778 return -1;
2779
2780 perf_stat.session = session;
2781 stat_config.output = stderr;
2782 evsel_list = session->evlist;
2783
2784 ret = perf_session__process_events(session);
2785 if (ret)
2786 return ret;
2787
2788 perf_session__delete(session);
2789 return 0;
2790}
2791
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002792static void setup_system_wide(int forks)
2793{
2794 /*
2795 * Make system wide (-a) the default target if
2796 * no target was specified and one of following
2797 * conditions is met:
2798 *
2799 * - there's no workload specified
2800 * - there is workload specified but all requested
2801 * events are system wide events
2802 */
2803 if (!target__none(&target))
2804 return;
2805
2806 if (!forks)
2807 target.system_wide = true;
2808 else {
2809 struct perf_evsel *counter;
2810
2811 evlist__for_each_entry(evsel_list, counter) {
2812 if (!counter->system_wide)
2813 return;
2814 }
2815
2816 if (evsel_list->nr_entries)
2817 target.system_wide = true;
2818 }
2819}
2820
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002821int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002822{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002823 const char * const stat_usage[] = {
2824 "perf stat [<options>] [<command>]",
2825 NULL
2826 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002827 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002828 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002829 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002830 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002831 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002832
Stephane Eranian5af52b52010-05-18 15:00:01 +02002833 setlocale(LC_ALL, "");
2834
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002835 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002836 if (evsel_list == NULL)
2837 return -ENOMEM;
2838
Wang Nan1669e502016-02-19 11:43:58 +00002839 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002840 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2841 (const char **) stat_usage,
2842 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002843 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002844 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002845
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002846 if (csv_sep) {
2847 csv_output = true;
2848 if (!strcmp(csv_sep, "\\t"))
2849 csv_sep = "\t";
2850 } else
2851 csv_sep = DEFAULT_SEPARATOR;
2852
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002853 if (argc && !strncmp(argv[0], "rec", 3)) {
2854 argc = __cmd_record(argc, argv);
2855 if (argc < 0)
2856 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002857 } else if (argc && !strncmp(argv[0], "rep", 3))
2858 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002859
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002860 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002861 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002862
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002863 /*
2864 * For record command the -o is already taken care of.
2865 */
2866 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002867 output = NULL;
2868
Jim Cromie56f3bae2011-09-07 17:14:00 -06002869 if (output_name && output_fd) {
2870 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002871 parse_options_usage(stat_usage, stat_options, "o", 1);
2872 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002873 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002874 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002875
Andi Kleen54b50912016-03-03 15:57:36 -08002876 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2877 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2878 goto out;
2879 }
2880
Andi Kleen54b50912016-03-03 15:57:36 -08002881 if (metric_only && run_count > 1) {
2882 fprintf(stderr, "--metric-only is not supported with -r\n");
2883 goto out;
2884 }
2885
Jiri Olsae55c14a2018-04-23 11:08:21 +02002886 if (walltime_run_table && run_count <= 1) {
2887 fprintf(stderr, "--table is only supported with -r\n");
2888 parse_options_usage(stat_usage, stat_options, "r", 1);
2889 parse_options_usage(NULL, stat_options, "table", 0);
2890 goto out;
2891 }
2892
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002893 if (output_fd < 0) {
2894 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002895 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002896 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002897 }
2898
Stephane Eranian4aa90152011-08-15 22:22:33 +02002899 if (!output) {
2900 struct timespec tm;
2901 mode = append_file ? "a" : "w";
2902
2903 output = fopen(output_name, mode);
2904 if (!output) {
2905 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002906 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002907 }
2908 clock_gettime(CLOCK_REALTIME, &tm);
2909 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002910 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002911 mode = append_file ? "a" : "w";
2912 output = fdopen(output_fd, mode);
2913 if (!output) {
2914 perror("Failed opening logfd");
2915 return -errno;
2916 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002917 }
2918
Jiri Olsa58215222015-07-21 14:31:24 +02002919 stat_config.output = output;
2920
Stephane Eraniand7470b62010-12-01 18:49:05 +02002921 /*
2922 * let the spreadsheet do the pretty-printing
2923 */
2924 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002925 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002926 if (big_num_opt == 1) {
2927 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002928 parse_options_usage(stat_usage, stat_options, "B", 1);
2929 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002930 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002931 } else /* Nope, so disable big number formatting */
2932 big_num = false;
2933 } else if (big_num_opt == 0) /* User passed --no-big-num */
2934 big_num = false;
2935
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002936 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002937
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002938 /*
2939 * Display user/system times only for single
2940 * run and when there's specified tracee.
2941 */
2942 if ((run_count == 1) && target__none(&target))
2943 ru_display = true;
2944
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002945 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002946 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002947 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002948 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002949 } else if (run_count == 0) {
2950 forever = true;
2951 run_count = 1;
2952 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002953
Jiri Olsae55c14a2018-04-23 11:08:21 +02002954 if (walltime_run_table) {
2955 walltime_run = zalloc(run_count * sizeof(walltime_run[0]));
2956 if (!walltime_run) {
2957 pr_err("failed to setup -r option");
2958 goto out;
2959 }
2960 }
2961
Jin Yao1d9f8d12017-12-05 22:03:10 +08002962 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2963 !target__has_task(&target)) {
2964 if (!target.system_wide || target.cpu_list) {
2965 fprintf(stderr, "The --per-thread option is only "
2966 "available when monitoring via -p -t -a "
2967 "options or only --per-thread.\n");
2968 parse_options_usage(NULL, stat_options, "p", 1);
2969 parse_options_usage(NULL, stat_options, "t", 1);
2970 goto out;
2971 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002972 }
2973
2974 /*
2975 * no_aggr, cgroup are for system-wide only
2976 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2977 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002978 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2979 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002980 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002981 fprintf(stderr, "both cgroup and no-aggregation "
2982 "modes only available in system-wide mode\n");
2983
Jiri Olsae0547312015-11-05 15:40:45 +01002984 parse_options_usage(stat_usage, stat_options, "G", 1);
2985 parse_options_usage(NULL, stat_options, "A", 1);
2986 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002987 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002988 }
2989
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002990 if (add_default_attributes())
2991 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002992
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002993 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002994
Jin Yao1d9f8d12017-12-05 22:03:10 +08002995 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2996 target.per_thread = true;
2997
Namhyung Kim77a6f012012-05-07 14:09:04 +09002998 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002999 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09003000 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01003001 parse_options_usage(stat_usage, stat_options, "p", 1);
3002 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003003 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09003004 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01003005 parse_options_usage(stat_usage, stat_options, "C", 1);
3006 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09003007 }
3008 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02003009 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02003010
3011 /*
3012 * Initialize thread_map with comm names,
3013 * so we could print it out on output.
3014 */
Jin Yao56739442017-12-05 22:03:07 +08003015 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02003016 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08003017 if (target.system_wide) {
3018 if (runtime_stat_new(&stat_config,
3019 thread_map__nr(evsel_list->threads))) {
3020 goto out;
3021 }
3022 }
3023 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02003024
yuzhoujiandb06a262018-01-29 10:25:22 +01003025 if (stat_config.times && interval)
3026 interval_count = true;
3027 else if (stat_config.times && !interval) {
3028 pr_err("interval-count option should be used together with "
3029 "interval-print.\n");
3030 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
3031 parse_options_usage(stat_usage, stat_options, "I", 1);
3032 goto out;
3033 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02003034
yuzhoujianf1f8ad52018-01-29 10:25:23 +01003035 if (timeout && timeout < 100) {
3036 if (timeout < 10) {
3037 pr_err("timeout must be >= 10ms.\n");
3038 parse_options_usage(stat_usage, stat_options, "timeout", 0);
3039 goto out;
3040 } else
3041 pr_warning("timeout < 100ms. "
3042 "The overhead percentage could be high in some cases. "
3043 "Please proceed with caution.\n");
3044 }
3045 if (timeout && interval) {
3046 pr_err("timeout option is not supported with interval-print.\n");
3047 parse_options_usage(stat_usage, stat_options, "timeout", 0);
3048 parse_options_usage(stat_usage, stat_options, "I", 1);
3049 goto out;
3050 }
3051
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003052 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003053 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03003054
Stephane Eranian86ee6e12013-02-14 13:57:27 +01003055 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003056 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01003057
Ingo Molnar58d7e992009-05-15 11:03:23 +02003058 /*
Jiri Olsa7d9ad162018-08-30 08:32:14 +02003059 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
3060 * while avoiding that older tools show confusing messages.
3061 *
3062 * However for pipe sessions we need to keep it zero,
3063 * because script's perf_evsel__check_attr is triggered
3064 * by attr->sample_type != 0, and we can't run it on
3065 * stat sessions.
3066 */
3067 stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
3068
3069 /*
Ingo Molnar58d7e992009-05-15 11:03:23 +02003070 * We dont want to block the signals - that would cause
3071 * child tasks to inherit that and Ctrl-C would not work.
3072 * What we want is for Ctrl-C to work in the exec()-ed
3073 * task, but being ignored by perf stat itself:
3074 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02003075 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003076 if (!forever)
3077 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01003078 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02003079 signal(SIGALRM, skip_signal);
3080 signal(SIGABRT, skip_signal);
3081
Ingo Molnar42202dd2009-06-13 14:57:28 +02003082 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003083 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09003084 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02003085 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
3086 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02003087
Jiri Olsae55c14a2018-04-23 11:08:21 +02003088 status = run_perf_stat(argc, argv, run_idx);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003089 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02003090 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02003091 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003092 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02003093 }
3094
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003095 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02003096 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003097
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003098 if (STAT_RECORD) {
3099 /*
3100 * We synthesize the kernel mmap record just so that older tools
3101 * don't emit warnings about not being able to resolve symbols
3102 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3103 * a saner message about no samples being in the perf.data file.
3104 *
3105 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01003106 * in header.c at the moment 'perf stat record' gets introduced, which
3107 * is not really needed once we start adding the stat specific PERF_RECORD_
3108 * records, but the need to suppress the kptr_restrict messages in older
3109 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003110 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003111 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003112 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3113 process_synthesized_event,
3114 &perf_stat.session->machines.host);
3115 if (err) {
3116 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3117 "older tools may produce warnings about this file\n.");
3118 }
3119
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003120 if (!interval) {
3121 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3122 pr_err("failed to write stat round event\n");
3123 }
3124
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003125 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003126 perf_stat.session->header.data_size += perf_stat.bytes_written;
3127 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3128 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003129
3130 perf_session__delete(perf_stat.session);
3131 }
3132
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003133 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003134 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003135out:
Jiri Olsae55c14a2018-04-23 11:08:21 +02003136 free(walltime_run);
3137
Kan Liangdaefd0b2017-05-26 12:05:38 -07003138 if (smi_cost && smi_reset)
3139 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3140
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003141 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003142
3143 runtime_stat_delete(&stat_config);
3144
Ingo Molnar42202dd2009-06-13 14:57:28 +02003145 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003146}