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