blob: 4fc0d80440e73d2d969755c0a3e3386e557f508f [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;
Ingo Molnar44db76c2009-06-03 19:36:07 +0200231 int i;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200232
233 if (!system_wide)
234 nr_cpus = 1;
235
236 for (counter = 0; counter < nr_counters; counter++)
237 create_perfstat_counter(counter);
238
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200239 /*
240 * Enable counters and exec the command:
241 */
242 t0 = rdclock();
243 prctl(PR_TASK_PERF_COUNTERS_ENABLE);
244
245 if ((pid = fork()) < 0)
246 perror("failed to fork");
Ingo Molnar44db76c2009-06-03 19:36:07 +0200247
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200248 if (!pid) {
Ingo Molnar52425192009-05-26 09:17:18 +0200249 if (execvp(argv[0], (char **)argv)) {
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200250 perror(argv[0]);
251 exit(-1);
252 }
253 }
Ingo Molnar44db76c2009-06-03 19:36:07 +0200254
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200255 while (wait(&status) >= 0)
256 ;
Ingo Molnar44db76c2009-06-03 19:36:07 +0200257
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200258 prctl(PR_TASK_PERF_COUNTERS_DISABLE);
259 t1 = rdclock();
260
Ingo Molnard7c29312009-05-30 12:38:51 +0200261 walltime_nsecs = t1 - t0;
262
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200263 fflush(stdout);
264
265 fprintf(stderr, "\n");
Ingo Molnar44db76c2009-06-03 19:36:07 +0200266 fprintf(stderr, " Performance counter stats for \'%s", argv[0]);
267
268 for (i = 1; i < argc; i++)
269 fprintf(stderr, " %s", argv[i]);
270
271 fprintf(stderr, "\':\n");
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200272 fprintf(stderr, "\n");
273
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200274 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200275 read_counter(counter);
276
277 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200278 print_counter(counter);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200279
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200280
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200281 fprintf(stderr, "\n");
282 fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n",
283 (double)(t1-t0)/1e6);
284 fprintf(stderr, "\n");
285
286 return 0;
287}
288
Ingo Molnar52425192009-05-26 09:17:18 +0200289static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200290{
Ingo Molnar52425192009-05-26 09:17:18 +0200291}
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200292
Ingo Molnar52425192009-05-26 09:17:18 +0200293static const char * const stat_usage[] = {
294 "perf stat [<options>] <command>",
295 NULL
296};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200297
Ingo Molnar52425192009-05-26 09:17:18 +0200298static char events_help_msg[EVENTS_HELP_MAX];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200299
Ingo Molnar52425192009-05-26 09:17:18 +0200300static const struct option options[] = {
301 OPT_CALLBACK('e', "event", NULL, "event",
302 events_help_msg, parse_events),
303 OPT_INTEGER('c', "count", &default_interval,
304 "event period to sample"),
305 OPT_BOOLEAN('i', "inherit", &inherit,
306 "child tasks inherit counters"),
307 OPT_INTEGER('p', "pid", &target_pid,
308 "stat events on existing pid"),
309 OPT_BOOLEAN('a', "all-cpus", &system_wide,
310 "system-wide collection from all CPUs"),
311 OPT_BOOLEAN('l', "scale", &scale,
312 "scale/normalize counters"),
313 OPT_END()
314};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200315
Ingo Molnar52425192009-05-26 09:17:18 +0200316int cmd_stat(int argc, const char **argv, const char *prefix)
317{
318 int counter;
319
320 page_size = sysconf(_SC_PAGE_SIZE);
321
322 create_events_help(events_help_msg);
323 memcpy(event_id, default_event_id, sizeof(default_event_id));
324
325 argc = parse_options(argc, argv, options, stat_usage, 0);
326 if (!argc)
327 usage_with_options(stat_usage, options);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200328
329 if (!nr_counters) {
330 nr_counters = 8;
331 }
332
333 for (counter = 0; counter < nr_counters; counter++) {
334 if (event_count[counter])
335 continue;
336
337 event_count[counter] = default_interval;
338 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200339 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
340 assert(nr_cpus <= MAX_NR_CPUS);
341 assert(nr_cpus >= 0);
342
Ingo Molnar58d7e992009-05-15 11:03:23 +0200343 /*
344 * We dont want to block the signals - that would cause
345 * child tasks to inherit that and Ctrl-C would not work.
346 * What we want is for Ctrl-C to work in the exec()-ed
347 * task, but being ignored by perf stat itself:
348 */
349 signal(SIGINT, skip_signal);
350 signal(SIGALRM, skip_signal);
351 signal(SIGABRT, skip_signal);
352
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200353 return do_perfstat(argc, argv);
354}