blob: 27abe6a2db3b6f418df7f4f3567ed067321c3c81 [file] [log] [blame]
Ingo Molnarddcacfa2009-04-20 15:37:32 +02001/*
Ingo Molnar52425192009-05-26 09:17:18 +02002 * perf stat: /usr/bin/time -alike performance counter statistics utility
Ingo Molnarddcacfa2009-04-20 15:37:32 +02003
4 It summarizes the counter events of all tasks (and child tasks),
5 covering all CPUs that the command (or workload) executes on.
6 It only counts the per-task events of the workload started,
7 independent of how many other tasks run on those CPUs.
8
9 Sample output:
10
Ingo Molnar52425192009-05-26 09:17:18 +020011 $ perf stat -e 1 -e 3 -e 5 ls -lR /usr/include/ >/dev/null
Ingo Molnarddcacfa2009-04-20 15:37:32 +020012
13 Performance counter stats for 'ls':
14
15 163516953 instructions
16 2295 cache-misses
17 2855182 branch-misses
Ingo Molnar52425192009-05-26 09:17:18 +020018 *
19 * Copyright (C) 2008, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
20 *
21 * Improvements and fixes by:
22 *
23 * Arjan van de Ven <arjan@linux.intel.com>
24 * Yanmin Zhang <yanmin.zhang@intel.com>
25 * Wu Fengguang <fengguang.wu@intel.com>
26 * Mike Galbraith <efault@gmx.de>
27 * Paul Mackerras <paulus@samba.org>
28 *
29 * Released under the GPL v2. (and only v2, not any later version)
Ingo Molnarddcacfa2009-04-20 15:37:32 +020030 */
31
Peter Zijlstra1a482f32009-05-23 18:28:58 +020032#include "perf.h"
Ingo Molnar16f762a2009-05-27 09:10:38 +020033#include "builtin.h"
Ingo Molnar148be2c2009-04-27 08:02:14 +020034#include "util/util.h"
Ingo Molnar52425192009-05-26 09:17:18 +020035#include "util/parse-options.h"
36#include "util/parse-events.h"
Ingo Molnarddcacfa2009-04-20 15:37:32 +020037
Ingo Molnarddcacfa2009-04-20 15:37:32 +020038#include <sys/prctl.h>
Peter Zijlstra16c8a102009-05-05 17:50:27 +020039
Ingo Molnarddcacfa2009-04-20 15:37:32 +020040static int system_wide = 0;
Ingo Molnar52425192009-05-26 09:17:18 +020041static int inherit = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020042
Ingo Molnar52425192009-05-26 09:17:18 +020043static __u64 default_event_id[MAX_COUNTERS] = {
Ingo Molnarddcacfa2009-04-20 15:37:32 +020044 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK),
45 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES),
46 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS),
47 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS),
48
49 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES),
50 EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS),
51 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES),
52 EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES),
53};
Ingo Molnar52425192009-05-26 09:17:18 +020054
Ingo Molnarddcacfa2009-04-20 15:37:32 +020055static int default_interval = 100000;
56static int event_count[MAX_COUNTERS];
57static int fd[MAX_NR_CPUS][MAX_COUNTERS];
58
Ingo Molnar52425192009-05-26 09:17:18 +020059static int target_pid = -1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020060static int nr_cpus = 0;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020061static unsigned int page_size;
62
Ingo Molnar66cf7822009-04-30 13:53:33 +020063static int scale = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020064
65static const unsigned int default_count[] = {
66 1000000,
67 1000000,
68 10000,
69 10000,
70 1000000,
71 10000,
72};
73
Ingo Molnar2996f5d2009-05-29 09:10:54 +020074static __u64 event_res[MAX_COUNTERS][3];
75static __u64 event_scaled[MAX_COUNTERS];
76
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +020077static __u64 runtime_nsecs;
Ingo Molnard7c29312009-05-30 12:38:51 +020078static __u64 walltime_nsecs;
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +020079
Ingo Molnarddcacfa2009-04-20 15:37:32 +020080static void create_perfstat_counter(int counter)
81{
Peter Zijlstrac70975b2009-06-02 17:38:21 +020082 struct perf_counter_attr attr;
Ingo Molnarddcacfa2009-04-20 15:37:32 +020083
Peter Zijlstrac70975b2009-06-02 17:38:21 +020084 memset(&attr, 0, sizeof(attr));
85 attr.config = event_id[counter];
86 attr.sample_type = 0;
87 attr.exclude_kernel = event_mask[counter] & EVENT_MASK_KERNEL;
88 attr.exclude_user = event_mask[counter] & EVENT_MASK_USER;
Peter Zijlstra16c8a102009-05-05 17:50:27 +020089
Ingo Molnarddcacfa2009-04-20 15:37:32 +020090 if (scale)
Peter Zijlstrac70975b2009-06-02 17:38:21 +020091 attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
Ingo Molnarddcacfa2009-04-20 15:37:32 +020092 PERF_FORMAT_TOTAL_TIME_RUNNING;
93
94 if (system_wide) {
95 int cpu;
96 for (cpu = 0; cpu < nr_cpus; cpu ++) {
Peter Zijlstrac70975b2009-06-02 17:38:21 +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 {
Peter Zijlstrac70975b2009-06-02 17:38:21 +0200105 attr.inherit = inherit;
106 attr.disabled = 1;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200107
Peter Zijlstrac70975b2009-06-02 17:38:21 +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{
122 if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK))
123 return 1;
124 if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK))
125 return 1;
126
127 return 0;
128}
129
130/*
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200131 * Read out the results of a single counter:
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200132 */
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200133static void read_counter(int counter)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200134{
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200135 __u64 *count, single_count[3];
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200136 ssize_t res;
137 int cpu, nv;
138 int scaled;
139
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200140 count = event_res[counter];
141
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200142 count[0] = count[1] = count[2] = 0;
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200143
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200144 nv = scale ? 3 : 1;
145 for (cpu = 0; cpu < nr_cpus; cpu ++) {
146 res = read(fd[cpu][counter], single_count, nv * sizeof(__u64));
147 assert(res == nv * sizeof(__u64));
148
149 count[0] += single_count[0];
150 if (scale) {
151 count[1] += single_count[1];
152 count[2] += single_count[2];
153 }
154 }
155
156 scaled = 0;
157 if (scale) {
158 if (count[2] == 0) {
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200159 event_scaled[counter] = -1;
160 count[0] = 0;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200161 return;
162 }
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200163
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200164 if (count[2] < count[1]) {
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200165 event_scaled[counter] = 1;
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200166 count[0] = (unsigned long long)
167 ((double)count[0] * count[1] / count[2] + 0.5);
168 }
169 }
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200170 /*
171 * Save the full runtime - to allow normalization during printout:
172 */
173 if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK))
174 runtime_nsecs = count[0];
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200175}
176
177/*
178 * Print out the results of a single counter:
179 */
180static void print_counter(int counter)
181{
182 __u64 *count;
183 int scaled;
184
185 count = event_res[counter];
186 scaled = event_scaled[counter];
187
188 if (scaled == -1) {
189 fprintf(stderr, " %14s %-20s\n",
190 "<not counted>", event_name(counter));
191 return;
192 }
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200193
194 if (nsec_counter(counter)) {
195 double msecs = (double)count[0] / 1000000;
196
Ingo Molnard7c29312009-05-30 12:38:51 +0200197 fprintf(stderr, " %14.6f %-20s",
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200198 msecs, event_name(counter));
Ingo Molnard7c29312009-05-30 12:38:51 +0200199 if (event_id[counter] ==
200 EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK)) {
201
202 fprintf(stderr, " # %11.3f CPU utilization factor",
203 (double)count[0] / (double)walltime_nsecs);
204 }
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200205 } else {
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200206 fprintf(stderr, " %14Ld %-20s",
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200207 count[0], event_name(counter));
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200208 if (runtime_nsecs)
Ingo Molnard7c29312009-05-30 12:38:51 +0200209 fprintf(stderr, " # %11.3f M/sec",
Ingo Molnarbe1ac0d2009-05-29 09:10:54 +0200210 (double)count[0]/runtime_nsecs*1000.0);
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200211 }
212 if (scaled)
213 fprintf(stderr, " (scaled from %.2f%%)",
214 (double) count[2] / count[1] * 100);
215 fprintf(stderr, "\n");
216}
217
Ingo Molnar16f762a2009-05-27 09:10:38 +0200218static int do_perfstat(int argc, const char **argv)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200219{
220 unsigned long long t0, t1;
221 int counter;
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200222 int status;
223 int pid;
224
225 if (!system_wide)
226 nr_cpus = 1;
227
228 for (counter = 0; counter < nr_counters; counter++)
229 create_perfstat_counter(counter);
230
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200231 /*
232 * Enable counters and exec the command:
233 */
234 t0 = rdclock();
235 prctl(PR_TASK_PERF_COUNTERS_ENABLE);
236
237 if ((pid = fork()) < 0)
238 perror("failed to fork");
239 if (!pid) {
Ingo Molnar52425192009-05-26 09:17:18 +0200240 if (execvp(argv[0], (char **)argv)) {
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200241 perror(argv[0]);
242 exit(-1);
243 }
244 }
245 while (wait(&status) >= 0)
246 ;
247 prctl(PR_TASK_PERF_COUNTERS_DISABLE);
248 t1 = rdclock();
249
Ingo Molnard7c29312009-05-30 12:38:51 +0200250 walltime_nsecs = t1 - t0;
251
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200252 fflush(stdout);
253
254 fprintf(stderr, "\n");
255 fprintf(stderr, " Performance counter stats for \'%s\':\n",
256 argv[0]);
257 fprintf(stderr, "\n");
258
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200259 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnar2996f5d2009-05-29 09:10:54 +0200260 read_counter(counter);
261
262 for (counter = 0; counter < nr_counters; counter++)
Ingo Molnarc04f5e52009-05-29 09:10:54 +0200263 print_counter(counter);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200264
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200265
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200266 fprintf(stderr, "\n");
267 fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n",
268 (double)(t1-t0)/1e6);
269 fprintf(stderr, "\n");
270
271 return 0;
272}
273
Ingo Molnar52425192009-05-26 09:17:18 +0200274static void skip_signal(int signo)
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200275{
Ingo Molnar52425192009-05-26 09:17:18 +0200276}
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200277
Ingo Molnar52425192009-05-26 09:17:18 +0200278static const char * const stat_usage[] = {
279 "perf stat [<options>] <command>",
280 NULL
281};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200282
Ingo Molnar52425192009-05-26 09:17:18 +0200283static char events_help_msg[EVENTS_HELP_MAX];
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200284
Ingo Molnar52425192009-05-26 09:17:18 +0200285static const struct option options[] = {
286 OPT_CALLBACK('e', "event", NULL, "event",
287 events_help_msg, parse_events),
288 OPT_INTEGER('c', "count", &default_interval,
289 "event period to sample"),
290 OPT_BOOLEAN('i', "inherit", &inherit,
291 "child tasks inherit counters"),
292 OPT_INTEGER('p', "pid", &target_pid,
293 "stat events on existing pid"),
294 OPT_BOOLEAN('a', "all-cpus", &system_wide,
295 "system-wide collection from all CPUs"),
296 OPT_BOOLEAN('l', "scale", &scale,
297 "scale/normalize counters"),
298 OPT_END()
299};
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200300
Ingo Molnar52425192009-05-26 09:17:18 +0200301int cmd_stat(int argc, const char **argv, const char *prefix)
302{
303 int counter;
304
305 page_size = sysconf(_SC_PAGE_SIZE);
306
307 create_events_help(events_help_msg);
308 memcpy(event_id, default_event_id, sizeof(default_event_id));
309
310 argc = parse_options(argc, argv, options, stat_usage, 0);
311 if (!argc)
312 usage_with_options(stat_usage, options);
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200313
314 if (!nr_counters) {
315 nr_counters = 8;
316 }
317
318 for (counter = 0; counter < nr_counters; counter++) {
319 if (event_count[counter])
320 continue;
321
322 event_count[counter] = default_interval;
323 }
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200324 nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
325 assert(nr_cpus <= MAX_NR_CPUS);
326 assert(nr_cpus >= 0);
327
Ingo Molnar58d7e992009-05-15 11:03:23 +0200328 /*
329 * We dont want to block the signals - that would cause
330 * child tasks to inherit that and Ctrl-C would not work.
331 * What we want is for Ctrl-C to work in the exec()-ed
332 * task, but being ignored by perf stat itself:
333 */
334 signal(SIGINT, skip_signal);
335 signal(SIGALRM, skip_signal);
336 signal(SIGABRT, skip_signal);
337
Ingo Molnarddcacfa2009-04-20 15:37:32 +0200338 return do_perfstat(argc, argv);
339}