blob: e720ba7b801eb03fdcd094cb87ea3df2d7ca52c1 [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 Weisbecker8f288272009-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 Weisbecker8f288272009-08-16 22:05:48 +020052#include "util/debug.h"
Ingo Molnara5d243d2011-04-27 05:39:24 +020053#include "util/color.h"
Liming Wang60666c62009-12-31 16:05:50 +080054#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110055#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030056#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020057#include "util/thread_map.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020058
Ingo Molnarddcacfa2009-04-20 15:37:32 +020059#include <sys/prctl.h>
Ingo Molnar42202dd2009-06-13 14:57:28 +020060#include <math.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020061#include <locale.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020062
Stephane Eraniand7470b62010-12-01 18:49:05 +020063#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060064#define CNTR_NOT_SUPPORTED "<not supported>"
65#define CNTR_NOT_COUNTED "<not counted>"
Stephane Eraniand7470b62010-12-01 18:49:05 +020066
Ingo Molnarcdd6c482009-09-21 12:02:48 +020067static struct perf_event_attr default_attrs[] = {
Ingo Molnara21ca2c2009-06-06 09:58:57 +020068
Ingo Molnar56aab462009-10-19 13:27:08 +020069 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
70 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
71 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
72 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
Ingo Molnara21ca2c2009-06-06 09:58:57 +020073
Ingo Molnar56aab462009-10-19 13:27:08 +020074 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
Ingo Molnar129c04c2011-04-29 14:41:28 +020075 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
76 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
Ingo Molnar56aab462009-10-19 13:27:08 +020077 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
Ingo Molnar56aab462009-10-19 13:27:08 +020078 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
79 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
Peter Zijlstraf4dbfa82009-06-11 14:06:28 +020080
Ingo Molnara21ca2c2009-06-06 09:58:57 +020081};
82
Ingo Molnarc6264de2011-04-27 13:50:47 +020083/*
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020084 * Detailed stats (-d), covering the L1 and last level data caches:
Ingo Molnarc6264de2011-04-27 13:50:47 +020085 */
86static struct perf_event_attr detailed_attrs[] = {
87
Ingo Molnarc6264de2011-04-27 13:50:47 +020088 { .type = PERF_TYPE_HW_CACHE,
89 .config =
90 PERF_COUNT_HW_CACHE_L1D << 0 |
91 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
92 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
93
94 { .type = PERF_TYPE_HW_CACHE,
95 .config =
96 PERF_COUNT_HW_CACHE_L1D << 0 |
97 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
98 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
99
100 { .type = PERF_TYPE_HW_CACHE,
101 .config =
102 PERF_COUNT_HW_CACHE_LL << 0 |
103 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
104 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
105
106 { .type = PERF_TYPE_HW_CACHE,
107 .config =
108 PERF_COUNT_HW_CACHE_LL << 0 |
109 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
110 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
111};
112
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200113/*
114 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
115 */
116static struct perf_event_attr very_detailed_attrs[] = {
117
118 { .type = PERF_TYPE_HW_CACHE,
119 .config =
120 PERF_COUNT_HW_CACHE_L1I << 0 |
121 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
122 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
123
124 { .type = PERF_TYPE_HW_CACHE,
125 .config =
126 PERF_COUNT_HW_CACHE_L1I << 0 |
127 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
128 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
129
130 { .type = PERF_TYPE_HW_CACHE,
131 .config =
132 PERF_COUNT_HW_CACHE_DTLB << 0 |
133 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
134 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
135
136 { .type = PERF_TYPE_HW_CACHE,
137 .config =
138 PERF_COUNT_HW_CACHE_DTLB << 0 |
139 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
140 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
141
142 { .type = PERF_TYPE_HW_CACHE,
143 .config =
144 PERF_COUNT_HW_CACHE_ITLB << 0 |
145 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
146 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
147
148 { .type = PERF_TYPE_HW_CACHE,
149 .config =
150 PERF_COUNT_HW_CACHE_ITLB << 0 |
151 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
152 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
153
154};
155
156/*
157 * Very, very detailed stats (-d -d -d), adding prefetch events:
158 */
159static struct perf_event_attr very_very_detailed_attrs[] = {
160
161 { .type = PERF_TYPE_HW_CACHE,
162 .config =
163 PERF_COUNT_HW_CACHE_L1D << 0 |
164 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
165 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
166
167 { .type = PERF_TYPE_HW_CACHE,
168 .config =
169 PERF_COUNT_HW_CACHE_L1D << 0 |
170 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
171 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
172};
173
174
175
Robert Richter666e6d42012-04-05 18:26:27 +0200176static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200177
Namhyung Kim77a6f012012-05-07 14:09:04 +0900178static struct perf_target target = {
179 .uid = UINT_MAX,
180};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530181
Namhyung Kim20f946b2012-04-26 14:15:16 +0900182static int run_idx = 0;
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530183static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200184static bool no_inherit = false;
Ian Munsiec0555642010-04-13 18:37:33 +1000185static bool scale = true;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200186static bool no_aggr = false;
Chris Wilson933da832009-10-04 01:35:01 +0100187static pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000188static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200189static int detailed_run = 0;
Ingo Molnarf9cef0a2011-04-28 18:17:11 +0200190static bool sync_run = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200191static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200192static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200193static const char *csv_sep = NULL;
194static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800195static bool group = false;
Stephane Eranian4aa90152011-08-15 22:22:33 +0200196static const char *output_name = NULL;
197static FILE *output = NULL;
Jim Cromie56f3bae2011-09-07 17:14:00 -0600198static int output_fd;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200199
Liming Wang60666c62009-12-31 16:05:50 +0800200static volatile int done = 0;
201
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200202struct stats
203{
Peter Zijlstra8a026312009-09-04 17:26:26 +0200204 double n, mean, M2;
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200205};
Ingo Molnar42202dd2009-06-13 14:57:28 +0200206
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200207struct perf_stat {
208 struct stats res_stats[3];
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200209};
210
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200211static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200212{
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200213 evsel->priv = zalloc(sizeof(struct perf_stat));
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200214 return evsel->priv == NULL ? -ENOMEM : 0;
215}
216
217static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
218{
219 free(evsel->priv);
220 evsel->priv = NULL;
221}
222
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200223static void update_stats(struct stats *stats, u64 val)
224{
Peter Zijlstra8a026312009-09-04 17:26:26 +0200225 double delta;
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200226
Peter Zijlstra8a026312009-09-04 17:26:26 +0200227 stats->n++;
228 delta = val - stats->mean;
229 stats->mean += delta / stats->n;
230 stats->M2 += delta*(val - stats->mean);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200231}
232
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200233static double avg_stats(struct stats *stats)
234{
Peter Zijlstra8a026312009-09-04 17:26:26 +0200235 return stats->mean;
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200236}
Ingo Molnar42202dd2009-06-13 14:57:28 +0200237
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200238/*
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200239 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
240 *
Peter Zijlstra8a026312009-09-04 17:26:26 +0200241 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
242 * s^2 = -------------------------------
243 * n - 1
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200244 *
245 * http://en.wikipedia.org/wiki/Stddev
246 *
247 * The std dev of the mean is related to the std dev by:
248 *
249 * s
250 * s_mean = -------
251 * sqrt(n)
252 *
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200253 */
254static double stddev_stats(struct stats *stats)
255{
Jim Cromie19f47402011-09-07 17:14:01 -0600256 double variance, variance_mean;
257
258 if (!stats->n)
259 return 0.0;
260
261 variance = stats->M2 / (stats->n - 1);
262 variance_mean = variance / stats->n;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200263
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200264 return sqrt(variance_mean);
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200265}
Ingo Molnar42202dd2009-06-13 14:57:28 +0200266
Robert Richter666e6d42012-04-05 18:26:27 +0200267static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
268static struct stats runtime_cycles_stats[MAX_NR_CPUS];
269static struct stats runtime_stalled_cycles_front_stats[MAX_NR_CPUS];
270static struct stats runtime_stalled_cycles_back_stats[MAX_NR_CPUS];
271static struct stats runtime_branches_stats[MAX_NR_CPUS];
272static struct stats runtime_cacherefs_stats[MAX_NR_CPUS];
273static struct stats runtime_l1_dcache_stats[MAX_NR_CPUS];
274static struct stats runtime_l1_icache_stats[MAX_NR_CPUS];
275static struct stats runtime_ll_cache_stats[MAX_NR_CPUS];
276static struct stats runtime_itlb_cache_stats[MAX_NR_CPUS];
277static struct stats runtime_dtlb_cache_stats[MAX_NR_CPUS];
278static struct stats walltime_nsecs_stats;
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200279
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200280static int create_perf_stat_counter(struct perf_evsel *evsel,
281 struct perf_evsel *first)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200282{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200283 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200284 struct xyarray *group_fd = NULL;
285
286 if (group && evsel != first)
287 group_fd = first->fd;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200288
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200289 if (scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200290 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
291 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200292
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200293 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300294
Namhyung Kimd67356e2012-05-07 14:09:03 +0900295 if (!perf_target__no_cpu(&target))
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200296 return perf_evsel__open_per_cpu(evsel, evsel_list->cpus,
297 group, group_fd);
Namhyung Kimd67356e2012-05-07 14:09:03 +0900298 if (perf_target__no_task(&target) && (!group || evsel == first)) {
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200299 attr->disabled = 1;
300 attr->enable_on_exec = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200301 }
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300302
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200303 return perf_evsel__open_per_thread(evsel, evsel_list->threads,
304 group, group_fd);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200305}
306
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200307/*
308 * Does the counter have nsecs as a unit?
309 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200310static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200311{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200312 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
313 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200314 return 1;
315
316 return 0;
317}
318
319/*
Ingo Molnardcd99362011-04-27 04:36:37 +0200320 * Update various tracking values we maintain to print
321 * more semantic information such as miss/hit ratios,
322 * instruction rates, etc:
323 */
324static void update_shadow_stats(struct perf_evsel *counter, u64 *count)
325{
326 if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK))
327 update_stats(&runtime_nsecs_stats[0], count[0]);
328 else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
329 update_stats(&runtime_cycles_stats[0], count[0]);
Ingo Molnard3d1e862011-04-29 13:49:08 +0200330 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
331 update_stats(&runtime_stalled_cycles_front_stats[0], count[0]);
Ingo Molnar129c04c2011-04-29 14:41:28 +0200332 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
Ingo Molnard3d1e862011-04-29 13:49:08 +0200333 update_stats(&runtime_stalled_cycles_back_stats[0], count[0]);
Ingo Molnardcd99362011-04-27 04:36:37 +0200334 else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
335 update_stats(&runtime_branches_stats[0], count[0]);
336 else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
337 update_stats(&runtime_cacherefs_stats[0], count[0]);
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200338 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
339 update_stats(&runtime_l1_dcache_stats[0], count[0]);
Ingo Molnarc33052572011-05-19 14:01:42 +0200340 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
341 update_stats(&runtime_l1_icache_stats[0], count[0]);
342 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
343 update_stats(&runtime_ll_cache_stats[0], count[0]);
344 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
345 update_stats(&runtime_dtlb_cache_stats[0], count[0]);
346 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
347 update_stats(&runtime_itlb_cache_stats[0], count[0]);
Ingo Molnardcd99362011-04-27 04:36:37 +0200348}
349
350/*
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200351 * Read out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200352 * aggregate counts across CPUs in system-wide mode
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200353 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200354static int read_counter_aggr(struct perf_evsel *counter)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200355{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200356 struct perf_stat *ps = counter->priv;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200357 u64 *count = counter->counts->aggr.values;
358 int i;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200359
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200360 if (__perf_evsel__read(counter, evsel_list->cpus->nr,
361 evsel_list->threads->nr, scale) < 0)
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200362 return -1;
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200363
364 for (i = 0; i < 3; i++)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200365 update_stats(&ps->res_stats[i], count[i]);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200366
367 if (verbose) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200368 fprintf(output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200369 event_name(counter), count[0], count[1], count[2]);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200370 }
371
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200372 /*
373 * Save the full runtime - to allow normalization during printout:
374 */
Ingo Molnardcd99362011-04-27 04:36:37 +0200375 update_shadow_stats(counter, count);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200376
377 return 0;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200378}
379
380/*
381 * Read out the results of a single counter:
382 * do not aggregate counts across CPUs in system-wide mode
383 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200384static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200385{
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200386 u64 *count;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200387 int cpu;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200388
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200389 for (cpu = 0; cpu < evsel_list->cpus->nr; cpu++) {
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200390 if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0)
391 return -1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200392
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200393 count = counter->counts->cpu[cpu].values;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200394
Ingo Molnardcd99362011-04-27 04:36:37 +0200395 update_shadow_stats(counter, count);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200396 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200397
398 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200399}
400
Ingo Molnarf37a2912009-07-01 12:37:06 +0200401static int run_perf_stat(int argc __used, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200402{
403 unsigned long long t0, t1;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200404 struct perf_evsel *counter, *first;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200405 int status = 0;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000406 int child_ready_pipe[2], go_pipe[2];
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300407 const bool forks = (argc > 0);
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000408 char buf;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200409
Liming Wang60666c62009-12-31 16:05:50 +0800410 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000411 perror("failed to create pipes");
412 exit(1);
413 }
414
Liming Wang60666c62009-12-31 16:05:50 +0800415 if (forks) {
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300416 if ((child_pid = fork()) < 0)
Liming Wang60666c62009-12-31 16:05:50 +0800417 perror("failed to fork");
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000418
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300419 if (!child_pid) {
Liming Wang60666c62009-12-31 16:05:50 +0800420 close(child_ready_pipe[0]);
421 close(go_pipe[1]);
422 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
423
424 /*
425 * Do a dummy execvp to get the PLT entry resolved,
426 * so we avoid the resolver overhead on the real
427 * execvp call.
428 */
429 execvp("", (char **)argv);
430
431 /*
432 * Tell the parent we're ready to go
433 */
434 close(child_ready_pipe[1]);
435
436 /*
437 * Wait until the parent tells us to go.
438 */
439 if (read(go_pipe[0], &buf, 1) == -1)
440 perror("unable to read pipe");
441
442 execvp(argv[0], (char **)argv);
443
444 perror(argv[0]);
445 exit(-1);
446 }
447
Namhyung Kimd67356e2012-05-07 14:09:03 +0900448 if (perf_target__none(&target))
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200449 evsel_list->threads->map[0] = child_pid;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300450
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000451 /*
Liming Wang60666c62009-12-31 16:05:50 +0800452 * Wait for the child to be ready to exec.
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000453 */
454 close(child_ready_pipe[1]);
Liming Wang60666c62009-12-31 16:05:50 +0800455 close(go_pipe[0]);
456 if (read(child_ready_pipe[0], &buf, 1) == -1)
Frederic Weisbeckera92bef02009-07-01 21:02:10 +0200457 perror("unable to read pipe");
Liming Wang60666c62009-12-31 16:05:50 +0800458 close(child_ready_pipe[0]);
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000459 }
460
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200461 first = list_entry(evsel_list->entries.next, struct perf_evsel, node);
462
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200463 list_for_each_entry(counter, &evsel_list->entries, node) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200464 if (create_perf_stat_counter(counter, first) < 0) {
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100465 if (errno == EINVAL || errno == ENOSYS ||
466 errno == ENOENT || errno == EOPNOTSUPP) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600467 if (verbose)
468 ui__warning("%s event is not supported by the kernel.\n",
469 event_name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600470 counter->supported = false;
Ingo Molnarede70292011-04-28 08:48:42 +0200471 continue;
David Ahernc63ca0c2011-04-29 16:04:15 -0600472 }
Ingo Molnarede70292011-04-28 08:48:42 +0200473
474 if (errno == EPERM || errno == EACCES) {
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200475 error("You may not have permission to collect %sstats.\n"
476 "\t Consider tweaking"
477 " /proc/sys/kernel/perf_event_paranoid or running as root.",
Namhyung Kim20f946b2012-04-26 14:15:16 +0900478 target.system_wide ? "system-wide " : "");
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200479 } else {
480 error("open_counter returned with %d (%s). "
481 "/bin/dmesg may provide additional information.\n",
482 errno, strerror(errno));
483 }
484 if (child_pid != -1)
485 kill(child_pid, SIGTERM);
486 die("Not all events could be opened.\n");
487 return -1;
488 }
David Ahern2cee77c2011-05-30 08:55:59 -0600489 counter->supported = true;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300490 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200491
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100492 if (perf_evlist__set_filters(evsel_list)) {
493 error("failed to set filter with %d (%s)\n", errno,
494 strerror(errno));
495 return -1;
496 }
497
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200498 /*
499 * Enable counters and exec the command:
500 */
501 t0 = rdclock();
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200502
Liming Wang60666c62009-12-31 16:05:50 +0800503 if (forks) {
504 close(go_pipe[1]);
505 wait(&status);
Andi Kleen33e49ea2011-09-15 14:31:40 -0700506 if (WIFSIGNALED(status))
507 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800508 } else {
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300509 while(!done) sleep(1);
Liming Wang60666c62009-12-31 16:05:50 +0800510 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200511
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200512 t1 = rdclock();
513
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200514 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200515
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200516 if (no_aggr) {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200517 list_for_each_entry(counter, &evsel_list->entries, node) {
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200518 read_counter(counter);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200519 perf_evsel__close_fd(counter, evsel_list->cpus->nr, 1);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200520 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200521 } else {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200522 list_for_each_entry(counter, &evsel_list->entries, node) {
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200523 read_counter_aggr(counter);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200524 perf_evsel__close_fd(counter, evsel_list->cpus->nr,
525 evsel_list->threads->nr);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200526 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200527 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200528
Ingo Molnar42202dd2009-06-13 14:57:28 +0200529 return WEXITSTATUS(status);
530}
531
Ingo Molnarf99844c2011-04-27 05:35:39 +0200532static void print_noise_pct(double total, double avg)
533{
534 double pct = 0.0;
535
536 if (avg)
537 pct = 100.0*total/avg;
538
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700539 if (csv_output)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200540 fprintf(output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600541 else if (pct)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200542 fprintf(output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200543}
544
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200545static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200546{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200547 struct perf_stat *ps;
548
Peter Zijlstra849abde2009-09-04 18:23:38 +0200549 if (run_count == 1)
550 return;
551
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200552 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200553 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200554}
555
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200556static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200557{
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200558 double msecs = avg / 1e6;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200559 char cpustr[16] = { '\0', };
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200560 const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-25s";
Ingo Molnar42202dd2009-06-13 14:57:28 +0200561
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200562 if (no_aggr)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200563 sprintf(cpustr, "CPU%*d%s",
564 csv_output ? 0 : -4,
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200565 evsel_list->cpus->map[cpu], csv_sep);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200566
Stephane Eranian4aa90152011-08-15 22:22:33 +0200567 fprintf(output, fmt, cpustr, msecs, csv_sep, event_name(evsel));
Stephane Eraniand7470b62010-12-01 18:49:05 +0200568
Stephane Eranian023695d2011-02-14 11:20:01 +0200569 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200570 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200571
Stephane Eraniand7470b62010-12-01 18:49:05 +0200572 if (csv_output)
573 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200574
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200575 if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Stephane Eranian4aa90152011-08-15 22:22:33 +0200576 fprintf(output, " # %8.3f CPUs utilized ",
577 avg / avg_stats(&walltime_nsecs_stats));
Namhyung Kim9dac6a22012-02-06 16:44:45 +0900578 else
579 fprintf(output, " ");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200580}
581
Namhyung Kim15e63922011-12-28 00:35:49 +0900582/* used for get_ratio_color() */
583enum grc_type {
584 GRC_STALLED_CYCLES_FE,
585 GRC_STALLED_CYCLES_BE,
586 GRC_CACHE_MISSES,
587 GRC_MAX_NR
588};
589
590static const char *get_ratio_color(enum grc_type type, double ratio)
591{
592 static const double grc_table[GRC_MAX_NR][3] = {
593 [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
594 [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
595 [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
596 };
597 const char *color = PERF_COLOR_NORMAL;
598
599 if (ratio > grc_table[type][0])
600 color = PERF_COLOR_RED;
601 else if (ratio > grc_table[type][1])
602 color = PERF_COLOR_MAGENTA;
603 else if (ratio > grc_table[type][2])
604 color = PERF_COLOR_YELLOW;
605
606 return color;
607}
608
Ingo Molnard3d1e862011-04-29 13:49:08 +0200609static void print_stalled_cycles_frontend(int cpu, struct perf_evsel *evsel __used, double avg)
610{
611 double total, ratio = 0.0;
612 const char *color;
613
614 total = avg_stats(&runtime_cycles_stats[cpu]);
615
616 if (total)
617 ratio = avg / total * 100.0;
618
Namhyung Kim15e63922011-12-28 00:35:49 +0900619 color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
Ingo Molnard3d1e862011-04-29 13:49:08 +0200620
Stephane Eranian4aa90152011-08-15 22:22:33 +0200621 fprintf(output, " # ");
622 color_fprintf(output, color, "%6.2f%%", ratio);
623 fprintf(output, " frontend cycles idle ");
Ingo Molnard3d1e862011-04-29 13:49:08 +0200624}
625
626static void print_stalled_cycles_backend(int cpu, struct perf_evsel *evsel __used, double avg)
Ingo Molnara5d243d2011-04-27 05:39:24 +0200627{
628 double total, ratio = 0.0;
629 const char *color;
630
631 total = avg_stats(&runtime_cycles_stats[cpu]);
632
633 if (total)
634 ratio = avg / total * 100.0;
635
Namhyung Kim15e63922011-12-28 00:35:49 +0900636 color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
Ingo Molnara5d243d2011-04-27 05:39:24 +0200637
Stephane Eranian4aa90152011-08-15 22:22:33 +0200638 fprintf(output, " # ");
639 color_fprintf(output, color, "%6.2f%%", ratio);
640 fprintf(output, " backend cycles idle ");
Ingo Molnara5d243d2011-04-27 05:39:24 +0200641}
642
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200643static void print_branch_misses(int cpu, struct perf_evsel *evsel __used, double avg)
644{
645 double total, ratio = 0.0;
646 const char *color;
647
648 total = avg_stats(&runtime_branches_stats[cpu]);
649
650 if (total)
651 ratio = avg / total * 100.0;
652
Namhyung Kim15e63922011-12-28 00:35:49 +0900653 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200654
Stephane Eranian4aa90152011-08-15 22:22:33 +0200655 fprintf(output, " # ");
656 color_fprintf(output, color, "%6.2f%%", ratio);
657 fprintf(output, " of all branches ");
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200658}
659
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200660static void print_l1_dcache_misses(int cpu, struct perf_evsel *evsel __used, double avg)
661{
662 double total, ratio = 0.0;
663 const char *color;
664
665 total = avg_stats(&runtime_l1_dcache_stats[cpu]);
666
667 if (total)
668 ratio = avg / total * 100.0;
669
Namhyung Kim15e63922011-12-28 00:35:49 +0900670 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200671
Stephane Eranian4aa90152011-08-15 22:22:33 +0200672 fprintf(output, " # ");
673 color_fprintf(output, color, "%6.2f%%", ratio);
674 fprintf(output, " of all L1-dcache hits ");
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200675}
676
Ingo Molnarc33052572011-05-19 14:01:42 +0200677static void print_l1_icache_misses(int cpu, struct perf_evsel *evsel __used, double avg)
678{
679 double total, ratio = 0.0;
680 const char *color;
681
682 total = avg_stats(&runtime_l1_icache_stats[cpu]);
683
684 if (total)
685 ratio = avg / total * 100.0;
686
Namhyung Kim15e63922011-12-28 00:35:49 +0900687 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc33052572011-05-19 14:01:42 +0200688
Stephane Eranian4aa90152011-08-15 22:22:33 +0200689 fprintf(output, " # ");
690 color_fprintf(output, color, "%6.2f%%", ratio);
691 fprintf(output, " of all L1-icache hits ");
Ingo Molnarc33052572011-05-19 14:01:42 +0200692}
693
694static void print_dtlb_cache_misses(int cpu, struct perf_evsel *evsel __used, double avg)
695{
696 double total, ratio = 0.0;
697 const char *color;
698
699 total = avg_stats(&runtime_dtlb_cache_stats[cpu]);
700
701 if (total)
702 ratio = avg / total * 100.0;
703
Namhyung Kim15e63922011-12-28 00:35:49 +0900704 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc33052572011-05-19 14:01:42 +0200705
Stephane Eranian4aa90152011-08-15 22:22:33 +0200706 fprintf(output, " # ");
707 color_fprintf(output, color, "%6.2f%%", ratio);
708 fprintf(output, " of all dTLB cache hits ");
Ingo Molnarc33052572011-05-19 14:01:42 +0200709}
710
711static void print_itlb_cache_misses(int cpu, struct perf_evsel *evsel __used, double avg)
712{
713 double total, ratio = 0.0;
714 const char *color;
715
716 total = avg_stats(&runtime_itlb_cache_stats[cpu]);
717
718 if (total)
719 ratio = avg / total * 100.0;
720
Namhyung Kim15e63922011-12-28 00:35:49 +0900721 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc33052572011-05-19 14:01:42 +0200722
Stephane Eranian4aa90152011-08-15 22:22:33 +0200723 fprintf(output, " # ");
724 color_fprintf(output, color, "%6.2f%%", ratio);
725 fprintf(output, " of all iTLB cache hits ");
Ingo Molnarc33052572011-05-19 14:01:42 +0200726}
727
728static void print_ll_cache_misses(int cpu, struct perf_evsel *evsel __used, double avg)
729{
730 double total, ratio = 0.0;
731 const char *color;
732
733 total = avg_stats(&runtime_ll_cache_stats[cpu]);
734
735 if (total)
736 ratio = avg / total * 100.0;
737
Namhyung Kim15e63922011-12-28 00:35:49 +0900738 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc33052572011-05-19 14:01:42 +0200739
Stephane Eranian4aa90152011-08-15 22:22:33 +0200740 fprintf(output, " # ");
741 color_fprintf(output, color, "%6.2f%%", ratio);
742 fprintf(output, " of all LL-cache hits ");
Ingo Molnarc33052572011-05-19 14:01:42 +0200743}
744
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200745static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200746{
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200747 double total, ratio = 0.0;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200748 char cpustr[16] = { '\0', };
Stephane Eraniand7470b62010-12-01 18:49:05 +0200749 const char *fmt;
750
751 if (csv_output)
752 fmt = "%s%.0f%s%s";
753 else if (big_num)
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200754 fmt = "%s%'18.0f%s%-25s";
Stephane Eraniand7470b62010-12-01 18:49:05 +0200755 else
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200756 fmt = "%s%18.0f%s%-25s";
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200757
758 if (no_aggr)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200759 sprintf(cpustr, "CPU%*d%s",
760 csv_output ? 0 : -4,
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200761 evsel_list->cpus->map[cpu], csv_sep);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200762 else
763 cpu = 0;
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200764
Stephane Eranian4aa90152011-08-15 22:22:33 +0200765 fprintf(output, fmt, cpustr, avg, csv_sep, event_name(evsel));
Stephane Eraniand7470b62010-12-01 18:49:05 +0200766
Stephane Eranian023695d2011-02-14 11:20:01 +0200767 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200768 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200769
Stephane Eraniand7470b62010-12-01 18:49:05 +0200770 if (csv_output)
771 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200772
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200773 if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200774 total = avg_stats(&runtime_cycles_stats[cpu]);
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200775
776 if (total)
777 ratio = avg / total;
778
Stephane Eranian4aa90152011-08-15 22:22:33 +0200779 fprintf(output, " # %5.2f insns per cycle ", ratio);
Ingo Molnar481f9882011-04-27 04:34:16 +0200780
Ingo Molnard3d1e862011-04-29 13:49:08 +0200781 total = avg_stats(&runtime_stalled_cycles_front_stats[cpu]);
782 total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[cpu]));
Ingo Molnar481f9882011-04-27 04:34:16 +0200783
784 if (total && avg) {
785 ratio = total / avg;
Stephane Eranian4aa90152011-08-15 22:22:33 +0200786 fprintf(output, "\n # %5.2f stalled cycles per insn", ratio);
Ingo Molnar481f9882011-04-27 04:34:16 +0200787 }
788
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200789 } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200790 runtime_branches_stats[cpu].n != 0) {
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200791 print_branch_misses(cpu, evsel, avg);
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200792 } else if (
793 evsel->attr.type == PERF_TYPE_HW_CACHE &&
794 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
795 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
796 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
Ingo Molnarc6264de2011-04-27 13:50:47 +0200797 runtime_l1_dcache_stats[cpu].n != 0) {
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200798 print_l1_dcache_misses(cpu, evsel, avg);
Ingo Molnarc33052572011-05-19 14:01:42 +0200799 } else if (
800 evsel->attr.type == PERF_TYPE_HW_CACHE &&
801 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
802 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
803 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
804 runtime_l1_icache_stats[cpu].n != 0) {
805 print_l1_icache_misses(cpu, evsel, avg);
806 } else if (
807 evsel->attr.type == PERF_TYPE_HW_CACHE &&
808 evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
809 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
810 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
811 runtime_dtlb_cache_stats[cpu].n != 0) {
812 print_dtlb_cache_misses(cpu, evsel, avg);
813 } else if (
814 evsel->attr.type == PERF_TYPE_HW_CACHE &&
815 evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
816 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
817 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
818 runtime_itlb_cache_stats[cpu].n != 0) {
819 print_itlb_cache_misses(cpu, evsel, avg);
820 } else if (
821 evsel->attr.type == PERF_TYPE_HW_CACHE &&
822 evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
823 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
824 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
825 runtime_ll_cache_stats[cpu].n != 0) {
826 print_ll_cache_misses(cpu, evsel, avg);
Ingo Molnard58f4c82011-04-27 03:42:18 +0200827 } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES) &&
828 runtime_cacherefs_stats[cpu].n != 0) {
829 total = avg_stats(&runtime_cacherefs_stats[cpu]);
830
831 if (total)
832 ratio = avg * 100 / total;
833
Stephane Eranian4aa90152011-08-15 22:22:33 +0200834 fprintf(output, " # %8.3f %% of all cache refs ", ratio);
Ingo Molnard58f4c82011-04-27 03:42:18 +0200835
Ingo Molnard3d1e862011-04-29 13:49:08 +0200836 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
837 print_stalled_cycles_frontend(cpu, evsel, avg);
Ingo Molnar129c04c2011-04-29 14:41:28 +0200838 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
Ingo Molnard3d1e862011-04-29 13:49:08 +0200839 print_stalled_cycles_backend(cpu, evsel, avg);
Ingo Molnar481f9882011-04-27 04:34:16 +0200840 } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
841 total = avg_stats(&runtime_nsecs_stats[cpu]);
842
843 if (total)
844 ratio = 1.0 * avg / total;
845
Stephane Eranian4aa90152011-08-15 22:22:33 +0200846 fprintf(output, " # %8.3f GHz ", ratio);
Ingo Molnar481f9882011-04-27 04:34:16 +0200847 } else if (runtime_nsecs_stats[cpu].n != 0) {
Namhyung Kim5fde25232012-02-06 16:44:44 +0900848 char unit = 'M';
849
Ingo Molnar481f9882011-04-27 04:34:16 +0200850 total = avg_stats(&runtime_nsecs_stats[cpu]);
851
852 if (total)
853 ratio = 1000.0 * avg / total;
Namhyung Kim5fde25232012-02-06 16:44:44 +0900854 if (ratio < 0.001) {
855 ratio *= 1000;
856 unit = 'K';
857 }
Ingo Molnar481f9882011-04-27 04:34:16 +0200858
Namhyung Kim5fde25232012-02-06 16:44:44 +0900859 fprintf(output, " # %8.3f %c/sec ", ratio, unit);
Ingo Molnara5d243d2011-04-27 05:39:24 +0200860 } else {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200861 fprintf(output, " ");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200862 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200863}
864
865/*
866 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200867 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +0200868 */
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200869static void print_counter_aggr(struct perf_evsel *counter)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200870{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200871 struct perf_stat *ps = counter->priv;
872 double avg = avg_stats(&ps->res_stats[0]);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200873 int scaled = counter->counts->scaled;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200874
Ingo Molnar42202dd2009-06-13 14:57:28 +0200875 if (scaled == -1) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200876 fprintf(output, "%*s%s%*s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200877 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600878 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian023695d2011-02-14 11:20:01 +0200879 csv_sep,
880 csv_output ? 0 : -24,
881 event_name(counter));
882
883 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200884 fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200885
Stephane Eranian4aa90152011-08-15 22:22:33 +0200886 fputc('\n', output);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200887 return;
888 }
889
890 if (nsec_counter(counter))
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200891 nsec_printout(-1, counter, avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200892 else
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200893 abs_printout(-1, counter, avg);
Peter Zijlstra849abde2009-09-04 18:23:38 +0200894
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700895 print_noise(counter, avg);
896
Stephane Eraniand7470b62010-12-01 18:49:05 +0200897 if (csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200898 fputc('\n', output);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200899 return;
900 }
901
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200902 if (scaled) {
903 double avg_enabled, avg_running;
904
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200905 avg_enabled = avg_stats(&ps->res_stats[1]);
906 avg_running = avg_stats(&ps->res_stats[2]);
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200907
Stephane Eranian4aa90152011-08-15 22:22:33 +0200908 fprintf(output, " [%5.2f%%]", 100 * avg_running / avg_enabled);
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200909 }
Stephane Eranian4aa90152011-08-15 22:22:33 +0200910 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200911}
912
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200913/*
914 * Print out the results of a single counter:
915 * does not use aggregated count in system-wide
916 */
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200917static void print_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200918{
919 u64 ena, run, val;
920 int cpu;
921
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200922 for (cpu = 0; cpu < evsel_list->cpus->nr; cpu++) {
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200923 val = counter->counts->cpu[cpu].val;
924 ena = counter->counts->cpu[cpu].ena;
925 run = counter->counts->cpu[cpu].run;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200926 if (run == 0 || ena == 0) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200927 fprintf(output, "CPU%*d%s%*s%s%*s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200928 csv_output ? 0 : -4,
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200929 evsel_list->cpus->map[cpu], csv_sep,
Stephane Eraniand7470b62010-12-01 18:49:05 +0200930 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600931 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
932 csv_sep,
Stephane Eranian023695d2011-02-14 11:20:01 +0200933 csv_output ? 0 : -24,
Stephane Eraniand7470b62010-12-01 18:49:05 +0200934 event_name(counter));
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200935
Stephane Eranian023695d2011-02-14 11:20:01 +0200936 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200937 fprintf(output, "%s%s",
938 csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200939
Stephane Eranian4aa90152011-08-15 22:22:33 +0200940 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200941 continue;
942 }
943
944 if (nsec_counter(counter))
945 nsec_printout(cpu, counter, val);
946 else
947 abs_printout(cpu, counter, val);
948
Stephane Eraniand7470b62010-12-01 18:49:05 +0200949 if (!csv_output) {
950 print_noise(counter, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200951
Ingo Molnarc6264de2011-04-27 13:50:47 +0200952 if (run != ena)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200953 fprintf(output, " (%.2f%%)",
954 100.0 * run / ena);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200955 }
Stephane Eranian4aa90152011-08-15 22:22:33 +0200956 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200957 }
958}
959
Ingo Molnar42202dd2009-06-13 14:57:28 +0200960static void print_stat(int argc, const char **argv)
961{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200962 struct perf_evsel *counter;
963 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200964
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200965 fflush(stdout);
966
Stephane Eraniand7470b62010-12-01 18:49:05 +0200967 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200968 fprintf(output, "\n");
969 fprintf(output, " Performance counter stats for ");
Namhyung Kimd67356e2012-05-07 14:09:03 +0900970 if (perf_target__no_task(&target)) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200971 fprintf(output, "\'%s", argv[0]);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200972 for (i = 1; i < argc; i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200973 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +0900974 } else if (target.pid)
975 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200976 else
Namhyung Kim20f946b2012-04-26 14:15:16 +0900977 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +0200978
Stephane Eranian4aa90152011-08-15 22:22:33 +0200979 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +0200980 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200981 fprintf(output, " (%d runs)", run_count);
982 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +0200983 }
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200984
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200985 if (no_aggr) {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200986 list_for_each_entry(counter, &evsel_list->entries, node)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200987 print_counter(counter);
988 } else {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200989 list_for_each_entry(counter, &evsel_list->entries, node)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200990 print_counter_aggr(counter);
991 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200992
Stephane Eraniand7470b62010-12-01 18:49:05 +0200993 if (!csv_output) {
Ingo Molnarc33052572011-05-19 14:01:42 +0200994 if (!null_run)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200995 fprintf(output, "\n");
996 fprintf(output, " %17.9f seconds time elapsed",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200997 avg_stats(&walltime_nsecs_stats)/1e9);
998 if (run_count > 1) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200999 fprintf(output, " ");
Ingo Molnarf99844c2011-04-27 05:35:39 +02001000 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
1001 avg_stats(&walltime_nsecs_stats));
Stephane Eraniand7470b62010-12-01 18:49:05 +02001002 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001003 fprintf(output, "\n\n");
Ingo Molnar566747e2009-06-27 06:24:32 +02001004 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001005}
1006
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001007static volatile int signr = -1;
1008
Ingo Molnar52425192009-05-26 09:17:18 +02001009static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001010{
Zhang, Yanmin6be28502010-03-18 11:36:03 -03001011 if(child_pid == -1)
Liming Wang60666c62009-12-31 16:05:50 +08001012 done = 1;
1013
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001014 signr = signo;
1015}
1016
1017static void sig_atexit(void)
1018{
Chris Wilson933da832009-10-04 01:35:01 +01001019 if (child_pid != -1)
1020 kill(child_pid, SIGTERM);
1021
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001022 if (signr == -1)
1023 return;
1024
1025 signal(signr, SIG_DFL);
1026 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001027}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001028
Ingo Molnar52425192009-05-26 09:17:18 +02001029static const char * const stat_usage[] = {
Liming Wang60666c62009-12-31 16:05:50 +08001030 "perf stat [<options>] [<command>]",
Ingo Molnar52425192009-05-26 09:17:18 +02001031 NULL
1032};
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001033
Stephane Eraniand7470b62010-12-01 18:49:05 +02001034static int stat__set_big_num(const struct option *opt __used,
1035 const char *s __used, int unset)
1036{
1037 big_num_opt = unset ? 0 : 1;
1038 return 0;
1039}
1040
Stephane Eranian4aa90152011-08-15 22:22:33 +02001041static bool append_file;
1042
Ingo Molnar52425192009-05-26 09:17:18 +02001043static const struct option options[] = {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001044 OPT_CALLBACK('e', "event", &evsel_list, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001045 "event selector. use 'perf list' to list available events",
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001046 parse_events_option),
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +01001047 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1048 "event filter", parse_filter),
Stephane Eranian2e6cdf92010-05-12 10:40:01 +02001049 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1050 "child tasks do not inherit counters"),
Namhyung Kim20f946b2012-04-26 14:15:16 +09001051 OPT_STRING('p', "pid", &target.pid, "pid",
David Ahernb52956c2012-02-08 09:32:52 -07001052 "stat events on existing process id"),
Namhyung Kim20f946b2012-04-26 14:15:16 +09001053 OPT_STRING('t', "tid", &target.tid, "tid",
David Ahernb52956c2012-02-08 09:32:52 -07001054 "stat events on existing thread id"),
Namhyung Kim20f946b2012-04-26 14:15:16 +09001055 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +05301056 "system-wide collection from all CPUs"),
Lin Ming43bece72011-08-17 18:42:07 +08001057 OPT_BOOLEAN('g', "group", &group,
1058 "put the counters into a counter group"),
Brice Goglinb26bc5a2009-08-07 10:18:39 +02001059 OPT_BOOLEAN('c', "scale", &scale,
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +05301060 "scale/normalize counters"),
Ian Munsiec0555642010-04-13 18:37:33 +10001061 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar743ee1f2009-06-07 17:06:46 +02001062 "be more verbose (show counter open errors, etc)"),
Ingo Molnar42202dd2009-06-13 14:57:28 +02001063 OPT_INTEGER('r', "repeat", &run_count,
1064 "repeat command and print average + stddev (max: 100)"),
Ingo Molnar0cfb7a12009-06-27 06:10:30 +02001065 OPT_BOOLEAN('n', "null", &null_run,
1066 "null run - dont start any counters"),
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001067 OPT_INCR('d', "detailed", &detailed_run,
Ingo Molnarc6264de2011-04-27 13:50:47 +02001068 "detailed run - start a lot of events"),
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02001069 OPT_BOOLEAN('S', "sync", &sync_run,
1070 "call sync() before starting a run"),
Stephane Eraniand7470b62010-12-01 18:49:05 +02001071 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1072 "print large numbers with thousands\' separators",
1073 stat__set_big_num),
Namhyung Kim20f946b2012-04-26 14:15:16 +09001074 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001075 "list of cpus to monitor in system-wide"),
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001076 OPT_BOOLEAN('A', "no-aggr", &no_aggr,
1077 "disable CPU count aggregation"),
Stephane Eraniand7470b62010-12-01 18:49:05 +02001078 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1079 "print counts with custom separator"),
Stephane Eranian023695d2011-02-14 11:20:01 +02001080 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1081 "monitor event in cgroup name only",
1082 parse_cgroups),
Stephane Eranian4aa90152011-08-15 22:22:33 +02001083 OPT_STRING('o', "output", &output_name, "file",
1084 "output file name"),
1085 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
Jim Cromie56f3bae2011-09-07 17:14:00 -06001086 OPT_INTEGER(0, "log-fd", &output_fd,
1087 "log output to fd, instead of stderr"),
Ingo Molnar52425192009-05-26 09:17:18 +02001088 OPT_END()
1089};
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001090
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001091/*
1092 * Add default attributes, if there were no attributes specified or
1093 * if -d/--detailed, -d -d or -d -d -d is used:
1094 */
1095static int add_default_attributes(void)
1096{
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001097 /* Set attrs if no event is selected and !null_run: */
1098 if (null_run)
1099 return 0;
1100
1101 if (!evsel_list->nr_entries) {
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001102 if (perf_evlist__add_attrs_array(evsel_list, default_attrs) < 0)
1103 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001104 }
1105
1106 /* Detailed events get appended to the event list: */
1107
1108 if (detailed_run < 1)
1109 return 0;
1110
1111 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001112 if (perf_evlist__add_attrs_array(evsel_list, detailed_attrs) < 0)
1113 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001114
1115 if (detailed_run < 2)
1116 return 0;
1117
1118 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001119 if (perf_evlist__add_attrs_array(evsel_list, very_detailed_attrs) < 0)
1120 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001121
1122 if (detailed_run < 3)
1123 return 0;
1124
1125 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001126 return perf_evlist__add_attrs_array(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001127}
1128
Ingo Molnarf37a2912009-07-01 12:37:06 +02001129int cmd_stat(int argc, const char **argv, const char *prefix __used)
Ingo Molnar52425192009-05-26 09:17:18 +02001130{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001131 struct perf_evsel *pos;
1132 int status = -ENOMEM;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001133 const char *mode;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001134
Stephane Eranian5af52b52010-05-18 15:00:01 +02001135 setlocale(LC_ALL, "");
1136
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001137 evsel_list = perf_evlist__new(NULL, NULL);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001138 if (evsel_list == NULL)
1139 return -ENOMEM;
1140
Anton Blancharda0541232009-07-22 23:04:12 +10001141 argc = parse_options(argc, argv, options, stat_usage,
1142 PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001143
Stephane Eranian4aa90152011-08-15 22:22:33 +02001144 output = stderr;
1145 if (output_name && strcmp(output_name, "-"))
1146 output = NULL;
1147
Jim Cromie56f3bae2011-09-07 17:14:00 -06001148 if (output_name && output_fd) {
1149 fprintf(stderr, "cannot use both --output and --log-fd\n");
1150 usage_with_options(stat_usage, options);
1151 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001152 if (!output) {
1153 struct timespec tm;
1154 mode = append_file ? "a" : "w";
1155
1156 output = fopen(output_name, mode);
1157 if (!output) {
1158 perror("failed to create output file");
1159 exit(-1);
1160 }
1161 clock_gettime(CLOCK_REALTIME, &tm);
1162 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Jim Cromie56f3bae2011-09-07 17:14:00 -06001163 } else if (output_fd != 2) {
1164 mode = append_file ? "a" : "w";
1165 output = fdopen(output_fd, mode);
1166 if (!output) {
1167 perror("Failed opening logfd");
1168 return -errno;
1169 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001170 }
1171
Jim Cromied4ffd042011-09-07 17:14:03 -06001172 if (csv_sep) {
Stephane Eraniand7470b62010-12-01 18:49:05 +02001173 csv_output = true;
Jim Cromied4ffd042011-09-07 17:14:03 -06001174 if (!strcmp(csv_sep, "\\t"))
1175 csv_sep = "\t";
1176 } else
Stephane Eraniand7470b62010-12-01 18:49:05 +02001177 csv_sep = DEFAULT_SEPARATOR;
1178
1179 /*
1180 * let the spreadsheet do the pretty-printing
1181 */
1182 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06001183 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02001184 if (big_num_opt == 1) {
1185 fprintf(stderr, "-B option not supported with -x\n");
1186 usage_with_options(stat_usage, options);
1187 } else /* Nope, so disable big number formatting */
1188 big_num = false;
1189 } else if (big_num_opt == 0) /* User passed --no-big-num */
1190 big_num = false;
1191
Namhyung Kimd67356e2012-05-07 14:09:03 +09001192 if (!argc && perf_target__no_task(&target))
Ingo Molnar52425192009-05-26 09:17:18 +02001193 usage_with_options(stat_usage, options);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +02001194 if (run_count <= 0)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001195 usage_with_options(stat_usage, options);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001196
Stephane Eranian023695d2011-02-14 11:20:01 +02001197 /* no_aggr, cgroup are for system-wide only */
Namhyung Kimd67356e2012-05-07 14:09:03 +09001198 if ((no_aggr || nr_cgroups) && perf_target__no_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02001199 fprintf(stderr, "both cgroup and no-aggregation "
1200 "modes only available in system-wide mode\n");
1201
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001202 usage_with_options(stat_usage, options);
Stephane Eranian023695d2011-02-14 11:20:01 +02001203 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001204
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001205 if (add_default_attributes())
1206 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001207
Namhyung Kim4bd0f2d2012-04-26 14:15:18 +09001208 perf_target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02001209
Namhyung Kim77a6f012012-05-07 14:09:04 +09001210 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
1211 if (!perf_target__no_task(&target))
1212 pr_err("Problems finding threads of monitor\n");
1213 if (!perf_target__no_cpu(&target))
1214 perror("failed to parse CPUs map");
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02001215
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001216 usage_with_options(stat_usage, options);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02001217 return -1;
1218 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001219
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001220 list_for_each_entry(pos, &evsel_list->entries, node) {
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001221 if (perf_evsel__alloc_stat_priv(pos) < 0 ||
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -02001222 perf_evsel__alloc_counts(pos, evsel_list->cpus->nr) < 0)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001223 goto out_free_fd;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001224 }
1225
Ingo Molnar58d7e992009-05-15 11:03:23 +02001226 /*
1227 * We dont want to block the signals - that would cause
1228 * child tasks to inherit that and Ctrl-C would not work.
1229 * What we want is for Ctrl-C to work in the exec()-ed
1230 * task, but being ignored by perf stat itself:
1231 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001232 atexit(sig_atexit);
Ingo Molnar58d7e992009-05-15 11:03:23 +02001233 signal(SIGINT, skip_signal);
1234 signal(SIGALRM, skip_signal);
1235 signal(SIGABRT, skip_signal);
1236
Ingo Molnar42202dd2009-06-13 14:57:28 +02001237 status = 0;
1238 for (run_idx = 0; run_idx < run_count; run_idx++) {
1239 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001240 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
1241 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02001242
1243 if (sync_run)
1244 sync();
1245
Ingo Molnar42202dd2009-06-13 14:57:28 +02001246 status = run_perf_stat(argc, argv);
1247 }
1248
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -03001249 if (status != -1)
1250 print_stat(argc, argv);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001251out_free_fd:
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001252 list_for_each_entry(pos, &evsel_list->entries, node)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001253 perf_evsel__free_stat_priv(pos);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001254 perf_evlist__delete_maps(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02001255out:
1256 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001257 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001258}