blob: 9711e5524233bf429084d85bcda6c9b3d5255c10 [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 Molnara21ca2c2009-06-06 09:58:57 +020047static struct perf_counter_attr default_attrs[MAX_COUNTERS] = {
48
49 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_TASK_CLOCK },
50 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_CONTEXT_SWITCHES },
51 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_CPU_MIGRATIONS },
52 { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_PAGE_FAULTS },
53
54 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_CPU_CYCLES },
55 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_INSTRUCTIONS },
56 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_CACHE_REFERENCES },
57 { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_CACHE_MISSES },
58};
59
Ingo Molnarddcacfa2009-04-20 15:37:32 +020060static int system_wide = 0;
Ingo Molnar52425192009-05-26 09:17:18 +020061static int inherit = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020062
Ingo Molnarddcacfa2009-04-20 15:37:32 +020063static int fd[MAX_NR_CPUS][MAX_COUNTERS];
64
Ingo Molnar52425192009-05-26 09:17:18 +020065static int target_pid = -1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020066static int nr_cpus = 0;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020067static unsigned int page_size;
68
Ingo Molnar66cf7822009-04-30 13:53:33 +020069static int scale = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020070
71static const unsigned int default_count[] = {
72 1000000,
73 1000000,
74 10000,
75 10000,
76 1000000,
77 10000,
78};
79
Ingo Molnar2996f5d2009-05-29 09:10:54 +020080static __u64 event_res[MAX_COUNTERS][3];
81static __u64 event_scaled[MAX_COUNTERS];
82
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +020083static __u64 runtime_nsecs;
Ingo Molnard7c29312009-05-30 12:38:51 +020084static __u64 walltime_nsecs;
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +020085
Ingo Molnarddcacfa2009-04-20 15:37:32 +020086static void create_perfstat_counter(int counter)
87{
Ingo Molnara21ca2c2009-06-06 09:58:57 +020088 struct perf_counter_attr *attr = attrs + counter;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020089
Ingo Molnarddcacfa2009-04-20 15:37:32 +020090 if (scale)
Ingo Molnara21ca2c2009-06-06 09:58:57 +020091 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
92 PERF_FORMAT_TOTAL_TIME_RUNNING;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020093
94 if (system_wide) {
95 int cpu;
96 for (cpu = 0; cpu < nr_cpus; cpu ++) {
Ingo Molnara21ca2c2009-06-06 09:58:57 +020097 fd[cpu][counter] = sys_perf_counter_open(attr, -1, cpu, -1, 0);
Ingo Molnarddcacfa2009-04-20 15:37:32 +020098 if (fd[cpu][counter] < 0) {
99 printf("perfstat error: syscall returned with %d (%s)\n",
100 fd[cpu][counter], strerror(errno));
101 exit(-1);
102 }
103 }
104 } else {
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200105 attr->inherit = inherit;
106 attr->disabled = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200107
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200108 fd[0][counter] = sys_perf_counter_open(attr, 0, -1, -1, 0);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200109 if (fd[0][counter] < 0) {
110 printf("perfstat error: syscall returned with %d (%s)\n",
111 fd[0][counter], strerror(errno));
112 exit(-1);
113 }
114 }
115}
116
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200117/*
118 * Does the counter have nsecs as a unit?
119 */
120static inline int nsec_counter(int counter)
121{
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200122 if (attrs[counter].type != PERF_TYPE_SOFTWARE)
123 return 0;
124
125 if (attrs[counter].config == PERF_COUNT_CPU_CLOCK)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200126 return 1;
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200127
128 if (attrs[counter].config == PERF_COUNT_TASK_CLOCK)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200129 return 1;
130
131 return 0;
132}
133
134/*
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200135 * Read out the results of a single counter:
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200136 */
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200137static void read_counter(int counter)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200138{
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200139 __u64 *count, single_count[3];
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200140 ssize_t res;
141 int cpu, nv;
142 int scaled;
143
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200144 count = event_res[counter];
145
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200146 count[0] = count[1] = count[2] = 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200147
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200148 nv = scale ? 3 : 1;
149 for (cpu = 0; cpu < nr_cpus; cpu ++) {
150 res = read(fd[cpu][counter], single_count, nv * sizeof(__u64));
151 assert(res == nv * sizeof(__u64));
152
153 count[0] += single_count[0];
154 if (scale) {
155 count[1] += single_count[1];
156 count[2] += single_count[2];
157 }
158 }
159
160 scaled = 0;
161 if (scale) {
162 if (count[2] == 0) {
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200163 event_scaled[counter] = -1;
164 count[0] = 0;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200165 return;
166 }
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200167
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200168 if (count[2] < count[1]) {
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200169 event_scaled[counter] = 1;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200170 count[0] = (unsigned long long)
171 ((double)count[0] * count[1] / count[2] + 0.5);
172 }
173 }
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200174 /*
175 * Save the full runtime - to allow normalization during printout:
176 */
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200177 if (attrs[counter].type == PERF_TYPE_SOFTWARE &&
178 attrs[counter].config == PERF_COUNT_TASK_CLOCK)
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200179 runtime_nsecs = count[0];
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200180}
181
182/*
183 * Print out the results of a single counter:
184 */
185static void print_counter(int counter)
186{
187 __u64 *count;
188 int scaled;
189
190 count = event_res[counter];
191 scaled = event_scaled[counter];
192
193 if (scaled == -1) {
194 fprintf(stderr, " %14s %-20s\n",
195 "<not counted>", event_name(counter));
196 return;
197 }
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200198
199 if (nsec_counter(counter)) {
200 double msecs = (double)count[0] / 1000000;
201
Ingo Molnard7c29312009-05-30 12:38:51 +0200202 fprintf(stderr, " %14.6f %-20s",
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200203 msecs, event_name(counter));
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200204 if (attrs[counter].type == PERF_TYPE_SOFTWARE &&
205 attrs[counter].config == PERF_COUNT_TASK_CLOCK) {
Ingo Molnard7c29312009-05-30 12:38:51 +0200206
207 fprintf(stderr, " # %11.3f CPU utilization factor",
208 (double)count[0] / (double)walltime_nsecs);
209 }
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200210 } else {
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200211 fprintf(stderr, " %14Ld %-20s",
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200212 count[0], event_name(counter));
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200213 if (runtime_nsecs)
Ingo Molnard7c29312009-05-30 12:38:51 +0200214 fprintf(stderr, " # %11.3f M/sec",
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200215 (double)count[0]/runtime_nsecs*1000.0);
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200216 }
217 if (scaled)
218 fprintf(stderr, " (scaled from %.2f%%)",
219 (double) count[2] / count[1] * 100);
220 fprintf(stderr, "\n");
221}
222
Ingo Molnar16f762a2009-05-27 09:10:38 +0200223static int do_perfstat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200224{
225 unsigned long long t0, t1;
226 int counter;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200227 int status;
228 int pid;
Ingo Molnar44db76c2009-06-03 19:36:07 +0200229 int i;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200230
231 if (!system_wide)
232 nr_cpus = 1;
233
234 for (counter = 0; counter < nr_counters; counter++)
235 create_perfstat_counter(counter);
236
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200237 /*
238 * Enable counters and exec the command:
239 */
240 t0 = rdclock();
241 prctl(PR_TASK_PERF_COUNTERS_ENABLE);
242
243 if ((pid = fork()) < 0)
244 perror("failed to fork");
Ingo Molnar44db76c2009-06-03 19:36:07 +0200245
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200246 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 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200252
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200253 while (wait(&status) >= 0)
254 ;
Ingo Molnar44db76c2009-06-03 19:36:07 +0200255
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200256 prctl(PR_TASK_PERF_COUNTERS_DISABLE);
257 t1 = rdclock();
258
Ingo Molnard7c29312009-05-30 12:38:51 +0200259 walltime_nsecs = t1 - t0;
260
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200261 fflush(stdout);
262
263 fprintf(stderr, "\n");
Ingo Molnar44db76c2009-06-03 19:36:07 +0200264 fprintf(stderr, " Performance counter stats for \'%s", argv[0]);
265
266 for (i = 1; i < argc; i++)
267 fprintf(stderr, " %s", argv[i]);
268
269 fprintf(stderr, "\':\n");
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200270 fprintf(stderr, "\n");
271
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200272 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200273 read_counter(counter);
274
275 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200276 print_counter(counter);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200277
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200278
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200279 fprintf(stderr, "\n");
280 fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n",
281 (double)(t1-t0)/1e6);
282 fprintf(stderr, "\n");
283
284 return 0;
285}
286
Ingo Molnar52425192009-05-26 09:17:18 +0200287static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200288{
Ingo Molnar52425192009-05-26 09:17:18 +0200289}
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200290
Ingo Molnar52425192009-05-26 09:17:18 +0200291static const char * const stat_usage[] = {
292 "perf stat [<options>] <command>",
293 NULL
294};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200295
Ingo Molnar52425192009-05-26 09:17:18 +0200296static char events_help_msg[EVENTS_HELP_MAX];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200297
Ingo Molnar52425192009-05-26 09:17:18 +0200298static const struct option options[] = {
299 OPT_CALLBACK('e', "event", NULL, "event",
300 events_help_msg, parse_events),
Ingo Molnar52425192009-05-26 09:17:18 +0200301 OPT_BOOLEAN('i', "inherit", &inherit,
302 "child tasks inherit counters"),
303 OPT_INTEGER('p', "pid", &target_pid,
304 "stat events on existing pid"),
305 OPT_BOOLEAN('a', "all-cpus", &system_wide,
306 "system-wide collection from all CPUs"),
307 OPT_BOOLEAN('l', "scale", &scale,
308 "scale/normalize counters"),
309 OPT_END()
310};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200311
Ingo Molnar52425192009-05-26 09:17:18 +0200312int cmd_stat(int argc, const char **argv, const char *prefix)
313{
Ingo Molnar52425192009-05-26 09:17:18 +0200314 page_size = sysconf(_SC_PAGE_SIZE);
315
316 create_events_help(events_help_msg);
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200317
318 memcpy(attrs, default_attrs, sizeof(attrs));
Ingo Molnar52425192009-05-26 09:17:18 +0200319
320 argc = parse_options(argc, argv, options, stat_usage, 0);
321 if (!argc)
322 usage_with_options(stat_usage, options);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200323
Ingo Molnara21ca2c2009-06-06 09:58:57 +0200324 if (!nr_counters)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200325 nr_counters = 8;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200326
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200327 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
328 assert(nr_cpus <= MAX_NR_CPUS);
329 assert(nr_cpus >= 0);
330
Ingo Molnar58d7e992009-05-15 11:03:23 +0200331 /*
332 * We dont want to block the signals - that would cause
333 * child tasks to inherit that and Ctrl-C would not work.
334 * What we want is for Ctrl-C to work in the exec()-ed
335 * task, but being ignored by perf stat itself:
336 */
337 signal(SIGINT, skip_signal);
338 signal(SIGALRM, skip_signal);
339 signal(SIGABRT, skip_signal);
340
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200341 return do_perfstat(argc, argv);
342}