]>
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" | |
8f28827a FW |
45 | #include "util/event.h" |
46 | #include "util/debug.h" | |
60666c63 | 47 | #include "util/header.h" |
a12b51c4 | 48 | #include "util/cpumap.h" |
d6d901c2 | 49 | #include "util/thread.h" |
ddcacfa0 | 50 | |
ddcacfa0 | 51 | #include <sys/prctl.h> |
42202dd5 | 52 | #include <math.h> |
5af52b51 | 53 | #include <locale.h> |
16c8a109 | 54 | |
cdd6c482 | 55 | static struct perf_event_attr default_attrs[] = { |
ddcacfa0 | 56 | |
56aab464 IM |
57 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK }, |
58 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES }, | |
59 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS }, | |
60 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS }, | |
f4dbfa8f | 61 | |
56aab464 IM |
62 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES }, |
63 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS }, | |
56aab464 IM |
64 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, |
65 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES }, | |
dd86e72a IM |
66 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES }, |
67 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES }, | |
f4dbfa8f | 68 | |
ddcacfa0 | 69 | }; |
5242519b | 70 | |
c0555642 | 71 | static bool system_wide = false; |
c45c6ea2 | 72 | static int nr_cpus = 0; |
3d632595 | 73 | static int run_idx = 0; |
ddcacfa0 | 74 | |
3d632595 | 75 | static int run_count = 1; |
2e6cdf99 | 76 | static bool no_inherit = false; |
c0555642 | 77 | static bool scale = true; |
933da83a | 78 | static pid_t target_pid = -1; |
d6d901c2 ZY |
79 | static pid_t target_tid = -1; |
80 | static pid_t *all_tids = NULL; | |
81 | static int thread_num = 0; | |
933da83a | 82 | static pid_t child_pid = -1; |
c0555642 | 83 | static bool null_run = false; |
5af52b51 | 84 | static bool big_num = false; |
c45c6ea2 | 85 | static const char *cpu_list; |
5af52b51 | 86 | |
ddcacfa0 | 87 | |
d6d901c2 | 88 | static int *fd[MAX_NR_CPUS][MAX_COUNTERS]; |
42202dd5 | 89 | |
63d40deb | 90 | static int event_scaled[MAX_COUNTERS]; |
3d632595 | 91 | |
60666c63 LW |
92 | static volatile int done = 0; |
93 | ||
506d4bc8 PZ |
94 | struct stats |
95 | { | |
8a02631a | 96 | double n, mean, M2; |
506d4bc8 | 97 | }; |
42202dd5 | 98 | |
9e9772c4 PZ |
99 | static void update_stats(struct stats *stats, u64 val) |
100 | { | |
8a02631a | 101 | double delta; |
9e9772c4 | 102 | |
8a02631a PZ |
103 | stats->n++; |
104 | delta = val - stats->mean; | |
105 | stats->mean += delta / stats->n; | |
106 | stats->M2 += delta*(val - stats->mean); | |
9e9772c4 PZ |
107 | } |
108 | ||
506d4bc8 PZ |
109 | static double avg_stats(struct stats *stats) |
110 | { | |
8a02631a | 111 | return stats->mean; |
506d4bc8 | 112 | } |
42202dd5 | 113 | |
506d4bc8 | 114 | /* |
63d40deb PZ |
115 | * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance |
116 | * | |
8a02631a PZ |
117 | * (\Sum n_i^2) - ((\Sum n_i)^2)/n |
118 | * s^2 = ------------------------------- | |
119 | * n - 1 | |
63d40deb PZ |
120 | * |
121 | * http://en.wikipedia.org/wiki/Stddev | |
122 | * | |
123 | * The std dev of the mean is related to the std dev by: | |
124 | * | |
125 | * s | |
126 | * s_mean = ------- | |
127 | * sqrt(n) | |
128 | * | |
506d4bc8 PZ |
129 | */ |
130 | static double stddev_stats(struct stats *stats) | |
131 | { | |
8a02631a PZ |
132 | double variance = stats->M2 / (stats->n - 1); |
133 | double variance_mean = variance / stats->n; | |
42202dd5 | 134 | |
63d40deb | 135 | return sqrt(variance_mean); |
506d4bc8 | 136 | } |
42202dd5 | 137 | |
506d4bc8 | 138 | struct stats event_res_stats[MAX_COUNTERS][3]; |
506d4bc8 PZ |
139 | struct stats runtime_nsecs_stats; |
140 | struct stats walltime_nsecs_stats; | |
141 | struct stats runtime_cycles_stats; | |
11018201 | 142 | struct stats runtime_branches_stats; |
be1ac0d8 | 143 | |
b9ebdcc0 JSR |
144 | #define MATCH_EVENT(t, c, counter) \ |
145 | (attrs[counter].type == PERF_TYPE_##t && \ | |
146 | attrs[counter].config == PERF_COUNT_##c) | |
147 | ||
cca03c0a | 148 | #define ERR_PERF_OPEN \ |
cdd6c482 | 149 | "Error: counter %d, sys_perf_event_open() syscall returned with %d (%s)\n" |
cca03c0a | 150 | |
084ab9f8 | 151 | static int create_perf_stat_counter(int counter) |
ddcacfa0 | 152 | { |
cdd6c482 | 153 | struct perf_event_attr *attr = attrs + counter; |
d6d901c2 | 154 | int thread; |
084ab9f8 | 155 | int ncreated = 0; |
16c8a109 | 156 | |
ddcacfa0 | 157 | if (scale) |
a21ca2ca IM |
158 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | |
159 | PERF_FORMAT_TOTAL_TIME_RUNNING; | |
ddcacfa0 IM |
160 | |
161 | if (system_wide) { | |
c45c6ea2 | 162 | int cpu; |
f37a291c | 163 | |
cca03c0a | 164 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
d6d901c2 ZY |
165 | fd[cpu][counter][0] = sys_perf_event_open(attr, |
166 | -1, cpumap[cpu], -1, 0); | |
084ab9f8 ACM |
167 | if (fd[cpu][counter][0] < 0) |
168 | pr_debug(ERR_PERF_OPEN, counter, | |
169 | fd[cpu][counter][0], strerror(errno)); | |
170 | else | |
171 | ++ncreated; | |
ddcacfa0 IM |
172 | } |
173 | } else { | |
2e6cdf99 SE |
174 | attr->inherit = !no_inherit; |
175 | if (target_pid == -1 && target_tid == -1) { | |
6be2850e ZY |
176 | attr->disabled = 1; |
177 | attr->enable_on_exec = 1; | |
178 | } | |
d6d901c2 ZY |
179 | for (thread = 0; thread < thread_num; thread++) { |
180 | fd[0][counter][thread] = sys_perf_event_open(attr, | |
181 | all_tids[thread], -1, -1, 0); | |
084ab9f8 ACM |
182 | if (fd[0][counter][thread] < 0) |
183 | pr_debug(ERR_PERF_OPEN, counter, | |
184 | fd[0][counter][thread], | |
185 | strerror(errno)); | |
186 | else | |
187 | ++ncreated; | |
d6d901c2 | 188 | } |
ddcacfa0 | 189 | } |
084ab9f8 ACM |
190 | |
191 | return ncreated; | |
ddcacfa0 IM |
192 | } |
193 | ||
c04f5e5d IM |
194 | /* |
195 | * Does the counter have nsecs as a unit? | |
196 | */ | |
197 | static inline int nsec_counter(int counter) | |
198 | { | |
b9ebdcc0 JSR |
199 | if (MATCH_EVENT(SOFTWARE, SW_CPU_CLOCK, counter) || |
200 | MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) | |
c04f5e5d IM |
201 | return 1; |
202 | ||
203 | return 0; | |
204 | } | |
205 | ||
206 | /* | |
2996f5dd | 207 | * Read out the results of a single counter: |
c04f5e5d | 208 | */ |
2996f5dd | 209 | static void read_counter(int counter) |
c04f5e5d | 210 | { |
849abde9 | 211 | u64 count[3], single_count[3]; |
c45c6ea2 | 212 | int cpu; |
f37a291c | 213 | size_t res, nv; |
c04f5e5d | 214 | int scaled; |
d6d901c2 | 215 | int i, thread; |
c04f5e5d IM |
216 | |
217 | count[0] = count[1] = count[2] = 0; | |
2996f5dd | 218 | |
c04f5e5d | 219 | nv = scale ? 3 : 1; |
cca03c0a | 220 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
d6d901c2 ZY |
221 | for (thread = 0; thread < thread_num; thread++) { |
222 | if (fd[cpu][counter][thread] < 0) | |
223 | continue; | |
224 | ||
225 | res = read(fd[cpu][counter][thread], | |
226 | single_count, nv * sizeof(u64)); | |
227 | assert(res == nv * sizeof(u64)); | |
228 | ||
229 | close(fd[cpu][counter][thread]); | |
230 | fd[cpu][counter][thread] = -1; | |
231 | ||
232 | count[0] += single_count[0]; | |
233 | if (scale) { | |
234 | count[1] += single_count[1]; | |
235 | count[2] += single_count[2]; | |
236 | } | |
c04f5e5d IM |
237 | } |
238 | } | |
239 | ||
240 | scaled = 0; | |
241 | if (scale) { | |
242 | if (count[2] == 0) { | |
9e9772c4 | 243 | event_scaled[counter] = -1; |
2996f5dd | 244 | count[0] = 0; |
c04f5e5d IM |
245 | return; |
246 | } | |
2996f5dd | 247 | |
c04f5e5d | 248 | if (count[2] < count[1]) { |
9e9772c4 | 249 | event_scaled[counter] = 1; |
c04f5e5d IM |
250 | count[0] = (unsigned long long) |
251 | ((double)count[0] * count[1] / count[2] + 0.5); | |
252 | } | |
253 | } | |
9e9772c4 PZ |
254 | |
255 | for (i = 0; i < 3; i++) | |
256 | update_stats(&event_res_stats[counter][i], count[i]); | |
257 | ||
258 | if (verbose) { | |
259 | fprintf(stderr, "%s: %Ld %Ld %Ld\n", event_name(counter), | |
260 | count[0], count[1], count[2]); | |
261 | } | |
262 | ||
be1ac0d8 IM |
263 | /* |
264 | * Save the full runtime - to allow normalization during printout: | |
265 | */ | |
b9ebdcc0 | 266 | if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) |
9e9772c4 | 267 | update_stats(&runtime_nsecs_stats, count[0]); |
b9ebdcc0 | 268 | if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter)) |
9e9772c4 | 269 | update_stats(&runtime_cycles_stats, count[0]); |
11018201 AB |
270 | if (MATCH_EVENT(HARDWARE, HW_BRANCH_INSTRUCTIONS, counter)) |
271 | update_stats(&runtime_branches_stats, count[0]); | |
2996f5dd IM |
272 | } |
273 | ||
f37a291c | 274 | static int run_perf_stat(int argc __used, const char **argv) |
42202dd5 IM |
275 | { |
276 | unsigned long long t0, t1; | |
277 | int status = 0; | |
084ab9f8 | 278 | int counter, ncreated = 0; |
051ae7f7 | 279 | int child_ready_pipe[2], go_pipe[2]; |
6be2850e | 280 | const bool forks = (argc > 0); |
051ae7f7 | 281 | char buf; |
42202dd5 IM |
282 | |
283 | if (!system_wide) | |
284 | nr_cpus = 1; | |
285 | ||
60666c63 | 286 | if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) { |
051ae7f7 PM |
287 | perror("failed to create pipes"); |
288 | exit(1); | |
289 | } | |
290 | ||
60666c63 | 291 | if (forks) { |
6be2850e | 292 | if ((child_pid = fork()) < 0) |
60666c63 LW |
293 | perror("failed to fork"); |
294 | ||
6be2850e | 295 | if (!child_pid) { |
60666c63 LW |
296 | close(child_ready_pipe[0]); |
297 | close(go_pipe[1]); | |
298 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); | |
299 | ||
300 | /* | |
301 | * Do a dummy execvp to get the PLT entry resolved, | |
302 | * so we avoid the resolver overhead on the real | |
303 | * execvp call. | |
304 | */ | |
305 | execvp("", (char **)argv); | |
306 | ||
307 | /* | |
308 | * Tell the parent we're ready to go | |
309 | */ | |
310 | close(child_ready_pipe[1]); | |
311 | ||
312 | /* | |
313 | * Wait until the parent tells us to go. | |
314 | */ | |
315 | if (read(go_pipe[0], &buf, 1) == -1) | |
316 | perror("unable to read pipe"); | |
317 | ||
318 | execvp(argv[0], (char **)argv); | |
319 | ||
320 | perror(argv[0]); | |
321 | exit(-1); | |
322 | } | |
051ae7f7 | 323 | |
d6d901c2 ZY |
324 | if (target_tid == -1 && target_pid == -1 && !system_wide) |
325 | all_tids[0] = child_pid; | |
326 | ||
051ae7f7 | 327 | /* |
60666c63 | 328 | * Wait for the child to be ready to exec. |
051ae7f7 PM |
329 | */ |
330 | close(child_ready_pipe[1]); | |
60666c63 LW |
331 | close(go_pipe[0]); |
332 | if (read(child_ready_pipe[0], &buf, 1) == -1) | |
a92bef0f | 333 | perror("unable to read pipe"); |
60666c63 | 334 | close(child_ready_pipe[0]); |
051ae7f7 PM |
335 | } |
336 | ||
42202dd5 | 337 | for (counter = 0; counter < nr_counters; counter++) |
084ab9f8 ACM |
338 | ncreated += create_perf_stat_counter(counter); |
339 | ||
340 | if (ncreated == 0) { | |
341 | pr_err("No permission to collect %sstats.\n" | |
342 | "Consider tweaking /proc/sys/kernel/perf_event_paranoid.\n", | |
343 | system_wide ? "system-wide " : ""); | |
344 | if (child_pid != -1) | |
345 | kill(child_pid, SIGTERM); | |
346 | return -1; | |
347 | } | |
42202dd5 IM |
348 | |
349 | /* | |
350 | * Enable counters and exec the command: | |
351 | */ | |
352 | t0 = rdclock(); | |
42202dd5 | 353 | |
60666c63 LW |
354 | if (forks) { |
355 | close(go_pipe[1]); | |
356 | wait(&status); | |
357 | } else { | |
6be2850e | 358 | while(!done) sleep(1); |
60666c63 | 359 | } |
42202dd5 | 360 | |
42202dd5 IM |
361 | t1 = rdclock(); |
362 | ||
9e9772c4 | 363 | update_stats(&walltime_nsecs_stats, t1 - t0); |
42202dd5 IM |
364 | |
365 | for (counter = 0; counter < nr_counters; counter++) | |
366 | read_counter(counter); | |
367 | ||
368 | return WEXITSTATUS(status); | |
369 | } | |
370 | ||
849abde9 | 371 | static void print_noise(int counter, double avg) |
42202dd5 | 372 | { |
849abde9 PZ |
373 | if (run_count == 1) |
374 | return; | |
375 | ||
376 | fprintf(stderr, " ( +- %7.3f%% )", | |
377 | 100 * stddev_stats(&event_res_stats[counter][0]) / avg); | |
42202dd5 IM |
378 | } |
379 | ||
849abde9 | 380 | static void nsec_printout(int counter, double avg) |
44175b6f | 381 | { |
506d4bc8 | 382 | double msecs = avg / 1e6; |
44175b6f | 383 | |
5af52b51 | 384 | fprintf(stderr, " %18.6f %-24s", msecs, event_name(counter)); |
44175b6f | 385 | |
b9ebdcc0 | 386 | if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) { |
506d4bc8 PZ |
387 | fprintf(stderr, " # %10.3f CPUs ", |
388 | avg / avg_stats(&walltime_nsecs_stats)); | |
44175b6f IM |
389 | } |
390 | } | |
391 | ||
849abde9 | 392 | static void abs_printout(int counter, double avg) |
44175b6f | 393 | { |
c7f7fea3 IM |
394 | double total, ratio = 0.0; |
395 | ||
5af52b51 SE |
396 | if (big_num) |
397 | fprintf(stderr, " %'18.0f %-24s", avg, event_name(counter)); | |
398 | else | |
399 | fprintf(stderr, " %18.0f %-24s", avg, event_name(counter)); | |
44175b6f | 400 | |
506d4bc8 | 401 | if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) { |
c7f7fea3 IM |
402 | total = avg_stats(&runtime_cycles_stats); |
403 | ||
404 | if (total) | |
405 | ratio = avg / total; | |
406 | ||
407 | fprintf(stderr, " # %10.3f IPC ", ratio); | |
7255fe2a LDM |
408 | } else if (MATCH_EVENT(HARDWARE, HW_BRANCH_MISSES, counter) && |
409 | runtime_branches_stats.n != 0) { | |
11018201 AB |
410 | total = avg_stats(&runtime_branches_stats); |
411 | ||
412 | if (total) | |
413 | ratio = avg * 100 / total; | |
414 | ||
dd86e72a | 415 | fprintf(stderr, " # %10.3f %% ", ratio); |
11018201 | 416 | |
7255fe2a | 417 | } else if (runtime_nsecs_stats.n != 0) { |
c7f7fea3 IM |
418 | total = avg_stats(&runtime_nsecs_stats); |
419 | ||
420 | if (total) | |
421 | ratio = 1000.0 * avg / total; | |
422 | ||
423 | fprintf(stderr, " # %10.3f M/sec", ratio); | |
44175b6f | 424 | } |
44175b6f IM |
425 | } |
426 | ||
2996f5dd IM |
427 | /* |
428 | * Print out the results of a single counter: | |
429 | */ | |
430 | static void print_counter(int counter) | |
431 | { | |
849abde9 | 432 | double avg = avg_stats(&event_res_stats[counter][0]); |
63d40deb | 433 | int scaled = event_scaled[counter]; |
2996f5dd | 434 | |
2996f5dd | 435 | if (scaled == -1) { |
5af52b51 | 436 | fprintf(stderr, " %18s %-24s\n", |
2996f5dd IM |
437 | "<not counted>", event_name(counter)); |
438 | return; | |
439 | } | |
c04f5e5d | 440 | |
44175b6f | 441 | if (nsec_counter(counter)) |
849abde9 | 442 | nsec_printout(counter, avg); |
44175b6f | 443 | else |
849abde9 PZ |
444 | abs_printout(counter, avg); |
445 | ||
446 | print_noise(counter, avg); | |
506d4bc8 PZ |
447 | |
448 | if (scaled) { | |
449 | double avg_enabled, avg_running; | |
450 | ||
451 | avg_enabled = avg_stats(&event_res_stats[counter][1]); | |
452 | avg_running = avg_stats(&event_res_stats[counter][2]); | |
d7c29318 | 453 | |
210ad39f | 454 | fprintf(stderr, " (scaled from %.2f%%)", |
506d4bc8 PZ |
455 | 100 * avg_running / avg_enabled); |
456 | } | |
44175b6f | 457 | |
c04f5e5d IM |
458 | fprintf(stderr, "\n"); |
459 | } | |
460 | ||
42202dd5 IM |
461 | static void print_stat(int argc, const char **argv) |
462 | { | |
463 | int i, counter; | |
464 | ||
ddcacfa0 IM |
465 | fflush(stdout); |
466 | ||
467 | fprintf(stderr, "\n"); | |
60666c63 | 468 | fprintf(stderr, " Performance counter stats for "); |
d6d901c2 | 469 | if(target_pid == -1 && target_tid == -1) { |
60666c63 LW |
470 | fprintf(stderr, "\'%s", argv[0]); |
471 | for (i = 1; i < argc; i++) | |
472 | fprintf(stderr, " %s", argv[i]); | |
d6d901c2 ZY |
473 | } else if (target_pid != -1) |
474 | fprintf(stderr, "process id \'%d", target_pid); | |
475 | else | |
476 | fprintf(stderr, "thread id \'%d", target_tid); | |
44db76c8 | 477 | |
42202dd5 IM |
478 | fprintf(stderr, "\'"); |
479 | if (run_count > 1) | |
480 | fprintf(stderr, " (%d runs)", run_count); | |
481 | fprintf(stderr, ":\n\n"); | |
2996f5dd | 482 | |
c04f5e5d IM |
483 | for (counter = 0; counter < nr_counters; counter++) |
484 | print_counter(counter); | |
ddcacfa0 | 485 | |
ddcacfa0 | 486 | fprintf(stderr, "\n"); |
5af52b51 | 487 | fprintf(stderr, " %18.9f seconds time elapsed", |
506d4bc8 | 488 | avg_stats(&walltime_nsecs_stats)/1e9); |
566747e6 IM |
489 | if (run_count > 1) { |
490 | fprintf(stderr, " ( +- %7.3f%% )", | |
506d4bc8 PZ |
491 | 100*stddev_stats(&walltime_nsecs_stats) / |
492 | avg_stats(&walltime_nsecs_stats)); | |
566747e6 IM |
493 | } |
494 | fprintf(stderr, "\n\n"); | |
ddcacfa0 IM |
495 | } |
496 | ||
f7b7c26e PZ |
497 | static volatile int signr = -1; |
498 | ||
5242519b | 499 | static void skip_signal(int signo) |
ddcacfa0 | 500 | { |
6be2850e | 501 | if(child_pid == -1) |
60666c63 LW |
502 | done = 1; |
503 | ||
f7b7c26e PZ |
504 | signr = signo; |
505 | } | |
506 | ||
507 | static void sig_atexit(void) | |
508 | { | |
933da83a CW |
509 | if (child_pid != -1) |
510 | kill(child_pid, SIGTERM); | |
511 | ||
f7b7c26e PZ |
512 | if (signr == -1) |
513 | return; | |
514 | ||
515 | signal(signr, SIG_DFL); | |
516 | kill(getpid(), signr); | |
5242519b IM |
517 | } |
518 | ||
519 | static const char * const stat_usage[] = { | |
60666c63 | 520 | "perf stat [<options>] [<command>]", |
5242519b IM |
521 | NULL |
522 | }; | |
523 | ||
5242519b IM |
524 | static const struct option options[] = { |
525 | OPT_CALLBACK('e', "event", NULL, "event", | |
86847b62 TG |
526 | "event selector. use 'perf list' to list available events", |
527 | parse_events), | |
2e6cdf99 SE |
528 | OPT_BOOLEAN('i', "no-inherit", &no_inherit, |
529 | "child tasks do not inherit counters"), | |
5242519b | 530 | OPT_INTEGER('p', "pid", &target_pid, |
d6d901c2 ZY |
531 | "stat events on existing process id"), |
532 | OPT_INTEGER('t', "tid", &target_tid, | |
533 | "stat events on existing thread id"), | |
5242519b | 534 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
3d632595 | 535 | "system-wide collection from all CPUs"), |
b26bc5a7 | 536 | OPT_BOOLEAN('c', "scale", &scale, |
3d632595 | 537 | "scale/normalize counters"), |
c0555642 | 538 | OPT_INCR('v', "verbose", &verbose, |
743ee1f8 | 539 | "be more verbose (show counter open errors, etc)"), |
42202dd5 IM |
540 | OPT_INTEGER('r', "repeat", &run_count, |
541 | "repeat command and print average + stddev (max: 100)"), | |
0cfb7a13 IM |
542 | OPT_BOOLEAN('n', "null", &null_run, |
543 | "null run - dont start any counters"), | |
5af52b51 SE |
544 | OPT_BOOLEAN('B', "big-num", &big_num, |
545 | "print large numbers with thousands\' separators"), | |
c45c6ea2 SE |
546 | OPT_STRING('C', "cpu", &cpu_list, "cpu", |
547 | "list of cpus to monitor in system-wide"), | |
5242519b IM |
548 | OPT_END() |
549 | }; | |
550 | ||
f37a291c | 551 | int cmd_stat(int argc, const char **argv, const char *prefix __used) |
5242519b | 552 | { |
42202dd5 | 553 | int status; |
d6d901c2 | 554 | int i,j; |
42202dd5 | 555 | |
5af52b51 SE |
556 | setlocale(LC_ALL, ""); |
557 | ||
a0541234 AB |
558 | argc = parse_options(argc, argv, options, stat_usage, |
559 | PARSE_OPT_STOP_AT_NON_OPTION); | |
d6d901c2 | 560 | if (!argc && target_pid == -1 && target_tid == -1) |
5242519b | 561 | usage_with_options(stat_usage, options); |
9e9772c4 | 562 | if (run_count <= 0) |
42202dd5 | 563 | usage_with_options(stat_usage, options); |
ddcacfa0 | 564 | |
c3043569 JSR |
565 | /* Set attrs and nr_counters if no event is selected and !null_run */ |
566 | if (!null_run && !nr_counters) { | |
567 | memcpy(attrs, default_attrs, sizeof(default_attrs)); | |
568 | nr_counters = ARRAY_SIZE(default_attrs); | |
569 | } | |
ddcacfa0 | 570 | |
a12b51c4 | 571 | if (system_wide) |
c45c6ea2 | 572 | nr_cpus = read_cpu_map(cpu_list); |
a12b51c4 PM |
573 | else |
574 | nr_cpus = 1; | |
ddcacfa0 | 575 | |
c45c6ea2 SE |
576 | if (nr_cpus < 1) |
577 | usage_with_options(stat_usage, options); | |
578 | ||
d6d901c2 ZY |
579 | if (target_pid != -1) { |
580 | target_tid = target_pid; | |
581 | thread_num = find_all_tid(target_pid, &all_tids); | |
582 | if (thread_num <= 0) { | |
583 | fprintf(stderr, "Can't find all threads of pid %d\n", | |
584 | target_pid); | |
585 | usage_with_options(stat_usage, options); | |
586 | } | |
587 | } else { | |
588 | all_tids=malloc(sizeof(pid_t)); | |
589 | if (!all_tids) | |
590 | return -ENOMEM; | |
591 | ||
592 | all_tids[0] = target_tid; | |
593 | thread_num = 1; | |
594 | } | |
595 | ||
596 | for (i = 0; i < MAX_NR_CPUS; i++) { | |
597 | for (j = 0; j < MAX_COUNTERS; j++) { | |
598 | fd[i][j] = malloc(sizeof(int)*thread_num); | |
599 | if (!fd[i][j]) | |
600 | return -ENOMEM; | |
601 | } | |
602 | } | |
603 | ||
58d7e993 IM |
604 | /* |
605 | * We dont want to block the signals - that would cause | |
606 | * child tasks to inherit that and Ctrl-C would not work. | |
607 | * What we want is for Ctrl-C to work in the exec()-ed | |
608 | * task, but being ignored by perf stat itself: | |
609 | */ | |
f7b7c26e | 610 | atexit(sig_atexit); |
58d7e993 IM |
611 | signal(SIGINT, skip_signal); |
612 | signal(SIGALRM, skip_signal); | |
613 | signal(SIGABRT, skip_signal); | |
614 | ||
42202dd5 IM |
615 | status = 0; |
616 | for (run_idx = 0; run_idx < run_count; run_idx++) { | |
617 | if (run_count != 1 && verbose) | |
3d632595 | 618 | fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1); |
42202dd5 IM |
619 | status = run_perf_stat(argc, argv); |
620 | } | |
621 | ||
084ab9f8 ACM |
622 | if (status != -1) |
623 | print_stat(argc, argv); | |
42202dd5 IM |
624 | |
625 | return status; | |
ddcacfa0 | 626 | } |