blob: 557081e0c6d9480ed9340b5bf7d270961f03614d [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"
Xiao Guangrong0007ece2012-09-17 16:31:14 +080054#include "util/stat.h"
Liming Wang60666c62009-12-31 16:05:50 +080055#include "util/header.h"
Paul Mackerrasa12b51c2010-03-10 20:36:09 +110056#include "util/cpumap.h"
Zhang, Yanmind6d901c2010-03-18 11:36:05 -030057#include "util/thread.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020058#include "util/thread_map.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020059
Peter Zijlstra1f16c572012-10-23 13:40:14 +020060#include <stdlib.h>
Ingo Molnarddcacfa2009-04-20 15:37:32 +020061#include <sys/prctl.h>
Stephane Eranian5af52b52010-05-18 15:00:01 +020062#include <locale.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020063
Stephane Eraniand7470b62010-12-01 18:49:05 +020064#define DEFAULT_SEPARATOR " "
David Ahern2cee77c2011-05-30 08:55:59 -060065#define CNTR_NOT_SUPPORTED "<not supported>"
66#define CNTR_NOT_COUNTED "<not counted>"
Stephane Eraniand7470b62010-12-01 18:49:05 +020067
Robert Richter666e6d42012-04-05 18:26:27 +020068static struct perf_evlist *evsel_list;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020069
Namhyung Kim77a6f012012-05-07 14:09:04 +090070static struct perf_target target = {
71 .uid = UINT_MAX,
72};
Jaswinder Singh Rajput3d632592009-06-24 18:19:34 +053073
74static int run_count = 1;
Stephane Eranian2e6cdf92010-05-12 10:40:01 +020075static bool no_inherit = false;
Ian Munsiec0555642010-04-13 18:37:33 +100076static bool scale = true;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +020077static bool no_aggr = false;
Chris Wilson933da832009-10-04 01:35:01 +010078static pid_t child_pid = -1;
Ian Munsiec0555642010-04-13 18:37:33 +100079static bool null_run = false;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +020080static int detailed_run = 0;
Arnaldo Carvalho de Melo201e0b02010-12-01 17:53:27 -020081static bool big_num = true;
Stephane Eraniand7470b62010-12-01 18:49:05 +020082static int big_num_opt = -1;
Stephane Eraniand7470b62010-12-01 18:49:05 +020083static const char *csv_sep = NULL;
84static bool csv_output = false;
Lin Ming43bece72011-08-17 18:42:07 +080085static bool group = false;
Stephane Eranian4aa90152011-08-15 22:22:33 +020086static FILE *output = NULL;
Peter Zijlstra1f16c572012-10-23 13:40:14 +020087static const char *pre_cmd = NULL;
88static const char *post_cmd = NULL;
89static bool sync_run = false;
Stephane Eranian5af52b52010-05-18 15:00:01 +020090
Liming Wang60666c62009-12-31 16:05:50 +080091static volatile int done = 0;
92
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020093struct perf_stat {
94 struct stats res_stats[3];
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020095};
96
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020097static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020098{
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020099 evsel->priv = zalloc(sizeof(struct perf_stat));
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200100 return evsel->priv == NULL ? -ENOMEM : 0;
101}
102
103static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
104{
105 free(evsel->priv);
106 evsel->priv = NULL;
107}
108
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800109static inline struct cpu_map *perf_evsel__cpus(struct perf_evsel *evsel)
110{
111 return (evsel->cpus && !target.cpu_list) ? evsel->cpus : evsel_list->cpus;
112}
113
114static inline int perf_evsel__nr_cpus(struct perf_evsel *evsel)
115{
116 return perf_evsel__cpus(evsel)->nr;
117}
118
Robert Richter666e6d42012-04-05 18:26:27 +0200119static struct stats runtime_nsecs_stats[MAX_NR_CPUS];
120static struct stats runtime_cycles_stats[MAX_NR_CPUS];
121static struct stats runtime_stalled_cycles_front_stats[MAX_NR_CPUS];
122static struct stats runtime_stalled_cycles_back_stats[MAX_NR_CPUS];
123static struct stats runtime_branches_stats[MAX_NR_CPUS];
124static struct stats runtime_cacherefs_stats[MAX_NR_CPUS];
125static struct stats runtime_l1_dcache_stats[MAX_NR_CPUS];
126static struct stats runtime_l1_icache_stats[MAX_NR_CPUS];
127static struct stats runtime_ll_cache_stats[MAX_NR_CPUS];
128static struct stats runtime_itlb_cache_stats[MAX_NR_CPUS];
129static struct stats runtime_dtlb_cache_stats[MAX_NR_CPUS];
130static struct stats walltime_nsecs_stats;
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200131
Jiri Olsacac21422012-11-12 18:34:00 +0100132static int create_perf_stat_counter(struct perf_evsel *evsel)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200133{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200134 struct perf_event_attr *attr = &evsel->attr;
Stephane Eranian5622c072012-04-27 14:45:38 +0200135 bool exclude_guest_missing = false;
136 int ret;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200137
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200138 if (scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200139 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
140 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200141
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200142 attr->inherit = !no_inherit;
Arnaldo Carvalho de Melo5d2cd902011-04-14 11:20:14 -0300143
Stephane Eranian5622c072012-04-27 14:45:38 +0200144retry:
145 if (exclude_guest_missing)
146 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
147
Arnaldo Carvalho de Melo16ee6572012-05-18 13:13:33 -0300148 if (perf_target__has_cpu(&target)) {
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800149 ret = perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
Stephane Eranian5622c072012-04-27 14:45:38 +0200150 if (ret)
151 goto check_ret;
152 return 0;
153 }
154
Jiri Olsacac21422012-11-12 18:34:00 +0100155 if (!perf_target__has_task(&target) && (!evsel->leader)) {
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200156 attr->disabled = 1;
157 attr->enable_on_exec = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200158 }
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300159
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200160 ret = perf_evsel__open_per_thread(evsel, evsel_list->threads);
Stephane Eranian5622c072012-04-27 14:45:38 +0200161 if (!ret)
162 return 0;
163 /* fall through */
164check_ret:
165 if (ret && errno == EINVAL) {
166 if (!exclude_guest_missing &&
167 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
168 pr_debug("Old kernel, cannot exclude "
169 "guest or host samples.\n");
170 exclude_guest_missing = true;
171 goto retry;
172 }
173 }
174 return ret;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200175}
176
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200177/*
178 * Does the counter have nsecs as a unit?
179 */
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200180static inline int nsec_counter(struct perf_evsel *evsel)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200181{
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200182 if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
183 perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200184 return 1;
185
186 return 0;
187}
188
189/*
Ingo Molnardcd99362011-04-27 04:36:37 +0200190 * Update various tracking values we maintain to print
191 * more semantic information such as miss/hit ratios,
192 * instruction rates, etc:
193 */
194static void update_shadow_stats(struct perf_evsel *counter, u64 *count)
195{
196 if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK))
197 update_stats(&runtime_nsecs_stats[0], count[0]);
198 else if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
199 update_stats(&runtime_cycles_stats[0], count[0]);
Ingo Molnard3d1e862011-04-29 13:49:08 +0200200 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_FRONTEND))
201 update_stats(&runtime_stalled_cycles_front_stats[0], count[0]);
Ingo Molnar129c04c2011-04-29 14:41:28 +0200202 else if (perf_evsel__match(counter, HARDWARE, HW_STALLED_CYCLES_BACKEND))
Ingo Molnard3d1e862011-04-29 13:49:08 +0200203 update_stats(&runtime_stalled_cycles_back_stats[0], count[0]);
Ingo Molnardcd99362011-04-27 04:36:37 +0200204 else if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
205 update_stats(&runtime_branches_stats[0], count[0]);
206 else if (perf_evsel__match(counter, HARDWARE, HW_CACHE_REFERENCES))
207 update_stats(&runtime_cacherefs_stats[0], count[0]);
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200208 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1D))
209 update_stats(&runtime_l1_dcache_stats[0], count[0]);
Ingo Molnarc33052572011-05-19 14:01:42 +0200210 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_L1I))
211 update_stats(&runtime_l1_icache_stats[0], count[0]);
212 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_LL))
213 update_stats(&runtime_ll_cache_stats[0], count[0]);
214 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_DTLB))
215 update_stats(&runtime_dtlb_cache_stats[0], count[0]);
216 else if (perf_evsel__match(counter, HW_CACHE, HW_CACHE_ITLB))
217 update_stats(&runtime_itlb_cache_stats[0], count[0]);
Ingo Molnardcd99362011-04-27 04:36:37 +0200218}
219
220/*
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200221 * Read out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200222 * aggregate counts across CPUs in system-wide mode
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200223 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200224static int read_counter_aggr(struct perf_evsel *counter)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200225{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200226 struct perf_stat *ps = counter->priv;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200227 u64 *count = counter->counts->aggr.values;
228 int i;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200229
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800230 if (__perf_evsel__read(counter, perf_evsel__nr_cpus(counter),
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200231 evsel_list->threads->nr, scale) < 0)
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200232 return -1;
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200233
234 for (i = 0; i < 3; i++)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200235 update_stats(&ps->res_stats[i], count[i]);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200236
237 if (verbose) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200238 fprintf(output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300239 perf_evsel__name(counter), count[0], count[1], count[2]);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200240 }
241
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200242 /*
243 * Save the full runtime - to allow normalization during printout:
244 */
Ingo Molnardcd99362011-04-27 04:36:37 +0200245 update_shadow_stats(counter, count);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200246
247 return 0;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200248}
249
250/*
251 * Read out the results of a single counter:
252 * do not aggregate counts across CPUs in system-wide mode
253 */
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200254static int read_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200255{
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200256 u64 *count;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200257 int cpu;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200258
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800259 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200260 if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0)
261 return -1;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200262
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200263 count = counter->counts->cpu[cpu].values;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200264
Ingo Molnardcd99362011-04-27 04:36:37 +0200265 update_shadow_stats(counter, count);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200266 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200267
268 return 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200269}
270
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200271static int __run_perf_stat(int argc __maybe_unused, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200272{
273 unsigned long long t0, t1;
Jiri Olsacac21422012-11-12 18:34:00 +0100274 struct perf_evsel *counter;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200275 int status = 0;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000276 int child_ready_pipe[2], go_pipe[2];
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300277 const bool forks = (argc > 0);
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000278 char buf;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200279
Liming Wang60666c62009-12-31 16:05:50 +0800280 if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000281 perror("failed to create pipes");
David Ahernfceda7f2012-08-26 12:24:44 -0600282 return -1;
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000283 }
284
Liming Wang60666c62009-12-31 16:05:50 +0800285 if (forks) {
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300286 if ((child_pid = fork()) < 0)
Liming Wang60666c62009-12-31 16:05:50 +0800287 perror("failed to fork");
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000288
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300289 if (!child_pid) {
Liming Wang60666c62009-12-31 16:05:50 +0800290 close(child_ready_pipe[0]);
291 close(go_pipe[1]);
292 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
293
294 /*
295 * Do a dummy execvp to get the PLT entry resolved,
296 * so we avoid the resolver overhead on the real
297 * execvp call.
298 */
299 execvp("", (char **)argv);
300
301 /*
302 * Tell the parent we're ready to go
303 */
304 close(child_ready_pipe[1]);
305
306 /*
307 * Wait until the parent tells us to go.
308 */
309 if (read(go_pipe[0], &buf, 1) == -1)
310 perror("unable to read pipe");
311
312 execvp(argv[0], (char **)argv);
313
314 perror(argv[0]);
315 exit(-1);
316 }
317
Namhyung Kimd67356e2012-05-07 14:09:03 +0900318 if (perf_target__none(&target))
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200319 evsel_list->threads->map[0] = child_pid;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -0300320
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000321 /*
Liming Wang60666c62009-12-31 16:05:50 +0800322 * Wait for the child to be ready to exec.
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000323 */
324 close(child_ready_pipe[1]);
Liming Wang60666c62009-12-31 16:05:50 +0800325 close(go_pipe[0]);
326 if (read(child_ready_pipe[0], &buf, 1) == -1)
Frederic Weisbeckera92bef02009-07-01 21:02:10 +0200327 perror("unable to read pipe");
Liming Wang60666c62009-12-31 16:05:50 +0800328 close(child_ready_pipe[0]);
Paul Mackerras051ae7f2009-06-29 21:13:21 +1000329 }
330
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200331 if (group)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300332 perf_evlist__set_leader(evsel_list);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200333
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200334 list_for_each_entry(counter, &evsel_list->entries, node) {
Jiri Olsacac21422012-11-12 18:34:00 +0100335 if (create_perf_stat_counter(counter) < 0) {
David Ahern979987a2012-05-08 09:29:16 -0600336 /*
337 * PPC returns ENXIO for HW counters until 2.6.37
338 * (behavior changed with commit b0a873e).
339 */
Anton Blanchard38f6ae12011-12-02 09:38:33 +1100340 if (errno == EINVAL || errno == ENOSYS ||
David Ahern979987a2012-05-08 09:29:16 -0600341 errno == ENOENT || errno == EOPNOTSUPP ||
342 errno == ENXIO) {
David Ahernc63ca0c2011-04-29 16:04:15 -0600343 if (verbose)
344 ui__warning("%s event is not supported by the kernel.\n",
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300345 perf_evsel__name(counter));
David Ahern2cee77c2011-05-30 08:55:59 -0600346 counter->supported = false;
Ingo Molnarede70292011-04-28 08:48:42 +0200347 continue;
David Ahernc63ca0c2011-04-29 16:04:15 -0600348 }
Ingo Molnarede70292011-04-28 08:48:42 +0200349
350 if (errno == EPERM || errno == EACCES) {
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200351 error("You may not have permission to collect %sstats.\n"
352 "\t Consider tweaking"
353 " /proc/sys/kernel/perf_event_paranoid or running as root.",
Namhyung Kim20f946b2012-04-26 14:15:16 +0900354 target.system_wide ? "system-wide " : "");
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200355 } else {
356 error("open_counter returned with %d (%s). "
357 "/bin/dmesg may provide additional information.\n",
358 errno, strerror(errno));
359 }
360 if (child_pid != -1)
361 kill(child_pid, SIGTERM);
David Ahernfceda7f2012-08-26 12:24:44 -0600362
363 pr_err("Not all events could be opened.\n");
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200364 return -1;
365 }
David Ahern2cee77c2011-05-30 08:55:59 -0600366 counter->supported = true;
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -0300367 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200368
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300369 if (perf_evlist__apply_filters(evsel_list)) {
Frederic Weisbeckercfd748a2011-03-14 16:40:30 +0100370 error("failed to set filter with %d (%s)\n", errno,
371 strerror(errno));
372 return -1;
373 }
374
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200375 /*
376 * Enable counters and exec the command:
377 */
378 t0 = rdclock();
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200379
Liming Wang60666c62009-12-31 16:05:50 +0800380 if (forks) {
381 close(go_pipe[1]);
382 wait(&status);
Andi Kleen33e49ea2011-09-15 14:31:40 -0700383 if (WIFSIGNALED(status))
384 psignal(WTERMSIG(status), argv[0]);
Liming Wang60666c62009-12-31 16:05:50 +0800385 } else {
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300386 while(!done) sleep(1);
Liming Wang60666c62009-12-31 16:05:50 +0800387 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200388
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200389 t1 = rdclock();
390
Peter Zijlstra9e9772c2009-09-04 15:36:08 +0200391 update_stats(&walltime_nsecs_stats, t1 - t0);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200392
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200393 if (no_aggr) {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200394 list_for_each_entry(counter, &evsel_list->entries, node) {
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200395 read_counter(counter);
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800396 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter), 1);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200397 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200398 } else {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200399 list_for_each_entry(counter, &evsel_list->entries, node) {
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200400 read_counter_aggr(counter);
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800401 perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter),
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200402 evsel_list->threads->nr);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200403 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200404 }
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200405
Ingo Molnar42202dd2009-06-13 14:57:28 +0200406 return WEXITSTATUS(status);
407}
408
Peter Zijlstra1f16c572012-10-23 13:40:14 +0200409static int run_perf_stat(int argc __maybe_unused, const char **argv)
410{
411 int ret;
412
413 if (pre_cmd) {
414 ret = system(pre_cmd);
415 if (ret)
416 return ret;
417 }
418
419 if (sync_run)
420 sync();
421
422 ret = __run_perf_stat(argc, argv);
423 if (ret)
424 return ret;
425
426 if (post_cmd) {
427 ret = system(post_cmd);
428 if (ret)
429 return ret;
430 }
431
432 return ret;
433}
434
Ingo Molnarf99844c2011-04-27 05:35:39 +0200435static void print_noise_pct(double total, double avg)
436{
Xiao Guangrong0007ece2012-09-17 16:31:14 +0800437 double pct = rel_stddev_stats(total, avg);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200438
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700439 if (csv_output)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200440 fprintf(output, "%s%.2f%%", csv_sep, pct);
Jim Cromiea1bca6c2011-09-07 17:14:02 -0600441 else if (pct)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200442 fprintf(output, " ( +-%6.2f%% )", pct);
Ingo Molnarf99844c2011-04-27 05:35:39 +0200443}
444
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200445static void print_noise(struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200446{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200447 struct perf_stat *ps;
448
Peter Zijlstra849abde2009-09-04 18:23:38 +0200449 if (run_count == 1)
450 return;
451
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200452 ps = evsel->priv;
Ingo Molnarf99844c2011-04-27 05:35:39 +0200453 print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200454}
455
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200456static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200457{
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200458 double msecs = avg / 1e6;
Stephane Eraniand7470b62010-12-01 18:49:05 +0200459 char cpustr[16] = { '\0', };
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200460 const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-25s";
Ingo Molnar42202dd2009-06-13 14:57:28 +0200461
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200462 if (no_aggr)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200463 sprintf(cpustr, "CPU%*d%s",
464 csv_output ? 0 : -4,
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800465 perf_evsel__cpus(evsel)->map[cpu], csv_sep);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200466
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300467 fprintf(output, fmt, cpustr, msecs, csv_sep, perf_evsel__name(evsel));
Stephane Eraniand7470b62010-12-01 18:49:05 +0200468
Stephane Eranian023695d2011-02-14 11:20:01 +0200469 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200470 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200471
Stephane Eraniand7470b62010-12-01 18:49:05 +0200472 if (csv_output)
473 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200474
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200475 if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
Stephane Eranian4aa90152011-08-15 22:22:33 +0200476 fprintf(output, " # %8.3f CPUs utilized ",
477 avg / avg_stats(&walltime_nsecs_stats));
Namhyung Kim9dac6a22012-02-06 16:44:45 +0900478 else
479 fprintf(output, " ");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200480}
481
Namhyung Kim15e63922011-12-28 00:35:49 +0900482/* used for get_ratio_color() */
483enum grc_type {
484 GRC_STALLED_CYCLES_FE,
485 GRC_STALLED_CYCLES_BE,
486 GRC_CACHE_MISSES,
487 GRC_MAX_NR
488};
489
490static const char *get_ratio_color(enum grc_type type, double ratio)
491{
492 static const double grc_table[GRC_MAX_NR][3] = {
493 [GRC_STALLED_CYCLES_FE] = { 50.0, 30.0, 10.0 },
494 [GRC_STALLED_CYCLES_BE] = { 75.0, 50.0, 20.0 },
495 [GRC_CACHE_MISSES] = { 20.0, 10.0, 5.0 },
496 };
497 const char *color = PERF_COLOR_NORMAL;
498
499 if (ratio > grc_table[type][0])
500 color = PERF_COLOR_RED;
501 else if (ratio > grc_table[type][1])
502 color = PERF_COLOR_MAGENTA;
503 else if (ratio > grc_table[type][2])
504 color = PERF_COLOR_YELLOW;
505
506 return color;
507}
508
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300509static void print_stalled_cycles_frontend(int cpu,
510 struct perf_evsel *evsel
511 __maybe_unused, double avg)
Ingo Molnard3d1e862011-04-29 13:49:08 +0200512{
513 double total, ratio = 0.0;
514 const char *color;
515
516 total = avg_stats(&runtime_cycles_stats[cpu]);
517
518 if (total)
519 ratio = avg / total * 100.0;
520
Namhyung Kim15e63922011-12-28 00:35:49 +0900521 color = get_ratio_color(GRC_STALLED_CYCLES_FE, ratio);
Ingo Molnard3d1e862011-04-29 13:49:08 +0200522
Stephane Eranian4aa90152011-08-15 22:22:33 +0200523 fprintf(output, " # ");
524 color_fprintf(output, color, "%6.2f%%", ratio);
525 fprintf(output, " frontend cycles idle ");
Ingo Molnard3d1e862011-04-29 13:49:08 +0200526}
527
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300528static void print_stalled_cycles_backend(int cpu,
529 struct perf_evsel *evsel
530 __maybe_unused, double avg)
Ingo Molnara5d243d2011-04-27 05:39:24 +0200531{
532 double total, ratio = 0.0;
533 const char *color;
534
535 total = avg_stats(&runtime_cycles_stats[cpu]);
536
537 if (total)
538 ratio = avg / total * 100.0;
539
Namhyung Kim15e63922011-12-28 00:35:49 +0900540 color = get_ratio_color(GRC_STALLED_CYCLES_BE, ratio);
Ingo Molnara5d243d2011-04-27 05:39:24 +0200541
Stephane Eranian4aa90152011-08-15 22:22:33 +0200542 fprintf(output, " # ");
543 color_fprintf(output, color, "%6.2f%%", ratio);
544 fprintf(output, " backend cycles idle ");
Ingo Molnara5d243d2011-04-27 05:39:24 +0200545}
546
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300547static void print_branch_misses(int cpu,
548 struct perf_evsel *evsel __maybe_unused,
549 double avg)
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200550{
551 double total, ratio = 0.0;
552 const char *color;
553
554 total = avg_stats(&runtime_branches_stats[cpu]);
555
556 if (total)
557 ratio = avg / total * 100.0;
558
Namhyung Kim15e63922011-12-28 00:35:49 +0900559 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200560
Stephane Eranian4aa90152011-08-15 22:22:33 +0200561 fprintf(output, " # ");
562 color_fprintf(output, color, "%6.2f%%", ratio);
563 fprintf(output, " of all branches ");
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200564}
565
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300566static void print_l1_dcache_misses(int cpu,
567 struct perf_evsel *evsel __maybe_unused,
568 double avg)
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200569{
570 double total, ratio = 0.0;
571 const char *color;
572
573 total = avg_stats(&runtime_l1_dcache_stats[cpu]);
574
575 if (total)
576 ratio = avg / total * 100.0;
577
Namhyung Kim15e63922011-12-28 00:35:49 +0900578 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200579
Stephane Eranian4aa90152011-08-15 22:22:33 +0200580 fprintf(output, " # ");
581 color_fprintf(output, color, "%6.2f%%", ratio);
582 fprintf(output, " of all L1-dcache hits ");
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200583}
584
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300585static void print_l1_icache_misses(int cpu,
586 struct perf_evsel *evsel __maybe_unused,
587 double avg)
Ingo Molnarc33052572011-05-19 14:01:42 +0200588{
589 double total, ratio = 0.0;
590 const char *color;
591
592 total = avg_stats(&runtime_l1_icache_stats[cpu]);
593
594 if (total)
595 ratio = avg / total * 100.0;
596
Namhyung Kim15e63922011-12-28 00:35:49 +0900597 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc33052572011-05-19 14:01:42 +0200598
Stephane Eranian4aa90152011-08-15 22:22:33 +0200599 fprintf(output, " # ");
600 color_fprintf(output, color, "%6.2f%%", ratio);
601 fprintf(output, " of all L1-icache hits ");
Ingo Molnarc33052572011-05-19 14:01:42 +0200602}
603
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300604static void print_dtlb_cache_misses(int cpu,
605 struct perf_evsel *evsel __maybe_unused,
606 double avg)
Ingo Molnarc33052572011-05-19 14:01:42 +0200607{
608 double total, ratio = 0.0;
609 const char *color;
610
611 total = avg_stats(&runtime_dtlb_cache_stats[cpu]);
612
613 if (total)
614 ratio = avg / total * 100.0;
615
Namhyung Kim15e63922011-12-28 00:35:49 +0900616 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc33052572011-05-19 14:01:42 +0200617
Stephane Eranian4aa90152011-08-15 22:22:33 +0200618 fprintf(output, " # ");
619 color_fprintf(output, color, "%6.2f%%", ratio);
620 fprintf(output, " of all dTLB cache hits ");
Ingo Molnarc33052572011-05-19 14:01:42 +0200621}
622
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300623static void print_itlb_cache_misses(int cpu,
624 struct perf_evsel *evsel __maybe_unused,
625 double avg)
Ingo Molnarc33052572011-05-19 14:01:42 +0200626{
627 double total, ratio = 0.0;
628 const char *color;
629
630 total = avg_stats(&runtime_itlb_cache_stats[cpu]);
631
632 if (total)
633 ratio = avg / total * 100.0;
634
Namhyung Kim15e63922011-12-28 00:35:49 +0900635 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc33052572011-05-19 14:01:42 +0200636
Stephane Eranian4aa90152011-08-15 22:22:33 +0200637 fprintf(output, " # ");
638 color_fprintf(output, color, "%6.2f%%", ratio);
639 fprintf(output, " of all iTLB cache hits ");
Ingo Molnarc33052572011-05-19 14:01:42 +0200640}
641
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300642static void print_ll_cache_misses(int cpu,
643 struct perf_evsel *evsel __maybe_unused,
644 double avg)
Ingo Molnarc33052572011-05-19 14:01:42 +0200645{
646 double total, ratio = 0.0;
647 const char *color;
648
649 total = avg_stats(&runtime_ll_cache_stats[cpu]);
650
651 if (total)
652 ratio = avg / total * 100.0;
653
Namhyung Kim15e63922011-12-28 00:35:49 +0900654 color = get_ratio_color(GRC_CACHE_MISSES, ratio);
Ingo Molnarc33052572011-05-19 14:01:42 +0200655
Stephane Eranian4aa90152011-08-15 22:22:33 +0200656 fprintf(output, " # ");
657 color_fprintf(output, color, "%6.2f%%", ratio);
658 fprintf(output, " of all LL-cache hits ");
Ingo Molnarc33052572011-05-19 14:01:42 +0200659}
660
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200661static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200662{
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200663 double total, ratio = 0.0;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200664 char cpustr[16] = { '\0', };
Stephane Eraniand7470b62010-12-01 18:49:05 +0200665 const char *fmt;
666
667 if (csv_output)
668 fmt = "%s%.0f%s%s";
669 else if (big_num)
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200670 fmt = "%s%'18.0f%s%-25s";
Stephane Eraniand7470b62010-12-01 18:49:05 +0200671 else
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200672 fmt = "%s%18.0f%s%-25s";
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200673
674 if (no_aggr)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200675 sprintf(cpustr, "CPU%*d%s",
676 csv_output ? 0 : -4,
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800677 perf_evsel__cpus(evsel)->map[cpu], csv_sep);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200678 else
679 cpu = 0;
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200680
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300681 fprintf(output, fmt, cpustr, avg, csv_sep, perf_evsel__name(evsel));
Stephane Eraniand7470b62010-12-01 18:49:05 +0200682
Stephane Eranian023695d2011-02-14 11:20:01 +0200683 if (evsel->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200684 fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200685
Stephane Eraniand7470b62010-12-01 18:49:05 +0200686 if (csv_output)
687 return;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200688
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200689 if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200690 total = avg_stats(&runtime_cycles_stats[cpu]);
Ingo Molnarc7f7fea2009-09-22 14:53:51 +0200691
692 if (total)
693 ratio = avg / total;
694
Stephane Eranian4aa90152011-08-15 22:22:33 +0200695 fprintf(output, " # %5.2f insns per cycle ", ratio);
Ingo Molnar481f9882011-04-27 04:34:16 +0200696
Ingo Molnard3d1e862011-04-29 13:49:08 +0200697 total = avg_stats(&runtime_stalled_cycles_front_stats[cpu]);
698 total = max(total, avg_stats(&runtime_stalled_cycles_back_stats[cpu]));
Ingo Molnar481f9882011-04-27 04:34:16 +0200699
700 if (total && avg) {
701 ratio = total / avg;
Stephane Eranian4aa90152011-08-15 22:22:33 +0200702 fprintf(output, "\n # %5.2f stalled cycles per insn", ratio);
Ingo Molnar481f9882011-04-27 04:34:16 +0200703 }
704
Arnaldo Carvalho de Melodaec78a2011-01-03 16:49:44 -0200705 } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200706 runtime_branches_stats[cpu].n != 0) {
Ingo Molnarc78df6c2011-04-27 12:16:10 +0200707 print_branch_misses(cpu, evsel, avg);
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200708 } else if (
709 evsel->attr.type == PERF_TYPE_HW_CACHE &&
710 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1D |
711 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
712 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
Ingo Molnarc6264de2011-04-27 13:50:47 +0200713 runtime_l1_dcache_stats[cpu].n != 0) {
Ingo Molnar8bb6c792011-04-27 13:25:24 +0200714 print_l1_dcache_misses(cpu, evsel, avg);
Ingo Molnarc33052572011-05-19 14:01:42 +0200715 } else if (
716 evsel->attr.type == PERF_TYPE_HW_CACHE &&
717 evsel->attr.config == ( PERF_COUNT_HW_CACHE_L1I |
718 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
719 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
720 runtime_l1_icache_stats[cpu].n != 0) {
721 print_l1_icache_misses(cpu, evsel, avg);
722 } else if (
723 evsel->attr.type == PERF_TYPE_HW_CACHE &&
724 evsel->attr.config == ( PERF_COUNT_HW_CACHE_DTLB |
725 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
726 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
727 runtime_dtlb_cache_stats[cpu].n != 0) {
728 print_dtlb_cache_misses(cpu, evsel, avg);
729 } else if (
730 evsel->attr.type == PERF_TYPE_HW_CACHE &&
731 evsel->attr.config == ( PERF_COUNT_HW_CACHE_ITLB |
732 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
733 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
734 runtime_itlb_cache_stats[cpu].n != 0) {
735 print_itlb_cache_misses(cpu, evsel, avg);
736 } else if (
737 evsel->attr.type == PERF_TYPE_HW_CACHE &&
738 evsel->attr.config == ( PERF_COUNT_HW_CACHE_LL |
739 ((PERF_COUNT_HW_CACHE_OP_READ) << 8) |
740 ((PERF_COUNT_HW_CACHE_RESULT_MISS) << 16)) &&
741 runtime_ll_cache_stats[cpu].n != 0) {
742 print_ll_cache_misses(cpu, evsel, avg);
Ingo Molnard58f4c82011-04-27 03:42:18 +0200743 } else if (perf_evsel__match(evsel, HARDWARE, HW_CACHE_MISSES) &&
744 runtime_cacherefs_stats[cpu].n != 0) {
745 total = avg_stats(&runtime_cacherefs_stats[cpu]);
746
747 if (total)
748 ratio = avg * 100 / total;
749
Stephane Eranian4aa90152011-08-15 22:22:33 +0200750 fprintf(output, " # %8.3f %% of all cache refs ", ratio);
Ingo Molnard58f4c82011-04-27 03:42:18 +0200751
Ingo Molnard3d1e862011-04-29 13:49:08 +0200752 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_FRONTEND)) {
753 print_stalled_cycles_frontend(cpu, evsel, avg);
Ingo Molnar129c04c2011-04-29 14:41:28 +0200754 } else if (perf_evsel__match(evsel, HARDWARE, HW_STALLED_CYCLES_BACKEND)) {
Ingo Molnard3d1e862011-04-29 13:49:08 +0200755 print_stalled_cycles_backend(cpu, evsel, avg);
Ingo Molnar481f9882011-04-27 04:34:16 +0200756 } else if (perf_evsel__match(evsel, HARDWARE, HW_CPU_CYCLES)) {
757 total = avg_stats(&runtime_nsecs_stats[cpu]);
758
759 if (total)
760 ratio = 1.0 * avg / total;
761
Stephane Eranian4aa90152011-08-15 22:22:33 +0200762 fprintf(output, " # %8.3f GHz ", ratio);
Ingo Molnar481f9882011-04-27 04:34:16 +0200763 } else if (runtime_nsecs_stats[cpu].n != 0) {
Namhyung Kim5fde25232012-02-06 16:44:44 +0900764 char unit = 'M';
765
Ingo Molnar481f9882011-04-27 04:34:16 +0200766 total = avg_stats(&runtime_nsecs_stats[cpu]);
767
768 if (total)
769 ratio = 1000.0 * avg / total;
Namhyung Kim5fde25232012-02-06 16:44:44 +0900770 if (ratio < 0.001) {
771 ratio *= 1000;
772 unit = 'K';
773 }
Ingo Molnar481f9882011-04-27 04:34:16 +0200774
Namhyung Kim5fde25232012-02-06 16:44:44 +0900775 fprintf(output, " # %8.3f %c/sec ", ratio, unit);
Ingo Molnara5d243d2011-04-27 05:39:24 +0200776 } else {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200777 fprintf(output, " ");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200778 }
Ingo Molnar42202dd2009-06-13 14:57:28 +0200779}
780
781/*
782 * Print out the results of a single counter:
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200783 * aggregated counts in system-wide mode
Ingo Molnar42202dd2009-06-13 14:57:28 +0200784 */
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200785static void print_counter_aggr(struct perf_evsel *counter)
Ingo Molnar42202dd2009-06-13 14:57:28 +0200786{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200787 struct perf_stat *ps = counter->priv;
788 double avg = avg_stats(&ps->res_stats[0]);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200789 int scaled = counter->counts->scaled;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200790
Ingo Molnar42202dd2009-06-13 14:57:28 +0200791 if (scaled == -1) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200792 fprintf(output, "%*s%s%*s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200793 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600794 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
Stephane Eranian023695d2011-02-14 11:20:01 +0200795 csv_sep,
796 csv_output ? 0 : -24,
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300797 perf_evsel__name(counter));
Stephane Eranian023695d2011-02-14 11:20:01 +0200798
799 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200800 fprintf(output, "%s%s", csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200801
Stephane Eranian4aa90152011-08-15 22:22:33 +0200802 fputc('\n', output);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200803 return;
804 }
805
806 if (nsec_counter(counter))
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200807 nsec_printout(-1, counter, avg);
Ingo Molnar42202dd2009-06-13 14:57:28 +0200808 else
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200809 abs_printout(-1, counter, avg);
Peter Zijlstra849abde2009-09-04 18:23:38 +0200810
Zhengyu He3ae9a34d2011-06-23 13:45:42 -0700811 print_noise(counter, avg);
812
Stephane Eraniand7470b62010-12-01 18:49:05 +0200813 if (csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200814 fputc('\n', output);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200815 return;
816 }
817
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200818 if (scaled) {
819 double avg_enabled, avg_running;
820
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200821 avg_enabled = avg_stats(&ps->res_stats[1]);
822 avg_running = avg_stats(&ps->res_stats[2]);
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200823
Stephane Eranian4aa90152011-08-15 22:22:33 +0200824 fprintf(output, " [%5.2f%%]", 100 * avg_running / avg_enabled);
Peter Zijlstra506d4bc2009-09-04 15:36:12 +0200825 }
Stephane Eranian4aa90152011-08-15 22:22:33 +0200826 fprintf(output, "\n");
Ingo Molnar42202dd2009-06-13 14:57:28 +0200827}
828
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200829/*
830 * Print out the results of a single counter:
831 * does not use aggregated count in system-wide
832 */
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200833static void print_counter(struct perf_evsel *counter)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200834{
835 u64 ena, run, val;
836 int cpu;
837
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800838 for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200839 val = counter->counts->cpu[cpu].val;
840 ena = counter->counts->cpu[cpu].ena;
841 run = counter->counts->cpu[cpu].run;
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200842 if (run == 0 || ena == 0) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200843 fprintf(output, "CPU%*d%s%*s%s%*s",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200844 csv_output ? 0 : -4,
Yan, Zheng7ae92e72012-09-10 15:53:50 +0800845 perf_evsel__cpus(counter)->map[cpu], csv_sep,
Stephane Eraniand7470b62010-12-01 18:49:05 +0200846 csv_output ? 0 : 18,
David Ahern2cee77c2011-05-30 08:55:59 -0600847 counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
848 csv_sep,
Stephane Eranian023695d2011-02-14 11:20:01 +0200849 csv_output ? 0 : -24,
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300850 perf_evsel__name(counter));
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200851
Stephane Eranian023695d2011-02-14 11:20:01 +0200852 if (counter->cgrp)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200853 fprintf(output, "%s%s",
854 csv_sep, counter->cgrp->name);
Stephane Eranian023695d2011-02-14 11:20:01 +0200855
Stephane Eranian4aa90152011-08-15 22:22:33 +0200856 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200857 continue;
858 }
859
860 if (nsec_counter(counter))
861 nsec_printout(cpu, counter, val);
862 else
863 abs_printout(cpu, counter, val);
864
Stephane Eraniand7470b62010-12-01 18:49:05 +0200865 if (!csv_output) {
866 print_noise(counter, 1.0);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200867
Ingo Molnarc6264de2011-04-27 13:50:47 +0200868 if (run != ena)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200869 fprintf(output, " (%.2f%%)",
870 100.0 * run / ena);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200871 }
Stephane Eranian4aa90152011-08-15 22:22:33 +0200872 fputc('\n', output);
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200873 }
874}
875
Ingo Molnar42202dd2009-06-13 14:57:28 +0200876static void print_stat(int argc, const char **argv)
877{
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200878 struct perf_evsel *counter;
879 int i;
Ingo Molnar42202dd2009-06-13 14:57:28 +0200880
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200881 fflush(stdout);
882
Stephane Eraniand7470b62010-12-01 18:49:05 +0200883 if (!csv_output) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200884 fprintf(output, "\n");
885 fprintf(output, " Performance counter stats for ");
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900886 if (!perf_target__has_task(&target)) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200887 fprintf(output, "\'%s", argv[0]);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200888 for (i = 1; i < argc; i++)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200889 fprintf(output, " %s", argv[i]);
Namhyung Kim20f946b2012-04-26 14:15:16 +0900890 } else if (target.pid)
891 fprintf(output, "process id \'%s", target.pid);
Stephane Eraniand7470b62010-12-01 18:49:05 +0200892 else
Namhyung Kim20f946b2012-04-26 14:15:16 +0900893 fprintf(output, "thread id \'%s", target.tid);
Ingo Molnar44db76c2009-06-03 19:36:07 +0200894
Stephane Eranian4aa90152011-08-15 22:22:33 +0200895 fprintf(output, "\'");
Stephane Eraniand7470b62010-12-01 18:49:05 +0200896 if (run_count > 1)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200897 fprintf(output, " (%d runs)", run_count);
898 fprintf(output, ":\n\n");
Stephane Eraniand7470b62010-12-01 18:49:05 +0200899 }
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200900
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200901 if (no_aggr) {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200902 list_for_each_entry(counter, &evsel_list->entries, node)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200903 print_counter(counter);
904 } else {
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200905 list_for_each_entry(counter, &evsel_list->entries, node)
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +0200906 print_counter_aggr(counter);
907 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200908
Stephane Eraniand7470b62010-12-01 18:49:05 +0200909 if (!csv_output) {
Ingo Molnarc33052572011-05-19 14:01:42 +0200910 if (!null_run)
Stephane Eranian4aa90152011-08-15 22:22:33 +0200911 fprintf(output, "\n");
912 fprintf(output, " %17.9f seconds time elapsed",
Stephane Eraniand7470b62010-12-01 18:49:05 +0200913 avg_stats(&walltime_nsecs_stats)/1e9);
914 if (run_count > 1) {
Stephane Eranian4aa90152011-08-15 22:22:33 +0200915 fprintf(output, " ");
Ingo Molnarf99844c2011-04-27 05:35:39 +0200916 print_noise_pct(stddev_stats(&walltime_nsecs_stats),
917 avg_stats(&walltime_nsecs_stats));
Stephane Eraniand7470b62010-12-01 18:49:05 +0200918 }
Stephane Eranian4aa90152011-08-15 22:22:33 +0200919 fprintf(output, "\n\n");
Ingo Molnar566747e2009-06-27 06:24:32 +0200920 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200921}
922
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200923static volatile int signr = -1;
924
Ingo Molnar52425192009-05-26 09:17:18 +0200925static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200926{
Zhang, Yanmin6be28502010-03-18 11:36:03 -0300927 if(child_pid == -1)
Liming Wang60666c62009-12-31 16:05:50 +0800928 done = 1;
929
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200930 signr = signo;
931}
932
933static void sig_atexit(void)
934{
Chris Wilson933da832009-10-04 01:35:01 +0100935 if (child_pid != -1)
936 kill(child_pid, SIGTERM);
937
Peter Zijlstraf7b7c262009-06-10 15:55:59 +0200938 if (signr == -1)
939 return;
940
941 signal(signr, SIG_DFL);
942 kill(getpid(), signr);
Ingo Molnar52425192009-05-26 09:17:18 +0200943}
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200944
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300945static int stat__set_big_num(const struct option *opt __maybe_unused,
946 const char *s __maybe_unused, int unset)
Stephane Eraniand7470b62010-12-01 18:49:05 +0200947{
948 big_num_opt = unset ? 0 : 1;
949 return 0;
950}
951
Ingo Molnar2cba3ff2011-05-19 13:30:56 +0200952/*
953 * Add default attributes, if there were no attributes specified or
954 * if -d/--detailed, -d -d or -d -d -d is used:
955 */
956static int add_default_attributes(void)
957{
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -0300958 struct perf_event_attr default_attrs[] = {
959
960 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK },
961 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES },
962 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS },
963 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS },
964
965 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES },
966 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND },
967 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND },
968 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS },
969 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS },
970 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES },
971
972};
973
974/*
975 * Detailed stats (-d), covering the L1 and last level data caches:
976 */
977 struct perf_event_attr detailed_attrs[] = {
978
979 { .type = PERF_TYPE_HW_CACHE,
980 .config =
981 PERF_COUNT_HW_CACHE_L1D << 0 |
982 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
983 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
984
985 { .type = PERF_TYPE_HW_CACHE,
986 .config =
987 PERF_COUNT_HW_CACHE_L1D << 0 |
988 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
989 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
990
991 { .type = PERF_TYPE_HW_CACHE,
992 .config =
993 PERF_COUNT_HW_CACHE_LL << 0 |
994 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
995 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
996
997 { .type = PERF_TYPE_HW_CACHE,
998 .config =
999 PERF_COUNT_HW_CACHE_LL << 0 |
1000 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1001 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1002};
1003
1004/*
1005 * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1006 */
1007 struct perf_event_attr very_detailed_attrs[] = {
1008
1009 { .type = PERF_TYPE_HW_CACHE,
1010 .config =
1011 PERF_COUNT_HW_CACHE_L1I << 0 |
1012 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1013 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1014
1015 { .type = PERF_TYPE_HW_CACHE,
1016 .config =
1017 PERF_COUNT_HW_CACHE_L1I << 0 |
1018 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1019 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1020
1021 { .type = PERF_TYPE_HW_CACHE,
1022 .config =
1023 PERF_COUNT_HW_CACHE_DTLB << 0 |
1024 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1025 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1026
1027 { .type = PERF_TYPE_HW_CACHE,
1028 .config =
1029 PERF_COUNT_HW_CACHE_DTLB << 0 |
1030 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1031 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1032
1033 { .type = PERF_TYPE_HW_CACHE,
1034 .config =
1035 PERF_COUNT_HW_CACHE_ITLB << 0 |
1036 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1037 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1038
1039 { .type = PERF_TYPE_HW_CACHE,
1040 .config =
1041 PERF_COUNT_HW_CACHE_ITLB << 0 |
1042 (PERF_COUNT_HW_CACHE_OP_READ << 8) |
1043 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1044
1045};
1046
1047/*
1048 * Very, very detailed stats (-d -d -d), adding prefetch events:
1049 */
1050 struct perf_event_attr very_very_detailed_attrs[] = {
1051
1052 { .type = PERF_TYPE_HW_CACHE,
1053 .config =
1054 PERF_COUNT_HW_CACHE_L1D << 0 |
1055 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1056 (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) },
1057
1058 { .type = PERF_TYPE_HW_CACHE,
1059 .config =
1060 PERF_COUNT_HW_CACHE_L1D << 0 |
1061 (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) |
1062 (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) },
1063};
1064
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001065 /* Set attrs if no event is selected and !null_run: */
1066 if (null_run)
1067 return 0;
1068
1069 if (!evsel_list->nr_entries) {
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001070 if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001071 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001072 }
1073
1074 /* Detailed events get appended to the event list: */
1075
1076 if (detailed_run < 1)
1077 return 0;
1078
1079 /* Append detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001080 if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001081 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001082
1083 if (detailed_run < 2)
1084 return 0;
1085
1086 /* Append very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001087 if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -02001088 return -1;
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001089
1090 if (detailed_run < 3)
1091 return 0;
1092
1093 /* Append very, very detailed run extra attributes: */
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -03001094 return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001095}
1096
Irina Tirdea1d037ca2012-09-11 01:15:03 +03001097int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused)
Ingo Molnar52425192009-05-26 09:17:18 +02001098{
Peter Zijlstra1f16c572012-10-23 13:40:14 +02001099 bool append_file = false;
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001100 int output_fd = 0;
1101 const char *output_name = NULL;
1102 const struct option options[] = {
1103 OPT_CALLBACK('e', "event", &evsel_list, "event",
1104 "event selector. use 'perf list' to list available events",
1105 parse_events_option),
1106 OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1107 "event filter", parse_filter),
1108 OPT_BOOLEAN('i', "no-inherit", &no_inherit,
1109 "child tasks do not inherit counters"),
1110 OPT_STRING('p', "pid", &target.pid, "pid",
1111 "stat events on existing process id"),
1112 OPT_STRING('t', "tid", &target.tid, "tid",
1113 "stat events on existing thread id"),
1114 OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1115 "system-wide collection from all CPUs"),
1116 OPT_BOOLEAN('g', "group", &group,
1117 "put the counters into a counter group"),
1118 OPT_BOOLEAN('c', "scale", &scale, "scale/normalize counters"),
1119 OPT_INCR('v', "verbose", &verbose,
1120 "be more verbose (show counter open errors, etc)"),
1121 OPT_INTEGER('r', "repeat", &run_count,
1122 "repeat command and print average + stddev (max: 100)"),
1123 OPT_BOOLEAN('n', "null", &null_run,
1124 "null run - dont start any counters"),
1125 OPT_INCR('d', "detailed", &detailed_run,
1126 "detailed run - start a lot of events"),
1127 OPT_BOOLEAN('S', "sync", &sync_run,
1128 "call sync() before starting a run"),
1129 OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1130 "print large numbers with thousands\' separators",
1131 stat__set_big_num),
1132 OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1133 "list of cpus to monitor in system-wide"),
1134 OPT_BOOLEAN('A', "no-aggr", &no_aggr, "disable CPU count aggregation"),
1135 OPT_STRING('x', "field-separator", &csv_sep, "separator",
1136 "print counts with custom separator"),
1137 OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1138 "monitor event in cgroup name only", parse_cgroups),
1139 OPT_STRING('o', "output", &output_name, "file", "output file name"),
1140 OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1141 OPT_INTEGER(0, "log-fd", &output_fd,
1142 "log output to fd, instead of stderr"),
Peter Zijlstra1f16c572012-10-23 13:40:14 +02001143 OPT_STRING(0, "pre", &pre_cmd, "command",
1144 "command to run prior to the measured command"),
1145 OPT_STRING(0, "post", &post_cmd, "command",
1146 "command to run after to the measured command"),
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001147 OPT_END()
1148 };
1149 const char * const stat_usage[] = {
1150 "perf stat [<options>] [<command>]",
1151 NULL
1152 };
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001153 struct perf_evsel *pos;
Arnaldo Carvalho de Melob070a5472012-10-01 15:20:58 -03001154 int status = -ENOMEM, run_idx;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001155 const char *mode;
Ingo Molnar42202dd2009-06-13 14:57:28 +02001156
Stephane Eranian5af52b52010-05-18 15:00:01 +02001157 setlocale(LC_ALL, "");
1158
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001159 evsel_list = perf_evlist__new(NULL, NULL);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001160 if (evsel_list == NULL)
1161 return -ENOMEM;
1162
Anton Blancharda0541232009-07-22 23:04:12 +10001163 argc = parse_options(argc, argv, options, stat_usage,
1164 PARSE_OPT_STOP_AT_NON_OPTION);
Stephane Eraniand7470b62010-12-01 18:49:05 +02001165
Stephane Eranian4aa90152011-08-15 22:22:33 +02001166 output = stderr;
1167 if (output_name && strcmp(output_name, "-"))
1168 output = NULL;
1169
Jim Cromie56f3bae2011-09-07 17:14:00 -06001170 if (output_name && output_fd) {
1171 fprintf(stderr, "cannot use both --output and --log-fd\n");
1172 usage_with_options(stat_usage, options);
1173 }
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001174
1175 if (output_fd < 0) {
1176 fprintf(stderr, "argument to --log-fd must be a > 0\n");
1177 usage_with_options(stat_usage, options);
1178 }
1179
Stephane Eranian4aa90152011-08-15 22:22:33 +02001180 if (!output) {
1181 struct timespec tm;
1182 mode = append_file ? "a" : "w";
1183
1184 output = fopen(output_name, mode);
1185 if (!output) {
1186 perror("failed to create output file");
David Ahernfceda7f2012-08-26 12:24:44 -06001187 return -1;
Stephane Eranian4aa90152011-08-15 22:22:33 +02001188 }
1189 clock_gettime(CLOCK_REALTIME, &tm);
1190 fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
Stephane Eranianfc3e4d02012-05-15 13:11:11 +02001191 } else if (output_fd > 0) {
Jim Cromie56f3bae2011-09-07 17:14:00 -06001192 mode = append_file ? "a" : "w";
1193 output = fdopen(output_fd, mode);
1194 if (!output) {
1195 perror("Failed opening logfd");
1196 return -errno;
1197 }
Stephane Eranian4aa90152011-08-15 22:22:33 +02001198 }
1199
Jim Cromied4ffd042011-09-07 17:14:03 -06001200 if (csv_sep) {
Stephane Eraniand7470b62010-12-01 18:49:05 +02001201 csv_output = true;
Jim Cromied4ffd042011-09-07 17:14:03 -06001202 if (!strcmp(csv_sep, "\\t"))
1203 csv_sep = "\t";
1204 } else
Stephane Eraniand7470b62010-12-01 18:49:05 +02001205 csv_sep = DEFAULT_SEPARATOR;
1206
1207 /*
1208 * let the spreadsheet do the pretty-printing
1209 */
1210 if (csv_output) {
Jim Cromie61a9f322011-09-07 17:14:04 -06001211 /* User explicitly passed -B? */
Stephane Eraniand7470b62010-12-01 18:49:05 +02001212 if (big_num_opt == 1) {
1213 fprintf(stderr, "-B option not supported with -x\n");
1214 usage_with_options(stat_usage, options);
1215 } else /* Nope, so disable big number formatting */
1216 big_num = false;
1217 } else if (big_num_opt == 0) /* User passed --no-big-num */
1218 big_num = false;
1219
Namhyung Kimaa22dd42012-05-16 18:45:47 +09001220 if (!argc && !perf_target__has_task(&target))
Ingo Molnar52425192009-05-26 09:17:18 +02001221 usage_with_options(stat_usage, options);
Peter Zijlstra9e9772c2009-09-04 15:36:08 +02001222 if (run_count <= 0)
Ingo Molnar42202dd2009-06-13 14:57:28 +02001223 usage_with_options(stat_usage, options);
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001224
Stephane Eranian023695d2011-02-14 11:20:01 +02001225 /* no_aggr, cgroup are for system-wide only */
Namhyung Kimaa22dd42012-05-16 18:45:47 +09001226 if ((no_aggr || nr_cgroups) && !perf_target__has_cpu(&target)) {
Stephane Eranian023695d2011-02-14 11:20:01 +02001227 fprintf(stderr, "both cgroup and no-aggregation "
1228 "modes only available in system-wide mode\n");
1229
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001230 usage_with_options(stat_usage, options);
Stephane Eranian023695d2011-02-14 11:20:01 +02001231 }
Stephane Eranianf5b4a9c32010-11-16 11:05:01 +02001232
Ingo Molnar2cba3ff2011-05-19 13:30:56 +02001233 if (add_default_attributes())
1234 goto out;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001235
Namhyung Kim4bd0f2d2012-04-26 14:15:18 +09001236 perf_target__validate(&target);
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02001237
Namhyung Kim77a6f012012-05-07 14:09:04 +09001238 if (perf_evlist__create_maps(evsel_list, &target) < 0) {
Namhyung Kimaa22dd42012-05-16 18:45:47 +09001239 if (perf_target__has_task(&target))
Namhyung Kim77a6f012012-05-07 14:09:04 +09001240 pr_err("Problems finding threads of monitor\n");
Namhyung Kimaa22dd42012-05-16 18:45:47 +09001241 if (perf_target__has_cpu(&target))
Namhyung Kim77a6f012012-05-07 14:09:04 +09001242 perror("failed to parse CPUs map");
Arnaldo Carvalho de Melo5c98d4662011-01-03 17:53:33 -02001243
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001244 usage_with_options(stat_usage, options);
Arnaldo Carvalho de Melo60d567e2011-01-03 17:49:48 -02001245 return -1;
1246 }
Stephane Eranianc45c6ea2010-05-28 12:00:01 +02001247
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001248 list_for_each_entry(pos, &evsel_list->entries, node) {
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001249 if (perf_evsel__alloc_stat_priv(pos) < 0 ||
Yan, Zheng7ae92e72012-09-10 15:53:50 +08001250 perf_evsel__alloc_counts(pos, perf_evsel__nr_cpus(pos)) < 0)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001251 goto out_free_fd;
Zhang, Yanmind6d901c2010-03-18 11:36:05 -03001252 }
1253
Ingo Molnar58d7e992009-05-15 11:03:23 +02001254 /*
1255 * We dont want to block the signals - that would cause
1256 * child tasks to inherit that and Ctrl-C would not work.
1257 * What we want is for Ctrl-C to work in the exec()-ed
1258 * task, but being ignored by perf stat itself:
1259 */
Peter Zijlstraf7b7c262009-06-10 15:55:59 +02001260 atexit(sig_atexit);
Ingo Molnar58d7e992009-05-15 11:03:23 +02001261 signal(SIGINT, skip_signal);
1262 signal(SIGALRM, skip_signal);
1263 signal(SIGABRT, skip_signal);
1264
Ingo Molnar42202dd2009-06-13 14:57:28 +02001265 status = 0;
1266 for (run_idx = 0; run_idx < run_count; run_idx++) {
1267 if (run_count != 1 && verbose)
Stephane Eranian4aa90152011-08-15 22:22:33 +02001268 fprintf(output, "[ perf stat: executing run #%d ... ]\n",
1269 run_idx + 1);
Ingo Molnarf9cef0a2011-04-28 18:17:11 +02001270
Ingo Molnar42202dd2009-06-13 14:57:28 +02001271 status = run_perf_stat(argc, argv);
1272 }
1273
Arnaldo Carvalho de Melo084ab9f2010-03-22 13:10:28 -03001274 if (status != -1)
1275 print_stat(argc, argv);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001276out_free_fd:
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -02001277 list_for_each_entry(pos, &evsel_list->entries, node)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001278 perf_evsel__free_stat_priv(pos);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -02001279 perf_evlist__delete_maps(evsel_list);
Arnaldo Carvalho de Melo0015e2e2011-02-01 16:18:10 -02001280out:
1281 perf_evlist__delete(evsel_list);
Ingo Molnar42202dd2009-06-13 14:57:28 +02001282 return status;
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001283}