blob: 2357a663b6755fe541b6f178b38360ae27133acc [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 Molnarbf9e1872009-06-02 23:37:05 +02009 $ perf stat ~/hackbench 10
10 Time: 0.104
Ingo Molnarddcacfa2009-04-20 15:37:32 +020011
Ingo Molnarbf9e1872009-06-02 23:37:05 +020012 Performance counter stats for '/home/mingo/hackbench':
Ingo Molnarddcacfa2009-04-20 15:37:32 +020013
Ingo Molnarbf9e1872009-06-02 23:37:05 +020014 1255.538611 task clock ticks # 10.143 CPU utilization factor
15 54011 context switches # 0.043 M/sec
16 385 CPU migrations # 0.000 M/sec
17 17755 pagefaults # 0.014 M/sec
18 3808323185 CPU cycles # 3033.219 M/sec
19 1575111190 instructions # 1254.530 M/sec
20 17367895 cache references # 13.833 M/sec
21 7674421 cache misses # 6.112 M/sec
Ingo Molnarddcacfa2009-04-20 15:37:32 +020022
Ingo Molnarbf9e1872009-06-02 23:37:05 +020023 Wall-clock time elapsed: 123.786620 msecs
Ingo Molnarddcacfa2009-04-20 15:37:32 +020024
Ingo Molnar52425192009-05-26 09:17:18 +020025 *
26 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
27 *
28 * Improvements and fixes by:
29 *
30 * Arjan van de Ven <arjan@linux.intel.com>
31 * Yanmin Zhang <yanmin.zhang@intel.com>
32 * Wu Fengguang <fengguang.wu@intel.com>
33 * Mike Galbraith <efault@gmx.de>
34 * Paul Mackerras <paulus@samba.org>
35 *
36 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnarddcacfa2009-04-20 15:37:32 +020037 */
38
Peter Zijlstra1a482f32009-05-23 18:28:58 +020039#include "perf.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +020040#include "builtin.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020041#include "util/util.h"
Ingo Molnar52425192009-05-26 09:17:18 +020042#include "util/parse-options.h"
43#include "util/parse-events.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020044
Ingo Molnarddcacfa2009-04-20 15:37:32 +020045#include <sys/prctl.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020046
Ingo Molnarddcacfa2009-04-20 15:37:32 +020047static int system_wide = 0;
Ingo Molnar52425192009-05-26 09:17:18 +020048static int inherit = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020049
Ingo Molnar52425192009-05-26 09:17:18 +020050static __u64 default_event_id[MAX_COUNTERS] = {
Ingo Molnarddcacfa2009-04-20 15:37:32 +020051 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK),
52 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES),
53 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS),
54 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS),
55
56 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES),
57 EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS),
58 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES),
59 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES),
60};
Ingo Molnar52425192009-05-26 09:17:18 +020061
Ingo Molnarddcacfa2009-04-20 15:37:32 +020062static int default_interval = 100000;
63static int event_count[MAX_COUNTERS];
64static int fd[MAX_NR_CPUS][MAX_COUNTERS];
65
Ingo Molnar52425192009-05-26 09:17:18 +020066static int target_pid = -1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020067static int nr_cpus = 0;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020068static unsigned int page_size;
69
Ingo Molnar66cf7822009-04-30 13:53:33 +020070static int scale = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020071
72static const unsigned int default_count[] = {
73 1000000,
74 1000000,
75 10000,
76 10000,
77 1000000,
78 10000,
79};
80
Ingo Molnar2996f5d2009-05-29 09:10:54 +020081static __u64 event_res[MAX_COUNTERS][3];
82static __u64 event_scaled[MAX_COUNTERS];
83
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +020084static __u64 runtime_nsecs;
Ingo Molnard7c29312009-05-30 12:38:51 +020085static __u64 walltime_nsecs;
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +020086
Ingo Molnarddcacfa2009-04-20 15:37:32 +020087static void create_perfstat_counter(int counter)
88{
Peter Zijlstrac70975b2009-06-02 17:38:21 +020089 struct perf_counter_attr attr;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020090
Peter Zijlstrac70975b2009-06-02 17:38:21 +020091 memset(&attr, 0, sizeof(attr));
92 attr.config = event_id[counter];
93 attr.sample_type = 0;
94 attr.exclude_kernel = event_mask[counter] & EVENT_MASK_KERNEL;
95 attr.exclude_user = event_mask[counter] & EVENT_MASK_USER;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020096
Ingo Molnarddcacfa2009-04-20 15:37:32 +020097 if (scale)
Peter Zijlstrac70975b2009-06-02 17:38:21 +020098 attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
Ingo Molnarddcacfa2009-04-20 15:37:32 +020099 PERF_FORMAT_TOTAL_TIME_RUNNING;
100
101 if (system_wide) {
102 int cpu;
103 for (cpu = 0; cpu < nr_cpus; cpu ++) {
Peter Zijlstrac70975b2009-06-02 17:38:21 +0200104 fd[cpu][counter] = sys_perf_counter_open(&attr, -1, cpu, -1, 0);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200105 if (fd[cpu][counter] < 0) {
106 printf("perfstat error: syscall returned with %d (%s)\n",
107 fd[cpu][counter], strerror(errno));
108 exit(-1);
109 }
110 }
111 } else {
Peter Zijlstrac70975b2009-06-02 17:38:21 +0200112 attr.inherit = inherit;
113 attr.disabled = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200114
Peter Zijlstrac70975b2009-06-02 17:38:21 +0200115 fd[0][counter] = sys_perf_counter_open(&attr, 0, -1, -1, 0);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200116 if (fd[0][counter] < 0) {
117 printf("perfstat error: syscall returned with %d (%s)\n",
118 fd[0][counter], strerror(errno));
119 exit(-1);
120 }
121 }
122}
123
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200124/*
125 * Does the counter have nsecs as a unit?
126 */
127static inline int nsec_counter(int counter)
128{
129 if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK))
130 return 1;
131 if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK))
132 return 1;
133
134 return 0;
135}
136
137/*
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200138 * Read out the results of a single counter:
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200139 */
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200140static void read_counter(int counter)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200141{
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200142 __u64 *count, single_count[3];
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200143 ssize_t res;
144 int cpu, nv;
145 int scaled;
146
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200147 count = event_res[counter];
148
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200149 count[0] = count[1] = count[2] = 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200150
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200151 nv = scale ? 3 : 1;
152 for (cpu = 0; cpu < nr_cpus; cpu ++) {
153 res = read(fd[cpu][counter], single_count, nv * sizeof(__u64));
154 assert(res == nv * sizeof(__u64));
155
156 count[0] += single_count[0];
157 if (scale) {
158 count[1] += single_count[1];
159 count[2] += single_count[2];
160 }
161 }
162
163 scaled = 0;
164 if (scale) {
165 if (count[2] == 0) {
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200166 event_scaled[counter] = -1;
167 count[0] = 0;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200168 return;
169 }
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200170
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200171 if (count[2] < count[1]) {
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200172 event_scaled[counter] = 1;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200173 count[0] = (unsigned long long)
174 ((double)count[0] * count[1] / count[2] + 0.5);
175 }
176 }
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200177 /*
178 * Save the full runtime - to allow normalization during printout:
179 */
180 if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK))
181 runtime_nsecs = count[0];
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200182}
183
184/*
185 * Print out the results of a single counter:
186 */
187static void print_counter(int counter)
188{
189 __u64 *count;
190 int scaled;
191
192 count = event_res[counter];
193 scaled = event_scaled[counter];
194
195 if (scaled == -1) {
196 fprintf(stderr, " %14s %-20s\n",
197 "<not counted>", event_name(counter));
198 return;
199 }
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200200
201 if (nsec_counter(counter)) {
202 double msecs = (double)count[0] / 1000000;
203
Ingo Molnard7c29312009-05-30 12:38:51 +0200204 fprintf(stderr, " %14.6f %-20s",
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200205 msecs, event_name(counter));
Ingo Molnard7c29312009-05-30 12:38:51 +0200206 if (event_id[counter] ==
207 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK)) {
208
209 fprintf(stderr, " # %11.3f CPU utilization factor",
210 (double)count[0] / (double)walltime_nsecs);
211 }
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200212 } else {
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200213 fprintf(stderr, " %14Ld %-20s",
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200214 count[0], event_name(counter));
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200215 if (runtime_nsecs)
Ingo Molnard7c29312009-05-30 12:38:51 +0200216 fprintf(stderr, " # %11.3f M/sec",
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200217 (double)count[0]/runtime_nsecs*1000.0);
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200218 }
219 if (scaled)
220 fprintf(stderr, " (scaled from %.2f%%)",
221 (double) count[2] / count[1] * 100);
222 fprintf(stderr, "\n");
223}
224
Ingo Molnar16f762a2009-05-27 09:10:38 +0200225static int do_perfstat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200226{
227 unsigned long long t0, t1;
228 int counter;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200229 int status;
230 int pid;
231
232 if (!system_wide)
233 nr_cpus = 1;
234
235 for (counter = 0; counter < nr_counters; counter++)
236 create_perfstat_counter(counter);
237
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200238 /*
239 * Enable counters and exec the command:
240 */
241 t0 = rdclock();
242 prctl(PR_TASK_PERF_COUNTERS_ENABLE);
243
244 if ((pid = fork()) < 0)
245 perror("failed to fork");
246 if (!pid) {
Ingo Molnar52425192009-05-26 09:17:18 +0200247 if (execvp(argv[0], (char **)argv)) {
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200248 perror(argv[0]);
249 exit(-1);
250 }
251 }
252 while (wait(&status) >= 0)
253 ;
254 prctl(PR_TASK_PERF_COUNTERS_DISABLE);
255 t1 = rdclock();
256
Ingo Molnard7c29312009-05-30 12:38:51 +0200257 walltime_nsecs = t1 - t0;
258
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200259 fflush(stdout);
260
261 fprintf(stderr, "\n");
262 fprintf(stderr, " Performance counter stats for \'%s\':\n",
263 argv[0]);
264 fprintf(stderr, "\n");
265
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200266 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200267 read_counter(counter);
268
269 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200270 print_counter(counter);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200271
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200272
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200273 fprintf(stderr, "\n");
274 fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n",
275 (double)(t1-t0)/1e6);
276 fprintf(stderr, "\n");
277
278 return 0;
279}
280
Ingo Molnar52425192009-05-26 09:17:18 +0200281static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200282{
Ingo Molnar52425192009-05-26 09:17:18 +0200283}
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200284
Ingo Molnar52425192009-05-26 09:17:18 +0200285static const char * const stat_usage[] = {
286 "perf stat [<options>] <command>",
287 NULL
288};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200289
Ingo Molnar52425192009-05-26 09:17:18 +0200290static char events_help_msg[EVENTS_HELP_MAX];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200291
Ingo Molnar52425192009-05-26 09:17:18 +0200292static const struct option options[] = {
293 OPT_CALLBACK('e', "event", NULL, "event",
294 events_help_msg, parse_events),
295 OPT_INTEGER('c', "count", &default_interval,
296 "event period to sample"),
297 OPT_BOOLEAN('i', "inherit", &inherit,
298 "child tasks inherit counters"),
299 OPT_INTEGER('p', "pid", &target_pid,
300 "stat events on existing pid"),
301 OPT_BOOLEAN('a', "all-cpus", &system_wide,
302 "system-wide collection from all CPUs"),
303 OPT_BOOLEAN('l', "scale", &scale,
304 "scale/normalize counters"),
305 OPT_END()
306};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200307
Ingo Molnar52425192009-05-26 09:17:18 +0200308int cmd_stat(int argc, const char **argv, const char *prefix)
309{
310 int counter;
311
312 page_size = sysconf(_SC_PAGE_SIZE);
313
314 create_events_help(events_help_msg);
315 memcpy(event_id, default_event_id, sizeof(default_event_id));
316
317 argc = parse_options(argc, argv, options, stat_usage, 0);
318 if (!argc)
319 usage_with_options(stat_usage, options);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200320
321 if (!nr_counters) {
322 nr_counters = 8;
323 }
324
325 for (counter = 0; counter < nr_counters; counter++) {
326 if (event_count[counter])
327 continue;
328
329 event_count[counter] = default_interval;
330 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200331 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
332 assert(nr_cpus <= MAX_NR_CPUS);
333 assert(nr_cpus >= 0);
334
Ingo Molnar58d7e992009-05-15 11:03:23 +0200335 /*
336 * We dont want to block the signals - that would cause
337 * child tasks to inherit that and Ctrl-C would not work.
338 * What we want is for Ctrl-C to work in the exec()-ed
339 * task, but being ignored by perf stat itself:
340 */
341 signal(SIGINT, skip_signal);
342 signal(SIGALRM, skip_signal);
343 signal(SIGABRT, skip_signal);
344
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200345 return do_perfstat(argc, argv);
346}