blob: a027b4712e486ae575d01198d6c8a05d8af04aac [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 Olsa664c98d2015-11-05 15:40:50 +0100461static int perf_stat_synthesize_config(bool is_pipe)
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100462{
463 int err;
464
Jiri Olsa664c98d2015-11-05 15:40:50 +0100465 if (is_pipe) {
466 err = perf_event__synthesize_attrs(NULL, perf_stat.session,
467 process_synthesized_event);
468 if (err < 0) {
469 pr_err("Couldn't synthesize attrs.\n");
470 return err;
471 }
472 }
473
Andi Kleenbfd8f722017-11-17 13:42:58 -0800474 err = perf_event__synthesize_extra_attr(NULL,
475 evsel_list,
476 process_synthesized_event,
477 is_pipe);
Jiri Olsa7b60a7e2015-11-05 15:40:54 +0100478
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100479 err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads,
480 process_synthesized_event,
481 NULL);
482 if (err < 0) {
483 pr_err("Couldn't synthesize thread map.\n");
484 return err;
485 }
486
487 err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus,
488 process_synthesized_event, NULL);
489 if (err < 0) {
490 pr_err("Couldn't synthesize thread map.\n");
491 return err;
492 }
493
494 err = perf_event__synthesize_stat_config(NULL, &stat_config,
495 process_synthesized_event, NULL);
496 if (err < 0) {
497 pr_err("Couldn't synthesize config.\n");
498 return err;
499 }
500
501 return 0;
502}
503
Jiri Olsa2af46462015-11-05 15:40:49 +0100504#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
505
506static int __store_counter_ids(struct perf_evsel *counter,
507 struct cpu_map *cpus,
508 struct thread_map *threads)
509{
510 int cpu, thread;
511
512 for (cpu = 0; cpu < cpus->nr; cpu++) {
513 for (thread = 0; thread < threads->nr; thread++) {
514 int fd = FD(counter, cpu, thread);
515
516 if (perf_evlist__id_add_fd(evsel_list, counter,
517 cpu, thread, fd) < 0)
518 return -1;
519 }
520 }
521
522 return 0;
523}
524
525static int store_counter_ids(struct perf_evsel *counter)
526{
527 struct cpu_map *cpus = counter->cpus;
528 struct thread_map *threads = counter->threads;
529
530 if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr))
531 return -ENOMEM;
532
533 return __store_counter_ids(counter, cpus, threads);
534}
535
Jiri Olsa82bf3112017-07-26 14:02:06 +0200536static bool perf_evsel__should_store_id(struct perf_evsel *counter)
537{
538 return STAT_RECORD || counter->attr.read_format & PERF_FORMAT_ID;
539}
540
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700541static struct perf_evsel *perf_evsel__reset_weak_group(struct perf_evsel *evsel)
542{
543 struct perf_evsel *c2, *leader;
544 bool is_open = true;
545
546 leader = evsel->leader;
547 pr_debug("Weak group for %s/%d failed\n",
548 leader->name, leader->nr_members);
549
550 /*
551 * for_each_group_member doesn't work here because it doesn't
552 * include the first entry.
553 */
554 evlist__for_each_entry(evsel_list, c2) {
555 if (c2 == evsel)
556 is_open = false;
557 if (c2->leader == leader) {
558 if (is_open)
559 perf_evsel__close(c2);
560 c2->leader = c2;
561 c2->nr_members = 0;
562 }
563 }
564 return leader;
565}
566
Namhyung Kimacf28922013-03-11 16:43:18 +0900567static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200568{
Jiri Olsaec0d3d12015-07-21 14:31:25 +0200569 int interval = stat_config.interval;
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -0300570 char msg[BUFSIZ];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200571 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100572 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100573 struct timespec ts;
Stephane Eranian410136f2013-11-12 17:58:49 +0100574 size_t l;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200575 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300576 const bool forks = (argc > 0);
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100577 bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600578 struct perf_evsel_config_term *err_term;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200579
Stephane Eranian13370a92013-01-29 12:47:44 +0100580 if (interval) {
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -0300581 ts.tv_sec = interval / USEC_PER_MSEC;
582 ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
Stephane Eranian13370a92013-01-29 12:47:44 +0100583 } else {
584 ts.tv_sec = 1;
585 ts.tv_nsec = 0;
586 }
587
Liming Wang60666c62009-12-31 16:05:50 +0800588 if (forks) {
Jiri Olsa664c98d2015-11-05 15:40:50 +0100589 if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
Arnaldo Carvalho de Melo735f7e02014-01-03 14:56:49 -0300590 workload_exec_failed_signal) < 0) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900591 perror("failed to prepare workload");
592 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800593 }
Namhyung Kimd20a47e2013-09-30 18:01:11 +0900594 child_pid = evsel_list->workload.pid;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000595 }
596
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200597 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300598 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200599
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300600 evlist__for_each_entry(evsel_list, counter) {
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300601try_again:
Jiri Olsacac21422012-11-12 18:34:00 +0100602 if (create_perf_stat_counter(counter) < 0) {
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700603
604 /* Weak group failed. Reset the group. */
Andi Kleen35c19802017-09-05 14:13:24 -0700605 if ((errno == EINVAL || errno == EBADF) &&
Andi Kleen5a5dfe42017-08-31 12:40:26 -0700606 counter->leader != counter &&
607 counter->weak_group) {
608 counter = perf_evsel__reset_weak_group(counter);
609 goto try_again;
610 }
611
David Ahern979987a2012-05-08 09:29:16 -0600612 /*
613 * PPC returns ENXIO for HW counters until 2.6.37
614 * (behavior changed with commit b0a873e).
615 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100616 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600617 errno == ENOENT || errno == EOPNOTSUPP ||
618 errno == ENXIO) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900619 if (verbose > 0)
David Ahernc63ca0c2011-04-29 16:04:15 -0600620 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300621 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600622 counter->supported = false;
Kan Liangcb5ef602015-06-11 02:32:40 -0400623
624 if ((counter->leader != counter) ||
625 !(counter->leader->nr_members > 1))
626 continue;
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300627 } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
Namhyung Kimbb963e12017-02-17 17:17:38 +0900628 if (verbose > 0)
Arnaldo Carvalho de Melo42ef8a72016-05-12 16:25:18 -0300629 ui__warning("%s\n", msg);
630 goto try_again;
631 }
Ingo Molnarede70292011-04-28 08:48:42 +0200632
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300633 perf_evsel__open_strerror(counter, &target,
634 errno, msg, sizeof(msg));
635 ui__error("%s\n", msg);
636
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200637 if (child_pid != -1)
638 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600639
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200640 return -1;
641 }
David Ahern2cee77c2011-05-30 08:55:59 -0600642 counter->supported = true;
Stephane Eranian410136f2013-11-12 17:58:49 +0100643
644 l = strlen(counter->unit);
645 if (l > unit_width)
646 unit_width = l;
Jiri Olsa2af46462015-11-05 15:40:49 +0100647
Jiri Olsa82bf3112017-07-26 14:02:06 +0200648 if (perf_evsel__should_store_id(counter) &&
649 store_counter_ids(counter))
Jiri Olsa2af46462015-11-05 15:40:49 +0100650 return -1;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300651 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200652
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300653 if (perf_evlist__apply_filters(evsel_list, &counter)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300654 pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
Arnaldo Carvalho de Melo23d4aad2015-03-24 19:23:47 -0300655 counter->filter, perf_evsel__name(counter), errno,
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300656 str_error_r(errno, msg, sizeof(msg)));
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100657 return -1;
658 }
659
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600660 if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
Arnaldo Carvalho de Melo62d94b02017-06-27 11:22:31 -0300661 pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
Mathieu Poirier5d8bb1e2016-09-16 09:50:03 -0600662 err_term->val.drv_cfg, perf_evsel__name(counter), errno,
663 str_error_r(errno, msg, sizeof(msg)));
664 return -1;
665 }
666
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100667 if (STAT_RECORD) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100668 int err, fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100669
Jiri Olsa664c98d2015-11-05 15:40:50 +0100670 if (is_pipe) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100671 err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
Jiri Olsa664c98d2015-11-05 15:40:50 +0100672 } else {
673 err = perf_session__write_header(perf_stat.session, evsel_list,
674 fd, false);
675 }
676
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100677 if (err < 0)
678 return err;
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100679
Jiri Olsa664c98d2015-11-05 15:40:50 +0100680 err = perf_stat_synthesize_config(is_pipe);
Jiri Olsa8b99b1a2015-11-05 15:40:48 +0100681 if (err < 0)
682 return err;
Jiri Olsa4979d0c2015-11-05 15:40:46 +0100683 }
684
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200685 /*
686 * Enable counters and exec the command:
687 */
688 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100689 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200690
Liming Wang60666c62009-12-31 16:05:50 +0800691 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900692 perf_evlist__start_workload(evsel_list);
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100693 enable_counters();
Namhyung Kimacf28922013-03-11 16:43:18 +0900694
Stephane Eranian13370a92013-01-29 12:47:44 +0100695 if (interval) {
696 while (!waitpid(child_pid, &status, WNOHANG)) {
697 nanosleep(&ts, NULL);
Jiri Olsaba411a92015-06-26 11:29:24 +0200698 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100699 }
700 }
Milian Wolffdfc9eec2017-09-12 17:25:23 +0200701 waitpid(child_pid, &status, 0);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300702
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300703 if (workload_exec_errno) {
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -0300704 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300705 pr_err("Workload failed: %s\n", emsg);
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300706 return -1;
Arnaldo Carvalho de Melof33cbe72014-01-02 15:11:25 -0300707 }
Arnaldo Carvalho de Melo6af206f2013-12-28 15:45:08 -0300708
Andi Kleen33e49ea2011-09-15 14:31:40 -0700709 if (WIFSIGNALED(status))
710 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800711 } else {
Jiri Olsa67ccdec2015-12-03 10:06:44 +0100712 enable_counters();
Stephane Eranian13370a92013-01-29 12:47:44 +0100713 while (!done) {
714 nanosleep(&ts, NULL);
715 if (interval)
Jiri Olsaba411a92015-06-26 11:29:24 +0200716 process_interval();
Stephane Eranian13370a92013-01-29 12:47:44 +0100717 }
Liming Wang60666c62009-12-31 16:05:50 +0800718 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200719
Mark Rutland3df33ef2016-08-09 14:04:29 +0100720 disable_counters();
721
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200722 t1 = rdclock();
723
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200724 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200725
Mark Rutland3df33ef2016-08-09 14:04:29 +0100726 /*
727 * Closing a group leader splits the group, and as we only disable
728 * group leaders, results in remaining events becoming enabled. To
729 * avoid arbitrary skew, we must read all counters before closing any
730 * group leaders.
731 */
732 read_counters();
733 perf_evlist__close(evsel_list);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200734
Ingo Molnar42202dd2009-06-13 14:57:28 +0200735 return WEXITSTATUS(status);
736}
737
Arnaldo Carvalho de Melo41cde472014-01-03 17:34:42 -0300738static int run_perf_stat(int argc, const char **argv)
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200739{
740 int ret;
741
742 if (pre_cmd) {
743 ret = system(pre_cmd);
744 if (ret)
745 return ret;
746 }
747
748 if (sync_run)
749 sync();
750
751 ret = __run_perf_stat(argc, argv);
752 if (ret)
753 return ret;
754
755 if (post_cmd) {
756 ret = system(post_cmd);
757 if (ret)
758 return ret;
759 }
760
761 return ret;
762}
763
Andi Kleend73515c2015-03-11 07:16:27 -0700764static void print_running(u64 run, u64 ena)
765{
766 if (csv_output) {
Jiri Olsa58215222015-07-21 14:31:24 +0200767 fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
Andi Kleend73515c2015-03-11 07:16:27 -0700768 csv_sep,
769 run,
770 csv_sep,
771 ena ? 100.0 * run / ena : 100.0);
772 } else if (run != ena) {
Jiri Olsa58215222015-07-21 14:31:24 +0200773 fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena);
Andi Kleend73515c2015-03-11 07:16:27 -0700774 }
775}
776
Ingo Molnarf99844c2011-04-27 05:35:39 +0200777static void print_noise_pct(double total, double avg)
778{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800779 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200780
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700781 if (csv_output)
Jiri Olsa58215222015-07-21 14:31:24 +0200782 fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600783 else if (pct)
Jiri Olsa58215222015-07-21 14:31:24 +0200784 fprintf(stat_config.output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200785}
786
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200787static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200788{
Jiri Olsa581cc8a2015-10-16 12:41:03 +0200789 struct perf_stat_evsel *ps;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200790
Peter Zijlstra849abde2009-09-04 18:23:38 +0200791 if (run_count == 1)
792 return;
793
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -0300794 ps = evsel->stats;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200795 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200796}
797
Stephane Eranian12c08a92013-02-14 13:57:29 +0100798static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200799{
Jiri Olsa421a50f2015-07-21 14:31:22 +0200800 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100801 case AGGR_CORE:
Jiri Olsa58215222015-07-21 14:31:24 +0200802 fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
Stephane Eranian12c08a92013-02-14 13:57:29 +0100803 cpu_map__id_to_socket(id),
804 csv_output ? 0 : -8,
805 cpu_map__id_to_cpu(id),
806 csv_sep,
807 csv_output ? 0 : 4,
808 nr,
809 csv_sep);
810 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100811 case AGGR_SOCKET:
Jiri Olsa58215222015-07-21 14:31:24 +0200812 fprintf(stat_config.output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100813 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100814 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100815 csv_sep,
816 csv_output ? 0 : 4,
817 nr,
818 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100819 break;
820 case AGGR_NONE:
Jiri Olsa58215222015-07-21 14:31:24 +0200821 fprintf(stat_config.output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200822 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100823 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100824 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +0200825 case AGGR_THREAD:
Jiri Olsa58215222015-07-21 14:31:24 +0200826 fprintf(stat_config.output, "%*s-%*d%s",
Jiri Olsa32b8af82015-06-26 11:29:27 +0200827 csv_output ? 0 : 16,
828 thread_map__comm(evsel->threads, id),
829 csv_output ? 0 : -8,
830 thread_map__pid(evsel->threads, id),
831 csv_sep);
832 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100833 case AGGR_GLOBAL:
Jiri Olsa208df992015-10-16 12:41:04 +0200834 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100835 default:
836 break;
837 }
838}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200839
Andi Kleen140aead2016-01-30 09:06:49 -0800840struct outstate {
841 FILE *fh;
842 bool newline;
Andi Kleenf9483392016-01-30 09:06:50 -0800843 const char *prefix;
Andi Kleen92a61f62016-02-29 14:36:21 -0800844 int nfields;
Andi Kleen44d49a62016-02-29 14:36:22 -0800845 int id, nr;
846 struct perf_evsel *evsel;
Andi Kleen140aead2016-01-30 09:06:49 -0800847};
848
849#define METRIC_LEN 35
850
851static void new_line_std(void *ctx)
852{
853 struct outstate *os = ctx;
854
855 os->newline = true;
856}
857
858static void do_new_line_std(struct outstate *os)
859{
860 fputc('\n', os->fh);
Andi Kleenf9483392016-01-30 09:06:50 -0800861 fputs(os->prefix, os->fh);
Andi Kleen44d49a62016-02-29 14:36:22 -0800862 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen140aead2016-01-30 09:06:49 -0800863 if (stat_config.aggr_mode == AGGR_NONE)
864 fprintf(os->fh, " ");
Andi Kleen140aead2016-01-30 09:06:49 -0800865 fprintf(os->fh, " ");
866}
867
868static void print_metric_std(void *ctx, const char *color, const char *fmt,
869 const char *unit, double val)
870{
871 struct outstate *os = ctx;
872 FILE *out = os->fh;
873 int n;
874 bool newline = os->newline;
875
876 os->newline = false;
877
878 if (unit == NULL || fmt == NULL) {
879 fprintf(out, "%-*s", METRIC_LEN, "");
880 return;
881 }
882
883 if (newline)
884 do_new_line_std(os);
885
886 n = fprintf(out, " # ");
887 if (color)
888 n += color_fprintf(out, color, fmt, val);
889 else
890 n += fprintf(out, fmt, val);
891 fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
892}
893
Andi Kleen92a61f62016-02-29 14:36:21 -0800894static void new_line_csv(void *ctx)
895{
896 struct outstate *os = ctx;
897 int i;
898
899 fputc('\n', os->fh);
900 if (os->prefix)
901 fprintf(os->fh, "%s%s", os->prefix, csv_sep);
Andi Kleen44d49a62016-02-29 14:36:22 -0800902 aggr_printout(os->evsel, os->id, os->nr);
Andi Kleen92a61f62016-02-29 14:36:21 -0800903 for (i = 0; i < os->nfields; i++)
904 fputs(csv_sep, os->fh);
905}
906
907static void print_metric_csv(void *ctx,
908 const char *color __maybe_unused,
909 const char *fmt, const char *unit, double val)
910{
911 struct outstate *os = ctx;
912 FILE *out = os->fh;
913 char buf[64], *vals, *ends;
914
915 if (unit == NULL || fmt == NULL) {
916 fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep);
917 return;
918 }
919 snprintf(buf, sizeof(buf), fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900920 ends = vals = ltrim(buf);
Andi Kleen92a61f62016-02-29 14:36:21 -0800921 while (isdigit(*ends) || *ends == '.')
922 ends++;
923 *ends = 0;
924 while (isspace(*unit))
925 unit++;
926 fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit);
927}
928
Andi Kleen54b50912016-03-03 15:57:36 -0800929#define METRIC_ONLY_LEN 20
930
931/* Filter out some columns that don't work well in metrics only mode */
932
933static bool valid_only_metric(const char *unit)
934{
935 if (!unit)
936 return false;
937 if (strstr(unit, "/sec") ||
938 strstr(unit, "hz") ||
939 strstr(unit, "Hz") ||
940 strstr(unit, "CPUs utilized"))
941 return false;
942 return true;
943}
944
945static const char *fixunit(char *buf, struct perf_evsel *evsel,
946 const char *unit)
947{
948 if (!strncmp(unit, "of all", 6)) {
949 snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel),
950 unit);
951 return buf;
952 }
953 return unit;
954}
955
956static void print_metric_only(void *ctx, const char *color, const char *fmt,
957 const char *unit, double val)
958{
959 struct outstate *os = ctx;
960 FILE *out = os->fh;
961 int n;
962 char buf[1024];
963 unsigned mlen = METRIC_ONLY_LEN;
964
965 if (!valid_only_metric(unit))
966 return;
967 unit = fixunit(buf, os->evsel, unit);
968 if (color)
969 n = color_fprintf(out, color, fmt, val);
970 else
971 n = fprintf(out, fmt, val);
972 if (n > METRIC_ONLY_LEN)
973 n = METRIC_ONLY_LEN;
974 if (mlen < strlen(unit))
975 mlen = strlen(unit) + 1;
976 fprintf(out, "%*s", mlen - n, "");
977}
978
979static void print_metric_only_csv(void *ctx, const char *color __maybe_unused,
980 const char *fmt,
981 const char *unit, double val)
982{
983 struct outstate *os = ctx;
984 FILE *out = os->fh;
985 char buf[64], *vals, *ends;
986 char tbuf[1024];
987
988 if (!valid_only_metric(unit))
989 return;
990 unit = fixunit(tbuf, os->evsel, unit);
991 snprintf(buf, sizeof buf, fmt, val);
Taeung Songb07c40d2017-04-07 23:24:18 +0900992 ends = vals = ltrim(buf);
Andi Kleen54b50912016-03-03 15:57:36 -0800993 while (isdigit(*ends) || *ends == '.')
994 ends++;
995 *ends = 0;
996 fprintf(out, "%s%s", vals, csv_sep);
997}
998
999static void new_line_metric(void *ctx __maybe_unused)
1000{
1001}
1002
1003static void print_metric_header(void *ctx, const char *color __maybe_unused,
1004 const char *fmt __maybe_unused,
1005 const char *unit, double val __maybe_unused)
1006{
1007 struct outstate *os = ctx;
1008 char tbuf[1024];
1009
1010 if (!valid_only_metric(unit))
1011 return;
1012 unit = fixunit(tbuf, os->evsel, unit);
1013 if (csv_output)
1014 fprintf(os->fh, "%s%s", unit, csv_sep);
1015 else
1016 fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit);
1017}
1018
Andi Kleenda88c7f2014-09-24 13:50:46 -07001019static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001020{
Jiri Olsa58215222015-07-21 14:31:24 +02001021 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001022 double msecs = avg / NSEC_PER_MSEC;
Stephane Eranian410136f2013-11-12 17:58:49 +01001023 const char *fmt_v, *fmt_n;
David Ahern4bbe5a62013-09-28 14:28:00 -06001024 char name[25];
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001025
Stephane Eranian410136f2013-11-12 17:58:49 +01001026 fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
1027 fmt_n = csv_output ? "%s" : "%-25s";
1028
Andi Kleenda88c7f2014-09-24 13:50:46 -07001029 aggr_printout(evsel, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001030
David Ahern4bbe5a62013-09-28 14:28:00 -06001031 scnprintf(name, sizeof(name), "%s%s",
1032 perf_evsel__name(evsel), csv_output ? "" : " (msec)");
Stephane Eranian410136f2013-11-12 17:58:49 +01001033
1034 fprintf(output, fmt_v, msecs, csv_sep);
1035
1036 if (csv_output)
1037 fprintf(output, "%s%s", evsel->unit, csv_sep);
1038 else
1039 fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep);
1040
1041 fprintf(output, fmt_n, name);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001042
Stephane Eranian023695d2011-02-14 11:20:01 +02001043 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001044 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001045}
1046
Andi Kleen44d49a62016-02-29 14:36:22 -08001047static int first_shadow_cpu(struct perf_evsel *evsel, int id)
1048{
1049 int i;
1050
1051 if (!aggr_get_id)
1052 return 0;
1053
1054 if (stat_config.aggr_mode == AGGR_NONE)
1055 return id;
1056
1057 if (stat_config.aggr_mode == AGGR_GLOBAL)
1058 return 0;
1059
1060 for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) {
1061 int cpu2 = perf_evsel__cpus(evsel)->map[i];
1062
1063 if (aggr_get_id(evsel_list->cpus, cpu2) == id)
1064 return cpu2;
1065 }
1066 return 0;
1067}
1068
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001069static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1070{
Jiri Olsa58215222015-07-21 14:31:24 +02001071 FILE *output = stat_config.output;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001072 double sc = evsel->scale;
1073 const char *fmt;
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001074
1075 if (csv_output) {
Andi Kleene3b03b62016-05-05 16:04:03 -07001076 fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001077 } else {
1078 if (big_num)
Andi Kleene3b03b62016-05-05 16:04:03 -07001079 fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001080 else
Andi Kleene3b03b62016-05-05 16:04:03 -07001081 fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001082 }
1083
1084 aggr_printout(evsel, id, nr);
1085
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001086 fprintf(output, fmt, avg, csv_sep);
1087
1088 if (evsel->unit)
1089 fprintf(output, "%-*s%s",
1090 csv_output ? 0 : unit_width,
1091 evsel->unit, csv_sep);
1092
1093 fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel));
1094
1095 if (evsel->cgrp)
1096 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Andi Kleeneedfcb42015-11-02 17:50:21 -08001097}
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001098
Andi Kleenf9483392016-01-30 09:06:50 -08001099static void printout(int id, int nr, struct perf_evsel *counter, double uval,
Andi Kleencb110f42016-01-30 09:06:51 -08001100 char *prefix, u64 run, u64 ena, double noise)
Andi Kleeneedfcb42015-11-02 17:50:21 -08001101{
Andi Kleen140aead2016-01-30 09:06:49 -08001102 struct perf_stat_output_ctx out;
Andi Kleenf9483392016-01-30 09:06:50 -08001103 struct outstate os = {
1104 .fh = stat_config.output,
Andi Kleen44d49a62016-02-29 14:36:22 -08001105 .prefix = prefix ? prefix : "",
1106 .id = id,
1107 .nr = nr,
1108 .evsel = counter,
Andi Kleenf9483392016-01-30 09:06:50 -08001109 };
Andi Kleen140aead2016-01-30 09:06:49 -08001110 print_metric_t pm = print_metric_std;
1111 void (*nl)(void *);
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001112
Andi Kleen54b50912016-03-03 15:57:36 -08001113 if (metric_only) {
1114 nl = new_line_metric;
1115 if (csv_output)
1116 pm = print_metric_only_csv;
1117 else
1118 pm = print_metric_only;
1119 } else
1120 nl = new_line_std;
Andi Kleeneedfcb42015-11-02 17:50:21 -08001121
Andi Kleen54b50912016-03-03 15:57:36 -08001122 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001123 static int aggr_fields[] = {
1124 [AGGR_GLOBAL] = 0,
1125 [AGGR_THREAD] = 1,
1126 [AGGR_NONE] = 1,
1127 [AGGR_SOCKET] = 2,
1128 [AGGR_CORE] = 2,
1129 };
1130
1131 pm = print_metric_csv;
1132 nl = new_line_csv;
1133 os.nfields = 3;
1134 os.nfields += aggr_fields[stat_config.aggr_mode];
1135 if (counter->cgrp)
1136 os.nfields++;
1137 }
Andi Kleenb002f3b2016-02-17 14:44:00 -08001138 if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
Andi Kleen54b50912016-03-03 15:57:36 -08001139 if (metric_only) {
1140 pm(&os, NULL, "", "", 0);
1141 return;
1142 }
Andi Kleencb110f42016-01-30 09:06:51 -08001143 aggr_printout(counter, id, nr);
1144
1145 fprintf(stat_config.output, "%*s%s",
1146 csv_output ? 0 : 18,
1147 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1148 csv_sep);
1149
Borislav Petkov02d492e2017-02-07 01:40:05 +01001150 if (counter->supported)
1151 print_free_counters_hint = 1;
1152
Andi Kleencb110f42016-01-30 09:06:51 -08001153 fprintf(stat_config.output, "%-*s%s",
1154 csv_output ? 0 : unit_width,
1155 counter->unit, csv_sep);
1156
1157 fprintf(stat_config.output, "%*s",
1158 csv_output ? 0 : -25,
1159 perf_evsel__name(counter));
1160
1161 if (counter->cgrp)
1162 fprintf(stat_config.output, "%s%s",
1163 csv_sep, counter->cgrp->name);
1164
Andi Kleen92a61f62016-02-29 14:36:21 -08001165 if (!csv_output)
1166 pm(&os, NULL, NULL, "", 0);
1167 print_noise(counter, noise);
Andi Kleencb110f42016-01-30 09:06:51 -08001168 print_running(run, ena);
Andi Kleen92a61f62016-02-29 14:36:21 -08001169 if (csv_output)
1170 pm(&os, NULL, NULL, "", 0);
Andi Kleencb110f42016-01-30 09:06:51 -08001171 return;
1172 }
1173
Andi Kleen54b50912016-03-03 15:57:36 -08001174 if (metric_only)
1175 /* nothing */;
1176 else if (nsec_counter(counter))
Andi Kleeneedfcb42015-11-02 17:50:21 -08001177 nsec_printout(id, nr, counter, uval);
1178 else
1179 abs_printout(id, nr, counter, uval);
1180
Andi Kleen140aead2016-01-30 09:06:49 -08001181 out.print_metric = pm;
1182 out.new_line = nl;
1183 out.ctx = &os;
Andi Kleen37932c12017-03-20 13:17:08 -07001184 out.force_header = false;
Andi Kleen140aead2016-01-30 09:06:49 -08001185
Andi Kleen54b50912016-03-03 15:57:36 -08001186 if (csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001187 print_noise(counter, noise);
1188 print_running(run, ena);
1189 }
1190
1191 perf_stat__print_shadow_stats(counter, uval,
Andi Kleen44d49a62016-02-29 14:36:22 -08001192 first_shadow_cpu(counter, id),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001193 &out, &metric_events);
Andi Kleen54b50912016-03-03 15:57:36 -08001194 if (!csv_output && !metric_only) {
Andi Kleen92a61f62016-02-29 14:36:21 -08001195 print_noise(counter, noise);
1196 print_running(run, ena);
1197 }
Jiri Olsa556b1fb2015-06-03 16:25:56 +02001198}
1199
Andi Kleen44d49a62016-02-29 14:36:22 -08001200static void aggr_update_shadow(void)
1201{
1202 int cpu, s2, id, s;
1203 u64 val;
1204 struct perf_evsel *counter;
1205
1206 for (s = 0; s < aggr_map->nr; s++) {
1207 id = aggr_map->map[s];
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001208 evlist__for_each_entry(evsel_list, counter) {
Andi Kleen44d49a62016-02-29 14:36:22 -08001209 val = 0;
1210 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1211 s2 = aggr_get_id(evsel_list->cpus, cpu);
1212 if (s2 != id)
1213 continue;
1214 val += perf_counts(counter->counts, cpu, 0)->val;
1215 }
Jiri Olsa54830dd2017-01-23 22:42:56 +01001216 perf_stat__update_shadow_stats(counter, val,
Andi Kleen44d49a62016-02-29 14:36:22 -08001217 first_shadow_cpu(counter, id));
1218 }
1219 }
1220}
1221
Andi Kleen430daf22017-03-20 13:17:00 -07001222static void collect_all_aliases(struct perf_evsel *counter,
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001223 void (*cb)(struct perf_evsel *counter, void *data,
1224 bool first),
1225 void *data)
1226{
Andi Kleen430daf22017-03-20 13:17:00 -07001227 struct perf_evsel *alias;
1228
1229 alias = list_prepare_entry(counter, &(evsel_list->entries), node);
1230 list_for_each_entry_continue (alias, &evsel_list->entries, node) {
1231 if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) ||
1232 alias->scale != counter->scale ||
1233 alias->cgrp != counter->cgrp ||
1234 strcmp(alias->unit, counter->unit) ||
1235 nsec_counter(alias) != nsec_counter(counter))
1236 break;
1237 alias->merged_stat = true;
1238 cb(alias, data, false);
1239 }
1240}
1241
1242static bool collect_data(struct perf_evsel *counter,
1243 void (*cb)(struct perf_evsel *counter, void *data,
1244 bool first),
1245 void *data)
1246{
1247 if (counter->merged_stat)
1248 return false;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001249 cb(counter, data, true);
Arnaldo Carvalho de Melo63ce8442017-08-31 15:32:18 -03001250 if (!no_merge && counter->auto_merge_stats)
Andi Kleen430daf22017-03-20 13:17:00 -07001251 collect_all_aliases(counter, cb, data);
1252 return true;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001253}
1254
1255struct aggr_data {
1256 u64 ena, run, val;
1257 int id;
1258 int nr;
1259 int cpu;
1260};
1261
1262static void aggr_cb(struct perf_evsel *counter, void *data, bool first)
1263{
1264 struct aggr_data *ad = data;
1265 int cpu, s2;
1266
1267 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1268 struct perf_counts_values *counts;
1269
1270 s2 = aggr_get_id(perf_evsel__cpus(counter), cpu);
1271 if (s2 != ad->id)
1272 continue;
1273 if (first)
1274 ad->nr++;
1275 counts = perf_counts(counter->counts, cpu, 0);
Andi Kleenb4229e92017-03-20 13:17:01 -07001276 /*
1277 * When any result is bad, make them all to give
1278 * consistent output in interval mode.
1279 */
1280 if (counts->ena == 0 || counts->run == 0 ||
1281 counter->counts->scaled == -1) {
1282 ad->ena = 0;
1283 ad->run = 0;
1284 break;
1285 }
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001286 ad->val += counts->val;
1287 ad->ena += counts->ena;
1288 ad->run += counts->run;
1289 }
1290}
1291
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001292static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001293{
Jiri Olsa58215222015-07-21 14:31:24 +02001294 FILE *output = stat_config.output;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001295 struct perf_evsel *counter;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001296 int s, id, nr;
Stephane Eranian410136f2013-11-12 17:58:49 +01001297 double uval;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001298 u64 ena, run, val;
Andi Kleen54b50912016-03-03 15:57:36 -08001299 bool first;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001300
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001301 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001302 return;
1303
Andi Kleen44d49a62016-02-29 14:36:22 -08001304 aggr_update_shadow();
1305
Andi Kleen54b50912016-03-03 15:57:36 -08001306 /*
1307 * With metric_only everything is on a single line.
1308 * Without each counter has its own line.
1309 */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001310 for (s = 0; s < aggr_map->nr; s++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001311 struct aggr_data ad;
Andi Kleen54b50912016-03-03 15:57:36 -08001312 if (prefix && metric_only)
1313 fprintf(output, "%s", prefix);
1314
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001315 ad.id = id = aggr_map->map[s];
Andi Kleen54b50912016-03-03 15:57:36 -08001316 first = true;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001317 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001318 if (is_duration_time(counter))
1319 continue;
1320
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001321 ad.val = ad.ena = ad.run = 0;
1322 ad.nr = 0;
Andi Kleen430daf22017-03-20 13:17:00 -07001323 if (!collect_data(counter, aggr_cb, &ad))
1324 continue;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001325 nr = ad.nr;
1326 ena = ad.ena;
1327 run = ad.run;
1328 val = ad.val;
Andi Kleen54b50912016-03-03 15:57:36 -08001329 if (first && metric_only) {
1330 first = false;
1331 aggr_printout(counter, id, nr);
1332 }
1333 if (prefix && !metric_only)
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001334 fprintf(output, "%s", prefix);
1335
Stephane Eranian410136f2013-11-12 17:58:49 +01001336 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001337 printout(id, nr, counter, uval, prefix, run, ena, 1.0);
Andi Kleen54b50912016-03-03 15:57:36 -08001338 if (!metric_only)
1339 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001340 }
Andi Kleen54b50912016-03-03 15:57:36 -08001341 if (metric_only)
1342 fputc('\n', output);
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001343 }
1344}
1345
Jiri Olsa32b8af82015-06-26 11:29:27 +02001346static void print_aggr_thread(struct perf_evsel *counter, char *prefix)
1347{
Jiri Olsa58215222015-07-21 14:31:24 +02001348 FILE *output = stat_config.output;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001349 int nthreads = thread_map__nr(counter->threads);
1350 int ncpus = cpu_map__nr(counter->cpus);
1351 int cpu, thread;
1352 double uval;
1353
1354 for (thread = 0; thread < nthreads; thread++) {
1355 u64 ena = 0, run = 0, val = 0;
1356
1357 for (cpu = 0; cpu < ncpus; cpu++) {
1358 val += perf_counts(counter->counts, cpu, thread)->val;
1359 ena += perf_counts(counter->counts, cpu, thread)->ena;
1360 run += perf_counts(counter->counts, cpu, thread)->run;
1361 }
1362
1363 if (prefix)
1364 fprintf(output, "%s", prefix);
1365
1366 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001367 printout(thread, 0, counter, uval, prefix, run, ena, 1.0);
Jiri Olsa32b8af82015-06-26 11:29:27 +02001368 fputc('\n', output);
1369 }
1370}
1371
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001372struct caggr_data {
1373 double avg, avg_enabled, avg_running;
1374};
1375
1376static void counter_aggr_cb(struct perf_evsel *counter, void *data,
1377 bool first __maybe_unused)
1378{
1379 struct caggr_data *cd = data;
Arnaldo Carvalho de Meloe669e832017-10-26 14:22:34 -03001380 struct perf_stat_evsel *ps = counter->stats;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001381
1382 cd->avg += avg_stats(&ps->res_stats[0]);
1383 cd->avg_enabled += avg_stats(&ps->res_stats[1]);
1384 cd->avg_running += avg_stats(&ps->res_stats[2]);
1385}
1386
Ingo Molnar42202dd2009-06-13 14:57:28 +02001387/*
1388 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001389 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001390 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001391static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001392{
Jiri Olsa58215222015-07-21 14:31:24 +02001393 FILE *output = stat_config.output;
Stephane Eranian410136f2013-11-12 17:58:49 +01001394 double uval;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001395 struct caggr_data cd = { .avg = 0.0 };
Andi Kleend73515c2015-03-11 07:16:27 -07001396
Andi Kleen430daf22017-03-20 13:17:00 -07001397 if (!collect_data(counter, counter_aggr_cb, &cd))
1398 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001399
Andi Kleen54b50912016-03-03 15:57:36 -08001400 if (prefix && !metric_only)
Stephane Eranian13370a92013-01-29 12:47:44 +01001401 fprintf(output, "%s", prefix);
1402
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001403 uval = cd.avg * counter->scale;
1404 printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, cd.avg);
Andi Kleen54b50912016-03-03 15:57:36 -08001405 if (!metric_only)
1406 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001407}
1408
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001409static void counter_cb(struct perf_evsel *counter, void *data,
1410 bool first __maybe_unused)
1411{
1412 struct aggr_data *ad = data;
1413
1414 ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
1415 ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
1416 ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
1417}
1418
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001419/*
1420 * Print out the results of a single counter:
1421 * does not use aggregated count in system-wide
1422 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001423static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001424{
Jiri Olsa58215222015-07-21 14:31:24 +02001425 FILE *output = stat_config.output;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001426 u64 ena, run, val;
Stephane Eranian410136f2013-11-12 17:58:49 +01001427 double uval;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001428 int cpu;
1429
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001430 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001431 struct aggr_data ad = { .cpu = cpu };
1432
Andi Kleen430daf22017-03-20 13:17:00 -07001433 if (!collect_data(counter, counter_cb, &ad))
1434 return;
Andi Kleenfbe51fb2017-03-20 13:16:59 -07001435 val = ad.val;
1436 ena = ad.ena;
1437 run = ad.run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001438
1439 if (prefix)
1440 fprintf(output, "%s", prefix);
1441
Stephane Eranian410136f2013-11-12 17:58:49 +01001442 uval = val * counter->scale;
Andi Kleencb110f42016-01-30 09:06:51 -08001443 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001444
Stephane Eranian4aa90152011-08-15 22:22:33 +02001445 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001446 }
1447}
1448
Andi Kleen206cab62016-03-03 15:57:37 -08001449static void print_no_aggr_metric(char *prefix)
1450{
1451 int cpu;
1452 int nrcpus = 0;
1453 struct perf_evsel *counter;
1454 u64 ena, run, val;
1455 double uval;
1456
1457 nrcpus = evsel_list->cpus->nr;
1458 for (cpu = 0; cpu < nrcpus; cpu++) {
1459 bool first = true;
1460
1461 if (prefix)
1462 fputs(prefix, stat_config.output);
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001463 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001464 if (is_duration_time(counter))
1465 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001466 if (first) {
1467 aggr_printout(counter, cpu, 0);
1468 first = false;
1469 }
1470 val = perf_counts(counter->counts, cpu, 0)->val;
1471 ena = perf_counts(counter->counts, cpu, 0)->ena;
1472 run = perf_counts(counter->counts, cpu, 0)->run;
1473
1474 uval = val * counter->scale;
1475 printout(cpu, 0, counter, uval, prefix, run, ena, 1.0);
1476 }
1477 fputc('\n', stat_config.output);
1478 }
1479}
1480
Andi Kleen54b50912016-03-03 15:57:36 -08001481static int aggr_header_lens[] = {
1482 [AGGR_CORE] = 18,
1483 [AGGR_SOCKET] = 12,
Andi Kleen206cab62016-03-03 15:57:37 -08001484 [AGGR_NONE] = 6,
Andi Kleen54b50912016-03-03 15:57:36 -08001485 [AGGR_THREAD] = 24,
1486 [AGGR_GLOBAL] = 0,
1487};
1488
Andi Kleenc51fd632016-05-24 12:52:39 -07001489static const char *aggr_header_csv[] = {
1490 [AGGR_CORE] = "core,cpus,",
1491 [AGGR_SOCKET] = "socket,cpus",
1492 [AGGR_NONE] = "cpu,",
1493 [AGGR_THREAD] = "comm-pid,",
1494 [AGGR_GLOBAL] = ""
1495};
1496
Andi Kleen41c8ca22016-05-24 12:52:38 -07001497static void print_metric_headers(const char *prefix, bool no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001498{
1499 struct perf_stat_output_ctx out;
1500 struct perf_evsel *counter;
1501 struct outstate os = {
1502 .fh = stat_config.output
1503 };
1504
1505 if (prefix)
1506 fprintf(stat_config.output, "%s", prefix);
1507
Andi Kleen41c8ca22016-05-24 12:52:38 -07001508 if (!csv_output && !no_indent)
Andi Kleen54b50912016-03-03 15:57:36 -08001509 fprintf(stat_config.output, "%*s",
1510 aggr_header_lens[stat_config.aggr_mode], "");
Andi Kleenc51fd632016-05-24 12:52:39 -07001511 if (csv_output) {
1512 if (stat_config.interval)
1513 fputs("time,", stat_config.output);
1514 fputs(aggr_header_csv[stat_config.aggr_mode],
1515 stat_config.output);
1516 }
Andi Kleen54b50912016-03-03 15:57:36 -08001517
1518 /* Print metrics headers only */
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001519 evlist__for_each_entry(evsel_list, counter) {
Andi Kleene864c5c2017-08-31 12:40:35 -07001520 if (is_duration_time(counter))
1521 continue;
Andi Kleen54b50912016-03-03 15:57:36 -08001522 os.evsel = counter;
1523 out.ctx = &os;
1524 out.print_metric = print_metric_header;
1525 out.new_line = new_line_metric;
Andi Kleen37932c12017-03-20 13:17:08 -07001526 out.force_header = true;
Andi Kleen54b50912016-03-03 15:57:36 -08001527 os.evsel = counter;
1528 perf_stat__print_shadow_stats(counter, 0,
1529 0,
Andi Kleenb18f3e32017-08-31 12:40:31 -07001530 &out,
1531 &metric_events);
Andi Kleen54b50912016-03-03 15:57:36 -08001532 }
1533 fputc('\n', stat_config.output);
1534}
1535
Jiri Olsad4f63a42015-06-26 11:29:26 +02001536static void print_interval(char *prefix, struct timespec *ts)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001537{
Jiri Olsa58215222015-07-21 14:31:24 +02001538 FILE *output = stat_config.output;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001539 static int num_print_interval;
1540
1541 sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1542
Andi Kleen41c8ca22016-05-24 12:52:38 -07001543 if (num_print_interval == 0 && !csv_output) {
Jiri Olsa421a50f2015-07-21 14:31:22 +02001544 switch (stat_config.aggr_mode) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02001545 case AGGR_SOCKET:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001546 fprintf(output, "# time socket cpus");
1547 if (!metric_only)
1548 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001549 break;
1550 case AGGR_CORE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001551 fprintf(output, "# time core cpus");
1552 if (!metric_only)
1553 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001554 break;
1555 case AGGR_NONE:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001556 fprintf(output, "# time CPU");
1557 if (!metric_only)
1558 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001559 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001560 case AGGR_THREAD:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001561 fprintf(output, "# time comm-pid");
1562 if (!metric_only)
1563 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa32b8af82015-06-26 11:29:27 +02001564 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001565 case AGGR_GLOBAL:
1566 default:
Andi Kleen41c8ca22016-05-24 12:52:38 -07001567 fprintf(output, "# time");
1568 if (!metric_only)
1569 fprintf(output, " counts %*s events\n", unit_width, "unit");
Jiri Olsa208df992015-10-16 12:41:04 +02001570 case AGGR_UNSET:
1571 break;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001572 }
1573 }
1574
Andi Kleen41c8ca22016-05-24 12:52:38 -07001575 if (num_print_interval == 0 && metric_only)
1576 print_metric_headers(" ", true);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001577 if (++num_print_interval == 25)
1578 num_print_interval = 0;
1579}
1580
1581static void print_header(int argc, const char **argv)
1582{
Jiri Olsa58215222015-07-21 14:31:24 +02001583 FILE *output = stat_config.output;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001584 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001585
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001586 fflush(stdout);
1587
Stephane Eraniand7470b62010-12-01 18:49:05 +02001588 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001589 fprintf(output, "\n");
1590 fprintf(output, " Performance counter stats for ");
David Ahern62d3b612013-09-28 14:27:58 -06001591 if (target.system_wide)
1592 fprintf(output, "\'system wide");
1593 else if (target.cpu_list)
1594 fprintf(output, "\'CPU(s) %s", target.cpu_list);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03001595 else if (!target__has_task(&target)) {
Jiri Olsaba6039b62015-11-05 15:40:55 +01001596 fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1597 for (i = 1; argv && (i < argc); i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001598 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001599 } else if (target.pid)
1600 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001601 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001602 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001603
Stephane Eranian4aa90152011-08-15 22:22:33 +02001604 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001605 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001606 fprintf(output, " (%d runs)", run_count);
1607 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001608 }
Jiri Olsad4f63a42015-06-26 11:29:26 +02001609}
1610
1611static void print_footer(void)
1612{
Jiri Olsa58215222015-07-21 14:31:24 +02001613 FILE *output = stat_config.output;
Andi Kleen918c7b062017-05-22 18:00:16 -07001614 int n;
Jiri Olsa58215222015-07-21 14:31:24 +02001615
Jiri Olsad4f63a42015-06-26 11:29:26 +02001616 if (!null_run)
1617 fprintf(output, "\n");
1618 fprintf(output, " %17.9f seconds time elapsed",
Arnaldo Carvalho de Melo310ebb92016-08-08 14:57:04 -03001619 avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC);
Jiri Olsad4f63a42015-06-26 11:29:26 +02001620 if (run_count > 1) {
1621 fprintf(output, " ");
1622 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1623 avg_stats(&walltime_nsecs_stats));
1624 }
1625 fprintf(output, "\n\n");
Borislav Petkov02d492e2017-02-07 01:40:05 +01001626
Andi Kleen918c7b062017-05-22 18:00:16 -07001627 if (print_free_counters_hint &&
1628 sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1629 n > 0)
Borislav Petkov02d492e2017-02-07 01:40:05 +01001630 fprintf(output,
1631"Some events weren't counted. Try disabling the NMI watchdog:\n"
1632" echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1633" perf stat ...\n"
1634" echo 1 > /proc/sys/kernel/nmi_watchdog\n");
Jiri Olsad4f63a42015-06-26 11:29:26 +02001635}
1636
1637static void print_counters(struct timespec *ts, int argc, const char **argv)
1638{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001639 int interval = stat_config.interval;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001640 struct perf_evsel *counter;
1641 char buf[64], *prefix = NULL;
1642
Jiri Olsa664c98d2015-11-05 15:40:50 +01001643 /* Do not print anything if we record to the pipe. */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01001644 if (STAT_RECORD && perf_stat.data.is_pipe)
Jiri Olsa664c98d2015-11-05 15:40:50 +01001645 return;
1646
Jiri Olsad4f63a42015-06-26 11:29:26 +02001647 if (interval)
1648 print_interval(prefix = buf, ts);
1649 else
1650 print_header(argc, argv);
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001651
Andi Kleen54b50912016-03-03 15:57:36 -08001652 if (metric_only) {
1653 static int num_print_iv;
1654
Andi Kleen41c8ca22016-05-24 12:52:38 -07001655 if (num_print_iv == 0 && !interval)
1656 print_metric_headers(prefix, false);
Andi Kleen54b50912016-03-03 15:57:36 -08001657 if (num_print_iv++ == 25)
1658 num_print_iv = 0;
1659 if (stat_config.aggr_mode == AGGR_GLOBAL && prefix)
1660 fprintf(stat_config.output, "%s", prefix);
1661 }
1662
Jiri Olsa421a50f2015-07-21 14:31:22 +02001663 switch (stat_config.aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001664 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001665 case AGGR_SOCKET:
Jiri Olsad4f63a42015-06-26 11:29:26 +02001666 print_aggr(prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001667 break;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001668 case AGGR_THREAD:
Andi Kleene864c5c2017-08-31 12:40:35 -07001669 evlist__for_each_entry(evsel_list, counter) {
1670 if (is_duration_time(counter))
1671 continue;
Jiri Olsa32b8af82015-06-26 11:29:27 +02001672 print_aggr_thread(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001673 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02001674 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001675 case AGGR_GLOBAL:
Andi Kleene864c5c2017-08-31 12:40:35 -07001676 evlist__for_each_entry(evsel_list, counter) {
1677 if (is_duration_time(counter))
1678 continue;
Jiri Olsad4f63a42015-06-26 11:29:26 +02001679 print_counter_aggr(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001680 }
Andi Kleen54b50912016-03-03 15:57:36 -08001681 if (metric_only)
1682 fputc('\n', stat_config.output);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001683 break;
1684 case AGGR_NONE:
Andi Kleen206cab62016-03-03 15:57:37 -08001685 if (metric_only)
1686 print_no_aggr_metric(prefix);
1687 else {
Andi Kleene864c5c2017-08-31 12:40:35 -07001688 evlist__for_each_entry(evsel_list, counter) {
1689 if (is_duration_time(counter))
1690 continue;
Andi Kleen206cab62016-03-03 15:57:37 -08001691 print_counter(counter, prefix);
Andi Kleene864c5c2017-08-31 12:40:35 -07001692 }
Andi Kleen206cab62016-03-03 15:57:37 -08001693 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001694 break;
Jiri Olsa208df992015-10-16 12:41:04 +02001695 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001696 default:
1697 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001698 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001699
Jiri Olsad4f63a42015-06-26 11:29:26 +02001700 if (!interval && !csv_output)
1701 print_footer();
1702
Jiri Olsa58215222015-07-21 14:31:24 +02001703 fflush(stat_config.output);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001704}
1705
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001706static volatile int signr = -1;
1707
Ingo Molnar52425192009-05-26 09:17:18 +02001708static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001709{
Jiri Olsaec0d3d12015-07-21 14:31:25 +02001710 if ((child_pid == -1) || stat_config.interval)
Liming Wang60666c62009-12-31 16:05:50 +08001711 done = 1;
1712
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001713 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001714 /*
1715 * render child_pid harmless
1716 * won't send SIGTERM to a random
1717 * process in case of race condition
1718 * and fast PID recycling
1719 */
1720 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001721}
1722
1723static void sig_atexit(void)
1724{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001725 sigset_t set, oset;
1726
1727 /*
1728 * avoid race condition with SIGCHLD handler
1729 * in skip_signal() which is modifying child_pid
1730 * goal is to avoid send SIGTERM to a random
1731 * process
1732 */
1733 sigemptyset(&set);
1734 sigaddset(&set, SIGCHLD);
1735 sigprocmask(SIG_BLOCK, &set, &oset);
1736
Chris Wilson933da832009-10-04 01:35:01 +01001737 if (child_pid != -1)
1738 kill(child_pid, SIGTERM);
1739
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001740 sigprocmask(SIG_SETMASK, &oset, NULL);
1741
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001742 if (signr == -1)
1743 return;
1744
1745 signal(signr, SIG_DFL);
1746 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001747}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001748
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001749static int stat__set_big_num(const struct option *opt __maybe_unused,
1750 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001751{
1752 big_num_opt = unset ? 0 : 1;
1753 return 0;
1754}
1755
Andi Kleen44b1e602016-05-30 12:49:42 -03001756static int enable_metric_only(const struct option *opt __maybe_unused,
1757 const char *s __maybe_unused, int unset)
1758{
1759 force_metric_only = true;
1760 metric_only = !unset;
1761 return 0;
1762}
1763
Andi Kleenb18f3e32017-08-31 12:40:31 -07001764static int parse_metric_groups(const struct option *opt,
1765 const char *str,
1766 int unset __maybe_unused)
1767{
1768 return metricgroup__parse_groups(opt, str, &metric_events);
1769}
1770
Jiri Olsae0547312015-11-05 15:40:45 +01001771static const struct option stat_options[] = {
1772 OPT_BOOLEAN('T', "transaction", &transaction_run,
1773 "hardware transaction statistics"),
1774 OPT_CALLBACK('e', "event", &evsel_list, "event",
1775 "event selector. use 'perf list' to list available events",
1776 parse_events_option),
1777 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1778 "event filter", parse_filter),
1779 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1780 "child tasks do not inherit counters"),
1781 OPT_STRING('p', "pid", &target.pid, "pid",
1782 "stat events on existing process id"),
1783 OPT_STRING('t', "tid", &target.tid, "tid",
1784 "stat events on existing thread id"),
1785 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1786 "system-wide collection from all CPUs"),
1787 OPT_BOOLEAN('g', "group", &group,
1788 "put the counters into a counter group"),
1789 OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"),
1790 OPT_INCR('v', "verbose", &verbose,
1791 "be more verbose (show counter open errors, etc)"),
1792 OPT_INTEGER('r', "repeat", &run_count,
1793 "repeat command and print average + stddev (max: 100, forever: 0)"),
1794 OPT_BOOLEAN('n', "null", &null_run,
1795 "null run - dont start any counters"),
1796 OPT_INCR('d', "detailed", &detailed_run,
1797 "detailed run - start a lot of events"),
1798 OPT_BOOLEAN('S', "sync", &sync_run,
1799 "call sync() before starting a run"),
1800 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1801 "print large numbers with thousands\' separators",
1802 stat__set_big_num),
1803 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1804 "list of cpus to monitor in system-wide"),
1805 OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1806 "disable CPU count aggregation", AGGR_NONE),
Andi Kleen430daf22017-03-20 13:17:00 -07001807 OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
Jiri Olsae0547312015-11-05 15:40:45 +01001808 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1809 "print counts with custom separator"),
1810 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1811 "monitor event in cgroup name only", parse_cgroups),
1812 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1813 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1814 OPT_INTEGER(0, "log-fd", &output_fd,
1815 "log output to fd, instead of stderr"),
1816 OPT_STRING(0, "pre", &pre_cmd, "command",
1817 "command to run prior to the measured command"),
1818 OPT_STRING(0, "post", &post_cmd, "command",
1819 "command to run after to the measured command"),
1820 OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1821 "print counts at regular interval in ms (>= 10)"),
1822 OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1823 "aggregate counts per processor socket", AGGR_SOCKET),
1824 OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1825 "aggregate counts per physical processor core", AGGR_CORE),
1826 OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1827 "aggregate counts per thread", AGGR_THREAD),
1828 OPT_UINTEGER('D', "delay", &initial_delay,
1829 "ms to wait before starting measurement after program start"),
Andi Kleen44b1e602016-05-30 12:49:42 -03001830 OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL,
1831 "Only print computed metrics. No raw values", enable_metric_only),
1832 OPT_BOOLEAN(0, "topdown", &topdown_run,
1833 "measure topdown level 1 statistics"),
Kan Liangdaefd0b2017-05-26 12:05:38 -07001834 OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1835 "measure SMI cost"),
Andi Kleenb18f3e32017-08-31 12:40:31 -07001836 OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1837 "monitor specified metrics or metric groups (separated by ,)",
1838 parse_metric_groups),
Jiri Olsae0547312015-11-05 15:40:45 +01001839 OPT_END()
1840};
1841
Jiri Olsa1fe7a302015-10-16 12:41:15 +02001842static int perf_stat__get_socket(struct cpu_map *map, int cpu)
1843{
1844 return cpu_map__get_socket(map, cpu, NULL);
1845}
1846
1847static int perf_stat__get_core(struct cpu_map *map, int cpu)
1848{
1849 return cpu_map__get_core(map, cpu, NULL);
1850}
1851
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001852static int cpu_map__get_max(struct cpu_map *map)
1853{
1854 int i, max = -1;
1855
1856 for (i = 0; i < map->nr; i++) {
1857 if (map->map[i] > max)
1858 max = map->map[i];
1859 }
1860
1861 return max;
1862}
1863
1864static struct cpu_map *cpus_aggr_map;
1865
1866static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx)
1867{
1868 int cpu;
1869
1870 if (idx >= map->nr)
1871 return -1;
1872
1873 cpu = map->map[idx];
1874
1875 if (cpus_aggr_map->map[cpu] == -1)
1876 cpus_aggr_map->map[cpu] = get_id(map, idx);
1877
1878 return cpus_aggr_map->map[cpu];
1879}
1880
1881static int perf_stat__get_socket_cached(struct cpu_map *map, int idx)
1882{
1883 return perf_stat__get_aggr(perf_stat__get_socket, map, idx);
1884}
1885
1886static int perf_stat__get_core_cached(struct cpu_map *map, int idx)
1887{
1888 return perf_stat__get_aggr(perf_stat__get_core, map, idx);
1889}
1890
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001891static int perf_stat_init_aggr_mode(void)
1892{
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001893 int nr;
1894
Jiri Olsa421a50f2015-07-21 14:31:22 +02001895 switch (stat_config.aggr_mode) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001896 case AGGR_SOCKET:
1897 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1898 perror("cannot build socket map");
1899 return -1;
1900 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001901 aggr_get_id = perf_stat__get_socket_cached;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001902 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001903 case AGGR_CORE:
1904 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1905 perror("cannot build core map");
1906 return -1;
1907 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001908 aggr_get_id = perf_stat__get_core_cached;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001909 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001910 case AGGR_NONE:
1911 case AGGR_GLOBAL:
Jiri Olsa32b8af82015-06-26 11:29:27 +02001912 case AGGR_THREAD:
Jiri Olsa208df992015-10-16 12:41:04 +02001913 case AGGR_UNSET:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001914 default:
1915 break;
1916 }
Jiri Olsa1e5a2932015-10-25 15:51:18 +01001917
1918 /*
1919 * The evsel_list->cpus is the base we operate on,
1920 * taking the highest cpu number to be the size of
1921 * the aggregation translate cpumap.
1922 */
1923 nr = cpu_map__get_max(evsel_list->cpus);
1924 cpus_aggr_map = cpu_map__empty_new(nr + 1);
1925 return cpus_aggr_map ? 0 : -ENOMEM;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001926}
1927
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09001928static void perf_stat__exit_aggr_mode(void)
1929{
1930 cpu_map__put(aggr_map);
1931 cpu_map__put(cpus_aggr_map);
1932 aggr_map = NULL;
1933 cpus_aggr_map = NULL;
1934}
1935
Jiri Olsa68d702f2015-11-05 15:40:58 +01001936static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx)
1937{
1938 int cpu;
1939
1940 if (idx > map->nr)
1941 return -1;
1942
1943 cpu = map->map[idx];
1944
Jan Stancekda8a58b2017-02-17 12:10:26 +01001945 if (cpu >= env->nr_cpus_avail)
Jiri Olsa68d702f2015-11-05 15:40:58 +01001946 return -1;
1947
1948 return cpu;
1949}
1950
1951static int perf_env__get_socket(struct cpu_map *map, int idx, void *data)
1952{
1953 struct perf_env *env = data;
1954 int cpu = perf_env__get_cpu(env, map, idx);
1955
1956 return cpu == -1 ? -1 : env->cpu[cpu].socket_id;
1957}
1958
1959static int perf_env__get_core(struct cpu_map *map, int idx, void *data)
1960{
1961 struct perf_env *env = data;
1962 int core = -1, cpu = perf_env__get_cpu(env, map, idx);
1963
1964 if (cpu != -1) {
1965 int socket_id = env->cpu[cpu].socket_id;
1966
1967 /*
1968 * Encode socket in upper 16 bits
1969 * core_id is relative to socket, and
1970 * we need a global id. So we combine
1971 * socket + core id.
1972 */
1973 core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff);
1974 }
1975
1976 return core;
1977}
1978
1979static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus,
1980 struct cpu_map **sockp)
1981{
1982 return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env);
1983}
1984
1985static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus,
1986 struct cpu_map **corep)
1987{
1988 return cpu_map__build_map(cpus, corep, perf_env__get_core, env);
1989}
1990
1991static int perf_stat__get_socket_file(struct cpu_map *map, int idx)
1992{
1993 return perf_env__get_socket(map, idx, &perf_stat.session->header.env);
1994}
1995
1996static int perf_stat__get_core_file(struct cpu_map *map, int idx)
1997{
1998 return perf_env__get_core(map, idx, &perf_stat.session->header.env);
1999}
2000
2001static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
2002{
2003 struct perf_env *env = &st->session->header.env;
2004
2005 switch (stat_config.aggr_mode) {
2006 case AGGR_SOCKET:
2007 if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) {
2008 perror("cannot build socket map");
2009 return -1;
2010 }
2011 aggr_get_id = perf_stat__get_socket_file;
2012 break;
2013 case AGGR_CORE:
2014 if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) {
2015 perror("cannot build core map");
2016 return -1;
2017 }
2018 aggr_get_id = perf_stat__get_core_file;
2019 break;
2020 case AGGR_NONE:
2021 case AGGR_GLOBAL:
2022 case AGGR_THREAD:
2023 case AGGR_UNSET:
2024 default:
2025 break;
2026 }
2027
2028 return 0;
2029}
2030
Andi Kleen44b1e602016-05-30 12:49:42 -03002031static int topdown_filter_events(const char **attr, char **str, bool use_group)
2032{
2033 int off = 0;
2034 int i;
2035 int len = 0;
2036 char *s;
2037
2038 for (i = 0; attr[i]; i++) {
2039 if (pmu_have_event("cpu", attr[i])) {
2040 len += strlen(attr[i]) + 1;
2041 attr[i - off] = attr[i];
2042 } else
2043 off++;
2044 }
2045 attr[i - off] = NULL;
2046
2047 *str = malloc(len + 1 + 2);
2048 if (!*str)
2049 return -1;
2050 s = *str;
2051 if (i - off == 0) {
2052 *s = 0;
2053 return 0;
2054 }
2055 if (use_group)
2056 *s++ = '{';
2057 for (i = 0; attr[i]; i++) {
2058 strcpy(s, attr[i]);
2059 s += strlen(s);
2060 *s++ = ',';
2061 }
2062 if (use_group) {
2063 s[-1] = '}';
2064 *s = 0;
2065 } else
2066 s[-1] = 0;
2067 return 0;
2068}
2069
2070__weak bool arch_topdown_check_group(bool *warn)
2071{
2072 *warn = false;
2073 return false;
2074}
2075
2076__weak void arch_topdown_group_warn(void)
2077{
2078}
2079
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002080/*
2081 * Add default attributes, if there were no attributes specified or
2082 * if -d/--detailed, -d -d or -d -d -d is used:
2083 */
2084static int add_default_attributes(void)
2085{
Andi Kleen44b1e602016-05-30 12:49:42 -03002086 int err;
Andi Kleen9dec4472016-02-26 16:27:56 -08002087 struct perf_event_attr default_attrs0[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002088
2089 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
2090 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
2091 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
2092 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
2093
2094 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Andi Kleen9dec4472016-02-26 16:27:56 -08002095};
2096 struct perf_event_attr frontend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002097 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002098};
2099 struct perf_event_attr backend_attrs[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002100 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Andi Kleen9dec4472016-02-26 16:27:56 -08002101};
2102 struct perf_event_attr default_attrs1[] = {
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002103 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
2104 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
2105 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
2106
2107};
2108
2109/*
2110 * Detailed stats (-d), covering the L1 and last level data caches:
2111 */
2112 struct perf_event_attr detailed_attrs[] = {
2113
2114 { .type = PERF_TYPE_HW_CACHE,
2115 .config =
2116 PERF_COUNT_HW_CACHE_L1D << 0 |
2117 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2118 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2119
2120 { .type = PERF_TYPE_HW_CACHE,
2121 .config =
2122 PERF_COUNT_HW_CACHE_L1D << 0 |
2123 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2124 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2125
2126 { .type = PERF_TYPE_HW_CACHE,
2127 .config =
2128 PERF_COUNT_HW_CACHE_LL << 0 |
2129 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2130 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2131
2132 { .type = PERF_TYPE_HW_CACHE,
2133 .config =
2134 PERF_COUNT_HW_CACHE_LL << 0 |
2135 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2136 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2137};
2138
2139/*
2140 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
2141 */
2142 struct perf_event_attr very_detailed_attrs[] = {
2143
2144 { .type = PERF_TYPE_HW_CACHE,
2145 .config =
2146 PERF_COUNT_HW_CACHE_L1I << 0 |
2147 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2148 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2149
2150 { .type = PERF_TYPE_HW_CACHE,
2151 .config =
2152 PERF_COUNT_HW_CACHE_L1I << 0 |
2153 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2154 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2155
2156 { .type = PERF_TYPE_HW_CACHE,
2157 .config =
2158 PERF_COUNT_HW_CACHE_DTLB << 0 |
2159 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2160 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2161
2162 { .type = PERF_TYPE_HW_CACHE,
2163 .config =
2164 PERF_COUNT_HW_CACHE_DTLB << 0 |
2165 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2166 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2167
2168 { .type = PERF_TYPE_HW_CACHE,
2169 .config =
2170 PERF_COUNT_HW_CACHE_ITLB << 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_ITLB << 0 |
2177 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
2178 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2179
2180};
2181
2182/*
2183 * Very, very detailed stats (-d -d -d), adding prefetch events:
2184 */
2185 struct perf_event_attr very_very_detailed_attrs[] = {
2186
2187 { .type = PERF_TYPE_HW_CACHE,
2188 .config =
2189 PERF_COUNT_HW_CACHE_L1D << 0 |
2190 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2191 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
2192
2193 { .type = PERF_TYPE_HW_CACHE,
2194 .config =
2195 PERF_COUNT_HW_CACHE_L1D << 0 |
2196 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
2197 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
2198};
2199
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002200 /* Set attrs if no event is selected and !null_run: */
2201 if (null_run)
2202 return 0;
2203
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002204 if (transaction_run) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002205 if (pmu_have_event("cpu", "cycles-ct") &&
2206 pmu_have_event("cpu", "el-start"))
Jiri Olsaa4547422015-06-03 16:25:53 +02002207 err = parse_events(evsel_list, transaction_attrs, NULL);
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002208 else
Jiri Olsaa4547422015-06-03 16:25:53 +02002209 err = parse_events(evsel_list, transaction_limited_attrs, NULL);
2210 if (err) {
Andi Kleen4cabc3d2013-08-21 16:47:26 -07002211 fprintf(stderr, "Cannot set up transaction events\n");
2212 return -1;
2213 }
2214 return 0;
2215 }
2216
Kan Liangdaefd0b2017-05-26 12:05:38 -07002217 if (smi_cost) {
2218 int smi;
2219
2220 if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2221 fprintf(stderr, "freeze_on_smi is not supported.\n");
2222 return -1;
2223 }
2224
2225 if (!smi) {
2226 if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2227 fprintf(stderr, "Failed to set freeze_on_smi.\n");
2228 return -1;
2229 }
2230 smi_reset = true;
2231 }
2232
2233 if (pmu_have_event("msr", "aperf") &&
2234 pmu_have_event("msr", "smi")) {
2235 if (!force_metric_only)
2236 metric_only = true;
2237 err = parse_events(evsel_list, smi_cost_attrs, NULL);
2238 } else {
2239 fprintf(stderr, "To measure SMI cost, it needs "
2240 "msr/aperf/, msr/smi/ and cpu/cycles/ support\n");
2241 return -1;
2242 }
2243 if (err) {
2244 fprintf(stderr, "Cannot set up SMI cost events\n");
2245 return -1;
2246 }
2247 return 0;
2248 }
2249
Andi Kleen44b1e602016-05-30 12:49:42 -03002250 if (topdown_run) {
2251 char *str = NULL;
2252 bool warn = false;
2253
2254 if (stat_config.aggr_mode != AGGR_GLOBAL &&
2255 stat_config.aggr_mode != AGGR_CORE) {
2256 pr_err("top down event configuration requires --per-core mode\n");
2257 return -1;
2258 }
2259 stat_config.aggr_mode = AGGR_CORE;
2260 if (nr_cgroups || !target__has_cpu(&target)) {
2261 pr_err("top down event configuration requires system-wide mode (-a)\n");
2262 return -1;
2263 }
2264
2265 if (!force_metric_only)
2266 metric_only = true;
2267 if (topdown_filter_events(topdown_attrs, &str,
2268 arch_topdown_check_group(&warn)) < 0) {
2269 pr_err("Out of memory\n");
2270 return -1;
2271 }
2272 if (topdown_attrs[0] && str) {
2273 if (warn)
2274 arch_topdown_group_warn();
2275 err = parse_events(evsel_list, str, NULL);
2276 if (err) {
2277 fprintf(stderr,
2278 "Cannot set up top down events %s: %d\n",
2279 str, err);
2280 free(str);
2281 return -1;
2282 }
2283 } else {
2284 fprintf(stderr, "System does not support topdown\n");
2285 return -1;
2286 }
2287 free(str);
2288 }
2289
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002290 if (!evsel_list->nr_entries) {
Namhyung Kima1f3d562016-05-13 15:01:03 +09002291 if (target__has_cpu(&target))
2292 default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2293
Andi Kleen9dec4472016-02-26 16:27:56 -08002294 if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2295 return -1;
2296 if (pmu_have_event("cpu", "stalled-cycles-frontend")) {
2297 if (perf_evlist__add_default_attrs(evsel_list,
2298 frontend_attrs) < 0)
2299 return -1;
2300 }
2301 if (pmu_have_event("cpu", "stalled-cycles-backend")) {
2302 if (perf_evlist__add_default_attrs(evsel_list,
2303 backend_attrs) < 0)
2304 return -1;
2305 }
2306 if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002307 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002308 }
2309
2310 /* Detailed events get appended to the event list: */
2311
2312 if (detailed_run < 1)
2313 return 0;
2314
2315 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002316 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002317 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002318
2319 if (detailed_run < 2)
2320 return 0;
2321
2322 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002323 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02002324 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002325
2326 if (detailed_run < 3)
2327 return 0;
2328
2329 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03002330 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002331}
2332
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002333static const char * const stat_record_usage[] = {
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002334 "perf stat record [<options>]",
2335 NULL,
2336};
2337
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002338static void init_features(struct perf_session *session)
2339{
2340 int feat;
2341
2342 for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2343 perf_header__set_feat(&session->header, feat);
2344
2345 perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2346 perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2347 perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2348 perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2349}
2350
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002351static int __cmd_record(int argc, const char **argv)
2352{
2353 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002354 struct perf_data *data = &perf_stat.data;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002355
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002356 argc = parse_options(argc, argv, stat_options, stat_record_usage,
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002357 PARSE_OPT_STOP_AT_NON_OPTION);
2358
2359 if (output_name)
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002360 data->file.path = output_name;
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002361
Jiri Olsae9d6db8e82015-11-05 15:40:53 +01002362 if (run_count != 1 || forever) {
2363 pr_err("Cannot use -r option with perf stat record.\n");
2364 return -1;
2365 }
2366
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002367 session = perf_session__new(data, false, NULL);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002368 if (session == NULL) {
2369 pr_err("Perf session creation failed.\n");
2370 return -1;
2371 }
2372
Jiri Olsa3ba78bd2015-11-05 15:40:47 +01002373 init_features(session);
2374
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002375 session->evlist = evsel_list;
2376 perf_stat.session = session;
2377 perf_stat.record = true;
2378 return argc;
2379}
2380
Jiri Olsaa56f9392015-11-05 15:40:59 +01002381static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2382 union perf_event *event,
2383 struct perf_session *session)
2384{
Andi Kleene3b03b62016-05-05 16:04:03 -07002385 struct stat_round_event *stat_round = &event->stat_round;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002386 struct perf_evsel *counter;
2387 struct timespec tsh, *ts = NULL;
2388 const char **argv = session->header.env.cmdline_argv;
2389 int argc = session->header.env.nr_cmdline;
2390
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002391 evlist__for_each_entry(evsel_list, counter)
Jiri Olsaa56f9392015-11-05 15:40:59 +01002392 perf_stat_process_counter(&stat_config, counter);
2393
Andi Kleene3b03b62016-05-05 16:04:03 -07002394 if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2395 update_stats(&walltime_nsecs_stats, stat_round->time);
Jiri Olsaa56f9392015-11-05 15:40:59 +01002396
Andi Kleene3b03b62016-05-05 16:04:03 -07002397 if (stat_config.interval && stat_round->time) {
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -03002398 tsh.tv_sec = stat_round->time / NSEC_PER_SEC;
2399 tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
Jiri Olsaa56f9392015-11-05 15:40:59 +01002400 ts = &tsh;
2401 }
2402
2403 print_counters(ts, argc, argv);
2404 return 0;
2405}
2406
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002407static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002408int process_stat_config_event(struct perf_tool *tool,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002409 union perf_event *event,
2410 struct perf_session *session __maybe_unused)
2411{
Jiri Olsa68d702f2015-11-05 15:40:58 +01002412 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2413
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002414 perf_event__read_stat_config(&stat_config, &event->stat_config);
Jiri Olsa68d702f2015-11-05 15:40:58 +01002415
Jiri Olsa89af4e02015-11-05 15:41:02 +01002416 if (cpu_map__empty(st->cpus)) {
2417 if (st->aggr_mode != AGGR_UNSET)
2418 pr_warning("warning: processing task data, aggregation mode not set\n");
2419 return 0;
2420 }
2421
2422 if (st->aggr_mode != AGGR_UNSET)
2423 stat_config.aggr_mode = st->aggr_mode;
2424
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002425 if (perf_stat.data.is_pipe)
Jiri Olsa68d702f2015-11-05 15:40:58 +01002426 perf_stat_init_aggr_mode();
2427 else
2428 perf_stat_init_aggr_mode_file(st);
2429
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002430 return 0;
2431}
2432
Jiri Olsa1975d362015-11-05 15:40:56 +01002433static int set_maps(struct perf_stat *st)
2434{
2435 if (!st->cpus || !st->threads)
2436 return 0;
2437
2438 if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2439 return -EINVAL;
2440
2441 perf_evlist__set_maps(evsel_list, st->cpus, st->threads);
2442
2443 if (perf_evlist__alloc_stats(evsel_list, true))
2444 return -ENOMEM;
2445
2446 st->maps_allocated = true;
2447 return 0;
2448}
2449
2450static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002451int process_thread_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002452 union perf_event *event,
2453 struct perf_session *session __maybe_unused)
2454{
2455 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2456
2457 if (st->threads) {
2458 pr_warning("Extra thread map event, ignoring.\n");
2459 return 0;
2460 }
2461
2462 st->threads = thread_map__new_event(&event->thread_map);
2463 if (!st->threads)
2464 return -ENOMEM;
2465
2466 return set_maps(st);
2467}
2468
2469static
Arnaldo Carvalho de Melo7e6a7992016-12-12 10:52:10 -03002470int process_cpu_map_event(struct perf_tool *tool,
Jiri Olsa1975d362015-11-05 15:40:56 +01002471 union perf_event *event,
2472 struct perf_session *session __maybe_unused)
2473{
2474 struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2475 struct cpu_map *cpus;
2476
2477 if (st->cpus) {
2478 pr_warning("Extra cpu map event, ignoring.\n");
2479 return 0;
2480 }
2481
2482 cpus = cpu_map__new_data(&event->cpu_map.data);
2483 if (!cpus)
2484 return -ENOMEM;
2485
2486 st->cpus = cpus;
2487 return set_maps(st);
2488}
2489
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002490static const char * const stat_report_usage[] = {
Jiri Olsaba6039b62015-11-05 15:40:55 +01002491 "perf stat report [<options>]",
2492 NULL,
2493};
2494
2495static struct perf_stat perf_stat = {
2496 .tool = {
2497 .attr = perf_event__process_attr,
Jiri Olsafa6ea782015-11-05 15:41:00 +01002498 .event_update = perf_event__process_event_update,
Jiri Olsa1975d362015-11-05 15:40:56 +01002499 .thread_map = process_thread_map_event,
2500 .cpu_map = process_cpu_map_event,
Jiri Olsa62ba18b2015-11-05 15:40:57 +01002501 .stat_config = process_stat_config_event,
Jiri Olsaa56f9392015-11-05 15:40:59 +01002502 .stat = perf_event__process_stat_event,
2503 .stat_round = process_stat_round_event,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002504 },
Jiri Olsa89af4e02015-11-05 15:41:02 +01002505 .aggr_mode = AGGR_UNSET,
Jiri Olsaba6039b62015-11-05 15:40:55 +01002506};
2507
2508static int __cmd_report(int argc, const char **argv)
2509{
2510 struct perf_session *session;
2511 const struct option options[] = {
2512 OPT_STRING('i', "input", &input_name, "file", "input file name"),
Jiri Olsa89af4e02015-11-05 15:41:02 +01002513 OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2514 "aggregate counts per processor socket", AGGR_SOCKET),
2515 OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2516 "aggregate counts per physical processor core", AGGR_CORE),
2517 OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2518 "disable CPU count aggregation", AGGR_NONE),
Jiri Olsaba6039b62015-11-05 15:40:55 +01002519 OPT_END()
2520 };
2521 struct stat st;
2522 int ret;
2523
Jiri Olsa8a59f3c2016-01-12 10:35:29 +01002524 argc = parse_options(argc, argv, options, stat_report_usage, 0);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002525
2526 if (!input_name || !strlen(input_name)) {
2527 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2528 input_name = "-";
2529 else
2530 input_name = "perf.data";
2531 }
2532
Jiri Olsaeae8ad82017-01-23 22:25:41 +01002533 perf_stat.data.file.path = input_name;
2534 perf_stat.data.mode = PERF_DATA_MODE_READ;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002535
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002536 session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
Jiri Olsaba6039b62015-11-05 15:40:55 +01002537 if (session == NULL)
2538 return -1;
2539
2540 perf_stat.session = session;
2541 stat_config.output = stderr;
2542 evsel_list = session->evlist;
2543
2544 ret = perf_session__process_events(session);
2545 if (ret)
2546 return ret;
2547
2548 perf_session__delete(session);
2549 return 0;
2550}
2551
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002552static void setup_system_wide(int forks)
2553{
2554 /*
2555 * Make system wide (-a) the default target if
2556 * no target was specified and one of following
2557 * conditions is met:
2558 *
2559 * - there's no workload specified
2560 * - there is workload specified but all requested
2561 * events are system wide events
2562 */
2563 if (!target__none(&target))
2564 return;
2565
2566 if (!forks)
2567 target.system_wide = true;
2568 else {
2569 struct perf_evsel *counter;
2570
2571 evlist__for_each_entry(evsel_list, counter) {
2572 if (!counter->system_wide)
2573 return;
2574 }
2575
2576 if (evsel_list->nr_entries)
2577 target.system_wide = true;
2578 }
2579}
2580
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -03002581int cmd_stat(int argc, const char **argv)
Ingo Molnar52425192009-05-26 09:17:18 +02002582{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03002583 const char * const stat_usage[] = {
2584 "perf stat [<options>] [<command>]",
2585 NULL
2586 };
Namhyung Kimcc03c542013-11-01 16:33:15 +09002587 int status = -EINVAL, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002588 const char *mode;
Jiri Olsa58215222015-07-21 14:31:24 +02002589 FILE *output = stderr;
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002590 unsigned int interval;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002591 const char * const stat_subcommands[] = { "record", "report" };
Ingo Molnar42202dd2009-06-13 14:57:28 +02002592
Stephane Eranian5af52b52010-05-18 15:00:01 +02002593 setlocale(LC_ALL, "");
2594
Namhyung Kim334fe7a2013-03-11 16:43:12 +09002595 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02002596 if (evsel_list == NULL)
2597 return -ENOMEM;
2598
Wang Nan1669e502016-02-19 11:43:58 +00002599 parse_events__shrink_config_terms();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002600 argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2601 (const char **) stat_usage,
2602 PARSE_OPT_STOP_AT_NON_OPTION);
Andi Kleen37932c12017-03-20 13:17:08 -07002603 perf_stat__collect_metric_expr(evsel_list);
Andi Kleenfb4605b2016-03-01 10:57:52 -08002604 perf_stat__init_shadow_stats();
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002605
Jiri Olsa6edb78a2015-11-05 15:41:01 +01002606 if (csv_sep) {
2607 csv_output = true;
2608 if (!strcmp(csv_sep, "\\t"))
2609 csv_sep = "\t";
2610 } else
2611 csv_sep = DEFAULT_SEPARATOR;
2612
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002613 if (argc && !strncmp(argv[0], "rec", 3)) {
2614 argc = __cmd_record(argc, argv);
2615 if (argc < 0)
2616 return -1;
Jiri Olsaba6039b62015-11-05 15:40:55 +01002617 } else if (argc && !strncmp(argv[0], "rep", 3))
2618 return __cmd_report(argc, argv);
Stephane Eraniand7470b62010-12-01 18:49:05 +02002619
Jiri Olsaec0d3d12015-07-21 14:31:25 +02002620 interval = stat_config.interval;
2621
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002622 /*
2623 * For record command the -o is already taken care of.
2624 */
2625 if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
Stephane Eranian4aa90152011-08-15 22:22:33 +02002626 output = NULL;
2627
Jim Cromie56f3bae2011-09-07 17:14:00 -06002628 if (output_name && output_fd) {
2629 fprintf(stderr, "cannot use both --output and --log-fd\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002630 parse_options_usage(stat_usage, stat_options, "o", 1);
2631 parse_options_usage(NULL, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002632 goto out;
Jim Cromie56f3bae2011-09-07 17:14:00 -06002633 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002634
Andi Kleen54b50912016-03-03 15:57:36 -08002635 if (metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2636 fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2637 goto out;
2638 }
2639
Andi Kleen54b50912016-03-03 15:57:36 -08002640 if (metric_only && run_count > 1) {
2641 fprintf(stderr, "--metric-only is not supported with -r\n");
2642 goto out;
2643 }
2644
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002645 if (output_fd < 0) {
2646 fprintf(stderr, "argument to --log-fd must be a > 0\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002647 parse_options_usage(stat_usage, stat_options, "log-fd", 0);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002648 goto out;
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002649 }
2650
Stephane Eranian4aa90152011-08-15 22:22:33 +02002651 if (!output) {
2652 struct timespec tm;
2653 mode = append_file ? "a" : "w";
2654
2655 output = fopen(output_name, mode);
2656 if (!output) {
2657 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06002658 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02002659 }
2660 clock_gettime(CLOCK_REALTIME, &tm);
2661 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02002662 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06002663 mode = append_file ? "a" : "w";
2664 output = fdopen(output_fd, mode);
2665 if (!output) {
2666 perror("Failed opening logfd");
2667 return -errno;
2668 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02002669 }
2670
Jiri Olsa58215222015-07-21 14:31:24 +02002671 stat_config.output = output;
2672
Stephane Eraniand7470b62010-12-01 18:49:05 +02002673 /*
2674 * let the spreadsheet do the pretty-printing
2675 */
2676 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06002677 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02002678 if (big_num_opt == 1) {
2679 fprintf(stderr, "-B option not supported with -x\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002680 parse_options_usage(stat_usage, stat_options, "B", 1);
2681 parse_options_usage(NULL, stat_options, "x", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002682 goto out;
Stephane Eraniand7470b62010-12-01 18:49:05 +02002683 } else /* Nope, so disable big number formatting */
2684 big_num = false;
2685 } else if (big_num_opt == 0) /* User passed --no-big-num */
2686 big_num = false;
2687
Jiri Olsae3ba76d2017-02-27 10:48:18 +01002688 setup_system_wide(argc);
David Ahernac3063b2013-09-30 07:37:37 -06002689
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002690 if (run_count < 0) {
Namhyung Kimcc03c542013-11-01 16:33:15 +09002691 pr_err("Run count must be a positive number\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002692 parse_options_usage(stat_usage, stat_options, "r", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002693 goto out;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002694 } else if (run_count == 0) {
2695 forever = true;
2696 run_count = 1;
2697 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002698
Jiri Olsa421a50f2015-07-21 14:31:22 +02002699 if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) {
Jiri Olsa32b8af82015-06-26 11:29:27 +02002700 fprintf(stderr, "The --per-thread option is only available "
2701 "when monitoring via -p -t options.\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002702 parse_options_usage(NULL, stat_options, "p", 1);
2703 parse_options_usage(NULL, stat_options, "t", 1);
Jiri Olsa32b8af82015-06-26 11:29:27 +02002704 goto out;
2705 }
2706
2707 /*
2708 * no_aggr, cgroup are for system-wide only
2709 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2710 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002711 if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2712 stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002713 !target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02002714 fprintf(stderr, "both cgroup and no-aggregation "
2715 "modes only available in system-wide mode\n");
2716
Jiri Olsae0547312015-11-05 15:40:45 +01002717 parse_options_usage(stat_usage, stat_options, "G", 1);
2718 parse_options_usage(NULL, stat_options, "A", 1);
2719 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002720 goto out;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01002721 }
2722
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02002723 if (add_default_attributes())
2724 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002725
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002726 target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02002727
Namhyung Kim77a6f012012-05-07 14:09:04 +09002728 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002729 if (target__has_task(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002730 pr_err("Problems finding threads of monitor\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002731 parse_options_usage(stat_usage, stat_options, "p", 1);
2732 parse_options_usage(NULL, stat_options, "t", 1);
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002733 } else if (target__has_cpu(&target)) {
Namhyung Kim77a6f012012-05-07 14:09:04 +09002734 perror("failed to parse CPUs map");
Jiri Olsae0547312015-11-05 15:40:45 +01002735 parse_options_usage(stat_usage, stat_options, "C", 1);
2736 parse_options_usage(NULL, stat_options, "a", 1);
Namhyung Kimcc03c542013-11-01 16:33:15 +09002737 }
2738 goto out;
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02002739 }
Jiri Olsa32b8af82015-06-26 11:29:27 +02002740
2741 /*
2742 * Initialize thread_map with comm names,
2743 * so we could print it out on output.
2744 */
Jiri Olsa421a50f2015-07-21 14:31:22 +02002745 if (stat_config.aggr_mode == AGGR_THREAD)
Jiri Olsa32b8af82015-06-26 11:29:27 +02002746 thread_map__read_comms(evsel_list->threads);
2747
Stephane Eranian13370a92013-01-29 12:47:44 +01002748 if (interval && interval < 100) {
Kan Liang19afd102015-10-02 05:04:34 -04002749 if (interval < 10) {
2750 pr_err("print interval must be >= 10ms\n");
Jiri Olsae0547312015-11-05 15:40:45 +01002751 parse_options_usage(stat_usage, stat_options, "I", 1);
Kan Liang19afd102015-10-02 05:04:34 -04002752 goto out;
2753 } else
2754 pr_warning("print interval < 100ms. "
2755 "The overhead percentage could be high in some cases. "
2756 "Please proceed with caution.\n");
Stephane Eranian13370a92013-01-29 12:47:44 +01002757 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02002758
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002759 if (perf_evlist__alloc_stats(evsel_list, interval))
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002760 goto out;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03002761
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002762 if (perf_stat_init_aggr_mode())
Arnaldo Carvalho de Melo03ad9742014-01-03 15:56:06 -03002763 goto out;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01002764
Ingo Molnar58d7e992009-05-15 11:03:23 +02002765 /*
2766 * We dont want to block the signals - that would cause
2767 * child tasks to inherit that and Ctrl-C would not work.
2768 * What we want is for Ctrl-C to work in the exec()-ed
2769 * task, but being ignored by perf stat itself:
2770 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02002771 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002772 if (!forever)
2773 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01002774 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02002775 signal(SIGALRM, skip_signal);
2776 signal(SIGABRT, skip_signal);
2777
Ingo Molnar42202dd2009-06-13 14:57:28 +02002778 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002779 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Namhyung Kimbb963e12017-02-17 17:17:38 +09002780 if (run_count != 1 && verbose > 0)
Stephane Eranian4aa90152011-08-15 22:22:33 +02002781 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2782 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02002783
Ingo Molnar42202dd2009-06-13 14:57:28 +02002784 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002785 if (forever && status != -1) {
Jiri Olsad4f63a42015-06-26 11:29:26 +02002786 print_counters(NULL, argc, argv);
Jiri Olsa254ecbc72015-06-26 11:29:13 +02002787 perf_stat__reset_stats();
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002788 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02002789 }
2790
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05002791 if (!forever && status != -1 && !interval)
Jiri Olsad4f63a42015-06-26 11:29:26 +02002792 print_counters(NULL, argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002793
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002794 if (STAT_RECORD) {
2795 /*
2796 * We synthesize the kernel mmap record just so that older tools
2797 * don't emit warnings about not being able to resolve symbols
2798 * due to /proc/sys/kernel/kptr_restrict settings and instear provide
2799 * a saner message about no samples being in the perf.data file.
2800 *
2801 * This also serves to suppress a warning about f_header.data.size == 0
Jiri Olsa8b99b1a2015-11-05 15:40:48 +01002802 * in header.c at the moment 'perf stat record' gets introduced, which
2803 * is not really needed once we start adding the stat specific PERF_RECORD_
2804 * records, but the need to suppress the kptr_restrict messages in older
2805 * tools remain -acme
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002806 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002807 int fd = perf_data__fd(&perf_stat.data);
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002808 int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2809 process_synthesized_event,
2810 &perf_stat.session->machines.host);
2811 if (err) {
2812 pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2813 "older tools may produce warnings about this file\n.");
2814 }
2815
Jiri Olsa7aad0c32015-11-05 15:40:52 +01002816 if (!interval) {
2817 if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2818 pr_err("failed to write stat round event\n");
2819 }
2820
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002821 if (!perf_stat.data.is_pipe) {
Jiri Olsa664c98d2015-11-05 15:40:50 +01002822 perf_stat.session->header.data_size += perf_stat.bytes_written;
2823 perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2824 }
Jiri Olsa4979d0c2015-11-05 15:40:46 +01002825
2826 perf_session__delete(perf_stat.session);
2827 }
2828
Masami Hiramatsu544c2ae2015-12-09 11:11:27 +09002829 perf_stat__exit_aggr_mode();
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03002830 perf_evlist__free_stats(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002831out:
Kan Liangdaefd0b2017-05-26 12:05:38 -07002832 if (smi_cost && smi_reset)
2833 sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
2834
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02002835 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02002836 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02002837}