blob: 30e6b374e0953fceaf7b7ef7b9191ccf46e6a048 [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;
Kan Liang30060ea2018-04-24 11:20:11 -0700175static int print_mixed_hw_group_error;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200176
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100177struct perf_stat {
178 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100179 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100180 struct perf_session *session;
181 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100182 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100183 bool maps_allocated;
184 struct cpu_map *cpus;
185 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100186 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100187};
188
189static struct perf_stat perf_stat;
190#define STAT_RECORD perf_stat.record
191
Liming Wang60666c62009-12-31 16:05:50 +0800192static volatile int done = 0;
193
Jiri Olsa421a50f2015-07-21 14:31:22 +0200194static struct perf_stat_config stat_config = {
195 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200196 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200197};
198
Andi Kleene864c5c2017-08-31 12:40:35 -0700199static bool is_duration_time(struct perf_evsel *evsel)
200{
201 return !strcmp(evsel->name, "duration_time");
202}
203
Stephane Eranian13370a92013-01-29 12:47:44 +0100204static inline void diff_timespec(struct timespec *r, struct timespec *a,
205 struct timespec *b)
206{
207 r->tv_sec = a->tv_sec - b->tv_sec;
208 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300209 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100210 r->tv_sec--;
211 } else {
212 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
213 }
214}
215
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200216static void perf_stat__reset_stats(void)
217{
Jin Yao56739442017-12-05 22:03:07 +0800218 int i;
219
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200220 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200221 perf_stat__reset_shadow_stats();
Jin Yao56739442017-12-05 22:03:07 +0800222
223 for (i = 0; i < stat_config.stats_num; i++)
224 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200225}
226
Jiri Olsacac21422012-11-12 18:34:00 +0100227static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200228{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200229 struct perf_event_attr *attr = &evsel->attr;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200230 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200231
Jiri Olsa82bf3112017-07-26 14:02:06 +0200232 if (stat_config.scale) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200233 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
234 PERF_FORMAT_TOTAL_TIME_RUNNING;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200235 }
236
237 /*
238 * The event is part of non trivial group, let's enable
239 * the group read (for leader) and ID retrieval for all
240 * members.
241 */
242 if (leader->nr_members > 1)
243 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200244
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200245 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300246
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100247 /*
248 * Some events get initialized with sample_(period/type) set,
249 * like tracepoints. Clear it up for counting.
250 */
251 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100252
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100253 /*
254 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
255 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100256 *
257 * However for pipe sessions we need to keep it zero,
258 * because script's perf_evsel__check_attr is triggered
259 * by attr->sample_type != 0, and we can't run it on
260 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100261 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100262 if (!(STAT_RECORD && perf_stat.data.is_pipe))
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100263 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100264
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100265 /*
266 * Disabling all counters initially, they will be enabled
267 * either manually by us or by kernel via enable_on_exec
268 * set later.
269 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100270 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100271 attr->disabled = 1;
272
Jiri Olsac8280ce2015-12-03 10:06:45 +0100273 /*
274 * In case of initial_delay we enable tracee
275 * events manually.
276 */
277 if (target__none(&target) && !initial_delay)
278 attr->enable_on_exec = 1;
279 }
280
Jin Yao1d9f8d12017-12-05 22:03:10 +0800281 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300282 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200283
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300284 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200285}
286
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200287/*
288 * Does the counter have nsecs as a unit?
289 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200290static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200291{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200292 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
293 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200294 return 1;
295
296 return 0;
297}
298
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100299static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100300 union perf_event *event,
301 struct perf_sample *sample __maybe_unused,
302 struct machine *machine __maybe_unused)
303{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100304 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100305 pr_err("failed to write perf data, error: %m\n");
306 return -1;
307 }
308
309 perf_stat.bytes_written += event->header.size;
310 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100311}
312
Jiri Olsa1975d362015-11-05 15:40:56 +0100313static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100314{
Jiri Olsa1975d362015-11-05 15:40:56 +0100315 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100316 process_synthesized_event,
317 NULL);
318}
319
320#define WRITE_STAT_ROUND_EVENT(time, interval) \
321 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
322
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100323#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
324
325static int
326perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
327 struct perf_counts_values *count)
328{
329 struct perf_sample_id *sid = SID(counter, cpu, thread);
330
331 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
332 process_synthesized_event, NULL);
333}
334
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200335/*
336 * Read out the results of a single counter:
337 * do not aggregate counts across CPUs in system-wide mode
338 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200339static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200340{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100341 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100342 int ncpus, cpu, thread;
343
Jin Yao1d9f8d12017-12-05 22:03:10 +0800344 if (target__has_cpu(&target) && !target__has_per_thread(&target))
Mark Rutland00e727b2016-07-15 11:08:10 +0100345 ncpus = perf_evsel__nr_cpus(counter);
346 else
347 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200348
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000349 if (!counter->supported)
350 return -ENOENT;
351
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100352 if (counter->system_wide)
353 nthreads = 1;
354
355 for (thread = 0; thread < nthreads; thread++) {
356 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200357 struct perf_counts_values *count;
358
359 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200360
361 /*
362 * The leader's group read loads data into its group members
363 * (via perf_evsel__read_counter) and sets threir count->loaded.
364 */
365 if (!count->loaded &&
366 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700367 counter->counts->scaled = -1;
368 perf_counts(counter->counts, cpu, thread)->ena = 0;
369 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100370 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700371 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100372
Jiri Olsa82bf3112017-07-26 14:02:06 +0200373 count->loaded = false;
374
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100375 if (STAT_RECORD) {
376 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
377 pr_err("failed to write stat event\n");
378 return -1;
379 }
380 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700381
382 if (verbose > 1) {
383 fprintf(stat_config.output,
384 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
385 perf_evsel__name(counter),
386 cpu,
387 count->val, count->ena, count->run);
388 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100389 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200390 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200391
392 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200393}
394
Mark Rutland3df33ef2016-08-09 14:04:29 +0100395static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200396{
397 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700398 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200399
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300400 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700401 ret = read_counter(counter);
402 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700403 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200404
Stephane Eraniandb49a712017-04-12 11:23:01 -0700405 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200406 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200407 }
408}
409
Jiri Olsaba411a92015-06-26 11:29:24 +0200410static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100411{
Stephane Eranian13370a92013-01-29 12:47:44 +0100412 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100413
Mark Rutland3df33ef2016-08-09 14:04:29 +0100414 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100415
Stephane Eranian13370a92013-01-29 12:47:44 +0100416 clock_gettime(CLOCK_MONOTONIC, &ts);
417 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100418
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100419 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300420 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100421 pr_err("failed to write stat round event\n");
422 }
423
Andi Kleenb90f1332017-08-31 12:40:36 -0700424 init_stats(&walltime_nsecs_stats);
425 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200426 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100427}
428
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100429static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700430{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100431 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300432 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100433
434 /*
435 * We need to enable counters only if:
436 * - we don't have tracee (attaching to task or cpu)
437 * - we have initial delay configured
438 */
439 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100440 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700441}
442
Mark Rutland3df33ef2016-08-09 14:04:29 +0100443static void disable_counters(void)
444{
445 /*
446 * If we don't have tracee (attaching to task or cpu), counters may
447 * still be running. To get accurate group ratios, we must stop groups
448 * from counting before reading their constituent counters.
449 */
450 if (!target__none(&target))
451 perf_evlist__disable(evsel_list);
452}
453
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300454static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300455
456/*
457 * perf_evlist__prepare_workload will send a SIGUSR1
458 * if the fork fails, since we asked by setting its
459 * want_signal to true.
460 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300461static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
462 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300463{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300464 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300465}
466
Jiri Olsa664c98d2015-11-05 15:40:50 +0100467static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100468{
469 int err;
470
Jiri Olsa664c98d2015-11-05 15:40:50 +0100471 if (is_pipe) {
472 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
473 process_synthesized_event);
474 if (err < 0) {
475 pr_err("Couldn't synthesize attrs.\n");
476 return err;
477 }
478 }
479
Andi Kleenbfd8f722017-11-17 13:42:58 -0800480 err = perf_event__synthesize_extra_attr(NULL,
481 evsel_list,
482 process_synthesized_event,
483 is_pipe);
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100484
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100485 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
486 process_synthesized_event,
487 NULL);
488 if (err < 0) {
489 pr_err("Couldn't synthesize thread map.\n");
490 return err;
491 }
492
493 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
494 process_synthesized_event, NULL);
495 if (err < 0) {
496 pr_err("Couldn't synthesize thread map.\n");
497 return err;
498 }
499
500 err = perf_event__synthesize_stat_config(NULL, &stat_config,
501 process_synthesized_event, NULL);
502 if (err < 0) {
503 pr_err("Couldn't synthesize config.\n");
504 return err;
505 }
506
507 return 0;
508}
509
Jiri Olsa2af46462015-11-05 15:40:49 +0100510#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
511
Andi Kleen42811d52017-10-05 19:00:28 -0700512static int __store_counter_ids(struct perf_evsel *counter)
Jiri Olsa2af46462015-11-05 15:40:49 +0100513{
514 int cpu, thread;
515
Andi Kleen42811d52017-10-05 19:00:28 -0700516 for (cpu = 0; cpu < xyarray__max_x(counter->fd); cpu++) {
517 for (thread = 0; thread < xyarray__max_y(counter->fd);
518 thread++) {
Jiri Olsa2af46462015-11-05 15:40:49 +0100519 int fd = FD(counter, cpu, thread);
520
521 if (perf_evlist__id_add_fd(evsel_list, counter,
522 cpu, thread, fd) < 0)
523 return -1;
524 }
525 }
526
527 return 0;
528}
529
530static int store_counter_ids(struct perf_evsel *counter)
531{
532 struct cpu_map *cpus = counter->cpus;
533 struct thread_map *threads = counter->threads;
534
535 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
536 return -ENOMEM;
537
Andi Kleen42811d52017-10-05 19:00:28 -0700538 return __store_counter_ids(counter);
Jiri Olsa2af46462015-11-05 15:40:49 +0100539}
540
Jiri Olsa82bf3112017-07-26 14:02:06 +0200541static bool perf_evsel__should_store_id(struct perf_evsel *counter)
542{
543 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
544}
545
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700546static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
547{
548 struct perf_evsel *c2, *leader;
549 bool is_open = true;
550
551 leader = evsel->leader;
552 pr_debug("Weak group for %s/%d failed\n",
553 leader->name, leader->nr_members);
554
555 /*
556 * for_each_group_member doesn't work here because it doesn't
557 * include the first entry.
558 */
559 evlist__for_each_entry(evsel_list, c2) {
560 if (c2 == evsel)
561 is_open = false;
562 if (c2->leader == leader) {
563 if (is_open)
564 perf_evsel__close(c2);
565 c2->leader = c2;
566 c2->nr_members = 0;
567 }
568 }
569 return leader;
570}
571
Namhyung Kimacf28922013-03-11 16:43:18 +0900572static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200573{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200574 int interval = stat_config.interval;
yuzhoujiandb06a262018-01-29 10:25:22 +0100575 int times = stat_config.times;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100576 int timeout = stat_config.timeout;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300577 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200578 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100579 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100580 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100581 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200582 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300583 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100584 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600585 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200586
Stephane Eranian13370a92013-01-29 12:47:44 +0100587 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300588 ts.tv_sec = interval / USEC_PER_MSEC;
589 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100590 } else if (timeout) {
591 ts.tv_sec = timeout / USEC_PER_MSEC;
592 ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100593 } else {
594 ts.tv_sec = 1;
595 ts.tv_nsec = 0;
596 }
597
Liming Wang60666c62009-12-31 16:05:50 +0800598 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100599 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300600 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900601 perror("failed to prepare workload");
602 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800603 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900604 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000605 }
606
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200607 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300608 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200609
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300610 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300611try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100612 if (create_perf_stat_counter(counter) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700613
614 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700615 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700616 counter->leader != counter &&
617 counter->weak_group) {
618 counter = perf_evsel__reset_weak_group(counter);
619 goto try_again;
620 }
621
David Ahern979987a2012-05-08 09:29:16 -0600622 /*
623 * PPC returns ENXIO for HW counters until 2.6.37
624 * (behavior changed with commit b0a873e).
625 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100626 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600627 errno == ENOENT || errno == EOPNOTSUPP ||
628 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900629 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600630 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300631 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600632 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400633
634 if ((counter->leader != counter) ||
635 !(counter->leader->nr_members > 1))
636 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300637 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900638 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300639 ui__warning("%s\n", msg);
640 goto try_again;
Jin Yaoab6c79b2018-01-16 23:43:08 +0800641 } else if (target__has_per_thread(&target) &&
642 evsel_list->threads &&
643 evsel_list->threads->err_thread != -1) {
644 /*
645 * For global --per-thread case, skip current
646 * error thread.
647 */
648 if (!thread_map__remove(evsel_list->threads,
649 evsel_list->threads->err_thread)) {
650 evsel_list->threads->err_thread = -1;
651 goto try_again;
652 }
653 }
Ingo Molnarede70292011-04-28 08:48:42 +0200654
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300655 perf_evsel__open_strerror(counter, &target,
656 errno, msg, sizeof(msg));
657 ui__error("%s\n", msg);
658
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200659 if (child_pid != -1)
660 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600661
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200662 return -1;
663 }
David Ahern2cee77c2011-05-30 08:55:59 -0600664 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100665
666 l = strlen(counter->unit);
667 if (l > unit_width)
668 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100669
Jiri Olsa82bf3112017-07-26 14:02:06 +0200670 if (perf_evsel__should_store_id(counter) &&
671 store_counter_ids(counter))
Jiri Olsa2af46462015-11-05 15:40:49 +0100672 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300673 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200674
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300675 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300676 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300677 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300678 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100679 return -1;
680 }
681
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600682 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300683 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600684 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
685 str_error_r(errno, msg, sizeof(msg)));
686 return -1;
687 }
688
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100689 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100690 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100691
Jiri Olsa664c98d2015-11-05 15:40:50 +0100692 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100693 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100694 } else {
695 err = perf_session__write_header(perf_stat.session, evsel_list,
696 fd, false);
697 }
698
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100699 if (err < 0)
700 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100701
Jiri Olsa664c98d2015-11-05 15:40:50 +0100702 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100703 if (err < 0)
704 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100705 }
706
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200707 /*
708 * Enable counters and exec the command:
709 */
710 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100711 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200712
Liming Wang60666c62009-12-31 16:05:50 +0800713 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900714 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100715 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900716
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100717 if (interval || timeout) {
Stephane Eranian13370a92013-01-29 12:47:44 +0100718 while (!waitpid(child_pid, &status, WNOHANG)) {
719 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100720 if (timeout)
721 break;
Jiri Olsaba411a92015-06-26 11:29:24 +0200722 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100723 if (interval_count && !(--times))
724 break;
Stephane Eranian13370a92013-01-29 12:47:44 +0100725 }
726 }
Milian Wolffdfc9eec2017-09-12 17:25:23 +0200727 waitpid(child_pid, &status, 0);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300728
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300729 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300730 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300731 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300732 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300733 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300734
Andi Kleen33e49ea2011-09-15 14:31:40 -0700735 if (WIFSIGNALED(status))
736 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800737 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100738 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100739 while (!done) {
740 nanosleep(&ts, NULL);
yuzhoujianf1f8ad52018-01-29 10:25:23 +0100741 if (timeout)
742 break;
yuzhoujiandb06a262018-01-29 10:25:22 +0100743 if (interval) {
Jiri Olsaba411a92015-06-26 11:29:24 +0200744 process_interval();
yuzhoujiandb06a262018-01-29 10:25:22 +0100745 if (interval_count && !(--times))
746 break;
747 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100748 }
Liming Wang60666c62009-12-31 16:05:50 +0800749 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200750
Mark Rutland3df33ef2016-08-09 14:04:29 +0100751 disable_counters();
752
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200753 t1 = rdclock();
754
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200755 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200756
Mark Rutland3df33ef2016-08-09 14:04:29 +0100757 /*
758 * Closing a group leader splits the group, and as we only disable
759 * group leaders, results in remaining events becoming enabled. To
760 * avoid arbitrary skew, we must read all counters before closing any
761 * group leaders.
762 */
763 read_counters();
764 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200765
Ingo Molnar42202dd2009-06-13 14:57:28 +0200766 return WEXITSTATUS(status);
767}
768
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300769static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200770{
771 int ret;
772
773 if (pre_cmd) {
774 ret = system(pre_cmd);
775 if (ret)
776 return ret;
777 }
778
779 if (sync_run)
780 sync();
781
782 ret = __run_perf_stat(argc, argv);
783 if (ret)
784 return ret;
785
786 if (post_cmd) {
787 ret = system(post_cmd);
788 if (ret)
789 return ret;
790 }
791
792 return ret;
793}
794
Andi Kleend73515c2015-03-11 07:16:27 -0700795static void print_running(u64 run, u64 ena)
796{
797 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200798 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700799 csv_sep,
800 run,
801 csv_sep,
802 ena ? 100.0 * run / ena : 100.0);
803 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200804 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700805 }
806}
807
Ingo Molnarf99844c2011-04-27 05:35:39 +0200808static void print_noise_pct(double total, double avg)
809{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800810 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200811
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700812 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200813 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600814 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200815 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200816}
817
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200818static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200819{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200820 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200821
Peter Zijlstra849abde2009-09-04 18:23:38 +0200822 if (run_count == 1)
823 return;
824
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300825 ps = evsel->stats;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200826 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200827}
828
Stephane Eranian12c08a92013-02-14 13:57:29 +0100829static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200830{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200831 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100832 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200833 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100834 cpu_map__id_to_socket(id),
835 csv_output ? 0 : -8,
836 cpu_map__id_to_cpu(id),
837 csv_sep,
838 csv_output ? 0 : 4,
839 nr,
840 csv_sep);
841 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100842 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200843 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100844 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100845 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100846 csv_sep,
847 csv_output ? 0 : 4,
848 nr,
849 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100850 break;
851 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200852 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200853 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100854 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100855 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200856 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200857 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200858 csv_output ? 0 : 16,
859 thread_map__comm(evsel->threads, id),
860 csv_output ? 0 : -8,
861 thread_map__pid(evsel->threads, id),
862 csv_sep);
863 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100864 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200865 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100866 default:
867 break;
868 }
869}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200870
Andi Kleen140aead2016-01-30 09:06:49 -0800871struct outstate {
872 FILE *fh;
873 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800874 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800875 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800876 int id, nr;
877 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800878};
879
880#define METRIC_LEN 35
881
882static void new_line_std(void *ctx)
883{
884 struct outstate *os = ctx;
885
886 os->newline = true;
887}
888
889static void do_new_line_std(struct outstate *os)
890{
891 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800892 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800893 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800894 if (stat_config.aggr_mode == AGGR_NONE)
895 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800896 fprintf(os->fh, " ");
897}
898
899static void print_metric_std(void *ctx, const char *color, const char *fmt,
900 const char *unit, double val)
901{
902 struct outstate *os = ctx;
903 FILE *out = os->fh;
904 int n;
905 bool newline = os->newline;
906
907 os->newline = false;
908
909 if (unit == NULL || fmt == NULL) {
910 fprintf(out, "%-*s", METRIC_LEN, "");
911 return;
912 }
913
914 if (newline)
915 do_new_line_std(os);
916
917 n = fprintf(out, " # ");
918 if (color)
919 n += color_fprintf(out, color, fmt, val);
920 else
921 n += fprintf(out, fmt, val);
922 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
923}
924
Andi Kleen92a61f62016-02-29 14:36:21 -0800925static void new_line_csv(void *ctx)
926{
927 struct outstate *os = ctx;
928 int i;
929
930 fputc('\n', os->fh);
931 if (os->prefix)
932 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800933 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800934 for (i = 0; i < os->nfields; i++)
935 fputs(csv_sep, os->fh);
936}
937
938static void print_metric_csv(void *ctx,
939 const char *color __maybe_unused,
940 const char *fmt, const char *unit, double val)
941{
942 struct outstate *os = ctx;
943 FILE *out = os->fh;
944 char buf[64], *vals, *ends;
945
946 if (unit == NULL || fmt == NULL) {
Ilya Pronin40c21892018-03-05 22:43:53 -0800947 fprintf(out, "%s%s", csv_sep, csv_sep);
Andi Kleen92a61f62016-02-29 14:36:21 -0800948 return;
949 }
950 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900951 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800952 while (isdigit(*ends) || *ends == '.')
953 ends++;
954 *ends = 0;
955 while (isspace(*unit))
956 unit++;
957 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
958}
959
Andi Kleen54b50912016-03-03 15:57:36 -0800960#define METRIC_ONLY_LEN 20
961
962/* Filter out some columns that don't work well in metrics only mode */
963
964static bool valid_only_metric(const char *unit)
965{
966 if (!unit)
967 return false;
968 if (strstr(unit, "/sec") ||
969 strstr(unit, "hz") ||
970 strstr(unit, "Hz") ||
971 strstr(unit, "CPUs utilized"))
972 return false;
973 return true;
974}
975
976static const char *fixunit(char *buf, struct perf_evsel *evsel,
977 const char *unit)
978{
979 if (!strncmp(unit, "of all", 6)) {
980 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
981 unit);
982 return buf;
983 }
984 return unit;
985}
986
987static void print_metric_only(void *ctx, const char *color, const char *fmt,
988 const char *unit, double val)
989{
990 struct outstate *os = ctx;
991 FILE *out = os->fh;
992 int n;
993 char buf[1024];
994 unsigned mlen = METRIC_ONLY_LEN;
995
996 if (!valid_only_metric(unit))
997 return;
998 unit = fixunit(buf, os->evsel, unit);
999 if (color)
1000 n = color_fprintf(out, color, fmt, val);
1001 else
1002 n = fprintf(out, fmt, val);
1003 if (n > METRIC_ONLY_LEN)
1004 n = METRIC_ONLY_LEN;
1005 if (mlen < strlen(unit))
1006 mlen = strlen(unit) + 1;
1007 fprintf(out, "%*s", mlen - n, "");
1008}
1009
1010static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
1011 const char *fmt,
1012 const char *unit, double val)
1013{
1014 struct outstate *os = ctx;
1015 FILE *out = os->fh;
1016 char buf[64], *vals, *ends;
1017 char tbuf[1024];
1018
1019 if (!valid_only_metric(unit))
1020 return;
1021 unit = fixunit(tbuf, os->evsel, unit);
1022 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +09001023 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -08001024 while (isdigit(*ends) || *ends == '.')
1025 ends++;
1026 *ends = 0;
1027 fprintf(out, "%s%s", vals, csv_sep);
1028}
1029
1030static void new_line_metric(void *ctx __maybe_unused)
1031{
1032}
1033
1034static void print_metric_header(void *ctx, const char *color __maybe_unused,
1035 const char *fmt __maybe_unused,
1036 const char *unit, double val __maybe_unused)
1037{
1038 struct outstate *os = ctx;
1039 char tbuf[1024];
1040
1041 if (!valid_only_metric(unit))
1042 return;
1043 unit = fixunit(tbuf, os->evsel, unit);
1044 if (csv_output)
1045 fprintf(os->fh, "%s%s", unit, csv_sep);
1046 else
1047 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
1048}
1049
Andi Kleenda88c7f2014-09-24 13:50:46 -07001050static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001051{
Jiri Olsa58215222015-07-21 14:31:24 +02001052 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001053 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +01001054 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -06001055 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001056
Stephane Eranian410136f2013-11-12 17:58:49 +01001057 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
1058 fmt_n = csv_output ? "%s" : "%-25s";
1059
Andi Kleenda88c7f2014-09-24 13:50:46 -07001060 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001061
David Ahern4bbe5a62013-09-28 14:28:00 -06001062 scnprintf(name, sizeof(name), "%s%s",
1063 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +01001064
1065 fprintf(output, fmt_v, msecs, csv_sep);
1066
1067 if (csv_output)
1068 fprintf(output, "%s%s", evsel->unit, csv_sep);
1069 else
1070 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1071
1072 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001073
Stephane Eranian023695d2011-02-14 11:20:01 +02001074 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001075 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001076}
1077
Andi Kleen44d49a62016-02-29 14:36:22 -08001078static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1079{
1080 int i;
1081
1082 if (!aggr_get_id)
1083 return 0;
1084
1085 if (stat_config.aggr_mode == AGGR_NONE)
1086 return id;
1087
1088 if (stat_config.aggr_mode == AGGR_GLOBAL)
1089 return 0;
1090
1091 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1092 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1093
1094 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1095 return cpu2;
1096 }
1097 return 0;
1098}
1099
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001100static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1101{
Jiri Olsa58215222015-07-21 14:31:24 +02001102 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001103 double sc = evsel->scale;
1104 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001105
1106 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001107 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001108 } else {
1109 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001110 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001111 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001112 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001113 }
1114
1115 aggr_printout(evsel, id, nr);
1116
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001117 fprintf(output, fmt, avg, csv_sep);
1118
1119 if (evsel->unit)
1120 fprintf(output, "%-*s%s",
1121 csv_output ? 0 : unit_width,
1122 evsel->unit, csv_sep);
1123
1124 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1125
1126 if (evsel->cgrp)
1127 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001128}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001129
Kan Liang30060ea2018-04-24 11:20:11 -07001130static bool is_mixed_hw_group(struct perf_evsel *counter)
1131{
1132 struct perf_evlist *evlist = counter->evlist;
1133 u32 pmu_type = counter->attr.type;
1134 struct perf_evsel *pos;
1135
1136 if (counter->nr_members < 2)
1137 return false;
1138
1139 evlist__for_each_entry(evlist, pos) {
1140 /* software events can be part of any hardware group */
1141 if (pos->attr.type == PERF_TYPE_SOFTWARE)
1142 continue;
1143 if (pmu_type == PERF_TYPE_SOFTWARE) {
1144 pmu_type = pos->attr.type;
1145 continue;
1146 }
1147 if (pmu_type != pos->attr.type)
1148 return true;
1149 }
1150
1151 return false;
1152}
1153
Andi Kleenf9483392016-01-30 09:06:50 -08001154static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +08001155 char *prefix, u64 run, u64 ena, double noise,
1156 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001157{
Andi Kleen140aead2016-01-30 09:06:49 -08001158 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001159 struct outstate os = {
1160 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001161 .prefix = prefix ? prefix : "",
1162 .id = id,
1163 .nr = nr,
1164 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001165 };
Andi Kleen140aead2016-01-30 09:06:49 -08001166 print_metric_t pm = print_metric_std;
1167 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001168
Andi Kleen54b50912016-03-03 15:57:36 -08001169 if (metric_only) {
1170 nl = new_line_metric;
1171 if (csv_output)
1172 pm = print_metric_only_csv;
1173 else
1174 pm = print_metric_only;
1175 } else
1176 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001177
Andi Kleen54b50912016-03-03 15:57:36 -08001178 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001179 static int aggr_fields[] = {
1180 [AGGR_GLOBAL] = 0,
1181 [AGGR_THREAD] = 1,
1182 [AGGR_NONE] = 1,
1183 [AGGR_SOCKET] = 2,
1184 [AGGR_CORE] = 2,
1185 };
1186
1187 pm = print_metric_csv;
1188 nl = new_line_csv;
1189 os.nfields = 3;
1190 os.nfields += aggr_fields[stat_config.aggr_mode];
1191 if (counter->cgrp)
1192 os.nfields++;
1193 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001194 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001195 if (metric_only) {
1196 pm(&os, NULL, "", "", 0);
1197 return;
1198 }
Andi Kleencb110f42016-01-30 09:06:51 -08001199 aggr_printout(counter, id, nr);
1200
1201 fprintf(stat_config.output, "%*s%s",
1202 csv_output ? 0 : 18,
1203 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1204 csv_sep);
1205
Kan Liang30060ea2018-04-24 11:20:11 -07001206 if (counter->supported) {
Borislav Petkov02d492e2017-02-07 01:40:05 +01001207 print_free_counters_hint = 1;
Kan Liang30060ea2018-04-24 11:20:11 -07001208 if (is_mixed_hw_group(counter))
1209 print_mixed_hw_group_error = 1;
1210 }
Borislav Petkov02d492e2017-02-07 01:40:05 +01001211
Andi Kleencb110f42016-01-30 09:06:51 -08001212 fprintf(stat_config.output, "%-*s%s",
1213 csv_output ? 0 : unit_width,
1214 counter->unit, csv_sep);
1215
1216 fprintf(stat_config.output, "%*s",
1217 csv_output ? 0 : -25,
1218 perf_evsel__name(counter));
1219
1220 if (counter->cgrp)
1221 fprintf(stat_config.output, "%s%s",
1222 csv_sep, counter->cgrp->name);
1223
Andi Kleen92a61f62016-02-29 14:36:21 -08001224 if (!csv_output)
1225 pm(&os, NULL, NULL, "", 0);
1226 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001227 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001228 if (csv_output)
1229 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001230 return;
1231 }
1232
Andi Kleen54b50912016-03-03 15:57:36 -08001233 if (metric_only)
1234 /* nothing */;
1235 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001236 nsec_printout(id, nr, counter, uval);
1237 else
1238 abs_printout(id, nr, counter, uval);
1239
Andi Kleen140aead2016-01-30 09:06:49 -08001240 out.print_metric = pm;
1241 out.new_line = nl;
1242 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001243 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001244
Andi Kleen54b50912016-03-03 15:57:36 -08001245 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001246 print_noise(counter, noise);
1247 print_running(run, ena);
1248 }
1249
1250 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001251 first_shadow_cpu(counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001252 &out, &metric_events, st);
Andi Kleen54b50912016-03-03 15:57:36 -08001253 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001254 print_noise(counter, noise);
1255 print_running(run, ena);
1256 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001257}
1258
Andi Kleen44d49a62016-02-29 14:36:22 -08001259static void aggr_update_shadow(void)
1260{
1261 int cpu, s2, id, s;
1262 u64 val;
1263 struct perf_evsel *counter;
1264
1265 for (s = 0; s < aggr_map->nr; s++) {
1266 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001267 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001268 val = 0;
1269 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1270 s2 = aggr_get_id(evsel_list->cpus, cpu);
1271 if (s2 != id)
1272 continue;
1273 val += perf_counts(counter->counts, cpu, 0)->val;
1274 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001275 perf_stat__update_shadow_stats(counter, val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001276 first_shadow_cpu(counter, id),
1277 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001278 }
1279 }
1280}
1281
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001282static void uniquify_event_name(struct perf_evsel *counter)
1283{
1284 char *new_name;
1285 char *config;
1286
1287 if (!counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
1288 strlen(counter->pmu_name)))
1289 return;
1290
1291 config = strchr(counter->name, '/');
1292 if (config) {
1293 if (asprintf(&new_name,
1294 "%s%s", counter->pmu_name, config) > 0) {
1295 free(counter->name);
1296 counter->name = new_name;
1297 }
1298 } else {
1299 if (asprintf(&new_name,
1300 "%s [%s]", counter->name, counter->pmu_name) > 0) {
1301 free(counter->name);
1302 counter->name = new_name;
1303 }
1304 }
1305}
1306
Andi Kleen430daf22017-03-20 13:17:00 -07001307static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001308 void (*cb)(struct perf_evsel *counter, void *data,
1309 bool first),
1310 void *data)
1311{
Andi Kleen430daf22017-03-20 13:17:00 -07001312 struct perf_evsel *alias;
1313
1314 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1315 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1316 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1317 alias->scale != counter->scale ||
1318 alias->cgrp != counter->cgrp ||
1319 strcmp(alias->unit, counter->unit) ||
1320 nsec_counter(alias) != nsec_counter(counter))
1321 break;
1322 alias->merged_stat = true;
1323 cb(alias, data, false);
1324 }
1325}
1326
1327static bool collect_data(struct perf_evsel *counter,
1328 void (*cb)(struct perf_evsel *counter, void *data,
1329 bool first),
1330 void *data)
1331{
1332 if (counter->merged_stat)
1333 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001334 cb(counter, data, true);
Agustin Vega-Frias8c5421c2018-03-06 09:04:43 -05001335 if (no_merge)
1336 uniquify_event_name(counter);
1337 else if (counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001338 collect_all_aliases(counter, cb, data);
1339 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001340}
1341
1342struct aggr_data {
1343 u64 ena, run, val;
1344 int id;
1345 int nr;
1346 int cpu;
1347};
1348
1349static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1350{
1351 struct aggr_data *ad = data;
1352 int cpu, s2;
1353
1354 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1355 struct perf_counts_values *counts;
1356
1357 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1358 if (s2 != ad->id)
1359 continue;
1360 if (first)
1361 ad->nr++;
1362 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001363 /*
1364 * When any result is bad, make them all to give
1365 * consistent output in interval mode.
1366 */
1367 if (counts->ena == 0 || counts->run == 0 ||
1368 counter->counts->scaled == -1) {
1369 ad->ena = 0;
1370 ad->run = 0;
1371 break;
1372 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001373 ad->val += counts->val;
1374 ad->ena += counts->ena;
1375 ad->run += counts->run;
1376 }
1377}
1378
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001379static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001380{
Jiri Olsa58215222015-07-21 14:31:24 +02001381 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001382 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001383 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001384 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001385 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001386 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001387
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001388 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001389 return;
1390
Andi Kleen44d49a62016-02-29 14:36:22 -08001391 aggr_update_shadow();
1392
Andi Kleen54b50912016-03-03 15:57:36 -08001393 /*
1394 * With metric_only everything is on a single line.
1395 * Without each counter has its own line.
1396 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001397 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001398 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001399 if (prefix && metric_only)
1400 fprintf(output, "%s", prefix);
1401
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001402 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001403 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001404 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001405 if (is_duration_time(counter))
1406 continue;
1407
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001408 ad.val = ad.ena = ad.run = 0;
1409 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001410 if (!collect_data(counter, aggr_cb, &ad))
1411 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001412 nr = ad.nr;
1413 ena = ad.ena;
1414 run = ad.run;
1415 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001416 if (first && metric_only) {
1417 first = false;
1418 aggr_printout(counter, id, nr);
1419 }
1420 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001421 fprintf(output, "%s", prefix);
1422
Stephane Eranian410136f2013-11-12 17:58:49 +01001423 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001424 printout(id, nr, counter, uval, prefix, run, ena, 1.0,
1425 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001426 if (!metric_only)
1427 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001428 }
Andi Kleen54b50912016-03-03 15:57:36 -08001429 if (metric_only)
1430 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001431 }
1432}
1433
Jin Yao29734552017-12-05 22:03:11 +08001434static int cmp_val(const void *a, const void *b)
Jiri Olsa32b8af82015-06-26 11:29:27 +02001435{
Jin Yao29734552017-12-05 22:03:11 +08001436 return ((struct perf_aggr_thread_value *)b)->val -
1437 ((struct perf_aggr_thread_value *)a)->val;
1438}
1439
1440static struct perf_aggr_thread_value *sort_aggr_thread(
1441 struct perf_evsel *counter,
1442 int nthreads, int ncpus,
1443 int *ret)
1444{
1445 int cpu, thread, i = 0;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001446 double uval;
Jin Yao29734552017-12-05 22:03:11 +08001447 struct perf_aggr_thread_value *buf;
1448
1449 buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1450 if (!buf)
1451 return NULL;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001452
1453 for (thread = 0; thread < nthreads; thread++) {
1454 u64 ena = 0, run = 0, val = 0;
1455
1456 for (cpu = 0; cpu < ncpus; cpu++) {
1457 val += perf_counts(counter->counts, cpu, thread)->val;
1458 ena += perf_counts(counter->counts, cpu, thread)->ena;
1459 run += perf_counts(counter->counts, cpu, thread)->run;
1460 }
1461
Jin Yao29734552017-12-05 22:03:11 +08001462 uval = val * counter->scale;
1463
1464 /*
1465 * Skip value 0 when enabling --per-thread globally,
1466 * otherwise too many 0 output.
1467 */
1468 if (uval == 0.0 && target__has_per_thread(&target))
1469 continue;
1470
1471 buf[i].counter = counter;
1472 buf[i].id = thread;
1473 buf[i].uval = uval;
1474 buf[i].val = val;
1475 buf[i].run = run;
1476 buf[i].ena = ena;
1477 i++;
1478 }
1479
1480 qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
1481
1482 if (ret)
1483 *ret = i;
1484
1485 return buf;
1486}
1487
1488static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1489{
1490 FILE *output = stat_config.output;
1491 int nthreads = thread_map__nr(counter->threads);
1492 int ncpus = cpu_map__nr(counter->cpus);
1493 int thread, sorted_threads, id;
1494 struct perf_aggr_thread_value *buf;
1495
1496 buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads);
1497 if (!buf) {
1498 perror("cannot sort aggr thread");
1499 return;
1500 }
1501
1502 for (thread = 0; thread < sorted_threads; thread++) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02001503 if (prefix)
1504 fprintf(output, "%s", prefix);
1505
Jin Yao29734552017-12-05 22:03:11 +08001506 id = buf[thread].id;
Jin Yao14e72a22017-12-05 22:03:08 +08001507 if (stat_config.stats)
Jin Yao29734552017-12-05 22:03:11 +08001508 printout(id, 0, buf[thread].counter, buf[thread].uval,
1509 prefix, buf[thread].run, buf[thread].ena, 1.0,
1510 &stat_config.stats[id]);
Jin Yao14e72a22017-12-05 22:03:08 +08001511 else
Jin Yao29734552017-12-05 22:03:11 +08001512 printout(id, 0, buf[thread].counter, buf[thread].uval,
1513 prefix, buf[thread].run, buf[thread].ena, 1.0,
1514 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001515 fputc('\n', output);
1516 }
Jin Yao29734552017-12-05 22:03:11 +08001517
1518 free(buf);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001519}
1520
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001521struct caggr_data {
1522 double avg, avg_enabled, avg_running;
1523};
1524
1525static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1526 bool first __maybe_unused)
1527{
1528 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001529 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001530
1531 cd->avg += avg_stats(&ps->res_stats[0]);
1532 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1533 cd->avg_running += avg_stats(&ps->res_stats[2]);
1534}
1535
Ingo Molnar42202dd2009-06-13 14:57:28 +02001536/*
1537 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001538 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001539 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001540static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001541{
Jiri Olsa58215222015-07-21 14:31:24 +02001542 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001543 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001544 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001545
Andi Kleen430daf22017-03-20 13:17:00 -07001546 if (!collect_data(counter, counter_aggr_cb, &cd))
1547 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001548
Andi Kleen54b50912016-03-03 15:57:36 -08001549 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001550 fprintf(output, "%s", prefix);
1551
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001552 uval = cd.avg * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001553 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
1554 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001555 if (!metric_only)
1556 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001557}
1558
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001559static void counter_cb(struct perf_evsel *counter, void *data,
1560 bool first __maybe_unused)
1561{
1562 struct aggr_data *ad = data;
1563
1564 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1565 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1566 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1567}
1568
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001569/*
1570 * Print out the results of a single counter:
1571 * does not use aggregated count in system-wide
1572 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001573static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001574{
Jiri Olsa58215222015-07-21 14:31:24 +02001575 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001576 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001577 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001578 int cpu;
1579
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001580 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001581 struct aggr_data ad = { .cpu = cpu };
1582
Andi Kleen430daf22017-03-20 13:17:00 -07001583 if (!collect_data(counter, counter_cb, &ad))
1584 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001585 val = ad.val;
1586 ena = ad.ena;
1587 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001588
1589 if (prefix)
1590 fprintf(output, "%s", prefix);
1591
Stephane Eranian410136f2013-11-12 17:58:49 +01001592 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001593 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1594 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001595
Stephane Eranian4aa90152011-08-15 22:22:33 +02001596 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001597 }
1598}
1599
Andi Kleen206cab62016-03-03 15:57:37 -08001600static void print_no_aggr_metric(char *prefix)
1601{
1602 int cpu;
1603 int nrcpus = 0;
1604 struct perf_evsel *counter;
1605 u64 ena, run, val;
1606 double uval;
1607
1608 nrcpus = evsel_list->cpus->nr;
1609 for (cpu = 0; cpu < nrcpus; cpu++) {
1610 bool first = true;
1611
1612 if (prefix)
1613 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001614 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001615 if (is_duration_time(counter))
1616 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001617 if (first) {
1618 aggr_printout(counter, cpu, 0);
1619 first = false;
1620 }
1621 val = perf_counts(counter->counts, cpu, 0)->val;
1622 ena = perf_counts(counter->counts, cpu, 0)->ena;
1623 run = perf_counts(counter->counts, cpu, 0)->run;
1624
1625 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001626 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1627 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001628 }
1629 fputc('\n', stat_config.output);
1630 }
1631}
1632
Andi Kleen54b50912016-03-03 15:57:36 -08001633static int aggr_header_lens[] = {
1634 [AGGR_CORE] = 18,
1635 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001636 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001637 [AGGR_THREAD] = 24,
1638 [AGGR_GLOBAL] = 0,
1639};
1640
Andi Kleenc51fd632016-05-24 12:52:39 -07001641static const char *aggr_header_csv[] = {
1642 [AGGR_CORE] = "core,cpus,",
1643 [AGGR_SOCKET] = "socket,cpus",
1644 [AGGR_NONE] = "cpu,",
1645 [AGGR_THREAD] = "comm-pid,",
1646 [AGGR_GLOBAL] = ""
1647};
1648
Andi Kleen41c8ca22016-05-24 12:52:38 -07001649static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001650{
1651 struct perf_stat_output_ctx out;
1652 struct perf_evsel *counter;
1653 struct outstate os = {
1654 .fh = stat_config.output
1655 };
1656
1657 if (prefix)
1658 fprintf(stat_config.output, "%s", prefix);
1659
Andi Kleen41c8ca22016-05-24 12:52:38 -07001660 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001661 fprintf(stat_config.output, "%*s",
1662 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001663 if (csv_output) {
1664 if (stat_config.interval)
1665 fputs("time,", stat_config.output);
1666 fputs(aggr_header_csv[stat_config.aggr_mode],
1667 stat_config.output);
1668 }
Andi Kleen54b50912016-03-03 15:57:36 -08001669
1670 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001671 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001672 if (is_duration_time(counter))
1673 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001674 os.evsel = counter;
1675 out.ctx = &os;
1676 out.print_metric = print_metric_header;
1677 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001678 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001679 os.evsel = counter;
1680 perf_stat__print_shadow_stats(counter, 0,
1681 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001682 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001683 &metric_events,
1684 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001685 }
1686 fputc('\n', stat_config.output);
1687}
1688
Jiri Olsad4f63a42015-06-26 11:29:26 +02001689static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001690{
Jiri Olsa58215222015-07-21 14:31:24 +02001691 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001692 static int num_print_interval;
1693
1694 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1695
Andi Kleen41c8ca22016-05-24 12:52:38 -07001696 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001697 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001698 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001699 fprintf(output, "# time socket cpus");
1700 if (!metric_only)
1701 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001702 break;
1703 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001704 fprintf(output, "# time core cpus");
1705 if (!metric_only)
1706 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001707 break;
1708 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001709 fprintf(output, "# time CPU");
1710 if (!metric_only)
1711 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001712 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001713 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001714 fprintf(output, "# time comm-pid");
1715 if (!metric_only)
1716 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001717 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001718 case AGGR_GLOBAL:
1719 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001720 fprintf(output, "# time");
1721 if (!metric_only)
1722 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001723 case AGGR_UNSET:
1724 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001725 }
1726 }
1727
Andi Kleen41c8ca22016-05-24 12:52:38 -07001728 if (num_print_interval == 0 && metric_only)
1729 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001730 if (++num_print_interval == 25)
1731 num_print_interval = 0;
1732}
1733
1734static void print_header(int argc, const char **argv)
1735{
Jiri Olsa58215222015-07-21 14:31:24 +02001736 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001737 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001738
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001739 fflush(stdout);
1740
Stephane Eraniand7470b62010-12-01 18:49:05 +02001741 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001742 fprintf(output, "\n");
1743 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001744 if (target.system_wide)
1745 fprintf(output, "\'system wide");
1746 else if (target.cpu_list)
1747 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001748 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001749 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1750 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001751 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001752 } else if (target.pid)
1753 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001754 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001755 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001756
Stephane Eranian4aa90152011-08-15 22:22:33 +02001757 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001758 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001759 fprintf(output, " (%d runs)", run_count);
1760 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001761 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001762}
1763
1764static void print_footer(void)
1765{
Jiri Olsa58215222015-07-21 14:31:24 +02001766 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001767 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001768
Jiri Olsad4f63a42015-06-26 11:29:26 +02001769 if (!null_run)
1770 fprintf(output, "\n");
1771 fprintf(output, " %17.9f seconds time elapsed",
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001772 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001773 if (run_count > 1) {
1774 fprintf(output, " ");
1775 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1776 avg_stats(&walltime_nsecs_stats));
1777 }
1778 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001779
Andi Kleen918c7b062017-05-22 18:00:16 -07001780 if (print_free_counters_hint &&
1781 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1782 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001783 fprintf(output,
1784"Some events weren't counted. Try disabling the NMI watchdog:\n"
1785" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1786" perf stat ...\n"
1787" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Kan Liang30060ea2018-04-24 11:20:11 -07001788
1789 if (print_mixed_hw_group_error)
1790 fprintf(output,
1791 "The events in group usually have to be from "
1792 "the same PMU. Try reorganizing the group.\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001793}
1794
1795static void print_counters(struct timespec *ts, int argc, const char **argv)
1796{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001797 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001798 struct perf_evsel *counter;
1799 char buf[64], *prefix = NULL;
1800
Jiri Olsa664c98d2015-11-05 15:40:50 +01001801 /* Do not print anything if we record to the pipe. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001802 if (STAT_RECORD && perf_stat.data.is_pipe)
Jiri Olsa664c98d2015-11-05 15:40:50 +01001803 return;
1804
Jiri Olsad4f63a42015-06-26 11:29:26 +02001805 if (interval)
1806 print_interval(prefix = buf, ts);
1807 else
1808 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001809
Andi Kleen54b50912016-03-03 15:57:36 -08001810 if (metric_only) {
1811 static int num_print_iv;
1812
Andi Kleen41c8ca22016-05-24 12:52:38 -07001813 if (num_print_iv == 0 && !interval)
1814 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001815 if (num_print_iv++ == 25)
1816 num_print_iv = 0;
1817 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1818 fprintf(stat_config.output, "%s", prefix);
1819 }
1820
Jiri Olsa421a50f2015-07-21 14:31:22 +02001821 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001822 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001823 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001824 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001825 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001826 case AGGR_THREAD:
Andi Kleene864c5c2017-08-31 12:40:35 -07001827 evlist__for_each_entry(evsel_list, counter) {
1828 if (is_duration_time(counter))
1829 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001830 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001831 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001832 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001833 case AGGR_GLOBAL:
Andi Kleene864c5c2017-08-31 12:40:35 -07001834 evlist__for_each_entry(evsel_list, counter) {
1835 if (is_duration_time(counter))
1836 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001837 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001838 }
Andi Kleen54b50912016-03-03 15:57:36 -08001839 if (metric_only)
1840 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001841 break;
1842 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001843 if (metric_only)
1844 print_no_aggr_metric(prefix);
1845 else {
Andi Kleene864c5c2017-08-31 12:40:35 -07001846 evlist__for_each_entry(evsel_list, counter) {
1847 if (is_duration_time(counter))
1848 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001849 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001850 }
Andi Kleen206cab62016-03-03 15:57:37 -08001851 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001852 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001853 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001854 default:
1855 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001856 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001857
Jiri Olsad4f63a42015-06-26 11:29:26 +02001858 if (!interval && !csv_output)
1859 print_footer();
1860
Jiri Olsa58215222015-07-21 14:31:24 +02001861 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001862}
1863
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001864static volatile int signr = -1;
1865
Ingo Molnar52425192009-05-26 09:17:18 +02001866static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001867{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001868 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001869 done = 1;
1870
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001871 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001872 /*
1873 * render child_pid harmless
1874 * won't send SIGTERM to a random
1875 * process in case of race condition
1876 * and fast PID recycling
1877 */
1878 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001879}
1880
1881static void sig_atexit(void)
1882{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001883 sigset_t set, oset;
1884
1885 /*
1886 * avoid race condition with SIGCHLD handler
1887 * in skip_signal() which is modifying child_pid
1888 * goal is to avoid send SIGTERM to a random
1889 * process
1890 */
1891 sigemptyset(&set);
1892 sigaddset(&set, SIGCHLD);
1893 sigprocmask(SIG_BLOCK, &set, &oset);
1894
Chris Wilson933da832009-10-04 01:35:01 +01001895 if (child_pid != -1)
1896 kill(child_pid, SIGTERM);
1897
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001898 sigprocmask(SIG_SETMASK, &oset, NULL);
1899
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001900 if (signr == -1)
1901 return;
1902
1903 signal(signr, SIG_DFL);
1904 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001905}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001906
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001907static int stat__set_big_num(const struct option *opt __maybe_unused,
1908 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001909{
1910 big_num_opt = unset ? 0 : 1;
1911 return 0;
1912}
1913
Andi Kleen44b1e602016-05-30 12:49:42 -03001914static int enable_metric_only(const struct option *opt __maybe_unused,
1915 const char *s __maybe_unused, int unset)
1916{
1917 force_metric_only = true;
1918 metric_only = !unset;
1919 return 0;
1920}
1921
Andi Kleenb18f3e32017-08-31 12:40:31 -07001922static int parse_metric_groups(const struct option *opt,
1923 const char *str,
1924 int unset __maybe_unused)
1925{
1926 return metricgroup__parse_groups(opt, str, &metric_events);
1927}
1928
Jiri Olsae0547312015-11-05 15:40:45 +01001929static const struct option stat_options[] = {
1930 OPT_BOOLEAN('T', "transaction", &transaction_run,
1931 "hardware transaction statistics"),
1932 OPT_CALLBACK('e', "event", &evsel_list, "event",
1933 "event selector. use 'perf list' to list available events",
1934 parse_events_option),
1935 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1936 "event filter", parse_filter),
1937 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1938 "child tasks do not inherit counters"),
1939 OPT_STRING('p', "pid", &target.pid, "pid",
1940 "stat events on existing process id"),
1941 OPT_STRING('t', "tid", &target.tid, "tid",
1942 "stat events on existing thread id"),
1943 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1944 "system-wide collection from all CPUs"),
1945 OPT_BOOLEAN('g', "group", &group,
1946 "put the counters into a counter group"),
1947 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1948 OPT_INCR('v', "verbose", &verbose,
1949 "be more verbose (show counter open errors, etc)"),
1950 OPT_INTEGER('r', "repeat", &run_count,
1951 "repeat command and print average + stddev (max: 100, forever: 0)"),
1952 OPT_BOOLEAN('n', "null", &null_run,
1953 "null run - dont start any counters"),
1954 OPT_INCR('d', "detailed", &detailed_run,
1955 "detailed run - start a lot of events"),
1956 OPT_BOOLEAN('S', "sync", &sync_run,
1957 "call sync() before starting a run"),
1958 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1959 "print large numbers with thousands\' separators",
1960 stat__set_big_num),
1961 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1962 "list of cpus to monitor in system-wide"),
1963 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1964 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001965 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001966 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1967 "print counts with custom separator"),
1968 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1969 "monitor event in cgroup name only", parse_cgroups),
1970 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1971 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1972 OPT_INTEGER(0, "log-fd", &output_fd,
1973 "log output to fd, instead of stderr"),
1974 OPT_STRING(0, "pre", &pre_cmd, "command",
1975 "command to run prior to the measured command"),
1976 OPT_STRING(0, "post", &post_cmd, "command",
1977 "command to run after to the measured command"),
1978 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
Alexey Budankov9dc9a952018-04-03 21:18:33 +03001979 "print counts at regular interval in ms "
1980 "(overhead is possible for values <= 100ms)"),
yuzhoujiandb06a262018-01-29 10:25:22 +01001981 OPT_INTEGER(0, "interval-count", &stat_config.times,
1982 "print counts for fixed number of times"),
yuzhoujianf1f8ad52018-01-29 10:25:23 +01001983 OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1984 "stop workload and print counts after a timeout period in ms (>= 10ms)"),
Jiri Olsae0547312015-11-05 15:40:45 +01001985 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1986 "aggregate counts per processor socket", AGGR_SOCKET),
1987 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1988 "aggregate counts per physical processor core", AGGR_CORE),
1989 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1990 "aggregate counts per thread", AGGR_THREAD),
1991 OPT_UINTEGER('D', "delay", &initial_delay,
1992 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001993 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1994 "Only print computed metrics. No raw values", enable_metric_only),
1995 OPT_BOOLEAN(0, "topdown", &topdown_run,
1996 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001997 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1998 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001999 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
2000 "monitor specified metrics or metric groups (separated by ,)",
2001 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01002002 OPT_END()
2003};
2004
Jiri Olsa1fe7a302015-10-16 12:41:15 +02002005static int perf_stat__get_socket(struct cpu_map *map, int cpu)
2006{
2007 return cpu_map__get_socket(map, cpu, NULL);
2008}
2009
2010static int perf_stat__get_core(struct cpu_map *map, int cpu)
2011{
2012 return cpu_map__get_core(map, cpu, NULL);
2013}
2014
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002015static int cpu_map__get_max(struct cpu_map *map)
2016{
2017 int i, max = -1;
2018
2019 for (i = 0; i < map->nr; i++) {
2020 if (map->map[i] > max)
2021 max = map->map[i];
2022 }
2023
2024 return max;
2025}
2026
2027static struct cpu_map *cpus_aggr_map;
2028
2029static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
2030{
2031 int cpu;
2032
2033 if (idx >= map->nr)
2034 return -1;
2035
2036 cpu = map->map[idx];
2037
2038 if (cpus_aggr_map->map[cpu] == -1)
2039 cpus_aggr_map->map[cpu] = get_id(map, idx);
2040
2041 return cpus_aggr_map->map[cpu];
2042}
2043
2044static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
2045{
2046 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
2047}
2048
2049static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
2050{
2051 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
2052}
2053
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002054static int perf_stat_init_aggr_mode(void)
2055{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002056 int nr;
2057
Jiri Olsa421a50f2015-07-21 14:31:22 +02002058 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002059 case AGGR_SOCKET:
2060 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
2061 perror("cannot build socket map");
2062 return -1;
2063 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002064 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002065 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002066 case AGGR_CORE:
2067 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
2068 perror("cannot build core map");
2069 return -1;
2070 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002071 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01002072 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002073 case AGGR_NONE:
2074 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02002075 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02002076 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002077 default:
2078 break;
2079 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01002080
2081 /*
2082 * The evsel_list->cpus is the base we operate on,
2083 * taking the highest cpu number to be the size of
2084 * the aggregation translate cpumap.
2085 */
2086 nr = cpu_map__get_max(evsel_list->cpus);
2087 cpus_aggr_map = cpu_map__empty_new(nr + 1);
2088 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002089}
2090
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002091static void perf_stat__exit_aggr_mode(void)
2092{
2093 cpu_map__put(aggr_map);
2094 cpu_map__put(cpus_aggr_map);
2095 aggr_map = NULL;
2096 cpus_aggr_map = NULL;
2097}
2098
Jiri Olsa68d702f2015-11-05 15:40:58 +01002099static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
2100{
2101 int cpu;
2102
2103 if (idx > map->nr)
2104 return -1;
2105
2106 cpu = map->map[idx];
2107
Jan Stancekda8a58b2017-02-17 12:10:26 +01002108 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002109 return -1;
2110
2111 return cpu;
2112}
2113
2114static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2115{
2116 struct perf_env *env = data;
2117 int cpu = perf_env__get_cpu(env, map, idx);
2118
2119 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2120}
2121
2122static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2123{
2124 struct perf_env *env = data;
2125 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2126
2127 if (cpu != -1) {
2128 int socket_id = env->cpu[cpu].socket_id;
2129
2130 /*
2131 * Encode socket in upper 16 bits
2132 * core_id is relative to socket, and
2133 * we need a global id. So we combine
2134 * socket + core id.
2135 */
2136 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2137 }
2138
2139 return core;
2140}
2141
2142static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2143 struct cpu_map **sockp)
2144{
2145 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2146}
2147
2148static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2149 struct cpu_map **corep)
2150{
2151 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2152}
2153
2154static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2155{
2156 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2157}
2158
2159static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2160{
2161 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2162}
2163
2164static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2165{
2166 struct perf_env *env = &st->session->header.env;
2167
2168 switch (stat_config.aggr_mode) {
2169 case AGGR_SOCKET:
2170 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2171 perror("cannot build socket map");
2172 return -1;
2173 }
2174 aggr_get_id = perf_stat__get_socket_file;
2175 break;
2176 case AGGR_CORE:
2177 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2178 perror("cannot build core map");
2179 return -1;
2180 }
2181 aggr_get_id = perf_stat__get_core_file;
2182 break;
2183 case AGGR_NONE:
2184 case AGGR_GLOBAL:
2185 case AGGR_THREAD:
2186 case AGGR_UNSET:
2187 default:
2188 break;
2189 }
2190
2191 return 0;
2192}
2193
Andi Kleen44b1e602016-05-30 12:49:42 -03002194static int topdown_filter_events(const char **attr, char **str, bool use_group)
2195{
2196 int off = 0;
2197 int i;
2198 int len = 0;
2199 char *s;
2200
2201 for (i = 0; attr[i]; i++) {
2202 if (pmu_have_event("cpu", attr[i])) {
2203 len += strlen(attr[i]) + 1;
2204 attr[i - off] = attr[i];
2205 } else
2206 off++;
2207 }
2208 attr[i - off] = NULL;
2209
2210 *str = malloc(len + 1 + 2);
2211 if (!*str)
2212 return -1;
2213 s = *str;
2214 if (i - off == 0) {
2215 *s = 0;
2216 return 0;
2217 }
2218 if (use_group)
2219 *s++ = '{';
2220 for (i = 0; attr[i]; i++) {
2221 strcpy(s, attr[i]);
2222 s += strlen(s);
2223 *s++ = ',';
2224 }
2225 if (use_group) {
2226 s[-1] = '}';
2227 *s = 0;
2228 } else
2229 s[-1] = 0;
2230 return 0;
2231}
2232
2233__weak bool arch_topdown_check_group(bool *warn)
2234{
2235 *warn = false;
2236 return false;
2237}
2238
2239__weak void arch_topdown_group_warn(void)
2240{
2241}
2242
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002243/*
2244 * Add default attributes, if there were no attributes specified or
2245 * if -d/--detailed, -d -d or -d -d -d is used:
2246 */
2247static int add_default_attributes(void)
2248{
Andi Kleen44b1e602016-05-30 12:49:42 -03002249 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002250 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002251
2252 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2253 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2254 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2255 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2256
2257 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002258};
2259 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002260 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002261};
2262 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002263 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002264};
2265 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002266 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2267 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2268 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2269
2270};
2271
2272/*
2273 * Detailed stats (-d), covering the L1 and last level data caches:
2274 */
2275 struct perf_event_attr detailed_attrs[] = {
2276
2277 { .type = PERF_TYPE_HW_CACHE,
2278 .config =
2279 PERF_COUNT_HW_CACHE_L1D << 0 |
2280 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2281 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2282
2283 { .type = PERF_TYPE_HW_CACHE,
2284 .config =
2285 PERF_COUNT_HW_CACHE_L1D << 0 |
2286 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2287 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2288
2289 { .type = PERF_TYPE_HW_CACHE,
2290 .config =
2291 PERF_COUNT_HW_CACHE_LL << 0 |
2292 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2293 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2294
2295 { .type = PERF_TYPE_HW_CACHE,
2296 .config =
2297 PERF_COUNT_HW_CACHE_LL << 0 |
2298 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2299 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2300};
2301
2302/*
2303 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2304 */
2305 struct perf_event_attr very_detailed_attrs[] = {
2306
2307 { .type = PERF_TYPE_HW_CACHE,
2308 .config =
2309 PERF_COUNT_HW_CACHE_L1I << 0 |
2310 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2311 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2312
2313 { .type = PERF_TYPE_HW_CACHE,
2314 .config =
2315 PERF_COUNT_HW_CACHE_L1I << 0 |
2316 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2317 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2318
2319 { .type = PERF_TYPE_HW_CACHE,
2320 .config =
2321 PERF_COUNT_HW_CACHE_DTLB << 0 |
2322 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2323 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2324
2325 { .type = PERF_TYPE_HW_CACHE,
2326 .config =
2327 PERF_COUNT_HW_CACHE_DTLB << 0 |
2328 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2329 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2330
2331 { .type = PERF_TYPE_HW_CACHE,
2332 .config =
2333 PERF_COUNT_HW_CACHE_ITLB << 0 |
2334 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2335 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2336
2337 { .type = PERF_TYPE_HW_CACHE,
2338 .config =
2339 PERF_COUNT_HW_CACHE_ITLB << 0 |
2340 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2341 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2342
2343};
2344
2345/*
2346 * Very, very detailed stats (-d -d -d), adding prefetch events:
2347 */
2348 struct perf_event_attr very_very_detailed_attrs[] = {
2349
2350 { .type = PERF_TYPE_HW_CACHE,
2351 .config =
2352 PERF_COUNT_HW_CACHE_L1D << 0 |
2353 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2354 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2355
2356 { .type = PERF_TYPE_HW_CACHE,
2357 .config =
2358 PERF_COUNT_HW_CACHE_L1D << 0 |
2359 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2360 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2361};
2362
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002363 /* Set attrs if no event is selected and !null_run: */
2364 if (null_run)
2365 return 0;
2366
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002367 if (transaction_run) {
Thomas Richterfca323402018-03-08 15:57:35 +01002368 struct parse_events_error errinfo;
2369
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002370 if (pmu_have_event("cpu", "cycles-ct") &&
2371 pmu_have_event("cpu", "el-start"))
Thomas Richterfca323402018-03-08 15:57:35 +01002372 err = parse_events(evsel_list, transaction_attrs,
2373 &errinfo);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002374 else
Thomas Richterfca323402018-03-08 15:57:35 +01002375 err = parse_events(evsel_list,
2376 transaction_limited_attrs,
2377 &errinfo);
Jiri Olsaa4547422015-06-03 16:25:53 +02002378 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002379 fprintf(stderr, "Cannot set up transaction events\n");
2380 return -1;
2381 }
2382 return 0;
2383 }
2384
Kan Liangdaefd0b2017-05-26 12:05:38 -07002385 if (smi_cost) {
2386 int smi;
2387
2388 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2389 fprintf(stderr, "freeze_on_smi is not supported.\n");
2390 return -1;
2391 }
2392
2393 if (!smi) {
2394 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2395 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2396 return -1;
2397 }
2398 smi_reset = true;
2399 }
2400
2401 if (pmu_have_event("msr", "aperf") &&
2402 pmu_have_event("msr", "smi")) {
2403 if (!force_metric_only)
2404 metric_only = true;
2405 err = parse_events(evsel_list, smi_cost_attrs, NULL);
2406 } else {
2407 fprintf(stderr, "To measure SMI cost, it needs "
2408 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2409 return -1;
2410 }
2411 if (err) {
2412 fprintf(stderr, "Cannot set up SMI cost events\n");
2413 return -1;
2414 }
2415 return 0;
2416 }
2417
Andi Kleen44b1e602016-05-30 12:49:42 -03002418 if (topdown_run) {
2419 char *str = NULL;
2420 bool warn = false;
2421
2422 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2423 stat_config.aggr_mode != AGGR_CORE) {
2424 pr_err("top down event configuration requires --per-core mode\n");
2425 return -1;
2426 }
2427 stat_config.aggr_mode = AGGR_CORE;
2428 if (nr_cgroups || !target__has_cpu(&target)) {
2429 pr_err("top down event configuration requires system-wide mode (-a)\n");
2430 return -1;
2431 }
2432
2433 if (!force_metric_only)
2434 metric_only = true;
2435 if (topdown_filter_events(topdown_attrs, &str,
2436 arch_topdown_check_group(&warn)) < 0) {
2437 pr_err("Out of memory\n");
2438 return -1;
2439 }
2440 if (topdown_attrs[0] && str) {
2441 if (warn)
2442 arch_topdown_group_warn();
2443 err = parse_events(evsel_list, str, NULL);
2444 if (err) {
2445 fprintf(stderr,
2446 "Cannot set up top down events %s: %d\n",
2447 str, err);
2448 free(str);
2449 return -1;
2450 }
2451 } else {
2452 fprintf(stderr, "System does not support topdown\n");
2453 return -1;
2454 }
2455 free(str);
2456 }
2457
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002458 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002459 if (target__has_cpu(&target))
2460 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2461
Andi Kleen9dec4472016-02-26 16:27:56 -08002462 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2463 return -1;
2464 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2465 if (perf_evlist__add_default_attrs(evsel_list,
2466 frontend_attrs) < 0)
2467 return -1;
2468 }
2469 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2470 if (perf_evlist__add_default_attrs(evsel_list,
2471 backend_attrs) < 0)
2472 return -1;
2473 }
2474 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002475 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002476 }
2477
2478 /* Detailed events get appended to the event list: */
2479
2480 if (detailed_run < 1)
2481 return 0;
2482
2483 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002484 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002485 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002486
2487 if (detailed_run < 2)
2488 return 0;
2489
2490 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002491 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002492 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002493
2494 if (detailed_run < 3)
2495 return 0;
2496
2497 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002498 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002499}
2500
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002501static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002502 "perf stat record [<options>]",
2503 NULL,
2504};
2505
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002506static void init_features(struct perf_session *session)
2507{
2508 int feat;
2509
2510 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2511 perf_header__set_feat(&session->header, feat);
2512
2513 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2514 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2515 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2516 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2517}
2518
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002519static int __cmd_record(int argc, const char **argv)
2520{
2521 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002522 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002523
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002524 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002525 PARSE_OPT_STOP_AT_NON_OPTION);
2526
2527 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002528 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002529
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002530 if (run_count != 1 || forever) {
2531 pr_err("Cannot use -r option with perf stat record.\n");
2532 return -1;
2533 }
2534
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002535 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002536 if (session == NULL) {
2537 pr_err("Perf session creation failed.\n");
2538 return -1;
2539 }
2540
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002541 init_features(session);
2542
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002543 session->evlist = evsel_list;
2544 perf_stat.session = session;
2545 perf_stat.record = true;
2546 return argc;
2547}
2548
Jiri Olsaa56f9392015-11-05 15:40:59 +01002549static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2550 union perf_event *event,
2551 struct perf_session *session)
2552{
Andi Kleene3b03b62016-05-05 16:04:03 -07002553 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002554 struct perf_evsel *counter;
2555 struct timespec tsh, *ts = NULL;
2556 const char **argv = session->header.env.cmdline_argv;
2557 int argc = session->header.env.nr_cmdline;
2558
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002559 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002560 perf_stat_process_counter(&stat_config, counter);
2561
Andi Kleene3b03b62016-05-05 16:04:03 -07002562 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2563 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002564
Andi Kleene3b03b62016-05-05 16:04:03 -07002565 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002566 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2567 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002568 ts = &tsh;
2569 }
2570
2571 print_counters(ts, argc, argv);
2572 return 0;
2573}
2574
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002575static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002576int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002577 union perf_event *event,
2578 struct perf_session *session __maybe_unused)
2579{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002580 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2581
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002582 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002583
Jiri Olsa89af4e02015-11-05 15:41:02 +01002584 if (cpu_map__empty(st->cpus)) {
2585 if (st->aggr_mode != AGGR_UNSET)
2586 pr_warning("warning: processing task data, aggregation mode not set\n");
2587 return 0;
2588 }
2589
2590 if (st->aggr_mode != AGGR_UNSET)
2591 stat_config.aggr_mode = st->aggr_mode;
2592
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002593 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002594 perf_stat_init_aggr_mode();
2595 else
2596 perf_stat_init_aggr_mode_file(st);
2597
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002598 return 0;
2599}
2600
Jiri Olsa1975d362015-11-05 15:40:56 +01002601static int set_maps(struct perf_stat *st)
2602{
2603 if (!st->cpus || !st->threads)
2604 return 0;
2605
2606 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2607 return -EINVAL;
2608
2609 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2610
2611 if (perf_evlist__alloc_stats(evsel_list, true))
2612 return -ENOMEM;
2613
2614 st->maps_allocated = true;
2615 return 0;
2616}
2617
2618static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002619int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002620 union perf_event *event,
2621 struct perf_session *session __maybe_unused)
2622{
2623 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2624
2625 if (st->threads) {
2626 pr_warning("Extra thread map event, ignoring.\n");
2627 return 0;
2628 }
2629
2630 st->threads = thread_map__new_event(&event->thread_map);
2631 if (!st->threads)
2632 return -ENOMEM;
2633
2634 return set_maps(st);
2635}
2636
2637static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002638int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002639 union perf_event *event,
2640 struct perf_session *session __maybe_unused)
2641{
2642 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2643 struct cpu_map *cpus;
2644
2645 if (st->cpus) {
2646 pr_warning("Extra cpu map event, ignoring.\n");
2647 return 0;
2648 }
2649
2650 cpus = cpu_map__new_data(&event->cpu_map.data);
2651 if (!cpus)
2652 return -ENOMEM;
2653
2654 st->cpus = cpus;
2655 return set_maps(st);
2656}
2657
Jin Yao56739442017-12-05 22:03:07 +08002658static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2659{
2660 int i;
2661
2662 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2663 if (!config->stats)
2664 return -1;
2665
2666 config->stats_num = nthreads;
2667
2668 for (i = 0; i < nthreads; i++)
2669 runtime_stat__init(&config->stats[i]);
2670
2671 return 0;
2672}
2673
2674static void runtime_stat_delete(struct perf_stat_config *config)
2675{
2676 int i;
2677
2678 if (!config->stats)
2679 return;
2680
2681 for (i = 0; i < config->stats_num; i++)
2682 runtime_stat__exit(&config->stats[i]);
2683
2684 free(config->stats);
2685}
2686
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002687static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002688 "perf stat report [<options>]",
2689 NULL,
2690};
2691
2692static struct perf_stat perf_stat = {
2693 .tool = {
2694 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002695 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002696 .thread_map = process_thread_map_event,
2697 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18ba2015-11-05 15:40:57 +01002698 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002699 .stat = perf_event__process_stat_event,
2700 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002701 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002702 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002703};
2704
2705static int __cmd_report(int argc, const char **argv)
2706{
2707 struct perf_session *session;
2708 const struct option options[] = {
2709 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002710 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2711 "aggregate counts per processor socket", AGGR_SOCKET),
2712 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2713 "aggregate counts per physical processor core", AGGR_CORE),
2714 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2715 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002716 OPT_END()
2717 };
2718 struct stat st;
2719 int ret;
2720
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002721 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002722
2723 if (!input_name || !strlen(input_name)) {
2724 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2725 input_name = "-";
2726 else
2727 input_name = "perf.data";
2728 }
2729
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002730 perf_stat.data.file.path = input_name;
2731 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002732
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002733 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002734 if (session == NULL)
2735 return -1;
2736
2737 perf_stat.session = session;
2738 stat_config.output = stderr;
2739 evsel_list = session->evlist;
2740
2741 ret = perf_session__process_events(session);
2742 if (ret)
2743 return ret;
2744
2745 perf_session__delete(session);
2746 return 0;
2747}
2748
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002749static void setup_system_wide(int forks)
2750{
2751 /*
2752 * Make system wide (-a) the default target if
2753 * no target was specified and one of following
2754 * conditions is met:
2755 *
2756 * - there's no workload specified
2757 * - there is workload specified but all requested
2758 * events are system wide events
2759 */
2760 if (!target__none(&target))
2761 return;
2762
2763 if (!forks)
2764 target.system_wide = true;
2765 else {
2766 struct perf_evsel *counter;
2767
2768 evlist__for_each_entry(evsel_list, counter) {
2769 if (!counter->system_wide)
2770 return;
2771 }
2772
2773 if (evsel_list->nr_entries)
2774 target.system_wide = true;
2775 }
2776}
2777
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002778int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002779{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03002780 const char * const stat_usage[] = {
2781 "perf stat [<options>] [<command>]",
2782 NULL
2783 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002784 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002785 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002786 FILE *output = stderr;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002787 unsigned int interval, timeout;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002788 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002789
Stephane Eranian5af52b52010-05-18 15:00:01 +02002790 setlocale(LC_ALL, "");
2791
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002792 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002793 if (evsel_list == NULL)
2794 return -ENOMEM;
2795
Wang Nan1669e502016-02-19 11:43:58 +00002796 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002797 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2798 (const char **) stat_usage,
2799 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002800 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002801 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002802
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002803 if (csv_sep) {
2804 csv_output = true;
2805 if (!strcmp(csv_sep, "\\t"))
2806 csv_sep = "\t";
2807 } else
2808 csv_sep = DEFAULT_SEPARATOR;
2809
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002810 if (argc && !strncmp(argv[0], "rec", 3)) {
2811 argc = __cmd_record(argc, argv);
2812 if (argc < 0)
2813 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002814 } else if (argc && !strncmp(argv[0], "rep", 3))
2815 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002816
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002817 interval = stat_config.interval;
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002818 timeout = stat_config.timeout;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002819
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002820 /*
2821 * For record command the -o is already taken care of.
2822 */
2823 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002824 output = NULL;
2825
Jim Cromie56f3bae2011-09-07 17:14:00 -06002826 if (output_name && output_fd) {
2827 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002828 parse_options_usage(stat_usage, stat_options, "o", 1);
2829 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002830 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002831 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002832
Andi Kleen54b50912016-03-03 15:57:36 -08002833 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2834 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2835 goto out;
2836 }
2837
Andi Kleen54b50912016-03-03 15:57:36 -08002838 if (metric_only && run_count > 1) {
2839 fprintf(stderr, "--metric-only is not supported with -r\n");
2840 goto out;
2841 }
2842
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002843 if (output_fd < 0) {
2844 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002845 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002846 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002847 }
2848
Stephane Eranian4aa90152011-08-15 22:22:33 +02002849 if (!output) {
2850 struct timespec tm;
2851 mode = append_file ? "a" : "w";
2852
2853 output = fopen(output_name, mode);
2854 if (!output) {
2855 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002856 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002857 }
2858 clock_gettime(CLOCK_REALTIME, &tm);
2859 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002860 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002861 mode = append_file ? "a" : "w";
2862 output = fdopen(output_fd, mode);
2863 if (!output) {
2864 perror("Failed opening logfd");
2865 return -errno;
2866 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002867 }
2868
Jiri Olsa58215222015-07-21 14:31:24 +02002869 stat_config.output = output;
2870
Stephane Eraniand7470b62010-12-01 18:49:05 +02002871 /*
2872 * let the spreadsheet do the pretty-printing
2873 */
2874 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002875 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002876 if (big_num_opt == 1) {
2877 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002878 parse_options_usage(stat_usage, stat_options, "B", 1);
2879 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002880 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002881 } else /* Nope, so disable big number formatting */
2882 big_num = false;
2883 } else if (big_num_opt == 0) /* User passed --no-big-num */
2884 big_num = false;
2885
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002886 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002887
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002888 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002889 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002890 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002891 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002892 } else if (run_count == 0) {
2893 forever = true;
2894 run_count = 1;
2895 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002896
Jin Yao1d9f8d12017-12-05 22:03:10 +08002897 if ((stat_config.aggr_mode == AGGR_THREAD) &&
2898 !target__has_task(&target)) {
2899 if (!target.system_wide || target.cpu_list) {
2900 fprintf(stderr, "The --per-thread option is only "
2901 "available when monitoring via -p -t -a "
2902 "options or only --per-thread.\n");
2903 parse_options_usage(NULL, stat_options, "p", 1);
2904 parse_options_usage(NULL, stat_options, "t", 1);
2905 goto out;
2906 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002907 }
2908
2909 /*
2910 * no_aggr, cgroup are for system-wide only
2911 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2912 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002913 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2914 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002915 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002916 fprintf(stderr, "both cgroup and no-aggregation "
2917 "modes only available in system-wide mode\n");
2918
Jiri Olsae0547312015-11-05 15:40:45 +01002919 parse_options_usage(stat_usage, stat_options, "G", 1);
2920 parse_options_usage(NULL, stat_options, "A", 1);
2921 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002922 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002923 }
2924
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002925 if (add_default_attributes())
2926 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002927
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002928 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002929
Jin Yao1d9f8d12017-12-05 22:03:10 +08002930 if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2931 target.per_thread = true;
2932
Namhyung Kim77a6f012012-05-07 14:09:04 +09002933 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002934 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002935 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002936 parse_options_usage(stat_usage, stat_options, "p", 1);
2937 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002938 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002939 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002940 parse_options_usage(stat_usage, stat_options, "C", 1);
2941 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002942 }
2943 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002944 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002945
2946 /*
2947 * Initialize thread_map with comm names,
2948 * so we could print it out on output.
2949 */
Jin Yao56739442017-12-05 22:03:07 +08002950 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002951 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08002952 if (target.system_wide) {
2953 if (runtime_stat_new(&stat_config,
2954 thread_map__nr(evsel_list->threads))) {
2955 goto out;
2956 }
2957 }
2958 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002959
yuzhoujiandb06a262018-01-29 10:25:22 +01002960 if (stat_config.times && interval)
2961 interval_count = true;
2962 else if (stat_config.times && !interval) {
2963 pr_err("interval-count option should be used together with "
2964 "interval-print.\n");
2965 parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2966 parse_options_usage(stat_usage, stat_options, "I", 1);
2967 goto out;
2968 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002969
yuzhoujianf1f8ad52018-01-29 10:25:23 +01002970 if (timeout && timeout < 100) {
2971 if (timeout < 10) {
2972 pr_err("timeout must be >= 10ms.\n");
2973 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2974 goto out;
2975 } else
2976 pr_warning("timeout < 100ms. "
2977 "The overhead percentage could be high in some cases. "
2978 "Please proceed with caution.\n");
2979 }
2980 if (timeout && interval) {
2981 pr_err("timeout option is not supported with interval-print.\n");
2982 parse_options_usage(stat_usage, stat_options, "timeout", 0);
2983 parse_options_usage(stat_usage, stat_options, "I", 1);
2984 goto out;
2985 }
2986
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002987 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002988 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002989
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002990 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002991 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002992
Ingo Molnar58d7e992009-05-15 11:03:23 +02002993 /*
2994 * We dont want to block the signals - that would cause
2995 * child tasks to inherit that and Ctrl-C would not work.
2996 * What we want is for Ctrl-C to work in the exec()-ed
2997 * task, but being ignored by perf stat itself:
2998 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002999 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003000 if (!forever)
3001 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01003002 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02003003 signal(SIGALRM, skip_signal);
3004 signal(SIGABRT, skip_signal);
3005
Ingo Molnar42202dd2009-06-13 14:57:28 +02003006 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003007 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09003008 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02003009 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
3010 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02003011
Ingo Molnar42202dd2009-06-13 14:57:28 +02003012 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003013 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02003014 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02003015 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003016 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02003017 }
3018
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05003019 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02003020 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003021
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003022 if (STAT_RECORD) {
3023 /*
3024 * We synthesize the kernel mmap record just so that older tools
3025 * don't emit warnings about not being able to resolve symbols
3026 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
3027 * a saner message about no samples being in the perf.data file.
3028 *
3029 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01003030 * in header.c at the moment 'perf stat record' gets introduced, which
3031 * is not really needed once we start adding the stat specific PERF_RECORD_
3032 * records, but the need to suppress the kptr_restrict messages in older
3033 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003034 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003035 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003036 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
3037 process_synthesized_event,
3038 &perf_stat.session->machines.host);
3039 if (err) {
3040 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
3041 "older tools may produce warnings about this file\n.");
3042 }
3043
Jiri Olsa7aad0c32015-11-05 15:40:52 +01003044 if (!interval) {
3045 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
3046 pr_err("failed to write stat round event\n");
3047 }
3048
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01003049 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01003050 perf_stat.session->header.data_size += perf_stat.bytes_written;
3051 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3052 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01003053
3054 perf_session__delete(perf_stat.session);
3055 }
3056
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09003057 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03003058 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003059out:
Kan Liangdaefd0b2017-05-26 12:05:38 -07003060 if (smi_cost && smi_reset)
3061 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3062
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02003063 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08003064
3065 runtime_stat_delete(&stat_config);
3066
Ingo Molnar42202dd2009-06-13 14:57:28 +02003067 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003068}