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 | 45 | #include "util/event.h" |
69aad6f1 | 46 | #include "util/evsel.h" |
8f28827a | 47 | #include "util/debug.h" |
60666c63 | 48 | #include "util/header.h" |
a12b51c4 | 49 | #include "util/cpumap.h" |
d6d901c2 | 50 | #include "util/thread.h" |
ddcacfa0 | 51 | |
ddcacfa0 | 52 | #include <sys/prctl.h> |
42202dd5 | 53 | #include <math.h> |
5af52b51 | 54 | #include <locale.h> |
16c8a109 | 55 | |
d7470b6a SE |
56 | #define DEFAULT_SEPARATOR " " |
57 | ||
cdd6c482 | 58 | static struct perf_event_attr default_attrs[] = { |
ddcacfa0 | 59 | |
56aab464 IM |
60 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK }, |
61 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES }, | |
62 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS }, | |
63 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS }, | |
f4dbfa8f | 64 | |
56aab464 IM |
65 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES }, |
66 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS }, | |
56aab464 IM |
67 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, |
68 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES }, | |
dd86e72a IM |
69 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_REFERENCES }, |
70 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CACHE_MISSES }, | |
f4dbfa8f | 71 | |
ddcacfa0 | 72 | }; |
5242519b | 73 | |
c0555642 | 74 | static bool system_wide = false; |
60d567e2 | 75 | static struct cpu_map *cpus; |
3d632595 | 76 | static int run_idx = 0; |
ddcacfa0 | 77 | |
3d632595 | 78 | static int run_count = 1; |
2e6cdf99 | 79 | static bool no_inherit = false; |
c0555642 | 80 | static bool scale = true; |
f5b4a9c3 | 81 | static bool no_aggr = false; |
933da83a | 82 | static pid_t target_pid = -1; |
d6d901c2 | 83 | static pid_t target_tid = -1; |
5c98d466 | 84 | static struct thread_map *threads; |
933da83a | 85 | static pid_t child_pid = -1; |
c0555642 | 86 | static bool null_run = false; |
201e0b06 | 87 | static bool big_num = true; |
d7470b6a | 88 | static int big_num_opt = -1; |
c45c6ea2 | 89 | static const char *cpu_list; |
d7470b6a SE |
90 | static const char *csv_sep = NULL; |
91 | static bool csv_output = false; | |
5af52b51 | 92 | |
60666c63 LW |
93 | static volatile int done = 0; |
94 | ||
506d4bc8 PZ |
95 | struct stats |
96 | { | |
8a02631a | 97 | double n, mean, M2; |
506d4bc8 | 98 | }; |
42202dd5 | 99 | |
69aad6f1 ACM |
100 | struct perf_stat { |
101 | struct stats res_stats[3]; | |
69aad6f1 ACM |
102 | }; |
103 | ||
c52b12ed | 104 | static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel) |
69aad6f1 | 105 | { |
c52b12ed | 106 | evsel->priv = zalloc(sizeof(struct perf_stat)); |
69aad6f1 ACM |
107 | return evsel->priv == NULL ? -ENOMEM : 0; |
108 | } | |
109 | ||
110 | static void perf_evsel__free_stat_priv(struct perf_evsel *evsel) | |
111 | { | |
112 | free(evsel->priv); | |
113 | evsel->priv = NULL; | |
114 | } | |
115 | ||
9e9772c4 PZ |
116 | static void update_stats(struct stats *stats, u64 val) |
117 | { | |
8a02631a | 118 | double delta; |
9e9772c4 | 119 | |
8a02631a PZ |
120 | stats->n++; |
121 | delta = val - stats->mean; | |
122 | stats->mean += delta / stats->n; | |
123 | stats->M2 += delta*(val - stats->mean); | |
9e9772c4 PZ |
124 | } |
125 | ||
506d4bc8 PZ |
126 | static double avg_stats(struct stats *stats) |
127 | { | |
8a02631a | 128 | return stats->mean; |
506d4bc8 | 129 | } |
42202dd5 | 130 | |
506d4bc8 | 131 | /* |
63d40deb PZ |
132 | * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance |
133 | * | |
8a02631a PZ |
134 | * (\Sum n_i^2) - ((\Sum n_i)^2)/n |
135 | * s^2 = ------------------------------- | |
136 | * n - 1 | |
63d40deb PZ |
137 | * |
138 | * http://en.wikipedia.org/wiki/Stddev | |
139 | * | |
140 | * The std dev of the mean is related to the std dev by: | |
141 | * | |
142 | * s | |
143 | * s_mean = ------- | |
144 | * sqrt(n) | |
145 | * | |
506d4bc8 PZ |
146 | */ |
147 | static double stddev_stats(struct stats *stats) | |
148 | { | |
8a02631a PZ |
149 | double variance = stats->M2 / (stats->n - 1); |
150 | double variance_mean = variance / stats->n; | |
42202dd5 | 151 | |
63d40deb | 152 | return sqrt(variance_mean); |
506d4bc8 | 153 | } |
42202dd5 | 154 | |
f5b4a9c3 SE |
155 | struct stats runtime_nsecs_stats[MAX_NR_CPUS]; |
156 | struct stats runtime_cycles_stats[MAX_NR_CPUS]; | |
157 | struct stats runtime_branches_stats[MAX_NR_CPUS]; | |
506d4bc8 | 158 | struct stats walltime_nsecs_stats; |
be1ac0d8 | 159 | |
48290609 | 160 | static int create_perf_stat_counter(struct perf_evsel *evsel) |
ddcacfa0 | 161 | { |
69aad6f1 | 162 | struct perf_event_attr *attr = &evsel->attr; |
16c8a109 | 163 | |
ddcacfa0 | 164 | if (scale) |
a21ca2ca IM |
165 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | |
166 | PERF_FORMAT_TOTAL_TIME_RUNNING; | |
ddcacfa0 | 167 | |
48290609 | 168 | if (system_wide) |
86bd5e86 | 169 | return perf_evsel__open_per_cpu(evsel, cpus); |
48290609 ACM |
170 | |
171 | attr->inherit = !no_inherit; | |
172 | if (target_pid == -1 && target_tid == -1) { | |
173 | attr->disabled = 1; | |
174 | attr->enable_on_exec = 1; | |
ddcacfa0 | 175 | } |
084ab9f8 | 176 | |
86bd5e86 | 177 | return perf_evsel__open_per_thread(evsel, threads); |
ddcacfa0 IM |
178 | } |
179 | ||
c04f5e5d IM |
180 | /* |
181 | * Does the counter have nsecs as a unit? | |
182 | */ | |
daec78a0 | 183 | static inline int nsec_counter(struct perf_evsel *evsel) |
c04f5e5d | 184 | { |
daec78a0 ACM |
185 | if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) || |
186 | perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK)) | |
c04f5e5d IM |
187 | return 1; |
188 | ||
189 | return 0; | |
190 | } | |
191 | ||
192 | /* | |
2996f5dd | 193 | * Read out the results of a single counter: |
f5b4a9c3 | 194 | * aggregate counts across CPUs in system-wide mode |
c04f5e5d | 195 | */ |
c52b12ed | 196 | static int read_counter_aggr(struct perf_evsel *counter) |
c04f5e5d | 197 | { |
69aad6f1 | 198 | struct perf_stat *ps = counter->priv; |
c52b12ed ACM |
199 | u64 *count = counter->counts->aggr.values; |
200 | int i; | |
2996f5dd | 201 | |
5c98d466 | 202 | if (__perf_evsel__read(counter, cpus->nr, threads->nr, scale) < 0) |
c52b12ed | 203 | return -1; |
9e9772c4 PZ |
204 | |
205 | for (i = 0; i < 3; i++) | |
69aad6f1 | 206 | update_stats(&ps->res_stats[i], count[i]); |
9e9772c4 PZ |
207 | |
208 | if (verbose) { | |
9486aa38 ACM |
209 | fprintf(stderr, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", |
210 | event_name(counter), count[0], count[1], count[2]); | |
9e9772c4 PZ |
211 | } |
212 | ||
be1ac0d8 IM |
213 | /* |
214 | * Save the full runtime - to allow normalization during printout: | |
215 | */ | |
daec78a0 | 216 | if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK)) |
f5b4a9c3 | 217 | update_stats(&runtime_nsecs_stats[0], count[0]); |
daec78a0 | 218 | if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES)) |
f5b4a9c3 | 219 | update_stats(&runtime_cycles_stats[0], count[0]); |
daec78a0 | 220 | if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS)) |
f5b4a9c3 | 221 | update_stats(&runtime_branches_stats[0], count[0]); |
c52b12ed ACM |
222 | |
223 | return 0; | |
f5b4a9c3 SE |
224 | } |
225 | ||
226 | /* | |
227 | * Read out the results of a single counter: | |
228 | * do not aggregate counts across CPUs in system-wide mode | |
229 | */ | |
c52b12ed | 230 | static int read_counter(struct perf_evsel *counter) |
f5b4a9c3 | 231 | { |
c52b12ed | 232 | u64 *count; |
f5b4a9c3 | 233 | int cpu; |
f5b4a9c3 | 234 | |
60d567e2 | 235 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
c52b12ed ACM |
236 | if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0) |
237 | return -1; | |
f5b4a9c3 | 238 | |
c52b12ed | 239 | count = counter->counts->cpu[cpu].values; |
f5b4a9c3 | 240 | |
daec78a0 | 241 | if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK)) |
f5b4a9c3 | 242 | update_stats(&runtime_nsecs_stats[cpu], count[0]); |
daec78a0 | 243 | if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES)) |
f5b4a9c3 | 244 | update_stats(&runtime_cycles_stats[cpu], count[0]); |
daec78a0 | 245 | if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS)) |
f5b4a9c3 SE |
246 | update_stats(&runtime_branches_stats[cpu], count[0]); |
247 | } | |
c52b12ed ACM |
248 | |
249 | return 0; | |
2996f5dd IM |
250 | } |
251 | ||
f37a291c | 252 | static int run_perf_stat(int argc __used, const char **argv) |
42202dd5 IM |
253 | { |
254 | unsigned long long t0, t1; | |
69aad6f1 | 255 | struct perf_evsel *counter; |
42202dd5 | 256 | int status = 0; |
051ae7f7 | 257 | int child_ready_pipe[2], go_pipe[2]; |
6be2850e | 258 | const bool forks = (argc > 0); |
051ae7f7 | 259 | char buf; |
42202dd5 | 260 | |
60666c63 | 261 | if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) { |
051ae7f7 PM |
262 | perror("failed to create pipes"); |
263 | exit(1); | |
264 | } | |
265 | ||
60666c63 | 266 | if (forks) { |
6be2850e | 267 | if ((child_pid = fork()) < 0) |
60666c63 LW |
268 | perror("failed to fork"); |
269 | ||
6be2850e | 270 | if (!child_pid) { |
60666c63 LW |
271 | close(child_ready_pipe[0]); |
272 | close(go_pipe[1]); | |
273 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); | |
274 | ||
275 | /* | |
276 | * Do a dummy execvp to get the PLT entry resolved, | |
277 | * so we avoid the resolver overhead on the real | |
278 | * execvp call. | |
279 | */ | |
280 | execvp("", (char **)argv); | |
281 | ||
282 | /* | |
283 | * Tell the parent we're ready to go | |
284 | */ | |
285 | close(child_ready_pipe[1]); | |
286 | ||
287 | /* | |
288 | * Wait until the parent tells us to go. | |
289 | */ | |
290 | if (read(go_pipe[0], &buf, 1) == -1) | |
291 | perror("unable to read pipe"); | |
292 | ||
293 | execvp(argv[0], (char **)argv); | |
294 | ||
295 | perror(argv[0]); | |
296 | exit(-1); | |
297 | } | |
051ae7f7 | 298 | |
d6d901c2 | 299 | if (target_tid == -1 && target_pid == -1 && !system_wide) |
5c98d466 | 300 | threads->map[0] = child_pid; |
d6d901c2 | 301 | |
051ae7f7 | 302 | /* |
60666c63 | 303 | * Wait for the child to be ready to exec. |
051ae7f7 PM |
304 | */ |
305 | close(child_ready_pipe[1]); | |
60666c63 LW |
306 | close(go_pipe[0]); |
307 | if (read(child_ready_pipe[0], &buf, 1) == -1) | |
a92bef0f | 308 | perror("unable to read pipe"); |
60666c63 | 309 | close(child_ready_pipe[0]); |
051ae7f7 PM |
310 | } |
311 | ||
48290609 ACM |
312 | list_for_each_entry(counter, &evsel_list, node) { |
313 | if (create_perf_stat_counter(counter) < 0) { | |
314 | if (errno == -EPERM || errno == -EACCES) { | |
315 | error("You may not have permission to collect %sstats.\n" | |
316 | "\t Consider tweaking" | |
317 | " /proc/sys/kernel/perf_event_paranoid or running as root.", | |
318 | system_wide ? "system-wide " : ""); | |
5a3446bc DA |
319 | } else if (errno == ENOENT) { |
320 | error("%s event is not supported. ", event_name(counter)); | |
48290609 ACM |
321 | } else { |
322 | error("open_counter returned with %d (%s). " | |
323 | "/bin/dmesg may provide additional information.\n", | |
324 | errno, strerror(errno)); | |
325 | } | |
326 | if (child_pid != -1) | |
327 | kill(child_pid, SIGTERM); | |
328 | die("Not all events could be opened.\n"); | |
329 | return -1; | |
330 | } | |
084ab9f8 | 331 | } |
42202dd5 IM |
332 | |
333 | /* | |
334 | * Enable counters and exec the command: | |
335 | */ | |
336 | t0 = rdclock(); | |
42202dd5 | 337 | |
60666c63 LW |
338 | if (forks) { |
339 | close(go_pipe[1]); | |
340 | wait(&status); | |
341 | } else { | |
6be2850e | 342 | while(!done) sleep(1); |
60666c63 | 343 | } |
42202dd5 | 344 | |
42202dd5 IM |
345 | t1 = rdclock(); |
346 | ||
9e9772c4 | 347 | update_stats(&walltime_nsecs_stats, t1 - t0); |
42202dd5 | 348 | |
f5b4a9c3 | 349 | if (no_aggr) { |
c52b12ed | 350 | list_for_each_entry(counter, &evsel_list, node) { |
f5b4a9c3 | 351 | read_counter(counter); |
60d567e2 | 352 | perf_evsel__close_fd(counter, cpus->nr, 1); |
c52b12ed | 353 | } |
f5b4a9c3 | 354 | } else { |
c52b12ed | 355 | list_for_each_entry(counter, &evsel_list, node) { |
f5b4a9c3 | 356 | read_counter_aggr(counter); |
5c98d466 | 357 | perf_evsel__close_fd(counter, cpus->nr, threads->nr); |
c52b12ed | 358 | } |
f5b4a9c3 | 359 | } |
c52b12ed | 360 | |
42202dd5 IM |
361 | return WEXITSTATUS(status); |
362 | } | |
363 | ||
69aad6f1 | 364 | static void print_noise(struct perf_evsel *evsel, double avg) |
42202dd5 | 365 | { |
69aad6f1 ACM |
366 | struct perf_stat *ps; |
367 | ||
849abde9 PZ |
368 | if (run_count == 1) |
369 | return; | |
370 | ||
69aad6f1 | 371 | ps = evsel->priv; |
849abde9 | 372 | fprintf(stderr, " ( +- %7.3f%% )", |
69aad6f1 | 373 | 100 * stddev_stats(&ps->res_stats[0]) / avg); |
42202dd5 IM |
374 | } |
375 | ||
daec78a0 | 376 | static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg) |
44175b6f | 377 | { |
506d4bc8 | 378 | double msecs = avg / 1e6; |
d7470b6a SE |
379 | char cpustr[16] = { '\0', }; |
380 | const char *fmt = csv_output ? "%s%.6f%s%s" : "%s%18.6f%s%-24s"; | |
44175b6f | 381 | |
f5b4a9c3 | 382 | if (no_aggr) |
d7470b6a SE |
383 | sprintf(cpustr, "CPU%*d%s", |
384 | csv_output ? 0 : -4, | |
60d567e2 | 385 | cpus->map[cpu], csv_sep); |
d7470b6a | 386 | |
daec78a0 | 387 | fprintf(stderr, fmt, cpustr, msecs, csv_sep, event_name(evsel)); |
d7470b6a SE |
388 | |
389 | if (csv_output) | |
390 | return; | |
44175b6f | 391 | |
daec78a0 | 392 | if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK)) |
506d4bc8 PZ |
393 | fprintf(stderr, " # %10.3f CPUs ", |
394 | avg / avg_stats(&walltime_nsecs_stats)); | |
44175b6f IM |
395 | } |
396 | ||
daec78a0 | 397 | static void abs_printout(int cpu, struct perf_evsel *evsel, double avg) |
44175b6f | 398 | { |
c7f7fea3 | 399 | double total, ratio = 0.0; |
f5b4a9c3 | 400 | char cpustr[16] = { '\0', }; |
d7470b6a SE |
401 | const char *fmt; |
402 | ||
403 | if (csv_output) | |
404 | fmt = "%s%.0f%s%s"; | |
405 | else if (big_num) | |
406 | fmt = "%s%'18.0f%s%-24s"; | |
407 | else | |
408 | fmt = "%s%18.0f%s%-24s"; | |
f5b4a9c3 SE |
409 | |
410 | if (no_aggr) | |
d7470b6a SE |
411 | sprintf(cpustr, "CPU%*d%s", |
412 | csv_output ? 0 : -4, | |
60d567e2 | 413 | cpus->map[cpu], csv_sep); |
f5b4a9c3 SE |
414 | else |
415 | cpu = 0; | |
c7f7fea3 | 416 | |
daec78a0 | 417 | fprintf(stderr, fmt, cpustr, avg, csv_sep, event_name(evsel)); |
d7470b6a SE |
418 | |
419 | if (csv_output) | |
420 | return; | |
44175b6f | 421 | |
daec78a0 | 422 | if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) { |
f5b4a9c3 | 423 | total = avg_stats(&runtime_cycles_stats[cpu]); |
c7f7fea3 IM |
424 | |
425 | if (total) | |
426 | ratio = avg / total; | |
427 | ||
428 | fprintf(stderr, " # %10.3f IPC ", ratio); | |
daec78a0 | 429 | } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) && |
f5b4a9c3 SE |
430 | runtime_branches_stats[cpu].n != 0) { |
431 | total = avg_stats(&runtime_branches_stats[cpu]); | |
11018201 AB |
432 | |
433 | if (total) | |
434 | ratio = avg * 100 / total; | |
435 | ||
dd86e72a | 436 | fprintf(stderr, " # %10.3f %% ", ratio); |
11018201 | 437 | |
f5b4a9c3 SE |
438 | } else if (runtime_nsecs_stats[cpu].n != 0) { |
439 | total = avg_stats(&runtime_nsecs_stats[cpu]); | |
c7f7fea3 IM |
440 | |
441 | if (total) | |
442 | ratio = 1000.0 * avg / total; | |
443 | ||
444 | fprintf(stderr, " # %10.3f M/sec", ratio); | |
44175b6f | 445 | } |
44175b6f IM |
446 | } |
447 | ||
2996f5dd IM |
448 | /* |
449 | * Print out the results of a single counter: | |
f5b4a9c3 | 450 | * aggregated counts in system-wide mode |
2996f5dd | 451 | */ |
69aad6f1 | 452 | static void print_counter_aggr(struct perf_evsel *counter) |
2996f5dd | 453 | { |
69aad6f1 ACM |
454 | struct perf_stat *ps = counter->priv; |
455 | double avg = avg_stats(&ps->res_stats[0]); | |
c52b12ed | 456 | int scaled = counter->counts->scaled; |
2996f5dd | 457 | |
2996f5dd | 458 | if (scaled == -1) { |
d7470b6a SE |
459 | fprintf(stderr, "%*s%s%-24s\n", |
460 | csv_output ? 0 : 18, | |
461 | "<not counted>", csv_sep, event_name(counter)); | |
2996f5dd IM |
462 | return; |
463 | } | |
c04f5e5d | 464 | |
44175b6f | 465 | if (nsec_counter(counter)) |
f5b4a9c3 | 466 | nsec_printout(-1, counter, avg); |
44175b6f | 467 | else |
f5b4a9c3 | 468 | abs_printout(-1, counter, avg); |
849abde9 | 469 | |
d7470b6a SE |
470 | if (csv_output) { |
471 | fputc('\n', stderr); | |
472 | return; | |
473 | } | |
474 | ||
849abde9 | 475 | print_noise(counter, avg); |
506d4bc8 PZ |
476 | |
477 | if (scaled) { | |
478 | double avg_enabled, avg_running; | |
479 | ||
69aad6f1 ACM |
480 | avg_enabled = avg_stats(&ps->res_stats[1]); |
481 | avg_running = avg_stats(&ps->res_stats[2]); | |
d7c29318 | 482 | |
210ad39f | 483 | fprintf(stderr, " (scaled from %.2f%%)", |
506d4bc8 PZ |
484 | 100 * avg_running / avg_enabled); |
485 | } | |
44175b6f | 486 | |
c04f5e5d IM |
487 | fprintf(stderr, "\n"); |
488 | } | |
489 | ||
f5b4a9c3 SE |
490 | /* |
491 | * Print out the results of a single counter: | |
492 | * does not use aggregated count in system-wide | |
493 | */ | |
69aad6f1 | 494 | static void print_counter(struct perf_evsel *counter) |
f5b4a9c3 SE |
495 | { |
496 | u64 ena, run, val; | |
497 | int cpu; | |
498 | ||
60d567e2 | 499 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
c52b12ed ACM |
500 | val = counter->counts->cpu[cpu].val; |
501 | ena = counter->counts->cpu[cpu].ena; | |
502 | run = counter->counts->cpu[cpu].run; | |
f5b4a9c3 | 503 | if (run == 0 || ena == 0) { |
d7470b6a SE |
504 | fprintf(stderr, "CPU%*d%s%*s%s%-24s", |
505 | csv_output ? 0 : -4, | |
60d567e2 | 506 | cpus->map[cpu], csv_sep, |
d7470b6a SE |
507 | csv_output ? 0 : 18, |
508 | "<not counted>", csv_sep, | |
509 | event_name(counter)); | |
f5b4a9c3 SE |
510 | |
511 | fprintf(stderr, "\n"); | |
512 | continue; | |
513 | } | |
514 | ||
515 | if (nsec_counter(counter)) | |
516 | nsec_printout(cpu, counter, val); | |
517 | else | |
518 | abs_printout(cpu, counter, val); | |
519 | ||
d7470b6a SE |
520 | if (!csv_output) { |
521 | print_noise(counter, 1.0); | |
f5b4a9c3 | 522 | |
d7470b6a SE |
523 | if (run != ena) { |
524 | fprintf(stderr, " (scaled from %.2f%%)", | |
f5b4a9c3 | 525 | 100.0 * run / ena); |
d7470b6a | 526 | } |
f5b4a9c3 SE |
527 | } |
528 | fprintf(stderr, "\n"); | |
529 | } | |
530 | } | |
531 | ||
42202dd5 IM |
532 | static void print_stat(int argc, const char **argv) |
533 | { | |
69aad6f1 ACM |
534 | struct perf_evsel *counter; |
535 | int i; | |
42202dd5 | 536 | |
ddcacfa0 IM |
537 | fflush(stdout); |
538 | ||
d7470b6a SE |
539 | if (!csv_output) { |
540 | fprintf(stderr, "\n"); | |
541 | fprintf(stderr, " Performance counter stats for "); | |
542 | if(target_pid == -1 && target_tid == -1) { | |
543 | fprintf(stderr, "\'%s", argv[0]); | |
544 | for (i = 1; i < argc; i++) | |
545 | fprintf(stderr, " %s", argv[i]); | |
546 | } else if (target_pid != -1) | |
547 | fprintf(stderr, "process id \'%d", target_pid); | |
548 | else | |
549 | fprintf(stderr, "thread id \'%d", target_tid); | |
44db76c8 | 550 | |
d7470b6a SE |
551 | fprintf(stderr, "\'"); |
552 | if (run_count > 1) | |
553 | fprintf(stderr, " (%d runs)", run_count); | |
554 | fprintf(stderr, ":\n\n"); | |
555 | } | |
2996f5dd | 556 | |
f5b4a9c3 | 557 | if (no_aggr) { |
69aad6f1 | 558 | list_for_each_entry(counter, &evsel_list, node) |
f5b4a9c3 SE |
559 | print_counter(counter); |
560 | } else { | |
69aad6f1 | 561 | list_for_each_entry(counter, &evsel_list, node) |
f5b4a9c3 SE |
562 | print_counter_aggr(counter); |
563 | } | |
ddcacfa0 | 564 | |
d7470b6a SE |
565 | if (!csv_output) { |
566 | fprintf(stderr, "\n"); | |
567 | fprintf(stderr, " %18.9f seconds time elapsed", | |
568 | avg_stats(&walltime_nsecs_stats)/1e9); | |
569 | if (run_count > 1) { | |
570 | fprintf(stderr, " ( +- %7.3f%% )", | |
506d4bc8 PZ |
571 | 100*stddev_stats(&walltime_nsecs_stats) / |
572 | avg_stats(&walltime_nsecs_stats)); | |
d7470b6a SE |
573 | } |
574 | fprintf(stderr, "\n\n"); | |
566747e6 | 575 | } |
ddcacfa0 IM |
576 | } |
577 | ||
f7b7c26e PZ |
578 | static volatile int signr = -1; |
579 | ||
5242519b | 580 | static void skip_signal(int signo) |
ddcacfa0 | 581 | { |
6be2850e | 582 | if(child_pid == -1) |
60666c63 LW |
583 | done = 1; |
584 | ||
f7b7c26e PZ |
585 | signr = signo; |
586 | } | |
587 | ||
588 | static void sig_atexit(void) | |
589 | { | |
933da83a CW |
590 | if (child_pid != -1) |
591 | kill(child_pid, SIGTERM); | |
592 | ||
f7b7c26e PZ |
593 | if (signr == -1) |
594 | return; | |
595 | ||
596 | signal(signr, SIG_DFL); | |
597 | kill(getpid(), signr); | |
5242519b IM |
598 | } |
599 | ||
600 | static const char * const stat_usage[] = { | |
60666c63 | 601 | "perf stat [<options>] [<command>]", |
5242519b IM |
602 | NULL |
603 | }; | |
604 | ||
d7470b6a SE |
605 | static int stat__set_big_num(const struct option *opt __used, |
606 | const char *s __used, int unset) | |
607 | { | |
608 | big_num_opt = unset ? 0 : 1; | |
609 | return 0; | |
610 | } | |
611 | ||
5242519b IM |
612 | static const struct option options[] = { |
613 | OPT_CALLBACK('e', "event", NULL, "event", | |
86847b62 TG |
614 | "event selector. use 'perf list' to list available events", |
615 | parse_events), | |
2e6cdf99 SE |
616 | OPT_BOOLEAN('i', "no-inherit", &no_inherit, |
617 | "child tasks do not inherit counters"), | |
5242519b | 618 | OPT_INTEGER('p', "pid", &target_pid, |
d6d901c2 ZY |
619 | "stat events on existing process id"), |
620 | OPT_INTEGER('t', "tid", &target_tid, | |
621 | "stat events on existing thread id"), | |
5242519b | 622 | OPT_BOOLEAN('a', "all-cpus", &system_wide, |
3d632595 | 623 | "system-wide collection from all CPUs"), |
b26bc5a7 | 624 | OPT_BOOLEAN('c', "scale", &scale, |
3d632595 | 625 | "scale/normalize counters"), |
c0555642 | 626 | OPT_INCR('v', "verbose", &verbose, |
743ee1f8 | 627 | "be more verbose (show counter open errors, etc)"), |
42202dd5 IM |
628 | OPT_INTEGER('r', "repeat", &run_count, |
629 | "repeat command and print average + stddev (max: 100)"), | |
0cfb7a13 IM |
630 | OPT_BOOLEAN('n', "null", &null_run, |
631 | "null run - dont start any counters"), | |
d7470b6a SE |
632 | OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL, |
633 | "print large numbers with thousands\' separators", | |
634 | stat__set_big_num), | |
c45c6ea2 SE |
635 | OPT_STRING('C', "cpu", &cpu_list, "cpu", |
636 | "list of cpus to monitor in system-wide"), | |
f5b4a9c3 SE |
637 | OPT_BOOLEAN('A', "no-aggr", &no_aggr, |
638 | "disable CPU count aggregation"), | |
d7470b6a SE |
639 | OPT_STRING('x', "field-separator", &csv_sep, "separator", |
640 | "print counts with custom separator"), | |
5242519b IM |
641 | OPT_END() |
642 | }; | |
643 | ||
f37a291c | 644 | int cmd_stat(int argc, const char **argv, const char *prefix __used) |
5242519b | 645 | { |
69aad6f1 ACM |
646 | struct perf_evsel *pos; |
647 | int status = -ENOMEM; | |
42202dd5 | 648 | |
5af52b51 SE |
649 | setlocale(LC_ALL, ""); |
650 | ||
a0541234 AB |
651 | argc = parse_options(argc, argv, options, stat_usage, |
652 | PARSE_OPT_STOP_AT_NON_OPTION); | |
d7470b6a SE |
653 | |
654 | if (csv_sep) | |
655 | csv_output = true; | |
656 | else | |
657 | csv_sep = DEFAULT_SEPARATOR; | |
658 | ||
659 | /* | |
660 | * let the spreadsheet do the pretty-printing | |
661 | */ | |
662 | if (csv_output) { | |
663 | /* User explicitely passed -B? */ | |
664 | if (big_num_opt == 1) { | |
665 | fprintf(stderr, "-B option not supported with -x\n"); | |
666 | usage_with_options(stat_usage, options); | |
667 | } else /* Nope, so disable big number formatting */ | |
668 | big_num = false; | |
669 | } else if (big_num_opt == 0) /* User passed --no-big-num */ | |
670 | big_num = false; | |
671 | ||
d6d901c2 | 672 | if (!argc && target_pid == -1 && target_tid == -1) |
5242519b | 673 | usage_with_options(stat_usage, options); |
9e9772c4 | 674 | if (run_count <= 0) |
42202dd5 | 675 | usage_with_options(stat_usage, options); |
ddcacfa0 | 676 | |
f5b4a9c3 SE |
677 | /* no_aggr is for system-wide only */ |
678 | if (no_aggr && !system_wide) | |
679 | usage_with_options(stat_usage, options); | |
680 | ||
c3043569 JSR |
681 | /* Set attrs and nr_counters if no event is selected and !null_run */ |
682 | if (!null_run && !nr_counters) { | |
69aad6f1 ACM |
683 | size_t c; |
684 | ||
c3043569 | 685 | nr_counters = ARRAY_SIZE(default_attrs); |
69aad6f1 ACM |
686 | |
687 | for (c = 0; c < ARRAY_SIZE(default_attrs); ++c) { | |
23a2f3ab | 688 | pos = perf_evsel__new(&default_attrs[c], |
69aad6f1 ACM |
689 | nr_counters); |
690 | if (pos == NULL) | |
691 | goto out; | |
692 | list_add(&pos->node, &evsel_list); | |
693 | } | |
c3043569 | 694 | } |
ddcacfa0 | 695 | |
5c98d466 ACM |
696 | if (target_pid != -1) |
697 | target_tid = target_pid; | |
698 | ||
699 | threads = thread_map__new(target_pid, target_tid); | |
700 | if (threads == NULL) { | |
701 | pr_err("Problems finding threads of monitor\n"); | |
702 | usage_with_options(stat_usage, options); | |
703 | } | |
704 | ||
a12b51c4 | 705 | if (system_wide) |
60d567e2 | 706 | cpus = cpu_map__new(cpu_list); |
a12b51c4 | 707 | else |
60d567e2 | 708 | cpus = cpu_map__dummy_new(); |
ddcacfa0 | 709 | |
60d567e2 ACM |
710 | if (cpus == NULL) { |
711 | perror("failed to parse CPUs map"); | |
c45c6ea2 | 712 | usage_with_options(stat_usage, options); |
60d567e2 ACM |
713 | return -1; |
714 | } | |
c45c6ea2 | 715 | |
69aad6f1 | 716 | list_for_each_entry(pos, &evsel_list, node) { |
c52b12ed | 717 | if (perf_evsel__alloc_stat_priv(pos) < 0 || |
60d567e2 | 718 | perf_evsel__alloc_counts(pos, cpus->nr) < 0 || |
5c98d466 | 719 | perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0) |
69aad6f1 | 720 | goto out_free_fd; |
d6d901c2 ZY |
721 | } |
722 | ||
58d7e993 IM |
723 | /* |
724 | * We dont want to block the signals - that would cause | |
725 | * child tasks to inherit that and Ctrl-C would not work. | |
726 | * What we want is for Ctrl-C to work in the exec()-ed | |
727 | * task, but being ignored by perf stat itself: | |
728 | */ | |
f7b7c26e | 729 | atexit(sig_atexit); |
58d7e993 IM |
730 | signal(SIGINT, skip_signal); |
731 | signal(SIGALRM, skip_signal); | |
732 | signal(SIGABRT, skip_signal); | |
733 | ||
42202dd5 IM |
734 | status = 0; |
735 | for (run_idx = 0; run_idx < run_count; run_idx++) { | |
736 | if (run_count != 1 && verbose) | |
3d632595 | 737 | fprintf(stderr, "[ perf stat: executing run #%d ... ]\n", run_idx + 1); |
42202dd5 IM |
738 | status = run_perf_stat(argc, argv); |
739 | } | |
740 | ||
084ab9f8 ACM |
741 | if (status != -1) |
742 | print_stat(argc, argv); | |
69aad6f1 | 743 | out_free_fd: |
70d544d0 | 744 | list_for_each_entry(pos, &evsel_list, node) |
69aad6f1 | 745 | perf_evsel__free_stat_priv(pos); |
bd3bfe9e | 746 | perf_evsel_list__delete(); |
69aad6f1 | 747 | out: |
5c98d466 ACM |
748 | thread_map__delete(threads); |
749 | threads = NULL; | |
42202dd5 | 750 | return status; |
ddcacfa0 | 751 | } |