blob: f686d5ff594e6b93c6e6f732e7a1c97aa18c4e49 [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"
Ingo Molnar148be2c2009-04-27 08:02:14 +020046#include "util/util.h"
Ingo Molnar52425192009-05-26 09:17:18 +020047#include "util/parse-options.h"
48#include "util/parse-events.h"
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020049#include "util/event.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020050#include "util/evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020051#include "util/evsel.h"
Frederic Weisbecker8f28827a2009-08-16 22:05:48 +020052#include "util/debug.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020053#include "util/color.h"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080054#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080055#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110056#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030057#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020058#include "util/thread_map.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020059
Peter Zijlstra1f16c572012-10-23 13:40:14 +020060#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020061#include <sys/prctl.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020062#include <locale.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020063
Stephane Eraniand7470b62010-12-01 18:49:05 +020064#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060065#define CNTR_NOT_SUPPORTED "<not supported>"
66#define CNTR_NOT_COUNTED "<not counted>"
Stephane Eraniand7470b62010-12-01 18:49:05 +020067
Stephane Eranian13370a92013-01-29 12:47:44 +010068static void print_stat(int argc, const char **argv);
69static void print_counter_aggr(struct perf_evsel *counter, char *prefix);
70static void print_counter(struct perf_evsel *counter, char *prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +010071static void print_aggr(char *prefix);
Stephane Eranian13370a92013-01-29 12:47:44 +010072
Robert Richter666e6d42012-04-05 18:26:27 +020073static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020074
Namhyung Kim77a6f012012-05-07 14:09:04 +090075static struct perf_target target = {
76 .uid = UINT_MAX,
77};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +053078
Stephane Eranian86ee6e12013-02-14 13:57:27 +010079enum aggr_mode {
80 AGGR_NONE,
81 AGGR_GLOBAL,
82 AGGR_SOCKET,
Stephane Eranian12c08a92013-02-14 13:57:29 +010083 AGGR_CORE,
Stephane Eranian86ee6e12013-02-14 13:57:27 +010084};
85
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +053086static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +020087static bool no_inherit = false;
Ian Munsiec0555642010-04-13 18:37:33 +100088static bool scale = true;
Stephane Eranian86ee6e12013-02-14 13:57:27 +010089static enum aggr_mode aggr_mode = AGGR_GLOBAL;
Stephane Eraniand07f0b12013-06-04 17:44:26 +020090static volatile pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +100091static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020092static int detailed_run = 0;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -020093static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +020094static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +020095static const char *csv_sep = NULL;
96static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +080097static bool group = false;
Stephane Eranian4aa90152011-08-15 22:22:33 +020098static FILE *output = NULL;
Peter Zijlstra1f16c572012-10-23 13:40:14 +020099static const char *pre_cmd = NULL;
100static const char *post_cmd = NULL;
101static bool sync_run = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100102static unsigned int interval = 0;
Andi Kleen41191682013-08-02 17:41:11 -0700103static unsigned int initial_delay = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500104static bool forever = false;
Stephane Eranian13370a92013-01-29 12:47:44 +0100105static struct timespec ref_time;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100106static struct cpu_map *aggr_map;
107static int (*aggr_get_id)(struct cpu_map *m, int cpu);
Stephane Eranian5af52b52010-05-18 15:00:01 +0200108
Liming Wang60666c62009-12-31 16:05:50 +0800109static volatile int done = 0;
110
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200111struct perf_stat {
112 struct stats res_stats[3];
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200113};
114
Stephane Eranian13370a92013-01-29 12:47:44 +0100115static inline void diff_timespec(struct timespec *r, struct timespec *a,
116 struct timespec *b)
117{
118 r->tv_sec = a->tv_sec - b->tv_sec;
119 if (a->tv_nsec < b->tv_nsec) {
120 r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec;
121 r->tv_sec--;
122 } else {
123 r->tv_nsec = a->tv_nsec - b->tv_nsec ;
124 }
125}
126
127static inline struct cpu_map *perf_evsel__cpus(struct perf_evsel *evsel)
128{
129 return (evsel->cpus && !target.cpu_list) ? evsel->cpus : evsel_list->cpus;
130}
131
132static inline int perf_evsel__nr_cpus(struct perf_evsel *evsel)
133{
134 return perf_evsel__cpus(evsel)->nr;
135}
136
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500137static void perf_evsel__reset_stat_priv(struct perf_evsel *evsel)
138{
139 memset(evsel->priv, 0, sizeof(struct perf_stat));
140}
141
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200142static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200143{
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200144 evsel->priv = zalloc(sizeof(struct perf_stat));
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200145 return evsel->priv == NULL ? -ENOMEM : 0;
146}
147
148static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
149{
150 free(evsel->priv);
151 evsel->priv = NULL;
152}
153
Stephane Eranian13370a92013-01-29 12:47:44 +0100154static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800155{
Stephane Eranian13370a92013-01-29 12:47:44 +0100156 void *addr;
157 size_t sz;
158
159 sz = sizeof(*evsel->counts) +
160 (perf_evsel__nr_cpus(evsel) * sizeof(struct perf_counts_values));
161
162 addr = zalloc(sz);
163 if (!addr)
164 return -ENOMEM;
165
166 evsel->prev_raw_counts = addr;
167
168 return 0;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800169}
170
Stephane Eranian13370a92013-01-29 12:47:44 +0100171static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800172{
Stephane Eranian13370a92013-01-29 12:47:44 +0100173 free(evsel->prev_raw_counts);
174 evsel->prev_raw_counts = NULL;
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800175}
176
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -0300177static void perf_evlist__free_stats(struct perf_evlist *evlist)
178{
179 struct perf_evsel *evsel;
180
181 list_for_each_entry(evsel, &evlist->entries, node) {
182 perf_evsel__free_stat_priv(evsel);
183 perf_evsel__free_counts(evsel);
184 perf_evsel__free_prev_raw_counts(evsel);
185 }
186}
187
188static int perf_evlist__alloc_stats(struct perf_evlist *evlist, bool alloc_raw)
189{
190 struct perf_evsel *evsel;
191
192 list_for_each_entry(evsel, &evlist->entries, node) {
193 if (perf_evsel__alloc_stat_priv(evsel) < 0 ||
194 perf_evsel__alloc_counts(evsel, perf_evsel__nr_cpus(evsel)) < 0 ||
195 (alloc_raw && perf_evsel__alloc_prev_raw_counts(evsel) < 0))
196 goto out_free;
197 }
198
199 return 0;
200
201out_free:
202 perf_evlist__free_stats(evlist);
203 return -1;
204}
205
Robert Richter666e6d42012-04-05 18:26:27 +0200206static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
207static struct stats runtime_cycles_stats[MAX_NR_CPUS];
208static struct stats runtime_stalled_cycles_front_stats[MAX_NR_CPUS];
209static struct stats runtime_stalled_cycles_back_stats[MAX_NR_CPUS];
210static struct stats runtime_branches_stats[MAX_NR_CPUS];
211static struct stats runtime_cacherefs_stats[MAX_NR_CPUS];
212static struct stats runtime_l1_dcache_stats[MAX_NR_CPUS];
213static struct stats runtime_l1_icache_stats[MAX_NR_CPUS];
214static struct stats runtime_ll_cache_stats[MAX_NR_CPUS];
215static struct stats runtime_itlb_cache_stats[MAX_NR_CPUS];
216static struct stats runtime_dtlb_cache_stats[MAX_NR_CPUS];
217static struct stats walltime_nsecs_stats;
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200218
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -0300219static void perf_stat__reset_stats(struct perf_evlist *evlist)
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500220{
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -0300221 struct perf_evsel *evsel;
222
223 list_for_each_entry(evsel, &evlist->entries, node) {
224 perf_evsel__reset_stat_priv(evsel);
225 perf_evsel__reset_counts(evsel, perf_evsel__nr_cpus(evsel));
226 }
227
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500228 memset(runtime_nsecs_stats, 0, sizeof(runtime_nsecs_stats));
229 memset(runtime_cycles_stats, 0, sizeof(runtime_cycles_stats));
230 memset(runtime_stalled_cycles_front_stats, 0, sizeof(runtime_stalled_cycles_front_stats));
231 memset(runtime_stalled_cycles_back_stats, 0, sizeof(runtime_stalled_cycles_back_stats));
232 memset(runtime_branches_stats, 0, sizeof(runtime_branches_stats));
233 memset(runtime_cacherefs_stats, 0, sizeof(runtime_cacherefs_stats));
234 memset(runtime_l1_dcache_stats, 0, sizeof(runtime_l1_dcache_stats));
235 memset(runtime_l1_icache_stats, 0, sizeof(runtime_l1_icache_stats));
236 memset(runtime_ll_cache_stats, 0, sizeof(runtime_ll_cache_stats));
237 memset(runtime_itlb_cache_stats, 0, sizeof(runtime_itlb_cache_stats));
238 memset(runtime_dtlb_cache_stats, 0, sizeof(runtime_dtlb_cache_stats));
239 memset(&walltime_nsecs_stats, 0, sizeof(walltime_nsecs_stats));
240}
241
Jiri Olsacac21422012-11-12 18:34:00 +0100242static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200243{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200244 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200245
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200246 if (scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200247 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
248 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200249
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200250 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300251
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300252 if (perf_target__has_cpu(&target))
253 return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200254
Arnaldo Carvalho de Melo07ac0022012-11-13 17:27:28 -0300255 if (!perf_target__has_task(&target) &&
Namhyung Kim823254e2012-11-29 15:38:30 +0900256 perf_evsel__is_group_leader(evsel)) {
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200257 attr->disabled = 1;
Andi Kleen41191682013-08-02 17:41:11 -0700258 if (!initial_delay)
259 attr->enable_on_exec = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200260 }
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300261
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300262 return perf_evsel__open_per_thread(evsel, evsel_list->threads);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200263}
264
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200265/*
266 * Does the counter have nsecs as a unit?
267 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200268static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200269{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200270 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
271 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200272 return 1;
273
274 return 0;
275}
276
277/*
Ingo Molnardcd99362011-04-27 04:36:37 +0200278 * Update various tracking values we maintain to print
279 * more semantic information such as miss/hit ratios,
280 * instruction rates, etc:
281 */
282static void update_shadow_stats(struct perf_evsel *counter, u64 *count)
283{
284 if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK))
285 update_stats(&runtime_nsecs_stats[0], count[0]);
286 else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
287 update_stats(&runtime_cycles_stats[0], count[0]);
Ingo Molnard3d1e862011-04-29 13:49:08 +0200288 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
289 update_stats(&runtime_stalled_cycles_front_stats[0], count[0]);
Ingo Molnar129c04c2011-04-29 14:41:28 +0200290 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
Ingo Molnard3d1e862011-04-29 13:49:08 +0200291 update_stats(&runtime_stalled_cycles_back_stats[0], count[0]);
Ingo Molnardcd99362011-04-27 04:36:37 +0200292 else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
293 update_stats(&runtime_branches_stats[0], count[0]);
294 else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
295 update_stats(&runtime_cacherefs_stats[0], count[0]);
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200296 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
297 update_stats(&runtime_l1_dcache_stats[0], count[0]);
Ingo Molnarc3305252011-05-19 14:01:42 +0200298 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
299 update_stats(&runtime_l1_icache_stats[0], count[0]);
300 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
301 update_stats(&runtime_ll_cache_stats[0], count[0]);
302 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
303 update_stats(&runtime_dtlb_cache_stats[0], count[0]);
304 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
305 update_stats(&runtime_itlb_cache_stats[0], count[0]);
Ingo Molnardcd99362011-04-27 04:36:37 +0200306}
307
308/*
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200309 * Read out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200310 * aggregate counts across CPUs in system-wide mode
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200311 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200312static int read_counter_aggr(struct perf_evsel *counter)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200313{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200314 struct perf_stat *ps = counter->priv;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200315 u64 *count = counter->counts->aggr.values;
316 int i;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200317
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800318 if (__perf_evsel__read(counter, perf_evsel__nr_cpus(counter),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900319 thread_map__nr(evsel_list->threads), scale) < 0)
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200320 return -1;
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200321
322 for (i = 0; i < 3; i++)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200323 update_stats(&ps->res_stats[i], count[i]);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200324
325 if (verbose) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200326 fprintf(output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300327 perf_evsel__name(counter), count[0], count[1], count[2]);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200328 }
329
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200330 /*
331 * Save the full runtime - to allow normalization during printout:
332 */
Ingo Molnardcd99362011-04-27 04:36:37 +0200333 update_shadow_stats(counter, count);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200334
335 return 0;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200336}
337
338/*
339 * Read out the results of a single counter:
340 * do not aggregate counts across CPUs in system-wide mode
341 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200342static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200343{
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200344 u64 *count;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200345 int cpu;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200346
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800347 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200348 if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0)
349 return -1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200350
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200351 count = counter->counts->cpu[cpu].values;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200352
Ingo Molnardcd99362011-04-27 04:36:37 +0200353 update_shadow_stats(counter, count);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200354 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200355
356 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200357}
358
Stephane Eranian13370a92013-01-29 12:47:44 +0100359static void print_interval(void)
360{
361 static int num_print_interval;
362 struct perf_evsel *counter;
363 struct perf_stat *ps;
364 struct timespec ts, rs;
365 char prefix[64];
366
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100367 if (aggr_mode == AGGR_GLOBAL) {
Stephane Eranian13370a92013-01-29 12:47:44 +0100368 list_for_each_entry(counter, &evsel_list->entries, node) {
369 ps = counter->priv;
370 memset(ps->res_stats, 0, sizeof(ps->res_stats));
371 read_counter_aggr(counter);
372 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100373 } else {
374 list_for_each_entry(counter, &evsel_list->entries, node) {
375 ps = counter->priv;
376 memset(ps->res_stats, 0, sizeof(ps->res_stats));
377 read_counter(counter);
378 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100379 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100380
Stephane Eranian13370a92013-01-29 12:47:44 +0100381 clock_gettime(CLOCK_MONOTONIC, &ts);
382 diff_timespec(&rs, &ts, &ref_time);
383 sprintf(prefix, "%6lu.%09lu%s", rs.tv_sec, rs.tv_nsec, csv_sep);
384
385 if (num_print_interval == 0 && !csv_output) {
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100386 switch (aggr_mode) {
387 case AGGR_SOCKET:
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100388 fprintf(output, "# time socket cpus counts events\n");
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100389 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +0100390 case AGGR_CORE:
391 fprintf(output, "# time core cpus counts events\n");
392 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100393 case AGGR_NONE:
Stephane Eranian13370a92013-01-29 12:47:44 +0100394 fprintf(output, "# time CPU counts events\n");
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100395 break;
396 case AGGR_GLOBAL:
397 default:
Stephane Eranian13370a92013-01-29 12:47:44 +0100398 fprintf(output, "# time counts events\n");
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100399 }
Stephane Eranian13370a92013-01-29 12:47:44 +0100400 }
401
402 if (++num_print_interval == 25)
403 num_print_interval = 0;
404
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100405 switch (aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100406 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100407 case AGGR_SOCKET:
408 print_aggr(prefix);
409 break;
410 case AGGR_NONE:
Stephane Eranian13370a92013-01-29 12:47:44 +0100411 list_for_each_entry(counter, &evsel_list->entries, node)
412 print_counter(counter, prefix);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100413 break;
414 case AGGR_GLOBAL:
415 default:
Stephane Eranian13370a92013-01-29 12:47:44 +0100416 list_for_each_entry(counter, &evsel_list->entries, node)
417 print_counter_aggr(counter, prefix);
418 }
Andi Kleen2bbf03f2013-08-02 17:41:12 -0700419
420 fflush(output);
Stephane Eranian13370a92013-01-29 12:47:44 +0100421}
422
Andi Kleen41191682013-08-02 17:41:11 -0700423static void handle_initial_delay(void)
424{
425 struct perf_evsel *counter;
426
427 if (initial_delay) {
428 const int ncpus = cpu_map__nr(evsel_list->cpus),
429 nthreads = thread_map__nr(evsel_list->threads);
430
431 usleep(initial_delay * 1000);
432 list_for_each_entry(counter, &evsel_list->entries, node)
433 perf_evsel__enable(counter, ncpus, nthreads);
434 }
435}
436
Namhyung Kimacf28922013-03-11 16:43:18 +0900437static int __run_perf_stat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200438{
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300439 char msg[512];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200440 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100441 struct perf_evsel *counter;
Stephane Eranian13370a92013-01-29 12:47:44 +0100442 struct timespec ts;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200443 int status = 0;
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300444 const bool forks = (argc > 0);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200445
Stephane Eranian13370a92013-01-29 12:47:44 +0100446 if (interval) {
447 ts.tv_sec = interval / 1000;
448 ts.tv_nsec = (interval % 1000) * 1000000;
449 } else {
450 ts.tv_sec = 1;
451 ts.tv_nsec = 0;
452 }
453
Liming Wang60666c62009-12-31 16:05:50 +0800454 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900455 if (perf_evlist__prepare_workload(evsel_list, &target, argv,
456 false, false) < 0) {
457 perror("failed to prepare workload");
458 return -1;
Liming Wang60666c62009-12-31 16:05:50 +0800459 }
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000460 }
461
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200462 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300463 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200464
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200465 list_for_each_entry(counter, &evsel_list->entries, node) {
Jiri Olsacac21422012-11-12 18:34:00 +0100466 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600467 /*
468 * PPC returns ENXIO for HW counters until 2.6.37
469 * (behavior changed with commit b0a873e).
470 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100471 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600472 errno == ENOENT || errno == EOPNOTSUPP ||
473 errno == ENXIO) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600474 if (verbose)
475 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300476 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600477 counter->supported = false;
Ingo Molnarede70292011-04-28 08:48:42 +0200478 continue;
David Ahernc63ca0c2011-04-29 16:04:15 -0600479 }
Ingo Molnarede70292011-04-28 08:48:42 +0200480
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -0300481 perf_evsel__open_strerror(counter, &target,
482 errno, msg, sizeof(msg));
483 ui__error("%s\n", msg);
484
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200485 if (child_pid != -1)
486 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600487
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200488 return -1;
489 }
David Ahern2cee77c2011-05-30 08:55:59 -0600490 counter->supported = true;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300491 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200492
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300493 if (perf_evlist__apply_filters(evsel_list)) {
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100494 error("failed to set filter with %d (%s)\n", errno,
495 strerror(errno));
496 return -1;
497 }
498
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200499 /*
500 * Enable counters and exec the command:
501 */
502 t0 = rdclock();
Stephane Eranian13370a92013-01-29 12:47:44 +0100503 clock_gettime(CLOCK_MONOTONIC, &ref_time);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200504
Liming Wang60666c62009-12-31 16:05:50 +0800505 if (forks) {
Namhyung Kimacf28922013-03-11 16:43:18 +0900506 perf_evlist__start_workload(evsel_list);
Andi Kleen41191682013-08-02 17:41:11 -0700507 handle_initial_delay();
Namhyung Kimacf28922013-03-11 16:43:18 +0900508
Stephane Eranian13370a92013-01-29 12:47:44 +0100509 if (interval) {
510 while (!waitpid(child_pid, &status, WNOHANG)) {
511 nanosleep(&ts, NULL);
512 print_interval();
513 }
514 }
Liming Wang60666c62009-12-31 16:05:50 +0800515 wait(&status);
Andi Kleen33e49ea2011-09-15 14:31:40 -0700516 if (WIFSIGNALED(status))
517 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800518 } else {
Andi Kleen41191682013-08-02 17:41:11 -0700519 handle_initial_delay();
Stephane Eranian13370a92013-01-29 12:47:44 +0100520 while (!done) {
521 nanosleep(&ts, NULL);
522 if (interval)
523 print_interval();
524 }
Liming Wang60666c62009-12-31 16:05:50 +0800525 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200526
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200527 t1 = rdclock();
528
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200529 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200530
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100531 if (aggr_mode == AGGR_GLOBAL) {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200532 list_for_each_entry(counter, &evsel_list->entries, node) {
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200533 read_counter_aggr(counter);
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800534 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900535 thread_map__nr(evsel_list->threads));
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200536 }
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100537 } else {
538 list_for_each_entry(counter, &evsel_list->entries, node) {
539 read_counter(counter);
540 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter), 1);
541 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200542 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200543
Ingo Molnar42202dd2009-06-13 14:57:28 +0200544 return WEXITSTATUS(status);
545}
546
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200547static int run_perf_stat(int argc __maybe_unused, const char **argv)
548{
549 int ret;
550
551 if (pre_cmd) {
552 ret = system(pre_cmd);
553 if (ret)
554 return ret;
555 }
556
557 if (sync_run)
558 sync();
559
560 ret = __run_perf_stat(argc, argv);
561 if (ret)
562 return ret;
563
564 if (post_cmd) {
565 ret = system(post_cmd);
566 if (ret)
567 return ret;
568 }
569
570 return ret;
571}
572
Ingo Molnarf99844c2011-04-27 05:35:39 +0200573static void print_noise_pct(double total, double avg)
574{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800575 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200576
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700577 if (csv_output)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200578 fprintf(output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600579 else if (pct)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200580 fprintf(output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200581}
582
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200583static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200584{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200585 struct perf_stat *ps;
586
Peter Zijlstra849abde2009-09-04 18:23:38 +0200587 if (run_count == 1)
588 return;
589
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200590 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200591 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200592}
593
Stephane Eranian12c08a92013-02-14 13:57:29 +0100594static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200595{
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100596 switch (aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +0100597 case AGGR_CORE:
598 fprintf(output, "S%d-C%*d%s%*d%s",
599 cpu_map__id_to_socket(id),
600 csv_output ? 0 : -8,
601 cpu_map__id_to_cpu(id),
602 csv_sep,
603 csv_output ? 0 : 4,
604 nr,
605 csv_sep);
606 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100607 case AGGR_SOCKET:
608 fprintf(output, "S%*d%s%*d%s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100609 csv_output ? 0 : -5,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100610 id,
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100611 csv_sep,
612 csv_output ? 0 : 4,
613 nr,
614 csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100615 break;
616 case AGGR_NONE:
617 fprintf(output, "CPU%*d%s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200618 csv_output ? 0 : -4,
Stephane Eranian12c08a92013-02-14 13:57:29 +0100619 perf_evsel__cpus(evsel)->map[id], csv_sep);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100620 break;
621 case AGGR_GLOBAL:
622 default:
623 break;
624 }
625}
Stephane Eraniand7470b62010-12-01 18:49:05 +0200626
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100627static void nsec_printout(int cpu, int nr, struct perf_evsel *evsel, double avg)
628{
629 double msecs = avg / 1e6;
630 const char *fmt = csv_output ? "%.6f%s%s" : "%18.6f%s%-25s";
631
632 aggr_printout(evsel, cpu, nr);
633
634 fprintf(output, fmt, msecs, csv_sep, perf_evsel__name(evsel));
Stephane Eraniand7470b62010-12-01 18:49:05 +0200635
Stephane Eranian023695d2011-02-14 11:20:01 +0200636 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200637 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200638
Stephane Eranian13370a92013-01-29 12:47:44 +0100639 if (csv_output || interval)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200640 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200641
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200642 if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Stephane Eranian4aa90152011-08-15 22:22:33 +0200643 fprintf(output, " # %8.3f CPUs utilized ",
644 avg / avg_stats(&walltime_nsecs_stats));
Namhyung Kim9dac6a22012-02-06 16:44:45 +0900645 else
646 fprintf(output, " ");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200647}
648
Namhyung Kim15e63922011-12-28 00:35:49 +0900649/* used for get_ratio_color() */
650enum grc_type {
651 GRC_STALLED_CYCLES_FE,
652 GRC_STALLED_CYCLES_BE,
653 GRC_CACHE_MISSES,
654 GRC_MAX_NR
655};
656
657static const char *get_ratio_color(enum grc_type type, double ratio)
658{
659 static const double grc_table[GRC_MAX_NR][3] = {
660 [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
661 [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
662 [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
663 };
664 const char *color = PERF_COLOR_NORMAL;
665
666 if (ratio > grc_table[type][0])
667 color = PERF_COLOR_RED;
668 else if (ratio > grc_table[type][1])
669 color = PERF_COLOR_MAGENTA;
670 else if (ratio > grc_table[type][2])
671 color = PERF_COLOR_YELLOW;
672
673 return color;
674}
675
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300676static void print_stalled_cycles_frontend(int cpu,
677 struct perf_evsel *evsel
678 __maybe_unused, double avg)
Ingo Molnard3d1e862011-04-29 13:49:08 +0200679{
680 double total, ratio = 0.0;
681 const char *color;
682
683 total = avg_stats(&runtime_cycles_stats[cpu]);
684
685 if (total)
686 ratio = avg / total * 100.0;
687
Namhyung Kim15e63922011-12-28 00:35:49 +0900688 color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
Ingo Molnard3d1e862011-04-29 13:49:08 +0200689
Stephane Eranian4aa90152011-08-15 22:22:33 +0200690 fprintf(output, " # ");
691 color_fprintf(output, color, "%6.2f%%", ratio);
692 fprintf(output, " frontend cycles idle ");
Ingo Molnard3d1e862011-04-29 13:49:08 +0200693}
694
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300695static void print_stalled_cycles_backend(int cpu,
696 struct perf_evsel *evsel
697 __maybe_unused, double avg)
Ingo Molnara5d243d2011-04-27 05:39:24 +0200698{
699 double total, ratio = 0.0;
700 const char *color;
701
702 total = avg_stats(&runtime_cycles_stats[cpu]);
703
704 if (total)
705 ratio = avg / total * 100.0;
706
Namhyung Kim15e63922011-12-28 00:35:49 +0900707 color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
Ingo Molnara5d243d2011-04-27 05:39:24 +0200708
Stephane Eranian4aa90152011-08-15 22:22:33 +0200709 fprintf(output, " # ");
710 color_fprintf(output, color, "%6.2f%%", ratio);
711 fprintf(output, " backend cycles idle ");
Ingo Molnara5d243d2011-04-27 05:39:24 +0200712}
713
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300714static void print_branch_misses(int cpu,
715 struct perf_evsel *evsel __maybe_unused,
716 double avg)
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200717{
718 double total, ratio = 0.0;
719 const char *color;
720
721 total = avg_stats(&runtime_branches_stats[cpu]);
722
723 if (total)
724 ratio = avg / total * 100.0;
725
Namhyung Kim15e63922011-12-28 00:35:49 +0900726 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200727
Stephane Eranian4aa90152011-08-15 22:22:33 +0200728 fprintf(output, " # ");
729 color_fprintf(output, color, "%6.2f%%", ratio);
730 fprintf(output, " of all branches ");
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200731}
732
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300733static void print_l1_dcache_misses(int cpu,
734 struct perf_evsel *evsel __maybe_unused,
735 double avg)
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200736{
737 double total, ratio = 0.0;
738 const char *color;
739
740 total = avg_stats(&runtime_l1_dcache_stats[cpu]);
741
742 if (total)
743 ratio = avg / total * 100.0;
744
Namhyung Kim15e63922011-12-28 00:35:49 +0900745 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200746
Stephane Eranian4aa90152011-08-15 22:22:33 +0200747 fprintf(output, " # ");
748 color_fprintf(output, color, "%6.2f%%", ratio);
749 fprintf(output, " of all L1-dcache hits ");
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200750}
751
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300752static void print_l1_icache_misses(int cpu,
753 struct perf_evsel *evsel __maybe_unused,
754 double avg)
Ingo Molnarc3305252011-05-19 14:01:42 +0200755{
756 double total, ratio = 0.0;
757 const char *color;
758
759 total = avg_stats(&runtime_l1_icache_stats[cpu]);
760
761 if (total)
762 ratio = avg / total * 100.0;
763
Namhyung Kim15e63922011-12-28 00:35:49 +0900764 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc3305252011-05-19 14:01:42 +0200765
Stephane Eranian4aa90152011-08-15 22:22:33 +0200766 fprintf(output, " # ");
767 color_fprintf(output, color, "%6.2f%%", ratio);
768 fprintf(output, " of all L1-icache hits ");
Ingo Molnarc3305252011-05-19 14:01:42 +0200769}
770
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300771static void print_dtlb_cache_misses(int cpu,
772 struct perf_evsel *evsel __maybe_unused,
773 double avg)
Ingo Molnarc3305252011-05-19 14:01:42 +0200774{
775 double total, ratio = 0.0;
776 const char *color;
777
778 total = avg_stats(&runtime_dtlb_cache_stats[cpu]);
779
780 if (total)
781 ratio = avg / total * 100.0;
782
Namhyung Kim15e63922011-12-28 00:35:49 +0900783 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc3305252011-05-19 14:01:42 +0200784
Stephane Eranian4aa90152011-08-15 22:22:33 +0200785 fprintf(output, " # ");
786 color_fprintf(output, color, "%6.2f%%", ratio);
787 fprintf(output, " of all dTLB cache hits ");
Ingo Molnarc3305252011-05-19 14:01:42 +0200788}
789
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300790static void print_itlb_cache_misses(int cpu,
791 struct perf_evsel *evsel __maybe_unused,
792 double avg)
Ingo Molnarc3305252011-05-19 14:01:42 +0200793{
794 double total, ratio = 0.0;
795 const char *color;
796
797 total = avg_stats(&runtime_itlb_cache_stats[cpu]);
798
799 if (total)
800 ratio = avg / total * 100.0;
801
Namhyung Kim15e63922011-12-28 00:35:49 +0900802 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc3305252011-05-19 14:01:42 +0200803
Stephane Eranian4aa90152011-08-15 22:22:33 +0200804 fprintf(output, " # ");
805 color_fprintf(output, color, "%6.2f%%", ratio);
806 fprintf(output, " of all iTLB cache hits ");
Ingo Molnarc3305252011-05-19 14:01:42 +0200807}
808
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300809static void print_ll_cache_misses(int cpu,
810 struct perf_evsel *evsel __maybe_unused,
811 double avg)
Ingo Molnarc3305252011-05-19 14:01:42 +0200812{
813 double total, ratio = 0.0;
814 const char *color;
815
816 total = avg_stats(&runtime_ll_cache_stats[cpu]);
817
818 if (total)
819 ratio = avg / total * 100.0;
820
Namhyung Kim15e63922011-12-28 00:35:49 +0900821 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc3305252011-05-19 14:01:42 +0200822
Stephane Eranian4aa90152011-08-15 22:22:33 +0200823 fprintf(output, " # ");
824 color_fprintf(output, color, "%6.2f%%", ratio);
825 fprintf(output, " of all LL-cache hits ");
Ingo Molnarc3305252011-05-19 14:01:42 +0200826}
827
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100828static void abs_printout(int cpu, int nr, struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200829{
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200830 double total, ratio = 0.0;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200831 const char *fmt;
832
833 if (csv_output)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100834 fmt = "%.0f%s%s";
Stephane Eraniand7470b62010-12-01 18:49:05 +0200835 else if (big_num)
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100836 fmt = "%'18.0f%s%-25s";
Stephane Eraniand7470b62010-12-01 18:49:05 +0200837 else
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100838 fmt = "%18.0f%s%-25s";
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200839
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100840 aggr_printout(evsel, cpu, nr);
841
842 if (aggr_mode == AGGR_GLOBAL)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200843 cpu = 0;
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200844
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100845 fprintf(output, fmt, avg, csv_sep, perf_evsel__name(evsel));
Stephane Eraniand7470b62010-12-01 18:49:05 +0200846
Stephane Eranian023695d2011-02-14 11:20:01 +0200847 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200848 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200849
Stephane Eranian13370a92013-01-29 12:47:44 +0100850 if (csv_output || interval)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200851 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200852
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200853 if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200854 total = avg_stats(&runtime_cycles_stats[cpu]);
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200855 if (total)
856 ratio = avg / total;
857
Stephane Eranian4aa90152011-08-15 22:22:33 +0200858 fprintf(output, " # %5.2f insns per cycle ", ratio);
Ingo Molnar481f9882011-04-27 04:34:16 +0200859
Ingo Molnard3d1e862011-04-29 13:49:08 +0200860 total = avg_stats(&runtime_stalled_cycles_front_stats[cpu]);
861 total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[cpu]));
Ingo Molnar481f9882011-04-27 04:34:16 +0200862
863 if (total && avg) {
864 ratio = total / avg;
Stephane Eranian4aa90152011-08-15 22:22:33 +0200865 fprintf(output, "\n # %5.2f stalled cycles per insn", ratio);
Ingo Molnar481f9882011-04-27 04:34:16 +0200866 }
867
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200868 } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200869 runtime_branches_stats[cpu].n != 0) {
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200870 print_branch_misses(cpu, evsel, avg);
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200871 } else if (
872 evsel->attr.type == PERF_TYPE_HW_CACHE &&
873 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
874 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
875 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
Ingo Molnarc6264de2011-04-27 13:50:47 +0200876 runtime_l1_dcache_stats[cpu].n != 0) {
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200877 print_l1_dcache_misses(cpu, evsel, avg);
Ingo Molnarc3305252011-05-19 14:01:42 +0200878 } else if (
879 evsel->attr.type == PERF_TYPE_HW_CACHE &&
880 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
881 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
882 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
883 runtime_l1_icache_stats[cpu].n != 0) {
884 print_l1_icache_misses(cpu, evsel, avg);
885 } else if (
886 evsel->attr.type == PERF_TYPE_HW_CACHE &&
887 evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
888 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
889 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
890 runtime_dtlb_cache_stats[cpu].n != 0) {
891 print_dtlb_cache_misses(cpu, evsel, avg);
892 } else if (
893 evsel->attr.type == PERF_TYPE_HW_CACHE &&
894 evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
895 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
896 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
897 runtime_itlb_cache_stats[cpu].n != 0) {
898 print_itlb_cache_misses(cpu, evsel, avg);
899 } else if (
900 evsel->attr.type == PERF_TYPE_HW_CACHE &&
901 evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
902 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
903 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
904 runtime_ll_cache_stats[cpu].n != 0) {
905 print_ll_cache_misses(cpu, evsel, avg);
Ingo Molnard58f4c82011-04-27 03:42:18 +0200906 } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES) &&
907 runtime_cacherefs_stats[cpu].n != 0) {
908 total = avg_stats(&runtime_cacherefs_stats[cpu]);
909
910 if (total)
911 ratio = avg * 100 / total;
912
Stephane Eranian4aa90152011-08-15 22:22:33 +0200913 fprintf(output, " # %8.3f %% of all cache refs ", ratio);
Ingo Molnard58f4c82011-04-27 03:42:18 +0200914
Ingo Molnard3d1e862011-04-29 13:49:08 +0200915 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
916 print_stalled_cycles_frontend(cpu, evsel, avg);
Ingo Molnar129c04c2011-04-29 14:41:28 +0200917 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
Ingo Molnard3d1e862011-04-29 13:49:08 +0200918 print_stalled_cycles_backend(cpu, evsel, avg);
Ingo Molnar481f9882011-04-27 04:34:16 +0200919 } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
920 total = avg_stats(&runtime_nsecs_stats[cpu]);
921
922 if (total)
923 ratio = 1.0 * avg / total;
924
Stephane Eranian4aa90152011-08-15 22:22:33 +0200925 fprintf(output, " # %8.3f GHz ", ratio);
Ingo Molnar481f9882011-04-27 04:34:16 +0200926 } else if (runtime_nsecs_stats[cpu].n != 0) {
Namhyung Kim5fde2522012-02-06 16:44:44 +0900927 char unit = 'M';
928
Ingo Molnar481f9882011-04-27 04:34:16 +0200929 total = avg_stats(&runtime_nsecs_stats[cpu]);
930
931 if (total)
932 ratio = 1000.0 * avg / total;
Namhyung Kim5fde2522012-02-06 16:44:44 +0900933 if (ratio < 0.001) {
934 ratio *= 1000;
935 unit = 'K';
936 }
Ingo Molnar481f9882011-04-27 04:34:16 +0200937
Namhyung Kim5fde2522012-02-06 16:44:44 +0900938 fprintf(output, " # %8.3f %c/sec ", ratio, unit);
Ingo Molnara5d243d2011-04-27 05:39:24 +0200939 } else {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200940 fprintf(output, " ");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200941 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200942}
943
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100944static void print_aggr(char *prefix)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100945{
946 struct perf_evsel *counter;
Stephane Eranian582ec0822013-07-05 19:06:45 +0200947 int cpu, cpu2, s, s2, id, nr;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100948 u64 ena, run, val;
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100949
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100950 if (!(aggr_map || aggr_get_id))
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100951 return;
952
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100953 for (s = 0; s < aggr_map->nr; s++) {
954 id = aggr_map->map[s];
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100955 list_for_each_entry(counter, &evsel_list->entries, node) {
956 val = ena = run = 0;
957 nr = 0;
958 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Stephane Eranian582ec0822013-07-05 19:06:45 +0200959 cpu2 = perf_evsel__cpus(counter)->map[cpu];
960 s2 = aggr_get_id(evsel_list->cpus, cpu2);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100961 if (s2 != id)
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100962 continue;
963 val += counter->counts->cpu[cpu].val;
964 ena += counter->counts->cpu[cpu].ena;
965 run += counter->counts->cpu[cpu].run;
966 nr++;
967 }
968 if (prefix)
969 fprintf(output, "%s", prefix);
970
971 if (run == 0 || ena == 0) {
Stephane Eranian582ec0822013-07-05 19:06:45 +0200972 aggr_printout(counter, id, nr);
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100973
974 fprintf(output, "%*s%s%*s",
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100975 csv_output ? 0 : 18,
976 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
977 csv_sep,
978 csv_output ? 0 : -24,
979 perf_evsel__name(counter));
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100980
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100981 if (counter->cgrp)
982 fprintf(output, "%s%s",
983 csv_sep, counter->cgrp->name);
984
985 fputc('\n', output);
986 continue;
987 }
988
989 if (nsec_counter(counter))
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100990 nsec_printout(id, nr, counter, val);
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100991 else
Stephane Eranian86ee6e12013-02-14 13:57:27 +0100992 abs_printout(id, nr, counter, val);
Stephane Eraniand7e7a452013-02-06 15:46:02 +0100993
994 if (!csv_output) {
995 print_noise(counter, 1.0);
996
997 if (run != ena)
998 fprintf(output, " (%.2f%%)",
999 100.0 * run / ena);
1000 }
1001 fputc('\n', output);
1002 }
1003 }
1004}
1005
Ingo Molnar42202dd2009-06-13 14:57:28 +02001006/*
1007 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001008 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +02001009 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001010static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001011{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001012 struct perf_stat *ps = counter->priv;
1013 double avg = avg_stats(&ps->res_stats[0]);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001014 int scaled = counter->counts->scaled;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001015
Stephane Eranian13370a92013-01-29 12:47:44 +01001016 if (prefix)
1017 fprintf(output, "%s", prefix);
1018
Ingo Molnar42202dd2009-06-13 14:57:28 +02001019 if (scaled == -1) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001020 fprintf(output, "%*s%s%*s",
Stephane Eraniand7470b62010-12-01 18:49:05 +02001021 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -06001022 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian023695d2011-02-14 11:20:01 +02001023 csv_sep,
1024 csv_output ? 0 : -24,
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001025 perf_evsel__name(counter));
Stephane Eranian023695d2011-02-14 11:20:01 +02001026
1027 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001028 fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +02001029
Stephane Eranian4aa90152011-08-15 22:22:33 +02001030 fputc('\n', output);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001031 return;
1032 }
1033
1034 if (nsec_counter(counter))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001035 nsec_printout(-1, 0, counter, avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001036 else
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001037 abs_printout(-1, 0, counter, avg);
Peter Zijlstra849abde2009-09-04 18:23:38 +02001038
Zhengyu He3ae9a34d2011-06-23 13:45:42 -07001039 print_noise(counter, avg);
1040
Stephane Eraniand7470b62010-12-01 18:49:05 +02001041 if (csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001042 fputc('\n', output);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001043 return;
1044 }
1045
Peter Zijlstra506d4bc2009-09-04 15:36:12 +02001046 if (scaled) {
1047 double avg_enabled, avg_running;
1048
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001049 avg_enabled = avg_stats(&ps->res_stats[1]);
1050 avg_running = avg_stats(&ps->res_stats[2]);
Peter Zijlstra506d4bc2009-09-04 15:36:12 +02001051
Stephane Eranian4aa90152011-08-15 22:22:33 +02001052 fprintf(output, " [%5.2f%%]", 100 * avg_running / avg_enabled);
Peter Zijlstra506d4bc2009-09-04 15:36:12 +02001053 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001054 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +02001055}
1056
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001057/*
1058 * Print out the results of a single counter:
1059 * does not use aggregated count in system-wide
1060 */
Stephane Eranian13370a92013-01-29 12:47:44 +01001061static void print_counter(struct perf_evsel *counter, char *prefix)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001062{
1063 u64 ena, run, val;
1064 int cpu;
1065
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001066 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001067 val = counter->counts->cpu[cpu].val;
1068 ena = counter->counts->cpu[cpu].ena;
1069 run = counter->counts->cpu[cpu].run;
Stephane Eranian13370a92013-01-29 12:47:44 +01001070
1071 if (prefix)
1072 fprintf(output, "%s", prefix);
1073
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001074 if (run == 0 || ena == 0) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001075 fprintf(output, "CPU%*d%s%*s%s%*s",
Stephane Eraniand7470b62010-12-01 18:49:05 +02001076 csv_output ? 0 : -4,
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001077 perf_evsel__cpus(counter)->map[cpu], csv_sep,
Stephane Eraniand7470b62010-12-01 18:49:05 +02001078 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -06001079 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
1080 csv_sep,
Stephane Eranian023695d2011-02-14 11:20:01 +02001081 csv_output ? 0 : -24,
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001082 perf_evsel__name(counter));
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001083
Stephane Eranian023695d2011-02-14 11:20:01 +02001084 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001085 fprintf(output, "%s%s",
1086 csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +02001087
Stephane Eranian4aa90152011-08-15 22:22:33 +02001088 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001089 continue;
1090 }
1091
1092 if (nsec_counter(counter))
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001093 nsec_printout(cpu, 0, counter, val);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001094 else
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001095 abs_printout(cpu, 0, counter, val);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001096
Stephane Eraniand7470b62010-12-01 18:49:05 +02001097 if (!csv_output) {
1098 print_noise(counter, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001099
Ingo Molnarc6264de2011-04-27 13:50:47 +02001100 if (run != ena)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001101 fprintf(output, " (%.2f%%)",
1102 100.0 * run / ena);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001103 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001104 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001105 }
1106}
1107
Ingo Molnar42202dd2009-06-13 14:57:28 +02001108static void print_stat(int argc, const char **argv)
1109{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001110 struct perf_evsel *counter;
1111 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001112
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001113 fflush(stdout);
1114
Stephane Eraniand7470b62010-12-01 18:49:05 +02001115 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001116 fprintf(output, "\n");
1117 fprintf(output, " Performance counter stats for ");
Namhyung Kimaa22dd42012-05-16 18:45:47 +09001118 if (!perf_target__has_task(&target)) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001119 fprintf(output, "\'%s", argv[0]);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001120 for (i = 1; i < argc; i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001121 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +09001122 } else if (target.pid)
1123 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001124 else
Namhyung Kim20f946b2012-04-26 14:15:16 +09001125 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +02001126
Stephane Eranian4aa90152011-08-15 22:22:33 +02001127 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001128 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001129 fprintf(output, " (%d runs)", run_count);
1130 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +02001131 }
Ingo Molnar2996f5d2009-05-29 09:10:54 +02001132
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001133 switch (aggr_mode) {
Stephane Eranian12c08a92013-02-14 13:57:29 +01001134 case AGGR_CORE:
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001135 case AGGR_SOCKET:
1136 print_aggr(NULL);
1137 break;
1138 case AGGR_GLOBAL:
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001139 list_for_each_entry(counter, &evsel_list->entries, node)
Stephane Eranian13370a92013-01-29 12:47:44 +01001140 print_counter_aggr(counter, NULL);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001141 break;
1142 case AGGR_NONE:
1143 list_for_each_entry(counter, &evsel_list->entries, node)
1144 print_counter(counter, NULL);
1145 break;
1146 default:
1147 break;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001148 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001149
Stephane Eraniand7470b62010-12-01 18:49:05 +02001150 if (!csv_output) {
Ingo Molnarc3305252011-05-19 14:01:42 +02001151 if (!null_run)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001152 fprintf(output, "\n");
1153 fprintf(output, " %17.9f seconds time elapsed",
Stephane Eraniand7470b62010-12-01 18:49:05 +02001154 avg_stats(&walltime_nsecs_stats)/1e9);
1155 if (run_count > 1) {
Stephane Eranian4aa90152011-08-15 22:22:33 +02001156 fprintf(output, " ");
Ingo Molnarf99844c2011-04-27 05:35:39 +02001157 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1158 avg_stats(&walltime_nsecs_stats));
Stephane Eraniand7470b62010-12-01 18:49:05 +02001159 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001160 fprintf(output, "\n\n");
Ingo Molnar566747e2009-06-27 06:24:32 +02001161 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001162}
1163
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001164static volatile int signr = -1;
1165
Ingo Molnar52425192009-05-26 09:17:18 +02001166static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001167{
Stephane Eranian13370a92013-01-29 12:47:44 +01001168 if ((child_pid == -1) || interval)
Liming Wang60666c62009-12-31 16:05:50 +08001169 done = 1;
1170
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001171 signr = signo;
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001172 /*
1173 * render child_pid harmless
1174 * won't send SIGTERM to a random
1175 * process in case of race condition
1176 * and fast PID recycling
1177 */
1178 child_pid = -1;
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001179}
1180
1181static void sig_atexit(void)
1182{
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001183 sigset_t set, oset;
1184
1185 /*
1186 * avoid race condition with SIGCHLD handler
1187 * in skip_signal() which is modifying child_pid
1188 * goal is to avoid send SIGTERM to a random
1189 * process
1190 */
1191 sigemptyset(&set);
1192 sigaddset(&set, SIGCHLD);
1193 sigprocmask(SIG_BLOCK, &set, &oset);
1194
Chris Wilson933da832009-10-04 01:35:01 +01001195 if (child_pid != -1)
1196 kill(child_pid, SIGTERM);
1197
Stephane Eraniand07f0b12013-06-04 17:44:26 +02001198 sigprocmask(SIG_SETMASK, &oset, NULL);
1199
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001200 if (signr == -1)
1201 return;
1202
1203 signal(signr, SIG_DFL);
1204 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001205}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001206
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001207static int stat__set_big_num(const struct option *opt __maybe_unused,
1208 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +02001209{
1210 big_num_opt = unset ? 0 : 1;
1211 return 0;
1212}
1213
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001214static int perf_stat_init_aggr_mode(void)
1215{
1216 switch (aggr_mode) {
1217 case AGGR_SOCKET:
1218 if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) {
1219 perror("cannot build socket map");
1220 return -1;
1221 }
1222 aggr_get_id = cpu_map__get_socket;
1223 break;
Stephane Eranian12c08a92013-02-14 13:57:29 +01001224 case AGGR_CORE:
1225 if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) {
1226 perror("cannot build core map");
1227 return -1;
1228 }
1229 aggr_get_id = cpu_map__get_core;
1230 break;
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001231 case AGGR_NONE:
1232 case AGGR_GLOBAL:
1233 default:
1234 break;
1235 }
1236 return 0;
1237}
1238
1239
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001240/*
1241 * Add default attributes, if there were no attributes specified or
1242 * if -d/--detailed, -d -d or -d -d -d is used:
1243 */
1244static int add_default_attributes(void)
1245{
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001246 struct perf_event_attr default_attrs[] = {
1247
1248 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
1249 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
1250 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
1251 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
1252
1253 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
1254 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
1255 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
1256 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
1257 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
1258 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
1259
1260};
1261
1262/*
1263 * Detailed stats (-d), covering the L1 and last level data caches:
1264 */
1265 struct perf_event_attr detailed_attrs[] = {
1266
1267 { .type = PERF_TYPE_HW_CACHE,
1268 .config =
1269 PERF_COUNT_HW_CACHE_L1D << 0 |
1270 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1271 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1272
1273 { .type = PERF_TYPE_HW_CACHE,
1274 .config =
1275 PERF_COUNT_HW_CACHE_L1D << 0 |
1276 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1277 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1278
1279 { .type = PERF_TYPE_HW_CACHE,
1280 .config =
1281 PERF_COUNT_HW_CACHE_LL << 0 |
1282 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1283 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1284
1285 { .type = PERF_TYPE_HW_CACHE,
1286 .config =
1287 PERF_COUNT_HW_CACHE_LL << 0 |
1288 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1289 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1290};
1291
1292/*
1293 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1294 */
1295 struct perf_event_attr very_detailed_attrs[] = {
1296
1297 { .type = PERF_TYPE_HW_CACHE,
1298 .config =
1299 PERF_COUNT_HW_CACHE_L1I << 0 |
1300 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1301 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1302
1303 { .type = PERF_TYPE_HW_CACHE,
1304 .config =
1305 PERF_COUNT_HW_CACHE_L1I << 0 |
1306 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1307 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1308
1309 { .type = PERF_TYPE_HW_CACHE,
1310 .config =
1311 PERF_COUNT_HW_CACHE_DTLB << 0 |
1312 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1313 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1314
1315 { .type = PERF_TYPE_HW_CACHE,
1316 .config =
1317 PERF_COUNT_HW_CACHE_DTLB << 0 |
1318 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1319 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1320
1321 { .type = PERF_TYPE_HW_CACHE,
1322 .config =
1323 PERF_COUNT_HW_CACHE_ITLB << 0 |
1324 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1325 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1326
1327 { .type = PERF_TYPE_HW_CACHE,
1328 .config =
1329 PERF_COUNT_HW_CACHE_ITLB << 0 |
1330 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1331 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1332
1333};
1334
1335/*
1336 * Very, very detailed stats (-d -d -d), adding prefetch events:
1337 */
1338 struct perf_event_attr very_very_detailed_attrs[] = {
1339
1340 { .type = PERF_TYPE_HW_CACHE,
1341 .config =
1342 PERF_COUNT_HW_CACHE_L1D << 0 |
1343 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1344 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1345
1346 { .type = PERF_TYPE_HW_CACHE,
1347 .config =
1348 PERF_COUNT_HW_CACHE_L1D << 0 |
1349 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1350 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1351};
1352
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001353 /* Set attrs if no event is selected and !null_run: */
1354 if (null_run)
1355 return 0;
1356
1357 if (!evsel_list->nr_entries) {
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001358 if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001359 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001360 }
1361
1362 /* Detailed events get appended to the event list: */
1363
1364 if (detailed_run < 1)
1365 return 0;
1366
1367 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001368 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001369 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001370
1371 if (detailed_run < 2)
1372 return 0;
1373
1374 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001375 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001376 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001377
1378 if (detailed_run < 3)
1379 return 0;
1380
1381 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001382 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001383}
1384
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001385int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02001386{
Peter Zijlstra1f16c572012-10-23 13:40:14 +02001387 bool append_file = false;
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001388 int output_fd = 0;
1389 const char *output_name = NULL;
1390 const struct option options[] = {
1391 OPT_CALLBACK('e', "event", &evsel_list, "event",
1392 "event selector. use 'perf list' to list available events",
1393 parse_events_option),
1394 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1395 "event filter", parse_filter),
1396 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1397 "child tasks do not inherit counters"),
1398 OPT_STRING('p', "pid", &target.pid, "pid",
1399 "stat events on existing process id"),
1400 OPT_STRING('t', "tid", &target.tid, "tid",
1401 "stat events on existing thread id"),
1402 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1403 "system-wide collection from all CPUs"),
1404 OPT_BOOLEAN('g', "group", &group,
1405 "put the counters into a counter group"),
1406 OPT_BOOLEAN('c', "scale", &scale, "scale/normalize counters"),
1407 OPT_INCR('v', "verbose", &verbose,
1408 "be more verbose (show counter open errors, etc)"),
1409 OPT_INTEGER('r', "repeat", &run_count,
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001410 "repeat command and print average + stddev (max: 100, forever: 0)"),
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001411 OPT_BOOLEAN('n', "null", &null_run,
1412 "null run - dont start any counters"),
1413 OPT_INCR('d', "detailed", &detailed_run,
1414 "detailed run - start a lot of events"),
1415 OPT_BOOLEAN('S', "sync", &sync_run,
1416 "call sync() before starting a run"),
1417 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1418 "print large numbers with thousands\' separators",
1419 stat__set_big_num),
1420 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1421 "list of cpus to monitor in system-wide"),
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001422 OPT_SET_UINT('A', "no-aggr", &aggr_mode,
1423 "disable CPU count aggregation", AGGR_NONE),
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001424 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1425 "print counts with custom separator"),
1426 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1427 "monitor event in cgroup name only", parse_cgroups),
1428 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1429 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1430 OPT_INTEGER(0, "log-fd", &output_fd,
1431 "log output to fd, instead of stderr"),
Peter Zijlstra1f16c572012-10-23 13:40:14 +02001432 OPT_STRING(0, "pre", &pre_cmd, "command",
1433 "command to run prior to the measured command"),
1434 OPT_STRING(0, "post", &post_cmd, "command",
1435 "command to run after to the measured command"),
Stephane Eranian13370a92013-01-29 12:47:44 +01001436 OPT_UINTEGER('I', "interval-print", &interval,
1437 "print counts at regular interval in ms (>= 100)"),
Stephane Eraniand4304952013-02-14 13:57:28 +01001438 OPT_SET_UINT(0, "per-socket", &aggr_mode,
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001439 "aggregate counts per processor socket", AGGR_SOCKET),
Stephane Eranian12c08a92013-02-14 13:57:29 +01001440 OPT_SET_UINT(0, "per-core", &aggr_mode,
1441 "aggregate counts per physical processor core", AGGR_CORE),
Andi Kleen41191682013-08-02 17:41:11 -07001442 OPT_UINTEGER('D', "delay", &initial_delay,
1443 "ms to wait before starting measurement after program start"),
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001444 OPT_END()
1445 };
1446 const char * const stat_usage[] = {
1447 "perf stat [<options>] [<command>]",
1448 NULL
1449 };
Arnaldo Carvalho de Melob070a542012-10-01 15:20:58 -03001450 int status = -ENOMEM, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001451 const char *mode;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001452
Stephane Eranian5af52b52010-05-18 15:00:01 +02001453 setlocale(LC_ALL, "");
1454
Namhyung Kim334fe7a2013-03-11 16:43:12 +09001455 evsel_list = perf_evlist__new();
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001456 if (evsel_list == NULL)
1457 return -ENOMEM;
1458
Anton Blancharda0541232009-07-22 23:04:12 +10001459 argc = parse_options(argc, argv, options, stat_usage,
1460 PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001461
Stephane Eranian4aa90152011-08-15 22:22:33 +02001462 output = stderr;
1463 if (output_name && strcmp(output_name, "-"))
1464 output = NULL;
1465
Jim Cromie56f3bae2011-09-07 17:14:00 -06001466 if (output_name && output_fd) {
1467 fprintf(stderr, "cannot use both --output and --log-fd\n");
1468 usage_with_options(stat_usage, options);
1469 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001470
1471 if (output_fd < 0) {
1472 fprintf(stderr, "argument to --log-fd must be a > 0\n");
1473 usage_with_options(stat_usage, options);
1474 }
1475
Stephane Eranian4aa90152011-08-15 22:22:33 +02001476 if (!output) {
1477 struct timespec tm;
1478 mode = append_file ? "a" : "w";
1479
1480 output = fopen(output_name, mode);
1481 if (!output) {
1482 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06001483 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001484 }
1485 clock_gettime(CLOCK_REALTIME, &tm);
1486 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001487 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06001488 mode = append_file ? "a" : "w";
1489 output = fdopen(output_fd, mode);
1490 if (!output) {
1491 perror("Failed opening logfd");
1492 return -errno;
1493 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001494 }
1495
Jim Cromied4ffd042011-09-07 17:14:03 -06001496 if (csv_sep) {
Stephane Eraniand7470b62010-12-01 18:49:05 +02001497 csv_output = true;
Jim Cromied4ffd042011-09-07 17:14:03 -06001498 if (!strcmp(csv_sep, "\\t"))
1499 csv_sep = "\t";
1500 } else
Stephane Eraniand7470b62010-12-01 18:49:05 +02001501 csv_sep = DEFAULT_SEPARATOR;
1502
1503 /*
1504 * let the spreadsheet do the pretty-printing
1505 */
1506 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06001507 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02001508 if (big_num_opt == 1) {
1509 fprintf(stderr, "-B option not supported with -x\n");
1510 usage_with_options(stat_usage, options);
1511 } else /* Nope, so disable big number formatting */
1512 big_num = false;
1513 } else if (big_num_opt == 0) /* User passed --no-big-num */
1514 big_num = false;
1515
Namhyung Kimaa22dd42012-05-16 18:45:47 +09001516 if (!argc && !perf_target__has_task(&target))
Ingo Molnar52425192009-05-26 09:17:18 +02001517 usage_with_options(stat_usage, options);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001518 if (run_count < 0) {
Ingo Molnar42202dd2009-06-13 14:57:28 +02001519 usage_with_options(stat_usage, options);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001520 } else if (run_count == 0) {
1521 forever = true;
1522 run_count = 1;
1523 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001524
Stephane Eranian023695d2011-02-14 11:20:01 +02001525 /* no_aggr, cgroup are for system-wide only */
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001526 if ((aggr_mode != AGGR_GLOBAL || nr_cgroups)
1527 && !perf_target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02001528 fprintf(stderr, "both cgroup and no-aggregation "
1529 "modes only available in system-wide mode\n");
1530
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001531 usage_with_options(stat_usage, options);
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001532 return -1;
Stephane Eraniand7e7a452013-02-06 15:46:02 +01001533 }
1534
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001535 if (add_default_attributes())
1536 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001537
Namhyung Kim4bd0f2d2012-04-26 14:15:18 +09001538 perf_target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02001539
Namhyung Kim77a6f012012-05-07 14:09:04 +09001540 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Namhyung Kimaa22dd42012-05-16 18:45:47 +09001541 if (perf_target__has_task(&target))
Namhyung Kim77a6f012012-05-07 14:09:04 +09001542 pr_err("Problems finding threads of monitor\n");
Namhyung Kimaa22dd42012-05-16 18:45:47 +09001543 if (perf_target__has_cpu(&target))
Namhyung Kim77a6f012012-05-07 14:09:04 +09001544 perror("failed to parse CPUs map");
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02001545
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001546 usage_with_options(stat_usage, options);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02001547 return -1;
1548 }
Stephane Eranian13370a92013-01-29 12:47:44 +01001549 if (interval && interval < 100) {
1550 pr_err("print interval must be >= 100ms\n");
1551 usage_with_options(stat_usage, options);
1552 return -1;
1553 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001554
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001555 if (perf_evlist__alloc_stats(evsel_list, interval))
1556 goto out_free_maps;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001557
Stephane Eranian86ee6e12013-02-14 13:57:27 +01001558 if (perf_stat_init_aggr_mode())
1559 goto out;
1560
Ingo Molnar58d7e992009-05-15 11:03:23 +02001561 /*
1562 * We dont want to block the signals - that would cause
1563 * child tasks to inherit that and Ctrl-C would not work.
1564 * What we want is for Ctrl-C to work in the exec()-ed
1565 * task, but being ignored by perf stat itself:
1566 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001567 atexit(sig_atexit);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001568 if (!forever)
1569 signal(SIGINT, skip_signal);
Stephane Eranian13370a92013-01-29 12:47:44 +01001570 signal(SIGCHLD, skip_signal);
Ingo Molnar58d7e992009-05-15 11:03:23 +02001571 signal(SIGALRM, skip_signal);
1572 signal(SIGABRT, skip_signal);
1573
Ingo Molnar42202dd2009-06-13 14:57:28 +02001574 status = 0;
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001575 for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
Ingo Molnar42202dd2009-06-13 14:57:28 +02001576 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001577 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
1578 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02001579
Ingo Molnar42202dd2009-06-13 14:57:28 +02001580 status = run_perf_stat(argc, argv);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001581 if (forever && status != -1) {
1582 print_stat(argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001583 perf_stat__reset_stats(evsel_list);
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001584 }
Ingo Molnar42202dd2009-06-13 14:57:28 +02001585 }
1586
Frederik Deweerdta7e191c2013-03-01 13:02:27 -05001587 if (!forever && status != -1 && !interval)
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -03001588 print_stat(argc, argv);
Arnaldo Carvalho de Melod134ffb2013-03-18 11:24:21 -03001589
1590 perf_evlist__free_stats(evsel_list);
1591out_free_maps:
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001592 perf_evlist__delete_maps(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02001593out:
1594 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001595 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001596}