blob: d097b5b47eb81e16b83a8a37f3385aeea124a0ed [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 Eranian2e6cdf92010-05-12 10:40:01 +0200151static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200152static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000153static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200154static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700155static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300156static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700157static bool smi_cost = false;
158static bool smi_reset = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200159static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200160static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200161static const char *csv_sep = NULL;
162static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800163static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200164static const char *pre_cmd = NULL;
165static const char *post_cmd = NULL;
166static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700167static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100168static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500169static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800170static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300171static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700172static bool no_merge = false;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200173static bool walltime_run_table = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100174static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100175static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100176static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100177static bool append_file;
yuzhoujiandb06a262018-01-29 10:25:22 +0100178static bool interval_count;
Jiri Olsa9660e082018-06-07 00:15:06 +0200179static bool interval_clear;
Jiri Olsae0547312015-11-05 15:40:45 +0100180static const char *output_name;
181static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100182static int print_free_counters_hint;
Kan Liang30060ea2018-04-24 11:20:11 -0700183static int print_mixed_hw_group_error;
Jiri Olsae55c14a2018-04-23 11:08:21 +0200184static u64 *walltime_run;
Jiri Olsa0ce2da12018-06-05 14:13:13 +0200185static bool ru_display = false;
186static struct rusage ru_data;
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200187static unsigned int metric_only_len = METRIC_ONLY_LEN;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200188
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100189struct perf_stat {
190 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100191 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100192 struct perf_session *session;
193 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100194 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100195 bool maps_allocated;
196 struct cpu_map *cpus;
197 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100198 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100199};
200
201static struct perf_stat perf_stat;
202#define STAT_RECORD perf_stat.record
203
Liming Wang60666c62009-12-31 16:05:50 +0800204static volatile int done = 0;
205
Jiri Olsa421a50f2015-07-21 14:31:22 +0200206static struct perf_stat_config stat_config = {
207 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200208 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200209};
210
Andi Kleene864c5c2017-08-31 12:40:35 -0700211static bool is_duration_time(struct perf_evsel *evsel)
212{
213 return !strcmp(evsel->name, "duration_time");
214}
215
Stephane Eranian13370a92013-01-29 12:47:44 +0100216static inline void diff_timespec(struct timespec *r, struct timespec *a,
217 struct timespec *b)
218{
219 r->tv_sec = a->tv_sec - b->tv_sec;
220 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300221 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100222 r->tv_sec--;
223 } else {
224 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
225 }
226}
227
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200228static void perf_stat__reset_stats(void)
229{
Jin Yao56739442017-12-05 22:03:07 +0800230 int i;
231
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200232 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200233 perf_stat__reset_shadow_stats();
Jin Yao56739442017-12-05 22:03:07 +0800234
235 for (i = 0; i < stat_config.stats_num; i++)
236 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200237}
238
Jiri Olsacac21422012-11-12 18:34:00 +0100239static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200240{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200241 struct perf_event_attr *attr = &evsel->attr;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200242 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200243
Jiri Olsa82bf3112017-07-26 14:02:06 +0200244 if (stat_config.scale) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200245 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
246 PERF_FORMAT_TOTAL_TIME_RUNNING;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200247 }
248
249 /*
250 * The event is part of non trivial group, let's enable
251 * the group read (for leader) and ID retrieval for all
252 * members.
253 */
254 if (leader->nr_members > 1)
255 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200256
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200257 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300258
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100259 /*
260 * Some events get initialized with sample_(period/type) set,
261 * like tracepoints. Clear it up for counting.
262 */
263 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100264
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100265 /*
266 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
267 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100268 *
269 * However for pipe sessions we need to keep it zero,
270 * because script's perf_evsel__check_attr is triggered
271 * by attr->sample_type != 0, and we can't run it on
272 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100273 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100274 if (!(STAT_RECORD && perf_stat.data.is_pipe))
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100275 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100276
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100277 /*
278 * Disabling all counters initially, they will be enabled
279 * either manually by us or by kernel via enable_on_exec
280 * set later.
281 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100282 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100283 attr->disabled = 1;
284
Jiri Olsac8280ce2015-12-03 10:06:45 +0100285 /*
286 * In case of initial_delay we enable tracee
287 * events manually.
288 */
289 if (target__none(&target) && !initial_delay)
290 attr->enable_on_exec = 1;
291 }
292
Jin Yao1d9f8d12017-12-05 22:03:10 +0800293 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300294 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200295
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300296 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200297}
298
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100299static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100300 union perf_event *event,
301 struct perf_sample *sample __maybe_unused,
302 struct machine *machine __maybe_unused)
303{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100304 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100305 pr_err("failed to write perf data, error: %m\n");
306 return -1;
307 }
308
309 perf_stat.bytes_written += event->header.size;
310 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100311}
312
Jiri Olsa1975d362015-11-05 15:40:56 +0100313static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100314{
Jiri Olsa1975d362015-11-05 15:40:56 +0100315 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100316 process_synthesized_event,
317 NULL);
318}
319
320#define WRITE_STAT_ROUND_EVENT(time, interval) \
321 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
322
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100323#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
324
325static int
326perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
327 struct perf_counts_values *count)
328{
329 struct perf_sample_id *sid = SID(counter, cpu, thread);
330
331 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
332 process_synthesized_event, NULL);
333}
334
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200335/*
336 * Read out the results of a single counter:
337 * do not aggregate counts across CPUs in system-wide mode
338 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200339static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200340{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100341 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100342 int ncpus, cpu, thread;
343
Jin Yao1d9f8d12017-12-05 22:03:10 +0800344 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Mark Rutland00e727b2016-07-15 11:08:10 +0100345 ncpus = perf_evsel__nr_cpus(counter);
346 else
347 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200348
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000349 if (!counter->supported)
350 return -ENOENT;
351
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100352 if (counter->system_wide)
353 nthreads = 1;
354
355 for (thread = 0; thread < nthreads; thread++) {
356 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200357 struct perf_counts_values *count;
358
359 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200360
361 /*
362 * The leader's group read loads data into its group members
363 * (via perf_evsel__read_counter) and sets threir count->loaded.
364 */
365 if (!count->loaded &&
366 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700367 counter->counts->scaled = -1;
368 perf_counts(counter->counts, cpu, thread)->ena = 0;
369 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100370 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700371 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100372
Jiri Olsa82bf3112017-07-26 14:02:06 +0200373 count->loaded = false;
374
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100375 if (STAT_RECORD) {
376 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
377 pr_err("failed to write stat event\n");
378 return -1;
379 }
380 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700381
382 if (verbose > 1) {
383 fprintf(stat_config.output,
384 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
385 perf_evsel__name(counter),
386 cpu,
387 count->val, count->ena, count->run);
388 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100389 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200390 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200391
392 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200393}
394
Mark Rutland3df33ef2016-08-09 14:04:29 +0100395static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200396{
397 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700398 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200399
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300400 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700401 ret = read_counter(counter);
402 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700403 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200404
Stephane Eraniandb49a712017-04-12 11:23:01 -0700405 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200406 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200407 }
408}
409
Jiri Olsaba411a92015-06-26 11:29:24 +0200410static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100411{
Stephane Eranian13370a92013-01-29 12:47:44 +0100412 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100413
Mark Rutland3df33ef2016-08-09 14:04:29 +0100414 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100415
Stephane Eranian13370a92013-01-29 12:47:44 +0100416 clock_gettime(CLOCK_MONOTONIC, &ts);
417 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100418
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100419 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300420 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100421 pr_err("failed to write stat round event\n");
422 }
423
Andi Kleenb90f1332017-08-31 12:40:36 -0700424 init_stats(&walltime_nsecs_stats);
425 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200426 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100427}
428
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100429static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700430{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100431 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300432 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100433
434 /*
435 * We need to enable counters only if:
436 * - we don't have tracee (attaching to task or cpu)
437 * - we have initial delay configured
438 */
439 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100440 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700441}
442
Mark Rutland3df33ef2016-08-09 14:04:29 +0100443static void disable_counters(void)
444{
445 /*
446 * If we don't have tracee (attaching to task or cpu), counters may
447 * still be running. To get accurate group ratios, we must stop groups
448 * from counting before reading their constituent counters.
449 */
450 if (!target__none(&target))
451 perf_evlist__disable(evsel_list);
452}
453
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300454static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300455
456/*
457 * perf_evlist__prepare_workload will send a SIGUSR1
458 * if the fork fails, since we asked by setting its
459 * want_signal to true.
460 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300461static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
462 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300463{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300464 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300465}
466
Jiri Olsa664c98d2015-11-05 15:40:50 +0100467static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100468{
469 int err;
470
Jiri Olsa664c98d2015-11-05 15:40:50 +0100471 if (is_pipe) {
472 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
473 process_synthesized_event);
474 if (err < 0) {
475 pr_err("Couldn't synthesize attrs.\n");
476 return err;
477 }
478 }
479
Andi Kleenbfd8f722017-11-17 13:42:58 -0800480 err = perf_event__synthesize_extra_attr(NULL,
481 evsel_list,
482 process_synthesized_event,
483 is_pipe);
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100484
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100485 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
486 process_synthesized_event,
487 NULL);
488 if (err < 0) {
489 pr_err("Couldn't synthesize thread map.\n");
490 return err;
491 }
492
493 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
494 process_synthesized_event, NULL);
495 if (err < 0) {
496 pr_err("Couldn't synthesize thread map.\n");
497 return err;
498 }
499
500 err = perf_event__synthesize_stat_config(NULL, &stat_config,
501 process_synthesized_event, NULL);
502 if (err < 0) {
503 pr_err("Couldn't synthesize config.\n");
504 return err;
505 }
506
507 return 0;
508}
509
Jiri Olsa2af46462015-11-05 15:40:49 +0100510#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
511
Andi Kleen42811d52017-10-05 19:00:28 -0700512static int __store_counter_ids(struct perf_evsel *counter)
Jiri Olsa2af46462015-11-05 15:40:49 +0100513{
514 int cpu, thread;
515
Andi Kleen42811d52017-10-05 19:00:28 -0700516 for (cpu = 0; cpu < xyarray__max_x(counter->fd); cpu++) {
517 for (thread = 0; thread < xyarray__max_y(counter->fd);
518 thread++) {
Jiri Olsa2af46462015-11-05 15:40:49 +0100519 int fd = FD(counter, cpu, thread);
520
521 if (perf_evlist__id_add_fd(evsel_list, counter,
522 cpu, thread, fd) < 0)
523 return -1;
524 }
525 }
526
527 return 0;
528}
529
530static int store_counter_ids(struct perf_evsel *counter)
531{
532 struct cpu_map *cpus = counter->cpus;
533 struct thread_map *threads = counter->threads;
534
535 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
536 return -ENOMEM;
537
Andi Kleen42811d52017-10-05 19:00:28 -0700538 return __store_counter_ids(counter);
Jiri Olsa2af46462015-11-05 15:40:49 +0100539}
540
Jiri Olsa82bf3112017-07-26 14:02:06 +0200541static bool perf_evsel__should_store_id(struct perf_evsel *counter)
542{
543 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
544}
545
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700546static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
547{
548 struct perf_evsel *c2, *leader;
549 bool is_open = true;
550
551 leader = evsel->leader;
552 pr_debug("Weak group for %s/%d failed\n",
553 leader->name, leader->nr_members);
554
555 /*
556 * for_each_group_member doesn't work here because it doesn't
557 * include the first entry.
558 */
559 evlist__for_each_entry(evsel_list, c2) {
560 if (c2 == evsel)
561 is_open = false;
562 if (c2->leader == leader) {
563 if (is_open)
564 perf_evsel__close(c2);
565 c2->leader = c2;
566 c2->nr_members = 0;
567 }
568 }
569 return leader;
570}
571
Jiri Olsae55c14a2018-04-23 11:08:21 +0200572static int __run_perf_stat(int argc, const char **argv, int run_idx)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200573{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200574 int interval = stat_config.interval;
yuzhoujiandb06a262018-01-29 10:25:22 +0100575 int times = stat_config.times;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100576 int timeout = stat_config.timeout;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300577 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200578 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100579 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100580 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100581 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200582 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300583 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100584 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600585 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200586
Stephane Eranian13370a92013-01-29 12:47:44 +0100587 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300588 ts.tv_sec = interval / USEC_PER_MSEC;
589 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100590 } else if (timeout) {
591 ts.tv_sec = timeout / USEC_PER_MSEC;
592 ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100593 } else {
594 ts.tv_sec = 1;
595 ts.tv_nsec = 0;
596 }
597
Liming Wang60666c62009-12-31 16:05:50 +0800598 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100599 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300600 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900601 perror("failed to prepare workload");
602 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800603 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900604 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000605 }
606
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200607 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300608 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200609
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300610 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300611try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100612 if (create_perf_stat_counter(counter) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700613
614 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700615 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700616 counter->leader != counter &&
617 counter->weak_group) {
618 counter = perf_evsel__reset_weak_group(counter);
619 goto try_again;
620 }
621
David Ahern979987a2012-05-08 09:29:16 -0600622 /*
623 * PPC returns ENXIO for HW counters until 2.6.37
624 * (behavior changed with commit b0a873e).
625 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100626 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600627 errno == ENOENT || errno == EOPNOTSUPP ||
628 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900629 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600630 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300631 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600632 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400633
634 if ((counter->leader != counter) ||
635 !(counter->leader->nr_members > 1))
636 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300637 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900638 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300639 ui__warning("%s\n", msg);
640 goto try_again;
Jin Yaoab6c79b2018-01-16 23:43:08 +0800641 } else if (target__has_per_thread(&target) &&
642 evsel_list->threads &&
643 evsel_list->threads->err_thread != -1) {
644 /*
645 * For global --per-thread case, skip current
646 * error thread.
647 */
648 if (!thread_map__remove(evsel_list->threads,
649 evsel_list->threads->err_thread)) {
650 evsel_list->threads->err_thread = -1;
651 goto try_again;
652 }
653 }
Ingo Molnarede70292011-04-28 08:48:42 +0200654
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300655 perf_evsel__open_strerror(counter, &target,
656 errno, msg, sizeof(msg));
657 ui__error("%s\n", msg);
658
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200659 if (child_pid != -1)
660 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600661
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200662 return -1;
663 }
David Ahern2cee77c2011-05-30 08:55:59 -0600664 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100665
666 l = strlen(counter->unit);
667 if (l > unit_width)
668 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100669
Jiri Olsa82bf3112017-07-26 14:02:06 +0200670 if (perf_evsel__should_store_id(counter) &&
671 store_counter_ids(counter))
Jiri Olsa2af46462015-11-05 15:40:49 +0100672 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300673 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200674
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300675 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300676 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300677 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300678 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100679 return -1;
680 }
681
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600682 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300683 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600684 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
685 str_error_r(errno, msg, sizeof(msg)));
686 return -1;
687 }
688
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100689 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100690 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100691
Jiri Olsa664c98d2015-11-05 15:40:50 +0100692 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100693 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100694 } else {
695 err = perf_session__write_header(perf_stat.session, evsel_list,
696 fd, false);
697 }
698
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100699 if (err < 0)
700 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100701
Jiri Olsa664c98d2015-11-05 15:40:50 +0100702 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100703 if (err < 0)
704 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100705 }
706
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200707 /*
708 * Enable counters and exec the command:
709 */
710 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100711 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200712
Liming Wang60666c62009-12-31 16:05:50 +0800713 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900714 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100715 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900716
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100717 if (interval || timeout) {
Stephane Eranian13370a92013-01-29 12:47:44 +0100718 while (!waitpid(child_pid, &status, WNOHANG)) {
719 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100720 if (timeout)
721 break;
Jiri Olsaba411a92015-06-26 11:29:24 +0200722 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100723 if (interval_count && !(--times))
724 break;
Stephane Eranian13370a92013-01-29 12:47:44 +0100725 }
726 }
Jiri Olsa0ce2da12018-06-05 14:13:13 +0200727 wait4(child_pid, &status, 0, &ru_data);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300728
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300729 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300730 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300731 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300732 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300733 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300734
Andi Kleen33e49ea2011-09-15 14:31:40 -0700735 if (WIFSIGNALED(status))
736 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800737 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100738 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100739 while (!done) {
740 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100741 if (timeout)
742 break;
yuzhoujiandb06a262018-01-29 10:25:22 +0100743 if (interval) {
Jiri Olsaba411a92015-06-26 11:29:24 +0200744 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100745 if (interval_count && !(--times))
746 break;
747 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100748 }
Liming Wang60666c62009-12-31 16:05:50 +0800749 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200750
Mark Rutland3df33ef2016-08-09 14:04:29 +0100751 disable_counters();
752
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200753 t1 = rdclock();
754
Jiri Olsae55c14a2018-04-23 11:08:21 +0200755 if (walltime_run_table)
756 walltime_run[run_idx] = t1 - t0;
757
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200758 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200759
Mark Rutland3df33ef2016-08-09 14:04:29 +0100760 /*
761 * Closing a group leader splits the group, and as we only disable
762 * group leaders, results in remaining events becoming enabled. To
763 * avoid arbitrary skew, we must read all counters before closing any
764 * group leaders.
765 */
766 read_counters();
767 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200768
Ingo Molnar42202dd2009-06-13 14:57:28 +0200769 return WEXITSTATUS(status);
770}
771
Jiri Olsae55c14a2018-04-23 11:08:21 +0200772static int run_perf_stat(int argc, const char **argv, int run_idx)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200773{
774 int ret;
775
776 if (pre_cmd) {
777 ret = system(pre_cmd);
778 if (ret)
779 return ret;
780 }
781
782 if (sync_run)
783 sync();
784
Jiri Olsae55c14a2018-04-23 11:08:21 +0200785 ret = __run_perf_stat(argc, argv, run_idx);
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200786 if (ret)
787 return ret;
788
789 if (post_cmd) {
790 ret = system(post_cmd);
791 if (ret)
792 return ret;
793 }
794
795 return ret;
796}
797
Andi Kleend73515c2015-03-11 07:16:27 -0700798static void print_running(u64 run, u64 ena)
799{
800 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200801 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700802 csv_sep,
803 run,
804 csv_sep,
805 ena ? 100.0 * run / ena : 100.0);
806 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200807 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700808 }
809}
810
Ingo Molnarf99844c2011-04-27 05:35:39 +0200811static void print_noise_pct(double total, double avg)
812{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800813 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200814
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700815 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200816 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600817 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200818 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200819}
820
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200821static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200822{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200823 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200824
Peter Zijlstra849abde2009-09-04 18:23:38 +0200825 if (run_count == 1)
826 return;
827
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300828 ps = evsel->stats;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200829 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200830}
831
Stephane Eranian12c08a92013-02-14 13:57:29 +0100832static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200833{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200834 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100835 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200836 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100837 cpu_map__id_to_socket(id),
838 csv_output ? 0 : -8,
839 cpu_map__id_to_cpu(id),
840 csv_sep,
841 csv_output ? 0 : 4,
842 nr,
843 csv_sep);
844 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100845 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200846 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100847 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100848 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100849 csv_sep,
850 csv_output ? 0 : 4,
851 nr,
852 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100853 break;
854 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200855 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200856 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100857 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100858 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200859 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200860 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200861 csv_output ? 0 : 16,
862 thread_map__comm(evsel->threads, id),
863 csv_output ? 0 : -8,
864 thread_map__pid(evsel->threads, id),
865 csv_sep);
866 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100867 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200868 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100869 default:
870 break;
871 }
872}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200873
Andi Kleen140aead2016-01-30 09:06:49 -0800874struct outstate {
875 FILE *fh;
876 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800877 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800878 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800879 int id, nr;
880 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800881};
882
883#define METRIC_LEN 35
884
885static void new_line_std(void *ctx)
886{
887 struct outstate *os = ctx;
888
889 os->newline = true;
890}
891
892static void do_new_line_std(struct outstate *os)
893{
894 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800895 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800896 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800897 if (stat_config.aggr_mode == AGGR_NONE)
898 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800899 fprintf(os->fh, " ");
900}
901
902static void print_metric_std(void *ctx, const char *color, const char *fmt,
903 const char *unit, double val)
904{
905 struct outstate *os = ctx;
906 FILE *out = os->fh;
907 int n;
908 bool newline = os->newline;
909
910 os->newline = false;
911
912 if (unit == NULL || fmt == NULL) {
913 fprintf(out, "%-*s", METRIC_LEN, "");
914 return;
915 }
916
917 if (newline)
918 do_new_line_std(os);
919
920 n = fprintf(out, " # ");
921 if (color)
922 n += color_fprintf(out, color, fmt, val);
923 else
924 n += fprintf(out, fmt, val);
925 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
926}
927
Andi Kleen92a61f62016-02-29 14:36:21 -0800928static void new_line_csv(void *ctx)
929{
930 struct outstate *os = ctx;
931 int i;
932
933 fputc('\n', os->fh);
934 if (os->prefix)
935 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800936 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800937 for (i = 0; i < os->nfields; i++)
938 fputs(csv_sep, os->fh);
939}
940
941static void print_metric_csv(void *ctx,
942 const char *color __maybe_unused,
943 const char *fmt, const char *unit, double val)
944{
945 struct outstate *os = ctx;
946 FILE *out = os->fh;
947 char buf[64], *vals, *ends;
948
949 if (unit == NULL || fmt == NULL) {
Ilya Pronin40c21892018-03-05 22:43:53 -0800950 fprintf(out, "%s%s", csv_sep, csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800951 return;
952 }
953 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900954 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800955 while (isdigit(*ends) || *ends == '.')
956 ends++;
957 *ends = 0;
958 while (isspace(*unit))
959 unit++;
960 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
961}
962
Andi Kleen54b50912016-03-03 15:57:36 -0800963/* Filter out some columns that don't work well in metrics only mode */
964
965static bool valid_only_metric(const char *unit)
966{
967 if (!unit)
968 return false;
969 if (strstr(unit, "/sec") ||
970 strstr(unit, "hz") ||
971 strstr(unit, "Hz") ||
972 strstr(unit, "CPUs utilized"))
973 return false;
974 return true;
975}
976
977static const char *fixunit(char *buf, struct perf_evsel *evsel,
978 const char *unit)
979{
980 if (!strncmp(unit, "of all", 6)) {
981 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
982 unit);
983 return buf;
984 }
985 return unit;
986}
987
988static void print_metric_only(void *ctx, const char *color, const char *fmt,
989 const char *unit, double val)
990{
991 struct outstate *os = ctx;
992 FILE *out = os->fh;
Jiri Olsaf5155722018-06-07 00:15:08 +0200993 char buf[1024], str[1024];
Jiri Olsac1a1f5d2018-06-07 00:15:09 +0200994 unsigned mlen = metric_only_len;
Andi Kleen54b50912016-03-03 15:57:36 -0800995
996 if (!valid_only_metric(unit))
997 return;
998 unit = fixunit(buf, os->evsel, unit);
Andi Kleen54b50912016-03-03 15:57:36 -0800999 if (mlen < strlen(unit))
1000 mlen = strlen(unit) + 1;
Jiri Olsaf5155722018-06-07 00:15:08 +02001001
1002 if (color)
1003 mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
1004
1005 color_snprintf(str, sizeof(str), color ?: "", fmt, val);
1006 fprintf(out, "%*s ", mlen, str);
Andi Kleen54b50912016-03-03 15:57:36 -08001007}
1008
1009static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
1010 const char *fmt,
1011 const char *unit, double val)
1012{
1013 struct outstate *os = ctx;
1014 FILE *out = os->fh;
1015 char buf[64], *vals, *ends;
1016 char tbuf[1024];
1017
1018 if (!valid_only_metric(unit))
1019 return;
1020 unit = fixunit(tbuf, os->evsel, unit);
1021 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +09001022 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -08001023 while (isdigit(*ends) || *ends == '.')
1024 ends++;
1025 *ends = 0;
1026 fprintf(out, "%s%s", vals, csv_sep);
1027}
1028
1029static void new_line_metric(void *ctx __maybe_unused)
1030{
1031}
1032
1033static void print_metric_header(void *ctx, const char *color __maybe_unused,
1034 const char *fmt __maybe_unused,
1035 const char *unit, double val __maybe_unused)
1036{
1037 struct outstate *os = ctx;
1038 char tbuf[1024];
1039
1040 if (!valid_only_metric(unit))
1041 return;
1042 unit = fixunit(tbuf, os->evsel, unit);
1043 if (csv_output)
1044 fprintf(os->fh, "%s%s", unit, csv_sep);
1045 else
Jiri Olsac1a1f5d2018-06-07 00:15:09 +02001046 fprintf(os->fh, "%*s ", metric_only_len, unit);
Andi Kleen54b50912016-03-03 15:57:36 -08001047}
1048
Andi Kleen44d49a62016-02-29 14:36:22 -08001049static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1050{
1051 int i;
1052
1053 if (!aggr_get_id)
1054 return 0;
1055
1056 if (stat_config.aggr_mode == AGGR_NONE)
1057 return id;
1058
1059 if (stat_config.aggr_mode == AGGR_GLOBAL)
1060 return 0;
1061
1062 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1063 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1064
1065 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1066 return cpu2;
1067 }
1068 return 0;
1069}
1070
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001071static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1072{
Jiri Olsa58215222015-07-21 14:31:24 +02001073 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001074 double sc = evsel->scale;
1075 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001076
1077 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001078 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001079 } else {
1080 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001081 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001082 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001083 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001084 }
1085
1086 aggr_printout(evsel, id, nr);
1087
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001088 fprintf(output, fmt, avg, csv_sep);
1089
1090 if (evsel->unit)
1091 fprintf(output, "%-*s%s",
1092 csv_output ? 0 : unit_width,
1093 evsel->unit, csv_sep);
1094
1095 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1096
1097 if (evsel->cgrp)
1098 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001099}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001100
Kan Liang30060ea2018-04-24 11:20:11 -07001101static bool is_mixed_hw_group(struct perf_evsel *counter)
1102{
1103 struct perf_evlist *evlist = counter->evlist;
1104 u32 pmu_type = counter->attr.type;
1105 struct perf_evsel *pos;
1106
1107 if (counter->nr_members < 2)
1108 return false;
1109
1110 evlist__for_each_entry(evlist, pos) {
1111 /* software events can be part of any hardware group */
1112 if (pos->attr.type == PERF_TYPE_SOFTWARE)
1113 continue;
1114 if (pmu_type == PERF_TYPE_SOFTWARE) {
1115 pmu_type = pos->attr.type;
1116 continue;
1117 }
1118 if (pmu_type != pos->attr.type)
1119 return true;
1120 }
1121
1122 return false;
1123}
1124
Andi Kleenf9483392016-01-30 09:06:50 -08001125static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +08001126 char *prefix, u64 run, u64 ena, double noise,
1127 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001128{
Andi Kleen140aead2016-01-30 09:06:49 -08001129 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001130 struct outstate os = {
1131 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001132 .prefix = prefix ? prefix : "",
1133 .id = id,
1134 .nr = nr,
1135 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001136 };
Andi Kleen140aead2016-01-30 09:06:49 -08001137 print_metric_t pm = print_metric_std;
1138 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001139
Andi Kleen54b50912016-03-03 15:57:36 -08001140 if (metric_only) {
1141 nl = new_line_metric;
1142 if (csv_output)
1143 pm = print_metric_only_csv;
1144 else
1145 pm = print_metric_only;
1146 } else
1147 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001148
Andi Kleen54b50912016-03-03 15:57:36 -08001149 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001150 static int aggr_fields[] = {
1151 [AGGR_GLOBAL] = 0,
1152 [AGGR_THREAD] = 1,
1153 [AGGR_NONE] = 1,
1154 [AGGR_SOCKET] = 2,
1155 [AGGR_CORE] = 2,
1156 };
1157
1158 pm = print_metric_csv;
1159 nl = new_line_csv;
1160 os.nfields = 3;
1161 os.nfields += aggr_fields[stat_config.aggr_mode];
1162 if (counter->cgrp)
1163 os.nfields++;
1164 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001165 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001166 if (metric_only) {
1167 pm(&os, NULL, "", "", 0);
1168 return;
1169 }
Andi Kleencb110f42016-01-30 09:06:51 -08001170 aggr_printout(counter, id, nr);
1171
1172 fprintf(stat_config.output, "%*s%s",
1173 csv_output ? 0 : 18,
1174 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1175 csv_sep);
1176
Kan Liang30060ea2018-04-24 11:20:11 -07001177 if (counter->supported) {
Borislav Petkov02d492e2017-02-07 01:40:05 +01001178 print_free_counters_hint = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001179 if (is_mixed_hw_group(counter))
1180 print_mixed_hw_group_error = 1;
1181 }
Borislav Petkov02d492e2017-02-07 01:40:05 +01001182
Andi Kleencb110f42016-01-30 09:06:51 -08001183 fprintf(stat_config.output, "%-*s%s",
1184 csv_output ? 0 : unit_width,
1185 counter->unit, csv_sep);
1186
1187 fprintf(stat_config.output, "%*s",
1188 csv_output ? 0 : -25,
1189 perf_evsel__name(counter));
1190
1191 if (counter->cgrp)
1192 fprintf(stat_config.output, "%s%s",
1193 csv_sep, counter->cgrp->name);
1194
Andi Kleen92a61f62016-02-29 14:36:21 -08001195 if (!csv_output)
1196 pm(&os, NULL, NULL, "", 0);
1197 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001198 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001199 if (csv_output)
1200 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001201 return;
1202 }
1203
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001204 if (!metric_only)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001205 abs_printout(id, nr, counter, uval);
1206
Andi Kleen140aead2016-01-30 09:06:49 -08001207 out.print_metric = pm;
1208 out.new_line = nl;
1209 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001210 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001211
Andi Kleen54b50912016-03-03 15:57:36 -08001212 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001213 print_noise(counter, noise);
1214 print_running(run, ena);
1215 }
1216
1217 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001218 first_shadow_cpu(counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001219 &out, &metric_events, st);
Andi Kleen54b50912016-03-03 15:57:36 -08001220 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001221 print_noise(counter, noise);
1222 print_running(run, ena);
1223 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001224}
1225
Andi Kleen44d49a62016-02-29 14:36:22 -08001226static void aggr_update_shadow(void)
1227{
1228 int cpu, s2, id, s;
1229 u64 val;
1230 struct perf_evsel *counter;
1231
1232 for (s = 0; s < aggr_map->nr; s++) {
1233 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001234 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001235 val = 0;
1236 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1237 s2 = aggr_get_id(evsel_list->cpus, cpu);
1238 if (s2 != id)
1239 continue;
1240 val += perf_counts(counter->counts, cpu, 0)->val;
1241 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001242 perf_stat__update_shadow_stats(counter, val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001243 first_shadow_cpu(counter, id),
1244 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001245 }
1246 }
1247}
1248
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001249static void uniquify_event_name(struct perf_evsel *counter)
1250{
1251 char *new_name;
1252 char *config;
1253
Kan Liang80ee8c52018-04-24 11:20:14 -07001254 if (counter->uniquified_name ||
1255 !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001256 strlen(counter->pmu_name)))
1257 return;
1258
1259 config = strchr(counter->name, '/');
1260 if (config) {
1261 if (asprintf(&new_name,
1262 "%s%s", counter->pmu_name, config) > 0) {
1263 free(counter->name);
1264 counter->name = new_name;
1265 }
1266 } else {
1267 if (asprintf(&new_name,
1268 "%s [%s]", counter->name, counter->pmu_name) > 0) {
1269 free(counter->name);
1270 counter->name = new_name;
1271 }
1272 }
Kan Liang80ee8c52018-04-24 11:20:14 -07001273
1274 counter->uniquified_name = true;
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001275}
1276
Andi Kleen430daf22017-03-20 13:17:00 -07001277static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001278 void (*cb)(struct perf_evsel *counter, void *data,
1279 bool first),
1280 void *data)
1281{
Andi Kleen430daf22017-03-20 13:17:00 -07001282 struct perf_evsel *alias;
1283
1284 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1285 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1286 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1287 alias->scale != counter->scale ||
1288 alias->cgrp != counter->cgrp ||
1289 strcmp(alias->unit, counter->unit) ||
Jiri Olsa0aa802a2018-07-20 13:00:34 +02001290 perf_evsel__is_clock(alias) != perf_evsel__is_clock(counter))
Andi Kleen430daf22017-03-20 13:17:00 -07001291 break;
1292 alias->merged_stat = true;
1293 cb(alias, data, false);
1294 }
1295}
1296
1297static bool collect_data(struct perf_evsel *counter,
1298 void (*cb)(struct perf_evsel *counter, void *data,
1299 bool first),
1300 void *data)
1301{
1302 if (counter->merged_stat)
1303 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001304 cb(counter, data, true);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001305 if (no_merge)
1306 uniquify_event_name(counter);
1307 else if (counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001308 collect_all_aliases(counter, cb, data);
1309 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001310}
1311
1312struct aggr_data {
1313 u64 ena, run, val;
1314 int id;
1315 int nr;
1316 int cpu;
1317};
1318
1319static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1320{
1321 struct aggr_data *ad = data;
1322 int cpu, s2;
1323
1324 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1325 struct perf_counts_values *counts;
1326
1327 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1328 if (s2 != ad->id)
1329 continue;
1330 if (first)
1331 ad->nr++;
1332 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001333 /*
1334 * When any result is bad, make them all to give
1335 * consistent output in interval mode.
1336 */
1337 if (counts->ena == 0 || counts->run == 0 ||
1338 counter->counts->scaled == -1) {
1339 ad->ena = 0;
1340 ad->run = 0;
1341 break;
1342 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001343 ad->val += counts->val;
1344 ad->ena += counts->ena;
1345 ad->run += counts->run;
1346 }
1347}
1348
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001349static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001350{
Jiri Olsa58215222015-07-21 14:31:24 +02001351 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001352 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001353 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001354 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001355 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001356 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001357
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001358 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001359 return;
1360
Andi Kleen44d49a62016-02-29 14:36:22 -08001361 aggr_update_shadow();
1362
Andi Kleen54b50912016-03-03 15:57:36 -08001363 /*
1364 * With metric_only everything is on a single line.
1365 * Without each counter has its own line.
1366 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001367 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001368 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001369 if (prefix && metric_only)
1370 fprintf(output, "%s", prefix);
1371
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001372 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001373 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001374 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001375 if (is_duration_time(counter))
1376 continue;
1377
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001378 ad.val = ad.ena = ad.run = 0;
1379 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001380 if (!collect_data(counter, aggr_cb, &ad))
1381 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001382 nr = ad.nr;
1383 ena = ad.ena;
1384 run = ad.run;
1385 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001386 if (first && metric_only) {
1387 first = false;
1388 aggr_printout(counter, id, nr);
1389 }
1390 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001391 fprintf(output, "%s", prefix);
1392
Stephane Eranian410136f2013-11-12 17:58:49 +01001393 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001394 printout(id, nr, counter, uval, prefix, run, ena, 1.0,
1395 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001396 if (!metric_only)
1397 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001398 }
Andi Kleen54b50912016-03-03 15:57:36 -08001399 if (metric_only)
1400 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001401 }
1402}
1403
Jin Yao29734552017-12-05 22:03:11 +08001404static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001405{
Jin Yao29734552017-12-05 22:03:11 +08001406 return ((struct perf_aggr_thread_value *)b)->val -
1407 ((struct perf_aggr_thread_value *)a)->val;
1408}
1409
1410static struct perf_aggr_thread_value *sort_aggr_thread(
1411 struct perf_evsel *counter,
1412 int nthreads, int ncpus,
1413 int *ret)
1414{
1415 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001416 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001417 struct perf_aggr_thread_value *buf;
1418
1419 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1420 if (!buf)
1421 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001422
1423 for (thread = 0; thread < nthreads; thread++) {
1424 u64 ena = 0, run = 0, val = 0;
1425
1426 for (cpu = 0; cpu < ncpus; cpu++) {
1427 val += perf_counts(counter->counts, cpu, thread)->val;
1428 ena += perf_counts(counter->counts, cpu, thread)->ena;
1429 run += perf_counts(counter->counts, cpu, thread)->run;
1430 }
1431
Jin Yao29734552017-12-05 22:03:11 +08001432 uval = val * counter->scale;
1433
1434 /*
1435 * Skip value 0 when enabling --per-thread globally,
1436 * otherwise too many 0 output.
1437 */
1438 if (uval == 0.0 && target__has_per_thread(&target))
1439 continue;
1440
1441 buf[i].counter = counter;
1442 buf[i].id = thread;
1443 buf[i].uval = uval;
1444 buf[i].val = val;
1445 buf[i].run = run;
1446 buf[i].ena = ena;
1447 i++;
1448 }
1449
1450 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1451
1452 if (ret)
1453 *ret = i;
1454
1455 return buf;
1456}
1457
1458static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1459{
1460 FILE *output = stat_config.output;
1461 int nthreads = thread_map__nr(counter->threads);
1462 int ncpus = cpu_map__nr(counter->cpus);
1463 int thread, sorted_threads, id;
1464 struct perf_aggr_thread_value *buf;
1465
1466 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads);
1467 if (!buf) {
1468 perror("cannot sort aggr thread");
1469 return;
1470 }
1471
1472 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001473 if (prefix)
1474 fprintf(output, "%s", prefix);
1475
Jin Yao29734552017-12-05 22:03:11 +08001476 id = buf[thread].id;
Jin Yao14e72a22017-12-05 22:03:08 +08001477 if (stat_config.stats)
Jin Yao29734552017-12-05 22:03:11 +08001478 printout(id, 0, buf[thread].counter, buf[thread].uval,
1479 prefix, buf[thread].run, buf[thread].ena, 1.0,
1480 &stat_config.stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001481 else
Jin Yao29734552017-12-05 22:03:11 +08001482 printout(id, 0, buf[thread].counter, buf[thread].uval,
1483 prefix, buf[thread].run, buf[thread].ena, 1.0,
1484 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001485 fputc('\n', output);
1486 }
Jin Yao29734552017-12-05 22:03:11 +08001487
1488 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001489}
1490
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001491struct caggr_data {
1492 double avg, avg_enabled, avg_running;
1493};
1494
1495static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1496 bool first __maybe_unused)
1497{
1498 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001499 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001500
1501 cd->avg += avg_stats(&ps->res_stats[0]);
1502 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1503 cd->avg_running += avg_stats(&ps->res_stats[2]);
1504}
1505
Ingo Molnar42202dd2009-06-13 14:57:28 +02001506/*
1507 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001508 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001509 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001510static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001511{
Jiri Olsa58215222015-07-21 14:31:24 +02001512 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001513 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001514 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001515
Andi Kleen430daf22017-03-20 13:17:00 -07001516 if (!collect_data(counter, counter_aggr_cb, &cd))
1517 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001518
Andi Kleen54b50912016-03-03 15:57:36 -08001519 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001520 fprintf(output, "%s", prefix);
1521
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001522 uval = cd.avg * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001523 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
1524 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001525 if (!metric_only)
1526 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001527}
1528
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001529static void counter_cb(struct perf_evsel *counter, void *data,
1530 bool first __maybe_unused)
1531{
1532 struct aggr_data *ad = data;
1533
1534 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1535 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1536 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1537}
1538
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001539/*
1540 * Print out the results of a single counter:
1541 * does not use aggregated count in system-wide
1542 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001543static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001544{
Jiri Olsa58215222015-07-21 14:31:24 +02001545 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001546 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001547 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001548 int cpu;
1549
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001550 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001551 struct aggr_data ad = { .cpu = cpu };
1552
Andi Kleen430daf22017-03-20 13:17:00 -07001553 if (!collect_data(counter, counter_cb, &ad))
1554 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001555 val = ad.val;
1556 ena = ad.ena;
1557 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001558
1559 if (prefix)
1560 fprintf(output, "%s", prefix);
1561
Stephane Eranian410136f2013-11-12 17:58:49 +01001562 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001563 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1564 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001565
Stephane Eranian4aa90152011-08-15 22:22:33 +02001566 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001567 }
1568}
1569
Andi Kleen206cab62016-03-03 15:57:37 -08001570static void print_no_aggr_metric(char *prefix)
1571{
1572 int cpu;
1573 int nrcpus = 0;
1574 struct perf_evsel *counter;
1575 u64 ena, run, val;
1576 double uval;
1577
1578 nrcpus = evsel_list->cpus->nr;
1579 for (cpu = 0; cpu < nrcpus; cpu++) {
1580 bool first = true;
1581
1582 if (prefix)
1583 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001584 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001585 if (is_duration_time(counter))
1586 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001587 if (first) {
1588 aggr_printout(counter, cpu, 0);
1589 first = false;
1590 }
1591 val = perf_counts(counter->counts, cpu, 0)->val;
1592 ena = perf_counts(counter->counts, cpu, 0)->ena;
1593 run = perf_counts(counter->counts, cpu, 0)->run;
1594
1595 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001596 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1597 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001598 }
1599 fputc('\n', stat_config.output);
1600 }
1601}
1602
Andi Kleen54b50912016-03-03 15:57:36 -08001603static int aggr_header_lens[] = {
1604 [AGGR_CORE] = 18,
1605 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001606 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001607 [AGGR_THREAD] = 24,
1608 [AGGR_GLOBAL] = 0,
1609};
1610
Andi Kleenc51fd632016-05-24 12:52:39 -07001611static const char *aggr_header_csv[] = {
1612 [AGGR_CORE] = "core,cpus,",
1613 [AGGR_SOCKET] = "socket,cpus",
1614 [AGGR_NONE] = "cpu,",
1615 [AGGR_THREAD] = "comm-pid,",
1616 [AGGR_GLOBAL] = ""
1617};
1618
Andi Kleen41c8ca22016-05-24 12:52:38 -07001619static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001620{
1621 struct perf_stat_output_ctx out;
1622 struct perf_evsel *counter;
1623 struct outstate os = {
1624 .fh = stat_config.output
1625 };
1626
1627 if (prefix)
1628 fprintf(stat_config.output, "%s", prefix);
1629
Andi Kleen41c8ca22016-05-24 12:52:38 -07001630 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001631 fprintf(stat_config.output, "%*s",
1632 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001633 if (csv_output) {
1634 if (stat_config.interval)
1635 fputs("time,", stat_config.output);
1636 fputs(aggr_header_csv[stat_config.aggr_mode],
1637 stat_config.output);
1638 }
Andi Kleen54b50912016-03-03 15:57:36 -08001639
1640 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001641 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001642 if (is_duration_time(counter))
1643 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001644 os.evsel = counter;
1645 out.ctx = &os;
1646 out.print_metric = print_metric_header;
1647 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001648 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001649 os.evsel = counter;
1650 perf_stat__print_shadow_stats(counter, 0,
1651 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001652 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001653 &metric_events,
1654 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001655 }
1656 fputc('\n', stat_config.output);
1657}
1658
Jiri Olsad4f63a42015-06-26 11:29:26 +02001659static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001660{
Jiri Olsa58215222015-07-21 14:31:24 +02001661 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001662 static int num_print_interval;
1663
Jiri Olsa9660e082018-06-07 00:15:06 +02001664 if (interval_clear)
1665 puts(CONSOLE_CLEAR);
1666
Jiri Olsad4f63a42015-06-26 11:29:26 +02001667 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1668
Jiri Olsa9660e082018-06-07 00:15:06 +02001669 if ((num_print_interval == 0 && !csv_output) || interval_clear) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001670 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001671 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001672 fprintf(output, "# time socket cpus");
1673 if (!metric_only)
1674 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001675 break;
1676 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001677 fprintf(output, "# time core cpus");
1678 if (!metric_only)
1679 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001680 break;
1681 case AGGR_NONE:
Jiri Olsaf5155722018-06-07 00:15:08 +02001682 fprintf(output, "# time CPU ");
Andi Kleen41c8ca22016-05-24 12:52:38 -07001683 if (!metric_only)
1684 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001685 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001686 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001687 fprintf(output, "# time comm-pid");
1688 if (!metric_only)
1689 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001690 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001691 case AGGR_GLOBAL:
1692 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001693 fprintf(output, "# time");
1694 if (!metric_only)
1695 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001696 case AGGR_UNSET:
1697 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001698 }
1699 }
1700
Jiri Olsac818cc02018-07-02 15:42:02 +02001701 if ((num_print_interval == 0 || interval_clear) && metric_only)
Andi Kleen41c8ca22016-05-24 12:52:38 -07001702 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001703 if (++num_print_interval == 25)
1704 num_print_interval = 0;
1705}
1706
1707static void print_header(int argc, const char **argv)
1708{
Jiri Olsa58215222015-07-21 14:31:24 +02001709 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001710 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001711
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001712 fflush(stdout);
1713
Stephane Eraniand7470b62010-12-01 18:49:05 +02001714 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001715 fprintf(output, "\n");
1716 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001717 if (target.system_wide)
1718 fprintf(output, "\'system wide");
1719 else if (target.cpu_list)
1720 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001721 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001722 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1723 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001724 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001725 } else if (target.pid)
1726 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001727 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001728 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001729
Stephane Eranian4aa90152011-08-15 22:22:33 +02001730 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001731 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001732 fprintf(output, " (%d runs)", run_count);
1733 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001734 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001735}
1736
Jiri Olsabc22de92018-04-23 11:08:20 +02001737static int get_precision(double num)
1738{
1739 if (num > 1)
1740 return 0;
1741
1742 return lround(ceil(-log10(num)));
1743}
1744
Jiri Olsae55c14a2018-04-23 11:08:21 +02001745static void print_table(FILE *output, int precision, double avg)
1746{
1747 char tmp[64];
1748 int idx, indent = 0;
1749
1750 scnprintf(tmp, 64, " %17.*f", precision, avg);
1751 while (tmp[indent] == ' ')
1752 indent++;
1753
1754 fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1755
1756 for (idx = 0; idx < run_count; idx++) {
1757 double run = (double) walltime_run[idx] / NSEC_PER_SEC;
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001758 int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
Jiri Olsae55c14a2018-04-23 11:08:21 +02001759
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001760 fprintf(output, " %17.*f (%+.*f) ",
Jiri Olsae55c14a2018-04-23 11:08:21 +02001761 precision, run, precision, run - avg);
Jiri Olsaabc60ba2018-04-23 11:08:22 +02001762
1763 for (h = 0; h < n; h++)
1764 fprintf(output, "#");
1765
1766 fprintf(output, "\n");
Jiri Olsae55c14a2018-04-23 11:08:21 +02001767 }
1768
1769 fprintf(output, "\n%*s# Final result:\n", indent, "");
1770}
1771
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001772static double timeval2double(struct timeval *t)
1773{
1774 return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1775}
1776
Jiri Olsad4f63a42015-06-26 11:29:26 +02001777static void print_footer(void)
1778{
Jiri Olsabc22de92018-04-23 11:08:20 +02001779 double avg = avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
Jiri Olsa58215222015-07-21 14:31:24 +02001780 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001781 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001782
Jiri Olsad4f63a42015-06-26 11:29:26 +02001783 if (!null_run)
1784 fprintf(output, "\n");
Jiri Olsabc22de92018-04-23 11:08:20 +02001785
1786 if (run_count == 1) {
1787 fprintf(output, " %17.9f seconds time elapsed", avg);
Jiri Olsa0ce2da12018-06-05 14:13:13 +02001788
1789 if (ru_display) {
1790 double ru_utime = timeval2double(&ru_data.ru_utime);
1791 double ru_stime = timeval2double(&ru_data.ru_stime);
1792
1793 fprintf(output, "\n\n");
1794 fprintf(output, " %17.9f seconds user\n", ru_utime);
1795 fprintf(output, " %17.9f seconds sys\n", ru_stime);
1796 }
Jiri Olsabc22de92018-04-23 11:08:20 +02001797 } else {
1798 double sd = stddev_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
1799 /*
1800 * Display at most 2 more significant
1801 * digits than the stddev inaccuracy.
1802 */
1803 int precision = get_precision(sd) + 2;
1804
Jiri Olsae55c14a2018-04-23 11:08:21 +02001805 if (walltime_run_table)
1806 print_table(output, precision, avg);
1807
Jiri Olsabc22de92018-04-23 11:08:20 +02001808 fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1809 precision, avg, precision, sd);
1810
1811 print_noise_pct(sd, avg);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001812 }
1813 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001814
Andi Kleen918c7b062017-05-22 18:00:16 -07001815 if (print_free_counters_hint &&
1816 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1817 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001818 fprintf(output,
1819"Some events weren't counted. Try disabling the NMI watchdog:\n"
1820" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1821" perf stat ...\n"
1822" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001823
1824 if (print_mixed_hw_group_error)
1825 fprintf(output,
1826 "The events in group usually have to be from "
1827 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001828}
1829
1830static void print_counters(struct timespec *ts, int argc, const char **argv)
1831{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001832 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001833 struct perf_evsel *counter;
1834 char buf[64], *prefix = NULL;
1835
Jiri Olsa664c98d2015-11-05 15:40:50 +01001836 /* Do not print anything if we record to the pipe. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001837 if (STAT_RECORD && perf_stat.data.is_pipe)
Jiri Olsa664c98d2015-11-05 15:40:50 +01001838 return;
1839
Jiri Olsad4f63a42015-06-26 11:29:26 +02001840 if (interval)
1841 print_interval(prefix = buf, ts);
1842 else
1843 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001844
Andi Kleen54b50912016-03-03 15:57:36 -08001845 if (metric_only) {
1846 static int num_print_iv;
1847
Andi Kleen41c8ca22016-05-24 12:52:38 -07001848 if (num_print_iv == 0 && !interval)
1849 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001850 if (num_print_iv++ == 25)
1851 num_print_iv = 0;
1852 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1853 fprintf(stat_config.output, "%s", prefix);
1854 }
1855
Jiri Olsa421a50f2015-07-21 14:31:22 +02001856 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001857 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001858 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001859 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001860 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001861 case AGGR_THREAD:
Andi Kleene864c5c2017-08-31 12:40:35 -07001862 evlist__for_each_entry(evsel_list, counter) {
1863 if (is_duration_time(counter))
1864 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001865 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001866 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001867 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001868 case AGGR_GLOBAL:
Andi Kleene864c5c2017-08-31 12:40:35 -07001869 evlist__for_each_entry(evsel_list, counter) {
1870 if (is_duration_time(counter))
1871 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001872 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001873 }
Andi Kleen54b50912016-03-03 15:57:36 -08001874 if (metric_only)
1875 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001876 break;
1877 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001878 if (metric_only)
1879 print_no_aggr_metric(prefix);
1880 else {
Andi Kleene864c5c2017-08-31 12:40:35 -07001881 evlist__for_each_entry(evsel_list, counter) {
1882 if (is_duration_time(counter))
1883 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001884 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001885 }
Andi Kleen206cab62016-03-03 15:57:37 -08001886 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001887 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001888 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001889 default:
1890 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001891 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001892
Jiri Olsad4f63a42015-06-26 11:29:26 +02001893 if (!interval && !csv_output)
1894 print_footer();
1895
Jiri Olsa58215222015-07-21 14:31:24 +02001896 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001897}
1898
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001899static volatile int signr = -1;
1900
Ingo Molnar52425192009-05-26 09:17:18 +02001901static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001902{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001903 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001904 done = 1;
1905
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001906 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001907 /*
1908 * render child_pid harmless
1909 * won't send SIGTERM to a random
1910 * process in case of race condition
1911 * and fast PID recycling
1912 */
1913 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001914}
1915
1916static void sig_atexit(void)
1917{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001918 sigset_t set, oset;
1919
1920 /*
1921 * avoid race condition with SIGCHLD handler
1922 * in skip_signal() which is modifying child_pid
1923 * goal is to avoid send SIGTERM to a random
1924 * process
1925 */
1926 sigemptyset(&set);
1927 sigaddset(&set, SIGCHLD);
1928 sigprocmask(SIG_BLOCK, &set, &oset);
1929
Chris Wilson933da832009-10-04 01:35:01 +01001930 if (child_pid != -1)
1931 kill(child_pid, SIGTERM);
1932
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001933 sigprocmask(SIG_SETMASK, &oset, NULL);
1934
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001935 if (signr == -1)
1936 return;
1937
1938 signal(signr, SIG_DFL);
1939 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001940}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001941
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001942static int stat__set_big_num(const struct option *opt __maybe_unused,
1943 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001944{
1945 big_num_opt = unset ? 0 : 1;
1946 return 0;
1947}
1948
Andi Kleen44b1e602016-05-30 12:49:42 -03001949static int enable_metric_only(const struct option *opt __maybe_unused,
1950 const char *s __maybe_unused, int unset)
1951{
1952 force_metric_only = true;
1953 metric_only = !unset;
1954 return 0;
1955}
1956
Andi Kleenb18f3e32017-08-31 12:40:31 -07001957static int parse_metric_groups(const struct option *opt,
1958 const char *str,
1959 int unset __maybe_unused)
1960{
1961 return metricgroup__parse_groups(opt, str, &metric_events);
1962}
1963
Jiri Olsae0547312015-11-05 15:40:45 +01001964static const struct option stat_options[] = {
1965 OPT_BOOLEAN('T', "transaction", &transaction_run,
1966 "hardware transaction statistics"),
1967 OPT_CALLBACK('e', "event", &evsel_list, "event",
1968 "event selector. use 'perf list' to list available events",
1969 parse_events_option),
1970 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1971 "event filter", parse_filter),
1972 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1973 "child tasks do not inherit counters"),
1974 OPT_STRING('p', "pid", &target.pid, "pid",
1975 "stat events on existing process id"),
1976 OPT_STRING('t', "tid", &target.tid, "tid",
1977 "stat events on existing thread id"),
1978 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1979 "system-wide collection from all CPUs"),
1980 OPT_BOOLEAN('g', "group", &group,
1981 "put the counters into a counter group"),
1982 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1983 OPT_INCR('v', "verbose", &verbose,
1984 "be more verbose (show counter open errors, etc)"),
1985 OPT_INTEGER('r', "repeat", &run_count,
1986 "repeat command and print average + stddev (max: 100, forever: 0)"),
Jiri Olsae55c14a2018-04-23 11:08:21 +02001987 OPT_BOOLEAN(0, "table", &walltime_run_table,
1988 "display details about each run (only with -r option)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001989 OPT_BOOLEAN('n', "null", &null_run,
1990 "null run - dont start any counters"),
1991 OPT_INCR('d', "detailed", &detailed_run,
1992 "detailed run - start a lot of events"),
1993 OPT_BOOLEAN('S', "sync", &sync_run,
1994 "call sync() before starting a run"),
1995 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1996 "print large numbers with thousands\' separators",
1997 stat__set_big_num),
1998 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1999 "list of cpus to monitor in system-wide"),
2000 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
2001 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07002002 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01002003 OPT_STRING('x', "field-separator", &csv_sep, "separator",
2004 "print counts with custom separator"),
2005 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
2006 "monitor event in cgroup name only", parse_cgroups),
2007 OPT_STRING('o', "output", &output_name, "file", "output file name"),
2008 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
2009 OPT_INTEGER(0, "log-fd", &output_fd,
2010 "log output to fd, instead of stderr"),
2011 OPT_STRING(0, "pre", &pre_cmd, "command",
2012 "command to run prior to the measured command"),
2013 OPT_STRING(0, "post", &post_cmd, "command",
2014 "command to run after to the measured command"),
2015 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03002016 "print counts at regular interval in ms "
2017 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01002018 OPT_INTEGER(0, "interval-count", &stat_config.times,
2019 "print counts for fixed number of times"),
Jiri Olsa9660e082018-06-07 00:15:06 +02002020 OPT_BOOLEAN(0, "interval-clear", &interval_clear,
2021 "clear screen in between new interval"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002022 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
2023 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01002024 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
2025 "aggregate counts per processor socket", AGGR_SOCKET),
2026 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
2027 "aggregate counts per physical processor core", AGGR_CORE),
2028 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
2029 "aggregate counts per thread", AGGR_THREAD),
2030 OPT_UINTEGER('D', "delay", &initial_delay,
2031 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03002032 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
2033 "Only print computed metrics. No raw values", enable_metric_only),
2034 OPT_BOOLEAN(0, "topdown", &topdown_run,
2035 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07002036 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
2037 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07002038 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
2039 "monitor specified metrics or metric groups (separated by ,)",
2040 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01002041 OPT_END()
2042};
2043
Jiri Olsa1fe7a302015-10-16 12:41:15 +02002044static int perf_stat__get_socket(struct cpu_map *map, int cpu)
2045{
2046 return cpu_map__get_socket(map, cpu, NULL);
2047}
2048
2049static int perf_stat__get_core(struct cpu_map *map, int cpu)
2050{
2051 return cpu_map__get_core(map, cpu, NULL);
2052}
2053
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002054static int cpu_map__get_max(struct cpu_map *map)
2055{
2056 int i, max = -1;
2057
2058 for (i = 0; i < map->nr; i++) {
2059 if (map->map[i] > max)
2060 max = map->map[i];
2061 }
2062
2063 return max;
2064}
2065
2066static struct cpu_map *cpus_aggr_map;
2067
2068static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
2069{
2070 int cpu;
2071
2072 if (idx >= map->nr)
2073 return -1;
2074
2075 cpu = map->map[idx];
2076
2077 if (cpus_aggr_map->map[cpu] == -1)
2078 cpus_aggr_map->map[cpu] = get_id(map, idx);
2079
2080 return cpus_aggr_map->map[cpu];
2081}
2082
2083static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
2084{
2085 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
2086}
2087
2088static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
2089{
2090 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
2091}
2092
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002093static int perf_stat_init_aggr_mode(void)
2094{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002095 int nr;
2096
Jiri Olsa421a50f2015-07-21 14:31:22 +02002097 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002098 case AGGR_SOCKET:
2099 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
2100 perror("cannot build socket map");
2101 return -1;
2102 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002103 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002104 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002105 case AGGR_CORE:
2106 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
2107 perror("cannot build core map");
2108 return -1;
2109 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002110 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002111 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002112 case AGGR_NONE:
2113 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002114 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002115 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002116 default:
2117 break;
2118 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002119
2120 /*
2121 * The evsel_list->cpus is the base we operate on,
2122 * taking the highest cpu number to be the size of
2123 * the aggregation translate cpumap.
2124 */
2125 nr = cpu_map__get_max(evsel_list->cpus);
2126 cpus_aggr_map = cpu_map__empty_new(nr + 1);
2127 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002128}
2129
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002130static void perf_stat__exit_aggr_mode(void)
2131{
2132 cpu_map__put(aggr_map);
2133 cpu_map__put(cpus_aggr_map);
2134 aggr_map = NULL;
2135 cpus_aggr_map = NULL;
2136}
2137
Jiri Olsa68d702f2015-11-05 15:40:58 +01002138static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2139{
2140 int cpu;
2141
2142 if (idx > map->nr)
2143 return -1;
2144
2145 cpu = map->map[idx];
2146
Jan Stancekda8a58b2017-02-17 12:10:26 +01002147 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002148 return -1;
2149
2150 return cpu;
2151}
2152
2153static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2154{
2155 struct perf_env *env = data;
2156 int cpu = perf_env__get_cpu(env, map, idx);
2157
2158 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2159}
2160
2161static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2162{
2163 struct perf_env *env = data;
2164 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2165
2166 if (cpu != -1) {
2167 int socket_id = env->cpu[cpu].socket_id;
2168
2169 /*
2170 * Encode socket in upper 16 bits
2171 * core_id is relative to socket, and
2172 * we need a global id. So we combine
2173 * socket + core id.
2174 */
2175 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2176 }
2177
2178 return core;
2179}
2180
2181static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2182 struct cpu_map **sockp)
2183{
2184 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2185}
2186
2187static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2188 struct cpu_map **corep)
2189{
2190 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2191}
2192
2193static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2194{
2195 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2196}
2197
2198static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2199{
2200 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2201}
2202
2203static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2204{
2205 struct perf_env *env = &st->session->header.env;
2206
2207 switch (stat_config.aggr_mode) {
2208 case AGGR_SOCKET:
2209 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2210 perror("cannot build socket map");
2211 return -1;
2212 }
2213 aggr_get_id = perf_stat__get_socket_file;
2214 break;
2215 case AGGR_CORE:
2216 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2217 perror("cannot build core map");
2218 return -1;
2219 }
2220 aggr_get_id = perf_stat__get_core_file;
2221 break;
2222 case AGGR_NONE:
2223 case AGGR_GLOBAL:
2224 case AGGR_THREAD:
2225 case AGGR_UNSET:
2226 default:
2227 break;
2228 }
2229
2230 return 0;
2231}
2232
Andi Kleen44b1e602016-05-30 12:49:42 -03002233static int topdown_filter_events(const char **attr, char **str, bool use_group)
2234{
2235 int off = 0;
2236 int i;
2237 int len = 0;
2238 char *s;
2239
2240 for (i = 0; attr[i]; i++) {
2241 if (pmu_have_event("cpu", attr[i])) {
2242 len += strlen(attr[i]) + 1;
2243 attr[i - off] = attr[i];
2244 } else
2245 off++;
2246 }
2247 attr[i - off] = NULL;
2248
2249 *str = malloc(len + 1 + 2);
2250 if (!*str)
2251 return -1;
2252 s = *str;
2253 if (i - off == 0) {
2254 *s = 0;
2255 return 0;
2256 }
2257 if (use_group)
2258 *s++ = '{';
2259 for (i = 0; attr[i]; i++) {
2260 strcpy(s, attr[i]);
2261 s += strlen(s);
2262 *s++ = ',';
2263 }
2264 if (use_group) {
2265 s[-1] = '}';
2266 *s = 0;
2267 } else
2268 s[-1] = 0;
2269 return 0;
2270}
2271
2272__weak bool arch_topdown_check_group(bool *warn)
2273{
2274 *warn = false;
2275 return false;
2276}
2277
2278__weak void arch_topdown_group_warn(void)
2279{
2280}
2281
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002282/*
2283 * Add default attributes, if there were no attributes specified or
2284 * if -d/--detailed, -d -d or -d -d -d is used:
2285 */
2286static int add_default_attributes(void)
2287{
Andi Kleen44b1e602016-05-30 12:49:42 -03002288 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002289 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002290
2291 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2292 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2293 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2294 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2295
2296 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002297};
2298 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002299 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002300};
2301 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002302 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002303};
2304 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002305 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2306 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2307 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2308
2309};
2310
2311/*
2312 * Detailed stats (-d), covering the L1 and last level data caches:
2313 */
2314 struct perf_event_attr detailed_attrs[] = {
2315
2316 { .type = PERF_TYPE_HW_CACHE,
2317 .config =
2318 PERF_COUNT_HW_CACHE_L1D << 0 |
2319 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2320 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2321
2322 { .type = PERF_TYPE_HW_CACHE,
2323 .config =
2324 PERF_COUNT_HW_CACHE_L1D << 0 |
2325 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2326 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2327
2328 { .type = PERF_TYPE_HW_CACHE,
2329 .config =
2330 PERF_COUNT_HW_CACHE_LL << 0 |
2331 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2332 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2333
2334 { .type = PERF_TYPE_HW_CACHE,
2335 .config =
2336 PERF_COUNT_HW_CACHE_LL << 0 |
2337 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2338 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2339};
2340
2341/*
2342 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2343 */
2344 struct perf_event_attr very_detailed_attrs[] = {
2345
2346 { .type = PERF_TYPE_HW_CACHE,
2347 .config =
2348 PERF_COUNT_HW_CACHE_L1I << 0 |
2349 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2350 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2351
2352 { .type = PERF_TYPE_HW_CACHE,
2353 .config =
2354 PERF_COUNT_HW_CACHE_L1I << 0 |
2355 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2356 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2357
2358 { .type = PERF_TYPE_HW_CACHE,
2359 .config =
2360 PERF_COUNT_HW_CACHE_DTLB << 0 |
2361 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2362 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2363
2364 { .type = PERF_TYPE_HW_CACHE,
2365 .config =
2366 PERF_COUNT_HW_CACHE_DTLB << 0 |
2367 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2368 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2369
2370 { .type = PERF_TYPE_HW_CACHE,
2371 .config =
2372 PERF_COUNT_HW_CACHE_ITLB << 0 |
2373 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2374 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2375
2376 { .type = PERF_TYPE_HW_CACHE,
2377 .config =
2378 PERF_COUNT_HW_CACHE_ITLB << 0 |
2379 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2380 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2381
2382};
2383
2384/*
2385 * Very, very detailed stats (-d -d -d), adding prefetch events:
2386 */
2387 struct perf_event_attr very_very_detailed_attrs[] = {
2388
2389 { .type = PERF_TYPE_HW_CACHE,
2390 .config =
2391 PERF_COUNT_HW_CACHE_L1D << 0 |
2392 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2393 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2394
2395 { .type = PERF_TYPE_HW_CACHE,
2396 .config =
2397 PERF_COUNT_HW_CACHE_L1D << 0 |
2398 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2399 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2400};
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002401 struct parse_events_error errinfo;
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002402
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002403 /* Set attrs if no event is selected and !null_run: */
2404 if (null_run)
2405 return 0;
2406
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002407 if (transaction_run) {
Thomas Richter742d92f2018-06-26 09:17:01 +02002408 /* Handle -T as -M transaction. Once platform specific metrics
2409 * support has been added to the json files, all archictures
2410 * will use this approach. To determine transaction support
2411 * on an architecture test for such a metric name.
2412 */
2413 if (metricgroup__has_metric("transaction")) {
2414 struct option opt = { .value = &evsel_list };
2415
2416 return metricgroup__parse_groups(&opt, "transaction",
2417 &metric_events);
2418 }
2419
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002420 if (pmu_have_event("cpu", "cycles-ct") &&
2421 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002422 err = parse_events(evsel_list, transaction_attrs,
2423 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002424 else
Thomas Richterfca323402018-03-08 15:57:35 +01002425 err = parse_events(evsel_list,
2426 transaction_limited_attrs,
2427 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002428 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002429 fprintf(stderr, "Cannot set up transaction events\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002430 parse_events_print_error(&errinfo, transaction_attrs);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002431 return -1;
2432 }
2433 return 0;
2434 }
2435
Kan Liangdaefd0b2017-05-26 12:05:38 -07002436 if (smi_cost) {
2437 int smi;
2438
2439 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2440 fprintf(stderr, "freeze_on_smi is not supported.\n");
2441 return -1;
2442 }
2443
2444 if (!smi) {
2445 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2446 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2447 return -1;
2448 }
2449 smi_reset = true;
2450 }
2451
2452 if (pmu_have_event("msr", "aperf") &&
2453 pmu_have_event("msr", "smi")) {
2454 if (!force_metric_only)
2455 metric_only = true;
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002456 err = parse_events(evsel_list, smi_cost_attrs, &errinfo);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002457 } else {
2458 fprintf(stderr, "To measure SMI cost, it needs "
2459 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002460 parse_events_print_error(&errinfo, smi_cost_attrs);
Kan Liangdaefd0b2017-05-26 12:05:38 -07002461 return -1;
2462 }
2463 if (err) {
2464 fprintf(stderr, "Cannot set up SMI cost events\n");
2465 return -1;
2466 }
2467 return 0;
2468 }
2469
Andi Kleen44b1e602016-05-30 12:49:42 -03002470 if (topdown_run) {
2471 char *str = NULL;
2472 bool warn = false;
2473
2474 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2475 stat_config.aggr_mode != AGGR_CORE) {
2476 pr_err("top down event configuration requires --per-core mode\n");
2477 return -1;
2478 }
2479 stat_config.aggr_mode = AGGR_CORE;
2480 if (nr_cgroups || !target__has_cpu(&target)) {
2481 pr_err("top down event configuration requires system-wide mode (-a)\n");
2482 return -1;
2483 }
2484
2485 if (!force_metric_only)
2486 metric_only = true;
2487 if (topdown_filter_events(topdown_attrs, &str,
2488 arch_topdown_check_group(&warn)) < 0) {
2489 pr_err("Out of memory\n");
2490 return -1;
2491 }
2492 if (topdown_attrs[0] && str) {
2493 if (warn)
2494 arch_topdown_group_warn();
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002495 err = parse_events(evsel_list, str, &errinfo);
Andi Kleen44b1e602016-05-30 12:49:42 -03002496 if (err) {
2497 fprintf(stderr,
2498 "Cannot set up top down events %s: %d\n",
2499 str, err);
2500 free(str);
Jiri Olsaa5cfa622018-06-07 00:15:10 +02002501 parse_events_print_error(&errinfo, str);
Andi Kleen44b1e602016-05-30 12:49:42 -03002502 return -1;
2503 }
2504 } else {
2505 fprintf(stderr, "System does not support topdown\n");
2506 return -1;
2507 }
2508 free(str);
2509 }
2510
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002511 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002512 if (target__has_cpu(&target))
2513 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2514
Andi Kleen9dec4472016-02-26 16:27:56 -08002515 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2516 return -1;
2517 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2518 if (perf_evlist__add_default_attrs(evsel_list,
2519 frontend_attrs) < 0)
2520 return -1;
2521 }
2522 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2523 if (perf_evlist__add_default_attrs(evsel_list,
2524 backend_attrs) < 0)
2525 return -1;
2526 }
2527 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002528 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002529 }
2530
2531 /* Detailed events get appended to the event list: */
2532
2533 if (detailed_run < 1)
2534 return 0;
2535
2536 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002537 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002538 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002539
2540 if (detailed_run < 2)
2541 return 0;
2542
2543 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002544 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002545 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002546
2547 if (detailed_run < 3)
2548 return 0;
2549
2550 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002551 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002552}
2553
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002554static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002555 "perf stat record [<options>]",
2556 NULL,
2557};
2558
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002559static void init_features(struct perf_session *session)
2560{
2561 int feat;
2562
2563 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2564 perf_header__set_feat(&session->header, feat);
2565
2566 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2567 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2568 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2569 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2570}
2571
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002572static int __cmd_record(int argc, const char **argv)
2573{
2574 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002575 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002576
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002577 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002578 PARSE_OPT_STOP_AT_NON_OPTION);
2579
2580 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002581 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002582
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002583 if (run_count != 1 || forever) {
2584 pr_err("Cannot use -r option with perf stat record.\n");
2585 return -1;
2586 }
2587
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002588 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002589 if (session == NULL) {
2590 pr_err("Perf session creation failed.\n");
2591 return -1;
2592 }
2593
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002594 init_features(session);
2595
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002596 session->evlist = evsel_list;
2597 perf_stat.session = session;
2598 perf_stat.record = true;
2599 return argc;
2600}
2601
Jiri Olsaa56f9392015-11-05 15:40:59 +01002602static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2603 union perf_event *event,
2604 struct perf_session *session)
2605{
Andi Kleene3b03b62016-05-05 16:04:03 -07002606 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002607 struct perf_evsel *counter;
2608 struct timespec tsh, *ts = NULL;
2609 const char **argv = session->header.env.cmdline_argv;
2610 int argc = session->header.env.nr_cmdline;
2611
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002612 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002613 perf_stat_process_counter(&stat_config, counter);
2614
Andi Kleene3b03b62016-05-05 16:04:03 -07002615 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2616 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002617
Andi Kleene3b03b62016-05-05 16:04:03 -07002618 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002619 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2620 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002621 ts = &tsh;
2622 }
2623
2624 print_counters(ts, argc, argv);
2625 return 0;
2626}
2627
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002628static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002629int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002630 union perf_event *event,
2631 struct perf_session *session __maybe_unused)
2632{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002633 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2634
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002635 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002636
Jiri Olsa89af4e02015-11-05 15:41:02 +01002637 if (cpu_map__empty(st->cpus)) {
2638 if (st->aggr_mode != AGGR_UNSET)
2639 pr_warning("warning: processing task data, aggregation mode not set\n");
2640 return 0;
2641 }
2642
2643 if (st->aggr_mode != AGGR_UNSET)
2644 stat_config.aggr_mode = st->aggr_mode;
2645
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002646 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002647 perf_stat_init_aggr_mode();
2648 else
2649 perf_stat_init_aggr_mode_file(st);
2650
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002651 return 0;
2652}
2653
Jiri Olsa1975d362015-11-05 15:40:56 +01002654static int set_maps(struct perf_stat *st)
2655{
2656 if (!st->cpus || !st->threads)
2657 return 0;
2658
2659 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2660 return -EINVAL;
2661
2662 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2663
2664 if (perf_evlist__alloc_stats(evsel_list, true))
2665 return -ENOMEM;
2666
2667 st->maps_allocated = true;
2668 return 0;
2669}
2670
2671static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002672int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002673 union perf_event *event,
2674 struct perf_session *session __maybe_unused)
2675{
2676 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2677
2678 if (st->threads) {
2679 pr_warning("Extra thread map event, ignoring.\n");
2680 return 0;
2681 }
2682
2683 st->threads = thread_map__new_event(&event->thread_map);
2684 if (!st->threads)
2685 return -ENOMEM;
2686
2687 return set_maps(st);
2688}
2689
2690static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002691int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002692 union perf_event *event,
2693 struct perf_session *session __maybe_unused)
2694{
2695 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2696 struct cpu_map *cpus;
2697
2698 if (st->cpus) {
2699 pr_warning("Extra cpu map event, ignoring.\n");
2700 return 0;
2701 }
2702
2703 cpus = cpu_map__new_data(&event->cpu_map.data);
2704 if (!cpus)
2705 return -ENOMEM;
2706
2707 st->cpus = cpus;
2708 return set_maps(st);
2709}
2710
Jin Yao56739442017-12-05 22:03:07 +08002711static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2712{
2713 int i;
2714
2715 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2716 if (!config->stats)
2717 return -1;
2718
2719 config->stats_num = nthreads;
2720
2721 for (i = 0; i < nthreads; i++)
2722 runtime_stat__init(&config->stats[i]);
2723
2724 return 0;
2725}
2726
2727static void runtime_stat_delete(struct perf_stat_config *config)
2728{
2729 int i;
2730
2731 if (!config->stats)
2732 return;
2733
2734 for (i = 0; i < config->stats_num; i++)
2735 runtime_stat__exit(&config->stats[i]);
2736
2737 free(config->stats);
2738}
2739
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002740static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002741 "perf stat report [<options>]",
2742 NULL,
2743};
2744
2745static struct perf_stat perf_stat = {
2746 .tool = {
2747 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002748 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002749 .thread_map = process_thread_map_event,
2750 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002751 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002752 .stat = perf_event__process_stat_event,
2753 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002754 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002755 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002756};
2757
2758static int __cmd_report(int argc, const char **argv)
2759{
2760 struct perf_session *session;
2761 const struct option options[] = {
2762 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002763 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2764 "aggregate counts per processor socket", AGGR_SOCKET),
2765 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2766 "aggregate counts per physical processor core", AGGR_CORE),
2767 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2768 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002769 OPT_END()
2770 };
2771 struct stat st;
2772 int ret;
2773
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002774 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002775
2776 if (!input_name || !strlen(input_name)) {
2777 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2778 input_name = "-";
2779 else
2780 input_name = "perf.data";
2781 }
2782
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002783 perf_stat.data.file.path = input_name;
2784 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002785
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002786 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002787 if (session == NULL)
2788 return -1;
2789
2790 perf_stat.session = session;
2791 stat_config.output = stderr;
2792 evsel_list = session->evlist;
2793
2794 ret = perf_session__process_events(session);
2795 if (ret)
2796 return ret;
2797
2798 perf_session__delete(session);
2799 return 0;
2800}
2801
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002802static void setup_system_wide(int forks)
2803{
2804 /*
2805 * Make system wide (-a) the default target if
2806 * no target was specified and one of following
2807 * conditions is met:
2808 *
2809 * - there's no workload specified
2810 * - there is workload specified but all requested
2811 * events are system wide events
2812 */
2813 if (!target__none(&target))
2814 return;
2815
2816 if (!forks)
2817 target.system_wide = true;
2818 else {
2819 struct perf_evsel *counter;
2820
2821 evlist__for_each_entry(evsel_list, counter) {
2822 if (!counter->system_wide)
2823 return;
2824 }
2825
2826 if (evsel_list->nr_entries)
2827 target.system_wide = true;
2828 }
2829}
2830
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002831int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002832{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002833 const char * const stat_usage[] = {
2834 "perf stat [<options>] [<command>]",
2835 NULL
2836 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002837 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002838 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002839 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002840 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002841 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002842
Stephane Eranian5af52b52010-05-18 15:00:01 +02002843 setlocale(LC_ALL, "");
2844
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002845 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002846 if (evsel_list == NULL)
2847 return -ENOMEM;
2848
Wang Nan1669e502016-02-19 11:43:58 +00002849 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002850 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2851 (const char **) stat_usage,
2852 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002853 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002854 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002855
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002856 if (csv_sep) {
2857 csv_output = true;
2858 if (!strcmp(csv_sep, "\\t"))
2859 csv_sep = "\t";
2860 } else
2861 csv_sep = DEFAULT_SEPARATOR;
2862
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002863 if (argc && !strncmp(argv[0], "rec", 3)) {
2864 argc = __cmd_record(argc, argv);
2865 if (argc < 0)
2866 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002867 } else if (argc && !strncmp(argv[0], "rep", 3))
2868 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002869
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002870 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002871 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002872
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002873 /*
2874 * For record command the -o is already taken care of.
2875 */
2876 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002877 output = NULL;
2878
Jim Cromie56f3bae2011-09-07 17:14:00 -06002879 if (output_name && output_fd) {
2880 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002881 parse_options_usage(stat_usage, stat_options, "o", 1);
2882 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002883 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002884 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002885
Andi Kleen54b50912016-03-03 15:57:36 -08002886 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2887 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2888 goto out;
2889 }
2890
Andi Kleen54b50912016-03-03 15:57:36 -08002891 if (metric_only && run_count > 1) {
2892 fprintf(stderr, "--metric-only is not supported with -r\n");
2893 goto out;
2894 }
2895
Jiri Olsae55c14a2018-04-23 11:08:21 +02002896 if (walltime_run_table && run_count <= 1) {
2897 fprintf(stderr, "--table is only supported with -r\n");
2898 parse_options_usage(stat_usage, stat_options, "r", 1);
2899 parse_options_usage(NULL, stat_options, "table", 0);
2900 goto out;
2901 }
2902
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002903 if (output_fd < 0) {
2904 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002905 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002906 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002907 }
2908
Stephane Eranian4aa90152011-08-15 22:22:33 +02002909 if (!output) {
2910 struct timespec tm;
2911 mode = append_file ? "a" : "w";
2912
2913 output = fopen(output_name, mode);
2914 if (!output) {
2915 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002916 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002917 }
2918 clock_gettime(CLOCK_REALTIME, &tm);
2919 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002920 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002921 mode = append_file ? "a" : "w";
2922 output = fdopen(output_fd, mode);
2923 if (!output) {
2924 perror("Failed opening logfd");
2925 return -errno;
2926 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002927 }
2928
Jiri Olsa58215222015-07-21 14:31:24 +02002929 stat_config.output = output;
2930
Stephane Eraniand7470b62010-12-01 18:49:05 +02002931 /*
2932 * let the spreadsheet do the pretty-printing
2933 */
2934 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002935 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002936 if (big_num_opt == 1) {
2937 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002938 parse_options_usage(stat_usage, stat_options, "B", 1);
2939 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002940 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002941 } else /* Nope, so disable big number formatting */
2942 big_num = false;
2943 } else if (big_num_opt == 0) /* User passed --no-big-num */
2944 big_num = false;
2945
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002946 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002947
Jiri Olsa0ce2da12018-06-05 14:13:13 +02002948 /*
2949 * Display user/system times only for single
2950 * run and when there's specified tracee.
2951 */
2952 if ((run_count == 1) && target__none(&target))
2953 ru_display = true;
2954
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002955 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002956 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002957 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002958 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002959 } else if (run_count == 0) {
2960 forever = true;
2961 run_count = 1;
2962 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002963
Jiri Olsae55c14a2018-04-23 11:08:21 +02002964 if (walltime_run_table) {
2965 walltime_run = zalloc(run_count * sizeof(walltime_run[0]));
2966 if (!walltime_run) {
2967 pr_err("failed to setup -r option");
2968 goto out;
2969 }
2970 }
2971
Jin Yao1d9f8d12017-12-05 22:03:10 +08002972 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2973 !target__has_task(&target)) {
2974 if (!target.system_wide || target.cpu_list) {
2975 fprintf(stderr, "The --per-thread option is only "
2976 "available when monitoring via -p -t -a "
2977 "options or only --per-thread.\n");
2978 parse_options_usage(NULL, stat_options, "p", 1);
2979 parse_options_usage(NULL, stat_options, "t", 1);
2980 goto out;
2981 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002982 }
2983
2984 /*
2985 * no_aggr, cgroup are for system-wide only
2986 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2987 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002988 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2989 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002990 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002991 fprintf(stderr, "both cgroup and no-aggregation "
2992 "modes only available in system-wide mode\n");
2993
Jiri Olsae0547312015-11-05 15:40:45 +01002994 parse_options_usage(stat_usage, stat_options, "G", 1);
2995 parse_options_usage(NULL, stat_options, "A", 1);
2996 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002997 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002998 }
2999
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02003000 if (add_default_attributes())
3001 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003002
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003003 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02003004
Jin Yao1d9f8d12017-12-05 22:03:10 +08003005 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
3006 target.per_thread = true;
3007
Namhyung Kim77a6f012012-05-07 14:09:04 +09003008 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003009 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09003010 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01003011 parse_options_usage(stat_usage, stat_options, "p", 1);
3012 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03003013 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09003014 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01003015 parse_options_usage(stat_usage, stat_options, "C", 1);
3016 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09003017 }
3018 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02003019 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02003020
3021 /*
3022 * Initialize thread_map with comm names,
3023 * so we could print it out on output.
3024 */
Jin Yao56739442017-12-05 22:03:07 +08003025 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02003026 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08003027 if (target.system_wide) {
3028 if (runtime_stat_new(&stat_config,
3029 thread_map__nr(evsel_list->threads))) {
3030 goto out;
3031 }
3032 }
3033 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02003034
yuzhoujiandb06a262018-01-29 10:25:22 +01003035 if (stat_config.times && interval)
3036 interval_count = true;
3037 else if (stat_config.times && !interval) {
3038 pr_err("interval-count option should be used together with "
3039 "interval-print.\n");
3040 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
3041 parse_options_usage(stat_usage, stat_options, "I", 1);
3042 goto out;
3043 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02003044
yuzhoujianf1f8ad52018-01-29 10:25:23 +01003045 if (timeout && timeout < 100) {
3046 if (timeout < 10) {
3047 pr_err("timeout must be >= 10ms.\n");
3048 parse_options_usage(stat_usage, stat_options, "timeout", 0);
3049 goto out;
3050 } else
3051 pr_warning("timeout < 100ms. "
3052 "The overhead percentage could be high in some cases. "
3053 "Please proceed with caution.\n");
3054 }
3055 if (timeout && interval) {
3056 pr_err("timeout option is not supported with interval-print.\n");
3057 parse_options_usage(stat_usage, stat_options, "timeout", 0);
3058 parse_options_usage(stat_usage, stat_options, "I", 1);
3059 goto out;
3060 }
3061
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003062 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003063 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03003064
Stephane Eranian86ee6e12013-02-14 13:57:27 +01003065 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03003066 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01003067
Ingo Molnar58d7e992009-05-15 11:03:23 +02003068 /*
3069 * We dont want to block the signals - that would cause
3070 * child tasks to inherit that and Ctrl-C would not work.
3071 * What we want is for Ctrl-C to work in the exec()-ed
3072 * task, but being ignored by perf stat itself:
3073 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02003074 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003075 if (!forever)
3076 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01003077 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02003078 signal(SIGALRM, skip_signal);
3079 signal(SIGABRT, skip_signal);
3080
Ingo Molnar42202dd2009-06-13 14:57:28 +02003081 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003082 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09003083 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02003084 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
3085 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02003086
Jiri Olsae55c14a2018-04-23 11:08:21 +02003087 status = run_perf_stat(argc, argv, run_idx);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003088 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02003089 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02003090 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003091 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02003092 }
3093
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003094 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02003095 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003096
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003097 if (STAT_RECORD) {
3098 /*
3099 * We synthesize the kernel mmap record just so that older tools
3100 * don't emit warnings about not being able to resolve symbols
3101 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3102 * a saner message about no samples being in the perf.data file.
3103 *
3104 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01003105 * in header.c at the moment 'perf stat record' gets introduced, which
3106 * is not really needed once we start adding the stat specific PERF_RECORD_
3107 * records, but the need to suppress the kptr_restrict messages in older
3108 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003109 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003110 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003111 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3112 process_synthesized_event,
3113 &perf_stat.session->machines.host);
3114 if (err) {
3115 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3116 "older tools may produce warnings about this file\n.");
3117 }
3118
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003119 if (!interval) {
3120 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3121 pr_err("failed to write stat round event\n");
3122 }
3123
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003124 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003125 perf_stat.session->header.data_size += perf_stat.bytes_written;
3126 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3127 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003128
3129 perf_session__delete(perf_stat.session);
3130 }
3131
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003132 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003133 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003134out:
Jiri Olsae55c14a2018-04-23 11:08:21 +02003135 free(walltime_run);
3136
Kan Liangdaefd0b2017-05-26 12:05:38 -07003137 if (smi_cost && smi_reset)
3138 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3139
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003140 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003141
3142 runtime_stat_delete(&stat_config);
3143
Ingo Molnar42202dd2009-06-13 14:57:28 +02003144 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003145}