blob: 4c492ac3ac07171b8d3f41d01cee2018b2a3641d [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"
Andi Kleen44b1e602016-05-30 12:49:42 -030066#include "util/group.h"
Arnaldo Carvalho de Meloa0675582017-04-17 16:51:59 -030067#include "util/string2.h"
Andi Kleenb18f3e32017-08-31 12:40:31 -070068#include "util/metricgroup.h"
Jiri Olsaba6039b62015-11-05 15:40:55 +010069#include "asm/bug.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020070
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030071#include <linux/time64.h>
Andi Kleen44b1e602016-05-30 12:49:42 -030072#include <api/fs/fs.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030073#include <errno.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030074#include <signal.h>
Peter Zijlstra1f16c572012-10-23 13:40:14 +020075#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020076#include <sys/prctl.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030077#include <inttypes.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020078#include <locale.h>
Andi Kleene3b03b62016-05-05 16:04:03 -070079#include <math.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030080#include <sys/types.h>
81#include <sys/stat.h>
Arnaldo Carvalho de Melo42087352017-04-19 19:06:30 -030082#include <sys/wait.h>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -030083#include <unistd.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020084
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030085#include "sane_ctype.h"
86
Stephane Eraniand7470b62010-12-01 18:49:05 +020087#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060088#define CNTR_NOT_SUPPORTED "<not supported>"
89#define CNTR_NOT_COUNTED "<not counted>"
Kan Liangdaefd0b2017-05-26 12:05:38 -070090#define FREEZE_ON_SMI_PATH "devices/cpu/freeze_on_smi"
Stephane Eraniand7470b62010-12-01 18:49:05 +020091
Jiri Olsad4f63a42015-06-26 11:29:26 +020092static void print_counters(struct timespec *ts, int argc, const char **argv);
Stephane Eranian13370a92013-01-29 12:47:44 +010093
Andi Kleen4cabc3d2013-08-21 16:47:26 -070094/* Default events used for perf stat -T */
Jiri Olsaa4547422015-06-03 16:25:53 +020095static const char *transaction_attrs = {
96 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -070097 "{"
98 "instructions,"
99 "cycles,"
100 "cpu/cycles-t/,"
101 "cpu/tx-start/,"
102 "cpu/el-start/,"
103 "cpu/cycles-ct/"
104 "}"
105};
106
107/* More limited version when the CPU does not have all events. */
Jiri Olsaa4547422015-06-03 16:25:53 +0200108static const char * transaction_limited_attrs = {
109 "task-clock,"
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700110 "{"
111 "instructions,"
112 "cycles,"
113 "cpu/cycles-t/,"
114 "cpu/tx-start/"
115 "}"
116};
117
Andi Kleen44b1e602016-05-30 12:49:42 -0300118static const char * topdown_attrs[] = {
119 "topdown-total-slots",
120 "topdown-slots-retired",
121 "topdown-recovery-bubbles",
122 "topdown-fetch-bubbles",
123 "topdown-slots-issued",
124 NULL,
125};
126
Kan Liangdaefd0b2017-05-26 12:05:38 -0700127static const char *smi_cost_attrs = {
128 "{"
129 "msr/aperf/,"
130 "msr/smi/,"
131 "cycles"
132 "}"
133};
134
Robert Richter666e6d42012-04-05 18:26:27 +0200135static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200136
Andi Kleenb18f3e32017-08-31 12:40:31 -0700137static struct rblist metric_events;
138
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300139static struct target target = {
Namhyung Kim77a6f012012-05-07 14:09:04 +0900140 .uid = UINT_MAX,
141};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530142
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100143typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
144
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530145static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200146static bool no_inherit = false;
Stephane Eraniand07f0b12013-06-04 17:44:26 +0200147static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000148static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200149static int detailed_run = 0;
Andi Kleen4cabc3d2013-08-21 16:47:26 -0700150static bool transaction_run;
Andi Kleen44b1e602016-05-30 12:49:42 -0300151static bool topdown_run = false;
Kan Liangdaefd0b2017-05-26 12:05:38 -0700152static bool smi_cost = false;
153static bool smi_reset = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200154static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200155static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200156static const char *csv_sep = NULL;
157static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800158static bool group = false;
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200159static const char *pre_cmd = NULL;
160static const char *post_cmd = NULL;
161static bool sync_run = false;
Andi Kleen41191682013-08-02 17:41:11 -0700162static unsigned int initial_delay = 0;
Stephane Eranian410136f2013-11-12 17:58:49 +0100163static unsigned int unit_width = 4; /* strlen("unit") */
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500164static bool forever = false;
Andi Kleen54b50912016-03-03 15:57:36 -0800165static bool metric_only = false;
Andi Kleen44b1e602016-05-30 12:49:42 -0300166static bool force_metric_only = false;
Andi Kleen430daf22017-03-20 13:17:00 -0700167static bool no_merge = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100168static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100169static struct cpu_map *aggr_map;
Jiri Olsa1e5a2932015-10-25 15:51:18 +0100170static aggr_get_id_t aggr_get_id;
Jiri Olsae0547312015-11-05 15:40:45 +0100171static bool append_file;
172static const char *output_name;
173static int output_fd;
Borislav Petkov02d492e2017-02-07 01:40:05 +0100174static int print_free_counters_hint;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200175
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100176struct perf_stat {
177 bool record;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100178 struct perf_data data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100179 struct perf_session *session;
180 u64 bytes_written;
Jiri Olsaba6039b62015-11-05 15:40:55 +0100181 struct perf_tool tool;
Jiri Olsa1975d362015-11-05 15:40:56 +0100182 bool maps_allocated;
183 struct cpu_map *cpus;
184 struct thread_map *threads;
Jiri Olsa89af4e02015-11-05 15:41:02 +0100185 enum aggr_mode aggr_mode;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100186};
187
188static struct perf_stat perf_stat;
189#define STAT_RECORD perf_stat.record
190
Liming Wang60666c62009-12-31 16:05:50 +0800191static volatile int done = 0;
192
Jiri Olsa421a50f2015-07-21 14:31:22 +0200193static struct perf_stat_config stat_config = {
194 .aggr_mode = AGGR_GLOBAL,
Jiri Olsa711a5722015-07-21 14:31:23 +0200195 .scale = true,
Jiri Olsa421a50f2015-07-21 14:31:22 +0200196};
197
Andi Kleene864c5c2017-08-31 12:40:35 -0700198static bool is_duration_time(struct perf_evsel *evsel)
199{
200 return !strcmp(evsel->name, "duration_time");
201}
202
Stephane Eranian13370a92013-01-29 12:47:44 +0100203static inline void diff_timespec(struct timespec *r, struct timespec *a,
204 struct timespec *b)
205{
206 r->tv_sec = a->tv_sec - b->tv_sec;
207 if (a->tv_nsec < b->tv_nsec) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300208 r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
Stephane Eranian13370a92013-01-29 12:47:44 +0100209 r->tv_sec--;
210 } else {
211 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
212 }
213}
214
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200215static void perf_stat__reset_stats(void)
216{
Jin Yao56739442017-12-05 22:03:07 +0800217 int i;
218
Jiri Olsa254ecbc72015-06-26 11:29:13 +0200219 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200220 perf_stat__reset_shadow_stats();
Jin Yao56739442017-12-05 22:03:07 +0800221
222 for (i = 0; i < stat_config.stats_num; i++)
223 perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200224}
225
Jiri Olsacac21422012-11-12 18:34:00 +0100226static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200227{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200228 struct perf_event_attr *attr = &evsel->attr;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200229 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200230
Jiri Olsa82bf3112017-07-26 14:02:06 +0200231 if (stat_config.scale) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200232 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
233 PERF_FORMAT_TOTAL_TIME_RUNNING;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200234 }
235
236 /*
237 * The event is part of non trivial group, let's enable
238 * the group read (for leader) and ID retrieval for all
239 * members.
240 */
241 if (leader->nr_members > 1)
242 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200243
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200244 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300245
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100246 /*
247 * Some events get initialized with sample_(period/type) set,
248 * like tracepoints. Clear it up for counting.
249 */
250 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100251
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100252 /*
253 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
254 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100255 *
256 * However for pipe sessions we need to keep it zero,
257 * because script's perf_evsel__check_attr is triggered
258 * by attr->sample_type != 0, and we can't run it on
259 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100260 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100261 if (!(STAT_RECORD && perf_stat.data.is_pipe))
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100262 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100263
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100264 /*
265 * Disabling all counters initially, they will be enabled
266 * either manually by us or by kernel via enable_on_exec
267 * set later.
268 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100269 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100270 attr->disabled = 1;
271
Jiri Olsac8280ce2015-12-03 10:06:45 +0100272 /*
273 * In case of initial_delay we enable tracee
274 * events manually.
275 */
276 if (target__none(&target) && !initial_delay)
277 attr->enable_on_exec = 1;
278 }
279
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300280 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300281 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200282
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300283 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200284}
285
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200286/*
287 * Does the counter have nsecs as a unit?
288 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200289static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200290{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200291 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
292 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200293 return 1;
294
295 return 0;
296}
297
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100298static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100299 union perf_event *event,
300 struct perf_sample *sample __maybe_unused,
301 struct machine *machine __maybe_unused)
302{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100303 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100304 pr_err("failed to write perf data, error: %m\n");
305 return -1;
306 }
307
308 perf_stat.bytes_written += event->header.size;
309 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100310}
311
Jiri Olsa1975d362015-11-05 15:40:56 +0100312static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100313{
Jiri Olsa1975d362015-11-05 15:40:56 +0100314 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100315 process_synthesized_event,
316 NULL);
317}
318
319#define WRITE_STAT_ROUND_EVENT(time, interval) \
320 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
321
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100322#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
323
324static int
325perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
326 struct perf_counts_values *count)
327{
328 struct perf_sample_id *sid = SID(counter, cpu, thread);
329
330 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
331 process_synthesized_event, NULL);
332}
333
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200334/*
335 * Read out the results of a single counter:
336 * do not aggregate counts across CPUs in system-wide mode
337 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200338static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200339{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100340 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100341 int ncpus, cpu, thread;
342
343 if (target__has_cpu(&target))
344 ncpus = perf_evsel__nr_cpus(counter);
345 else
346 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200347
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000348 if (!counter->supported)
349 return -ENOENT;
350
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100351 if (counter->system_wide)
352 nthreads = 1;
353
354 for (thread = 0; thread < nthreads; thread++) {
355 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200356 struct perf_counts_values *count;
357
358 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200359
360 /*
361 * The leader's group read loads data into its group members
362 * (via perf_evsel__read_counter) and sets threir count->loaded.
363 */
364 if (!count->loaded &&
365 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700366 counter->counts->scaled = -1;
367 perf_counts(counter->counts, cpu, thread)->ena = 0;
368 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100369 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700370 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100371
Jiri Olsa82bf3112017-07-26 14:02:06 +0200372 count->loaded = false;
373
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100374 if (STAT_RECORD) {
375 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
376 pr_err("failed to write stat event\n");
377 return -1;
378 }
379 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700380
381 if (verbose > 1) {
382 fprintf(stat_config.output,
383 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
384 perf_evsel__name(counter),
385 cpu,
386 count->val, count->ena, count->run);
387 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100388 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200389 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200390
391 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200392}
393
Mark Rutland3df33ef2016-08-09 14:04:29 +0100394static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200395{
396 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700397 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200398
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300399 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700400 ret = read_counter(counter);
401 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700402 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200403
Stephane Eraniandb49a712017-04-12 11:23:01 -0700404 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200405 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200406 }
407}
408
Jiri Olsaba411a92015-06-26 11:29:24 +0200409static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100410{
Stephane Eranian13370a92013-01-29 12:47:44 +0100411 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100412
Mark Rutland3df33ef2016-08-09 14:04:29 +0100413 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100414
Stephane Eranian13370a92013-01-29 12:47:44 +0100415 clock_gettime(CLOCK_MONOTONIC, &ts);
416 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100417
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100418 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300419 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100420 pr_err("failed to write stat round event\n");
421 }
422
Andi Kleenb90f1332017-08-31 12:40:36 -0700423 init_stats(&walltime_nsecs_stats);
424 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200425 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100426}
427
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100428static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700429{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100430 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300431 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100432
433 /*
434 * We need to enable counters only if:
435 * - we don't have tracee (attaching to task or cpu)
436 * - we have initial delay configured
437 */
438 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100439 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700440}
441
Mark Rutland3df33ef2016-08-09 14:04:29 +0100442static void disable_counters(void)
443{
444 /*
445 * If we don't have tracee (attaching to task or cpu), counters may
446 * still be running. To get accurate group ratios, we must stop groups
447 * from counting before reading their constituent counters.
448 */
449 if (!target__none(&target))
450 perf_evlist__disable(evsel_list);
451}
452
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300453static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300454
455/*
456 * perf_evlist__prepare_workload will send a SIGUSR1
457 * if the fork fails, since we asked by setting its
458 * want_signal to true.
459 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300460static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
461 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300462{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300463 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300464}
465
Jiri Olsa664c98d2015-11-05 15:40:50 +0100466static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100467{
468 int err;
469
Jiri Olsa664c98d2015-11-05 15:40:50 +0100470 if (is_pipe) {
471 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
472 process_synthesized_event);
473 if (err < 0) {
474 pr_err("Couldn't synthesize attrs.\n");
475 return err;
476 }
477 }
478
Andi Kleenbfd8f722017-11-17 13:42:58 -0800479 err = perf_event__synthesize_extra_attr(NULL,
480 evsel_list,
481 process_synthesized_event,
482 is_pipe);
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100483
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100484 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
485 process_synthesized_event,
486 NULL);
487 if (err < 0) {
488 pr_err("Couldn't synthesize thread map.\n");
489 return err;
490 }
491
492 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
493 process_synthesized_event, NULL);
494 if (err < 0) {
495 pr_err("Couldn't synthesize thread map.\n");
496 return err;
497 }
498
499 err = perf_event__synthesize_stat_config(NULL, &stat_config,
500 process_synthesized_event, NULL);
501 if (err < 0) {
502 pr_err("Couldn't synthesize config.\n");
503 return err;
504 }
505
506 return 0;
507}
508
Jiri Olsa2af46462015-11-05 15:40:49 +0100509#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
510
511static int __store_counter_ids(struct perf_evsel *counter,
512 struct cpu_map *cpus,
513 struct thread_map *threads)
514{
515 int cpu, thread;
516
517 for (cpu = 0; cpu < cpus->nr; cpu++) {
518 for (thread = 0; thread < threads->nr; thread++) {
519 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
538 return __store_counter_ids(counter, cpus, threads);
539}
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;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300575 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200576 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100577 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100578 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100579 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200580 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300581 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100582 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600583 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200584
Stephane Eranian13370a92013-01-29 12:47:44 +0100585 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300586 ts.tv_sec = interval / USEC_PER_MSEC;
587 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100588 } else {
589 ts.tv_sec = 1;
590 ts.tv_nsec = 0;
591 }
592
Liming Wang60666c62009-12-31 16:05:50 +0800593 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100594 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300595 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900596 perror("failed to prepare workload");
597 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800598 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900599 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000600 }
601
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200602 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300603 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200604
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300605 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300606try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100607 if (create_perf_stat_counter(counter) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700608
609 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700610 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700611 counter->leader != counter &&
612 counter->weak_group) {
613 counter = perf_evsel__reset_weak_group(counter);
614 goto try_again;
615 }
616
David Ahern979987a2012-05-08 09:29:16 -0600617 /*
618 * PPC returns ENXIO for HW counters until 2.6.37
619 * (behavior changed with commit b0a873e).
620 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100621 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600622 errno == ENOENT || errno == EOPNOTSUPP ||
623 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900624 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600625 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300626 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600627 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400628
629 if ((counter->leader != counter) ||
630 !(counter->leader->nr_members > 1))
631 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300632 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900633 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300634 ui__warning("%s\n", msg);
635 goto try_again;
636 }
Ingo Molnarede70292011-04-28 08:48:42 +0200637
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300638 perf_evsel__open_strerror(counter, &target,
639 errno, msg, sizeof(msg));
640 ui__error("%s\n", msg);
641
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200642 if (child_pid != -1)
643 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600644
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200645 return -1;
646 }
David Ahern2cee77c2011-05-30 08:55:59 -0600647 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100648
649 l = strlen(counter->unit);
650 if (l > unit_width)
651 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100652
Jiri Olsa82bf3112017-07-26 14:02:06 +0200653 if (perf_evsel__should_store_id(counter) &&
654 store_counter_ids(counter))
Jiri Olsa2af46462015-11-05 15:40:49 +0100655 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300656 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200657
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300658 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300659 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300660 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300661 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100662 return -1;
663 }
664
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600665 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300666 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600667 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
668 str_error_r(errno, msg, sizeof(msg)));
669 return -1;
670 }
671
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100672 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100673 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100674
Jiri Olsa664c98d2015-11-05 15:40:50 +0100675 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100676 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100677 } else {
678 err = perf_session__write_header(perf_stat.session, evsel_list,
679 fd, false);
680 }
681
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100682 if (err < 0)
683 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100684
Jiri Olsa664c98d2015-11-05 15:40:50 +0100685 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100686 if (err < 0)
687 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100688 }
689
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200690 /*
691 * Enable counters and exec the command:
692 */
693 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100694 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200695
Liming Wang60666c62009-12-31 16:05:50 +0800696 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900697 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100698 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900699
Stephane Eranian13370a92013-01-29 12:47:44 +0100700 if (interval) {
701 while (!waitpid(child_pid, &status, WNOHANG)) {
702 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200703 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100704 }
705 }
Milian Wolffdfc9eec2017-09-12 17:25:23 +0200706 waitpid(child_pid, &status, 0);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300707
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300708 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300709 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300710 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300711 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300712 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300713
Andi Kleen33e49ea2011-09-15 14:31:40 -0700714 if (WIFSIGNALED(status))
715 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800716 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100717 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100718 while (!done) {
719 nanosleep(&ts, NULL);
720 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200721 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100722 }
Liming Wang60666c62009-12-31 16:05:50 +0800723 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200724
Mark Rutland3df33ef2016-08-09 14:04:29 +0100725 disable_counters();
726
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200727 t1 = rdclock();
728
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200729 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200730
Mark Rutland3df33ef2016-08-09 14:04:29 +0100731 /*
732 * Closing a group leader splits the group, and as we only disable
733 * group leaders, results in remaining events becoming enabled. To
734 * avoid arbitrary skew, we must read all counters before closing any
735 * group leaders.
736 */
737 read_counters();
738 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200739
Ingo Molnar42202dd2009-06-13 14:57:28 +0200740 return WEXITSTATUS(status);
741}
742
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300743static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200744{
745 int ret;
746
747 if (pre_cmd) {
748 ret = system(pre_cmd);
749 if (ret)
750 return ret;
751 }
752
753 if (sync_run)
754 sync();
755
756 ret = __run_perf_stat(argc, argv);
757 if (ret)
758 return ret;
759
760 if (post_cmd) {
761 ret = system(post_cmd);
762 if (ret)
763 return ret;
764 }
765
766 return ret;
767}
768
Andi Kleend73515c2015-03-11 07:16:27 -0700769static void print_running(u64 run, u64 ena)
770{
771 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200772 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700773 csv_sep,
774 run,
775 csv_sep,
776 ena ? 100.0 * run / ena : 100.0);
777 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200778 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700779 }
780}
781
Ingo Molnarf99844c2011-04-27 05:35:39 +0200782static void print_noise_pct(double total, double avg)
783{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800784 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200785
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700786 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200787 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600788 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200789 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200790}
791
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200792static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200793{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200794 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200795
Peter Zijlstra849abde2009-09-04 18:23:38 +0200796 if (run_count == 1)
797 return;
798
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300799 ps = evsel->stats;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200800 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200801}
802
Stephane Eranian12c08a92013-02-14 13:57:29 +0100803static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200804{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200805 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100806 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200807 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100808 cpu_map__id_to_socket(id),
809 csv_output ? 0 : -8,
810 cpu_map__id_to_cpu(id),
811 csv_sep,
812 csv_output ? 0 : 4,
813 nr,
814 csv_sep);
815 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100816 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200817 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100818 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100819 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100820 csv_sep,
821 csv_output ? 0 : 4,
822 nr,
823 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100824 break;
825 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200826 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200827 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100828 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100829 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200830 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200831 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200832 csv_output ? 0 : 16,
833 thread_map__comm(evsel->threads, id),
834 csv_output ? 0 : -8,
835 thread_map__pid(evsel->threads, id),
836 csv_sep);
837 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100838 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200839 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100840 default:
841 break;
842 }
843}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200844
Andi Kleen140aead2016-01-30 09:06:49 -0800845struct outstate {
846 FILE *fh;
847 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800848 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800849 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800850 int id, nr;
851 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800852};
853
854#define METRIC_LEN 35
855
856static void new_line_std(void *ctx)
857{
858 struct outstate *os = ctx;
859
860 os->newline = true;
861}
862
863static void do_new_line_std(struct outstate *os)
864{
865 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800866 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800867 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800868 if (stat_config.aggr_mode == AGGR_NONE)
869 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800870 fprintf(os->fh, " ");
871}
872
873static void print_metric_std(void *ctx, const char *color, const char *fmt,
874 const char *unit, double val)
875{
876 struct outstate *os = ctx;
877 FILE *out = os->fh;
878 int n;
879 bool newline = os->newline;
880
881 os->newline = false;
882
883 if (unit == NULL || fmt == NULL) {
884 fprintf(out, "%-*s", METRIC_LEN, "");
885 return;
886 }
887
888 if (newline)
889 do_new_line_std(os);
890
891 n = fprintf(out, " # ");
892 if (color)
893 n += color_fprintf(out, color, fmt, val);
894 else
895 n += fprintf(out, fmt, val);
896 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
897}
898
Andi Kleen92a61f62016-02-29 14:36:21 -0800899static void new_line_csv(void *ctx)
900{
901 struct outstate *os = ctx;
902 int i;
903
904 fputc('\n', os->fh);
905 if (os->prefix)
906 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800907 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800908 for (i = 0; i < os->nfields; i++)
909 fputs(csv_sep, os->fh);
910}
911
912static void print_metric_csv(void *ctx,
913 const char *color __maybe_unused,
914 const char *fmt, const char *unit, double val)
915{
916 struct outstate *os = ctx;
917 FILE *out = os->fh;
918 char buf[64], *vals, *ends;
919
920 if (unit == NULL || fmt == NULL) {
921 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
922 return;
923 }
924 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900925 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800926 while (isdigit(*ends) || *ends == '.')
927 ends++;
928 *ends = 0;
929 while (isspace(*unit))
930 unit++;
931 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
932}
933
Andi Kleen54b50912016-03-03 15:57:36 -0800934#define METRIC_ONLY_LEN 20
935
936/* Filter out some columns that don't work well in metrics only mode */
937
938static bool valid_only_metric(const char *unit)
939{
940 if (!unit)
941 return false;
942 if (strstr(unit, "/sec") ||
943 strstr(unit, "hz") ||
944 strstr(unit, "Hz") ||
945 strstr(unit, "CPUs utilized"))
946 return false;
947 return true;
948}
949
950static const char *fixunit(char *buf, struct perf_evsel *evsel,
951 const char *unit)
952{
953 if (!strncmp(unit, "of all", 6)) {
954 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
955 unit);
956 return buf;
957 }
958 return unit;
959}
960
961static void print_metric_only(void *ctx, const char *color, const char *fmt,
962 const char *unit, double val)
963{
964 struct outstate *os = ctx;
965 FILE *out = os->fh;
966 int n;
967 char buf[1024];
968 unsigned mlen = METRIC_ONLY_LEN;
969
970 if (!valid_only_metric(unit))
971 return;
972 unit = fixunit(buf, os->evsel, unit);
973 if (color)
974 n = color_fprintf(out, color, fmt, val);
975 else
976 n = fprintf(out, fmt, val);
977 if (n > METRIC_ONLY_LEN)
978 n = METRIC_ONLY_LEN;
979 if (mlen < strlen(unit))
980 mlen = strlen(unit) + 1;
981 fprintf(out, "%*s", mlen - n, "");
982}
983
984static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
985 const char *fmt,
986 const char *unit, double val)
987{
988 struct outstate *os = ctx;
989 FILE *out = os->fh;
990 char buf[64], *vals, *ends;
991 char tbuf[1024];
992
993 if (!valid_only_metric(unit))
994 return;
995 unit = fixunit(tbuf, os->evsel, unit);
996 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900997 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -0800998 while (isdigit(*ends) || *ends == '.')
999 ends++;
1000 *ends = 0;
1001 fprintf(out, "%s%s", vals, csv_sep);
1002}
1003
1004static void new_line_metric(void *ctx __maybe_unused)
1005{
1006}
1007
1008static void print_metric_header(void *ctx, const char *color __maybe_unused,
1009 const char *fmt __maybe_unused,
1010 const char *unit, double val __maybe_unused)
1011{
1012 struct outstate *os = ctx;
1013 char tbuf[1024];
1014
1015 if (!valid_only_metric(unit))
1016 return;
1017 unit = fixunit(tbuf, os->evsel, unit);
1018 if (csv_output)
1019 fprintf(os->fh, "%s%s", unit, csv_sep);
1020 else
1021 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
1022}
1023
Andi Kleenda88c7f2014-09-24 13:50:46 -07001024static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001025{
Jiri Olsa58215222015-07-21 14:31:24 +02001026 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001027 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +01001028 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -06001029 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001030
Stephane Eranian410136f2013-11-12 17:58:49 +01001031 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
1032 fmt_n = csv_output ? "%s" : "%-25s";
1033
Andi Kleenda88c7f2014-09-24 13:50:46 -07001034 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001035
David Ahern4bbe5a62013-09-28 14:28:00 -06001036 scnprintf(name, sizeof(name), "%s%s",
1037 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +01001038
1039 fprintf(output, fmt_v, msecs, csv_sep);
1040
1041 if (csv_output)
1042 fprintf(output, "%s%s", evsel->unit, csv_sep);
1043 else
1044 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1045
1046 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001047
Stephane Eranian023695d2011-02-14 11:20:01 +02001048 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001049 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001050}
1051
Andi Kleen44d49a62016-02-29 14:36:22 -08001052static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1053{
1054 int i;
1055
1056 if (!aggr_get_id)
1057 return 0;
1058
1059 if (stat_config.aggr_mode == AGGR_NONE)
1060 return id;
1061
1062 if (stat_config.aggr_mode == AGGR_GLOBAL)
1063 return 0;
1064
1065 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1066 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1067
1068 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1069 return cpu2;
1070 }
1071 return 0;
1072}
1073
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001074static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1075{
Jiri Olsa58215222015-07-21 14:31:24 +02001076 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001077 double sc = evsel->scale;
1078 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001079
1080 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001081 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001082 } else {
1083 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001084 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001085 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001086 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001087 }
1088
1089 aggr_printout(evsel, id, nr);
1090
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001091 fprintf(output, fmt, avg, csv_sep);
1092
1093 if (evsel->unit)
1094 fprintf(output, "%-*s%s",
1095 csv_output ? 0 : unit_width,
1096 evsel->unit, csv_sep);
1097
1098 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1099
1100 if (evsel->cgrp)
1101 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001102}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001103
Andi Kleenf9483392016-01-30 09:06:50 -08001104static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Jin Yaoe0128b32017-12-05 22:03:05 +08001105 char *prefix, u64 run, u64 ena, double noise,
1106 struct runtime_stat *st)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001107{
Andi Kleen140aead2016-01-30 09:06:49 -08001108 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001109 struct outstate os = {
1110 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001111 .prefix = prefix ? prefix : "",
1112 .id = id,
1113 .nr = nr,
1114 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001115 };
Andi Kleen140aead2016-01-30 09:06:49 -08001116 print_metric_t pm = print_metric_std;
1117 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001118
Andi Kleen54b50912016-03-03 15:57:36 -08001119 if (metric_only) {
1120 nl = new_line_metric;
1121 if (csv_output)
1122 pm = print_metric_only_csv;
1123 else
1124 pm = print_metric_only;
1125 } else
1126 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001127
Andi Kleen54b50912016-03-03 15:57:36 -08001128 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001129 static int aggr_fields[] = {
1130 [AGGR_GLOBAL] = 0,
1131 [AGGR_THREAD] = 1,
1132 [AGGR_NONE] = 1,
1133 [AGGR_SOCKET] = 2,
1134 [AGGR_CORE] = 2,
1135 };
1136
1137 pm = print_metric_csv;
1138 nl = new_line_csv;
1139 os.nfields = 3;
1140 os.nfields += aggr_fields[stat_config.aggr_mode];
1141 if (counter->cgrp)
1142 os.nfields++;
1143 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001144 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001145 if (metric_only) {
1146 pm(&os, NULL, "", "", 0);
1147 return;
1148 }
Andi Kleencb110f42016-01-30 09:06:51 -08001149 aggr_printout(counter, id, nr);
1150
1151 fprintf(stat_config.output, "%*s%s",
1152 csv_output ? 0 : 18,
1153 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1154 csv_sep);
1155
Borislav Petkov02d492e2017-02-07 01:40:05 +01001156 if (counter->supported)
1157 print_free_counters_hint = 1;
1158
Andi Kleencb110f42016-01-30 09:06:51 -08001159 fprintf(stat_config.output, "%-*s%s",
1160 csv_output ? 0 : unit_width,
1161 counter->unit, csv_sep);
1162
1163 fprintf(stat_config.output, "%*s",
1164 csv_output ? 0 : -25,
1165 perf_evsel__name(counter));
1166
1167 if (counter->cgrp)
1168 fprintf(stat_config.output, "%s%s",
1169 csv_sep, counter->cgrp->name);
1170
Andi Kleen92a61f62016-02-29 14:36:21 -08001171 if (!csv_output)
1172 pm(&os, NULL, NULL, "", 0);
1173 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001174 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001175 if (csv_output)
1176 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001177 return;
1178 }
1179
Andi Kleen54b50912016-03-03 15:57:36 -08001180 if (metric_only)
1181 /* nothing */;
1182 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001183 nsec_printout(id, nr, counter, uval);
1184 else
1185 abs_printout(id, nr, counter, uval);
1186
Andi Kleen140aead2016-01-30 09:06:49 -08001187 out.print_metric = pm;
1188 out.new_line = nl;
1189 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001190 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001191
Andi Kleen54b50912016-03-03 15:57:36 -08001192 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001193 print_noise(counter, noise);
1194 print_running(run, ena);
1195 }
1196
1197 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001198 first_shadow_cpu(counter, id),
Jin Yaoe0128b32017-12-05 22:03:05 +08001199 &out, &metric_events, st);
Andi Kleen54b50912016-03-03 15:57:36 -08001200 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001201 print_noise(counter, noise);
1202 print_running(run, ena);
1203 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001204}
1205
Andi Kleen44d49a62016-02-29 14:36:22 -08001206static void aggr_update_shadow(void)
1207{
1208 int cpu, s2, id, s;
1209 u64 val;
1210 struct perf_evsel *counter;
1211
1212 for (s = 0; s < aggr_map->nr; s++) {
1213 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001214 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001215 val = 0;
1216 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1217 s2 = aggr_get_id(evsel_list->cpus, cpu);
1218 if (s2 != id)
1219 continue;
1220 val += perf_counts(counter->counts, cpu, 0)->val;
1221 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001222 perf_stat__update_shadow_stats(counter, val,
Jin Yao1fcd0392017-12-05 22:03:04 +08001223 first_shadow_cpu(counter, id),
1224 &rt_stat);
Andi Kleen44d49a62016-02-29 14:36:22 -08001225 }
1226 }
1227}
1228
Andi Kleen430daf22017-03-20 13:17:00 -07001229static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001230 void (*cb)(struct perf_evsel *counter, void *data,
1231 bool first),
1232 void *data)
1233{
Andi Kleen430daf22017-03-20 13:17:00 -07001234 struct perf_evsel *alias;
1235
1236 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1237 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1238 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1239 alias->scale != counter->scale ||
1240 alias->cgrp != counter->cgrp ||
1241 strcmp(alias->unit, counter->unit) ||
1242 nsec_counter(alias) != nsec_counter(counter))
1243 break;
1244 alias->merged_stat = true;
1245 cb(alias, data, false);
1246 }
1247}
1248
1249static bool collect_data(struct perf_evsel *counter,
1250 void (*cb)(struct perf_evsel *counter, void *data,
1251 bool first),
1252 void *data)
1253{
1254 if (counter->merged_stat)
1255 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001256 cb(counter, data, true);
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -03001257 if (!no_merge && counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001258 collect_all_aliases(counter, cb, data);
1259 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001260}
1261
1262struct aggr_data {
1263 u64 ena, run, val;
1264 int id;
1265 int nr;
1266 int cpu;
1267};
1268
1269static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1270{
1271 struct aggr_data *ad = data;
1272 int cpu, s2;
1273
1274 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1275 struct perf_counts_values *counts;
1276
1277 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1278 if (s2 != ad->id)
1279 continue;
1280 if (first)
1281 ad->nr++;
1282 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001283 /*
1284 * When any result is bad, make them all to give
1285 * consistent output in interval mode.
1286 */
1287 if (counts->ena == 0 || counts->run == 0 ||
1288 counter->counts->scaled == -1) {
1289 ad->ena = 0;
1290 ad->run = 0;
1291 break;
1292 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001293 ad->val += counts->val;
1294 ad->ena += counts->ena;
1295 ad->run += counts->run;
1296 }
1297}
1298
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001299static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001300{
Jiri Olsa58215222015-07-21 14:31:24 +02001301 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001302 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001303 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001304 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001305 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001306 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001307
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001308 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001309 return;
1310
Andi Kleen44d49a62016-02-29 14:36:22 -08001311 aggr_update_shadow();
1312
Andi Kleen54b50912016-03-03 15:57:36 -08001313 /*
1314 * With metric_only everything is on a single line.
1315 * Without each counter has its own line.
1316 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001317 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001318 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001319 if (prefix && metric_only)
1320 fprintf(output, "%s", prefix);
1321
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001322 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001323 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001324 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001325 if (is_duration_time(counter))
1326 continue;
1327
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001328 ad.val = ad.ena = ad.run = 0;
1329 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001330 if (!collect_data(counter, aggr_cb, &ad))
1331 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001332 nr = ad.nr;
1333 ena = ad.ena;
1334 run = ad.run;
1335 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001336 if (first && metric_only) {
1337 first = false;
1338 aggr_printout(counter, id, nr);
1339 }
1340 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001341 fprintf(output, "%s", prefix);
1342
Stephane Eranian410136f2013-11-12 17:58:49 +01001343 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001344 printout(id, nr, counter, uval, prefix, run, ena, 1.0,
1345 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001346 if (!metric_only)
1347 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001348 }
Andi Kleen54b50912016-03-03 15:57:36 -08001349 if (metric_only)
1350 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001351 }
1352}
1353
Jiri Olsa32b8af82015-06-26 11:29:27 +02001354static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1355{
Jiri Olsa58215222015-07-21 14:31:24 +02001356 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001357 int nthreads = thread_map__nr(counter->threads);
1358 int ncpus = cpu_map__nr(counter->cpus);
1359 int cpu, thread;
1360 double uval;
1361
1362 for (thread = 0; thread < nthreads; thread++) {
1363 u64 ena = 0, run = 0, val = 0;
1364
1365 for (cpu = 0; cpu < ncpus; cpu++) {
1366 val += perf_counts(counter->counts, cpu, thread)->val;
1367 ena += perf_counts(counter->counts, cpu, thread)->ena;
1368 run += perf_counts(counter->counts, cpu, thread)->run;
1369 }
1370
1371 if (prefix)
1372 fprintf(output, "%s", prefix);
1373
1374 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001375 printout(thread, 0, counter, uval, prefix, run, ena, 1.0,
1376 &rt_stat);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001377 fputc('\n', output);
1378 }
1379}
1380
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001381struct caggr_data {
1382 double avg, avg_enabled, avg_running;
1383};
1384
1385static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1386 bool first __maybe_unused)
1387{
1388 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001389 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001390
1391 cd->avg += avg_stats(&ps->res_stats[0]);
1392 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1393 cd->avg_running += avg_stats(&ps->res_stats[2]);
1394}
1395
Ingo Molnar42202dd2009-06-13 14:57:28 +02001396/*
1397 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001398 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001399 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001400static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001401{
Jiri Olsa58215222015-07-21 14:31:24 +02001402 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001403 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001404 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001405
Andi Kleen430daf22017-03-20 13:17:00 -07001406 if (!collect_data(counter, counter_aggr_cb, &cd))
1407 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001408
Andi Kleen54b50912016-03-03 15:57:36 -08001409 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001410 fprintf(output, "%s", prefix);
1411
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001412 uval = cd.avg * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001413 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
1414 cd.avg, &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001415 if (!metric_only)
1416 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001417}
1418
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001419static void counter_cb(struct perf_evsel *counter, void *data,
1420 bool first __maybe_unused)
1421{
1422 struct aggr_data *ad = data;
1423
1424 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1425 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1426 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1427}
1428
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001429/*
1430 * Print out the results of a single counter:
1431 * does not use aggregated count in system-wide
1432 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001433static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001434{
Jiri Olsa58215222015-07-21 14:31:24 +02001435 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001436 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001437 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001438 int cpu;
1439
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001440 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001441 struct aggr_data ad = { .cpu = cpu };
1442
Andi Kleen430daf22017-03-20 13:17:00 -07001443 if (!collect_data(counter, counter_cb, &ad))
1444 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001445 val = ad.val;
1446 ena = ad.ena;
1447 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001448
1449 if (prefix)
1450 fprintf(output, "%s", prefix);
1451
Stephane Eranian410136f2013-11-12 17:58:49 +01001452 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001453 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1454 &rt_stat);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001455
Stephane Eranian4aa90152011-08-15 22:22:33 +02001456 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001457 }
1458}
1459
Andi Kleen206cab62016-03-03 15:57:37 -08001460static void print_no_aggr_metric(char *prefix)
1461{
1462 int cpu;
1463 int nrcpus = 0;
1464 struct perf_evsel *counter;
1465 u64 ena, run, val;
1466 double uval;
1467
1468 nrcpus = evsel_list->cpus->nr;
1469 for (cpu = 0; cpu < nrcpus; cpu++) {
1470 bool first = true;
1471
1472 if (prefix)
1473 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001474 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001475 if (is_duration_time(counter))
1476 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001477 if (first) {
1478 aggr_printout(counter, cpu, 0);
1479 first = false;
1480 }
1481 val = perf_counts(counter->counts, cpu, 0)->val;
1482 ena = perf_counts(counter->counts, cpu, 0)->ena;
1483 run = perf_counts(counter->counts, cpu, 0)->run;
1484
1485 uval = val * counter->scale;
Jin Yaoe0128b32017-12-05 22:03:05 +08001486 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1487 &rt_stat);
Andi Kleen206cab62016-03-03 15:57:37 -08001488 }
1489 fputc('\n', stat_config.output);
1490 }
1491}
1492
Andi Kleen54b50912016-03-03 15:57:36 -08001493static int aggr_header_lens[] = {
1494 [AGGR_CORE] = 18,
1495 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001496 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001497 [AGGR_THREAD] = 24,
1498 [AGGR_GLOBAL] = 0,
1499};
1500
Andi Kleenc51fd632016-05-24 12:52:39 -07001501static const char *aggr_header_csv[] = {
1502 [AGGR_CORE] = "core,cpus,",
1503 [AGGR_SOCKET] = "socket,cpus",
1504 [AGGR_NONE] = "cpu,",
1505 [AGGR_THREAD] = "comm-pid,",
1506 [AGGR_GLOBAL] = ""
1507};
1508
Andi Kleen41c8ca22016-05-24 12:52:38 -07001509static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001510{
1511 struct perf_stat_output_ctx out;
1512 struct perf_evsel *counter;
1513 struct outstate os = {
1514 .fh = stat_config.output
1515 };
1516
1517 if (prefix)
1518 fprintf(stat_config.output, "%s", prefix);
1519
Andi Kleen41c8ca22016-05-24 12:52:38 -07001520 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001521 fprintf(stat_config.output, "%*s",
1522 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001523 if (csv_output) {
1524 if (stat_config.interval)
1525 fputs("time,", stat_config.output);
1526 fputs(aggr_header_csv[stat_config.aggr_mode],
1527 stat_config.output);
1528 }
Andi Kleen54b50912016-03-03 15:57:36 -08001529
1530 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001531 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001532 if (is_duration_time(counter))
1533 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001534 os.evsel = counter;
1535 out.ctx = &os;
1536 out.print_metric = print_metric_header;
1537 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001538 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001539 os.evsel = counter;
1540 perf_stat__print_shadow_stats(counter, 0,
1541 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001542 &out,
Jin Yaoe0128b32017-12-05 22:03:05 +08001543 &metric_events,
1544 &rt_stat);
Andi Kleen54b50912016-03-03 15:57:36 -08001545 }
1546 fputc('\n', stat_config.output);
1547}
1548
Jiri Olsad4f63a42015-06-26 11:29:26 +02001549static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001550{
Jiri Olsa58215222015-07-21 14:31:24 +02001551 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001552 static int num_print_interval;
1553
1554 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1555
Andi Kleen41c8ca22016-05-24 12:52:38 -07001556 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001557 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001558 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001559 fprintf(output, "# time socket cpus");
1560 if (!metric_only)
1561 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001562 break;
1563 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001564 fprintf(output, "# time core cpus");
1565 if (!metric_only)
1566 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001567 break;
1568 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001569 fprintf(output, "# time CPU");
1570 if (!metric_only)
1571 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001572 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001573 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001574 fprintf(output, "# time comm-pid");
1575 if (!metric_only)
1576 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001577 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001578 case AGGR_GLOBAL:
1579 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001580 fprintf(output, "# time");
1581 if (!metric_only)
1582 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001583 case AGGR_UNSET:
1584 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001585 }
1586 }
1587
Andi Kleen41c8ca22016-05-24 12:52:38 -07001588 if (num_print_interval == 0 && metric_only)
1589 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001590 if (++num_print_interval == 25)
1591 num_print_interval = 0;
1592}
1593
1594static void print_header(int argc, const char **argv)
1595{
Jiri Olsa58215222015-07-21 14:31:24 +02001596 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001597 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001598
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001599 fflush(stdout);
1600
Stephane Eraniand7470b62010-12-01 18:49:05 +02001601 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001602 fprintf(output, "\n");
1603 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001604 if (target.system_wide)
1605 fprintf(output, "\'system wide");
1606 else if (target.cpu_list)
1607 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001608 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001609 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1610 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001611 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001612 } else if (target.pid)
1613 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001614 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001615 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001616
Stephane Eranian4aa90152011-08-15 22:22:33 +02001617 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001618 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001619 fprintf(output, " (%d runs)", run_count);
1620 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001621 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001622}
1623
1624static void print_footer(void)
1625{
Jiri Olsa58215222015-07-21 14:31:24 +02001626 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001627 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001628
Jiri Olsad4f63a42015-06-26 11:29:26 +02001629 if (!null_run)
1630 fprintf(output, "\n");
1631 fprintf(output, " %17.9f seconds time elapsed",
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001632 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001633 if (run_count > 1) {
1634 fprintf(output, " ");
1635 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1636 avg_stats(&walltime_nsecs_stats));
1637 }
1638 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001639
Andi Kleen918c7b062017-05-22 18:00:16 -07001640 if (print_free_counters_hint &&
1641 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1642 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001643 fprintf(output,
1644"Some events weren't counted. Try disabling the NMI watchdog:\n"
1645" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1646" perf stat ...\n"
1647" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001648}
1649
1650static void print_counters(struct timespec *ts, int argc, const char **argv)
1651{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001652 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001653 struct perf_evsel *counter;
1654 char buf[64], *prefix = NULL;
1655
Jiri Olsa664c98d2015-11-05 15:40:50 +01001656 /* Do not print anything if we record to the pipe. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001657 if (STAT_RECORD && perf_stat.data.is_pipe)
Jiri Olsa664c98d2015-11-05 15:40:50 +01001658 return;
1659
Jiri Olsad4f63a42015-06-26 11:29:26 +02001660 if (interval)
1661 print_interval(prefix = buf, ts);
1662 else
1663 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001664
Andi Kleen54b50912016-03-03 15:57:36 -08001665 if (metric_only) {
1666 static int num_print_iv;
1667
Andi Kleen41c8ca22016-05-24 12:52:38 -07001668 if (num_print_iv == 0 && !interval)
1669 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001670 if (num_print_iv++ == 25)
1671 num_print_iv = 0;
1672 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1673 fprintf(stat_config.output, "%s", prefix);
1674 }
1675
Jiri Olsa421a50f2015-07-21 14:31:22 +02001676 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001677 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001678 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001679 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001680 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001681 case AGGR_THREAD:
Andi Kleene864c5c2017-08-31 12:40:35 -07001682 evlist__for_each_entry(evsel_list, counter) {
1683 if (is_duration_time(counter))
1684 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001685 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001686 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001687 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001688 case AGGR_GLOBAL:
Andi Kleene864c5c2017-08-31 12:40:35 -07001689 evlist__for_each_entry(evsel_list, counter) {
1690 if (is_duration_time(counter))
1691 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001692 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001693 }
Andi Kleen54b50912016-03-03 15:57:36 -08001694 if (metric_only)
1695 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001696 break;
1697 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001698 if (metric_only)
1699 print_no_aggr_metric(prefix);
1700 else {
Andi Kleene864c5c2017-08-31 12:40:35 -07001701 evlist__for_each_entry(evsel_list, counter) {
1702 if (is_duration_time(counter))
1703 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001704 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001705 }
Andi Kleen206cab62016-03-03 15:57:37 -08001706 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001707 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001708 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001709 default:
1710 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001711 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001712
Jiri Olsad4f63a42015-06-26 11:29:26 +02001713 if (!interval && !csv_output)
1714 print_footer();
1715
Jiri Olsa58215222015-07-21 14:31:24 +02001716 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001717}
1718
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001719static volatile int signr = -1;
1720
Ingo Molnar52425192009-05-26 09:17:18 +02001721static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001722{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001723 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001724 done = 1;
1725
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001726 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001727 /*
1728 * render child_pid harmless
1729 * won't send SIGTERM to a random
1730 * process in case of race condition
1731 * and fast PID recycling
1732 */
1733 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001734}
1735
1736static void sig_atexit(void)
1737{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001738 sigset_t set, oset;
1739
1740 /*
1741 * avoid race condition with SIGCHLD handler
1742 * in skip_signal() which is modifying child_pid
1743 * goal is to avoid send SIGTERM to a random
1744 * process
1745 */
1746 sigemptyset(&set);
1747 sigaddset(&set, SIGCHLD);
1748 sigprocmask(SIG_BLOCK, &set, &oset);
1749
Chris Wilson933da832009-10-04 01:35:01 +01001750 if (child_pid != -1)
1751 kill(child_pid, SIGTERM);
1752
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001753 sigprocmask(SIG_SETMASK, &oset, NULL);
1754
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001755 if (signr == -1)
1756 return;
1757
1758 signal(signr, SIG_DFL);
1759 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001760}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001761
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001762static int stat__set_big_num(const struct option *opt __maybe_unused,
1763 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001764{
1765 big_num_opt = unset ? 0 : 1;
1766 return 0;
1767}
1768
Andi Kleen44b1e602016-05-30 12:49:42 -03001769static int enable_metric_only(const struct option *opt __maybe_unused,
1770 const char *s __maybe_unused, int unset)
1771{
1772 force_metric_only = true;
1773 metric_only = !unset;
1774 return 0;
1775}
1776
Andi Kleenb18f3e32017-08-31 12:40:31 -07001777static int parse_metric_groups(const struct option *opt,
1778 const char *str,
1779 int unset __maybe_unused)
1780{
1781 return metricgroup__parse_groups(opt, str, &metric_events);
1782}
1783
Jiri Olsae0547312015-11-05 15:40:45 +01001784static const struct option stat_options[] = {
1785 OPT_BOOLEAN('T', "transaction", &transaction_run,
1786 "hardware transaction statistics"),
1787 OPT_CALLBACK('e', "event", &evsel_list, "event",
1788 "event selector. use 'perf list' to list available events",
1789 parse_events_option),
1790 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1791 "event filter", parse_filter),
1792 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1793 "child tasks do not inherit counters"),
1794 OPT_STRING('p', "pid", &target.pid, "pid",
1795 "stat events on existing process id"),
1796 OPT_STRING('t', "tid", &target.tid, "tid",
1797 "stat events on existing thread id"),
1798 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1799 "system-wide collection from all CPUs"),
1800 OPT_BOOLEAN('g', "group", &group,
1801 "put the counters into a counter group"),
1802 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1803 OPT_INCR('v', "verbose", &verbose,
1804 "be more verbose (show counter open errors, etc)"),
1805 OPT_INTEGER('r', "repeat", &run_count,
1806 "repeat command and print average + stddev (max: 100, forever: 0)"),
1807 OPT_BOOLEAN('n', "null", &null_run,
1808 "null run - dont start any counters"),
1809 OPT_INCR('d', "detailed", &detailed_run,
1810 "detailed run - start a lot of events"),
1811 OPT_BOOLEAN('S', "sync", &sync_run,
1812 "call sync() before starting a run"),
1813 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1814 "print large numbers with thousands\' separators",
1815 stat__set_big_num),
1816 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1817 "list of cpus to monitor in system-wide"),
1818 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1819 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001820 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001821 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1822 "print counts with custom separator"),
1823 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1824 "monitor event in cgroup name only", parse_cgroups),
1825 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1826 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1827 OPT_INTEGER(0, "log-fd", &output_fd,
1828 "log output to fd, instead of stderr"),
1829 OPT_STRING(0, "pre", &pre_cmd, "command",
1830 "command to run prior to the measured command"),
1831 OPT_STRING(0, "post", &post_cmd, "command",
1832 "command to run after to the measured command"),
1833 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1834 "print counts at regular interval in ms (>= 10)"),
1835 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1836 "aggregate counts per processor socket", AGGR_SOCKET),
1837 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1838 "aggregate counts per physical processor core", AGGR_CORE),
1839 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1840 "aggregate counts per thread", AGGR_THREAD),
1841 OPT_UINTEGER('D', "delay", &initial_delay,
1842 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001843 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1844 "Only print computed metrics. No raw values", enable_metric_only),
1845 OPT_BOOLEAN(0, "topdown", &topdown_run,
1846 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001847 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1848 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001849 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1850 "monitor specified metrics or metric groups (separated by ,)",
1851 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01001852 OPT_END()
1853};
1854
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001855static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1856{
1857 return cpu_map__get_socket(map, cpu, NULL);
1858}
1859
1860static int perf_stat__get_core(struct cpu_map *map, int cpu)
1861{
1862 return cpu_map__get_core(map, cpu, NULL);
1863}
1864
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001865static int cpu_map__get_max(struct cpu_map *map)
1866{
1867 int i, max = -1;
1868
1869 for (i = 0; i < map->nr; i++) {
1870 if (map->map[i] > max)
1871 max = map->map[i];
1872 }
1873
1874 return max;
1875}
1876
1877static struct cpu_map *cpus_aggr_map;
1878
1879static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1880{
1881 int cpu;
1882
1883 if (idx >= map->nr)
1884 return -1;
1885
1886 cpu = map->map[idx];
1887
1888 if (cpus_aggr_map->map[cpu] == -1)
1889 cpus_aggr_map->map[cpu] = get_id(map, idx);
1890
1891 return cpus_aggr_map->map[cpu];
1892}
1893
1894static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1895{
1896 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1897}
1898
1899static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1900{
1901 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1902}
1903
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001904static int perf_stat_init_aggr_mode(void)
1905{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001906 int nr;
1907
Jiri Olsa421a50f2015-07-21 14:31:22 +02001908 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001909 case AGGR_SOCKET:
1910 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1911 perror("cannot build socket map");
1912 return -1;
1913 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001914 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001915 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001916 case AGGR_CORE:
1917 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1918 perror("cannot build core map");
1919 return -1;
1920 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001921 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001922 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001923 case AGGR_NONE:
1924 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001925 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001926 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001927 default:
1928 break;
1929 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001930
1931 /*
1932 * The evsel_list->cpus is the base we operate on,
1933 * taking the highest cpu number to be the size of
1934 * the aggregation translate cpumap.
1935 */
1936 nr = cpu_map__get_max(evsel_list->cpus);
1937 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1938 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001939}
1940
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001941static void perf_stat__exit_aggr_mode(void)
1942{
1943 cpu_map__put(aggr_map);
1944 cpu_map__put(cpus_aggr_map);
1945 aggr_map = NULL;
1946 cpus_aggr_map = NULL;
1947}
1948
Jiri Olsa68d702f2015-11-05 15:40:58 +01001949static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1950{
1951 int cpu;
1952
1953 if (idx > map->nr)
1954 return -1;
1955
1956 cpu = map->map[idx];
1957
Jan Stancekda8a58b2017-02-17 12:10:26 +01001958 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001959 return -1;
1960
1961 return cpu;
1962}
1963
1964static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1965{
1966 struct perf_env *env = data;
1967 int cpu = perf_env__get_cpu(env, map, idx);
1968
1969 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1970}
1971
1972static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1973{
1974 struct perf_env *env = data;
1975 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1976
1977 if (cpu != -1) {
1978 int socket_id = env->cpu[cpu].socket_id;
1979
1980 /*
1981 * Encode socket in upper 16 bits
1982 * core_id is relative to socket, and
1983 * we need a global id. So we combine
1984 * socket + core id.
1985 */
1986 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1987 }
1988
1989 return core;
1990}
1991
1992static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1993 struct cpu_map **sockp)
1994{
1995 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1996}
1997
1998static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1999 struct cpu_map **corep)
2000{
2001 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2002}
2003
2004static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2005{
2006 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2007}
2008
2009static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2010{
2011 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2012}
2013
2014static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2015{
2016 struct perf_env *env = &st->session->header.env;
2017
2018 switch (stat_config.aggr_mode) {
2019 case AGGR_SOCKET:
2020 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2021 perror("cannot build socket map");
2022 return -1;
2023 }
2024 aggr_get_id = perf_stat__get_socket_file;
2025 break;
2026 case AGGR_CORE:
2027 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2028 perror("cannot build core map");
2029 return -1;
2030 }
2031 aggr_get_id = perf_stat__get_core_file;
2032 break;
2033 case AGGR_NONE:
2034 case AGGR_GLOBAL:
2035 case AGGR_THREAD:
2036 case AGGR_UNSET:
2037 default:
2038 break;
2039 }
2040
2041 return 0;
2042}
2043
Andi Kleen44b1e602016-05-30 12:49:42 -03002044static int topdown_filter_events(const char **attr, char **str, bool use_group)
2045{
2046 int off = 0;
2047 int i;
2048 int len = 0;
2049 char *s;
2050
2051 for (i = 0; attr[i]; i++) {
2052 if (pmu_have_event("cpu", attr[i])) {
2053 len += strlen(attr[i]) + 1;
2054 attr[i - off] = attr[i];
2055 } else
2056 off++;
2057 }
2058 attr[i - off] = NULL;
2059
2060 *str = malloc(len + 1 + 2);
2061 if (!*str)
2062 return -1;
2063 s = *str;
2064 if (i - off == 0) {
2065 *s = 0;
2066 return 0;
2067 }
2068 if (use_group)
2069 *s++ = '{';
2070 for (i = 0; attr[i]; i++) {
2071 strcpy(s, attr[i]);
2072 s += strlen(s);
2073 *s++ = ',';
2074 }
2075 if (use_group) {
2076 s[-1] = '}';
2077 *s = 0;
2078 } else
2079 s[-1] = 0;
2080 return 0;
2081}
2082
2083__weak bool arch_topdown_check_group(bool *warn)
2084{
2085 *warn = false;
2086 return false;
2087}
2088
2089__weak void arch_topdown_group_warn(void)
2090{
2091}
2092
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002093/*
2094 * Add default attributes, if there were no attributes specified or
2095 * if -d/--detailed, -d -d or -d -d -d is used:
2096 */
2097static int add_default_attributes(void)
2098{
Andi Kleen44b1e602016-05-30 12:49:42 -03002099 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002100 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002101
2102 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2103 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2104 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2105 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2106
2107 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002108};
2109 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002110 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002111};
2112 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002113 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002114};
2115 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002116 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2117 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2118 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2119
2120};
2121
2122/*
2123 * Detailed stats (-d), covering the L1 and last level data caches:
2124 */
2125 struct perf_event_attr detailed_attrs[] = {
2126
2127 { .type = PERF_TYPE_HW_CACHE,
2128 .config =
2129 PERF_COUNT_HW_CACHE_L1D << 0 |
2130 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2131 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2132
2133 { .type = PERF_TYPE_HW_CACHE,
2134 .config =
2135 PERF_COUNT_HW_CACHE_L1D << 0 |
2136 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2137 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2138
2139 { .type = PERF_TYPE_HW_CACHE,
2140 .config =
2141 PERF_COUNT_HW_CACHE_LL << 0 |
2142 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2143 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2144
2145 { .type = PERF_TYPE_HW_CACHE,
2146 .config =
2147 PERF_COUNT_HW_CACHE_LL << 0 |
2148 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2149 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2150};
2151
2152/*
2153 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2154 */
2155 struct perf_event_attr very_detailed_attrs[] = {
2156
2157 { .type = PERF_TYPE_HW_CACHE,
2158 .config =
2159 PERF_COUNT_HW_CACHE_L1I << 0 |
2160 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2161 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2162
2163 { .type = PERF_TYPE_HW_CACHE,
2164 .config =
2165 PERF_COUNT_HW_CACHE_L1I << 0 |
2166 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2167 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2168
2169 { .type = PERF_TYPE_HW_CACHE,
2170 .config =
2171 PERF_COUNT_HW_CACHE_DTLB << 0 |
2172 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2173 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2174
2175 { .type = PERF_TYPE_HW_CACHE,
2176 .config =
2177 PERF_COUNT_HW_CACHE_DTLB << 0 |
2178 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2179 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2180
2181 { .type = PERF_TYPE_HW_CACHE,
2182 .config =
2183 PERF_COUNT_HW_CACHE_ITLB << 0 |
2184 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2185 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2186
2187 { .type = PERF_TYPE_HW_CACHE,
2188 .config =
2189 PERF_COUNT_HW_CACHE_ITLB << 0 |
2190 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2191 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2192
2193};
2194
2195/*
2196 * Very, very detailed stats (-d -d -d), adding prefetch events:
2197 */
2198 struct perf_event_attr very_very_detailed_attrs[] = {
2199
2200 { .type = PERF_TYPE_HW_CACHE,
2201 .config =
2202 PERF_COUNT_HW_CACHE_L1D << 0 |
2203 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2204 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2205
2206 { .type = PERF_TYPE_HW_CACHE,
2207 .config =
2208 PERF_COUNT_HW_CACHE_L1D << 0 |
2209 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2210 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2211};
2212
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002213 /* Set attrs if no event is selected and !null_run: */
2214 if (null_run)
2215 return 0;
2216
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002217 if (transaction_run) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002218 if (pmu_have_event("cpu", "cycles-ct") &&
2219 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02002220 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002221 else
Jiri Olsaa4547422015-06-03 16:25:53 +02002222 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2223 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002224 fprintf(stderr, "Cannot set up transaction events\n");
2225 return -1;
2226 }
2227 return 0;
2228 }
2229
Kan Liangdaefd0b2017-05-26 12:05:38 -07002230 if (smi_cost) {
2231 int smi;
2232
2233 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2234 fprintf(stderr, "freeze_on_smi is not supported.\n");
2235 return -1;
2236 }
2237
2238 if (!smi) {
2239 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2240 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2241 return -1;
2242 }
2243 smi_reset = true;
2244 }
2245
2246 if (pmu_have_event("msr", "aperf") &&
2247 pmu_have_event("msr", "smi")) {
2248 if (!force_metric_only)
2249 metric_only = true;
2250 err = parse_events(evsel_list, smi_cost_attrs, NULL);
2251 } else {
2252 fprintf(stderr, "To measure SMI cost, it needs "
2253 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2254 return -1;
2255 }
2256 if (err) {
2257 fprintf(stderr, "Cannot set up SMI cost events\n");
2258 return -1;
2259 }
2260 return 0;
2261 }
2262
Andi Kleen44b1e602016-05-30 12:49:42 -03002263 if (topdown_run) {
2264 char *str = NULL;
2265 bool warn = false;
2266
2267 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2268 stat_config.aggr_mode != AGGR_CORE) {
2269 pr_err("top down event configuration requires --per-core mode\n");
2270 return -1;
2271 }
2272 stat_config.aggr_mode = AGGR_CORE;
2273 if (nr_cgroups || !target__has_cpu(&target)) {
2274 pr_err("top down event configuration requires system-wide mode (-a)\n");
2275 return -1;
2276 }
2277
2278 if (!force_metric_only)
2279 metric_only = true;
2280 if (topdown_filter_events(topdown_attrs, &str,
2281 arch_topdown_check_group(&warn)) < 0) {
2282 pr_err("Out of memory\n");
2283 return -1;
2284 }
2285 if (topdown_attrs[0] && str) {
2286 if (warn)
2287 arch_topdown_group_warn();
2288 err = parse_events(evsel_list, str, NULL);
2289 if (err) {
2290 fprintf(stderr,
2291 "Cannot set up top down events %s: %d\n",
2292 str, err);
2293 free(str);
2294 return -1;
2295 }
2296 } else {
2297 fprintf(stderr, "System does not support topdown\n");
2298 return -1;
2299 }
2300 free(str);
2301 }
2302
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002303 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002304 if (target__has_cpu(&target))
2305 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2306
Andi Kleen9dec4472016-02-26 16:27:56 -08002307 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2308 return -1;
2309 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2310 if (perf_evlist__add_default_attrs(evsel_list,
2311 frontend_attrs) < 0)
2312 return -1;
2313 }
2314 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2315 if (perf_evlist__add_default_attrs(evsel_list,
2316 backend_attrs) < 0)
2317 return -1;
2318 }
2319 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002320 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002321 }
2322
2323 /* Detailed events get appended to the event list: */
2324
2325 if (detailed_run < 1)
2326 return 0;
2327
2328 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002329 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002330 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002331
2332 if (detailed_run < 2)
2333 return 0;
2334
2335 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002336 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002337 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002338
2339 if (detailed_run < 3)
2340 return 0;
2341
2342 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002343 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002344}
2345
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002346static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002347 "perf stat record [<options>]",
2348 NULL,
2349};
2350
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002351static void init_features(struct perf_session *session)
2352{
2353 int feat;
2354
2355 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2356 perf_header__set_feat(&session->header, feat);
2357
2358 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2359 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2360 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2361 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2362}
2363
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002364static int __cmd_record(int argc, const char **argv)
2365{
2366 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002367 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002368
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002369 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002370 PARSE_OPT_STOP_AT_NON_OPTION);
2371
2372 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002373 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002374
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002375 if (run_count != 1 || forever) {
2376 pr_err("Cannot use -r option with perf stat record.\n");
2377 return -1;
2378 }
2379
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002380 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002381 if (session == NULL) {
2382 pr_err("Perf session creation failed.\n");
2383 return -1;
2384 }
2385
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002386 init_features(session);
2387
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002388 session->evlist = evsel_list;
2389 perf_stat.session = session;
2390 perf_stat.record = true;
2391 return argc;
2392}
2393
Jiri Olsaa56f9392015-11-05 15:40:59 +01002394static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2395 union perf_event *event,
2396 struct perf_session *session)
2397{
Andi Kleene3b03b62016-05-05 16:04:03 -07002398 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002399 struct perf_evsel *counter;
2400 struct timespec tsh, *ts = NULL;
2401 const char **argv = session->header.env.cmdline_argv;
2402 int argc = session->header.env.nr_cmdline;
2403
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002404 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002405 perf_stat_process_counter(&stat_config, counter);
2406
Andi Kleene3b03b62016-05-05 16:04:03 -07002407 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2408 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002409
Andi Kleene3b03b62016-05-05 16:04:03 -07002410 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002411 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2412 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002413 ts = &tsh;
2414 }
2415
2416 print_counters(ts, argc, argv);
2417 return 0;
2418}
2419
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002420static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002421int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002422 union perf_event *event,
2423 struct perf_session *session __maybe_unused)
2424{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002425 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2426
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002427 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002428
Jiri Olsa89af4e02015-11-05 15:41:02 +01002429 if (cpu_map__empty(st->cpus)) {
2430 if (st->aggr_mode != AGGR_UNSET)
2431 pr_warning("warning: processing task data, aggregation mode not set\n");
2432 return 0;
2433 }
2434
2435 if (st->aggr_mode != AGGR_UNSET)
2436 stat_config.aggr_mode = st->aggr_mode;
2437
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002438 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002439 perf_stat_init_aggr_mode();
2440 else
2441 perf_stat_init_aggr_mode_file(st);
2442
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002443 return 0;
2444}
2445
Jiri Olsa1975d362015-11-05 15:40:56 +01002446static int set_maps(struct perf_stat *st)
2447{
2448 if (!st->cpus || !st->threads)
2449 return 0;
2450
2451 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2452 return -EINVAL;
2453
2454 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2455
2456 if (perf_evlist__alloc_stats(evsel_list, true))
2457 return -ENOMEM;
2458
2459 st->maps_allocated = true;
2460 return 0;
2461}
2462
2463static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002464int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002465 union perf_event *event,
2466 struct perf_session *session __maybe_unused)
2467{
2468 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2469
2470 if (st->threads) {
2471 pr_warning("Extra thread map event, ignoring.\n");
2472 return 0;
2473 }
2474
2475 st->threads = thread_map__new_event(&event->thread_map);
2476 if (!st->threads)
2477 return -ENOMEM;
2478
2479 return set_maps(st);
2480}
2481
2482static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002483int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002484 union perf_event *event,
2485 struct perf_session *session __maybe_unused)
2486{
2487 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2488 struct cpu_map *cpus;
2489
2490 if (st->cpus) {
2491 pr_warning("Extra cpu map event, ignoring.\n");
2492 return 0;
2493 }
2494
2495 cpus = cpu_map__new_data(&event->cpu_map.data);
2496 if (!cpus)
2497 return -ENOMEM;
2498
2499 st->cpus = cpus;
2500 return set_maps(st);
2501}
2502
Jin Yao56739442017-12-05 22:03:07 +08002503static int runtime_stat_new(struct perf_stat_config *config, int nthreads)
2504{
2505 int i;
2506
2507 config->stats = calloc(nthreads, sizeof(struct runtime_stat));
2508 if (!config->stats)
2509 return -1;
2510
2511 config->stats_num = nthreads;
2512
2513 for (i = 0; i < nthreads; i++)
2514 runtime_stat__init(&config->stats[i]);
2515
2516 return 0;
2517}
2518
2519static void runtime_stat_delete(struct perf_stat_config *config)
2520{
2521 int i;
2522
2523 if (!config->stats)
2524 return;
2525
2526 for (i = 0; i < config->stats_num; i++)
2527 runtime_stat__exit(&config->stats[i]);
2528
2529 free(config->stats);
2530}
2531
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002532static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002533 "perf stat report [<options>]",
2534 NULL,
2535};
2536
2537static struct perf_stat perf_stat = {
2538 .tool = {
2539 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002540 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002541 .thread_map = process_thread_map_event,
2542 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002543 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002544 .stat = perf_event__process_stat_event,
2545 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002546 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002547 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002548};
2549
2550static int __cmd_report(int argc, const char **argv)
2551{
2552 struct perf_session *session;
2553 const struct option options[] = {
2554 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002555 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2556 "aggregate counts per processor socket", AGGR_SOCKET),
2557 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2558 "aggregate counts per physical processor core", AGGR_CORE),
2559 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2560 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002561 OPT_END()
2562 };
2563 struct stat st;
2564 int ret;
2565
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002566 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002567
2568 if (!input_name || !strlen(input_name)) {
2569 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2570 input_name = "-";
2571 else
2572 input_name = "perf.data";
2573 }
2574
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002575 perf_stat.data.file.path = input_name;
2576 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002577
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002578 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002579 if (session == NULL)
2580 return -1;
2581
2582 perf_stat.session = session;
2583 stat_config.output = stderr;
2584 evsel_list = session->evlist;
2585
2586 ret = perf_session__process_events(session);
2587 if (ret)
2588 return ret;
2589
2590 perf_session__delete(session);
2591 return 0;
2592}
2593
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002594static void setup_system_wide(int forks)
2595{
2596 /*
2597 * Make system wide (-a) the default target if
2598 * no target was specified and one of following
2599 * conditions is met:
2600 *
2601 * - there's no workload specified
2602 * - there is workload specified but all requested
2603 * events are system wide events
2604 */
2605 if (!target__none(&target))
2606 return;
2607
2608 if (!forks)
2609 target.system_wide = true;
2610 else {
2611 struct perf_evsel *counter;
2612
2613 evlist__for_each_entry(evsel_list, counter) {
2614 if (!counter->system_wide)
2615 return;
2616 }
2617
2618 if (evsel_list->nr_entries)
2619 target.system_wide = true;
2620 }
2621}
2622
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002623int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002624{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002625 const char * const stat_usage[] = {
2626 "perf stat [<options>] [<command>]",
2627 NULL
2628 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002629 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002630 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002631 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002632 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002633 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002634
Stephane Eranian5af52b52010-05-18 15:00:01 +02002635 setlocale(LC_ALL, "");
2636
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002637 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002638 if (evsel_list == NULL)
2639 return -ENOMEM;
2640
Wang Nan1669e502016-02-19 11:43:58 +00002641 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002642 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2643 (const char **) stat_usage,
2644 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002645 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002646 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002647
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002648 if (csv_sep) {
2649 csv_output = true;
2650 if (!strcmp(csv_sep, "\\t"))
2651 csv_sep = "\t";
2652 } else
2653 csv_sep = DEFAULT_SEPARATOR;
2654
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002655 if (argc && !strncmp(argv[0], "rec", 3)) {
2656 argc = __cmd_record(argc, argv);
2657 if (argc < 0)
2658 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002659 } else if (argc && !strncmp(argv[0], "rep", 3))
2660 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002661
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002662 interval = stat_config.interval;
2663
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002664 /*
2665 * For record command the -o is already taken care of.
2666 */
2667 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002668 output = NULL;
2669
Jim Cromie56f3bae2011-09-07 17:14:00 -06002670 if (output_name && output_fd) {
2671 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002672 parse_options_usage(stat_usage, stat_options, "o", 1);
2673 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002674 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002675 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002676
Andi Kleen54b50912016-03-03 15:57:36 -08002677 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2678 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2679 goto out;
2680 }
2681
Andi Kleen54b50912016-03-03 15:57:36 -08002682 if (metric_only && run_count > 1) {
2683 fprintf(stderr, "--metric-only is not supported with -r\n");
2684 goto out;
2685 }
2686
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002687 if (output_fd < 0) {
2688 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002689 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002690 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002691 }
2692
Stephane Eranian4aa90152011-08-15 22:22:33 +02002693 if (!output) {
2694 struct timespec tm;
2695 mode = append_file ? "a" : "w";
2696
2697 output = fopen(output_name, mode);
2698 if (!output) {
2699 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002700 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002701 }
2702 clock_gettime(CLOCK_REALTIME, &tm);
2703 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002704 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002705 mode = append_file ? "a" : "w";
2706 output = fdopen(output_fd, mode);
2707 if (!output) {
2708 perror("Failed opening logfd");
2709 return -errno;
2710 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002711 }
2712
Jiri Olsa58215222015-07-21 14:31:24 +02002713 stat_config.output = output;
2714
Stephane Eraniand7470b62010-12-01 18:49:05 +02002715 /*
2716 * let the spreadsheet do the pretty-printing
2717 */
2718 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002719 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002720 if (big_num_opt == 1) {
2721 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002722 parse_options_usage(stat_usage, stat_options, "B", 1);
2723 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002724 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002725 } else /* Nope, so disable big number formatting */
2726 big_num = false;
2727 } else if (big_num_opt == 0) /* User passed --no-big-num */
2728 big_num = false;
2729
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002730 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002731
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002732 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002733 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002734 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002735 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002736 } else if (run_count == 0) {
2737 forever = true;
2738 run_count = 1;
2739 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002740
Jiri Olsa421a50f2015-07-21 14:31:22 +02002741 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002742 fprintf(stderr, "The --per-thread option is only available "
2743 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002744 parse_options_usage(NULL, stat_options, "p", 1);
2745 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002746 goto out;
2747 }
2748
2749 /*
2750 * no_aggr, cgroup are for system-wide only
2751 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2752 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002753 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2754 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002755 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002756 fprintf(stderr, "both cgroup and no-aggregation "
2757 "modes only available in system-wide mode\n");
2758
Jiri Olsae0547312015-11-05 15:40:45 +01002759 parse_options_usage(stat_usage, stat_options, "G", 1);
2760 parse_options_usage(NULL, stat_options, "A", 1);
2761 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002762 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002763 }
2764
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002765 if (add_default_attributes())
2766 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002767
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002768 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002769
Namhyung Kim77a6f012012-05-07 14:09:04 +09002770 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002771 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002772 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002773 parse_options_usage(stat_usage, stat_options, "p", 1);
2774 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002775 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002776 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002777 parse_options_usage(stat_usage, stat_options, "C", 1);
2778 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002779 }
2780 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002781 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002782
2783 /*
2784 * Initialize thread_map with comm names,
2785 * so we could print it out on output.
2786 */
Jin Yao56739442017-12-05 22:03:07 +08002787 if (stat_config.aggr_mode == AGGR_THREAD) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002788 thread_map__read_comms(evsel_list->threads);
Jin Yao56739442017-12-05 22:03:07 +08002789 if (target.system_wide) {
2790 if (runtime_stat_new(&stat_config,
2791 thread_map__nr(evsel_list->threads))) {
2792 goto out;
2793 }
2794 }
2795 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002796
Stephane Eranian13370a92013-01-29 12:47:44 +01002797 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002798 if (interval < 10) {
2799 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002800 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002801 goto out;
2802 } else
2803 pr_warning("print interval < 100ms. "
2804 "The overhead percentage could be high in some cases. "
2805 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002806 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002807
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002808 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002809 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002810
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002811 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002812 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002813
Ingo Molnar58d7e992009-05-15 11:03:23 +02002814 /*
2815 * We dont want to block the signals - that would cause
2816 * child tasks to inherit that and Ctrl-C would not work.
2817 * What we want is for Ctrl-C to work in the exec()-ed
2818 * task, but being ignored by perf stat itself:
2819 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002820 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002821 if (!forever)
2822 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002823 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002824 signal(SIGALRM, skip_signal);
2825 signal(SIGABRT, skip_signal);
2826
Ingo Molnar42202dd2009-06-13 14:57:28 +02002827 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002828 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002829 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002830 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2831 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002832
Ingo Molnar42202dd2009-06-13 14:57:28 +02002833 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002834 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002835 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002836 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002837 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002838 }
2839
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002840 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002841 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002842
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002843 if (STAT_RECORD) {
2844 /*
2845 * We synthesize the kernel mmap record just so that older tools
2846 * don't emit warnings about not being able to resolve symbols
2847 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2848 * a saner message about no samples being in the perf.data file.
2849 *
2850 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002851 * in header.c at the moment 'perf stat record' gets introduced, which
2852 * is not really needed once we start adding the stat specific PERF_RECORD_
2853 * records, but the need to suppress the kptr_restrict messages in older
2854 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002855 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002856 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002857 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2858 process_synthesized_event,
2859 &perf_stat.session->machines.host);
2860 if (err) {
2861 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2862 "older tools may produce warnings about this file\n.");
2863 }
2864
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002865 if (!interval) {
2866 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2867 pr_err("failed to write stat round event\n");
2868 }
2869
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002870 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01002871 perf_stat.session->header.data_size += perf_stat.bytes_written;
2872 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2873 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002874
2875 perf_session__delete(perf_stat.session);
2876 }
2877
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002878 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002879 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002880out:
Kan Liangdaefd0b2017-05-26 12:05:38 -07002881 if (smi_cost && smi_reset)
2882 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
2883
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002884 perf_evlist__delete(evsel_list);
Jin Yao56739442017-12-05 22:03:07 +08002885
2886 runtime_stat_delete(&stat_config);
2887
Ingo Molnar42202dd2009-06-13 14:57:28 +02002888 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002889}