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 | |
2cba3ffb | 9 | $ perf stat ./hackbench 10 |
ddcacfa0 | 10 | |
2cba3ffb | 11 | Time: 0.118 |
ddcacfa0 | 12 | |
2cba3ffb | 13 | Performance counter stats for './hackbench 10': |
ddcacfa0 | 14 | |
2cba3ffb IM |
15 | 1708.761321 task-clock # 11.037 CPUs utilized |
16 | 41,190 context-switches # 0.024 M/sec | |
17 | 6,735 CPU-migrations # 0.004 M/sec | |
18 | 17,318 page-faults # 0.010 M/sec | |
19 | 5,205,202,243 cycles # 3.046 GHz | |
20 | 3,856,436,920 stalled-cycles-frontend # 74.09% frontend cycles idle | |
21 | 1,600,790,871 stalled-cycles-backend # 30.75% backend cycles idle | |
22 | 2,603,501,247 instructions # 0.50 insns per cycle | |
23 | # 1.48 stalled cycles per insn | |
24 | 484,357,498 branches # 283.455 M/sec | |
25 | 6,388,934 branch-misses # 1.32% of all branches | |
26 | ||
27 | 0.154822978 seconds time elapsed | |
ddcacfa0 | 28 | |
5242519b | 29 | * |
2cba3ffb | 30 | * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com> |
5242519b IM |
31 | * |
32 | * Improvements and fixes by: | |
33 | * | |
34 | * Arjan van de Ven <arjan@linux.intel.com> | |
35 | * Yanmin Zhang <yanmin.zhang@intel.com> | |
36 | * Wu Fengguang <fengguang.wu@intel.com> | |
37 | * Mike Galbraith <efault@gmx.de> | |
38 | * Paul Mackerras <paulus@samba.org> | |
6e750a8f | 39 | * Jaswinder Singh Rajput <jaswinder@kernel.org> |
5242519b IM |
40 | * |
41 | * Released under the GPL v2. (and only v2, not any later version) | |
ddcacfa0 IM |
42 | */ |
43 | ||
1a482f38 | 44 | #include "perf.h" |
16f762a2 | 45 | #include "builtin.h" |
f14d5707 | 46 | #include "util/cgroup.h" |
148be2c1 | 47 | #include "util/util.h" |
5242519b IM |
48 | #include "util/parse-options.h" |
49 | #include "util/parse-events.h" | |
4cabc3d1 | 50 | #include "util/pmu.h" |
8f28827a | 51 | #include "util/event.h" |
361c99a6 | 52 | #include "util/evlist.h" |
69aad6f1 | 53 | #include "util/evsel.h" |
8f28827a | 54 | #include "util/debug.h" |
a5d243d0 | 55 | #include "util/color.h" |
0007ecea | 56 | #include "util/stat.h" |
60666c63 | 57 | #include "util/header.h" |
a12b51c4 | 58 | #include "util/cpumap.h" |
d6d901c2 | 59 | #include "util/thread.h" |
fd78260b | 60 | #include "util/thread_map.h" |
ddcacfa0 | 61 | |
1f16c575 | 62 | #include <stdlib.h> |
ddcacfa0 | 63 | #include <sys/prctl.h> |
5af52b51 | 64 | #include <locale.h> |
16c8a109 | 65 | |
d7470b6a | 66 | #define DEFAULT_SEPARATOR " " |
2cee77c4 DA |
67 | #define CNTR_NOT_SUPPORTED "<not supported>" |
68 | #define CNTR_NOT_COUNTED "<not counted>" | |
d7470b6a | 69 | |
13370a9b SE |
70 | static void print_stat(int argc, const char **argv); |
71 | static void print_counter_aggr(struct perf_evsel *counter, char *prefix); | |
72 | static void print_counter(struct perf_evsel *counter, char *prefix); | |
86ee6e18 | 73 | static void print_aggr(char *prefix); |
13370a9b | 74 | |
4cabc3d1 | 75 | /* Default events used for perf stat -T */ |
a454742c JO |
76 | static const char *transaction_attrs = { |
77 | "task-clock," | |
4cabc3d1 AK |
78 | "{" |
79 | "instructions," | |
80 | "cycles," | |
81 | "cpu/cycles-t/," | |
82 | "cpu/tx-start/," | |
83 | "cpu/el-start/," | |
84 | "cpu/cycles-ct/" | |
85 | "}" | |
86 | }; | |
87 | ||
88 | /* More limited version when the CPU does not have all events. */ | |
a454742c JO |
89 | static const char * transaction_limited_attrs = { |
90 | "task-clock," | |
4cabc3d1 AK |
91 | "{" |
92 | "instructions," | |
93 | "cycles," | |
94 | "cpu/cycles-t/," | |
95 | "cpu/tx-start/" | |
96 | "}" | |
97 | }; | |
98 | ||
666e6d48 | 99 | static struct perf_evlist *evsel_list; |
361c99a6 | 100 | |
602ad878 | 101 | static struct target target = { |
77a6f014 NK |
102 | .uid = UINT_MAX, |
103 | }; | |
ddcacfa0 | 104 | |
3d632595 | 105 | static int run_count = 1; |
2e6cdf99 | 106 | static bool no_inherit = false; |
c0555642 | 107 | static bool scale = true; |
86ee6e18 | 108 | static enum aggr_mode aggr_mode = AGGR_GLOBAL; |
d07f0b12 | 109 | static volatile pid_t child_pid = -1; |
c0555642 | 110 | static bool null_run = false; |
2cba3ffb | 111 | static int detailed_run = 0; |
4cabc3d1 | 112 | static bool transaction_run; |
201e0b06 | 113 | static bool big_num = true; |
d7470b6a | 114 | static int big_num_opt = -1; |
d7470b6a SE |
115 | static const char *csv_sep = NULL; |
116 | static bool csv_output = false; | |
43bece79 | 117 | static bool group = false; |
4aa9015f | 118 | static FILE *output = NULL; |
1f16c575 PZ |
119 | static const char *pre_cmd = NULL; |
120 | static const char *post_cmd = NULL; | |
121 | static bool sync_run = false; | |
13370a9b | 122 | static unsigned int interval = 0; |
41191688 | 123 | static unsigned int initial_delay = 0; |
410136f5 | 124 | static unsigned int unit_width = 4; /* strlen("unit") */ |
a7e191c3 | 125 | static bool forever = false; |
13370a9b | 126 | static struct timespec ref_time; |
86ee6e18 SE |
127 | static struct cpu_map *aggr_map; |
128 | static int (*aggr_get_id)(struct cpu_map *m, int cpu); | |
5af52b51 | 129 | |
60666c63 LW |
130 | static volatile int done = 0; |
131 | ||
13370a9b SE |
132 | static inline void diff_timespec(struct timespec *r, struct timespec *a, |
133 | struct timespec *b) | |
134 | { | |
135 | r->tv_sec = a->tv_sec - b->tv_sec; | |
136 | if (a->tv_nsec < b->tv_nsec) { | |
137 | r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec; | |
138 | r->tv_sec--; | |
139 | } else { | |
140 | r->tv_nsec = a->tv_nsec - b->tv_nsec ; | |
141 | } | |
142 | } | |
143 | ||
254ecbc7 JO |
144 | static void perf_stat__reset_stats(void) |
145 | { | |
146 | perf_evlist__reset_stats(evsel_list); | |
f87027b9 | 147 | perf_stat__reset_shadow_stats(); |
1eda3b21 JO |
148 | } |
149 | ||
cac21425 | 150 | static int create_perf_stat_counter(struct perf_evsel *evsel) |
ddcacfa0 | 151 | { |
69aad6f1 | 152 | struct perf_event_attr *attr = &evsel->attr; |
727ab04e | 153 | |
ddcacfa0 | 154 | if (scale) |
a21ca2ca IM |
155 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | |
156 | PERF_FORMAT_TOTAL_TIME_RUNNING; | |
ddcacfa0 | 157 | |
5d2cd909 ACM |
158 | attr->inherit = !no_inherit; |
159 | ||
602ad878 | 160 | if (target__has_cpu(&target)) |
594ac61a | 161 | return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel)); |
5622c07b | 162 | |
602ad878 | 163 | if (!target__has_task(&target) && perf_evsel__is_group_leader(evsel)) { |
48290609 | 164 | attr->disabled = 1; |
41191688 AK |
165 | if (!initial_delay) |
166 | attr->enable_on_exec = 1; | |
ddcacfa0 | 167 | } |
084ab9f8 | 168 | |
594ac61a | 169 | return perf_evsel__open_per_thread(evsel, evsel_list->threads); |
ddcacfa0 IM |
170 | } |
171 | ||
c04f5e5d IM |
172 | /* |
173 | * Does the counter have nsecs as a unit? | |
174 | */ | |
daec78a0 | 175 | static inline int nsec_counter(struct perf_evsel *evsel) |
c04f5e5d | 176 | { |
daec78a0 ACM |
177 | if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) || |
178 | perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK)) | |
c04f5e5d IM |
179 | return 1; |
180 | ||
181 | return 0; | |
182 | } | |
183 | ||
779d0b99 JO |
184 | static void zero_per_pkg(struct perf_evsel *counter) |
185 | { | |
186 | if (counter->per_pkg_mask) | |
187 | memset(counter->per_pkg_mask, 0, MAX_NR_CPUS); | |
188 | } | |
189 | ||
190 | static int check_per_pkg(struct perf_evsel *counter, int cpu, bool *skip) | |
191 | { | |
192 | unsigned long *mask = counter->per_pkg_mask; | |
193 | struct cpu_map *cpus = perf_evsel__cpus(counter); | |
194 | int s; | |
195 | ||
196 | *skip = false; | |
197 | ||
198 | if (!counter->per_pkg) | |
199 | return 0; | |
200 | ||
201 | if (cpu_map__empty(cpus)) | |
202 | return 0; | |
203 | ||
204 | if (!mask) { | |
205 | mask = zalloc(MAX_NR_CPUS); | |
206 | if (!mask) | |
207 | return -ENOMEM; | |
208 | ||
209 | counter->per_pkg_mask = mask; | |
210 | } | |
211 | ||
212 | s = cpu_map__get_socket(cpus, cpu); | |
213 | if (s < 0) | |
214 | return -1; | |
215 | ||
216 | *skip = test_and_set_bit(s, mask) == 1; | |
217 | return 0; | |
218 | } | |
219 | ||
a6fa0038 | 220 | static int read_cb(struct perf_evsel *evsel, int cpu, int thread, |
060c4f9c JO |
221 | struct perf_counts_values *count) |
222 | { | |
1971f59f | 223 | struct perf_counts_values *aggr = &evsel->counts->aggr; |
779d0b99 JO |
224 | static struct perf_counts_values zero; |
225 | bool skip = false; | |
226 | ||
227 | if (check_per_pkg(evsel, cpu, &skip)) { | |
228 | pr_err("failed to read per-pkg counter\n"); | |
229 | return -1; | |
230 | } | |
231 | ||
232 | if (skip) | |
233 | count = &zero; | |
1971f59f | 234 | |
060c4f9c JO |
235 | switch (aggr_mode) { |
236 | case AGGR_CORE: | |
237 | case AGGR_SOCKET: | |
238 | case AGGR_NONE: | |
6c0345b7 | 239 | if (!evsel->snapshot) |
a6fa0038 | 240 | perf_evsel__compute_deltas(evsel, cpu, thread, count); |
060c4f9c | 241 | perf_counts_values__scale(count, scale, NULL); |
a6fa0038 | 242 | *perf_counts(evsel->counts, cpu, thread) = *count; |
56f0fd45 | 243 | if (aggr_mode == AGGR_NONE) |
f87027b9 | 244 | perf_stat__update_shadow_stats(evsel, count->values, cpu); |
060c4f9c JO |
245 | break; |
246 | case AGGR_GLOBAL: | |
1971f59f JO |
247 | aggr->val += count->val; |
248 | if (scale) { | |
249 | aggr->ena += count->ena; | |
250 | aggr->run += count->run; | |
251 | } | |
060c4f9c JO |
252 | default: |
253 | break; | |
254 | } | |
255 | ||
256 | return 0; | |
257 | } | |
258 | ||
1971f59f JO |
259 | static int read_counter(struct perf_evsel *counter); |
260 | ||
c04f5e5d | 261 | /* |
2996f5dd | 262 | * Read out the results of a single counter: |
f5b4a9c3 | 263 | * aggregate counts across CPUs in system-wide mode |
c04f5e5d | 264 | */ |
c52b12ed | 265 | static int read_counter_aggr(struct perf_evsel *counter) |
c04f5e5d | 266 | { |
1971f59f | 267 | struct perf_counts_values *aggr = &counter->counts->aggr; |
69aad6f1 | 268 | struct perf_stat *ps = counter->priv; |
c52b12ed ACM |
269 | u64 *count = counter->counts->aggr.values; |
270 | int i; | |
2996f5dd | 271 | |
1971f59f JO |
272 | aggr->val = aggr->ena = aggr->run = 0; |
273 | ||
274 | if (read_counter(counter)) | |
c52b12ed | 275 | return -1; |
9e9772c4 | 276 | |
6c0345b7 | 277 | if (!counter->snapshot) |
a6fa0038 | 278 | perf_evsel__compute_deltas(counter, -1, -1, aggr); |
1971f59f JO |
279 | perf_counts_values__scale(aggr, scale, &counter->counts->scaled); |
280 | ||
9e9772c4 | 281 | for (i = 0; i < 3; i++) |
69aad6f1 | 282 | update_stats(&ps->res_stats[i], count[i]); |
9e9772c4 PZ |
283 | |
284 | if (verbose) { | |
4aa9015f | 285 | fprintf(output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", |
7289f83c | 286 | perf_evsel__name(counter), count[0], count[1], count[2]); |
9e9772c4 PZ |
287 | } |
288 | ||
be1ac0d8 IM |
289 | /* |
290 | * Save the full runtime - to allow normalization during printout: | |
291 | */ | |
f87027b9 | 292 | perf_stat__update_shadow_stats(counter, count, 0); |
c52b12ed ACM |
293 | |
294 | return 0; | |
f5b4a9c3 SE |
295 | } |
296 | ||
297 | /* | |
298 | * Read out the results of a single counter: | |
299 | * do not aggregate counts across CPUs in system-wide mode | |
300 | */ | |
c52b12ed | 301 | static int read_counter(struct perf_evsel *counter) |
f5b4a9c3 | 302 | { |
9bf1a529 JO |
303 | int nthreads = thread_map__nr(evsel_list->threads); |
304 | int ncpus = perf_evsel__nr_cpus(counter); | |
305 | int cpu, thread; | |
f5b4a9c3 | 306 | |
3b4331d9 SP |
307 | if (!counter->supported) |
308 | return -ENOENT; | |
309 | ||
9bf1a529 JO |
310 | if (counter->system_wide) |
311 | nthreads = 1; | |
312 | ||
779d0b99 JO |
313 | if (counter->per_pkg) |
314 | zero_per_pkg(counter); | |
315 | ||
9bf1a529 JO |
316 | for (thread = 0; thread < nthreads; thread++) { |
317 | for (cpu = 0; cpu < ncpus; cpu++) { | |
318 | if (perf_evsel__read_cb(counter, cpu, thread, read_cb)) | |
319 | return -1; | |
320 | } | |
f5b4a9c3 | 321 | } |
c52b12ed ACM |
322 | |
323 | return 0; | |
2996f5dd IM |
324 | } |
325 | ||
13370a9b SE |
326 | static void print_interval(void) |
327 | { | |
328 | static int num_print_interval; | |
329 | struct perf_evsel *counter; | |
330 | struct perf_stat *ps; | |
331 | struct timespec ts, rs; | |
332 | char prefix[64]; | |
333 | ||
86ee6e18 | 334 | if (aggr_mode == AGGR_GLOBAL) { |
0050f7aa | 335 | evlist__for_each(evsel_list, counter) { |
13370a9b SE |
336 | ps = counter->priv; |
337 | memset(ps->res_stats, 0, sizeof(ps->res_stats)); | |
86ee6e18 | 338 | read_counter_aggr(counter); |
13370a9b | 339 | } |
86ee6e18 | 340 | } else { |
0050f7aa | 341 | evlist__for_each(evsel_list, counter) { |
13370a9b SE |
342 | ps = counter->priv; |
343 | memset(ps->res_stats, 0, sizeof(ps->res_stats)); | |
86ee6e18 | 344 | read_counter(counter); |
13370a9b SE |
345 | } |
346 | } | |
86ee6e18 | 347 | |
13370a9b SE |
348 | clock_gettime(CLOCK_MONOTONIC, &ts); |
349 | diff_timespec(&rs, &ts, &ref_time); | |
350 | sprintf(prefix, "%6lu.%09lu%s", rs.tv_sec, rs.tv_nsec, csv_sep); | |
351 | ||
352 | if (num_print_interval == 0 && !csv_output) { | |
86ee6e18 SE |
353 | switch (aggr_mode) { |
354 | case AGGR_SOCKET: | |
410136f5 | 355 | fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit"); |
86ee6e18 | 356 | break; |
12c08a9f | 357 | case AGGR_CORE: |
410136f5 | 358 | fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit"); |
12c08a9f | 359 | break; |
86ee6e18 | 360 | case AGGR_NONE: |
410136f5 | 361 | fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit"); |
86ee6e18 SE |
362 | break; |
363 | case AGGR_GLOBAL: | |
364 | default: | |
410136f5 | 365 | fprintf(output, "# time counts %*s events\n", unit_width, "unit"); |
86ee6e18 | 366 | } |
13370a9b SE |
367 | } |
368 | ||
369 | if (++num_print_interval == 25) | |
370 | num_print_interval = 0; | |
371 | ||
86ee6e18 | 372 | switch (aggr_mode) { |
12c08a9f | 373 | case AGGR_CORE: |
86ee6e18 SE |
374 | case AGGR_SOCKET: |
375 | print_aggr(prefix); | |
376 | break; | |
377 | case AGGR_NONE: | |
0050f7aa | 378 | evlist__for_each(evsel_list, counter) |
13370a9b | 379 | print_counter(counter, prefix); |
86ee6e18 SE |
380 | break; |
381 | case AGGR_GLOBAL: | |
382 | default: | |
0050f7aa | 383 | evlist__for_each(evsel_list, counter) |
13370a9b SE |
384 | print_counter_aggr(counter, prefix); |
385 | } | |
2bbf03f1 AK |
386 | |
387 | fflush(output); | |
13370a9b SE |
388 | } |
389 | ||
41191688 AK |
390 | static void handle_initial_delay(void) |
391 | { | |
392 | struct perf_evsel *counter; | |
393 | ||
394 | if (initial_delay) { | |
395 | const int ncpus = cpu_map__nr(evsel_list->cpus), | |
396 | nthreads = thread_map__nr(evsel_list->threads); | |
397 | ||
398 | usleep(initial_delay * 1000); | |
0050f7aa | 399 | evlist__for_each(evsel_list, counter) |
41191688 AK |
400 | perf_evsel__enable(counter, ncpus, nthreads); |
401 | } | |
402 | } | |
403 | ||
f33cbe72 | 404 | static volatile int workload_exec_errno; |
6af206fd ACM |
405 | |
406 | /* | |
407 | * perf_evlist__prepare_workload will send a SIGUSR1 | |
408 | * if the fork fails, since we asked by setting its | |
409 | * want_signal to true. | |
410 | */ | |
f33cbe72 ACM |
411 | static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info, |
412 | void *ucontext __maybe_unused) | |
6af206fd | 413 | { |
f33cbe72 | 414 | workload_exec_errno = info->si_value.sival_int; |
6af206fd ACM |
415 | } |
416 | ||
acf28922 | 417 | static int __run_perf_stat(int argc, const char **argv) |
42202dd5 | 418 | { |
56e52e85 | 419 | char msg[512]; |
42202dd5 | 420 | unsigned long long t0, t1; |
cac21425 | 421 | struct perf_evsel *counter; |
13370a9b | 422 | struct timespec ts; |
410136f5 | 423 | size_t l; |
42202dd5 | 424 | int status = 0; |
6be2850e | 425 | const bool forks = (argc > 0); |
42202dd5 | 426 | |
13370a9b SE |
427 | if (interval) { |
428 | ts.tv_sec = interval / 1000; | |
429 | ts.tv_nsec = (interval % 1000) * 1000000; | |
430 | } else { | |
431 | ts.tv_sec = 1; | |
432 | ts.tv_nsec = 0; | |
433 | } | |
434 | ||
60666c63 | 435 | if (forks) { |
735f7e0b ACM |
436 | if (perf_evlist__prepare_workload(evsel_list, &target, argv, false, |
437 | workload_exec_failed_signal) < 0) { | |
acf28922 NK |
438 | perror("failed to prepare workload"); |
439 | return -1; | |
60666c63 | 440 | } |
d20a47e7 | 441 | child_pid = evsel_list->workload.pid; |
051ae7f7 PM |
442 | } |
443 | ||
6a4bb04c | 444 | if (group) |
63dab225 | 445 | perf_evlist__set_leader(evsel_list); |
6a4bb04c | 446 | |
0050f7aa | 447 | evlist__for_each(evsel_list, counter) { |
cac21425 | 448 | if (create_perf_stat_counter(counter) < 0) { |
979987a5 DA |
449 | /* |
450 | * PPC returns ENXIO for HW counters until 2.6.37 | |
451 | * (behavior changed with commit b0a873e). | |
452 | */ | |
38f6ae1e | 453 | if (errno == EINVAL || errno == ENOSYS || |
979987a5 DA |
454 | errno == ENOENT || errno == EOPNOTSUPP || |
455 | errno == ENXIO) { | |
c63ca0c0 DA |
456 | if (verbose) |
457 | ui__warning("%s event is not supported by the kernel.\n", | |
7289f83c | 458 | perf_evsel__name(counter)); |
2cee77c4 | 459 | counter->supported = false; |
cb5ef600 KL |
460 | |
461 | if ((counter->leader != counter) || | |
462 | !(counter->leader->nr_members > 1)) | |
463 | continue; | |
c63ca0c0 | 464 | } |
ede70290 | 465 | |
56e52e85 ACM |
466 | perf_evsel__open_strerror(counter, &target, |
467 | errno, msg, sizeof(msg)); | |
468 | ui__error("%s\n", msg); | |
469 | ||
48290609 ACM |
470 | if (child_pid != -1) |
471 | kill(child_pid, SIGTERM); | |
fceda7fe | 472 | |
48290609 ACM |
473 | return -1; |
474 | } | |
2cee77c4 | 475 | counter->supported = true; |
410136f5 SE |
476 | |
477 | l = strlen(counter->unit); | |
478 | if (l > unit_width) | |
479 | unit_width = l; | |
084ab9f8 | 480 | } |
42202dd5 | 481 | |
23d4aad4 ACM |
482 | if (perf_evlist__apply_filters(evsel_list, &counter)) { |
483 | error("failed to set filter \"%s\" on event %s with %d (%s)\n", | |
484 | counter->filter, perf_evsel__name(counter), errno, | |
759e612b | 485 | strerror_r(errno, msg, sizeof(msg))); |
cfd748ae FW |
486 | return -1; |
487 | } | |
488 | ||
42202dd5 IM |
489 | /* |
490 | * Enable counters and exec the command: | |
491 | */ | |
492 | t0 = rdclock(); | |
13370a9b | 493 | clock_gettime(CLOCK_MONOTONIC, &ref_time); |
42202dd5 | 494 | |
60666c63 | 495 | if (forks) { |
acf28922 | 496 | perf_evlist__start_workload(evsel_list); |
41191688 | 497 | handle_initial_delay(); |
acf28922 | 498 | |
13370a9b SE |
499 | if (interval) { |
500 | while (!waitpid(child_pid, &status, WNOHANG)) { | |
501 | nanosleep(&ts, NULL); | |
502 | print_interval(); | |
503 | } | |
504 | } | |
60666c63 | 505 | wait(&status); |
6af206fd | 506 | |
f33cbe72 ACM |
507 | if (workload_exec_errno) { |
508 | const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg)); | |
509 | pr_err("Workload failed: %s\n", emsg); | |
6af206fd | 510 | return -1; |
f33cbe72 | 511 | } |
6af206fd | 512 | |
33e49ea7 AK |
513 | if (WIFSIGNALED(status)) |
514 | psignal(WTERMSIG(status), argv[0]); | |
60666c63 | 515 | } else { |
41191688 | 516 | handle_initial_delay(); |
13370a9b SE |
517 | while (!done) { |
518 | nanosleep(&ts, NULL); | |
519 | if (interval) | |
520 | print_interval(); | |
521 | } | |
60666c63 | 522 | } |
42202dd5 | 523 | |
42202dd5 IM |
524 | t1 = rdclock(); |
525 | ||
9e9772c4 | 526 | update_stats(&walltime_nsecs_stats, t1 - t0); |
42202dd5 | 527 | |
86ee6e18 | 528 | if (aggr_mode == AGGR_GLOBAL) { |
0050f7aa | 529 | evlist__for_each(evsel_list, counter) { |
f5b4a9c3 | 530 | read_counter_aggr(counter); |
7ae92e74 | 531 | perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter), |
b3a319d5 | 532 | thread_map__nr(evsel_list->threads)); |
c52b12ed | 533 | } |
86ee6e18 | 534 | } else { |
0050f7aa | 535 | evlist__for_each(evsel_list, counter) { |
86ee6e18 SE |
536 | read_counter(counter); |
537 | perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter), 1); | |
538 | } | |
f5b4a9c3 | 539 | } |
c52b12ed | 540 | |
42202dd5 IM |
541 | return WEXITSTATUS(status); |
542 | } | |
543 | ||
41cde476 | 544 | static int run_perf_stat(int argc, const char **argv) |
1f16c575 PZ |
545 | { |
546 | int ret; | |
547 | ||
548 | if (pre_cmd) { | |
549 | ret = system(pre_cmd); | |
550 | if (ret) | |
551 | return ret; | |
552 | } | |
553 | ||
554 | if (sync_run) | |
555 | sync(); | |
556 | ||
557 | ret = __run_perf_stat(argc, argv); | |
558 | if (ret) | |
559 | return ret; | |
560 | ||
561 | if (post_cmd) { | |
562 | ret = system(post_cmd); | |
563 | if (ret) | |
564 | return ret; | |
565 | } | |
566 | ||
567 | return ret; | |
568 | } | |
569 | ||
d73515c0 AK |
570 | static void print_running(u64 run, u64 ena) |
571 | { | |
572 | if (csv_output) { | |
573 | fprintf(output, "%s%" PRIu64 "%s%.2f", | |
574 | csv_sep, | |
575 | run, | |
576 | csv_sep, | |
577 | ena ? 100.0 * run / ena : 100.0); | |
578 | } else if (run != ena) { | |
579 | fprintf(output, " (%.2f%%)", 100.0 * run / ena); | |
580 | } | |
581 | } | |
582 | ||
f99844cb IM |
583 | static void print_noise_pct(double total, double avg) |
584 | { | |
0007ecea | 585 | double pct = rel_stddev_stats(total, avg); |
f99844cb | 586 | |
3ae9a34d | 587 | if (csv_output) |
4aa9015f | 588 | fprintf(output, "%s%.2f%%", csv_sep, pct); |
a1bca6cc | 589 | else if (pct) |
4aa9015f | 590 | fprintf(output, " ( +-%6.2f%% )", pct); |
f99844cb IM |
591 | } |
592 | ||
69aad6f1 | 593 | static void print_noise(struct perf_evsel *evsel, double avg) |
42202dd5 | 594 | { |
69aad6f1 ACM |
595 | struct perf_stat *ps; |
596 | ||
849abde9 PZ |
597 | if (run_count == 1) |
598 | return; | |
599 | ||
69aad6f1 | 600 | ps = evsel->priv; |
f99844cb | 601 | print_noise_pct(stddev_stats(&ps->res_stats[0]), avg); |
42202dd5 IM |
602 | } |
603 | ||
12c08a9f | 604 | static void aggr_printout(struct perf_evsel *evsel, int id, int nr) |
44175b6f | 605 | { |
86ee6e18 | 606 | switch (aggr_mode) { |
12c08a9f SE |
607 | case AGGR_CORE: |
608 | fprintf(output, "S%d-C%*d%s%*d%s", | |
609 | cpu_map__id_to_socket(id), | |
610 | csv_output ? 0 : -8, | |
611 | cpu_map__id_to_cpu(id), | |
612 | csv_sep, | |
613 | csv_output ? 0 : 4, | |
614 | nr, | |
615 | csv_sep); | |
616 | break; | |
86ee6e18 SE |
617 | case AGGR_SOCKET: |
618 | fprintf(output, "S%*d%s%*d%s", | |
d7e7a451 | 619 | csv_output ? 0 : -5, |
12c08a9f | 620 | id, |
d7e7a451 SE |
621 | csv_sep, |
622 | csv_output ? 0 : 4, | |
623 | nr, | |
624 | csv_sep); | |
86ee6e18 SE |
625 | break; |
626 | case AGGR_NONE: | |
627 | fprintf(output, "CPU%*d%s", | |
d7470b6a | 628 | csv_output ? 0 : -4, |
12c08a9f | 629 | perf_evsel__cpus(evsel)->map[id], csv_sep); |
86ee6e18 SE |
630 | break; |
631 | case AGGR_GLOBAL: | |
632 | default: | |
633 | break; | |
634 | } | |
635 | } | |
636 | ||
da88c7f7 | 637 | static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg) |
86ee6e18 SE |
638 | { |
639 | double msecs = avg / 1e6; | |
410136f5 | 640 | const char *fmt_v, *fmt_n; |
4bbe5a61 | 641 | char name[25]; |
86ee6e18 | 642 | |
410136f5 SE |
643 | fmt_v = csv_output ? "%.6f%s" : "%18.6f%s"; |
644 | fmt_n = csv_output ? "%s" : "%-25s"; | |
645 | ||
da88c7f7 | 646 | aggr_printout(evsel, id, nr); |
d7470b6a | 647 | |
4bbe5a61 DA |
648 | scnprintf(name, sizeof(name), "%s%s", |
649 | perf_evsel__name(evsel), csv_output ? "" : " (msec)"); | |
410136f5 SE |
650 | |
651 | fprintf(output, fmt_v, msecs, csv_sep); | |
652 | ||
653 | if (csv_output) | |
654 | fprintf(output, "%s%s", evsel->unit, csv_sep); | |
655 | else | |
656 | fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep); | |
657 | ||
658 | fprintf(output, fmt_n, name); | |
d7470b6a | 659 | |
023695d9 | 660 | if (evsel->cgrp) |
4aa9015f | 661 | fprintf(output, "%s%s", csv_sep, evsel->cgrp->name); |
023695d9 | 662 | |
13370a9b | 663 | if (csv_output || interval) |
d7470b6a | 664 | return; |
44175b6f | 665 | |
daec78a0 | 666 | if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK)) |
4aa9015f SE |
667 | fprintf(output, " # %8.3f CPUs utilized ", |
668 | avg / avg_stats(&walltime_nsecs_stats)); | |
9dac6a29 NK |
669 | else |
670 | fprintf(output, " "); | |
44175b6f IM |
671 | } |
672 | ||
556b1fb7 JO |
673 | static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg) |
674 | { | |
675 | double sc = evsel->scale; | |
676 | const char *fmt; | |
677 | int cpu = cpu_map__id_to_cpu(id); | |
678 | ||
679 | if (csv_output) { | |
680 | fmt = sc != 1.0 ? "%.2f%s" : "%.0f%s"; | |
681 | } else { | |
682 | if (big_num) | |
683 | fmt = sc != 1.0 ? "%'18.2f%s" : "%'18.0f%s"; | |
684 | else | |
685 | fmt = sc != 1.0 ? "%18.2f%s" : "%18.0f%s"; | |
686 | } | |
687 | ||
688 | aggr_printout(evsel, id, nr); | |
689 | ||
690 | if (aggr_mode == AGGR_GLOBAL) | |
691 | cpu = 0; | |
692 | ||
693 | fprintf(output, fmt, avg, csv_sep); | |
694 | ||
695 | if (evsel->unit) | |
696 | fprintf(output, "%-*s%s", | |
697 | csv_output ? 0 : unit_width, | |
698 | evsel->unit, csv_sep); | |
699 | ||
700 | fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel)); | |
701 | ||
702 | if (evsel->cgrp) | |
703 | fprintf(output, "%s%s", csv_sep, evsel->cgrp->name); | |
704 | ||
705 | if (csv_output || interval) | |
706 | return; | |
707 | ||
f87027b9 | 708 | perf_stat__print_shadow_stats(output, evsel, avg, cpu, aggr_mode); |
556b1fb7 JO |
709 | } |
710 | ||
86ee6e18 | 711 | static void print_aggr(char *prefix) |
d7e7a451 SE |
712 | { |
713 | struct perf_evsel *counter; | |
582ec082 | 714 | int cpu, cpu2, s, s2, id, nr; |
410136f5 | 715 | double uval; |
d7e7a451 | 716 | u64 ena, run, val; |
d7e7a451 | 717 | |
86ee6e18 | 718 | if (!(aggr_map || aggr_get_id)) |
d7e7a451 SE |
719 | return; |
720 | ||
86ee6e18 SE |
721 | for (s = 0; s < aggr_map->nr; s++) { |
722 | id = aggr_map->map[s]; | |
0050f7aa | 723 | evlist__for_each(evsel_list, counter) { |
d7e7a451 SE |
724 | val = ena = run = 0; |
725 | nr = 0; | |
726 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { | |
582ec082 SE |
727 | cpu2 = perf_evsel__cpus(counter)->map[cpu]; |
728 | s2 = aggr_get_id(evsel_list->cpus, cpu2); | |
86ee6e18 | 729 | if (s2 != id) |
d7e7a451 | 730 | continue; |
a6fa0038 JO |
731 | val += perf_counts(counter->counts, cpu, 0)->val; |
732 | ena += perf_counts(counter->counts, cpu, 0)->ena; | |
733 | run += perf_counts(counter->counts, cpu, 0)->run; | |
d7e7a451 SE |
734 | nr++; |
735 | } | |
736 | if (prefix) | |
737 | fprintf(output, "%s", prefix); | |
738 | ||
739 | if (run == 0 || ena == 0) { | |
582ec082 | 740 | aggr_printout(counter, id, nr); |
86ee6e18 | 741 | |
410136f5 | 742 | fprintf(output, "%*s%s", |
d7e7a451 SE |
743 | csv_output ? 0 : 18, |
744 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, | |
410136f5 SE |
745 | csv_sep); |
746 | ||
747 | fprintf(output, "%-*s%s", | |
748 | csv_output ? 0 : unit_width, | |
749 | counter->unit, csv_sep); | |
750 | ||
751 | fprintf(output, "%*s", | |
752 | csv_output ? 0 : -25, | |
d7e7a451 | 753 | perf_evsel__name(counter)); |
86ee6e18 | 754 | |
d7e7a451 SE |
755 | if (counter->cgrp) |
756 | fprintf(output, "%s%s", | |
757 | csv_sep, counter->cgrp->name); | |
758 | ||
d73515c0 | 759 | print_running(run, ena); |
d7e7a451 SE |
760 | fputc('\n', output); |
761 | continue; | |
762 | } | |
410136f5 | 763 | uval = val * counter->scale; |
d7e7a451 SE |
764 | |
765 | if (nsec_counter(counter)) | |
410136f5 | 766 | nsec_printout(id, nr, counter, uval); |
d7e7a451 | 767 | else |
410136f5 | 768 | abs_printout(id, nr, counter, uval); |
d7e7a451 | 769 | |
d73515c0 | 770 | if (!csv_output) |
d7e7a451 SE |
771 | print_noise(counter, 1.0); |
772 | ||
d73515c0 | 773 | print_running(run, ena); |
d7e7a451 SE |
774 | fputc('\n', output); |
775 | } | |
776 | } | |
777 | } | |
778 | ||
2996f5dd IM |
779 | /* |
780 | * Print out the results of a single counter: | |
f5b4a9c3 | 781 | * aggregated counts in system-wide mode |
2996f5dd | 782 | */ |
13370a9b | 783 | static void print_counter_aggr(struct perf_evsel *counter, char *prefix) |
2996f5dd | 784 | { |
69aad6f1 ACM |
785 | struct perf_stat *ps = counter->priv; |
786 | double avg = avg_stats(&ps->res_stats[0]); | |
c52b12ed | 787 | int scaled = counter->counts->scaled; |
410136f5 | 788 | double uval; |
d73515c0 AK |
789 | double avg_enabled, avg_running; |
790 | ||
791 | avg_enabled = avg_stats(&ps->res_stats[1]); | |
792 | avg_running = avg_stats(&ps->res_stats[2]); | |
2996f5dd | 793 | |
13370a9b SE |
794 | if (prefix) |
795 | fprintf(output, "%s", prefix); | |
796 | ||
3b4331d9 | 797 | if (scaled == -1 || !counter->supported) { |
410136f5 | 798 | fprintf(output, "%*s%s", |
d7470b6a | 799 | csv_output ? 0 : 18, |
2cee77c4 | 800 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, |
410136f5 SE |
801 | csv_sep); |
802 | fprintf(output, "%-*s%s", | |
803 | csv_output ? 0 : unit_width, | |
804 | counter->unit, csv_sep); | |
805 | fprintf(output, "%*s", | |
806 | csv_output ? 0 : -25, | |
7289f83c | 807 | perf_evsel__name(counter)); |
023695d9 SE |
808 | |
809 | if (counter->cgrp) | |
4aa9015f | 810 | fprintf(output, "%s%s", csv_sep, counter->cgrp->name); |
023695d9 | 811 | |
d73515c0 | 812 | print_running(avg_running, avg_enabled); |
4aa9015f | 813 | fputc('\n', output); |
2996f5dd IM |
814 | return; |
815 | } | |
c04f5e5d | 816 | |
410136f5 SE |
817 | uval = avg * counter->scale; |
818 | ||
44175b6f | 819 | if (nsec_counter(counter)) |
410136f5 | 820 | nsec_printout(-1, 0, counter, uval); |
44175b6f | 821 | else |
410136f5 | 822 | abs_printout(-1, 0, counter, uval); |
849abde9 | 823 | |
3ae9a34d ZH |
824 | print_noise(counter, avg); |
825 | ||
d73515c0 | 826 | print_running(avg_running, avg_enabled); |
4aa9015f | 827 | fprintf(output, "\n"); |
c04f5e5d IM |
828 | } |
829 | ||
f5b4a9c3 SE |
830 | /* |
831 | * Print out the results of a single counter: | |
832 | * does not use aggregated count in system-wide | |
833 | */ | |
13370a9b | 834 | static void print_counter(struct perf_evsel *counter, char *prefix) |
f5b4a9c3 SE |
835 | { |
836 | u64 ena, run, val; | |
410136f5 | 837 | double uval; |
f5b4a9c3 SE |
838 | int cpu; |
839 | ||
7ae92e74 | 840 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { |
a6fa0038 JO |
841 | val = perf_counts(counter->counts, cpu, 0)->val; |
842 | ena = perf_counts(counter->counts, cpu, 0)->ena; | |
843 | run = perf_counts(counter->counts, cpu, 0)->run; | |
13370a9b SE |
844 | |
845 | if (prefix) | |
846 | fprintf(output, "%s", prefix); | |
847 | ||
f5b4a9c3 | 848 | if (run == 0 || ena == 0) { |
410136f5 | 849 | fprintf(output, "CPU%*d%s%*s%s", |
d7470b6a | 850 | csv_output ? 0 : -4, |
7ae92e74 | 851 | perf_evsel__cpus(counter)->map[cpu], csv_sep, |
d7470b6a | 852 | csv_output ? 0 : 18, |
2cee77c4 | 853 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, |
410136f5 SE |
854 | csv_sep); |
855 | ||
856 | fprintf(output, "%-*s%s", | |
857 | csv_output ? 0 : unit_width, | |
858 | counter->unit, csv_sep); | |
859 | ||
860 | fprintf(output, "%*s", | |
861 | csv_output ? 0 : -25, | |
862 | perf_evsel__name(counter)); | |
f5b4a9c3 | 863 | |
023695d9 | 864 | if (counter->cgrp) |
4aa9015f SE |
865 | fprintf(output, "%s%s", |
866 | csv_sep, counter->cgrp->name); | |
023695d9 | 867 | |
d73515c0 | 868 | print_running(run, ena); |
4aa9015f | 869 | fputc('\n', output); |
f5b4a9c3 SE |
870 | continue; |
871 | } | |
872 | ||
410136f5 SE |
873 | uval = val * counter->scale; |
874 | ||
f5b4a9c3 | 875 | if (nsec_counter(counter)) |
410136f5 | 876 | nsec_printout(cpu, 0, counter, uval); |
f5b4a9c3 | 877 | else |
410136f5 | 878 | abs_printout(cpu, 0, counter, uval); |
f5b4a9c3 | 879 | |
d73515c0 | 880 | if (!csv_output) |
d7470b6a | 881 | print_noise(counter, 1.0); |
d73515c0 | 882 | print_running(run, ena); |
f5b4a9c3 | 883 | |
4aa9015f | 884 | fputc('\n', output); |
f5b4a9c3 SE |
885 | } |
886 | } | |
887 | ||
42202dd5 IM |
888 | static void print_stat(int argc, const char **argv) |
889 | { | |
69aad6f1 ACM |
890 | struct perf_evsel *counter; |
891 | int i; | |
42202dd5 | 892 | |
ddcacfa0 IM |
893 | fflush(stdout); |
894 | ||
d7470b6a | 895 | if (!csv_output) { |
4aa9015f SE |
896 | fprintf(output, "\n"); |
897 | fprintf(output, " Performance counter stats for "); | |
62d3b617 DA |
898 | if (target.system_wide) |
899 | fprintf(output, "\'system wide"); | |
900 | else if (target.cpu_list) | |
901 | fprintf(output, "\'CPU(s) %s", target.cpu_list); | |
602ad878 | 902 | else if (!target__has_task(&target)) { |
4aa9015f | 903 | fprintf(output, "\'%s", argv[0]); |
d7470b6a | 904 | for (i = 1; i < argc; i++) |
4aa9015f | 905 | fprintf(output, " %s", argv[i]); |
20f946b4 NK |
906 | } else if (target.pid) |
907 | fprintf(output, "process id \'%s", target.pid); | |
d7470b6a | 908 | else |
20f946b4 | 909 | fprintf(output, "thread id \'%s", target.tid); |
44db76c8 | 910 | |
4aa9015f | 911 | fprintf(output, "\'"); |
d7470b6a | 912 | if (run_count > 1) |
4aa9015f SE |
913 | fprintf(output, " (%d runs)", run_count); |
914 | fprintf(output, ":\n\n"); | |
d7470b6a | 915 | } |
2996f5dd | 916 | |
86ee6e18 | 917 | switch (aggr_mode) { |
12c08a9f | 918 | case AGGR_CORE: |
86ee6e18 SE |
919 | case AGGR_SOCKET: |
920 | print_aggr(NULL); | |
921 | break; | |
922 | case AGGR_GLOBAL: | |
0050f7aa | 923 | evlist__for_each(evsel_list, counter) |
13370a9b | 924 | print_counter_aggr(counter, NULL); |
86ee6e18 SE |
925 | break; |
926 | case AGGR_NONE: | |
0050f7aa | 927 | evlist__for_each(evsel_list, counter) |
86ee6e18 SE |
928 | print_counter(counter, NULL); |
929 | break; | |
930 | default: | |
931 | break; | |
f5b4a9c3 | 932 | } |
ddcacfa0 | 933 | |
d7470b6a | 934 | if (!csv_output) { |
c3305257 | 935 | if (!null_run) |
4aa9015f SE |
936 | fprintf(output, "\n"); |
937 | fprintf(output, " %17.9f seconds time elapsed", | |
d7470b6a SE |
938 | avg_stats(&walltime_nsecs_stats)/1e9); |
939 | if (run_count > 1) { | |
4aa9015f | 940 | fprintf(output, " "); |
f99844cb IM |
941 | print_noise_pct(stddev_stats(&walltime_nsecs_stats), |
942 | avg_stats(&walltime_nsecs_stats)); | |
d7470b6a | 943 | } |
4aa9015f | 944 | fprintf(output, "\n\n"); |
566747e6 | 945 | } |
ddcacfa0 IM |
946 | } |
947 | ||
f7b7c26e PZ |
948 | static volatile int signr = -1; |
949 | ||
5242519b | 950 | static void skip_signal(int signo) |
ddcacfa0 | 951 | { |
13370a9b | 952 | if ((child_pid == -1) || interval) |
60666c63 LW |
953 | done = 1; |
954 | ||
f7b7c26e | 955 | signr = signo; |
d07f0b12 SE |
956 | /* |
957 | * render child_pid harmless | |
958 | * won't send SIGTERM to a random | |
959 | * process in case of race condition | |
960 | * and fast PID recycling | |
961 | */ | |
962 | child_pid = -1; | |
f7b7c26e PZ |
963 | } |
964 | ||
965 | static void sig_atexit(void) | |
966 | { | |
d07f0b12 SE |
967 | sigset_t set, oset; |
968 | ||
969 | /* | |
970 | * avoid race condition with SIGCHLD handler | |
971 | * in skip_signal() which is modifying child_pid | |
972 | * goal is to avoid send SIGTERM to a random | |
973 | * process | |
974 | */ | |
975 | sigemptyset(&set); | |
976 | sigaddset(&set, SIGCHLD); | |
977 | sigprocmask(SIG_BLOCK, &set, &oset); | |
978 | ||
933da83a CW |
979 | if (child_pid != -1) |
980 | kill(child_pid, SIGTERM); | |
981 | ||
d07f0b12 SE |
982 | sigprocmask(SIG_SETMASK, &oset, NULL); |
983 | ||
f7b7c26e PZ |
984 | if (signr == -1) |
985 | return; | |
986 | ||
987 | signal(signr, SIG_DFL); | |
988 | kill(getpid(), signr); | |
5242519b IM |
989 | } |
990 | ||
1d037ca1 IT |
991 | static int stat__set_big_num(const struct option *opt __maybe_unused, |
992 | const char *s __maybe_unused, int unset) | |
d7470b6a SE |
993 | { |
994 | big_num_opt = unset ? 0 : 1; | |
995 | return 0; | |
996 | } | |
997 | ||
86ee6e18 SE |
998 | static int perf_stat_init_aggr_mode(void) |
999 | { | |
1000 | switch (aggr_mode) { | |
1001 | case AGGR_SOCKET: | |
1002 | if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) { | |
1003 | perror("cannot build socket map"); | |
1004 | return -1; | |
1005 | } | |
1006 | aggr_get_id = cpu_map__get_socket; | |
1007 | break; | |
12c08a9f SE |
1008 | case AGGR_CORE: |
1009 | if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) { | |
1010 | perror("cannot build core map"); | |
1011 | return -1; | |
1012 | } | |
1013 | aggr_get_id = cpu_map__get_core; | |
1014 | break; | |
86ee6e18 SE |
1015 | case AGGR_NONE: |
1016 | case AGGR_GLOBAL: | |
1017 | default: | |
1018 | break; | |
1019 | } | |
1020 | return 0; | |
1021 | } | |
1022 | ||
2cba3ffb IM |
1023 | /* |
1024 | * Add default attributes, if there were no attributes specified or | |
1025 | * if -d/--detailed, -d -d or -d -d -d is used: | |
1026 | */ | |
1027 | static int add_default_attributes(void) | |
1028 | { | |
b070a547 ACM |
1029 | struct perf_event_attr default_attrs[] = { |
1030 | ||
1031 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK }, | |
1032 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES }, | |
1033 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS }, | |
1034 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS }, | |
1035 | ||
1036 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES }, | |
1037 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND }, | |
1038 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND }, | |
1039 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS }, | |
1040 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, | |
1041 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES }, | |
1042 | ||
1043 | }; | |
1044 | ||
1045 | /* | |
1046 | * Detailed stats (-d), covering the L1 and last level data caches: | |
1047 | */ | |
1048 | struct perf_event_attr detailed_attrs[] = { | |
1049 | ||
1050 | { .type = PERF_TYPE_HW_CACHE, | |
1051 | .config = | |
1052 | PERF_COUNT_HW_CACHE_L1D << 0 | | |
1053 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1054 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
1055 | ||
1056 | { .type = PERF_TYPE_HW_CACHE, | |
1057 | .config = | |
1058 | PERF_COUNT_HW_CACHE_L1D << 0 | | |
1059 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1060 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
1061 | ||
1062 | { .type = PERF_TYPE_HW_CACHE, | |
1063 | .config = | |
1064 | PERF_COUNT_HW_CACHE_LL << 0 | | |
1065 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1066 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
1067 | ||
1068 | { .type = PERF_TYPE_HW_CACHE, | |
1069 | .config = | |
1070 | PERF_COUNT_HW_CACHE_LL << 0 | | |
1071 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1072 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
1073 | }; | |
1074 | ||
1075 | /* | |
1076 | * Very detailed stats (-d -d), covering the instruction cache and the TLB caches: | |
1077 | */ | |
1078 | struct perf_event_attr very_detailed_attrs[] = { | |
1079 | ||
1080 | { .type = PERF_TYPE_HW_CACHE, | |
1081 | .config = | |
1082 | PERF_COUNT_HW_CACHE_L1I << 0 | | |
1083 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1084 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
1085 | ||
1086 | { .type = PERF_TYPE_HW_CACHE, | |
1087 | .config = | |
1088 | PERF_COUNT_HW_CACHE_L1I << 0 | | |
1089 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1090 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
1091 | ||
1092 | { .type = PERF_TYPE_HW_CACHE, | |
1093 | .config = | |
1094 | PERF_COUNT_HW_CACHE_DTLB << 0 | | |
1095 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1096 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
1097 | ||
1098 | { .type = PERF_TYPE_HW_CACHE, | |
1099 | .config = | |
1100 | PERF_COUNT_HW_CACHE_DTLB << 0 | | |
1101 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1102 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
1103 | ||
1104 | { .type = PERF_TYPE_HW_CACHE, | |
1105 | .config = | |
1106 | PERF_COUNT_HW_CACHE_ITLB << 0 | | |
1107 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1108 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
1109 | ||
1110 | { .type = PERF_TYPE_HW_CACHE, | |
1111 | .config = | |
1112 | PERF_COUNT_HW_CACHE_ITLB << 0 | | |
1113 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1114 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
1115 | ||
1116 | }; | |
1117 | ||
1118 | /* | |
1119 | * Very, very detailed stats (-d -d -d), adding prefetch events: | |
1120 | */ | |
1121 | struct perf_event_attr very_very_detailed_attrs[] = { | |
1122 | ||
1123 | { .type = PERF_TYPE_HW_CACHE, | |
1124 | .config = | |
1125 | PERF_COUNT_HW_CACHE_L1D << 0 | | |
1126 | (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) | | |
1127 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
1128 | ||
1129 | { .type = PERF_TYPE_HW_CACHE, | |
1130 | .config = | |
1131 | PERF_COUNT_HW_CACHE_L1D << 0 | | |
1132 | (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) | | |
1133 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
1134 | }; | |
1135 | ||
2cba3ffb IM |
1136 | /* Set attrs if no event is selected and !null_run: */ |
1137 | if (null_run) | |
1138 | return 0; | |
1139 | ||
4cabc3d1 AK |
1140 | if (transaction_run) { |
1141 | int err; | |
1142 | if (pmu_have_event("cpu", "cycles-ct") && | |
1143 | pmu_have_event("cpu", "el-start")) | |
a454742c | 1144 | err = parse_events(evsel_list, transaction_attrs, NULL); |
4cabc3d1 | 1145 | else |
a454742c JO |
1146 | err = parse_events(evsel_list, transaction_limited_attrs, NULL); |
1147 | if (err) { | |
4cabc3d1 AK |
1148 | fprintf(stderr, "Cannot set up transaction events\n"); |
1149 | return -1; | |
1150 | } | |
1151 | return 0; | |
1152 | } | |
1153 | ||
2cba3ffb | 1154 | if (!evsel_list->nr_entries) { |
79695e1b | 1155 | if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0) |
50d08e47 | 1156 | return -1; |
2cba3ffb IM |
1157 | } |
1158 | ||
1159 | /* Detailed events get appended to the event list: */ | |
1160 | ||
1161 | if (detailed_run < 1) | |
1162 | return 0; | |
1163 | ||
1164 | /* Append detailed run extra attributes: */ | |
79695e1b | 1165 | if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0) |
50d08e47 | 1166 | return -1; |
2cba3ffb IM |
1167 | |
1168 | if (detailed_run < 2) | |
1169 | return 0; | |
1170 | ||
1171 | /* Append very detailed run extra attributes: */ | |
79695e1b | 1172 | if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0) |
50d08e47 | 1173 | return -1; |
2cba3ffb IM |
1174 | |
1175 | if (detailed_run < 3) | |
1176 | return 0; | |
1177 | ||
1178 | /* Append very, very detailed run extra attributes: */ | |
79695e1b | 1179 | return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs); |
2cba3ffb IM |
1180 | } |
1181 | ||
1d037ca1 | 1182 | int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) |
5242519b | 1183 | { |
1f16c575 | 1184 | bool append_file = false; |
b070a547 ACM |
1185 | int output_fd = 0; |
1186 | const char *output_name = NULL; | |
1187 | const struct option options[] = { | |
4cabc3d1 AK |
1188 | OPT_BOOLEAN('T', "transaction", &transaction_run, |
1189 | "hardware transaction statistics"), | |
b070a547 ACM |
1190 | OPT_CALLBACK('e', "event", &evsel_list, "event", |
1191 | "event selector. use 'perf list' to list available events", | |
1192 | parse_events_option), | |
1193 | OPT_CALLBACK(0, "filter", &evsel_list, "filter", | |
1194 | "event filter", parse_filter), | |
1195 | OPT_BOOLEAN('i', "no-inherit", &no_inherit, | |
1196 | "child tasks do not inherit counters"), | |
1197 | OPT_STRING('p', "pid", &target.pid, "pid", | |
1198 | "stat events on existing process id"), | |
1199 | OPT_STRING('t', "tid", &target.tid, "tid", | |
1200 | "stat events on existing thread id"), | |
1201 | OPT_BOOLEAN('a', "all-cpus", &target.system_wide, | |
1202 | "system-wide collection from all CPUs"), | |
1203 | OPT_BOOLEAN('g', "group", &group, | |
1204 | "put the counters into a counter group"), | |
1205 | OPT_BOOLEAN('c', "scale", &scale, "scale/normalize counters"), | |
1206 | OPT_INCR('v', "verbose", &verbose, | |
1207 | "be more verbose (show counter open errors, etc)"), | |
1208 | OPT_INTEGER('r', "repeat", &run_count, | |
a7e191c3 | 1209 | "repeat command and print average + stddev (max: 100, forever: 0)"), |
b070a547 ACM |
1210 | OPT_BOOLEAN('n', "null", &null_run, |
1211 | "null run - dont start any counters"), | |
1212 | OPT_INCR('d', "detailed", &detailed_run, | |
1213 | "detailed run - start a lot of events"), | |
1214 | OPT_BOOLEAN('S', "sync", &sync_run, | |
1215 | "call sync() before starting a run"), | |
48000a1a | 1216 | OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL, |
b070a547 ACM |
1217 | "print large numbers with thousands\' separators", |
1218 | stat__set_big_num), | |
1219 | OPT_STRING('C', "cpu", &target.cpu_list, "cpu", | |
1220 | "list of cpus to monitor in system-wide"), | |
86ee6e18 SE |
1221 | OPT_SET_UINT('A', "no-aggr", &aggr_mode, |
1222 | "disable CPU count aggregation", AGGR_NONE), | |
b070a547 ACM |
1223 | OPT_STRING('x', "field-separator", &csv_sep, "separator", |
1224 | "print counts with custom separator"), | |
1225 | OPT_CALLBACK('G', "cgroup", &evsel_list, "name", | |
1226 | "monitor event in cgroup name only", parse_cgroups), | |
1227 | OPT_STRING('o', "output", &output_name, "file", "output file name"), | |
1228 | OPT_BOOLEAN(0, "append", &append_file, "append to the output file"), | |
1229 | OPT_INTEGER(0, "log-fd", &output_fd, | |
1230 | "log output to fd, instead of stderr"), | |
1f16c575 PZ |
1231 | OPT_STRING(0, "pre", &pre_cmd, "command", |
1232 | "command to run prior to the measured command"), | |
1233 | OPT_STRING(0, "post", &post_cmd, "command", | |
1234 | "command to run after to the measured command"), | |
13370a9b SE |
1235 | OPT_UINTEGER('I', "interval-print", &interval, |
1236 | "print counts at regular interval in ms (>= 100)"), | |
d4304958 | 1237 | OPT_SET_UINT(0, "per-socket", &aggr_mode, |
86ee6e18 | 1238 | "aggregate counts per processor socket", AGGR_SOCKET), |
12c08a9f SE |
1239 | OPT_SET_UINT(0, "per-core", &aggr_mode, |
1240 | "aggregate counts per physical processor core", AGGR_CORE), | |
41191688 AK |
1241 | OPT_UINTEGER('D', "delay", &initial_delay, |
1242 | "ms to wait before starting measurement after program start"), | |
b070a547 ACM |
1243 | OPT_END() |
1244 | }; | |
1245 | const char * const stat_usage[] = { | |
1246 | "perf stat [<options>] [<command>]", | |
1247 | NULL | |
1248 | }; | |
cc03c542 | 1249 | int status = -EINVAL, run_idx; |
4aa9015f | 1250 | const char *mode; |
42202dd5 | 1251 | |
5af52b51 SE |
1252 | setlocale(LC_ALL, ""); |
1253 | ||
334fe7a3 | 1254 | evsel_list = perf_evlist__new(); |
361c99a6 ACM |
1255 | if (evsel_list == NULL) |
1256 | return -ENOMEM; | |
1257 | ||
a0541234 AB |
1258 | argc = parse_options(argc, argv, options, stat_usage, |
1259 | PARSE_OPT_STOP_AT_NON_OPTION); | |
d7470b6a | 1260 | |
4aa9015f SE |
1261 | output = stderr; |
1262 | if (output_name && strcmp(output_name, "-")) | |
1263 | output = NULL; | |
1264 | ||
56f3bae7 JC |
1265 | if (output_name && output_fd) { |
1266 | fprintf(stderr, "cannot use both --output and --log-fd\n"); | |
cc03c542 NK |
1267 | parse_options_usage(stat_usage, options, "o", 1); |
1268 | parse_options_usage(NULL, options, "log-fd", 0); | |
1269 | goto out; | |
56f3bae7 | 1270 | } |
fc3e4d07 SE |
1271 | |
1272 | if (output_fd < 0) { | |
1273 | fprintf(stderr, "argument to --log-fd must be a > 0\n"); | |
cc03c542 NK |
1274 | parse_options_usage(stat_usage, options, "log-fd", 0); |
1275 | goto out; | |
fc3e4d07 SE |
1276 | } |
1277 | ||
4aa9015f SE |
1278 | if (!output) { |
1279 | struct timespec tm; | |
1280 | mode = append_file ? "a" : "w"; | |
1281 | ||
1282 | output = fopen(output_name, mode); | |
1283 | if (!output) { | |
1284 | perror("failed to create output file"); | |
fceda7fe | 1285 | return -1; |
4aa9015f SE |
1286 | } |
1287 | clock_gettime(CLOCK_REALTIME, &tm); | |
1288 | fprintf(output, "# started on %s\n", ctime(&tm.tv_sec)); | |
fc3e4d07 | 1289 | } else if (output_fd > 0) { |
56f3bae7 JC |
1290 | mode = append_file ? "a" : "w"; |
1291 | output = fdopen(output_fd, mode); | |
1292 | if (!output) { | |
1293 | perror("Failed opening logfd"); | |
1294 | return -errno; | |
1295 | } | |
4aa9015f SE |
1296 | } |
1297 | ||
d4ffd04d | 1298 | if (csv_sep) { |
d7470b6a | 1299 | csv_output = true; |
d4ffd04d JC |
1300 | if (!strcmp(csv_sep, "\\t")) |
1301 | csv_sep = "\t"; | |
1302 | } else | |
d7470b6a SE |
1303 | csv_sep = DEFAULT_SEPARATOR; |
1304 | ||
1305 | /* | |
1306 | * let the spreadsheet do the pretty-printing | |
1307 | */ | |
1308 | if (csv_output) { | |
61a9f324 | 1309 | /* User explicitly passed -B? */ |
d7470b6a SE |
1310 | if (big_num_opt == 1) { |
1311 | fprintf(stderr, "-B option not supported with -x\n"); | |
cc03c542 NK |
1312 | parse_options_usage(stat_usage, options, "B", 1); |
1313 | parse_options_usage(NULL, options, "x", 1); | |
1314 | goto out; | |
d7470b6a SE |
1315 | } else /* Nope, so disable big number formatting */ |
1316 | big_num = false; | |
1317 | } else if (big_num_opt == 0) /* User passed --no-big-num */ | |
1318 | big_num = false; | |
1319 | ||
602ad878 | 1320 | if (!argc && target__none(&target)) |
5242519b | 1321 | usage_with_options(stat_usage, options); |
ac3063bd | 1322 | |
a7e191c3 | 1323 | if (run_count < 0) { |
cc03c542 NK |
1324 | pr_err("Run count must be a positive number\n"); |
1325 | parse_options_usage(stat_usage, options, "r", 1); | |
1326 | goto out; | |
a7e191c3 FD |
1327 | } else if (run_count == 0) { |
1328 | forever = true; | |
1329 | run_count = 1; | |
1330 | } | |
ddcacfa0 | 1331 | |
023695d9 | 1332 | /* no_aggr, cgroup are for system-wide only */ |
602ad878 ACM |
1333 | if ((aggr_mode != AGGR_GLOBAL || nr_cgroups) && |
1334 | !target__has_cpu(&target)) { | |
023695d9 SE |
1335 | fprintf(stderr, "both cgroup and no-aggregation " |
1336 | "modes only available in system-wide mode\n"); | |
1337 | ||
cc03c542 NK |
1338 | parse_options_usage(stat_usage, options, "G", 1); |
1339 | parse_options_usage(NULL, options, "A", 1); | |
1340 | parse_options_usage(NULL, options, "a", 1); | |
1341 | goto out; | |
d7e7a451 SE |
1342 | } |
1343 | ||
2cba3ffb IM |
1344 | if (add_default_attributes()) |
1345 | goto out; | |
ddcacfa0 | 1346 | |
602ad878 | 1347 | target__validate(&target); |
5c98d466 | 1348 | |
77a6f014 | 1349 | if (perf_evlist__create_maps(evsel_list, &target) < 0) { |
602ad878 | 1350 | if (target__has_task(&target)) { |
77a6f014 | 1351 | pr_err("Problems finding threads of monitor\n"); |
cc03c542 NK |
1352 | parse_options_usage(stat_usage, options, "p", 1); |
1353 | parse_options_usage(NULL, options, "t", 1); | |
602ad878 | 1354 | } else if (target__has_cpu(&target)) { |
77a6f014 | 1355 | perror("failed to parse CPUs map"); |
cc03c542 NK |
1356 | parse_options_usage(stat_usage, options, "C", 1); |
1357 | parse_options_usage(NULL, options, "a", 1); | |
1358 | } | |
1359 | goto out; | |
60d567e2 | 1360 | } |
13370a9b SE |
1361 | if (interval && interval < 100) { |
1362 | pr_err("print interval must be >= 100ms\n"); | |
cc03c542 | 1363 | parse_options_usage(stat_usage, options, "I", 1); |
03ad9747 | 1364 | goto out; |
13370a9b | 1365 | } |
c45c6ea2 | 1366 | |
d134ffb9 | 1367 | if (perf_evlist__alloc_stats(evsel_list, interval)) |
03ad9747 | 1368 | goto out; |
d6d901c2 | 1369 | |
86ee6e18 | 1370 | if (perf_stat_init_aggr_mode()) |
03ad9747 | 1371 | goto out; |
86ee6e18 | 1372 | |
58d7e993 IM |
1373 | /* |
1374 | * We dont want to block the signals - that would cause | |
1375 | * child tasks to inherit that and Ctrl-C would not work. | |
1376 | * What we want is for Ctrl-C to work in the exec()-ed | |
1377 | * task, but being ignored by perf stat itself: | |
1378 | */ | |
f7b7c26e | 1379 | atexit(sig_atexit); |
a7e191c3 FD |
1380 | if (!forever) |
1381 | signal(SIGINT, skip_signal); | |
13370a9b | 1382 | signal(SIGCHLD, skip_signal); |
58d7e993 IM |
1383 | signal(SIGALRM, skip_signal); |
1384 | signal(SIGABRT, skip_signal); | |
1385 | ||
42202dd5 | 1386 | status = 0; |
a7e191c3 | 1387 | for (run_idx = 0; forever || run_idx < run_count; run_idx++) { |
42202dd5 | 1388 | if (run_count != 1 && verbose) |
4aa9015f SE |
1389 | fprintf(output, "[ perf stat: executing run #%d ... ]\n", |
1390 | run_idx + 1); | |
f9cef0a9 | 1391 | |
42202dd5 | 1392 | status = run_perf_stat(argc, argv); |
a7e191c3 FD |
1393 | if (forever && status != -1) { |
1394 | print_stat(argc, argv); | |
254ecbc7 | 1395 | perf_stat__reset_stats(); |
a7e191c3 | 1396 | } |
42202dd5 IM |
1397 | } |
1398 | ||
a7e191c3 | 1399 | if (!forever && status != -1 && !interval) |
084ab9f8 | 1400 | print_stat(argc, argv); |
d134ffb9 ACM |
1401 | |
1402 | perf_evlist__free_stats(evsel_list); | |
0015e2e1 ACM |
1403 | out: |
1404 | perf_evlist__delete(evsel_list); | |
42202dd5 | 1405 | return status; |
ddcacfa0 | 1406 | } |