blob: d9ff24637eebffa91c0acd7b3ef0b2f50c96065a [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 Kim20f946b2012-04-26 14:15:16 +0900178static struct perf_target target;
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530179
Namhyung Kim20f946b2012-04-26 14:15:16 +0900180static int run_idx = 0;
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +0530181static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +0200182static bool no_inherit = false;
Ian Munsiec0555642010-04-13 18:37:33 +1000183static bool scale = true;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200184static bool no_aggr = false;
Chris Wilson933da832009-10-04 01:35:01 +0100185static pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +1000186static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200187static int detailed_run = 0;
Ingo Molnarf9cef0a2011-04-28 18:17:11 +0200188static bool sync_run = false;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -0200189static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200190static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200191static const char *csv_sep = NULL;
192static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +0800193static bool group = false;
Stephane Eranian4aa90152011-08-15 22:22:33 +0200194static const char *output_name = NULL;
195static FILE *output = NULL;
Jim Cromie56f3bae2011-09-07 17:14:00 -0600196static int output_fd;
Stephane Eranian5af52b52010-05-18 15:00:01 +0200197
Liming Wang60666c62009-12-31 16:05:50 +0800198static volatile int done = 0;
199
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200200struct stats
201{
Peter Zijlstra8a026312009-09-04 17:26:26 +0200202 double n, mean, M2;
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200203};
Ingo Molnar42202dd2009-06-13 14:57:28 +0200204
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200205struct perf_stat {
206 struct stats res_stats[3];
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200207};
208
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200209static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200210{
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200211 evsel->priv = zalloc(sizeof(struct perf_stat));
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200212 return evsel->priv == NULL ? -ENOMEM : 0;
213}
214
215static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
216{
217 free(evsel->priv);
218 evsel->priv = NULL;
219}
220
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200221static void update_stats(struct stats *stats, u64 val)
222{
Peter Zijlstra8a026312009-09-04 17:26:26 +0200223 double delta;
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200224
Peter Zijlstra8a026312009-09-04 17:26:26 +0200225 stats->n++;
226 delta = val - stats->mean;
227 stats->mean += delta / stats->n;
228 stats->M2 += delta*(val - stats->mean);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200229}
230
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200231static double avg_stats(struct stats *stats)
232{
Peter Zijlstra8a026312009-09-04 17:26:26 +0200233 return stats->mean;
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200234}
Ingo Molnar42202dd2009-06-13 14:57:28 +0200235
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200236/*
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200237 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
238 *
Peter Zijlstra8a026312009-09-04 17:26:26 +0200239 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
240 * s^2 = -------------------------------
241 * n - 1
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200242 *
243 * http://en.wikipedia.org/wiki/Stddev
244 *
245 * The std dev of the mean is related to the std dev by:
246 *
247 * s
248 * s_mean = -------
249 * sqrt(n)
250 *
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200251 */
252static double stddev_stats(struct stats *stats)
253{
Jim Cromie19f47402011-09-07 17:14:01 -0600254 double variance, variance_mean;
255
256 if (!stats->n)
257 return 0.0;
258
259 variance = stats->M2 / (stats->n - 1);
260 variance_mean = variance / stats->n;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200261
Peter Zijlstra63d40de2009-09-04 17:03:13 +0200262 return sqrt(variance_mean);
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200263}
Ingo Molnar42202dd2009-06-13 14:57:28 +0200264
Robert Richter666e6d42012-04-05 18:26:27 +0200265static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
266static struct stats runtime_cycles_stats[MAX_NR_CPUS];
267static struct stats runtime_stalled_cycles_front_stats[MAX_NR_CPUS];
268static struct stats runtime_stalled_cycles_back_stats[MAX_NR_CPUS];
269static struct stats runtime_branches_stats[MAX_NR_CPUS];
270static struct stats runtime_cacherefs_stats[MAX_NR_CPUS];
271static struct stats runtime_l1_dcache_stats[MAX_NR_CPUS];
272static struct stats runtime_l1_icache_stats[MAX_NR_CPUS];
273static struct stats runtime_ll_cache_stats[MAX_NR_CPUS];
274static struct stats runtime_itlb_cache_stats[MAX_NR_CPUS];
275static struct stats runtime_dtlb_cache_stats[MAX_NR_CPUS];
276static struct stats walltime_nsecs_stats;
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200277
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200278static int create_perf_stat_counter(struct perf_evsel *evsel,
279 struct perf_evsel *first)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200280{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200281 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200282 struct xyarray *group_fd = NULL;
283
284 if (group && evsel != first)
285 group_fd = first->fd;
Peter Zijlstra16c8a102009-05-05 17:50:27 +0200286
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200287 if (scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200288 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
289 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200290
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200291 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300292
Namhyung Kimd67356e2012-05-07 14:09:03 +0900293 if (!perf_target__no_cpu(&target))
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200294 return perf_evsel__open_per_cpu(evsel, evsel_list->cpus,
295 group, group_fd);
Namhyung Kimd67356e2012-05-07 14:09:03 +0900296 if (perf_target__no_task(&target) && (!group || evsel == first)) {
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200297 attr->disabled = 1;
298 attr->enable_on_exec = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200299 }
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300300
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200301 return perf_evsel__open_per_thread(evsel, evsel_list->threads,
302 group, group_fd);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200303}
304
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200305/*
306 * Does the counter have nsecs as a unit?
307 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200308static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200309{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200310 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
311 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200312 return 1;
313
314 return 0;
315}
316
317/*
Ingo Molnardcd99362011-04-27 04:36:37 +0200318 * Update various tracking values we maintain to print
319 * more semantic information such as miss/hit ratios,
320 * instruction rates, etc:
321 */
322static void update_shadow_stats(struct perf_evsel *counter, u64 *count)
323{
324 if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK))
325 update_stats(&runtime_nsecs_stats[0], count[0]);
326 else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
327 update_stats(&runtime_cycles_stats[0], count[0]);
Ingo Molnard3d1e862011-04-29 13:49:08 +0200328 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
329 update_stats(&runtime_stalled_cycles_front_stats[0], count[0]);
Ingo Molnar129c04c2011-04-29 14:41:28 +0200330 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
Ingo Molnard3d1e862011-04-29 13:49:08 +0200331 update_stats(&runtime_stalled_cycles_back_stats[0], count[0]);
Ingo Molnardcd99362011-04-27 04:36:37 +0200332 else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
333 update_stats(&runtime_branches_stats[0], count[0]);
334 else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
335 update_stats(&runtime_cacherefs_stats[0], count[0]);
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200336 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
337 update_stats(&runtime_l1_dcache_stats[0], count[0]);
Ingo Molnarc3305252011-05-19 14:01:42 +0200338 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
339 update_stats(&runtime_l1_icache_stats[0], count[0]);
340 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
341 update_stats(&runtime_ll_cache_stats[0], count[0]);
342 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
343 update_stats(&runtime_dtlb_cache_stats[0], count[0]);
344 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
345 update_stats(&runtime_itlb_cache_stats[0], count[0]);
Ingo Molnardcd99362011-04-27 04:36:37 +0200346}
347
348/*
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200349 * Read out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200350 * aggregate counts across CPUs in system-wide mode
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200351 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200352static int read_counter_aggr(struct perf_evsel *counter)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200353{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200354 struct perf_stat *ps = counter->priv;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200355 u64 *count = counter->counts->aggr.values;
356 int i;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200357
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200358 if (__perf_evsel__read(counter, evsel_list->cpus->nr,
359 evsel_list->threads->nr, scale) < 0)
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200360 return -1;
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200361
362 for (i = 0; i < 3; i++)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200363 update_stats(&ps->res_stats[i], count[i]);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200364
365 if (verbose) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200366 fprintf(output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200367 event_name(counter), count[0], count[1], count[2]);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200368 }
369
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200370 /*
371 * Save the full runtime - to allow normalization during printout:
372 */
Ingo Molnardcd99362011-04-27 04:36:37 +0200373 update_shadow_stats(counter, count);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200374
375 return 0;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200376}
377
378/*
379 * Read out the results of a single counter:
380 * do not aggregate counts across CPUs in system-wide mode
381 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200382static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200383{
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200384 u64 *count;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200385 int cpu;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200386
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200387 for (cpu = 0; cpu < evsel_list->cpus->nr; cpu++) {
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200388 if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0)
389 return -1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200390
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200391 count = counter->counts->cpu[cpu].values;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200392
Ingo Molnardcd99362011-04-27 04:36:37 +0200393 update_shadow_stats(counter, count);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200394 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200395
396 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200397}
398
Ingo Molnarf37a2912009-07-01 12:37:06 +0200399static int run_perf_stat(int argc __used, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200400{
401 unsigned long long t0, t1;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200402 struct perf_evsel *counter, *first;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200403 int status = 0;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000404 int child_ready_pipe[2], go_pipe[2];
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300405 const bool forks = (argc > 0);
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000406 char buf;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200407
Liming Wang60666c62009-12-31 16:05:50 +0800408 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000409 perror("failed to create pipes");
410 exit(1);
411 }
412
Liming Wang60666c62009-12-31 16:05:50 +0800413 if (forks) {
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300414 if ((child_pid = fork()) < 0)
Liming Wang60666c62009-12-31 16:05:50 +0800415 perror("failed to fork");
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000416
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300417 if (!child_pid) {
Liming Wang60666c62009-12-31 16:05:50 +0800418 close(child_ready_pipe[0]);
419 close(go_pipe[1]);
420 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
421
422 /*
423 * Do a dummy execvp to get the PLT entry resolved,
424 * so we avoid the resolver overhead on the real
425 * execvp call.
426 */
427 execvp("", (char **)argv);
428
429 /*
430 * Tell the parent we're ready to go
431 */
432 close(child_ready_pipe[1]);
433
434 /*
435 * Wait until the parent tells us to go.
436 */
437 if (read(go_pipe[0], &buf, 1) == -1)
438 perror("unable to read pipe");
439
440 execvp(argv[0], (char **)argv);
441
442 perror(argv[0]);
443 exit(-1);
444 }
445
Namhyung Kimd67356e2012-05-07 14:09:03 +0900446 if (perf_target__none(&target))
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200447 evsel_list->threads->map[0] = child_pid;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300448
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000449 /*
Liming Wang60666c62009-12-31 16:05:50 +0800450 * Wait for the child to be ready to exec.
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000451 */
452 close(child_ready_pipe[1]);
Liming Wang60666c62009-12-31 16:05:50 +0800453 close(go_pipe[0]);
454 if (read(child_ready_pipe[0], &buf, 1) == -1)
Frederic Weisbeckera92bef02009-07-01 21:02:10 +0200455 perror("unable to read pipe");
Liming Wang60666c62009-12-31 16:05:50 +0800456 close(child_ready_pipe[0]);
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000457 }
458
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200459 first = list_entry(evsel_list->entries.next, struct perf_evsel, node);
460
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200461 list_for_each_entry(counter, &evsel_list->entries, node) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200462 if (create_perf_stat_counter(counter, first) < 0) {
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100463 if (errno == EINVAL || errno == ENOSYS ||
464 errno == ENOENT || errno == EOPNOTSUPP) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600465 if (verbose)
466 ui__warning("%s event is not supported by the kernel.\n",
467 event_name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600468 counter->supported = false;
Ingo Molnarede70292011-04-28 08:48:42 +0200469 continue;
David Ahernc63ca0c2011-04-29 16:04:15 -0600470 }
Ingo Molnarede70292011-04-28 08:48:42 +0200471
472 if (errno == EPERM || errno == EACCES) {
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200473 error("You may not have permission to collect %sstats.\n"
474 "\t Consider tweaking"
475 " /proc/sys/kernel/perf_event_paranoid or running as root.",
Namhyung Kim20f946b2012-04-26 14:15:16 +0900476 target.system_wide ? "system-wide " : "");
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200477 } else {
478 error("open_counter returned with %d (%s). "
479 "/bin/dmesg may provide additional information.\n",
480 errno, strerror(errno));
481 }
482 if (child_pid != -1)
483 kill(child_pid, SIGTERM);
484 die("Not all events could be opened.\n");
485 return -1;
486 }
David Ahern2cee77c2011-05-30 08:55:59 -0600487 counter->supported = true;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300488 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200489
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100490 if (perf_evlist__set_filters(evsel_list)) {
491 error("failed to set filter with %d (%s)\n", errno,
492 strerror(errno));
493 return -1;
494 }
495
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200496 /*
497 * Enable counters and exec the command:
498 */
499 t0 = rdclock();
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200500
Liming Wang60666c62009-12-31 16:05:50 +0800501 if (forks) {
502 close(go_pipe[1]);
503 wait(&status);
Andi Kleen33e49ea2011-09-15 14:31:40 -0700504 if (WIFSIGNALED(status))
505 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800506 } else {
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300507 while(!done) sleep(1);
Liming Wang60666c62009-12-31 16:05:50 +0800508 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200509
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200510 t1 = rdclock();
511
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200512 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200513
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200514 if (no_aggr) {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200515 list_for_each_entry(counter, &evsel_list->entries, node) {
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200516 read_counter(counter);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200517 perf_evsel__close_fd(counter, evsel_list->cpus->nr, 1);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200518 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200519 } else {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200520 list_for_each_entry(counter, &evsel_list->entries, node) {
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200521 read_counter_aggr(counter);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200522 perf_evsel__close_fd(counter, evsel_list->cpus->nr,
523 evsel_list->threads->nr);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200524 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200525 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200526
Ingo Molnar42202dd2009-06-13 14:57:28 +0200527 return WEXITSTATUS(status);
528}
529
Ingo Molnarf99844c2011-04-27 05:35:39 +0200530static void print_noise_pct(double total, double avg)
531{
532 double pct = 0.0;
533
534 if (avg)
535 pct = 100.0*total/avg;
536
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700537 if (csv_output)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200538 fprintf(output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600539 else if (pct)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200540 fprintf(output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200541}
542
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200543static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200544{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200545 struct perf_stat *ps;
546
Peter Zijlstra849abde2009-09-04 18:23:38 +0200547 if (run_count == 1)
548 return;
549
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200550 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200551 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200552}
553
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200554static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200555{
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200556 double msecs = avg / 1e6;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200557 char cpustr[16] = { '\0', };
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200558 const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-25s";
Ingo Molnar42202dd2009-06-13 14:57:28 +0200559
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200560 if (no_aggr)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200561 sprintf(cpustr, "CPU%*d%s",
562 csv_output ? 0 : -4,
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200563 evsel_list->cpus->map[cpu], csv_sep);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200564
Stephane Eranian4aa90152011-08-15 22:22:33 +0200565 fprintf(output, fmt, cpustr, msecs, csv_sep, event_name(evsel));
Stephane Eraniand7470b62010-12-01 18:49:05 +0200566
Stephane Eranian023695d2011-02-14 11:20:01 +0200567 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200568 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200569
Stephane Eraniand7470b62010-12-01 18:49:05 +0200570 if (csv_output)
571 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200572
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200573 if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Stephane Eranian4aa90152011-08-15 22:22:33 +0200574 fprintf(output, " # %8.3f CPUs utilized ",
575 avg / avg_stats(&walltime_nsecs_stats));
Namhyung Kim9dac6a22012-02-06 16:44:45 +0900576 else
577 fprintf(output, " ");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200578}
579
Namhyung Kim15e63922011-12-28 00:35:49 +0900580/* used for get_ratio_color() */
581enum grc_type {
582 GRC_STALLED_CYCLES_FE,
583 GRC_STALLED_CYCLES_BE,
584 GRC_CACHE_MISSES,
585 GRC_MAX_NR
586};
587
588static const char *get_ratio_color(enum grc_type type, double ratio)
589{
590 static const double grc_table[GRC_MAX_NR][3] = {
591 [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
592 [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
593 [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
594 };
595 const char *color = PERF_COLOR_NORMAL;
596
597 if (ratio > grc_table[type][0])
598 color = PERF_COLOR_RED;
599 else if (ratio > grc_table[type][1])
600 color = PERF_COLOR_MAGENTA;
601 else if (ratio > grc_table[type][2])
602 color = PERF_COLOR_YELLOW;
603
604 return color;
605}
606
Ingo Molnard3d1e862011-04-29 13:49:08 +0200607static void print_stalled_cycles_frontend(int cpu, struct perf_evsel *evsel __used, double avg)
608{
609 double total, ratio = 0.0;
610 const char *color;
611
612 total = avg_stats(&runtime_cycles_stats[cpu]);
613
614 if (total)
615 ratio = avg / total * 100.0;
616
Namhyung Kim15e63922011-12-28 00:35:49 +0900617 color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
Ingo Molnard3d1e862011-04-29 13:49:08 +0200618
Stephane Eranian4aa90152011-08-15 22:22:33 +0200619 fprintf(output, " # ");
620 color_fprintf(output, color, "%6.2f%%", ratio);
621 fprintf(output, " frontend cycles idle ");
Ingo Molnard3d1e862011-04-29 13:49:08 +0200622}
623
624static void print_stalled_cycles_backend(int cpu, struct perf_evsel *evsel __used, double avg)
Ingo Molnara5d243d2011-04-27 05:39:24 +0200625{
626 double total, ratio = 0.0;
627 const char *color;
628
629 total = avg_stats(&runtime_cycles_stats[cpu]);
630
631 if (total)
632 ratio = avg / total * 100.0;
633
Namhyung Kim15e63922011-12-28 00:35:49 +0900634 color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
Ingo Molnara5d243d2011-04-27 05:39:24 +0200635
Stephane Eranian4aa90152011-08-15 22:22:33 +0200636 fprintf(output, " # ");
637 color_fprintf(output, color, "%6.2f%%", ratio);
638 fprintf(output, " backend cycles idle ");
Ingo Molnara5d243d2011-04-27 05:39:24 +0200639}
640
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200641static void print_branch_misses(int cpu, struct perf_evsel *evsel __used, double avg)
642{
643 double total, ratio = 0.0;
644 const char *color;
645
646 total = avg_stats(&runtime_branches_stats[cpu]);
647
648 if (total)
649 ratio = avg / total * 100.0;
650
Namhyung Kim15e63922011-12-28 00:35:49 +0900651 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200652
Stephane Eranian4aa90152011-08-15 22:22:33 +0200653 fprintf(output, " # ");
654 color_fprintf(output, color, "%6.2f%%", ratio);
655 fprintf(output, " of all branches ");
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200656}
657
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200658static void print_l1_dcache_misses(int cpu, struct perf_evsel *evsel __used, double avg)
659{
660 double total, ratio = 0.0;
661 const char *color;
662
663 total = avg_stats(&runtime_l1_dcache_stats[cpu]);
664
665 if (total)
666 ratio = avg / total * 100.0;
667
Namhyung Kim15e63922011-12-28 00:35:49 +0900668 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200669
Stephane Eranian4aa90152011-08-15 22:22:33 +0200670 fprintf(output, " # ");
671 color_fprintf(output, color, "%6.2f%%", ratio);
672 fprintf(output, " of all L1-dcache hits ");
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200673}
674
Ingo Molnarc3305252011-05-19 14:01:42 +0200675static void print_l1_icache_misses(int cpu, struct perf_evsel *evsel __used, double avg)
676{
677 double total, ratio = 0.0;
678 const char *color;
679
680 total = avg_stats(&runtime_l1_icache_stats[cpu]);
681
682 if (total)
683 ratio = avg / total * 100.0;
684
Namhyung Kim15e63922011-12-28 00:35:49 +0900685 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc3305252011-05-19 14:01:42 +0200686
Stephane Eranian4aa90152011-08-15 22:22:33 +0200687 fprintf(output, " # ");
688 color_fprintf(output, color, "%6.2f%%", ratio);
689 fprintf(output, " of all L1-icache hits ");
Ingo Molnarc3305252011-05-19 14:01:42 +0200690}
691
692static void print_dtlb_cache_misses(int cpu, struct perf_evsel *evsel __used, double avg)
693{
694 double total, ratio = 0.0;
695 const char *color;
696
697 total = avg_stats(&runtime_dtlb_cache_stats[cpu]);
698
699 if (total)
700 ratio = avg / total * 100.0;
701
Namhyung Kim15e63922011-12-28 00:35:49 +0900702 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc3305252011-05-19 14:01:42 +0200703
Stephane Eranian4aa90152011-08-15 22:22:33 +0200704 fprintf(output, " # ");
705 color_fprintf(output, color, "%6.2f%%", ratio);
706 fprintf(output, " of all dTLB cache hits ");
Ingo Molnarc3305252011-05-19 14:01:42 +0200707}
708
709static void print_itlb_cache_misses(int cpu, struct perf_evsel *evsel __used, double avg)
710{
711 double total, ratio = 0.0;
712 const char *color;
713
714 total = avg_stats(&runtime_itlb_cache_stats[cpu]);
715
716 if (total)
717 ratio = avg / total * 100.0;
718
Namhyung Kim15e63922011-12-28 00:35:49 +0900719 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc3305252011-05-19 14:01:42 +0200720
Stephane Eranian4aa90152011-08-15 22:22:33 +0200721 fprintf(output, " # ");
722 color_fprintf(output, color, "%6.2f%%", ratio);
723 fprintf(output, " of all iTLB cache hits ");
Ingo Molnarc3305252011-05-19 14:01:42 +0200724}
725
726static void print_ll_cache_misses(int cpu, struct perf_evsel *evsel __used, double avg)
727{
728 double total, ratio = 0.0;
729 const char *color;
730
731 total = avg_stats(&runtime_ll_cache_stats[cpu]);
732
733 if (total)
734 ratio = avg / total * 100.0;
735
Namhyung Kim15e63922011-12-28 00:35:49 +0900736 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc3305252011-05-19 14:01:42 +0200737
Stephane Eranian4aa90152011-08-15 22:22:33 +0200738 fprintf(output, " # ");
739 color_fprintf(output, color, "%6.2f%%", ratio);
740 fprintf(output, " of all LL-cache hits ");
Ingo Molnarc3305252011-05-19 14:01:42 +0200741}
742
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200743static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200744{
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200745 double total, ratio = 0.0;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200746 char cpustr[16] = { '\0', };
Stephane Eraniand7470b62010-12-01 18:49:05 +0200747 const char *fmt;
748
749 if (csv_output)
750 fmt = "%s%.0f%s%s";
751 else if (big_num)
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200752 fmt = "%s%'18.0f%s%-25s";
Stephane Eraniand7470b62010-12-01 18:49:05 +0200753 else
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200754 fmt = "%s%18.0f%s%-25s";
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200755
756 if (no_aggr)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200757 sprintf(cpustr, "CPU%*d%s",
758 csv_output ? 0 : -4,
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200759 evsel_list->cpus->map[cpu], csv_sep);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200760 else
761 cpu = 0;
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200762
Stephane Eranian4aa90152011-08-15 22:22:33 +0200763 fprintf(output, fmt, cpustr, avg, csv_sep, event_name(evsel));
Stephane Eraniand7470b62010-12-01 18:49:05 +0200764
Stephane Eranian023695d2011-02-14 11:20:01 +0200765 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200766 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200767
Stephane Eraniand7470b62010-12-01 18:49:05 +0200768 if (csv_output)
769 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200770
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200771 if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200772 total = avg_stats(&runtime_cycles_stats[cpu]);
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200773
774 if (total)
775 ratio = avg / total;
776
Stephane Eranian4aa90152011-08-15 22:22:33 +0200777 fprintf(output, " # %5.2f insns per cycle ", ratio);
Ingo Molnar481f9882011-04-27 04:34:16 +0200778
Ingo Molnard3d1e862011-04-29 13:49:08 +0200779 total = avg_stats(&runtime_stalled_cycles_front_stats[cpu]);
780 total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[cpu]));
Ingo Molnar481f9882011-04-27 04:34:16 +0200781
782 if (total && avg) {
783 ratio = total / avg;
Stephane Eranian4aa90152011-08-15 22:22:33 +0200784 fprintf(output, "\n # %5.2f stalled cycles per insn", ratio);
Ingo Molnar481f9882011-04-27 04:34:16 +0200785 }
786
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200787 } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200788 runtime_branches_stats[cpu].n != 0) {
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200789 print_branch_misses(cpu, evsel, avg);
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200790 } else if (
791 evsel->attr.type == PERF_TYPE_HW_CACHE &&
792 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
793 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
794 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
Ingo Molnarc6264de2011-04-27 13:50:47 +0200795 runtime_l1_dcache_stats[cpu].n != 0) {
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200796 print_l1_dcache_misses(cpu, evsel, avg);
Ingo Molnarc3305252011-05-19 14:01:42 +0200797 } else if (
798 evsel->attr.type == PERF_TYPE_HW_CACHE &&
799 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
800 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
801 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
802 runtime_l1_icache_stats[cpu].n != 0) {
803 print_l1_icache_misses(cpu, evsel, avg);
804 } else if (
805 evsel->attr.type == PERF_TYPE_HW_CACHE &&
806 evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
807 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
808 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
809 runtime_dtlb_cache_stats[cpu].n != 0) {
810 print_dtlb_cache_misses(cpu, evsel, avg);
811 } else if (
812 evsel->attr.type == PERF_TYPE_HW_CACHE &&
813 evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
814 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
815 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
816 runtime_itlb_cache_stats[cpu].n != 0) {
817 print_itlb_cache_misses(cpu, evsel, avg);
818 } else if (
819 evsel->attr.type == PERF_TYPE_HW_CACHE &&
820 evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
821 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
822 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
823 runtime_ll_cache_stats[cpu].n != 0) {
824 print_ll_cache_misses(cpu, evsel, avg);
Ingo Molnard58f4c82011-04-27 03:42:18 +0200825 } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES) &&
826 runtime_cacherefs_stats[cpu].n != 0) {
827 total = avg_stats(&runtime_cacherefs_stats[cpu]);
828
829 if (total)
830 ratio = avg * 100 / total;
831
Stephane Eranian4aa90152011-08-15 22:22:33 +0200832 fprintf(output, " # %8.3f %% of all cache refs ", ratio);
Ingo Molnard58f4c82011-04-27 03:42:18 +0200833
Ingo Molnard3d1e862011-04-29 13:49:08 +0200834 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
835 print_stalled_cycles_frontend(cpu, evsel, avg);
Ingo Molnar129c04c2011-04-29 14:41:28 +0200836 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
Ingo Molnard3d1e862011-04-29 13:49:08 +0200837 print_stalled_cycles_backend(cpu, evsel, avg);
Ingo Molnar481f9882011-04-27 04:34:16 +0200838 } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
839 total = avg_stats(&runtime_nsecs_stats[cpu]);
840
841 if (total)
842 ratio = 1.0 * avg / total;
843
Stephane Eranian4aa90152011-08-15 22:22:33 +0200844 fprintf(output, " # %8.3f GHz ", ratio);
Ingo Molnar481f9882011-04-27 04:34:16 +0200845 } else if (runtime_nsecs_stats[cpu].n != 0) {
Namhyung Kim5fde2522012-02-06 16:44:44 +0900846 char unit = 'M';
847
Ingo Molnar481f9882011-04-27 04:34:16 +0200848 total = avg_stats(&runtime_nsecs_stats[cpu]);
849
850 if (total)
851 ratio = 1000.0 * avg / total;
Namhyung Kim5fde2522012-02-06 16:44:44 +0900852 if (ratio < 0.001) {
853 ratio *= 1000;
854 unit = 'K';
855 }
Ingo Molnar481f9882011-04-27 04:34:16 +0200856
Namhyung Kim5fde2522012-02-06 16:44:44 +0900857 fprintf(output, " # %8.3f %c/sec ", ratio, unit);
Ingo Molnara5d243d2011-04-27 05:39:24 +0200858 } else {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200859 fprintf(output, " ");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200860 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200861}
862
863/*
864 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200865 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +0200866 */
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200867static void print_counter_aggr(struct perf_evsel *counter)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200868{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200869 struct perf_stat *ps = counter->priv;
870 double avg = avg_stats(&ps->res_stats[0]);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200871 int scaled = counter->counts->scaled;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200872
Ingo Molnar42202dd2009-06-13 14:57:28 +0200873 if (scaled == -1) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200874 fprintf(output, "%*s%s%*s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200875 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600876 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian023695d2011-02-14 11:20:01 +0200877 csv_sep,
878 csv_output ? 0 : -24,
879 event_name(counter));
880
881 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200882 fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200883
Stephane Eranian4aa90152011-08-15 22:22:33 +0200884 fputc('\n', output);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200885 return;
886 }
887
888 if (nsec_counter(counter))
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200889 nsec_printout(-1, counter, avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200890 else
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200891 abs_printout(-1, counter, avg);
Peter Zijlstra849abde2009-09-04 18:23:38 +0200892
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700893 print_noise(counter, avg);
894
Stephane Eraniand7470b62010-12-01 18:49:05 +0200895 if (csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200896 fputc('\n', output);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200897 return;
898 }
899
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200900 if (scaled) {
901 double avg_enabled, avg_running;
902
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200903 avg_enabled = avg_stats(&ps->res_stats[1]);
904 avg_running = avg_stats(&ps->res_stats[2]);
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200905
Stephane Eranian4aa90152011-08-15 22:22:33 +0200906 fprintf(output, " [%5.2f%%]", 100 * avg_running / avg_enabled);
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200907 }
Stephane Eranian4aa90152011-08-15 22:22:33 +0200908 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200909}
910
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200911/*
912 * Print out the results of a single counter:
913 * does not use aggregated count in system-wide
914 */
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200915static void print_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200916{
917 u64 ena, run, val;
918 int cpu;
919
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200920 for (cpu = 0; cpu < evsel_list->cpus->nr; cpu++) {
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200921 val = counter->counts->cpu[cpu].val;
922 ena = counter->counts->cpu[cpu].ena;
923 run = counter->counts->cpu[cpu].run;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200924 if (run == 0 || ena == 0) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200925 fprintf(output, "CPU%*d%s%*s%s%*s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200926 csv_output ? 0 : -4,
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200927 evsel_list->cpus->map[cpu], csv_sep,
Stephane Eraniand7470b62010-12-01 18:49:05 +0200928 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600929 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
930 csv_sep,
Stephane Eranian023695d2011-02-14 11:20:01 +0200931 csv_output ? 0 : -24,
Stephane Eraniand7470b62010-12-01 18:49:05 +0200932 event_name(counter));
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200933
Stephane Eranian023695d2011-02-14 11:20:01 +0200934 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200935 fprintf(output, "%s%s",
936 csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200937
Stephane Eranian4aa90152011-08-15 22:22:33 +0200938 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200939 continue;
940 }
941
942 if (nsec_counter(counter))
943 nsec_printout(cpu, counter, val);
944 else
945 abs_printout(cpu, counter, val);
946
Stephane Eraniand7470b62010-12-01 18:49:05 +0200947 if (!csv_output) {
948 print_noise(counter, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200949
Ingo Molnarc6264de2011-04-27 13:50:47 +0200950 if (run != ena)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200951 fprintf(output, " (%.2f%%)",
952 100.0 * run / ena);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200953 }
Stephane Eranian4aa90152011-08-15 22:22:33 +0200954 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200955 }
956}
957
Ingo Molnar42202dd2009-06-13 14:57:28 +0200958static void print_stat(int argc, const char **argv)
959{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200960 struct perf_evsel *counter;
961 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200962
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200963 fflush(stdout);
964
Stephane Eraniand7470b62010-12-01 18:49:05 +0200965 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200966 fprintf(output, "\n");
967 fprintf(output, " Performance counter stats for ");
Namhyung Kimd67356e2012-05-07 14:09:03 +0900968 if (perf_target__no_task(&target)) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200969 fprintf(output, "\'%s", argv[0]);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200970 for (i = 1; i < argc; i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200971 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +0900972 } else if (target.pid)
973 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200974 else
Namhyung Kim20f946b2012-04-26 14:15:16 +0900975 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +0200976
Stephane Eranian4aa90152011-08-15 22:22:33 +0200977 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +0200978 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200979 fprintf(output, " (%d runs)", run_count);
980 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +0200981 }
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200982
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200983 if (no_aggr) {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200984 list_for_each_entry(counter, &evsel_list->entries, node)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200985 print_counter(counter);
986 } else {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200987 list_for_each_entry(counter, &evsel_list->entries, node)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200988 print_counter_aggr(counter);
989 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200990
Stephane Eraniand7470b62010-12-01 18:49:05 +0200991 if (!csv_output) {
Ingo Molnarc3305252011-05-19 14:01:42 +0200992 if (!null_run)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200993 fprintf(output, "\n");
994 fprintf(output, " %17.9f seconds time elapsed",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200995 avg_stats(&walltime_nsecs_stats)/1e9);
996 if (run_count > 1) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200997 fprintf(output, " ");
Ingo Molnarf99844c2011-04-27 05:35:39 +0200998 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
999 avg_stats(&walltime_nsecs_stats));
Stephane Eraniand7470b62010-12-01 18:49:05 +02001000 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001001 fprintf(output, "\n\n");
Ingo Molnar566747e2009-06-27 06:24:32 +02001002 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001003}
1004
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001005static volatile int signr = -1;
1006
Ingo Molnar52425192009-05-26 09:17:18 +02001007static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001008{
Zhang, Yanmin6be28502010-03-18 11:36:03 -03001009 if(child_pid == -1)
Liming Wang60666c62009-12-31 16:05:50 +08001010 done = 1;
1011
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001012 signr = signo;
1013}
1014
1015static void sig_atexit(void)
1016{
Chris Wilson933da832009-10-04 01:35:01 +01001017 if (child_pid != -1)
1018 kill(child_pid, SIGTERM);
1019
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001020 if (signr == -1)
1021 return;
1022
1023 signal(signr, SIG_DFL);
1024 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +02001025}
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001026
Ingo Molnar52425192009-05-26 09:17:18 +02001027static const char * const stat_usage[] = {
Liming Wang60666c62009-12-31 16:05:50 +08001028 "perf stat [<options>] [<command>]",
Ingo Molnar52425192009-05-26 09:17:18 +02001029 NULL
1030};
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001031
Stephane Eraniand7470b62010-12-01 18:49:05 +02001032static int stat__set_big_num(const struct option *opt __used,
1033 const char *s __used, int unset)
1034{
1035 big_num_opt = unset ? 0 : 1;
1036 return 0;
1037}
1038
Stephane Eranian4aa90152011-08-15 22:22:33 +02001039static bool append_file;
1040
Ingo Molnar52425192009-05-26 09:17:18 +02001041static const struct option options[] = {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001042 OPT_CALLBACK('e', "event", &evsel_list, "event",
Thomas Gleixner86847b62009-06-06 12:24:17 +02001043 "event selector. use 'perf list' to list available events",
Jiri Olsaf120f9d2011-07-14 11:25:32 +02001044 parse_events_option),
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +01001045 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1046 "event filter", parse_filter),
Stephane Eranian2e6cdf92010-05-12 10:40:01 +02001047 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1048 "child tasks do not inherit counters"),
Namhyung Kim20f946b2012-04-26 14:15:16 +09001049 OPT_STRING('p', "pid", &target.pid, "pid",
David Ahernb52956c2012-02-08 09:32:52 -07001050 "stat events on existing process id"),
Namhyung Kim20f946b2012-04-26 14:15:16 +09001051 OPT_STRING('t', "tid", &target.tid, "tid",
David Ahernb52956c2012-02-08 09:32:52 -07001052 "stat events on existing thread id"),
Namhyung Kim20f946b2012-04-26 14:15:16 +09001053 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +05301054 "system-wide collection from all CPUs"),
Lin Ming43bece72011-08-17 18:42:07 +08001055 OPT_BOOLEAN('g', "group", &group,
1056 "put the counters into a counter group"),
Brice Goglinb26bc5a2009-08-07 10:18:39 +02001057 OPT_BOOLEAN('c', "scale", &scale,
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +05301058 "scale/normalize counters"),
Ian Munsiec0555642010-04-13 18:37:33 +10001059 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar743ee1f2009-06-07 17:06:46 +02001060 "be more verbose (show counter open errors, etc)"),
Ingo Molnar42202dd2009-06-13 14:57:28 +02001061 OPT_INTEGER('r', "repeat", &run_count,
1062 "repeat command and print average + stddev (max: 100)"),
Ingo Molnar0cfb7a12009-06-27 06:10:30 +02001063 OPT_BOOLEAN('n', "null", &null_run,
1064 "null run - dont start any counters"),
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001065 OPT_INCR('d', "detailed", &detailed_run,
Ingo Molnarc6264de2011-04-27 13:50:47 +02001066 "detailed run - start a lot of events"),
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02001067 OPT_BOOLEAN('S', "sync", &sync_run,
1068 "call sync() before starting a run"),
Stephane Eraniand7470b62010-12-01 18:49:05 +02001069 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1070 "print large numbers with thousands\' separators",
1071 stat__set_big_num),
Namhyung Kim20f946b2012-04-26 14:15:16 +09001072 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001073 "list of cpus to monitor in system-wide"),
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001074 OPT_BOOLEAN('A', "no-aggr", &no_aggr,
1075 "disable CPU count aggregation"),
Stephane Eraniand7470b62010-12-01 18:49:05 +02001076 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1077 "print counts with custom separator"),
Stephane Eranian023695d2011-02-14 11:20:01 +02001078 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1079 "monitor event in cgroup name only",
1080 parse_cgroups),
Stephane Eranian4aa90152011-08-15 22:22:33 +02001081 OPT_STRING('o', "output", &output_name, "file",
1082 "output file name"),
1083 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
Jim Cromie56f3bae2011-09-07 17:14:00 -06001084 OPT_INTEGER(0, "log-fd", &output_fd,
1085 "log output to fd, instead of stderr"),
Ingo Molnar52425192009-05-26 09:17:18 +02001086 OPT_END()
1087};
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001088
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001089/*
1090 * Add default attributes, if there were no attributes specified or
1091 * if -d/--detailed, -d -d or -d -d -d is used:
1092 */
1093static int add_default_attributes(void)
1094{
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001095 /* Set attrs if no event is selected and !null_run: */
1096 if (null_run)
1097 return 0;
1098
1099 if (!evsel_list->nr_entries) {
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001100 if (perf_evlist__add_attrs_array(evsel_list, default_attrs) < 0)
1101 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001102 }
1103
1104 /* Detailed events get appended to the event list: */
1105
1106 if (detailed_run < 1)
1107 return 0;
1108
1109 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001110 if (perf_evlist__add_attrs_array(evsel_list, detailed_attrs) < 0)
1111 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001112
1113 if (detailed_run < 2)
1114 return 0;
1115
1116 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001117 if (perf_evlist__add_attrs_array(evsel_list, very_detailed_attrs) < 0)
1118 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001119
1120 if (detailed_run < 3)
1121 return 0;
1122
1123 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001124 return perf_evlist__add_attrs_array(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001125}
1126
Ingo Molnarf37a2912009-07-01 12:37:06 +02001127int cmd_stat(int argc, const char **argv, const char *prefix __used)
Ingo Molnar52425192009-05-26 09:17:18 +02001128{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001129 struct perf_evsel *pos;
1130 int status = -ENOMEM;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001131 const char *mode;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001132
Stephane Eranian5af52b52010-05-18 15:00:01 +02001133 setlocale(LC_ALL, "");
1134
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001135 evsel_list = perf_evlist__new(NULL, NULL);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001136 if (evsel_list == NULL)
1137 return -ENOMEM;
1138
Anton Blancharda0541232009-07-22 23:04:12 +10001139 argc = parse_options(argc, argv, options, stat_usage,
1140 PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001141
Stephane Eranian4aa90152011-08-15 22:22:33 +02001142 output = stderr;
1143 if (output_name && strcmp(output_name, "-"))
1144 output = NULL;
1145
Jim Cromie56f3bae2011-09-07 17:14:00 -06001146 if (output_name && output_fd) {
1147 fprintf(stderr, "cannot use both --output and --log-fd\n");
1148 usage_with_options(stat_usage, options);
1149 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001150 if (!output) {
1151 struct timespec tm;
1152 mode = append_file ? "a" : "w";
1153
1154 output = fopen(output_name, mode);
1155 if (!output) {
1156 perror("failed to create output file");
1157 exit(-1);
1158 }
1159 clock_gettime(CLOCK_REALTIME, &tm);
1160 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Jim Cromie56f3bae2011-09-07 17:14:00 -06001161 } else if (output_fd != 2) {
1162 mode = append_file ? "a" : "w";
1163 output = fdopen(output_fd, mode);
1164 if (!output) {
1165 perror("Failed opening logfd");
1166 return -errno;
1167 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001168 }
1169
Jim Cromied4ffd042011-09-07 17:14:03 -06001170 if (csv_sep) {
Stephane Eraniand7470b62010-12-01 18:49:05 +02001171 csv_output = true;
Jim Cromied4ffd042011-09-07 17:14:03 -06001172 if (!strcmp(csv_sep, "\\t"))
1173 csv_sep = "\t";
1174 } else
Stephane Eraniand7470b62010-12-01 18:49:05 +02001175 csv_sep = DEFAULT_SEPARATOR;
1176
1177 /*
1178 * let the spreadsheet do the pretty-printing
1179 */
1180 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06001181 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02001182 if (big_num_opt == 1) {
1183 fprintf(stderr, "-B option not supported with -x\n");
1184 usage_with_options(stat_usage, options);
1185 } else /* Nope, so disable big number formatting */
1186 big_num = false;
1187 } else if (big_num_opt == 0) /* User passed --no-big-num */
1188 big_num = false;
1189
Namhyung Kimd67356e2012-05-07 14:09:03 +09001190 if (!argc && perf_target__no_task(&target))
Ingo Molnar52425192009-05-26 09:17:18 +02001191 usage_with_options(stat_usage, options);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +02001192 if (run_count <= 0)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001193 usage_with_options(stat_usage, options);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001194
Stephane Eranian023695d2011-02-14 11:20:01 +02001195 /* no_aggr, cgroup are for system-wide only */
Namhyung Kimd67356e2012-05-07 14:09:03 +09001196 if ((no_aggr || nr_cgroups) && perf_target__no_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02001197 fprintf(stderr, "both cgroup and no-aggregation "
1198 "modes only available in system-wide mode\n");
1199
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001200 usage_with_options(stat_usage, options);
Stephane Eranian023695d2011-02-14 11:20:01 +02001201 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001202
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001203 if (add_default_attributes())
1204 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001205
Namhyung Kim4bd0f2d2012-04-26 14:15:18 +09001206 perf_target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02001207
Namhyung Kim20f946b2012-04-26 14:15:16 +09001208 evsel_list->threads = thread_map__new_str(target.pid,
1209 target.tid, UINT_MAX);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001210 if (evsel_list->threads == NULL) {
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02001211 pr_err("Problems finding threads of monitor\n");
1212 usage_with_options(stat_usage, options);
1213 }
1214
Namhyung Kim20f946b2012-04-26 14:15:16 +09001215 if (target.system_wide)
1216 evsel_list->cpus = cpu_map__new(target.cpu_list);
Paul Mackerrasa12b51c2010-03-10 20:36:09 +11001217 else
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001218 evsel_list->cpus = cpu_map__dummy_new();
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001219
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001220 if (evsel_list->cpus == NULL) {
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02001221 perror("failed to parse CPUs map");
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001222 usage_with_options(stat_usage, options);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02001223 return -1;
1224 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001225
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001226 list_for_each_entry(pos, &evsel_list->entries, node) {
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001227 if (perf_evsel__alloc_stat_priv(pos) < 0 ||
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -02001228 perf_evsel__alloc_counts(pos, evsel_list->cpus->nr) < 0)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001229 goto out_free_fd;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001230 }
1231
Ingo Molnar58d7e992009-05-15 11:03:23 +02001232 /*
1233 * We dont want to block the signals - that would cause
1234 * child tasks to inherit that and Ctrl-C would not work.
1235 * What we want is for Ctrl-C to work in the exec()-ed
1236 * task, but being ignored by perf stat itself:
1237 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001238 atexit(sig_atexit);
Ingo Molnar58d7e992009-05-15 11:03:23 +02001239 signal(SIGINT, skip_signal);
1240 signal(SIGALRM, skip_signal);
1241 signal(SIGABRT, skip_signal);
1242
Ingo Molnar42202dd2009-06-13 14:57:28 +02001243 status = 0;
1244 for (run_idx = 0; run_idx < run_count; run_idx++) {
1245 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001246 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
1247 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02001248
1249 if (sync_run)
1250 sync();
1251
Ingo Molnar42202dd2009-06-13 14:57:28 +02001252 status = run_perf_stat(argc, argv);
1253 }
1254
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -03001255 if (status != -1)
1256 print_stat(argc, argv);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001257out_free_fd:
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001258 list_for_each_entry(pos, &evsel_list->entries, node)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001259 perf_evsel__free_stat_priv(pos);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001260 perf_evlist__delete_maps(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02001261out:
1262 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001263 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001264}