blob: 59af5a8419e2be2c158753bf9d6a14ebd1089d76 [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{
217 perf_evlist__reset_stats(evsel_list);
Jiri Olsaf87027b2015-06-03 16:25:59 +0200218 perf_stat__reset_shadow_stats();
Jiri Olsa1eda3b22015-06-03 16:25:55 +0200219}
220
Jiri Olsacac21422012-11-12 18:34:00 +0100221static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200222{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200223 struct perf_event_attr *attr = &evsel->attr;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200224 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200225
Jiri Olsa82bf3112017-07-26 14:02:06 +0200226 if (stat_config.scale) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200227 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
228 PERF_FORMAT_TOTAL_TIME_RUNNING;
Jiri Olsa82bf3112017-07-26 14:02:06 +0200229 }
230
231 /*
232 * The event is part of non trivial group, let's enable
233 * the group read (for leader) and ID retrieval for all
234 * members.
235 */
236 if (leader->nr_members > 1)
237 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200238
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200239 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300240
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100241 /*
242 * Some events get initialized with sample_(period/type) set,
243 * like tracepoints. Clear it up for counting.
244 */
245 attr->sample_period = 0;
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100246
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100247 /*
248 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
249 * while avoiding that older tools show confusing messages.
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100250 *
251 * However for pipe sessions we need to keep it zero,
252 * because script's perf_evsel__check_attr is triggered
253 * by attr->sample_type != 0, and we can't run it on
254 * stat sessions.
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100255 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100256 if (!(STAT_RECORD && perf_stat.data.is_pipe))
Jiri Olsa6db1a5c2016-01-05 22:09:05 +0100257 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
Jiri Olsa6acd8e92015-11-25 16:36:54 +0100258
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100259 /*
260 * Disabling all counters initially, they will be enabled
261 * either manually by us or by kernel via enable_on_exec
262 * set later.
263 */
Jiri Olsac8280ce2015-12-03 10:06:45 +0100264 if (perf_evsel__is_group_leader(evsel)) {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100265 attr->disabled = 1;
266
Jiri Olsac8280ce2015-12-03 10:06:45 +0100267 /*
268 * In case of initial_delay we enable tracee
269 * events manually.
270 */
271 if (target__none(&target) && !initial_delay)
272 attr->enable_on_exec = 1;
273 }
274
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -0300275 if (target__has_cpu(&target))
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300276 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200277
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300278 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200279}
280
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200281/*
282 * Does the counter have nsecs as a unit?
283 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200284static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200285{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200286 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
287 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200288 return 1;
289
290 return 0;
291}
292
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100293static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100294 union perf_event *event,
295 struct perf_sample *sample __maybe_unused,
296 struct machine *machine __maybe_unused)
297{
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100298 if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100299 pr_err("failed to write perf data, error: %m\n");
300 return -1;
301 }
302
303 perf_stat.bytes_written += event->header.size;
304 return 0;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100305}
306
Jiri Olsa1975d362015-11-05 15:40:56 +0100307static int write_stat_round_event(u64 tm, u64 type)
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100308{
Jiri Olsa1975d362015-11-05 15:40:56 +0100309 return perf_event__synthesize_stat_round(NULL, tm, type,
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100310 process_synthesized_event,
311 NULL);
312}
313
314#define WRITE_STAT_ROUND_EVENT(time, interval) \
315 write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
316
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100317#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
318
319static int
320perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread,
321 struct perf_counts_values *count)
322{
323 struct perf_sample_id *sid = SID(counter, cpu, thread);
324
325 return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
326 process_synthesized_event, NULL);
327}
328
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200329/*
330 * Read out the results of a single counter:
331 * do not aggregate counts across CPUs in system-wide mode
332 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200333static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200334{
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100335 int nthreads = thread_map__nr(evsel_list->threads);
Mark Rutland00e727b2016-07-15 11:08:10 +0100336 int ncpus, cpu, thread;
337
338 if (target__has_cpu(&target))
339 ncpus = perf_evsel__nr_cpus(counter);
340 else
341 ncpus = 1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200342
Suzuki K. Poulose3b4331d2015-02-13 18:40:58 +0000343 if (!counter->supported)
344 return -ENOENT;
345
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100346 if (counter->system_wide)
347 nthreads = 1;
348
349 for (thread = 0; thread < nthreads; thread++) {
350 for (cpu = 0; cpu < ncpus; cpu++) {
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200351 struct perf_counts_values *count;
352
353 count = perf_counts(counter->counts, cpu, thread);
Jiri Olsa82bf3112017-07-26 14:02:06 +0200354
355 /*
356 * The leader's group read loads data into its group members
357 * (via perf_evsel__read_counter) and sets threir count->loaded.
358 */
359 if (!count->loaded &&
360 perf_evsel__read_counter(counter, cpu, thread)) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700361 counter->counts->scaled = -1;
362 perf_counts(counter->counts, cpu, thread)->ena = 0;
363 perf_counts(counter->counts, cpu, thread)->run = 0;
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100364 return -1;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700365 }
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100366
Jiri Olsa82bf3112017-07-26 14:02:06 +0200367 count->loaded = false;
368
Jiri Olsa5a6ea812015-11-05 15:40:51 +0100369 if (STAT_RECORD) {
370 if (perf_evsel__write_stat_event(counter, cpu, thread, count)) {
371 pr_err("failed to write stat event\n");
372 return -1;
373 }
374 }
Andi Kleen0b1abbf2016-04-27 13:00:51 -0700375
376 if (verbose > 1) {
377 fprintf(stat_config.output,
378 "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
379 perf_evsel__name(counter),
380 cpu,
381 count->val, count->ena, count->run);
382 }
Jiri Olsa9bf1a522014-11-21 10:31:09 +0100383 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200384 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200385
386 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200387}
388
Mark Rutland3df33ef2016-08-09 14:04:29 +0100389static void read_counters(void)
Jiri Olsa106a94a2015-06-26 11:29:19 +0200390{
391 struct perf_evsel *counter;
Stephane Eraniandb49a712017-04-12 11:23:01 -0700392 int ret;
Jiri Olsa106a94a2015-06-26 11:29:19 +0200393
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300394 evlist__for_each_entry(evsel_list, counter) {
Stephane Eraniandb49a712017-04-12 11:23:01 -0700395 ret = read_counter(counter);
396 if (ret)
Andi Kleen245bad82015-09-01 15:52:46 -0700397 pr_debug("failed to read counter %s\n", counter->name);
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200398
Stephane Eraniandb49a712017-04-12 11:23:01 -0700399 if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
Jiri Olsa3b3eb042015-06-26 11:29:20 +0200400 pr_warning("failed to process counter %s\n", counter->name);
Jiri Olsa106a94a2015-06-26 11:29:19 +0200401 }
402}
403
Jiri Olsaba411a92015-06-26 11:29:24 +0200404static void process_interval(void)
Stephane Eranian13370a92013-01-29 12:47:44 +0100405{
Stephane Eranian13370a92013-01-29 12:47:44 +0100406 struct timespec ts, rs;
Stephane Eranian13370a92013-01-29 12:47:44 +0100407
Mark Rutland3df33ef2016-08-09 14:04:29 +0100408 read_counters();
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100409
Stephane Eranian13370a92013-01-29 12:47:44 +0100410 clock_gettime(CLOCK_MONOTONIC, &ts);
411 diff_timespec(&rs, &ts, &ref_time);
Stephane Eranian13370a92013-01-29 12:47:44 +0100412
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100413 if (STAT_RECORD) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -0300414 if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
Jiri Olsa7aad0c32015-11-05 15:40:52 +0100415 pr_err("failed to write stat round event\n");
416 }
417
Andi Kleenb90f1332017-08-31 12:40:36 -0700418 init_stats(&walltime_nsecs_stats);
419 update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
Jiri Olsad4f63a42015-06-26 11:29:26 +0200420 print_counters(&rs, 0, NULL);
Stephane Eranian13370a92013-01-29 12:47:44 +0100421}
422
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100423static void enable_counters(void)
Andi Kleen41191682013-08-02 17:41:11 -0700424{
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100425 if (initial_delay)
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300426 usleep(initial_delay * USEC_PER_MSEC);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100427
428 /*
429 * We need to enable counters only if:
430 * - we don't have tracee (attaching to task or cpu)
431 * - we have initial delay configured
432 */
433 if (!target__none(&target) || initial_delay)
Jiri Olsaab46db02015-12-03 10:06:43 +0100434 perf_evlist__enable(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700435}
436
Mark Rutland3df33ef2016-08-09 14:04:29 +0100437static void disable_counters(void)
438{
439 /*
440 * If we don't have tracee (attaching to task or cpu), counters may
441 * still be running. To get accurate group ratios, we must stop groups
442 * from counting before reading their constituent counters.
443 */
444 if (!target__none(&target))
445 perf_evlist__disable(evsel_list);
446}
447
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300448static volatile int workload_exec_errno;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300449
450/*
451 * perf_evlist__prepare_workload will send a SIGUSR1
452 * if the fork fails, since we asked by setting its
453 * want_signal to true.
454 */
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300455static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
456 void *ucontext __maybe_unused)
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300457{
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300458 workload_exec_errno = info->si_value.sival_int;
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300459}
460
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100461static bool has_unit(struct perf_evsel *counter)
462{
463 return counter->unit && *counter->unit;
464}
465
466static bool has_scale(struct perf_evsel *counter)
467{
468 return counter->scale != 1;
469}
470
Jiri Olsa664c98d2015-11-05 15:40:50 +0100471static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100472{
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100473 struct perf_evsel *counter;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100474 int err;
475
Jiri Olsa664c98d2015-11-05 15:40:50 +0100476 if (is_pipe) {
477 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
478 process_synthesized_event);
479 if (err < 0) {
480 pr_err("Couldn't synthesize attrs.\n");
481 return err;
482 }
483 }
484
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100485 /*
486 * Synthesize other events stuff not carried within
487 * attr event - unit, scale, name
488 */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300489 evlist__for_each_entry(evsel_list, counter) {
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100490 if (!counter->supported)
491 continue;
492
493 /*
494 * Synthesize unit and scale only if it's defined.
495 */
496 if (has_unit(counter)) {
497 err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event);
498 if (err < 0) {
499 pr_err("Couldn't synthesize evsel unit.\n");
500 return err;
501 }
502 }
503
504 if (has_scale(counter)) {
505 err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event);
506 if (err < 0) {
507 pr_err("Couldn't synthesize evsel scale.\n");
508 return err;
509 }
510 }
511
512 if (counter->own_cpus) {
513 err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event);
514 if (err < 0) {
515 pr_err("Couldn't synthesize evsel scale.\n");
516 return err;
517 }
518 }
519
520 /*
521 * Name is needed only for pipe output,
522 * perf.data carries event names.
523 */
524 if (is_pipe) {
525 err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event);
526 if (err < 0) {
527 pr_err("Couldn't synthesize evsel name.\n");
528 return err;
529 }
530 }
531 }
532
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100533 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
534 process_synthesized_event,
535 NULL);
536 if (err < 0) {
537 pr_err("Couldn't synthesize thread map.\n");
538 return err;
539 }
540
541 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
542 process_synthesized_event, NULL);
543 if (err < 0) {
544 pr_err("Couldn't synthesize thread map.\n");
545 return err;
546 }
547
548 err = perf_event__synthesize_stat_config(NULL, &stat_config,
549 process_synthesized_event, NULL);
550 if (err < 0) {
551 pr_err("Couldn't synthesize config.\n");
552 return err;
553 }
554
555 return 0;
556}
557
Jiri Olsa2af46462015-11-05 15:40:49 +0100558#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
559
560static int __store_counter_ids(struct perf_evsel *counter,
561 struct cpu_map *cpus,
562 struct thread_map *threads)
563{
564 int cpu, thread;
565
566 for (cpu = 0; cpu < cpus->nr; cpu++) {
567 for (thread = 0; thread < threads->nr; thread++) {
568 int fd = FD(counter, cpu, thread);
569
570 if (perf_evlist__id_add_fd(evsel_list, counter,
571 cpu, thread, fd) < 0)
572 return -1;
573 }
574 }
575
576 return 0;
577}
578
579static int store_counter_ids(struct perf_evsel *counter)
580{
581 struct cpu_map *cpus = counter->cpus;
582 struct thread_map *threads = counter->threads;
583
584 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
585 return -ENOMEM;
586
587 return __store_counter_ids(counter, cpus, threads);
588}
589
Jiri Olsa82bf3112017-07-26 14:02:06 +0200590static bool perf_evsel__should_store_id(struct perf_evsel *counter)
591{
592 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
593}
594
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700595static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
596{
597 struct perf_evsel *c2, *leader;
598 bool is_open = true;
599
600 leader = evsel->leader;
601 pr_debug("Weak group for %s/%d failed\n",
602 leader->name, leader->nr_members);
603
604 /*
605 * for_each_group_member doesn't work here because it doesn't
606 * include the first entry.
607 */
608 evlist__for_each_entry(evsel_list, c2) {
609 if (c2 == evsel)
610 is_open = false;
611 if (c2->leader == leader) {
612 if (is_open)
613 perf_evsel__close(c2);
614 c2->leader = c2;
615 c2->nr_members = 0;
616 }
617 }
618 return leader;
619}
620
Namhyung Kimacf28922013-03-11 16:43:18 +0900621static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200622{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200623 int interval = stat_config.interval;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300624 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200625 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100626 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100627 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100628 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200629 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300630 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100631 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600632 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200633
Stephane Eranian13370a92013-01-29 12:47:44 +0100634 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300635 ts.tv_sec = interval / USEC_PER_MSEC;
636 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100637 } else {
638 ts.tv_sec = 1;
639 ts.tv_nsec = 0;
640 }
641
Liming Wang60666c62009-12-31 16:05:50 +0800642 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100643 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300644 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900645 perror("failed to prepare workload");
646 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800647 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900648 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000649 }
650
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200651 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300652 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200653
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300654 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300655try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100656 if (create_perf_stat_counter(counter) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700657
658 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700659 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700660 counter->leader != counter &&
661 counter->weak_group) {
662 counter = perf_evsel__reset_weak_group(counter);
663 goto try_again;
664 }
665
David Ahern979987a2012-05-08 09:29:16 -0600666 /*
667 * PPC returns ENXIO for HW counters until 2.6.37
668 * (behavior changed with commit b0a873e).
669 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100670 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600671 errno == ENOENT || errno == EOPNOTSUPP ||
672 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900673 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600674 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300675 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600676 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400677
678 if ((counter->leader != counter) ||
679 !(counter->leader->nr_members > 1))
680 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300681 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900682 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300683 ui__warning("%s\n", msg);
684 goto try_again;
685 }
Ingo Molnarede70292011-04-28 08:48:42 +0200686
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300687 perf_evsel__open_strerror(counter, &target,
688 errno, msg, sizeof(msg));
689 ui__error("%s\n", msg);
690
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200691 if (child_pid != -1)
692 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600693
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200694 return -1;
695 }
David Ahern2cee77c2011-05-30 08:55:59 -0600696 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100697
698 l = strlen(counter->unit);
699 if (l > unit_width)
700 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100701
Jiri Olsa82bf3112017-07-26 14:02:06 +0200702 if (perf_evsel__should_store_id(counter) &&
703 store_counter_ids(counter))
Jiri Olsa2af46462015-11-05 15:40:49 +0100704 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300705 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200706
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300707 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300708 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300709 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300710 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100711 return -1;
712 }
713
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600714 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300715 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600716 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
717 str_error_r(errno, msg, sizeof(msg)));
718 return -1;
719 }
720
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100721 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100722 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100723
Jiri Olsa664c98d2015-11-05 15:40:50 +0100724 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100725 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100726 } else {
727 err = perf_session__write_header(perf_stat.session, evsel_list,
728 fd, false);
729 }
730
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100731 if (err < 0)
732 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100733
Jiri Olsa664c98d2015-11-05 15:40:50 +0100734 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100735 if (err < 0)
736 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100737 }
738
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200739 /*
740 * Enable counters and exec the command:
741 */
742 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100743 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200744
Liming Wang60666c62009-12-31 16:05:50 +0800745 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900746 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100747 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900748
Stephane Eranian13370a92013-01-29 12:47:44 +0100749 if (interval) {
750 while (!waitpid(child_pid, &status, WNOHANG)) {
751 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200752 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100753 }
754 }
Milian Wolffdfc9eec2017-09-12 17:25:23 +0200755 waitpid(child_pid, &status, 0);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300756
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300757 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300758 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300759 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300760 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300761 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300762
Andi Kleen33e49ea2011-09-15 14:31:40 -0700763 if (WIFSIGNALED(status))
764 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800765 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100766 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100767 while (!done) {
768 nanosleep(&ts, NULL);
769 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200770 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100771 }
Liming Wang60666c62009-12-31 16:05:50 +0800772 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200773
Mark Rutland3df33ef2016-08-09 14:04:29 +0100774 disable_counters();
775
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200776 t1 = rdclock();
777
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200778 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200779
Mark Rutland3df33ef2016-08-09 14:04:29 +0100780 /*
781 * Closing a group leader splits the group, and as we only disable
782 * group leaders, results in remaining events becoming enabled. To
783 * avoid arbitrary skew, we must read all counters before closing any
784 * group leaders.
785 */
786 read_counters();
787 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200788
Ingo Molnar42202dd2009-06-13 14:57:28 +0200789 return WEXITSTATUS(status);
790}
791
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300792static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200793{
794 int ret;
795
796 if (pre_cmd) {
797 ret = system(pre_cmd);
798 if (ret)
799 return ret;
800 }
801
802 if (sync_run)
803 sync();
804
805 ret = __run_perf_stat(argc, argv);
806 if (ret)
807 return ret;
808
809 if (post_cmd) {
810 ret = system(post_cmd);
811 if (ret)
812 return ret;
813 }
814
815 return ret;
816}
817
Andi Kleend73515c2015-03-11 07:16:27 -0700818static void print_running(u64 run, u64 ena)
819{
820 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200821 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700822 csv_sep,
823 run,
824 csv_sep,
825 ena ? 100.0 * run / ena : 100.0);
826 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200827 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700828 }
829}
830
Ingo Molnarf99844c2011-04-27 05:35:39 +0200831static void print_noise_pct(double total, double avg)
832{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800833 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200834
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700835 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200836 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600837 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200838 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200839}
840
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200841static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200842{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200843 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200844
Peter Zijlstra849abde2009-09-04 18:23:38 +0200845 if (run_count == 1)
846 return;
847
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300848 ps = evsel->stats;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200849 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200850}
851
Stephane Eranian12c08a92013-02-14 13:57:29 +0100852static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200853{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200854 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100855 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200856 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100857 cpu_map__id_to_socket(id),
858 csv_output ? 0 : -8,
859 cpu_map__id_to_cpu(id),
860 csv_sep,
861 csv_output ? 0 : 4,
862 nr,
863 csv_sep);
864 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100865 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200866 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100867 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100868 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100869 csv_sep,
870 csv_output ? 0 : 4,
871 nr,
872 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100873 break;
874 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200875 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200876 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100877 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100878 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200879 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200880 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200881 csv_output ? 0 : 16,
882 thread_map__comm(evsel->threads, id),
883 csv_output ? 0 : -8,
884 thread_map__pid(evsel->threads, id),
885 csv_sep);
886 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100887 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200888 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100889 default:
890 break;
891 }
892}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200893
Andi Kleen140aead2016-01-30 09:06:49 -0800894struct outstate {
895 FILE *fh;
896 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800897 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800898 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800899 int id, nr;
900 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800901};
902
903#define METRIC_LEN 35
904
905static void new_line_std(void *ctx)
906{
907 struct outstate *os = ctx;
908
909 os->newline = true;
910}
911
912static void do_new_line_std(struct outstate *os)
913{
914 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800915 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800916 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800917 if (stat_config.aggr_mode == AGGR_NONE)
918 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800919 fprintf(os->fh, " ");
920}
921
922static void print_metric_std(void *ctx, const char *color, const char *fmt,
923 const char *unit, double val)
924{
925 struct outstate *os = ctx;
926 FILE *out = os->fh;
927 int n;
928 bool newline = os->newline;
929
930 os->newline = false;
931
932 if (unit == NULL || fmt == NULL) {
933 fprintf(out, "%-*s", METRIC_LEN, "");
934 return;
935 }
936
937 if (newline)
938 do_new_line_std(os);
939
940 n = fprintf(out, " # ");
941 if (color)
942 n += color_fprintf(out, color, fmt, val);
943 else
944 n += fprintf(out, fmt, val);
945 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
946}
947
Andi Kleen92a61f62016-02-29 14:36:21 -0800948static void new_line_csv(void *ctx)
949{
950 struct outstate *os = ctx;
951 int i;
952
953 fputc('\n', os->fh);
954 if (os->prefix)
955 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800956 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800957 for (i = 0; i < os->nfields; i++)
958 fputs(csv_sep, os->fh);
959}
960
961static void print_metric_csv(void *ctx,
962 const char *color __maybe_unused,
963 const char *fmt, const char *unit, double val)
964{
965 struct outstate *os = ctx;
966 FILE *out = os->fh;
967 char buf[64], *vals, *ends;
968
969 if (unit == NULL || fmt == NULL) {
970 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
971 return;
972 }
973 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900974 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800975 while (isdigit(*ends) || *ends == '.')
976 ends++;
977 *ends = 0;
978 while (isspace(*unit))
979 unit++;
980 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
981}
982
Andi Kleen54b50912016-03-03 15:57:36 -0800983#define METRIC_ONLY_LEN 20
984
985/* Filter out some columns that don't work well in metrics only mode */
986
987static bool valid_only_metric(const char *unit)
988{
989 if (!unit)
990 return false;
991 if (strstr(unit, "/sec") ||
992 strstr(unit, "hz") ||
993 strstr(unit, "Hz") ||
994 strstr(unit, "CPUs utilized"))
995 return false;
996 return true;
997}
998
999static const char *fixunit(char *buf, struct perf_evsel *evsel,
1000 const char *unit)
1001{
1002 if (!strncmp(unit, "of all", 6)) {
1003 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
1004 unit);
1005 return buf;
1006 }
1007 return unit;
1008}
1009
1010static void print_metric_only(void *ctx, const char *color, const char *fmt,
1011 const char *unit, double val)
1012{
1013 struct outstate *os = ctx;
1014 FILE *out = os->fh;
1015 int n;
1016 char buf[1024];
1017 unsigned mlen = METRIC_ONLY_LEN;
1018
1019 if (!valid_only_metric(unit))
1020 return;
1021 unit = fixunit(buf, os->evsel, unit);
1022 if (color)
1023 n = color_fprintf(out, color, fmt, val);
1024 else
1025 n = fprintf(out, fmt, val);
1026 if (n > METRIC_ONLY_LEN)
1027 n = METRIC_ONLY_LEN;
1028 if (mlen < strlen(unit))
1029 mlen = strlen(unit) + 1;
1030 fprintf(out, "%*s", mlen - n, "");
1031}
1032
1033static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
1034 const char *fmt,
1035 const char *unit, double val)
1036{
1037 struct outstate *os = ctx;
1038 FILE *out = os->fh;
1039 char buf[64], *vals, *ends;
1040 char tbuf[1024];
1041
1042 if (!valid_only_metric(unit))
1043 return;
1044 unit = fixunit(tbuf, os->evsel, unit);
1045 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +09001046 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -08001047 while (isdigit(*ends) || *ends == '.')
1048 ends++;
1049 *ends = 0;
1050 fprintf(out, "%s%s", vals, csv_sep);
1051}
1052
1053static void new_line_metric(void *ctx __maybe_unused)
1054{
1055}
1056
1057static void print_metric_header(void *ctx, const char *color __maybe_unused,
1058 const char *fmt __maybe_unused,
1059 const char *unit, double val __maybe_unused)
1060{
1061 struct outstate *os = ctx;
1062 char tbuf[1024];
1063
1064 if (!valid_only_metric(unit))
1065 return;
1066 unit = fixunit(tbuf, os->evsel, unit);
1067 if (csv_output)
1068 fprintf(os->fh, "%s%s", unit, csv_sep);
1069 else
1070 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
1071}
1072
Andi Kleenda88c7f2014-09-24 13:50:46 -07001073static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001074{
Jiri Olsa58215222015-07-21 14:31:24 +02001075 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001076 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +01001077 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -06001078 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001079
Stephane Eranian410136f2013-11-12 17:58:49 +01001080 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
1081 fmt_n = csv_output ? "%s" : "%-25s";
1082
Andi Kleenda88c7f2014-09-24 13:50:46 -07001083 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001084
David Ahern4bbe5a62013-09-28 14:28:00 -06001085 scnprintf(name, sizeof(name), "%s%s",
1086 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +01001087
1088 fprintf(output, fmt_v, msecs, csv_sep);
1089
1090 if (csv_output)
1091 fprintf(output, "%s%s", evsel->unit, csv_sep);
1092 else
1093 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1094
1095 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001096
Stephane Eranian023695d2011-02-14 11:20:01 +02001097 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001098 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001099}
1100
Andi Kleen44d49a62016-02-29 14:36:22 -08001101static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1102{
1103 int i;
1104
1105 if (!aggr_get_id)
1106 return 0;
1107
1108 if (stat_config.aggr_mode == AGGR_NONE)
1109 return id;
1110
1111 if (stat_config.aggr_mode == AGGR_GLOBAL)
1112 return 0;
1113
1114 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1115 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1116
1117 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1118 return cpu2;
1119 }
1120 return 0;
1121}
1122
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001123static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1124{
Jiri Olsa58215222015-07-21 14:31:24 +02001125 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001126 double sc = evsel->scale;
1127 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001128
1129 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001130 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001131 } else {
1132 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001133 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001134 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001135 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001136 }
1137
1138 aggr_printout(evsel, id, nr);
1139
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001140 fprintf(output, fmt, avg, csv_sep);
1141
1142 if (evsel->unit)
1143 fprintf(output, "%-*s%s",
1144 csv_output ? 0 : unit_width,
1145 evsel->unit, csv_sep);
1146
1147 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1148
1149 if (evsel->cgrp)
1150 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001151}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001152
Andi Kleenf9483392016-01-30 09:06:50 -08001153static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Andi Kleencb110f42016-01-30 09:06:51 -08001154 char *prefix, u64 run, u64 ena, double noise)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001155{
Andi Kleen140aead2016-01-30 09:06:49 -08001156 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001157 struct outstate os = {
1158 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001159 .prefix = prefix ? prefix : "",
1160 .id = id,
1161 .nr = nr,
1162 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001163 };
Andi Kleen140aead2016-01-30 09:06:49 -08001164 print_metric_t pm = print_metric_std;
1165 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001166
Andi Kleen54b50912016-03-03 15:57:36 -08001167 if (metric_only) {
1168 nl = new_line_metric;
1169 if (csv_output)
1170 pm = print_metric_only_csv;
1171 else
1172 pm = print_metric_only;
1173 } else
1174 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001175
Andi Kleen54b50912016-03-03 15:57:36 -08001176 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001177 static int aggr_fields[] = {
1178 [AGGR_GLOBAL] = 0,
1179 [AGGR_THREAD] = 1,
1180 [AGGR_NONE] = 1,
1181 [AGGR_SOCKET] = 2,
1182 [AGGR_CORE] = 2,
1183 };
1184
1185 pm = print_metric_csv;
1186 nl = new_line_csv;
1187 os.nfields = 3;
1188 os.nfields += aggr_fields[stat_config.aggr_mode];
1189 if (counter->cgrp)
1190 os.nfields++;
1191 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001192 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001193 if (metric_only) {
1194 pm(&os, NULL, "", "", 0);
1195 return;
1196 }
Andi Kleencb110f42016-01-30 09:06:51 -08001197 aggr_printout(counter, id, nr);
1198
1199 fprintf(stat_config.output, "%*s%s",
1200 csv_output ? 0 : 18,
1201 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1202 csv_sep);
1203
Borislav Petkov02d492e2017-02-07 01:40:05 +01001204 if (counter->supported)
1205 print_free_counters_hint = 1;
1206
Andi Kleencb110f42016-01-30 09:06:51 -08001207 fprintf(stat_config.output, "%-*s%s",
1208 csv_output ? 0 : unit_width,
1209 counter->unit, csv_sep);
1210
1211 fprintf(stat_config.output, "%*s",
1212 csv_output ? 0 : -25,
1213 perf_evsel__name(counter));
1214
1215 if (counter->cgrp)
1216 fprintf(stat_config.output, "%s%s",
1217 csv_sep, counter->cgrp->name);
1218
Andi Kleen92a61f62016-02-29 14:36:21 -08001219 if (!csv_output)
1220 pm(&os, NULL, NULL, "", 0);
1221 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001222 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001223 if (csv_output)
1224 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001225 return;
1226 }
1227
Andi Kleen54b50912016-03-03 15:57:36 -08001228 if (metric_only)
1229 /* nothing */;
1230 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001231 nsec_printout(id, nr, counter, uval);
1232 else
1233 abs_printout(id, nr, counter, uval);
1234
Andi Kleen140aead2016-01-30 09:06:49 -08001235 out.print_metric = pm;
1236 out.new_line = nl;
1237 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001238 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001239
Andi Kleen54b50912016-03-03 15:57:36 -08001240 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001241 print_noise(counter, noise);
1242 print_running(run, ena);
1243 }
1244
1245 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001246 first_shadow_cpu(counter, id),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001247 &out, &metric_events);
Andi Kleen54b50912016-03-03 15:57:36 -08001248 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001249 print_noise(counter, noise);
1250 print_running(run, ena);
1251 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001252}
1253
Andi Kleen44d49a62016-02-29 14:36:22 -08001254static void aggr_update_shadow(void)
1255{
1256 int cpu, s2, id, s;
1257 u64 val;
1258 struct perf_evsel *counter;
1259
1260 for (s = 0; s < aggr_map->nr; s++) {
1261 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001262 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001263 val = 0;
1264 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1265 s2 = aggr_get_id(evsel_list->cpus, cpu);
1266 if (s2 != id)
1267 continue;
1268 val += perf_counts(counter->counts, cpu, 0)->val;
1269 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001270 perf_stat__update_shadow_stats(counter, val,
Andi Kleen44d49a62016-02-29 14:36:22 -08001271 first_shadow_cpu(counter, id));
1272 }
1273 }
1274}
1275
Andi Kleen430daf22017-03-20 13:17:00 -07001276static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001277 void (*cb)(struct perf_evsel *counter, void *data,
1278 bool first),
1279 void *data)
1280{
Andi Kleen430daf22017-03-20 13:17:00 -07001281 struct perf_evsel *alias;
1282
1283 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1284 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1285 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1286 alias->scale != counter->scale ||
1287 alias->cgrp != counter->cgrp ||
1288 strcmp(alias->unit, counter->unit) ||
1289 nsec_counter(alias) != nsec_counter(counter))
1290 break;
1291 alias->merged_stat = true;
1292 cb(alias, data, false);
1293 }
1294}
1295
1296static bool collect_data(struct perf_evsel *counter,
1297 void (*cb)(struct perf_evsel *counter, void *data,
1298 bool first),
1299 void *data)
1300{
1301 if (counter->merged_stat)
1302 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001303 cb(counter, data, true);
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -03001304 if (!no_merge && counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001305 collect_all_aliases(counter, cb, data);
1306 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001307}
1308
1309struct aggr_data {
1310 u64 ena, run, val;
1311 int id;
1312 int nr;
1313 int cpu;
1314};
1315
1316static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1317{
1318 struct aggr_data *ad = data;
1319 int cpu, s2;
1320
1321 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1322 struct perf_counts_values *counts;
1323
1324 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1325 if (s2 != ad->id)
1326 continue;
1327 if (first)
1328 ad->nr++;
1329 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001330 /*
1331 * When any result is bad, make them all to give
1332 * consistent output in interval mode.
1333 */
1334 if (counts->ena == 0 || counts->run == 0 ||
1335 counter->counts->scaled == -1) {
1336 ad->ena = 0;
1337 ad->run = 0;
1338 break;
1339 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001340 ad->val += counts->val;
1341 ad->ena += counts->ena;
1342 ad->run += counts->run;
1343 }
1344}
1345
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001346static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001347{
Jiri Olsa58215222015-07-21 14:31:24 +02001348 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001349 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001350 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001351 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001352 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001353 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001354
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001355 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001356 return;
1357
Andi Kleen44d49a62016-02-29 14:36:22 -08001358 aggr_update_shadow();
1359
Andi Kleen54b50912016-03-03 15:57:36 -08001360 /*
1361 * With metric_only everything is on a single line.
1362 * Without each counter has its own line.
1363 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001364 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001365 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001366 if (prefix && metric_only)
1367 fprintf(output, "%s", prefix);
1368
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001369 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001370 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001371 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001372 if (is_duration_time(counter))
1373 continue;
1374
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001375 ad.val = ad.ena = ad.run = 0;
1376 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001377 if (!collect_data(counter, aggr_cb, &ad))
1378 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001379 nr = ad.nr;
1380 ena = ad.ena;
1381 run = ad.run;
1382 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001383 if (first && metric_only) {
1384 first = false;
1385 aggr_printout(counter, id, nr);
1386 }
1387 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001388 fprintf(output, "%s", prefix);
1389
Stephane Eranian410136f2013-11-12 17:58:49 +01001390 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001391 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
Andi Kleen54b50912016-03-03 15:57:36 -08001392 if (!metric_only)
1393 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001394 }
Andi Kleen54b50912016-03-03 15:57:36 -08001395 if (metric_only)
1396 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001397 }
1398}
1399
Jiri Olsa32b8af82015-06-26 11:29:27 +02001400static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1401{
Jiri Olsa58215222015-07-21 14:31:24 +02001402 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001403 int nthreads = thread_map__nr(counter->threads);
1404 int ncpus = cpu_map__nr(counter->cpus);
1405 int cpu, thread;
1406 double uval;
1407
1408 for (thread = 0; thread < nthreads; thread++) {
1409 u64 ena = 0, run = 0, val = 0;
1410
1411 for (cpu = 0; cpu < ncpus; cpu++) {
1412 val += perf_counts(counter->counts, cpu, thread)->val;
1413 ena += perf_counts(counter->counts, cpu, thread)->ena;
1414 run += perf_counts(counter->counts, cpu, thread)->run;
1415 }
1416
1417 if (prefix)
1418 fprintf(output, "%s", prefix);
1419
1420 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001421 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001422 fputc('\n', output);
1423 }
1424}
1425
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001426struct caggr_data {
1427 double avg, avg_enabled, avg_running;
1428};
1429
1430static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1431 bool first __maybe_unused)
1432{
1433 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001434 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001435
1436 cd->avg += avg_stats(&ps->res_stats[0]);
1437 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1438 cd->avg_running += avg_stats(&ps->res_stats[2]);
1439}
1440
Ingo Molnar42202dd2009-06-13 14:57:28 +02001441/*
1442 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001443 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001444 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001445static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001446{
Jiri Olsa58215222015-07-21 14:31:24 +02001447 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001448 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001449 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001450
Andi Kleen430daf22017-03-20 13:17:00 -07001451 if (!collect_data(counter, counter_aggr_cb, &cd))
1452 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001453
Andi Kleen54b50912016-03-03 15:57:36 -08001454 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001455 fprintf(output, "%s", prefix);
1456
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001457 uval = cd.avg * counter->scale;
1458 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, cd.avg);
Andi Kleen54b50912016-03-03 15:57:36 -08001459 if (!metric_only)
1460 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001461}
1462
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001463static void counter_cb(struct perf_evsel *counter, void *data,
1464 bool first __maybe_unused)
1465{
1466 struct aggr_data *ad = data;
1467
1468 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1469 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1470 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1471}
1472
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001473/*
1474 * Print out the results of a single counter:
1475 * does not use aggregated count in system-wide
1476 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001477static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001478{
Jiri Olsa58215222015-07-21 14:31:24 +02001479 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001480 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001481 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001482 int cpu;
1483
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001484 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001485 struct aggr_data ad = { .cpu = cpu };
1486
Andi Kleen430daf22017-03-20 13:17:00 -07001487 if (!collect_data(counter, counter_cb, &ad))
1488 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001489 val = ad.val;
1490 ena = ad.ena;
1491 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001492
1493 if (prefix)
1494 fprintf(output, "%s", prefix);
1495
Stephane Eranian410136f2013-11-12 17:58:49 +01001496 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001497 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001498
Stephane Eranian4aa90152011-08-15 22:22:33 +02001499 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001500 }
1501}
1502
Andi Kleen206cab62016-03-03 15:57:37 -08001503static void print_no_aggr_metric(char *prefix)
1504{
1505 int cpu;
1506 int nrcpus = 0;
1507 struct perf_evsel *counter;
1508 u64 ena, run, val;
1509 double uval;
1510
1511 nrcpus = evsel_list->cpus->nr;
1512 for (cpu = 0; cpu < nrcpus; cpu++) {
1513 bool first = true;
1514
1515 if (prefix)
1516 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001517 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001518 if (is_duration_time(counter))
1519 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001520 if (first) {
1521 aggr_printout(counter, cpu, 0);
1522 first = false;
1523 }
1524 val = perf_counts(counter->counts, cpu, 0)->val;
1525 ena = perf_counts(counter->counts, cpu, 0)->ena;
1526 run = perf_counts(counter->counts, cpu, 0)->run;
1527
1528 uval = val * counter->scale;
1529 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1530 }
1531 fputc('\n', stat_config.output);
1532 }
1533}
1534
Andi Kleen54b50912016-03-03 15:57:36 -08001535static int aggr_header_lens[] = {
1536 [AGGR_CORE] = 18,
1537 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001538 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001539 [AGGR_THREAD] = 24,
1540 [AGGR_GLOBAL] = 0,
1541};
1542
Andi Kleenc51fd632016-05-24 12:52:39 -07001543static const char *aggr_header_csv[] = {
1544 [AGGR_CORE] = "core,cpus,",
1545 [AGGR_SOCKET] = "socket,cpus",
1546 [AGGR_NONE] = "cpu,",
1547 [AGGR_THREAD] = "comm-pid,",
1548 [AGGR_GLOBAL] = ""
1549};
1550
Andi Kleen41c8ca22016-05-24 12:52:38 -07001551static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001552{
1553 struct perf_stat_output_ctx out;
1554 struct perf_evsel *counter;
1555 struct outstate os = {
1556 .fh = stat_config.output
1557 };
1558
1559 if (prefix)
1560 fprintf(stat_config.output, "%s", prefix);
1561
Andi Kleen41c8ca22016-05-24 12:52:38 -07001562 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001563 fprintf(stat_config.output, "%*s",
1564 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001565 if (csv_output) {
1566 if (stat_config.interval)
1567 fputs("time,", stat_config.output);
1568 fputs(aggr_header_csv[stat_config.aggr_mode],
1569 stat_config.output);
1570 }
Andi Kleen54b50912016-03-03 15:57:36 -08001571
1572 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001573 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001574 if (is_duration_time(counter))
1575 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001576 os.evsel = counter;
1577 out.ctx = &os;
1578 out.print_metric = print_metric_header;
1579 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001580 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001581 os.evsel = counter;
1582 perf_stat__print_shadow_stats(counter, 0,
1583 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001584 &out,
1585 &metric_events);
Andi Kleen54b50912016-03-03 15:57:36 -08001586 }
1587 fputc('\n', stat_config.output);
1588}
1589
Jiri Olsad4f63a42015-06-26 11:29:26 +02001590static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001591{
Jiri Olsa58215222015-07-21 14:31:24 +02001592 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001593 static int num_print_interval;
1594
1595 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1596
Andi Kleen41c8ca22016-05-24 12:52:38 -07001597 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001598 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001599 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001600 fprintf(output, "# time socket cpus");
1601 if (!metric_only)
1602 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001603 break;
1604 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001605 fprintf(output, "# time core cpus");
1606 if (!metric_only)
1607 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001608 break;
1609 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001610 fprintf(output, "# time CPU");
1611 if (!metric_only)
1612 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001613 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001614 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001615 fprintf(output, "# time comm-pid");
1616 if (!metric_only)
1617 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001618 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001619 case AGGR_GLOBAL:
1620 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001621 fprintf(output, "# time");
1622 if (!metric_only)
1623 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001624 case AGGR_UNSET:
1625 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001626 }
1627 }
1628
Andi Kleen41c8ca22016-05-24 12:52:38 -07001629 if (num_print_interval == 0 && metric_only)
1630 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001631 if (++num_print_interval == 25)
1632 num_print_interval = 0;
1633}
1634
1635static void print_header(int argc, const char **argv)
1636{
Jiri Olsa58215222015-07-21 14:31:24 +02001637 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001638 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001639
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001640 fflush(stdout);
1641
Stephane Eraniand7470b62010-12-01 18:49:05 +02001642 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001643 fprintf(output, "\n");
1644 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001645 if (target.system_wide)
1646 fprintf(output, "\'system wide");
1647 else if (target.cpu_list)
1648 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001649 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001650 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1651 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001652 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001653 } else if (target.pid)
1654 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001655 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001656 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001657
Stephane Eranian4aa90152011-08-15 22:22:33 +02001658 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001659 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001660 fprintf(output, " (%d runs)", run_count);
1661 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001662 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001663}
1664
1665static void print_footer(void)
1666{
Jiri Olsa58215222015-07-21 14:31:24 +02001667 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001668 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001669
Jiri Olsad4f63a42015-06-26 11:29:26 +02001670 if (!null_run)
1671 fprintf(output, "\n");
1672 fprintf(output, " %17.9f seconds time elapsed",
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001673 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001674 if (run_count > 1) {
1675 fprintf(output, " ");
1676 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1677 avg_stats(&walltime_nsecs_stats));
1678 }
1679 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001680
Andi Kleen918c7b062017-05-22 18:00:16 -07001681 if (print_free_counters_hint &&
1682 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1683 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001684 fprintf(output,
1685"Some events weren't counted. Try disabling the NMI watchdog:\n"
1686" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1687" perf stat ...\n"
1688" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001689}
1690
1691static void print_counters(struct timespec *ts, int argc, const char **argv)
1692{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001693 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001694 struct perf_evsel *counter;
1695 char buf[64], *prefix = NULL;
1696
Jiri Olsa664c98d2015-11-05 15:40:50 +01001697 /* Do not print anything if we record to the pipe. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001698 if (STAT_RECORD && perf_stat.data.is_pipe)
Jiri Olsa664c98d2015-11-05 15:40:50 +01001699 return;
1700
Jiri Olsad4f63a42015-06-26 11:29:26 +02001701 if (interval)
1702 print_interval(prefix = buf, ts);
1703 else
1704 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001705
Andi Kleen54b50912016-03-03 15:57:36 -08001706 if (metric_only) {
1707 static int num_print_iv;
1708
Andi Kleen41c8ca22016-05-24 12:52:38 -07001709 if (num_print_iv == 0 && !interval)
1710 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001711 if (num_print_iv++ == 25)
1712 num_print_iv = 0;
1713 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1714 fprintf(stat_config.output, "%s", prefix);
1715 }
1716
Jiri Olsa421a50f2015-07-21 14:31:22 +02001717 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001718 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001719 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001720 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001721 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001722 case AGGR_THREAD:
Andi Kleene864c5c2017-08-31 12:40:35 -07001723 evlist__for_each_entry(evsel_list, counter) {
1724 if (is_duration_time(counter))
1725 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001726 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001727 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001728 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001729 case AGGR_GLOBAL:
Andi Kleene864c5c2017-08-31 12:40:35 -07001730 evlist__for_each_entry(evsel_list, counter) {
1731 if (is_duration_time(counter))
1732 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001733 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001734 }
Andi Kleen54b50912016-03-03 15:57:36 -08001735 if (metric_only)
1736 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001737 break;
1738 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001739 if (metric_only)
1740 print_no_aggr_metric(prefix);
1741 else {
Andi Kleene864c5c2017-08-31 12:40:35 -07001742 evlist__for_each_entry(evsel_list, counter) {
1743 if (is_duration_time(counter))
1744 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001745 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001746 }
Andi Kleen206cab62016-03-03 15:57:37 -08001747 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001748 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001749 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001750 default:
1751 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001752 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001753
Jiri Olsad4f63a42015-06-26 11:29:26 +02001754 if (!interval && !csv_output)
1755 print_footer();
1756
Jiri Olsa58215222015-07-21 14:31:24 +02001757 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001758}
1759
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001760static volatile int signr = -1;
1761
Ingo Molnar52425192009-05-26 09:17:18 +02001762static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001763{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001764 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001765 done = 1;
1766
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001767 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001768 /*
1769 * render child_pid harmless
1770 * won't send SIGTERM to a random
1771 * process in case of race condition
1772 * and fast PID recycling
1773 */
1774 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001775}
1776
1777static void sig_atexit(void)
1778{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001779 sigset_t set, oset;
1780
1781 /*
1782 * avoid race condition with SIGCHLD handler
1783 * in skip_signal() which is modifying child_pid
1784 * goal is to avoid send SIGTERM to a random
1785 * process
1786 */
1787 sigemptyset(&set);
1788 sigaddset(&set, SIGCHLD);
1789 sigprocmask(SIG_BLOCK, &set, &oset);
1790
Chris Wilson933da832009-10-04 01:35:01 +01001791 if (child_pid != -1)
1792 kill(child_pid, SIGTERM);
1793
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001794 sigprocmask(SIG_SETMASK, &oset, NULL);
1795
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001796 if (signr == -1)
1797 return;
1798
1799 signal(signr, SIG_DFL);
1800 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001801}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001802
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001803static int stat__set_big_num(const struct option *opt __maybe_unused,
1804 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001805{
1806 big_num_opt = unset ? 0 : 1;
1807 return 0;
1808}
1809
Andi Kleen44b1e602016-05-30 12:49:42 -03001810static int enable_metric_only(const struct option *opt __maybe_unused,
1811 const char *s __maybe_unused, int unset)
1812{
1813 force_metric_only = true;
1814 metric_only = !unset;
1815 return 0;
1816}
1817
Andi Kleenb18f3e32017-08-31 12:40:31 -07001818static int parse_metric_groups(const struct option *opt,
1819 const char *str,
1820 int unset __maybe_unused)
1821{
1822 return metricgroup__parse_groups(opt, str, &metric_events);
1823}
1824
Jiri Olsae0547312015-11-05 15:40:45 +01001825static const struct option stat_options[] = {
1826 OPT_BOOLEAN('T', "transaction", &transaction_run,
1827 "hardware transaction statistics"),
1828 OPT_CALLBACK('e', "event", &evsel_list, "event",
1829 "event selector. use 'perf list' to list available events",
1830 parse_events_option),
1831 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1832 "event filter", parse_filter),
1833 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1834 "child tasks do not inherit counters"),
1835 OPT_STRING('p', "pid", &target.pid, "pid",
1836 "stat events on existing process id"),
1837 OPT_STRING('t', "tid", &target.tid, "tid",
1838 "stat events on existing thread id"),
1839 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1840 "system-wide collection from all CPUs"),
1841 OPT_BOOLEAN('g', "group", &group,
1842 "put the counters into a counter group"),
1843 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1844 OPT_INCR('v', "verbose", &verbose,
1845 "be more verbose (show counter open errors, etc)"),
1846 OPT_INTEGER('r', "repeat", &run_count,
1847 "repeat command and print average + stddev (max: 100, forever: 0)"),
1848 OPT_BOOLEAN('n', "null", &null_run,
1849 "null run - dont start any counters"),
1850 OPT_INCR('d', "detailed", &detailed_run,
1851 "detailed run - start a lot of events"),
1852 OPT_BOOLEAN('S', "sync", &sync_run,
1853 "call sync() before starting a run"),
1854 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1855 "print large numbers with thousands\' separators",
1856 stat__set_big_num),
1857 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1858 "list of cpus to monitor in system-wide"),
1859 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1860 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001861 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001862 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1863 "print counts with custom separator"),
1864 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1865 "monitor event in cgroup name only", parse_cgroups),
1866 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1867 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1868 OPT_INTEGER(0, "log-fd", &output_fd,
1869 "log output to fd, instead of stderr"),
1870 OPT_STRING(0, "pre", &pre_cmd, "command",
1871 "command to run prior to the measured command"),
1872 OPT_STRING(0, "post", &post_cmd, "command",
1873 "command to run after to the measured command"),
1874 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1875 "print counts at regular interval in ms (>= 10)"),
1876 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1877 "aggregate counts per processor socket", AGGR_SOCKET),
1878 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1879 "aggregate counts per physical processor core", AGGR_CORE),
1880 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1881 "aggregate counts per thread", AGGR_THREAD),
1882 OPT_UINTEGER('D', "delay", &initial_delay,
1883 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001884 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1885 "Only print computed metrics. No raw values", enable_metric_only),
1886 OPT_BOOLEAN(0, "topdown", &topdown_run,
1887 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001888 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1889 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001890 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1891 "monitor specified metrics or metric groups (separated by ,)",
1892 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01001893 OPT_END()
1894};
1895
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001896static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1897{
1898 return cpu_map__get_socket(map, cpu, NULL);
1899}
1900
1901static int perf_stat__get_core(struct cpu_map *map, int cpu)
1902{
1903 return cpu_map__get_core(map, cpu, NULL);
1904}
1905
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001906static int cpu_map__get_max(struct cpu_map *map)
1907{
1908 int i, max = -1;
1909
1910 for (i = 0; i < map->nr; i++) {
1911 if (map->map[i] > max)
1912 max = map->map[i];
1913 }
1914
1915 return max;
1916}
1917
1918static struct cpu_map *cpus_aggr_map;
1919
1920static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1921{
1922 int cpu;
1923
1924 if (idx >= map->nr)
1925 return -1;
1926
1927 cpu = map->map[idx];
1928
1929 if (cpus_aggr_map->map[cpu] == -1)
1930 cpus_aggr_map->map[cpu] = get_id(map, idx);
1931
1932 return cpus_aggr_map->map[cpu];
1933}
1934
1935static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1936{
1937 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1938}
1939
1940static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1941{
1942 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1943}
1944
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001945static int perf_stat_init_aggr_mode(void)
1946{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001947 int nr;
1948
Jiri Olsa421a50f2015-07-21 14:31:22 +02001949 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001950 case AGGR_SOCKET:
1951 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1952 perror("cannot build socket map");
1953 return -1;
1954 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001955 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001956 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001957 case AGGR_CORE:
1958 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1959 perror("cannot build core map");
1960 return -1;
1961 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001962 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001963 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001964 case AGGR_NONE:
1965 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001966 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001967 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001968 default:
1969 break;
1970 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001971
1972 /*
1973 * The evsel_list->cpus is the base we operate on,
1974 * taking the highest cpu number to be the size of
1975 * the aggregation translate cpumap.
1976 */
1977 nr = cpu_map__get_max(evsel_list->cpus);
1978 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1979 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001980}
1981
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001982static void perf_stat__exit_aggr_mode(void)
1983{
1984 cpu_map__put(aggr_map);
1985 cpu_map__put(cpus_aggr_map);
1986 aggr_map = NULL;
1987 cpus_aggr_map = NULL;
1988}
1989
Jiri Olsa68d702f2015-11-05 15:40:58 +01001990static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1991{
1992 int cpu;
1993
1994 if (idx > map->nr)
1995 return -1;
1996
1997 cpu = map->map[idx];
1998
Jan Stancekda8a58b2017-02-17 12:10:26 +01001999 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002000 return -1;
2001
2002 return cpu;
2003}
2004
2005static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
2006{
2007 struct perf_env *env = data;
2008 int cpu = perf_env__get_cpu(env, map, idx);
2009
2010 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
2011}
2012
2013static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
2014{
2015 struct perf_env *env = data;
2016 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
2017
2018 if (cpu != -1) {
2019 int socket_id = env->cpu[cpu].socket_id;
2020
2021 /*
2022 * Encode socket in upper 16 bits
2023 * core_id is relative to socket, and
2024 * we need a global id. So we combine
2025 * socket + core id.
2026 */
2027 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
2028 }
2029
2030 return core;
2031}
2032
2033static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
2034 struct cpu_map **sockp)
2035{
2036 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
2037}
2038
2039static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
2040 struct cpu_map **corep)
2041{
2042 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
2043}
2044
2045static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
2046{
2047 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
2048}
2049
2050static int perf_stat__get_core_file(struct cpu_map *map, int idx)
2051{
2052 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
2053}
2054
2055static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2056{
2057 struct perf_env *env = &st->session->header.env;
2058
2059 switch (stat_config.aggr_mode) {
2060 case AGGR_SOCKET:
2061 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2062 perror("cannot build socket map");
2063 return -1;
2064 }
2065 aggr_get_id = perf_stat__get_socket_file;
2066 break;
2067 case AGGR_CORE:
2068 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2069 perror("cannot build core map");
2070 return -1;
2071 }
2072 aggr_get_id = perf_stat__get_core_file;
2073 break;
2074 case AGGR_NONE:
2075 case AGGR_GLOBAL:
2076 case AGGR_THREAD:
2077 case AGGR_UNSET:
2078 default:
2079 break;
2080 }
2081
2082 return 0;
2083}
2084
Andi Kleen44b1e602016-05-30 12:49:42 -03002085static int topdown_filter_events(const char **attr, char **str, bool use_group)
2086{
2087 int off = 0;
2088 int i;
2089 int len = 0;
2090 char *s;
2091
2092 for (i = 0; attr[i]; i++) {
2093 if (pmu_have_event("cpu", attr[i])) {
2094 len += strlen(attr[i]) + 1;
2095 attr[i - off] = attr[i];
2096 } else
2097 off++;
2098 }
2099 attr[i - off] = NULL;
2100
2101 *str = malloc(len + 1 + 2);
2102 if (!*str)
2103 return -1;
2104 s = *str;
2105 if (i - off == 0) {
2106 *s = 0;
2107 return 0;
2108 }
2109 if (use_group)
2110 *s++ = '{';
2111 for (i = 0; attr[i]; i++) {
2112 strcpy(s, attr[i]);
2113 s += strlen(s);
2114 *s++ = ',';
2115 }
2116 if (use_group) {
2117 s[-1] = '}';
2118 *s = 0;
2119 } else
2120 s[-1] = 0;
2121 return 0;
2122}
2123
2124__weak bool arch_topdown_check_group(bool *warn)
2125{
2126 *warn = false;
2127 return false;
2128}
2129
2130__weak void arch_topdown_group_warn(void)
2131{
2132}
2133
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002134/*
2135 * Add default attributes, if there were no attributes specified or
2136 * if -d/--detailed, -d -d or -d -d -d is used:
2137 */
2138static int add_default_attributes(void)
2139{
Andi Kleen44b1e602016-05-30 12:49:42 -03002140 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002141 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002142
2143 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2144 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2145 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2146 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2147
2148 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002149};
2150 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002151 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002152};
2153 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002154 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002155};
2156 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002157 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2158 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2159 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2160
2161};
2162
2163/*
2164 * Detailed stats (-d), covering the L1 and last level data caches:
2165 */
2166 struct perf_event_attr detailed_attrs[] = {
2167
2168 { .type = PERF_TYPE_HW_CACHE,
2169 .config =
2170 PERF_COUNT_HW_CACHE_L1D << 0 |
2171 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2172 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2173
2174 { .type = PERF_TYPE_HW_CACHE,
2175 .config =
2176 PERF_COUNT_HW_CACHE_L1D << 0 |
2177 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2178 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2179
2180 { .type = PERF_TYPE_HW_CACHE,
2181 .config =
2182 PERF_COUNT_HW_CACHE_LL << 0 |
2183 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2184 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2185
2186 { .type = PERF_TYPE_HW_CACHE,
2187 .config =
2188 PERF_COUNT_HW_CACHE_LL << 0 |
2189 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2190 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2191};
2192
2193/*
2194 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2195 */
2196 struct perf_event_attr very_detailed_attrs[] = {
2197
2198 { .type = PERF_TYPE_HW_CACHE,
2199 .config =
2200 PERF_COUNT_HW_CACHE_L1I << 0 |
2201 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2202 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2203
2204 { .type = PERF_TYPE_HW_CACHE,
2205 .config =
2206 PERF_COUNT_HW_CACHE_L1I << 0 |
2207 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2208 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2209
2210 { .type = PERF_TYPE_HW_CACHE,
2211 .config =
2212 PERF_COUNT_HW_CACHE_DTLB << 0 |
2213 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2214 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2215
2216 { .type = PERF_TYPE_HW_CACHE,
2217 .config =
2218 PERF_COUNT_HW_CACHE_DTLB << 0 |
2219 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2220 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2221
2222 { .type = PERF_TYPE_HW_CACHE,
2223 .config =
2224 PERF_COUNT_HW_CACHE_ITLB << 0 |
2225 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2226 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2227
2228 { .type = PERF_TYPE_HW_CACHE,
2229 .config =
2230 PERF_COUNT_HW_CACHE_ITLB << 0 |
2231 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2232 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2233
2234};
2235
2236/*
2237 * Very, very detailed stats (-d -d -d), adding prefetch events:
2238 */
2239 struct perf_event_attr very_very_detailed_attrs[] = {
2240
2241 { .type = PERF_TYPE_HW_CACHE,
2242 .config =
2243 PERF_COUNT_HW_CACHE_L1D << 0 |
2244 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2245 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2246
2247 { .type = PERF_TYPE_HW_CACHE,
2248 .config =
2249 PERF_COUNT_HW_CACHE_L1D << 0 |
2250 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2251 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2252};
2253
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002254 /* Set attrs if no event is selected and !null_run: */
2255 if (null_run)
2256 return 0;
2257
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002258 if (transaction_run) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002259 if (pmu_have_event("cpu", "cycles-ct") &&
2260 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02002261 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002262 else
Jiri Olsaa4547422015-06-03 16:25:53 +02002263 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2264 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002265 fprintf(stderr, "Cannot set up transaction events\n");
2266 return -1;
2267 }
2268 return 0;
2269 }
2270
Kan Liangdaefd0b2017-05-26 12:05:38 -07002271 if (smi_cost) {
2272 int smi;
2273
2274 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2275 fprintf(stderr, "freeze_on_smi is not supported.\n");
2276 return -1;
2277 }
2278
2279 if (!smi) {
2280 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2281 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2282 return -1;
2283 }
2284 smi_reset = true;
2285 }
2286
2287 if (pmu_have_event("msr", "aperf") &&
2288 pmu_have_event("msr", "smi")) {
2289 if (!force_metric_only)
2290 metric_only = true;
2291 err = parse_events(evsel_list, smi_cost_attrs, NULL);
2292 } else {
2293 fprintf(stderr, "To measure SMI cost, it needs "
2294 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2295 return -1;
2296 }
2297 if (err) {
2298 fprintf(stderr, "Cannot set up SMI cost events\n");
2299 return -1;
2300 }
2301 return 0;
2302 }
2303
Andi Kleen44b1e602016-05-30 12:49:42 -03002304 if (topdown_run) {
2305 char *str = NULL;
2306 bool warn = false;
2307
2308 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2309 stat_config.aggr_mode != AGGR_CORE) {
2310 pr_err("top down event configuration requires --per-core mode\n");
2311 return -1;
2312 }
2313 stat_config.aggr_mode = AGGR_CORE;
2314 if (nr_cgroups || !target__has_cpu(&target)) {
2315 pr_err("top down event configuration requires system-wide mode (-a)\n");
2316 return -1;
2317 }
2318
2319 if (!force_metric_only)
2320 metric_only = true;
2321 if (topdown_filter_events(topdown_attrs, &str,
2322 arch_topdown_check_group(&warn)) < 0) {
2323 pr_err("Out of memory\n");
2324 return -1;
2325 }
2326 if (topdown_attrs[0] && str) {
2327 if (warn)
2328 arch_topdown_group_warn();
2329 err = parse_events(evsel_list, str, NULL);
2330 if (err) {
2331 fprintf(stderr,
2332 "Cannot set up top down events %s: %d\n",
2333 str, err);
2334 free(str);
2335 return -1;
2336 }
2337 } else {
2338 fprintf(stderr, "System does not support topdown\n");
2339 return -1;
2340 }
2341 free(str);
2342 }
2343
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002344 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002345 if (target__has_cpu(&target))
2346 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2347
Andi Kleen9dec4472016-02-26 16:27:56 -08002348 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2349 return -1;
2350 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2351 if (perf_evlist__add_default_attrs(evsel_list,
2352 frontend_attrs) < 0)
2353 return -1;
2354 }
2355 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2356 if (perf_evlist__add_default_attrs(evsel_list,
2357 backend_attrs) < 0)
2358 return -1;
2359 }
2360 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002361 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002362 }
2363
2364 /* Detailed events get appended to the event list: */
2365
2366 if (detailed_run < 1)
2367 return 0;
2368
2369 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002370 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002371 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002372
2373 if (detailed_run < 2)
2374 return 0;
2375
2376 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002377 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002378 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002379
2380 if (detailed_run < 3)
2381 return 0;
2382
2383 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002384 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002385}
2386
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002387static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002388 "perf stat record [<options>]",
2389 NULL,
2390};
2391
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002392static void init_features(struct perf_session *session)
2393{
2394 int feat;
2395
2396 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2397 perf_header__set_feat(&session->header, feat);
2398
2399 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2400 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2401 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2402 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2403}
2404
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002405static int __cmd_record(int argc, const char **argv)
2406{
2407 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002408 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002409
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002410 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002411 PARSE_OPT_STOP_AT_NON_OPTION);
2412
2413 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002414 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002415
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002416 if (run_count != 1 || forever) {
2417 pr_err("Cannot use -r option with perf stat record.\n");
2418 return -1;
2419 }
2420
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002421 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002422 if (session == NULL) {
2423 pr_err("Perf session creation failed.\n");
2424 return -1;
2425 }
2426
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002427 init_features(session);
2428
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002429 session->evlist = evsel_list;
2430 perf_stat.session = session;
2431 perf_stat.record = true;
2432 return argc;
2433}
2434
Jiri Olsaa56f9392015-11-05 15:40:59 +01002435static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2436 union perf_event *event,
2437 struct perf_session *session)
2438{
Andi Kleene3b03b62016-05-05 16:04:03 -07002439 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002440 struct perf_evsel *counter;
2441 struct timespec tsh, *ts = NULL;
2442 const char **argv = session->header.env.cmdline_argv;
2443 int argc = session->header.env.nr_cmdline;
2444
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002445 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002446 perf_stat_process_counter(&stat_config, counter);
2447
Andi Kleene3b03b62016-05-05 16:04:03 -07002448 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2449 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002450
Andi Kleene3b03b62016-05-05 16:04:03 -07002451 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002452 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2453 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002454 ts = &tsh;
2455 }
2456
2457 print_counters(ts, argc, argv);
2458 return 0;
2459}
2460
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002461static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002462int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002463 union perf_event *event,
2464 struct perf_session *session __maybe_unused)
2465{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002466 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2467
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002468 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002469
Jiri Olsa89af4e02015-11-05 15:41:02 +01002470 if (cpu_map__empty(st->cpus)) {
2471 if (st->aggr_mode != AGGR_UNSET)
2472 pr_warning("warning: processing task data, aggregation mode not set\n");
2473 return 0;
2474 }
2475
2476 if (st->aggr_mode != AGGR_UNSET)
2477 stat_config.aggr_mode = st->aggr_mode;
2478
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002479 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002480 perf_stat_init_aggr_mode();
2481 else
2482 perf_stat_init_aggr_mode_file(st);
2483
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002484 return 0;
2485}
2486
Jiri Olsa1975d362015-11-05 15:40:56 +01002487static int set_maps(struct perf_stat *st)
2488{
2489 if (!st->cpus || !st->threads)
2490 return 0;
2491
2492 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2493 return -EINVAL;
2494
2495 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2496
2497 if (perf_evlist__alloc_stats(evsel_list, true))
2498 return -ENOMEM;
2499
2500 st->maps_allocated = true;
2501 return 0;
2502}
2503
2504static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002505int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002506 union perf_event *event,
2507 struct perf_session *session __maybe_unused)
2508{
2509 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2510
2511 if (st->threads) {
2512 pr_warning("Extra thread map event, ignoring.\n");
2513 return 0;
2514 }
2515
2516 st->threads = thread_map__new_event(&event->thread_map);
2517 if (!st->threads)
2518 return -ENOMEM;
2519
2520 return set_maps(st);
2521}
2522
2523static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002524int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002525 union perf_event *event,
2526 struct perf_session *session __maybe_unused)
2527{
2528 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2529 struct cpu_map *cpus;
2530
2531 if (st->cpus) {
2532 pr_warning("Extra cpu map event, ignoring.\n");
2533 return 0;
2534 }
2535
2536 cpus = cpu_map__new_data(&event->cpu_map.data);
2537 if (!cpus)
2538 return -ENOMEM;
2539
2540 st->cpus = cpus;
2541 return set_maps(st);
2542}
2543
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002544static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002545 "perf stat report [<options>]",
2546 NULL,
2547};
2548
2549static struct perf_stat perf_stat = {
2550 .tool = {
2551 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002552 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002553 .thread_map = process_thread_map_event,
2554 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002555 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002556 .stat = perf_event__process_stat_event,
2557 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002558 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002559 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002560};
2561
2562static int __cmd_report(int argc, const char **argv)
2563{
2564 struct perf_session *session;
2565 const struct option options[] = {
2566 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002567 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2568 "aggregate counts per processor socket", AGGR_SOCKET),
2569 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2570 "aggregate counts per physical processor core", AGGR_CORE),
2571 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2572 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002573 OPT_END()
2574 };
2575 struct stat st;
2576 int ret;
2577
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002578 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002579
2580 if (!input_name || !strlen(input_name)) {
2581 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2582 input_name = "-";
2583 else
2584 input_name = "perf.data";
2585 }
2586
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002587 perf_stat.data.file.path = input_name;
2588 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002589
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002590 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002591 if (session == NULL)
2592 return -1;
2593
2594 perf_stat.session = session;
2595 stat_config.output = stderr;
2596 evsel_list = session->evlist;
2597
2598 ret = perf_session__process_events(session);
2599 if (ret)
2600 return ret;
2601
2602 perf_session__delete(session);
2603 return 0;
2604}
2605
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002606static void setup_system_wide(int forks)
2607{
2608 /*
2609 * Make system wide (-a) the default target if
2610 * no target was specified and one of following
2611 * conditions is met:
2612 *
2613 * - there's no workload specified
2614 * - there is workload specified but all requested
2615 * events are system wide events
2616 */
2617 if (!target__none(&target))
2618 return;
2619
2620 if (!forks)
2621 target.system_wide = true;
2622 else {
2623 struct perf_evsel *counter;
2624
2625 evlist__for_each_entry(evsel_list, counter) {
2626 if (!counter->system_wide)
2627 return;
2628 }
2629
2630 if (evsel_list->nr_entries)
2631 target.system_wide = true;
2632 }
2633}
2634
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002635int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002636{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002637 const char * const stat_usage[] = {
2638 "perf stat [<options>] [<command>]",
2639 NULL
2640 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002641 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002642 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002643 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002644 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002645 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002646
Stephane Eranian5af52b52010-05-18 15:00:01 +02002647 setlocale(LC_ALL, "");
2648
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002649 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002650 if (evsel_list == NULL)
2651 return -ENOMEM;
2652
Wang Nan1669e502016-02-19 11:43:58 +00002653 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002654 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2655 (const char **) stat_usage,
2656 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002657 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002658 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002659
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002660 if (csv_sep) {
2661 csv_output = true;
2662 if (!strcmp(csv_sep, "\\t"))
2663 csv_sep = "\t";
2664 } else
2665 csv_sep = DEFAULT_SEPARATOR;
2666
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002667 if (argc && !strncmp(argv[0], "rec", 3)) {
2668 argc = __cmd_record(argc, argv);
2669 if (argc < 0)
2670 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002671 } else if (argc && !strncmp(argv[0], "rep", 3))
2672 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002673
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002674 interval = stat_config.interval;
2675
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002676 /*
2677 * For record command the -o is already taken care of.
2678 */
2679 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002680 output = NULL;
2681
Jim Cromie56f3bae2011-09-07 17:14:00 -06002682 if (output_name && output_fd) {
2683 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002684 parse_options_usage(stat_usage, stat_options, "o", 1);
2685 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002686 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002687 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002688
Andi Kleen54b50912016-03-03 15:57:36 -08002689 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2690 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2691 goto out;
2692 }
2693
Andi Kleen54b50912016-03-03 15:57:36 -08002694 if (metric_only && run_count > 1) {
2695 fprintf(stderr, "--metric-only is not supported with -r\n");
2696 goto out;
2697 }
2698
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002699 if (output_fd < 0) {
2700 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002701 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002702 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002703 }
2704
Stephane Eranian4aa90152011-08-15 22:22:33 +02002705 if (!output) {
2706 struct timespec tm;
2707 mode = append_file ? "a" : "w";
2708
2709 output = fopen(output_name, mode);
2710 if (!output) {
2711 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002712 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002713 }
2714 clock_gettime(CLOCK_REALTIME, &tm);
2715 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002716 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002717 mode = append_file ? "a" : "w";
2718 output = fdopen(output_fd, mode);
2719 if (!output) {
2720 perror("Failed opening logfd");
2721 return -errno;
2722 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002723 }
2724
Jiri Olsa58215222015-07-21 14:31:24 +02002725 stat_config.output = output;
2726
Stephane Eraniand7470b62010-12-01 18:49:05 +02002727 /*
2728 * let the spreadsheet do the pretty-printing
2729 */
2730 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002731 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002732 if (big_num_opt == 1) {
2733 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002734 parse_options_usage(stat_usage, stat_options, "B", 1);
2735 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002736 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002737 } else /* Nope, so disable big number formatting */
2738 big_num = false;
2739 } else if (big_num_opt == 0) /* User passed --no-big-num */
2740 big_num = false;
2741
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002742 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002743
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002744 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002745 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002746 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002747 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002748 } else if (run_count == 0) {
2749 forever = true;
2750 run_count = 1;
2751 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002752
Jiri Olsa421a50f2015-07-21 14:31:22 +02002753 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002754 fprintf(stderr, "The --per-thread option is only available "
2755 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002756 parse_options_usage(NULL, stat_options, "p", 1);
2757 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002758 goto out;
2759 }
2760
2761 /*
2762 * no_aggr, cgroup are for system-wide only
2763 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2764 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002765 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2766 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002767 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002768 fprintf(stderr, "both cgroup and no-aggregation "
2769 "modes only available in system-wide mode\n");
2770
Jiri Olsae0547312015-11-05 15:40:45 +01002771 parse_options_usage(stat_usage, stat_options, "G", 1);
2772 parse_options_usage(NULL, stat_options, "A", 1);
2773 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002774 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002775 }
2776
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002777 if (add_default_attributes())
2778 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002779
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002780 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002781
Namhyung Kim77a6f012012-05-07 14:09:04 +09002782 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002783 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002784 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002785 parse_options_usage(stat_usage, stat_options, "p", 1);
2786 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002787 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002788 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002789 parse_options_usage(stat_usage, stat_options, "C", 1);
2790 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002791 }
2792 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002793 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002794
2795 /*
2796 * Initialize thread_map with comm names,
2797 * so we could print it out on output.
2798 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002799 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02002800 thread_map__read_comms(evsel_list->threads);
2801
Stephane Eranian13370a92013-01-29 12:47:44 +01002802 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002803 if (interval < 10) {
2804 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002805 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002806 goto out;
2807 } else
2808 pr_warning("print interval < 100ms. "
2809 "The overhead percentage could be high in some cases. "
2810 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002811 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002812
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002813 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002814 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002815
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002816 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002817 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002818
Ingo Molnar58d7e992009-05-15 11:03:23 +02002819 /*
2820 * We dont want to block the signals - that would cause
2821 * child tasks to inherit that and Ctrl-C would not work.
2822 * What we want is for Ctrl-C to work in the exec()-ed
2823 * task, but being ignored by perf stat itself:
2824 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002825 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002826 if (!forever)
2827 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002828 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002829 signal(SIGALRM, skip_signal);
2830 signal(SIGABRT, skip_signal);
2831
Ingo Molnar42202dd2009-06-13 14:57:28 +02002832 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002833 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002834 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002835 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2836 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002837
Ingo Molnar42202dd2009-06-13 14:57:28 +02002838 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002839 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002840 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002841 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002842 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002843 }
2844
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002845 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002846 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002847
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002848 if (STAT_RECORD) {
2849 /*
2850 * We synthesize the kernel mmap record just so that older tools
2851 * don't emit warnings about not being able to resolve symbols
2852 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2853 * a saner message about no samples being in the perf.data file.
2854 *
2855 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002856 * in header.c at the moment 'perf stat record' gets introduced, which
2857 * is not really needed once we start adding the stat specific PERF_RECORD_
2858 * records, but the need to suppress the kptr_restrict messages in older
2859 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002860 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002861 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002862 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2863 process_synthesized_event,
2864 &perf_stat.session->machines.host);
2865 if (err) {
2866 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2867 "older tools may produce warnings about this file\n.");
2868 }
2869
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002870 if (!interval) {
2871 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2872 pr_err("failed to write stat round event\n");
2873 }
2874
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002875 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01002876 perf_stat.session->header.data_size += perf_stat.bytes_written;
2877 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2878 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002879
2880 perf_session__delete(perf_stat.session);
2881 }
2882
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002883 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002884 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002885out:
Kan Liangdaefd0b2017-05-26 12:05:38 -07002886 if (smi_cost && smi_reset)
2887 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
2888
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002889 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002890 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002891}