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