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