]>
Commit | Line | Data |
---|---|---|
ddcacfa0 | 1 | /* |
bf9e1876 IM |
2 | * 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: | |
ddcacfa0 | 8 | |
bf9e1876 IM |
9 | $ perf stat ~/hackbench 10 |
10 | Time: 0.104 | |
ddcacfa0 | 11 | |
bf9e1876 | 12 | Performance counter stats for '/home/mingo/hackbench': |
ddcacfa0 | 13 | |
bf9e1876 IM |
14 | 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 | |
ddcacfa0 | 22 | |
bf9e1876 | 23 | Wall-clock time elapsed: 123.786620 msecs |
ddcacfa0 | 24 | |
5242519b IM |
25 | * |
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> | |
6e750a8f | 35 | * Jaswinder Singh Rajput <jaswinder@kernel.org> |
5242519b IM |
36 | * |
37 | * Released under the GPL v2. (and only v2, not any later version) | |
ddcacfa0 IM |
38 | */ |
39 | ||
1a482f38 | 40 | #include "perf.h" |
16f762a2 | 41 | #include "builtin.h" |
148be2c1 | 42 | #include "util/util.h" |
5242519b IM |
43 | #include "util/parse-options.h" |
44 | #include "util/parse-events.h" | |
ddcacfa0 | 45 | |
ddcacfa0 | 46 | #include <sys/prctl.h> |
42202dd5 | 47 | #include <math.h> |
16c8a109 | 48 | |
c3043569 | 49 | static struct perf_counter_attr default_attrs[] = { |
ddcacfa0 | 50 | |
f4dbfa8f PZ |
51 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK }, |
52 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES}, | |
53 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS }, | |
54 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS }, | |
55 | ||
56 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES }, | |
57 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS }, | |
58 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES}, | |
59 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES }, | |
60 | ||
ddcacfa0 | 61 | }; |
5242519b | 62 | |
3d632595 JSR |
63 | #define MAX_RUN 100 |
64 | ||
a21ca2ca | 65 | static int system_wide = 0; |
743ee1f8 | 66 | static int verbose = 0; |
f37a291c | 67 | static unsigned int nr_cpus = 0; |
3d632595 | 68 | static int run_idx = 0; |
ddcacfa0 | 69 | |
3d632595 JSR |
70 | static int run_count = 1; |
71 | static int inherit = 1; | |
66cf7829 | 72 | static int scale = 1; |
3d632595 | 73 | static int target_pid = -1; |
0cfb7a13 | 74 | static int null_run = 0; |
ddcacfa0 | 75 | |
3d632595 | 76 | static int fd[MAX_NR_CPUS][MAX_COUNTERS]; |
42202dd5 | 77 | |
9cffa8d5 PM |
78 | static u64 runtime_nsecs[MAX_RUN]; |
79 | static u64 walltime_nsecs[MAX_RUN]; | |
80 | static u64 runtime_cycles[MAX_RUN]; | |
42202dd5 | 81 | |
3d632595 JSR |
82 | static u64 event_res[MAX_RUN][MAX_COUNTERS][3]; |
83 | static u64 event_scaled[MAX_RUN][MAX_COUNTERS]; | |
84 | ||
9cffa8d5 PM |
85 | static u64 event_res_avg[MAX_COUNTERS][3]; |
86 | static u64 event_res_noise[MAX_COUNTERS][3]; | |
42202dd5 | 87 | |
9cffa8d5 | 88 | static u64 event_scaled_avg[MAX_COUNTERS]; |
42202dd5 | 89 | |
9cffa8d5 PM |
90 | static u64 runtime_nsecs_avg; |
91 | static u64 runtime_nsecs_noise; | |
42202dd5 | 92 | |
9cffa8d5 PM |
93 | static u64 walltime_nsecs_avg; |
94 | static u64 walltime_nsecs_noise; | |
42202dd5 | 95 | |
9cffa8d5 PM |
96 | static u64 runtime_cycles_avg; |
97 | static u64 runtime_cycles_noise; | |
be1ac0d8 | 98 | |
cca03c0a JSR |
99 | #define ERR_PERF_OPEN \ |
100 | "Error: counter %d, sys_perf_counter_open() syscall returned with %d (%s)\n" | |
101 | ||
051ae7f7 | 102 | static void create_perf_stat_counter(int counter, int pid) |
ddcacfa0 | 103 | { |
a21ca2ca | 104 | struct perf_counter_attr *attr = attrs + counter; |
16c8a109 | 105 | |
ddcacfa0 | 106 | if (scale) |
a21ca2ca IM |
107 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | |
108 | PERF_FORMAT_TOTAL_TIME_RUNNING; | |
ddcacfa0 IM |
109 | |
110 | if (system_wide) { | |
f37a291c IM |
111 | unsigned int cpu; |
112 | ||
cca03c0a | 113 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
a21ca2ca | 114 | fd[cpu][counter] = sys_perf_counter_open(attr, -1, cpu, -1, 0); |
cca03c0a JSR |
115 | if (fd[cpu][counter] < 0 && verbose) |
116 | fprintf(stderr, ERR_PERF_OPEN, counter, | |
117 | fd[cpu][counter], strerror(errno)); | |
ddcacfa0 IM |
118 | } |
119 | } else { | |
57e7986e PM |
120 | attr->inherit = inherit; |
121 | attr->disabled = 1; | |
122 | attr->enable_on_exec = 1; | |
ddcacfa0 | 123 | |
051ae7f7 | 124 | fd[0][counter] = sys_perf_counter_open(attr, pid, -1, -1, 0); |
cca03c0a JSR |
125 | if (fd[0][counter] < 0 && verbose) |
126 | fprintf(stderr, ERR_PERF_OPEN, counter, | |
127 | fd[0][counter], strerror(errno)); | |
ddcacfa0 IM |
128 | } |
129 | } | |
130 | ||
c04f5e5d IM |
131 | /* |
132 | * Does the counter have nsecs as a unit? | |
133 | */ | |
134 | static inline int nsec_counter(int counter) | |
135 | { | |
a21ca2ca IM |
136 | if (attrs[counter].type != PERF_TYPE_SOFTWARE) |
137 | return 0; | |
138 | ||
f4dbfa8f | 139 | if (attrs[counter].config == PERF_COUNT_SW_CPU_CLOCK) |
c04f5e5d | 140 | return 1; |
a21ca2ca | 141 | |
f4dbfa8f | 142 | if (attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK) |
c04f5e5d IM |
143 | return 1; |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
148 | /* | |
2996f5dd | 149 | * Read out the results of a single counter: |
c04f5e5d | 150 | */ |
2996f5dd | 151 | static void read_counter(int counter) |
c04f5e5d | 152 | { |
9cffa8d5 | 153 | u64 *count, single_count[3]; |
f37a291c IM |
154 | unsigned int cpu; |
155 | size_t res, nv; | |
c04f5e5d IM |
156 | int scaled; |
157 | ||
42202dd5 | 158 | count = event_res[run_idx][counter]; |
2996f5dd | 159 | |
c04f5e5d | 160 | count[0] = count[1] = count[2] = 0; |
2996f5dd | 161 | |
c04f5e5d | 162 | nv = scale ? 3 : 1; |
cca03c0a | 163 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
743ee1f8 IM |
164 | if (fd[cpu][counter] < 0) |
165 | continue; | |
166 | ||
9cffa8d5 PM |
167 | res = read(fd[cpu][counter], single_count, nv * sizeof(u64)); |
168 | assert(res == nv * sizeof(u64)); | |
f37a291c | 169 | |
42202dd5 IM |
170 | close(fd[cpu][counter]); |
171 | fd[cpu][counter] = -1; | |
c04f5e5d IM |
172 | |
173 | count[0] += single_count[0]; | |
174 | if (scale) { | |
175 | count[1] += single_count[1]; | |
176 | count[2] += single_count[2]; | |
177 | } | |
178 | } | |
179 | ||
180 | scaled = 0; | |
181 | if (scale) { | |
182 | if (count[2] == 0) { | |
42202dd5 | 183 | event_scaled[run_idx][counter] = -1; |
2996f5dd | 184 | count[0] = 0; |
c04f5e5d IM |
185 | return; |
186 | } | |
2996f5dd | 187 | |
c04f5e5d | 188 | if (count[2] < count[1]) { |
42202dd5 | 189 | event_scaled[run_idx][counter] = 1; |
c04f5e5d IM |
190 | count[0] = (unsigned long long) |
191 | ((double)count[0] * count[1] / count[2] + 0.5); | |
192 | } | |
193 | } | |
be1ac0d8 IM |
194 | /* |
195 | * Save the full runtime - to allow normalization during printout: | |
196 | */ | |
a21ca2ca | 197 | if (attrs[counter].type == PERF_TYPE_SOFTWARE && |
f4dbfa8f | 198 | attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK) |
42202dd5 | 199 | runtime_nsecs[run_idx] = count[0]; |
e779898a | 200 | if (attrs[counter].type == PERF_TYPE_HARDWARE && |
f4dbfa8f | 201 | attrs[counter].config == PERF_COUNT_HW_CPU_CYCLES) |
42202dd5 | 202 | runtime_cycles[run_idx] = count[0]; |
2996f5dd IM |
203 | } |
204 | ||
f37a291c | 205 | static int run_perf_stat(int argc __used, const char **argv) |
42202dd5 IM |
206 | { |
207 | unsigned long long t0, t1; | |
208 | int status = 0; | |
209 | int counter; | |
210 | int pid; | |
051ae7f7 PM |
211 | int child_ready_pipe[2], go_pipe[2]; |
212 | char buf; | |
42202dd5 IM |
213 | |
214 | if (!system_wide) | |
215 | nr_cpus = 1; | |
216 | ||
051ae7f7 PM |
217 | if (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0) { |
218 | perror("failed to create pipes"); | |
219 | exit(1); | |
220 | } | |
221 | ||
222 | if ((pid = fork()) < 0) | |
223 | perror("failed to fork"); | |
224 | ||
225 | if (!pid) { | |
226 | close(child_ready_pipe[0]); | |
227 | close(go_pipe[1]); | |
228 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); | |
229 | ||
230 | /* | |
231 | * Do a dummy execvp to get the PLT entry resolved, | |
232 | * so we avoid the resolver overhead on the real | |
233 | * execvp call. | |
234 | */ | |
235 | execvp("", (char **)argv); | |
236 | ||
237 | /* | |
238 | * Tell the parent we're ready to go | |
239 | */ | |
240 | close(child_ready_pipe[1]); | |
241 | ||
242 | /* | |
243 | * Wait until the parent tells us to go. | |
244 | */ | |
245 | read(go_pipe[0], &buf, 1); | |
246 | ||
247 | execvp(argv[0], (char **)argv); | |
248 | ||
249 | perror(argv[0]); | |
250 | exit(-1); | |
251 | } | |
252 | ||
253 | /* | |
254 | * Wait for the child to be ready to exec. | |
255 | */ | |
256 | close(child_ready_pipe[1]); | |
257 | close(go_pipe[0]); | |
258 | read(child_ready_pipe[0], &buf, 1); | |
259 | close(child_ready_pipe[0]); | |
260 | ||
42202dd5 | 261 | for (counter = 0; counter < nr_counters; counter++) |
051ae7f7 | 262 | create_perf_stat_counter(counter, pid); |
42202dd5 IM |
263 | |
264 | /* | |
265 | * Enable counters and exec the command: | |
266 | */ | |
267 | t0 = rdclock(); | |
42202dd5 | 268 | |
051ae7f7 | 269 | close(go_pipe[1]); |
42202dd5 IM |
270 | wait(&status); |
271 | ||
42202dd5 IM |
272 | t1 = rdclock(); |
273 | ||
274 | walltime_nsecs[run_idx] = t1 - t0; | |
275 | ||
276 | for (counter = 0; counter < nr_counters; counter++) | |
277 | read_counter(counter); | |
278 | ||
279 | return WEXITSTATUS(status); | |
280 | } | |
281 | ||
9cffa8d5 | 282 | static void print_noise(u64 *count, u64 *noise) |
42202dd5 IM |
283 | { |
284 | if (run_count > 1) | |
285 | fprintf(stderr, " ( +- %7.3f%% )", | |
286 | (double)noise[0]/(count[0]+1)*100.0); | |
287 | } | |
288 | ||
9cffa8d5 | 289 | static void nsec_printout(int counter, u64 *count, u64 *noise) |
44175b6f IM |
290 | { |
291 | double msecs = (double)count[0] / 1000000; | |
292 | ||
6e750a8f | 293 | fprintf(stderr, " %14.6f %-24s", msecs, event_name(counter)); |
44175b6f IM |
294 | |
295 | if (attrs[counter].type == PERF_TYPE_SOFTWARE && | |
296 | attrs[counter].config == PERF_COUNT_SW_TASK_CLOCK) { | |
297 | ||
42202dd5 IM |
298 | if (walltime_nsecs_avg) |
299 | fprintf(stderr, " # %10.3f CPUs ", | |
300 | (double)count[0] / (double)walltime_nsecs_avg); | |
44175b6f | 301 | } |
42202dd5 | 302 | print_noise(count, noise); |
44175b6f IM |
303 | } |
304 | ||
9cffa8d5 | 305 | static void abs_printout(int counter, u64 *count, u64 *noise) |
44175b6f | 306 | { |
6e750a8f | 307 | fprintf(stderr, " %14Ld %-24s", count[0], event_name(counter)); |
44175b6f | 308 | |
42202dd5 | 309 | if (runtime_cycles_avg && |
44175b6f IM |
310 | attrs[counter].type == PERF_TYPE_HARDWARE && |
311 | attrs[counter].config == PERF_COUNT_HW_INSTRUCTIONS) { | |
312 | ||
42202dd5 IM |
313 | fprintf(stderr, " # %10.3f IPC ", |
314 | (double)count[0] / (double)runtime_cycles_avg); | |
315 | } else { | |
316 | if (runtime_nsecs_avg) { | |
317 | fprintf(stderr, " # %10.3f M/sec", | |
318 | (double)count[0]/runtime_nsecs_avg*1000.0); | |
319 | } | |
44175b6f | 320 | } |
42202dd5 | 321 | print_noise(count, noise); |
44175b6f IM |
322 | } |
323 | ||
2996f5dd IM |
324 | /* |
325 | * Print out the results of a single counter: | |
326 | */ | |
327 | static void print_counter(int counter) | |
328 | { | |
9cffa8d5 | 329 | u64 *count, *noise; |
2996f5dd IM |
330 | int scaled; |
331 | ||
42202dd5 IM |
332 | count = event_res_avg[counter]; |
333 | noise = event_res_noise[counter]; | |
334 | scaled = event_scaled_avg[counter]; | |
2996f5dd IM |
335 | |
336 | if (scaled == -1) { | |
6e750a8f | 337 | fprintf(stderr, " %14s %-24s\n", |
2996f5dd IM |
338 | "<not counted>", event_name(counter)); |
339 | return; | |
340 | } | |
c04f5e5d | 341 | |
44175b6f | 342 | if (nsec_counter(counter)) |
42202dd5 | 343 | nsec_printout(counter, count, noise); |
44175b6f | 344 | else |
42202dd5 | 345 | abs_printout(counter, count, noise); |
d7c29318 | 346 | |
c04f5e5d | 347 | if (scaled) |
210ad39f IM |
348 | fprintf(stderr, " (scaled from %.2f%%)", |
349 | (double) count[2] / count[1] * 100); | |
44175b6f | 350 | |
c04f5e5d IM |
351 | fprintf(stderr, "\n"); |
352 | } | |
353 | ||
42202dd5 | 354 | /* |
ef281a19 | 355 | * normalize_noise noise values down to stddev: |
42202dd5 | 356 | */ |
9cffa8d5 | 357 | static void normalize_noise(u64 *val) |
ddcacfa0 | 358 | { |
42202dd5 | 359 | double res; |
ddcacfa0 | 360 | |
42202dd5 | 361 | res = (double)*val / (run_count * sqrt((double)run_count)); |
ddcacfa0 | 362 | |
9cffa8d5 | 363 | *val = (u64)res; |
42202dd5 | 364 | } |
ddcacfa0 | 365 | |
9cffa8d5 | 366 | static void update_avg(const char *name, int idx, u64 *avg, u64 *val) |
ef281a19 IM |
367 | { |
368 | *avg += *val; | |
369 | ||
370 | if (verbose > 1) | |
371 | fprintf(stderr, "debug: %20s[%d]: %Ld\n", name, idx, *val); | |
372 | } | |
42202dd5 IM |
373 | /* |
374 | * Calculate the averages and noises: | |
375 | */ | |
376 | static void calc_avg(void) | |
377 | { | |
378 | int i, j; | |
379 | ||
ef281a19 IM |
380 | if (verbose > 1) |
381 | fprintf(stderr, "\n"); | |
382 | ||
42202dd5 | 383 | for (i = 0; i < run_count; i++) { |
ef281a19 IM |
384 | update_avg("runtime", 0, &runtime_nsecs_avg, runtime_nsecs + i); |
385 | update_avg("walltime", 0, &walltime_nsecs_avg, walltime_nsecs + i); | |
386 | update_avg("runtime_cycles", 0, &runtime_cycles_avg, runtime_cycles + i); | |
42202dd5 IM |
387 | |
388 | for (j = 0; j < nr_counters; j++) { | |
ef281a19 IM |
389 | update_avg("counter/0", j, |
390 | event_res_avg[j]+0, event_res[i][j]+0); | |
391 | update_avg("counter/1", j, | |
392 | event_res_avg[j]+1, event_res[i][j]+1); | |
393 | update_avg("counter/2", j, | |
394 | event_res_avg[j]+2, event_res[i][j]+2); | |
f37a291c | 395 | if (event_scaled[i][j] != (u64)-1) |
566747e6 IM |
396 | update_avg("scaled", j, |
397 | event_scaled_avg + j, event_scaled[i]+j); | |
398 | else | |
399 | event_scaled_avg[j] = -1; | |
42202dd5 IM |
400 | } |
401 | } | |
402 | runtime_nsecs_avg /= run_count; | |
403 | walltime_nsecs_avg /= run_count; | |
404 | runtime_cycles_avg /= run_count; | |
405 | ||
406 | for (j = 0; j < nr_counters; j++) { | |
407 | event_res_avg[j][0] /= run_count; | |
408 | event_res_avg[j][1] /= run_count; | |
409 | event_res_avg[j][2] /= run_count; | |
410 | } | |
44db76c8 | 411 | |
42202dd5 IM |
412 | for (i = 0; i < run_count; i++) { |
413 | runtime_nsecs_noise += | |
9cffa8d5 | 414 | abs((s64)(runtime_nsecs[i] - runtime_nsecs_avg)); |
42202dd5 | 415 | walltime_nsecs_noise += |
9cffa8d5 | 416 | abs((s64)(walltime_nsecs[i] - walltime_nsecs_avg)); |
42202dd5 | 417 | runtime_cycles_noise += |
9cffa8d5 | 418 | abs((s64)(runtime_cycles[i] - runtime_cycles_avg)); |
42202dd5 IM |
419 | |
420 | for (j = 0; j < nr_counters; j++) { | |
421 | event_res_noise[j][0] += | |
9cffa8d5 | 422 | abs((s64)(event_res[i][j][0] - event_res_avg[j][0])); |
42202dd5 | 423 | event_res_noise[j][1] += |
9cffa8d5 | 424 | abs((s64)(event_res[i][j][1] - event_res_avg[j][1])); |
42202dd5 | 425 | event_res_noise[j][2] += |
9cffa8d5 | 426 | abs((s64)(event_res[i][j][2] - event_res_avg[j][2])); |
ddcacfa0 IM |
427 | } |
428 | } | |
44db76c8 | 429 | |
ef281a19 IM |
430 | normalize_noise(&runtime_nsecs_noise); |
431 | normalize_noise(&walltime_nsecs_noise); | |
432 | normalize_noise(&runtime_cycles_noise); | |
44db76c8 | 433 | |
42202dd5 | 434 | for (j = 0; j < nr_counters; j++) { |
ef281a19 IM |
435 | normalize_noise(&event_res_noise[j][0]); |
436 | normalize_noise(&event_res_noise[j][1]); | |
437 | normalize_noise(&event_res_noise[j][2]); | |
42202dd5 IM |
438 | } |
439 | } | |
440 | ||
441 | static void print_stat(int argc, const char **argv) | |
442 | { | |
443 | int i, counter; | |
444 | ||
445 | calc_avg(); | |
ddcacfa0 IM |
446 | |
447 | fflush(stdout); | |
448 | ||
449 | fprintf(stderr, "\n"); | |
44db76c8 IM |
450 | fprintf(stderr, " Performance counter stats for \'%s", argv[0]); |
451 | ||
452 | for (i = 1; i < argc; i++) | |
453 | fprintf(stderr, " %s", argv[i]); | |
454 | ||
42202dd5 IM |
455 | fprintf(stderr, "\'"); |
456 | if (run_count > 1) | |
457 | fprintf(stderr, " (%d runs)", run_count); | |
458 | fprintf(stderr, ":\n\n"); | |
2996f5dd | 459 | |
c04f5e5d IM |
460 | for (counter = 0; counter < nr_counters; counter++) |
461 | print_counter(counter); | |
ddcacfa0 | 462 | |
ddcacfa0 | 463 | fprintf(stderr, "\n"); |
566747e6 | 464 | fprintf(stderr, " %14.9f seconds time elapsed", |
42202dd5 | 465 | (double)walltime_nsecs_avg/1e9); |
566747e6 IM |
466 | if (run_count > 1) { |
467 | fprintf(stderr, " ( +- %7.3f%% )", | |
468 | 100.0*(double)walltime_nsecs_noise/(double)walltime_nsecs_avg); | |
469 | } | |
470 | fprintf(stderr, "\n\n"); | |
ddcacfa0 IM |
471 | } |
472 | ||
f7b7c26e PZ |
473 | static volatile int signr = -1; |
474 | ||
5242519b | 475 | static void skip_signal(int signo) |
ddcacfa0 | 476 | { |
f7b7c26e PZ |
477 | signr = signo; |
478 | } | |
479 | ||
480 | static void sig_atexit(void) | |
481 | { | |
482 | if (signr == -1) | |
483 | return; | |
484 | ||
485 | signal(signr, SIG_DFL); | |
486 | kill(getpid(), signr); | |
5242519b IM |
487 | } |
488 | ||
489 | static const char * const stat_usage[] = { | |
490 | "perf stat [<options>] <command>", | |
491 | NULL | |
492 | }; | |
493 | ||
5242519b IM |
494 | static const struct option options[] = { |
495 | OPT_CALLBACK('e', "event", NULL, "event", | |
86847b62 TG |
496 | "event selector. use 'perf list' to list available events", |
497 | parse_events), | |
5242519b IM |
498 | OPT_BOOLEAN('i', "inherit", &inherit, |
499 | "child tasks inherit counters"), | |
500 | OPT_INTEGER('p', "pid", &target_pid, | |
501 | "stat events on existing pid"), | |
502 | OPT_BOOLEAN('a', "all-cpus", &system_wide, | |
3d632595 | 503 | "system-wide collection from all CPUs"), |
86847b62 | 504 | OPT_BOOLEAN('S', "scale", &scale, |
3d632595 | 505 | "scale/normalize counters"), |
743ee1f8 IM |
506 | OPT_BOOLEAN('v', "verbose", &verbose, |
507 | "be more verbose (show counter open errors, etc)"), | |
42202dd5 IM |
508 | OPT_INTEGER('r', "repeat", &run_count, |
509 | "repeat command and print average + stddev (max: 100)"), | |
0cfb7a13 IM |
510 | OPT_BOOLEAN('n', "null", &null_run, |
511 | "null run - dont start any counters"), | |
5242519b IM |
512 | OPT_END() |
513 | }; | |
514 | ||
f37a291c | 515 | int cmd_stat(int argc, const char **argv, const char *prefix __used) |
5242519b | 516 | { |
42202dd5 IM |
517 | int status; |
518 | ||
5242519b IM |
519 | argc = parse_options(argc, argv, options, stat_usage, 0); |
520 | if (!argc) | |
521 | usage_with_options(stat_usage, options); | |
42202dd5 IM |
522 | if (run_count <= 0 || run_count > MAX_RUN) |
523 | usage_with_options(stat_usage, options); | |
ddcacfa0 | 524 | |
c3043569 JSR |
525 | /* Set attrs and nr_counters if no event is selected and !null_run */ |
526 | if (!null_run && !nr_counters) { | |
527 | memcpy(attrs, default_attrs, sizeof(default_attrs)); | |
528 | nr_counters = ARRAY_SIZE(default_attrs); | |
529 | } | |
ddcacfa0 | 530 | |
ddcacfa0 IM |
531 | nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
532 | assert(nr_cpus <= MAX_NR_CPUS); | |
f37a291c | 533 | assert((int)nr_cpus >= 0); |
ddcacfa0 | 534 | |
58d7e993 IM |
535 | /* |
536 | * We dont want to block the signals - that would cause | |
537 | * child tasks to inherit that and Ctrl-C would not work. | |
538 | * What we want is for Ctrl-C to work in the exec()-ed | |
539 | * task, but being ignored by perf stat itself: | |
540 | */ | |
f7b7c26e | 541 | atexit(sig_atexit); |
58d7e993 IM |
542 | signal(SIGINT, skip_signal); |
543 | signal(SIGALRM, skip_signal); | |
544 | signal(SIGABRT, skip_signal); | |
545 | ||
42202dd5 IM |
546 | status = 0; |
547 | for (run_idx = 0; run_idx < run_count; run_idx++) { | |
548 | if (run_count != 1 && verbose) | |
3d632595 | 549 | fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1); |
42202dd5 IM |
550 | status = run_perf_stat(argc, argv); |
551 | } | |
552 | ||
553 | print_stat(argc, argv); | |
554 | ||
555 | return status; | |
ddcacfa0 | 556 | } |