blob: 147a27e8c937cdb5b81c6929f2896e97623cee28 [file] [log] [blame]
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001/*
Ingo Molnarbf9e1872009-06-02 23:37:05 +02002 * builtin-stat.c
3 *
4 * Builtin stat command: Give a precise performance counters summary
5 * overview about any workload, CPU or specific PID.
6 *
7 * Sample output:
Ingo Molnarddcacfa2009-04-20 15:37:32 +02008
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02009 $ perf stat ./hackbench 10
Ingo Molnarddcacfa2009-04-20 15:37:32 +020010
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020011 Time: 0.118
Ingo Molnarddcacfa2009-04-20 15:37:32 +020012
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020013 Performance counter stats for './hackbench 10':
Ingo Molnarddcacfa2009-04-20 15:37:32 +020014
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020015 1708.761321 task-clock # 11.037 CPUs utilized
16 41,190 context-switches # 0.024 M/sec
17 6,735 CPU-migrations # 0.004 M/sec
18 17,318 page-faults # 0.010 M/sec
19 5,205,202,243 cycles # 3.046 GHz
20 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle
21 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle
22 2,603,501,247 instructions # 0.50 insns per cycle
23 # 1.48 stalled cycles per insn
24 484,357,498 branches # 283.455 M/sec
25 6,388,934 branch-misses # 1.32% of all branches
26
27 0.154822978 seconds time elapsed
Ingo Molnarddcacfa2009-04-20 15:37:32 +020028
Ingo Molnar52425192009-05-26 09:17:18 +020029 *
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020030 * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
Ingo Molnar52425192009-05-26 09:17:18 +020031 *
32 * Improvements and fixes by:
33 *
34 * Arjan van de Ven <arjan@linux.intel.com>
35 * Yanmin Zhang <yanmin.zhang@intel.com>
36 * Wu Fengguang <fengguang.wu@intel.com>
37 * Mike Galbraith <efault@gmx.de>
38 * Paul Mackerras <paulus@samba.org>
Jaswinder Singh Rajput6e750a8f2009-06-27 03:02:07 +053039 * Jaswinder Singh Rajput <jaswinder@kernel.org>
Ingo Molnar52425192009-05-26 09:17:18 +020040 *
41 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnarddcacfa2009-04-20 15:37:32 +020042 */
43
Peter Zijlstra1a482f32009-05-23 18:28:58 +020044#include "perf.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +020045#include "builtin.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030046#include "util/cgroup.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020047#include "util/util.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060048#include <subcmd/parse-options.h>
Ingo Molnar52425192009-05-26 09:17:18 +020049#include "util/parse-events.h"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070050#include "util/pmu.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020051#include "util/event.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020052#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020053#include "util/evsel.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020054#include "util/debug.h"
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -060055#include "util/drv_configs.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020056#include "util/color.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080057#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080058#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110059#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030060#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020061#include "util/thread_map.h"
Jiri Olsad8095602015-08-07 12:51:03 +020062#include "util/counts.h"
Andi Kleen44b1e602016-05-30 12:49:42 -030063#include "util/group.h"
Jiri Olsa4979d0c2015-11-05 15:40:46 +010064#include "util/session.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010065#include "util/tool.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030066#include "util/string2.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070067#include "util/metricgroup.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010068#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020069
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030070#include <linux/time64.h>
Andi Kleen44b1e602016-05-30 12:49:42 -030071#include <api/fs/fs.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030072#include <errno.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030073#include <signal.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020074#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020075#include <sys/prctl.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030076#include <inttypes.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020077#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070078#include <math.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030079#include <sys/types.h>
80#include <sys/stat.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030081#include <sys/wait.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030082#include <unistd.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020083
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030084#include "sane_ctype.h"
85
Stephane Eraniand7470b62010-12-01 18:49:05 +020086#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060087#define CNTR_NOT_SUPPORTED "<not supported>"
88#define CNTR_NOT_COUNTED "<not counted>"
Kan Liangdaefd0b2017-05-26 12:05:38 -070089#define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
Stephane Eraniand7470b62010-12-01 18:49:05 +020090
Jiri Olsad4f63a42015-06-26 11:29:26 +020091static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010092
Andi Kleen4cabc3d2013-08-21 16:47:26 -070093/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020094static const char *transaction_attrs = {
95 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070096 "{"
97 "instructions,"
98 "cycles,"
99 "cpu/cycles-t/,"
100 "cpu/tx-start/,"
101 "cpu/el-start/,"
102 "cpu/cycles-ct/"
103 "}"
104};
105
106/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +0200107static const char * transaction_limited_attrs = {
108 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700109 "{"
110 "instructions,"
111 "cycles,"
112 "cpu/cycles-t/,"
113 "cpu/tx-start/"
114 "}"
115};
116
Andi Kleen44b1e602016-05-30 12:49:42 -0300117static const char * topdown_attrs[] = {
118 "topdown-total-slots",
119 "topdown-slots-retired",
120 "topdown-recovery-bubbles",
121 "topdown-fetch-bubbles",
122 "topdown-slots-issued",
123 NULL,
124};
125
Kan Liangdaefd0b2017-05-26 12:05:38 -0700126static const char *smi_cost_attrs = {
127 "{"
128 "msr/aperf/,"
129 "msr/smi/,"
130 "cycles"
131 "}"
132};
133
Robert Richter666e6d42012-04-05 18:26:27 +0200134static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200135
Andi Kleenb18f3e32017-08-31 12:40:31 -0700136static struct rblist metric_events;
137
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300138static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900139 .uid = UINT_MAX,
140};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530141
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100142typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
143
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530144static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200145static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200146static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000147static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200148static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700149static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300150static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700151static bool smi_cost = false;
152static bool smi_reset = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200153static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200154static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200155static const char *csv_sep = NULL;
156static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800157static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200158static const char *pre_cmd = NULL;
159static const char *post_cmd = NULL;
160static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700161static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100162static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500163static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800164static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300165static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700166static bool no_merge = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100167static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100168static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100169static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100170static bool append_file;
yuzhoujiandb06a262018-01-29 10:25:22 +0100171static bool interval_count;
Jiri Olsae0547312015-11-05 15:40:45 +0100172static const char *output_name;
173static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100174static int print_free_counters_hint;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200175
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100176struct perf_stat {
177 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100178 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100179 struct perf_session *session;
180 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100181 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100182 bool maps_allocated;
183 struct cpu_map *cpus;
184 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100185 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100186};
187
188static struct perf_stat perf_stat;
189#define STAT_RECORD perf_stat.record
190
Liming Wang60666c62009-12-31 16:05:50 +0800191static volatile int done = 0;
192
Jiri Olsa421a50f2015-07-21 14:31:22 +0200193static struct perf_stat_config stat_config = {
194 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200195 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200196};
197
Andi Kleene864c5c2017-08-31 12:40:35 -0700198static bool is_duration_time(struct perf_evsel *evsel)
199{
200 return !strcmp(evsel->name, "duration_time");
201}
202
Stephane Eranian13370a92013-01-29 12:47:44 +0100203static inline void diff_timespec(struct timespec *r, struct timespec *a,
204 struct timespec *b)
205{
206 r->tv_sec = a->tv_sec - b->tv_sec;
207 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300208 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100209 r->tv_sec--;
210 } else {
211 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
212 }
213}
214
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200215static void perf_stat__reset_stats(void)
216{
Jin Yao56739442017-12-05 22:03:07 +0800217 int i;
218
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200219 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200220 perf_stat__reset_shadow_stats();
Jin Yao56739442017-12-05 22:03:07 +0800221
222 for (i = 0; i < stat_config.stats_num; i++)
223 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200224}
225
Jiri Olsacac21422012-11-12 18:34:00 +0100226static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200227{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200228 struct perf_event_attr *attr = &evsel->attr;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200229 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200230
Jiri Olsa82bf3112017-07-26 14:02:06 +0200231 if (stat_config.scale) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200232 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
233 PERF_FORMAT_TOTAL_TIME_RUNNING;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200234 }
235
236 /*
237 * The event is part of non trivial group, let's enable
238 * the group read (for leader) and ID retrieval for all
239 * members.
240 */
241 if (leader->nr_members > 1)
242 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200243
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200244 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300245
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100246 /*
247 * Some events get initialized with sample_(period/type) set,
248 * like tracepoints. Clear it up for counting.
249 */
250 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100251
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100252 /*
253 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
254 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100255 *
256 * However for pipe sessions we need to keep it zero,
257 * because script's perf_evsel__check_attr is triggered
258 * by attr->sample_type != 0, and we can't run it on
259 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100260 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100261 if (!(STAT_RECORD && perf_stat.data.is_pipe))
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100262 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100263
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100264 /*
265 * Disabling all counters initially, they will be enabled
266 * either manually by us or by kernel via enable_on_exec
267 * set later.
268 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100269 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100270 attr->disabled = 1;
271
Jiri Olsac8280ce2015-12-03 10:06:45 +0100272 /*
273 * In case of initial_delay we enable tracee
274 * events manually.
275 */
276 if (target__none(&target) && !initial_delay)
277 attr->enable_on_exec = 1;
278 }
279
Jin Yao1d9f8d12017-12-05 22:03:10 +0800280 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300281 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200282
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300283 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200284}
285
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200286/*
287 * Does the counter have nsecs as a unit?
288 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200289static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200290{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200291 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
292 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200293 return 1;
294
295 return 0;
296}
297
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100298static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100299 union perf_event *event,
300 struct perf_sample *sample __maybe_unused,
301 struct machine *machine __maybe_unused)
302{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100303 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100304 pr_err("failed to write perf data, error: %m\n");
305 return -1;
306 }
307
308 perf_stat.bytes_written += event->header.size;
309 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100310}
311
Jiri Olsa1975d362015-11-05 15:40:56 +0100312static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100313{
Jiri Olsa1975d362015-11-05 15:40:56 +0100314 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100315 process_synthesized_event,
316 NULL);
317}
318
319#define WRITE_STAT_ROUND_EVENT(time, interval) \
320 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
321
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100322#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
323
324static int
325perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
326 struct perf_counts_values *count)
327{
328 struct perf_sample_id *sid = SID(counter, cpu, thread);
329
330 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
331 process_synthesized_event, NULL);
332}
333
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200334/*
335 * Read out the results of a single counter:
336 * do not aggregate counts across CPUs in system-wide mode
337 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200338static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200339{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100340 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100341 int ncpus, cpu, thread;
342
Jin Yao1d9f8d12017-12-05 22:03:10 +0800343 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Mark Rutland00e727b2016-07-15 11:08:10 +0100344 ncpus = perf_evsel__nr_cpus(counter);
345 else
346 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200347
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000348 if (!counter->supported)
349 return -ENOENT;
350
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100351 if (counter->system_wide)
352 nthreads = 1;
353
354 for (thread = 0; thread < nthreads; thread++) {
355 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200356 struct perf_counts_values *count;
357
358 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200359
360 /*
361 * The leader's group read loads data into its group members
362 * (via perf_evsel__read_counter) and sets threir count->loaded.
363 */
364 if (!count->loaded &&
365 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700366 counter->counts->scaled = -1;
367 perf_counts(counter->counts, cpu, thread)->ena = 0;
368 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100369 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700370 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100371
Jiri Olsa82bf3112017-07-26 14:02:06 +0200372 count->loaded = false;
373
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100374 if (STAT_RECORD) {
375 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
376 pr_err("failed to write stat event\n");
377 return -1;
378 }
379 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700380
381 if (verbose > 1) {
382 fprintf(stat_config.output,
383 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
384 perf_evsel__name(counter),
385 cpu,
386 count->val, count->ena, count->run);
387 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100388 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200389 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200390
391 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200392}
393
Mark Rutland3df33ef2016-08-09 14:04:29 +0100394static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200395{
396 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700397 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200398
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300399 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700400 ret = read_counter(counter);
401 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700402 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200403
Stephane Eraniandb49a712017-04-12 11:23:01 -0700404 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200405 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200406 }
407}
408
Jiri Olsaba411a92015-06-26 11:29:24 +0200409static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100410{
Stephane Eranian13370a92013-01-29 12:47:44 +0100411 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100412
Mark Rutland3df33ef2016-08-09 14:04:29 +0100413 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100414
Stephane Eranian13370a92013-01-29 12:47:44 +0100415 clock_gettime(CLOCK_MONOTONIC, &ts);
416 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100417
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100418 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300419 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100420 pr_err("failed to write stat round event\n");
421 }
422
Andi Kleenb90f1332017-08-31 12:40:36 -0700423 init_stats(&walltime_nsecs_stats);
424 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200425 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100426}
427
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100428static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700429{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100430 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300431 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100432
433 /*
434 * We need to enable counters only if:
435 * - we don't have tracee (attaching to task or cpu)
436 * - we have initial delay configured
437 */
438 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100439 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700440}
441
Mark Rutland3df33ef2016-08-09 14:04:29 +0100442static void disable_counters(void)
443{
444 /*
445 * If we don't have tracee (attaching to task or cpu), counters may
446 * still be running. To get accurate group ratios, we must stop groups
447 * from counting before reading their constituent counters.
448 */
449 if (!target__none(&target))
450 perf_evlist__disable(evsel_list);
451}
452
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300453static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300454
455/*
456 * perf_evlist__prepare_workload will send a SIGUSR1
457 * if the fork fails, since we asked by setting its
458 * want_signal to true.
459 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300460static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
461 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300462{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300463 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300464}
465
Jiri Olsa664c98d2015-11-05 15:40:50 +0100466static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100467{
468 int err;
469
Jiri Olsa664c98d2015-11-05 15:40:50 +0100470 if (is_pipe) {
471 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
472 process_synthesized_event);
473 if (err < 0) {
474 pr_err("Couldn't synthesize attrs.\n");
475 return err;
476 }
477 }
478
Andi Kleenbfd8f722017-11-17 13:42:58 -0800479 err = perf_event__synthesize_extra_attr(NULL,
480 evsel_list,
481 process_synthesized_event,
482 is_pipe);
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100483
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100484 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
485 process_synthesized_event,
486 NULL);
487 if (err < 0) {
488 pr_err("Couldn't synthesize thread map.\n");
489 return err;
490 }
491
492 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
493 process_synthesized_event, NULL);
494 if (err < 0) {
495 pr_err("Couldn't synthesize thread map.\n");
496 return err;
497 }
498
499 err = perf_event__synthesize_stat_config(NULL, &stat_config,
500 process_synthesized_event, NULL);
501 if (err < 0) {
502 pr_err("Couldn't synthesize config.\n");
503 return err;
504 }
505
506 return 0;
507}
508
Jiri Olsa2af46462015-11-05 15:40:49 +0100509#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
510
Andi Kleen42811d52017-10-05 19:00:28 -0700511static int __store_counter_ids(struct perf_evsel *counter)
Jiri Olsa2af46462015-11-05 15:40:49 +0100512{
513 int cpu, thread;
514
Andi Kleen42811d52017-10-05 19:00:28 -0700515 for (cpu = 0; cpu < xyarray__max_x(counter->fd); cpu++) {
516 for (thread = 0; thread < xyarray__max_y(counter->fd);
517 thread++) {
Jiri Olsa2af46462015-11-05 15:40:49 +0100518 int fd = FD(counter, cpu, thread);
519
520 if (perf_evlist__id_add_fd(evsel_list, counter,
521 cpu, thread, fd) < 0)
522 return -1;
523 }
524 }
525
526 return 0;
527}
528
529static int store_counter_ids(struct perf_evsel *counter)
530{
531 struct cpu_map *cpus = counter->cpus;
532 struct thread_map *threads = counter->threads;
533
534 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
535 return -ENOMEM;
536
Andi Kleen42811d52017-10-05 19:00:28 -0700537 return __store_counter_ids(counter);
Jiri Olsa2af46462015-11-05 15:40:49 +0100538}
539
Jiri Olsa82bf3112017-07-26 14:02:06 +0200540static bool perf_evsel__should_store_id(struct perf_evsel *counter)
541{
542 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
543}
544
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700545static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
546{
547 struct perf_evsel *c2, *leader;
548 bool is_open = true;
549
550 leader = evsel->leader;
551 pr_debug("Weak group for %s/%d failed\n",
552 leader->name, leader->nr_members);
553
554 /*
555 * for_each_group_member doesn't work here because it doesn't
556 * include the first entry.
557 */
558 evlist__for_each_entry(evsel_list, c2) {
559 if (c2 == evsel)
560 is_open = false;
561 if (c2->leader == leader) {
562 if (is_open)
563 perf_evsel__close(c2);
564 c2->leader = c2;
565 c2->nr_members = 0;
566 }
567 }
568 return leader;
569}
570
Namhyung Kimacf28922013-03-11 16:43:18 +0900571static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200572{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200573 int interval = stat_config.interval;
yuzhoujiandb06a262018-01-29 10:25:22 +0100574 int times = stat_config.times;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100575 int timeout = stat_config.timeout;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300576 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200577 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100578 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100579 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100580 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200581 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300582 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100583 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600584 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200585
Stephane Eranian13370a92013-01-29 12:47:44 +0100586 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300587 ts.tv_sec = interval / USEC_PER_MSEC;
588 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100589 } else if (timeout) {
590 ts.tv_sec = timeout / USEC_PER_MSEC;
591 ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100592 } else {
593 ts.tv_sec = 1;
594 ts.tv_nsec = 0;
595 }
596
Liming Wang60666c62009-12-31 16:05:50 +0800597 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100598 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300599 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900600 perror("failed to prepare workload");
601 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800602 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900603 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000604 }
605
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200606 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300607 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200608
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300609 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300610try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100611 if (create_perf_stat_counter(counter) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700612
613 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700614 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700615 counter->leader != counter &&
616 counter->weak_group) {
617 counter = perf_evsel__reset_weak_group(counter);
618 goto try_again;
619 }
620
David Ahern979987a2012-05-08 09:29:16 -0600621 /*
622 * PPC returns ENXIO for HW counters until 2.6.37
623 * (behavior changed with commit b0a873e).
624 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100625 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600626 errno == ENOENT || errno == EOPNOTSUPP ||
627 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900628 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600629 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300630 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600631 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400632
633 if ((counter->leader != counter) ||
634 !(counter->leader->nr_members > 1))
635 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300636 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900637 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300638 ui__warning("%s\n", msg);
639 goto try_again;
Jin Yaoab6c79b2018-01-16 23:43:08 +0800640 } else if (target__has_per_thread(&target) &&
641 evsel_list->threads &&
642 evsel_list->threads->err_thread != -1) {
643 /*
644 * For global --per-thread case, skip current
645 * error thread.
646 */
647 if (!thread_map__remove(evsel_list->threads,
648 evsel_list->threads->err_thread)) {
649 evsel_list->threads->err_thread = -1;
650 goto try_again;
651 }
652 }
Ingo Molnarede70292011-04-28 08:48:42 +0200653
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300654 perf_evsel__open_strerror(counter, &target,
655 errno, msg, sizeof(msg));
656 ui__error("%s\n", msg);
657
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200658 if (child_pid != -1)
659 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600660
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200661 return -1;
662 }
David Ahern2cee77c2011-05-30 08:55:59 -0600663 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100664
665 l = strlen(counter->unit);
666 if (l > unit_width)
667 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100668
Jiri Olsa82bf3112017-07-26 14:02:06 +0200669 if (perf_evsel__should_store_id(counter) &&
670 store_counter_ids(counter))
Jiri Olsa2af46462015-11-05 15:40:49 +0100671 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300672 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200673
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300674 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300675 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300676 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300677 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100678 return -1;
679 }
680
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600681 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300682 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600683 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
684 str_error_r(errno, msg, sizeof(msg)));
685 return -1;
686 }
687
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100688 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100689 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100690
Jiri Olsa664c98d2015-11-05 15:40:50 +0100691 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100692 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100693 } else {
694 err = perf_session__write_header(perf_stat.session, evsel_list,
695 fd, false);
696 }
697
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100698 if (err < 0)
699 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100700
Jiri Olsa664c98d2015-11-05 15:40:50 +0100701 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100702 if (err < 0)
703 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100704 }
705
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200706 /*
707 * Enable counters and exec the command:
708 */
709 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100710 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200711
Liming Wang60666c62009-12-31 16:05:50 +0800712 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900713 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100714 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900715
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100716 if (interval || timeout) {
Stephane Eranian13370a92013-01-29 12:47:44 +0100717 while (!waitpid(child_pid, &status, WNOHANG)) {
718 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100719 if (timeout)
720 break;
Jiri Olsaba411a92015-06-26 11:29:24 +0200721 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100722 if (interval_count && !(--times))
723 break;
Stephane Eranian13370a92013-01-29 12:47:44 +0100724 }
725 }
Milian Wolffdfc9eec2017-09-12 17:25:23 +0200726 waitpid(child_pid, &status, 0);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300727
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300728 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300729 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300730 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300731 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300732 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300733
Andi Kleen33e49ea2011-09-15 14:31:40 -0700734 if (WIFSIGNALED(status))
735 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800736 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100737 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100738 while (!done) {
739 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100740 if (timeout)
741 break;
yuzhoujiandb06a262018-01-29 10:25:22 +0100742 if (interval) {
Jiri Olsaba411a92015-06-26 11:29:24 +0200743 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100744 if (interval_count && !(--times))
745 break;
746 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100747 }
Liming Wang60666c62009-12-31 16:05:50 +0800748 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200749
Mark Rutland3df33ef2016-08-09 14:04:29 +0100750 disable_counters();
751
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200752 t1 = rdclock();
753
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200754 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200755
Mark Rutland3df33ef2016-08-09 14:04:29 +0100756 /*
757 * Closing a group leader splits the group, and as we only disable
758 * group leaders, results in remaining events becoming enabled. To
759 * avoid arbitrary skew, we must read all counters before closing any
760 * group leaders.
761 */
762 read_counters();
763 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200764
Ingo Molnar42202dd2009-06-13 14:57:28 +0200765 return WEXITSTATUS(status);
766}
767
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300768static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200769{
770 int ret;
771
772 if (pre_cmd) {
773 ret = system(pre_cmd);
774 if (ret)
775 return ret;
776 }
777
778 if (sync_run)
779 sync();
780
781 ret = __run_perf_stat(argc, argv);
782 if (ret)
783 return ret;
784
785 if (post_cmd) {
786 ret = system(post_cmd);
787 if (ret)
788 return ret;
789 }
790
791 return ret;
792}
793
Andi Kleend73515c2015-03-11 07:16:27 -0700794static void print_running(u64 run, u64 ena)
795{
796 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200797 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700798 csv_sep,
799 run,
800 csv_sep,
801 ena ? 100.0 * run / ena : 100.0);
802 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200803 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700804 }
805}
806
Ingo Molnarf99844c2011-04-27 05:35:39 +0200807static void print_noise_pct(double total, double avg)
808{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800809 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200810
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700811 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200812 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600813 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200814 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200815}
816
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200817static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200818{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200819 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200820
Peter Zijlstra849abde2009-09-04 18:23:38 +0200821 if (run_count == 1)
822 return;
823
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300824 ps = evsel->stats;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200825 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200826}
827
Stephane Eranian12c08a92013-02-14 13:57:29 +0100828static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200829{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200830 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100831 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200832 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100833 cpu_map__id_to_socket(id),
834 csv_output ? 0 : -8,
835 cpu_map__id_to_cpu(id),
836 csv_sep,
837 csv_output ? 0 : 4,
838 nr,
839 csv_sep);
840 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100841 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200842 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100843 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100844 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100845 csv_sep,
846 csv_output ? 0 : 4,
847 nr,
848 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100849 break;
850 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200851 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200852 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100853 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100854 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200855 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200856 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200857 csv_output ? 0 : 16,
858 thread_map__comm(evsel->threads, id),
859 csv_output ? 0 : -8,
860 thread_map__pid(evsel->threads, id),
861 csv_sep);
862 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100863 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200864 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100865 default:
866 break;
867 }
868}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200869
Andi Kleen140aead2016-01-30 09:06:49 -0800870struct outstate {
871 FILE *fh;
872 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800873 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800874 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800875 int id, nr;
876 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800877};
878
879#define METRIC_LEN 35
880
881static void new_line_std(void *ctx)
882{
883 struct outstate *os = ctx;
884
885 os->newline = true;
886}
887
888static void do_new_line_std(struct outstate *os)
889{
890 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800891 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800892 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800893 if (stat_config.aggr_mode == AGGR_NONE)
894 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800895 fprintf(os->fh, " ");
896}
897
898static void print_metric_std(void *ctx, const char *color, const char *fmt,
899 const char *unit, double val)
900{
901 struct outstate *os = ctx;
902 FILE *out = os->fh;
903 int n;
904 bool newline = os->newline;
905
906 os->newline = false;
907
908 if (unit == NULL || fmt == NULL) {
909 fprintf(out, "%-*s", METRIC_LEN, "");
910 return;
911 }
912
913 if (newline)
914 do_new_line_std(os);
915
916 n = fprintf(out, " # ");
917 if (color)
918 n += color_fprintf(out, color, fmt, val);
919 else
920 n += fprintf(out, fmt, val);
921 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
922}
923
Andi Kleen92a61f62016-02-29 14:36:21 -0800924static void new_line_csv(void *ctx)
925{
926 struct outstate *os = ctx;
927 int i;
928
929 fputc('\n', os->fh);
930 if (os->prefix)
931 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800932 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800933 for (i = 0; i < os->nfields; i++)
934 fputs(csv_sep, os->fh);
935}
936
937static void print_metric_csv(void *ctx,
938 const char *color __maybe_unused,
939 const char *fmt, const char *unit, double val)
940{
941 struct outstate *os = ctx;
942 FILE *out = os->fh;
943 char buf[64], *vals, *ends;
944
945 if (unit == NULL || fmt == NULL) {
Ilya Pronin40c21892018-03-05 22:43:53 -0800946 fprintf(out, "%s%s", csv_sep, csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800947 return;
948 }
949 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900950 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800951 while (isdigit(*ends) || *ends == '.')
952 ends++;
953 *ends = 0;
954 while (isspace(*unit))
955 unit++;
956 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
957}
958
Andi Kleen54b50912016-03-03 15:57:36 -0800959#define METRIC_ONLY_LEN 20
960
961/* Filter out some columns that don't work well in metrics only mode */
962
963static bool valid_only_metric(const char *unit)
964{
965 if (!unit)
966 return false;
967 if (strstr(unit, "/sec") ||
968 strstr(unit, "hz") ||
969 strstr(unit, "Hz") ||
970 strstr(unit, "CPUs utilized"))
971 return false;
972 return true;
973}
974
975static const char *fixunit(char *buf, struct perf_evsel *evsel,
976 const char *unit)
977{
978 if (!strncmp(unit, "of all", 6)) {
979 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
980 unit);
981 return buf;
982 }
983 return unit;
984}
985
986static void print_metric_only(void *ctx, const char *color, const char *fmt,
987 const char *unit, double val)
988{
989 struct outstate *os = ctx;
990 FILE *out = os->fh;
991 int n;
992 char buf[1024];
993 unsigned mlen = METRIC_ONLY_LEN;
994
995 if (!valid_only_metric(unit))
996 return;
997 unit = fixunit(buf, os->evsel, unit);
998 if (color)
999 n = color_fprintf(out, color, fmt, val);
1000 else
1001 n = fprintf(out, fmt, val);
1002 if (n > METRIC_ONLY_LEN)
1003 n = METRIC_ONLY_LEN;
1004 if (mlen < strlen(unit))
1005 mlen = strlen(unit) + 1;
1006 fprintf(out, "%*s", mlen - n, "");
1007}
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
1046 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
1047}
1048
Andi Kleenda88c7f2014-09-24 13:50:46 -07001049static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001050{
Jiri Olsa58215222015-07-21 14:31:24 +02001051 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001052 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +01001053 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -06001054 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001055
Stephane Eranian410136f2013-11-12 17:58:49 +01001056 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
1057 fmt_n = csv_output ? "%s" : "%-25s";
1058
Andi Kleenda88c7f2014-09-24 13:50:46 -07001059 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001060
David Ahern4bbe5a62013-09-28 14:28:00 -06001061 scnprintf(name, sizeof(name), "%s%s",
1062 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +01001063
1064 fprintf(output, fmt_v, msecs, csv_sep);
1065
1066 if (csv_output)
1067 fprintf(output, "%s%s", evsel->unit, csv_sep);
1068 else
1069 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1070
1071 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001072
Stephane Eranian023695d2011-02-14 11:20:01 +02001073 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001074 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001075}
1076
Andi Kleen44d49a62016-02-29 14:36:22 -08001077static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1078{
1079 int i;
1080
1081 if (!aggr_get_id)
1082 return 0;
1083
1084 if (stat_config.aggr_mode == AGGR_NONE)
1085 return id;
1086
1087 if (stat_config.aggr_mode == AGGR_GLOBAL)
1088 return 0;
1089
1090 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1091 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1092
1093 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1094 return cpu2;
1095 }
1096 return 0;
1097}
1098
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001099static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1100{
Jiri Olsa58215222015-07-21 14:31:24 +02001101 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001102 double sc = evsel->scale;
1103 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001104
1105 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001106 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001107 } else {
1108 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001109 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001110 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001111 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001112 }
1113
1114 aggr_printout(evsel, id, nr);
1115
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001116 fprintf(output, fmt, avg, csv_sep);
1117
1118 if (evsel->unit)
1119 fprintf(output, "%-*s%s",
1120 csv_output ? 0 : unit_width,
1121 evsel->unit, csv_sep);
1122
1123 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1124
1125 if (evsel->cgrp)
1126 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001127}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001128
Andi Kleenf9483392016-01-30 09:06:50 -08001129static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +08001130 char *prefix, u64 run, u64 ena, double noise,
1131 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001132{
Andi Kleen140aead2016-01-30 09:06:49 -08001133 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001134 struct outstate os = {
1135 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001136 .prefix = prefix ? prefix : "",
1137 .id = id,
1138 .nr = nr,
1139 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001140 };
Andi Kleen140aead2016-01-30 09:06:49 -08001141 print_metric_t pm = print_metric_std;
1142 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001143
Andi Kleen54b50912016-03-03 15:57:36 -08001144 if (metric_only) {
1145 nl = new_line_metric;
1146 if (csv_output)
1147 pm = print_metric_only_csv;
1148 else
1149 pm = print_metric_only;
1150 } else
1151 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001152
Andi Kleen54b50912016-03-03 15:57:36 -08001153 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001154 static int aggr_fields[] = {
1155 [AGGR_GLOBAL] = 0,
1156 [AGGR_THREAD] = 1,
1157 [AGGR_NONE] = 1,
1158 [AGGR_SOCKET] = 2,
1159 [AGGR_CORE] = 2,
1160 };
1161
1162 pm = print_metric_csv;
1163 nl = new_line_csv;
1164 os.nfields = 3;
1165 os.nfields += aggr_fields[stat_config.aggr_mode];
1166 if (counter->cgrp)
1167 os.nfields++;
1168 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001169 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001170 if (metric_only) {
1171 pm(&os, NULL, "", "", 0);
1172 return;
1173 }
Andi Kleencb110f42016-01-30 09:06:51 -08001174 aggr_printout(counter, id, nr);
1175
1176 fprintf(stat_config.output, "%*s%s",
1177 csv_output ? 0 : 18,
1178 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1179 csv_sep);
1180
Borislav Petkov02d492e2017-02-07 01:40:05 +01001181 if (counter->supported)
1182 print_free_counters_hint = 1;
1183
Andi Kleencb110f42016-01-30 09:06:51 -08001184 fprintf(stat_config.output, "%-*s%s",
1185 csv_output ? 0 : unit_width,
1186 counter->unit, csv_sep);
1187
1188 fprintf(stat_config.output, "%*s",
1189 csv_output ? 0 : -25,
1190 perf_evsel__name(counter));
1191
1192 if (counter->cgrp)
1193 fprintf(stat_config.output, "%s%s",
1194 csv_sep, counter->cgrp->name);
1195
Andi Kleen92a61f62016-02-29 14:36:21 -08001196 if (!csv_output)
1197 pm(&os, NULL, NULL, "", 0);
1198 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001199 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001200 if (csv_output)
1201 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001202 return;
1203 }
1204
Andi Kleen54b50912016-03-03 15:57:36 -08001205 if (metric_only)
1206 /* nothing */;
1207 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001208 nsec_printout(id, nr, counter, uval);
1209 else
1210 abs_printout(id, nr, counter, uval);
1211
Andi Kleen140aead2016-01-30 09:06:49 -08001212 out.print_metric = pm;
1213 out.new_line = nl;
1214 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001215 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001216
Andi Kleen54b50912016-03-03 15:57:36 -08001217 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001218 print_noise(counter, noise);
1219 print_running(run, ena);
1220 }
1221
1222 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001223 first_shadow_cpu(counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001224 &out, &metric_events, st);
Andi Kleen54b50912016-03-03 15:57:36 -08001225 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001226 print_noise(counter, noise);
1227 print_running(run, ena);
1228 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001229}
1230
Andi Kleen44d49a62016-02-29 14:36:22 -08001231static void aggr_update_shadow(void)
1232{
1233 int cpu, s2, id, s;
1234 u64 val;
1235 struct perf_evsel *counter;
1236
1237 for (s = 0; s < aggr_map->nr; s++) {
1238 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001239 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001240 val = 0;
1241 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1242 s2 = aggr_get_id(evsel_list->cpus, cpu);
1243 if (s2 != id)
1244 continue;
1245 val += perf_counts(counter->counts, cpu, 0)->val;
1246 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001247 perf_stat__update_shadow_stats(counter, val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001248 first_shadow_cpu(counter, id),
1249 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001250 }
1251 }
1252}
1253
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001254static void uniquify_event_name(struct perf_evsel *counter)
1255{
1256 char *new_name;
1257 char *config;
1258
1259 if (!counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
1260 strlen(counter->pmu_name)))
1261 return;
1262
1263 config = strchr(counter->name, '/');
1264 if (config) {
1265 if (asprintf(&new_name,
1266 "%s%s", counter->pmu_name, config) > 0) {
1267 free(counter->name);
1268 counter->name = new_name;
1269 }
1270 } else {
1271 if (asprintf(&new_name,
1272 "%s [%s]", counter->name, counter->pmu_name) > 0) {
1273 free(counter->name);
1274 counter->name = new_name;
1275 }
1276 }
1277}
1278
Andi Kleen430daf22017-03-20 13:17:00 -07001279static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001280 void (*cb)(struct perf_evsel *counter, void *data,
1281 bool first),
1282 void *data)
1283{
Andi Kleen430daf22017-03-20 13:17:00 -07001284 struct perf_evsel *alias;
1285
1286 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1287 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1288 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1289 alias->scale != counter->scale ||
1290 alias->cgrp != counter->cgrp ||
1291 strcmp(alias->unit, counter->unit) ||
1292 nsec_counter(alias) != nsec_counter(counter))
1293 break;
1294 alias->merged_stat = true;
1295 cb(alias, data, false);
1296 }
1297}
1298
1299static bool collect_data(struct perf_evsel *counter,
1300 void (*cb)(struct perf_evsel *counter, void *data,
1301 bool first),
1302 void *data)
1303{
1304 if (counter->merged_stat)
1305 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001306 cb(counter, data, true);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001307 if (no_merge)
1308 uniquify_event_name(counter);
1309 else if (counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001310 collect_all_aliases(counter, cb, data);
1311 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001312}
1313
1314struct aggr_data {
1315 u64 ena, run, val;
1316 int id;
1317 int nr;
1318 int cpu;
1319};
1320
1321static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1322{
1323 struct aggr_data *ad = data;
1324 int cpu, s2;
1325
1326 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1327 struct perf_counts_values *counts;
1328
1329 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1330 if (s2 != ad->id)
1331 continue;
1332 if (first)
1333 ad->nr++;
1334 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001335 /*
1336 * When any result is bad, make them all to give
1337 * consistent output in interval mode.
1338 */
1339 if (counts->ena == 0 || counts->run == 0 ||
1340 counter->counts->scaled == -1) {
1341 ad->ena = 0;
1342 ad->run = 0;
1343 break;
1344 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001345 ad->val += counts->val;
1346 ad->ena += counts->ena;
1347 ad->run += counts->run;
1348 }
1349}
1350
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001351static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001352{
Jiri Olsa58215222015-07-21 14:31:24 +02001353 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001354 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001355 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001356 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001357 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001358 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001359
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001360 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001361 return;
1362
Andi Kleen44d49a62016-02-29 14:36:22 -08001363 aggr_update_shadow();
1364
Andi Kleen54b50912016-03-03 15:57:36 -08001365 /*
1366 * With metric_only everything is on a single line.
1367 * Without each counter has its own line.
1368 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001369 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001370 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001371 if (prefix && metric_only)
1372 fprintf(output, "%s", prefix);
1373
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001374 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001375 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001376 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001377 if (is_duration_time(counter))
1378 continue;
1379
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001380 ad.val = ad.ena = ad.run = 0;
1381 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001382 if (!collect_data(counter, aggr_cb, &ad))
1383 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001384 nr = ad.nr;
1385 ena = ad.ena;
1386 run = ad.run;
1387 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001388 if (first && metric_only) {
1389 first = false;
1390 aggr_printout(counter, id, nr);
1391 }
1392 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001393 fprintf(output, "%s", prefix);
1394
Stephane Eranian410136f2013-11-12 17:58:49 +01001395 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001396 printout(id, nr, counter, uval, prefix, run, ena, 1.0,
1397 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001398 if (!metric_only)
1399 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001400 }
Andi Kleen54b50912016-03-03 15:57:36 -08001401 if (metric_only)
1402 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001403 }
1404}
1405
Jin Yao29734552017-12-05 22:03:11 +08001406static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001407{
Jin Yao29734552017-12-05 22:03:11 +08001408 return ((struct perf_aggr_thread_value *)b)->val -
1409 ((struct perf_aggr_thread_value *)a)->val;
1410}
1411
1412static struct perf_aggr_thread_value *sort_aggr_thread(
1413 struct perf_evsel *counter,
1414 int nthreads, int ncpus,
1415 int *ret)
1416{
1417 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001418 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001419 struct perf_aggr_thread_value *buf;
1420
1421 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1422 if (!buf)
1423 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001424
1425 for (thread = 0; thread < nthreads; thread++) {
1426 u64 ena = 0, run = 0, val = 0;
1427
1428 for (cpu = 0; cpu < ncpus; cpu++) {
1429 val += perf_counts(counter->counts, cpu, thread)->val;
1430 ena += perf_counts(counter->counts, cpu, thread)->ena;
1431 run += perf_counts(counter->counts, cpu, thread)->run;
1432 }
1433
Jin Yao29734552017-12-05 22:03:11 +08001434 uval = val * counter->scale;
1435
1436 /*
1437 * Skip value 0 when enabling --per-thread globally,
1438 * otherwise too many 0 output.
1439 */
1440 if (uval == 0.0 && target__has_per_thread(&target))
1441 continue;
1442
1443 buf[i].counter = counter;
1444 buf[i].id = thread;
1445 buf[i].uval = uval;
1446 buf[i].val = val;
1447 buf[i].run = run;
1448 buf[i].ena = ena;
1449 i++;
1450 }
1451
1452 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1453
1454 if (ret)
1455 *ret = i;
1456
1457 return buf;
1458}
1459
1460static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1461{
1462 FILE *output = stat_config.output;
1463 int nthreads = thread_map__nr(counter->threads);
1464 int ncpus = cpu_map__nr(counter->cpus);
1465 int thread, sorted_threads, id;
1466 struct perf_aggr_thread_value *buf;
1467
1468 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads);
1469 if (!buf) {
1470 perror("cannot sort aggr thread");
1471 return;
1472 }
1473
1474 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001475 if (prefix)
1476 fprintf(output, "%s", prefix);
1477
Jin Yao29734552017-12-05 22:03:11 +08001478 id = buf[thread].id;
Jin Yao14e72a22017-12-05 22:03:08 +08001479 if (stat_config.stats)
Jin Yao29734552017-12-05 22:03:11 +08001480 printout(id, 0, buf[thread].counter, buf[thread].uval,
1481 prefix, buf[thread].run, buf[thread].ena, 1.0,
1482 &stat_config.stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001483 else
Jin Yao29734552017-12-05 22:03:11 +08001484 printout(id, 0, buf[thread].counter, buf[thread].uval,
1485 prefix, buf[thread].run, buf[thread].ena, 1.0,
1486 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001487 fputc('\n', output);
1488 }
Jin Yao29734552017-12-05 22:03:11 +08001489
1490 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001491}
1492
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001493struct caggr_data {
1494 double avg, avg_enabled, avg_running;
1495};
1496
1497static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1498 bool first __maybe_unused)
1499{
1500 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001501 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001502
1503 cd->avg += avg_stats(&ps->res_stats[0]);
1504 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1505 cd->avg_running += avg_stats(&ps->res_stats[2]);
1506}
1507
Ingo Molnar42202dd2009-06-13 14:57:28 +02001508/*
1509 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001510 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001511 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001512static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001513{
Jiri Olsa58215222015-07-21 14:31:24 +02001514 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001515 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001516 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001517
Andi Kleen430daf22017-03-20 13:17:00 -07001518 if (!collect_data(counter, counter_aggr_cb, &cd))
1519 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001520
Andi Kleen54b50912016-03-03 15:57:36 -08001521 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001522 fprintf(output, "%s", prefix);
1523
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001524 uval = cd.avg * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001525 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
1526 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001527 if (!metric_only)
1528 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001529}
1530
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001531static void counter_cb(struct perf_evsel *counter, void *data,
1532 bool first __maybe_unused)
1533{
1534 struct aggr_data *ad = data;
1535
1536 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1537 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1538 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1539}
1540
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001541/*
1542 * Print out the results of a single counter:
1543 * does not use aggregated count in system-wide
1544 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001545static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001546{
Jiri Olsa58215222015-07-21 14:31:24 +02001547 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001548 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001549 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001550 int cpu;
1551
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001552 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001553 struct aggr_data ad = { .cpu = cpu };
1554
Andi Kleen430daf22017-03-20 13:17:00 -07001555 if (!collect_data(counter, counter_cb, &ad))
1556 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001557 val = ad.val;
1558 ena = ad.ena;
1559 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001560
1561 if (prefix)
1562 fprintf(output, "%s", prefix);
1563
Stephane Eranian410136f2013-11-12 17:58:49 +01001564 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001565 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1566 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001567
Stephane Eranian4aa90152011-08-15 22:22:33 +02001568 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001569 }
1570}
1571
Andi Kleen206cab62016-03-03 15:57:37 -08001572static void print_no_aggr_metric(char *prefix)
1573{
1574 int cpu;
1575 int nrcpus = 0;
1576 struct perf_evsel *counter;
1577 u64 ena, run, val;
1578 double uval;
1579
1580 nrcpus = evsel_list->cpus->nr;
1581 for (cpu = 0; cpu < nrcpus; cpu++) {
1582 bool first = true;
1583
1584 if (prefix)
1585 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001586 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001587 if (is_duration_time(counter))
1588 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001589 if (first) {
1590 aggr_printout(counter, cpu, 0);
1591 first = false;
1592 }
1593 val = perf_counts(counter->counts, cpu, 0)->val;
1594 ena = perf_counts(counter->counts, cpu, 0)->ena;
1595 run = perf_counts(counter->counts, cpu, 0)->run;
1596
1597 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001598 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1599 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001600 }
1601 fputc('\n', stat_config.output);
1602 }
1603}
1604
Andi Kleen54b50912016-03-03 15:57:36 -08001605static int aggr_header_lens[] = {
1606 [AGGR_CORE] = 18,
1607 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001608 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001609 [AGGR_THREAD] = 24,
1610 [AGGR_GLOBAL] = 0,
1611};
1612
Andi Kleenc51fd632016-05-24 12:52:39 -07001613static const char *aggr_header_csv[] = {
1614 [AGGR_CORE] = "core,cpus,",
1615 [AGGR_SOCKET] = "socket,cpus",
1616 [AGGR_NONE] = "cpu,",
1617 [AGGR_THREAD] = "comm-pid,",
1618 [AGGR_GLOBAL] = ""
1619};
1620
Andi Kleen41c8ca22016-05-24 12:52:38 -07001621static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001622{
1623 struct perf_stat_output_ctx out;
1624 struct perf_evsel *counter;
1625 struct outstate os = {
1626 .fh = stat_config.output
1627 };
1628
1629 if (prefix)
1630 fprintf(stat_config.output, "%s", prefix);
1631
Andi Kleen41c8ca22016-05-24 12:52:38 -07001632 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001633 fprintf(stat_config.output, "%*s",
1634 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001635 if (csv_output) {
1636 if (stat_config.interval)
1637 fputs("time,", stat_config.output);
1638 fputs(aggr_header_csv[stat_config.aggr_mode],
1639 stat_config.output);
1640 }
Andi Kleen54b50912016-03-03 15:57:36 -08001641
1642 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001643 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001644 if (is_duration_time(counter))
1645 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001646 os.evsel = counter;
1647 out.ctx = &os;
1648 out.print_metric = print_metric_header;
1649 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001650 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001651 os.evsel = counter;
1652 perf_stat__print_shadow_stats(counter, 0,
1653 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001654 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001655 &metric_events,
1656 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001657 }
1658 fputc('\n', stat_config.output);
1659}
1660
Jiri Olsad4f63a42015-06-26 11:29:26 +02001661static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001662{
Jiri Olsa58215222015-07-21 14:31:24 +02001663 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001664 static int num_print_interval;
1665
1666 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1667
Andi Kleen41c8ca22016-05-24 12:52:38 -07001668 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001669 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001670 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001671 fprintf(output, "# time socket cpus");
1672 if (!metric_only)
1673 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001674 break;
1675 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001676 fprintf(output, "# time core cpus");
1677 if (!metric_only)
1678 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001679 break;
1680 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001681 fprintf(output, "# time CPU");
1682 if (!metric_only)
1683 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001684 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001685 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001686 fprintf(output, "# time comm-pid");
1687 if (!metric_only)
1688 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001689 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001690 case AGGR_GLOBAL:
1691 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001692 fprintf(output, "# time");
1693 if (!metric_only)
1694 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001695 case AGGR_UNSET:
1696 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001697 }
1698 }
1699
Andi Kleen41c8ca22016-05-24 12:52:38 -07001700 if (num_print_interval == 0 && metric_only)
1701 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001702 if (++num_print_interval == 25)
1703 num_print_interval = 0;
1704}
1705
1706static void print_header(int argc, const char **argv)
1707{
Jiri Olsa58215222015-07-21 14:31:24 +02001708 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001709 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001710
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001711 fflush(stdout);
1712
Stephane Eraniand7470b62010-12-01 18:49:05 +02001713 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001714 fprintf(output, "\n");
1715 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001716 if (target.system_wide)
1717 fprintf(output, "\'system wide");
1718 else if (target.cpu_list)
1719 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001720 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001721 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1722 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001723 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001724 } else if (target.pid)
1725 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001726 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001727 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001728
Stephane Eranian4aa90152011-08-15 22:22:33 +02001729 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001730 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001731 fprintf(output, " (%d runs)", run_count);
1732 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001733 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001734}
1735
1736static void print_footer(void)
1737{
Jiri Olsa58215222015-07-21 14:31:24 +02001738 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001739 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001740
Jiri Olsad4f63a42015-06-26 11:29:26 +02001741 if (!null_run)
1742 fprintf(output, "\n");
1743 fprintf(output, " %17.9f seconds time elapsed",
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001744 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001745 if (run_count > 1) {
1746 fprintf(output, " ");
1747 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1748 avg_stats(&walltime_nsecs_stats));
1749 }
1750 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001751
Andi Kleen918c7b062017-05-22 18:00:16 -07001752 if (print_free_counters_hint &&
1753 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1754 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001755 fprintf(output,
1756"Some events weren't counted. Try disabling the NMI watchdog:\n"
1757" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1758" perf stat ...\n"
1759" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001760}
1761
1762static void print_counters(struct timespec *ts, int argc, const char **argv)
1763{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001764 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001765 struct perf_evsel *counter;
1766 char buf[64], *prefix = NULL;
1767
Jiri Olsa664c98d2015-11-05 15:40:50 +01001768 /* Do not print anything if we record to the pipe. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001769 if (STAT_RECORD && perf_stat.data.is_pipe)
Jiri Olsa664c98d2015-11-05 15:40:50 +01001770 return;
1771
Jiri Olsad4f63a42015-06-26 11:29:26 +02001772 if (interval)
1773 print_interval(prefix = buf, ts);
1774 else
1775 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001776
Andi Kleen54b50912016-03-03 15:57:36 -08001777 if (metric_only) {
1778 static int num_print_iv;
1779
Andi Kleen41c8ca22016-05-24 12:52:38 -07001780 if (num_print_iv == 0 && !interval)
1781 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001782 if (num_print_iv++ == 25)
1783 num_print_iv = 0;
1784 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1785 fprintf(stat_config.output, "%s", prefix);
1786 }
1787
Jiri Olsa421a50f2015-07-21 14:31:22 +02001788 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001789 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001790 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001791 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001792 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001793 case AGGR_THREAD:
Andi Kleene864c5c2017-08-31 12:40:35 -07001794 evlist__for_each_entry(evsel_list, counter) {
1795 if (is_duration_time(counter))
1796 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001797 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001798 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001799 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001800 case AGGR_GLOBAL:
Andi Kleene864c5c2017-08-31 12:40:35 -07001801 evlist__for_each_entry(evsel_list, counter) {
1802 if (is_duration_time(counter))
1803 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001804 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001805 }
Andi Kleen54b50912016-03-03 15:57:36 -08001806 if (metric_only)
1807 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001808 break;
1809 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001810 if (metric_only)
1811 print_no_aggr_metric(prefix);
1812 else {
Andi Kleene864c5c2017-08-31 12:40:35 -07001813 evlist__for_each_entry(evsel_list, counter) {
1814 if (is_duration_time(counter))
1815 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001816 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001817 }
Andi Kleen206cab62016-03-03 15:57:37 -08001818 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001819 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001820 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001821 default:
1822 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001823 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001824
Jiri Olsad4f63a42015-06-26 11:29:26 +02001825 if (!interval && !csv_output)
1826 print_footer();
1827
Jiri Olsa58215222015-07-21 14:31:24 +02001828 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001829}
1830
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001831static volatile int signr = -1;
1832
Ingo Molnar52425192009-05-26 09:17:18 +02001833static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001834{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001835 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001836 done = 1;
1837
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001838 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001839 /*
1840 * render child_pid harmless
1841 * won't send SIGTERM to a random
1842 * process in case of race condition
1843 * and fast PID recycling
1844 */
1845 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001846}
1847
1848static void sig_atexit(void)
1849{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001850 sigset_t set, oset;
1851
1852 /*
1853 * avoid race condition with SIGCHLD handler
1854 * in skip_signal() which is modifying child_pid
1855 * goal is to avoid send SIGTERM to a random
1856 * process
1857 */
1858 sigemptyset(&set);
1859 sigaddset(&set, SIGCHLD);
1860 sigprocmask(SIG_BLOCK, &set, &oset);
1861
Chris Wilson933da832009-10-04 01:35:01 +01001862 if (child_pid != -1)
1863 kill(child_pid, SIGTERM);
1864
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001865 sigprocmask(SIG_SETMASK, &oset, NULL);
1866
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001867 if (signr == -1)
1868 return;
1869
1870 signal(signr, SIG_DFL);
1871 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001872}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001873
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001874static int stat__set_big_num(const struct option *opt __maybe_unused,
1875 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001876{
1877 big_num_opt = unset ? 0 : 1;
1878 return 0;
1879}
1880
Andi Kleen44b1e602016-05-30 12:49:42 -03001881static int enable_metric_only(const struct option *opt __maybe_unused,
1882 const char *s __maybe_unused, int unset)
1883{
1884 force_metric_only = true;
1885 metric_only = !unset;
1886 return 0;
1887}
1888
Andi Kleenb18f3e32017-08-31 12:40:31 -07001889static int parse_metric_groups(const struct option *opt,
1890 const char *str,
1891 int unset __maybe_unused)
1892{
1893 return metricgroup__parse_groups(opt, str, &metric_events);
1894}
1895
Jiri Olsae0547312015-11-05 15:40:45 +01001896static const struct option stat_options[] = {
1897 OPT_BOOLEAN('T', "transaction", &transaction_run,
1898 "hardware transaction statistics"),
1899 OPT_CALLBACK('e', "event", &evsel_list, "event",
1900 "event selector. use 'perf list' to list available events",
1901 parse_events_option),
1902 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1903 "event filter", parse_filter),
1904 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1905 "child tasks do not inherit counters"),
1906 OPT_STRING('p', "pid", &target.pid, "pid",
1907 "stat events on existing process id"),
1908 OPT_STRING('t', "tid", &target.tid, "tid",
1909 "stat events on existing thread id"),
1910 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1911 "system-wide collection from all CPUs"),
1912 OPT_BOOLEAN('g', "group", &group,
1913 "put the counters into a counter group"),
1914 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1915 OPT_INCR('v', "verbose", &verbose,
1916 "be more verbose (show counter open errors, etc)"),
1917 OPT_INTEGER('r', "repeat", &run_count,
1918 "repeat command and print average + stddev (max: 100, forever: 0)"),
1919 OPT_BOOLEAN('n', "null", &null_run,
1920 "null run - dont start any counters"),
1921 OPT_INCR('d', "detailed", &detailed_run,
1922 "detailed run - start a lot of events"),
1923 OPT_BOOLEAN('S', "sync", &sync_run,
1924 "call sync() before starting a run"),
1925 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1926 "print large numbers with thousands\' separators",
1927 stat__set_big_num),
1928 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1929 "list of cpus to monitor in system-wide"),
1930 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1931 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001932 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001933 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1934 "print counts with custom separator"),
1935 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1936 "monitor event in cgroup name only", parse_cgroups),
1937 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1938 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1939 OPT_INTEGER(0, "log-fd", &output_fd,
1940 "log output to fd, instead of stderr"),
1941 OPT_STRING(0, "pre", &pre_cmd, "command",
1942 "command to run prior to the measured command"),
1943 OPT_STRING(0, "post", &post_cmd, "command",
1944 "command to run after to the measured command"),
1945 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03001946 "print counts at regular interval in ms "
1947 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01001948 OPT_INTEGER(0, "interval-count", &stat_config.times,
1949 "print counts for fixed number of times"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01001950 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1951 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001952 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1953 "aggregate counts per processor socket", AGGR_SOCKET),
1954 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1955 "aggregate counts per physical processor core", AGGR_CORE),
1956 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1957 "aggregate counts per thread", AGGR_THREAD),
1958 OPT_UINTEGER('D', "delay", &initial_delay,
1959 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001960 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1961 "Only print computed metrics. No raw values", enable_metric_only),
1962 OPT_BOOLEAN(0, "topdown", &topdown_run,
1963 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001964 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1965 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001966 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1967 "monitor specified metrics or metric groups (separated by ,)",
1968 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01001969 OPT_END()
1970};
1971
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001972static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1973{
1974 return cpu_map__get_socket(map, cpu, NULL);
1975}
1976
1977static int perf_stat__get_core(struct cpu_map *map, int cpu)
1978{
1979 return cpu_map__get_core(map, cpu, NULL);
1980}
1981
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001982static int cpu_map__get_max(struct cpu_map *map)
1983{
1984 int i, max = -1;
1985
1986 for (i = 0; i < map->nr; i++) {
1987 if (map->map[i] > max)
1988 max = map->map[i];
1989 }
1990
1991 return max;
1992}
1993
1994static struct cpu_map *cpus_aggr_map;
1995
1996static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1997{
1998 int cpu;
1999
2000 if (idx >= map->nr)
2001 return -1;
2002
2003 cpu = map->map[idx];
2004
2005 if (cpus_aggr_map->map[cpu] == -1)
2006 cpus_aggr_map->map[cpu] = get_id(map, idx);
2007
2008 return cpus_aggr_map->map[cpu];
2009}
2010
2011static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
2012{
2013 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
2014}
2015
2016static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
2017{
2018 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
2019}
2020
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002021static int perf_stat_init_aggr_mode(void)
2022{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002023 int nr;
2024
Jiri Olsa421a50f2015-07-21 14:31:22 +02002025 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002026 case AGGR_SOCKET:
2027 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
2028 perror("cannot build socket map");
2029 return -1;
2030 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002031 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002032 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002033 case AGGR_CORE:
2034 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
2035 perror("cannot build core map");
2036 return -1;
2037 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002038 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002039 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002040 case AGGR_NONE:
2041 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002042 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002043 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002044 default:
2045 break;
2046 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002047
2048 /*
2049 * The evsel_list->cpus is the base we operate on,
2050 * taking the highest cpu number to be the size of
2051 * the aggregation translate cpumap.
2052 */
2053 nr = cpu_map__get_max(evsel_list->cpus);
2054 cpus_aggr_map = cpu_map__empty_new(nr + 1);
2055 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002056}
2057
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002058static void perf_stat__exit_aggr_mode(void)
2059{
2060 cpu_map__put(aggr_map);
2061 cpu_map__put(cpus_aggr_map);
2062 aggr_map = NULL;
2063 cpus_aggr_map = NULL;
2064}
2065
Jiri Olsa68d702f2015-11-05 15:40:58 +01002066static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2067{
2068 int cpu;
2069
2070 if (idx > map->nr)
2071 return -1;
2072
2073 cpu = map->map[idx];
2074
Jan Stancekda8a58b2017-02-17 12:10:26 +01002075 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002076 return -1;
2077
2078 return cpu;
2079}
2080
2081static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2082{
2083 struct perf_env *env = data;
2084 int cpu = perf_env__get_cpu(env, map, idx);
2085
2086 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2087}
2088
2089static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2090{
2091 struct perf_env *env = data;
2092 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2093
2094 if (cpu != -1) {
2095 int socket_id = env->cpu[cpu].socket_id;
2096
2097 /*
2098 * Encode socket in upper 16 bits
2099 * core_id is relative to socket, and
2100 * we need a global id. So we combine
2101 * socket + core id.
2102 */
2103 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2104 }
2105
2106 return core;
2107}
2108
2109static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2110 struct cpu_map **sockp)
2111{
2112 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2113}
2114
2115static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2116 struct cpu_map **corep)
2117{
2118 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2119}
2120
2121static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2122{
2123 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2124}
2125
2126static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2127{
2128 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2129}
2130
2131static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2132{
2133 struct perf_env *env = &st->session->header.env;
2134
2135 switch (stat_config.aggr_mode) {
2136 case AGGR_SOCKET:
2137 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2138 perror("cannot build socket map");
2139 return -1;
2140 }
2141 aggr_get_id = perf_stat__get_socket_file;
2142 break;
2143 case AGGR_CORE:
2144 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2145 perror("cannot build core map");
2146 return -1;
2147 }
2148 aggr_get_id = perf_stat__get_core_file;
2149 break;
2150 case AGGR_NONE:
2151 case AGGR_GLOBAL:
2152 case AGGR_THREAD:
2153 case AGGR_UNSET:
2154 default:
2155 break;
2156 }
2157
2158 return 0;
2159}
2160
Andi Kleen44b1e602016-05-30 12:49:42 -03002161static int topdown_filter_events(const char **attr, char **str, bool use_group)
2162{
2163 int off = 0;
2164 int i;
2165 int len = 0;
2166 char *s;
2167
2168 for (i = 0; attr[i]; i++) {
2169 if (pmu_have_event("cpu", attr[i])) {
2170 len += strlen(attr[i]) + 1;
2171 attr[i - off] = attr[i];
2172 } else
2173 off++;
2174 }
2175 attr[i - off] = NULL;
2176
2177 *str = malloc(len + 1 + 2);
2178 if (!*str)
2179 return -1;
2180 s = *str;
2181 if (i - off == 0) {
2182 *s = 0;
2183 return 0;
2184 }
2185 if (use_group)
2186 *s++ = '{';
2187 for (i = 0; attr[i]; i++) {
2188 strcpy(s, attr[i]);
2189 s += strlen(s);
2190 *s++ = ',';
2191 }
2192 if (use_group) {
2193 s[-1] = '}';
2194 *s = 0;
2195 } else
2196 s[-1] = 0;
2197 return 0;
2198}
2199
2200__weak bool arch_topdown_check_group(bool *warn)
2201{
2202 *warn = false;
2203 return false;
2204}
2205
2206__weak void arch_topdown_group_warn(void)
2207{
2208}
2209
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002210/*
2211 * Add default attributes, if there were no attributes specified or
2212 * if -d/--detailed, -d -d or -d -d -d is used:
2213 */
2214static int add_default_attributes(void)
2215{
Andi Kleen44b1e602016-05-30 12:49:42 -03002216 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002217 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002218
2219 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2220 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2221 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2222 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2223
2224 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002225};
2226 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002227 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002228};
2229 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002230 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002231};
2232 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002233 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2234 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2235 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2236
2237};
2238
2239/*
2240 * Detailed stats (-d), covering the L1 and last level data caches:
2241 */
2242 struct perf_event_attr detailed_attrs[] = {
2243
2244 { .type = PERF_TYPE_HW_CACHE,
2245 .config =
2246 PERF_COUNT_HW_CACHE_L1D << 0 |
2247 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2248 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2249
2250 { .type = PERF_TYPE_HW_CACHE,
2251 .config =
2252 PERF_COUNT_HW_CACHE_L1D << 0 |
2253 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2254 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2255
2256 { .type = PERF_TYPE_HW_CACHE,
2257 .config =
2258 PERF_COUNT_HW_CACHE_LL << 0 |
2259 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2260 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2261
2262 { .type = PERF_TYPE_HW_CACHE,
2263 .config =
2264 PERF_COUNT_HW_CACHE_LL << 0 |
2265 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2266 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2267};
2268
2269/*
2270 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2271 */
2272 struct perf_event_attr very_detailed_attrs[] = {
2273
2274 { .type = PERF_TYPE_HW_CACHE,
2275 .config =
2276 PERF_COUNT_HW_CACHE_L1I << 0 |
2277 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2278 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2279
2280 { .type = PERF_TYPE_HW_CACHE,
2281 .config =
2282 PERF_COUNT_HW_CACHE_L1I << 0 |
2283 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2284 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2285
2286 { .type = PERF_TYPE_HW_CACHE,
2287 .config =
2288 PERF_COUNT_HW_CACHE_DTLB << 0 |
2289 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2290 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2291
2292 { .type = PERF_TYPE_HW_CACHE,
2293 .config =
2294 PERF_COUNT_HW_CACHE_DTLB << 0 |
2295 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2296 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2297
2298 { .type = PERF_TYPE_HW_CACHE,
2299 .config =
2300 PERF_COUNT_HW_CACHE_ITLB << 0 |
2301 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2302 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2303
2304 { .type = PERF_TYPE_HW_CACHE,
2305 .config =
2306 PERF_COUNT_HW_CACHE_ITLB << 0 |
2307 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2308 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2309
2310};
2311
2312/*
2313 * Very, very detailed stats (-d -d -d), adding prefetch events:
2314 */
2315 struct perf_event_attr very_very_detailed_attrs[] = {
2316
2317 { .type = PERF_TYPE_HW_CACHE,
2318 .config =
2319 PERF_COUNT_HW_CACHE_L1D << 0 |
2320 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2321 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2322
2323 { .type = PERF_TYPE_HW_CACHE,
2324 .config =
2325 PERF_COUNT_HW_CACHE_L1D << 0 |
2326 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2327 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2328};
2329
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002330 /* Set attrs if no event is selected and !null_run: */
2331 if (null_run)
2332 return 0;
2333
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002334 if (transaction_run) {
Thomas Richterfca323402018-03-08 15:57:35 +01002335 struct parse_events_error errinfo;
2336
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002337 if (pmu_have_event("cpu", "cycles-ct") &&
2338 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002339 err = parse_events(evsel_list, transaction_attrs,
2340 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002341 else
Thomas Richterfca323402018-03-08 15:57:35 +01002342 err = parse_events(evsel_list,
2343 transaction_limited_attrs,
2344 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002345 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002346 fprintf(stderr, "Cannot set up transaction events\n");
2347 return -1;
2348 }
2349 return 0;
2350 }
2351
Kan Liangdaefd0b2017-05-26 12:05:38 -07002352 if (smi_cost) {
2353 int smi;
2354
2355 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2356 fprintf(stderr, "freeze_on_smi is not supported.\n");
2357 return -1;
2358 }
2359
2360 if (!smi) {
2361 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2362 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2363 return -1;
2364 }
2365 smi_reset = true;
2366 }
2367
2368 if (pmu_have_event("msr", "aperf") &&
2369 pmu_have_event("msr", "smi")) {
2370 if (!force_metric_only)
2371 metric_only = true;
2372 err = parse_events(evsel_list, smi_cost_attrs, NULL);
2373 } else {
2374 fprintf(stderr, "To measure SMI cost, it needs "
2375 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2376 return -1;
2377 }
2378 if (err) {
2379 fprintf(stderr, "Cannot set up SMI cost events\n");
2380 return -1;
2381 }
2382 return 0;
2383 }
2384
Andi Kleen44b1e602016-05-30 12:49:42 -03002385 if (topdown_run) {
2386 char *str = NULL;
2387 bool warn = false;
2388
2389 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2390 stat_config.aggr_mode != AGGR_CORE) {
2391 pr_err("top down event configuration requires --per-core mode\n");
2392 return -1;
2393 }
2394 stat_config.aggr_mode = AGGR_CORE;
2395 if (nr_cgroups || !target__has_cpu(&target)) {
2396 pr_err("top down event configuration requires system-wide mode (-a)\n");
2397 return -1;
2398 }
2399
2400 if (!force_metric_only)
2401 metric_only = true;
2402 if (topdown_filter_events(topdown_attrs, &str,
2403 arch_topdown_check_group(&warn)) < 0) {
2404 pr_err("Out of memory\n");
2405 return -1;
2406 }
2407 if (topdown_attrs[0] && str) {
2408 if (warn)
2409 arch_topdown_group_warn();
2410 err = parse_events(evsel_list, str, NULL);
2411 if (err) {
2412 fprintf(stderr,
2413 "Cannot set up top down events %s: %d\n",
2414 str, err);
2415 free(str);
2416 return -1;
2417 }
2418 } else {
2419 fprintf(stderr, "System does not support topdown\n");
2420 return -1;
2421 }
2422 free(str);
2423 }
2424
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002425 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002426 if (target__has_cpu(&target))
2427 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2428
Andi Kleen9dec4472016-02-26 16:27:56 -08002429 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2430 return -1;
2431 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2432 if (perf_evlist__add_default_attrs(evsel_list,
2433 frontend_attrs) < 0)
2434 return -1;
2435 }
2436 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2437 if (perf_evlist__add_default_attrs(evsel_list,
2438 backend_attrs) < 0)
2439 return -1;
2440 }
2441 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002442 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002443 }
2444
2445 /* Detailed events get appended to the event list: */
2446
2447 if (detailed_run < 1)
2448 return 0;
2449
2450 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002451 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002452 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002453
2454 if (detailed_run < 2)
2455 return 0;
2456
2457 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002458 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002459 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002460
2461 if (detailed_run < 3)
2462 return 0;
2463
2464 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002465 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002466}
2467
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002468static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002469 "perf stat record [<options>]",
2470 NULL,
2471};
2472
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002473static void init_features(struct perf_session *session)
2474{
2475 int feat;
2476
2477 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2478 perf_header__set_feat(&session->header, feat);
2479
2480 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2481 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2482 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2483 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2484}
2485
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002486static int __cmd_record(int argc, const char **argv)
2487{
2488 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002489 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002490
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002491 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002492 PARSE_OPT_STOP_AT_NON_OPTION);
2493
2494 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002495 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002496
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002497 if (run_count != 1 || forever) {
2498 pr_err("Cannot use -r option with perf stat record.\n");
2499 return -1;
2500 }
2501
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002502 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002503 if (session == NULL) {
2504 pr_err("Perf session creation failed.\n");
2505 return -1;
2506 }
2507
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002508 init_features(session);
2509
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002510 session->evlist = evsel_list;
2511 perf_stat.session = session;
2512 perf_stat.record = true;
2513 return argc;
2514}
2515
Jiri Olsaa56f9392015-11-05 15:40:59 +01002516static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2517 union perf_event *event,
2518 struct perf_session *session)
2519{
Andi Kleene3b03b62016-05-05 16:04:03 -07002520 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002521 struct perf_evsel *counter;
2522 struct timespec tsh, *ts = NULL;
2523 const char **argv = session->header.env.cmdline_argv;
2524 int argc = session->header.env.nr_cmdline;
2525
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002526 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002527 perf_stat_process_counter(&stat_config, counter);
2528
Andi Kleene3b03b62016-05-05 16:04:03 -07002529 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2530 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002531
Andi Kleene3b03b62016-05-05 16:04:03 -07002532 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002533 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2534 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002535 ts = &tsh;
2536 }
2537
2538 print_counters(ts, argc, argv);
2539 return 0;
2540}
2541
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002542static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002543int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002544 union perf_event *event,
2545 struct perf_session *session __maybe_unused)
2546{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002547 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2548
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002549 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002550
Jiri Olsa89af4e02015-11-05 15:41:02 +01002551 if (cpu_map__empty(st->cpus)) {
2552 if (st->aggr_mode != AGGR_UNSET)
2553 pr_warning("warning: processing task data, aggregation mode not set\n");
2554 return 0;
2555 }
2556
2557 if (st->aggr_mode != AGGR_UNSET)
2558 stat_config.aggr_mode = st->aggr_mode;
2559
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002560 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002561 perf_stat_init_aggr_mode();
2562 else
2563 perf_stat_init_aggr_mode_file(st);
2564
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002565 return 0;
2566}
2567
Jiri Olsa1975d362015-11-05 15:40:56 +01002568static int set_maps(struct perf_stat *st)
2569{
2570 if (!st->cpus || !st->threads)
2571 return 0;
2572
2573 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2574 return -EINVAL;
2575
2576 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2577
2578 if (perf_evlist__alloc_stats(evsel_list, true))
2579 return -ENOMEM;
2580
2581 st->maps_allocated = true;
2582 return 0;
2583}
2584
2585static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002586int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002587 union perf_event *event,
2588 struct perf_session *session __maybe_unused)
2589{
2590 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2591
2592 if (st->threads) {
2593 pr_warning("Extra thread map event, ignoring.\n");
2594 return 0;
2595 }
2596
2597 st->threads = thread_map__new_event(&event->thread_map);
2598 if (!st->threads)
2599 return -ENOMEM;
2600
2601 return set_maps(st);
2602}
2603
2604static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002605int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002606 union perf_event *event,
2607 struct perf_session *session __maybe_unused)
2608{
2609 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2610 struct cpu_map *cpus;
2611
2612 if (st->cpus) {
2613 pr_warning("Extra cpu map event, ignoring.\n");
2614 return 0;
2615 }
2616
2617 cpus = cpu_map__new_data(&event->cpu_map.data);
2618 if (!cpus)
2619 return -ENOMEM;
2620
2621 st->cpus = cpus;
2622 return set_maps(st);
2623}
2624
Jin Yao56739442017-12-05 22:03:07 +08002625static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2626{
2627 int i;
2628
2629 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2630 if (!config->stats)
2631 return -1;
2632
2633 config->stats_num = nthreads;
2634
2635 for (i = 0; i < nthreads; i++)
2636 runtime_stat__init(&config->stats[i]);
2637
2638 return 0;
2639}
2640
2641static void runtime_stat_delete(struct perf_stat_config *config)
2642{
2643 int i;
2644
2645 if (!config->stats)
2646 return;
2647
2648 for (i = 0; i < config->stats_num; i++)
2649 runtime_stat__exit(&config->stats[i]);
2650
2651 free(config->stats);
2652}
2653
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002654static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002655 "perf stat report [<options>]",
2656 NULL,
2657};
2658
2659static struct perf_stat perf_stat = {
2660 .tool = {
2661 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002662 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002663 .thread_map = process_thread_map_event,
2664 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002665 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002666 .stat = perf_event__process_stat_event,
2667 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002668 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002669 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002670};
2671
2672static int __cmd_report(int argc, const char **argv)
2673{
2674 struct perf_session *session;
2675 const struct option options[] = {
2676 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002677 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2678 "aggregate counts per processor socket", AGGR_SOCKET),
2679 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2680 "aggregate counts per physical processor core", AGGR_CORE),
2681 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2682 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002683 OPT_END()
2684 };
2685 struct stat st;
2686 int ret;
2687
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002688 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002689
2690 if (!input_name || !strlen(input_name)) {
2691 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2692 input_name = "-";
2693 else
2694 input_name = "perf.data";
2695 }
2696
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002697 perf_stat.data.file.path = input_name;
2698 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002699
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002700 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002701 if (session == NULL)
2702 return -1;
2703
2704 perf_stat.session = session;
2705 stat_config.output = stderr;
2706 evsel_list = session->evlist;
2707
2708 ret = perf_session__process_events(session);
2709 if (ret)
2710 return ret;
2711
2712 perf_session__delete(session);
2713 return 0;
2714}
2715
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002716static void setup_system_wide(int forks)
2717{
2718 /*
2719 * Make system wide (-a) the default target if
2720 * no target was specified and one of following
2721 * conditions is met:
2722 *
2723 * - there's no workload specified
2724 * - there is workload specified but all requested
2725 * events are system wide events
2726 */
2727 if (!target__none(&target))
2728 return;
2729
2730 if (!forks)
2731 target.system_wide = true;
2732 else {
2733 struct perf_evsel *counter;
2734
2735 evlist__for_each_entry(evsel_list, counter) {
2736 if (!counter->system_wide)
2737 return;
2738 }
2739
2740 if (evsel_list->nr_entries)
2741 target.system_wide = true;
2742 }
2743}
2744
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002745int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002746{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002747 const char * const stat_usage[] = {
2748 "perf stat [<options>] [<command>]",
2749 NULL
2750 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002751 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002752 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002753 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002754 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002755 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002756
Stephane Eranian5af52b52010-05-18 15:00:01 +02002757 setlocale(LC_ALL, "");
2758
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002759 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002760 if (evsel_list == NULL)
2761 return -ENOMEM;
2762
Wang Nan1669e502016-02-19 11:43:58 +00002763 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002764 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2765 (const char **) stat_usage,
2766 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002767 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002768 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002769
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002770 if (csv_sep) {
2771 csv_output = true;
2772 if (!strcmp(csv_sep, "\\t"))
2773 csv_sep = "\t";
2774 } else
2775 csv_sep = DEFAULT_SEPARATOR;
2776
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002777 if (argc && !strncmp(argv[0], "rec", 3)) {
2778 argc = __cmd_record(argc, argv);
2779 if (argc < 0)
2780 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002781 } else if (argc && !strncmp(argv[0], "rep", 3))
2782 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002783
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002784 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002785 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002786
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002787 /*
2788 * For record command the -o is already taken care of.
2789 */
2790 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002791 output = NULL;
2792
Jim Cromie56f3bae2011-09-07 17:14:00 -06002793 if (output_name && output_fd) {
2794 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002795 parse_options_usage(stat_usage, stat_options, "o", 1);
2796 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002797 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002798 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002799
Andi Kleen54b50912016-03-03 15:57:36 -08002800 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2801 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2802 goto out;
2803 }
2804
Andi Kleen54b50912016-03-03 15:57:36 -08002805 if (metric_only && run_count > 1) {
2806 fprintf(stderr, "--metric-only is not supported with -r\n");
2807 goto out;
2808 }
2809
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002810 if (output_fd < 0) {
2811 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002812 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002813 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002814 }
2815
Stephane Eranian4aa90152011-08-15 22:22:33 +02002816 if (!output) {
2817 struct timespec tm;
2818 mode = append_file ? "a" : "w";
2819
2820 output = fopen(output_name, mode);
2821 if (!output) {
2822 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002823 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002824 }
2825 clock_gettime(CLOCK_REALTIME, &tm);
2826 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002827 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002828 mode = append_file ? "a" : "w";
2829 output = fdopen(output_fd, mode);
2830 if (!output) {
2831 perror("Failed opening logfd");
2832 return -errno;
2833 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002834 }
2835
Jiri Olsa58215222015-07-21 14:31:24 +02002836 stat_config.output = output;
2837
Stephane Eraniand7470b62010-12-01 18:49:05 +02002838 /*
2839 * let the spreadsheet do the pretty-printing
2840 */
2841 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002842 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002843 if (big_num_opt == 1) {
2844 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002845 parse_options_usage(stat_usage, stat_options, "B", 1);
2846 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002847 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002848 } else /* Nope, so disable big number formatting */
2849 big_num = false;
2850 } else if (big_num_opt == 0) /* User passed --no-big-num */
2851 big_num = false;
2852
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002853 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002854
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002855 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002856 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002857 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002858 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002859 } else if (run_count == 0) {
2860 forever = true;
2861 run_count = 1;
2862 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002863
Jin Yao1d9f8d12017-12-05 22:03:10 +08002864 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2865 !target__has_task(&target)) {
2866 if (!target.system_wide || target.cpu_list) {
2867 fprintf(stderr, "The --per-thread option is only "
2868 "available when monitoring via -p -t -a "
2869 "options or only --per-thread.\n");
2870 parse_options_usage(NULL, stat_options, "p", 1);
2871 parse_options_usage(NULL, stat_options, "t", 1);
2872 goto out;
2873 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002874 }
2875
2876 /*
2877 * no_aggr, cgroup are for system-wide only
2878 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2879 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002880 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2881 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002882 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002883 fprintf(stderr, "both cgroup and no-aggregation "
2884 "modes only available in system-wide mode\n");
2885
Jiri Olsae0547312015-11-05 15:40:45 +01002886 parse_options_usage(stat_usage, stat_options, "G", 1);
2887 parse_options_usage(NULL, stat_options, "A", 1);
2888 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002889 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002890 }
2891
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002892 if (add_default_attributes())
2893 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002894
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002895 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002896
Jin Yao1d9f8d12017-12-05 22:03:10 +08002897 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2898 target.per_thread = true;
2899
Namhyung Kim77a6f012012-05-07 14:09:04 +09002900 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002901 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002902 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002903 parse_options_usage(stat_usage, stat_options, "p", 1);
2904 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002905 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002906 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002907 parse_options_usage(stat_usage, stat_options, "C", 1);
2908 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002909 }
2910 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002911 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002912
2913 /*
2914 * Initialize thread_map with comm names,
2915 * so we could print it out on output.
2916 */
Jin Yao56739442017-12-05 22:03:07 +08002917 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002918 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08002919 if (target.system_wide) {
2920 if (runtime_stat_new(&stat_config,
2921 thread_map__nr(evsel_list->threads))) {
2922 goto out;
2923 }
2924 }
2925 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002926
yuzhoujiandb06a262018-01-29 10:25:22 +01002927 if (stat_config.times && interval)
2928 interval_count = true;
2929 else if (stat_config.times && !interval) {
2930 pr_err("interval-count option should be used together with "
2931 "interval-print.\n");
2932 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2933 parse_options_usage(stat_usage, stat_options, "I", 1);
2934 goto out;
2935 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002936
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002937 if (timeout && timeout < 100) {
2938 if (timeout < 10) {
2939 pr_err("timeout must be >= 10ms.\n");
2940 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2941 goto out;
2942 } else
2943 pr_warning("timeout < 100ms. "
2944 "The overhead percentage could be high in some cases. "
2945 "Please proceed with caution.\n");
2946 }
2947 if (timeout && interval) {
2948 pr_err("timeout option is not supported with interval-print.\n");
2949 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2950 parse_options_usage(stat_usage, stat_options, "I", 1);
2951 goto out;
2952 }
2953
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002954 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002955 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002956
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002957 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002958 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002959
Ingo Molnar58d7e992009-05-15 11:03:23 +02002960 /*
2961 * We dont want to block the signals - that would cause
2962 * child tasks to inherit that and Ctrl-C would not work.
2963 * What we want is for Ctrl-C to work in the exec()-ed
2964 * task, but being ignored by perf stat itself:
2965 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002966 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002967 if (!forever)
2968 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002969 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002970 signal(SIGALRM, skip_signal);
2971 signal(SIGABRT, skip_signal);
2972
Ingo Molnar42202dd2009-06-13 14:57:28 +02002973 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002974 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002975 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002976 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2977 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002978
Ingo Molnar42202dd2009-06-13 14:57:28 +02002979 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002980 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002981 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002982 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002983 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002984 }
2985
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002986 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002987 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002988
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002989 if (STAT_RECORD) {
2990 /*
2991 * We synthesize the kernel mmap record just so that older tools
2992 * don't emit warnings about not being able to resolve symbols
2993 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2994 * a saner message about no samples being in the perf.data file.
2995 *
2996 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002997 * in header.c at the moment 'perf stat record' gets introduced, which
2998 * is not really needed once we start adding the stat specific PERF_RECORD_
2999 * records, but the need to suppress the kptr_restrict messages in older
3000 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003001 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003002 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003003 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3004 process_synthesized_event,
3005 &perf_stat.session->machines.host);
3006 if (err) {
3007 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3008 "older tools may produce warnings about this file\n.");
3009 }
3010
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003011 if (!interval) {
3012 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3013 pr_err("failed to write stat round event\n");
3014 }
3015
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003016 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003017 perf_stat.session->header.data_size += perf_stat.bytes_written;
3018 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3019 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003020
3021 perf_session__delete(perf_stat.session);
3022 }
3023
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003024 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003025 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003026out:
Kan Liangdaefd0b2017-05-26 12:05:38 -07003027 if (smi_cost && smi_reset)
3028 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3029
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003030 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003031
3032 runtime_stat_delete(&stat_config);
3033
Ingo Molnar42202dd2009-06-13 14:57:28 +02003034 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003035}