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" |
4b6ab94e | 48 | #include <subcmd/parse-options.h> |
5242519b | 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" |
d809560b | 61 | #include "util/counts.h" |
4979d0c7 | 62 | #include "util/session.h" |
ddcacfa0 | 63 | |
1f16c575 | 64 | #include <stdlib.h> |
ddcacfa0 | 65 | #include <sys/prctl.h> |
5af52b51 | 66 | #include <locale.h> |
16c8a109 | 67 | |
d7470b6a | 68 | #define DEFAULT_SEPARATOR " " |
2cee77c4 DA |
69 | #define CNTR_NOT_SUPPORTED "<not supported>" |
70 | #define CNTR_NOT_COUNTED "<not counted>" | |
d7470b6a | 71 | |
d4f63a47 | 72 | static void print_counters(struct timespec *ts, int argc, const char **argv); |
13370a9b | 73 | |
4cabc3d1 | 74 | /* Default events used for perf stat -T */ |
a454742c JO |
75 | static const char *transaction_attrs = { |
76 | "task-clock," | |
4cabc3d1 AK |
77 | "{" |
78 | "instructions," | |
79 | "cycles," | |
80 | "cpu/cycles-t/," | |
81 | "cpu/tx-start/," | |
82 | "cpu/el-start/," | |
83 | "cpu/cycles-ct/" | |
84 | "}" | |
85 | }; | |
86 | ||
87 | /* More limited version when the CPU does not have all events. */ | |
a454742c JO |
88 | static const char * transaction_limited_attrs = { |
89 | "task-clock," | |
4cabc3d1 AK |
90 | "{" |
91 | "instructions," | |
92 | "cycles," | |
93 | "cpu/cycles-t/," | |
94 | "cpu/tx-start/" | |
95 | "}" | |
96 | }; | |
97 | ||
666e6d48 | 98 | static struct perf_evlist *evsel_list; |
361c99a6 | 99 | |
602ad878 | 100 | static struct target target = { |
77a6f014 NK |
101 | .uid = UINT_MAX, |
102 | }; | |
ddcacfa0 | 103 | |
1e5a2931 JO |
104 | typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu); |
105 | ||
3d632595 | 106 | static int run_count = 1; |
2e6cdf99 | 107 | static bool no_inherit = false; |
d07f0b12 | 108 | static volatile pid_t child_pid = -1; |
c0555642 | 109 | static bool null_run = false; |
2cba3ffb | 110 | static int detailed_run = 0; |
4cabc3d1 | 111 | static bool transaction_run; |
201e0b06 | 112 | static bool big_num = true; |
d7470b6a | 113 | static int big_num_opt = -1; |
d7470b6a SE |
114 | static const char *csv_sep = NULL; |
115 | static bool csv_output = false; | |
43bece79 | 116 | static bool group = false; |
1f16c575 PZ |
117 | static const char *pre_cmd = NULL; |
118 | static const char *post_cmd = NULL; | |
119 | static bool sync_run = false; | |
41191688 | 120 | static unsigned int initial_delay = 0; |
410136f5 | 121 | static unsigned int unit_width = 4; /* strlen("unit") */ |
a7e191c3 | 122 | static bool forever = false; |
13370a9b | 123 | static struct timespec ref_time; |
86ee6e18 | 124 | static struct cpu_map *aggr_map; |
1e5a2931 | 125 | static aggr_get_id_t aggr_get_id; |
e0547311 JO |
126 | static bool append_file; |
127 | static const char *output_name; | |
128 | static int output_fd; | |
5af52b51 | 129 | |
4979d0c7 JO |
130 | struct perf_stat { |
131 | bool record; | |
132 | struct perf_data_file file; | |
133 | struct perf_session *session; | |
134 | u64 bytes_written; | |
135 | }; | |
136 | ||
137 | static struct perf_stat perf_stat; | |
138 | #define STAT_RECORD perf_stat.record | |
139 | ||
60666c63 LW |
140 | static volatile int done = 0; |
141 | ||
421a50f3 JO |
142 | static struct perf_stat_config stat_config = { |
143 | .aggr_mode = AGGR_GLOBAL, | |
711a572e | 144 | .scale = true, |
421a50f3 JO |
145 | }; |
146 | ||
13370a9b SE |
147 | static inline void diff_timespec(struct timespec *r, struct timespec *a, |
148 | struct timespec *b) | |
149 | { | |
150 | r->tv_sec = a->tv_sec - b->tv_sec; | |
151 | if (a->tv_nsec < b->tv_nsec) { | |
152 | r->tv_nsec = a->tv_nsec + 1000000000L - b->tv_nsec; | |
153 | r->tv_sec--; | |
154 | } else { | |
155 | r->tv_nsec = a->tv_nsec - b->tv_nsec ; | |
156 | } | |
157 | } | |
158 | ||
254ecbc7 JO |
159 | static void perf_stat__reset_stats(void) |
160 | { | |
161 | perf_evlist__reset_stats(evsel_list); | |
f87027b9 | 162 | perf_stat__reset_shadow_stats(); |
1eda3b21 JO |
163 | } |
164 | ||
cac21425 | 165 | static int create_perf_stat_counter(struct perf_evsel *evsel) |
ddcacfa0 | 166 | { |
69aad6f1 | 167 | struct perf_event_attr *attr = &evsel->attr; |
727ab04e | 168 | |
711a572e | 169 | if (stat_config.scale) |
a21ca2ca IM |
170 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | |
171 | PERF_FORMAT_TOTAL_TIME_RUNNING; | |
ddcacfa0 | 172 | |
5d2cd909 ACM |
173 | attr->inherit = !no_inherit; |
174 | ||
6acd8e92 JO |
175 | /* |
176 | * Some events get initialized with sample_(period/type) set, | |
177 | * like tracepoints. Clear it up for counting. | |
178 | */ | |
179 | attr->sample_period = 0; | |
4979d0c7 JO |
180 | /* |
181 | * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless | |
182 | * while avoiding that older tools show confusing messages. | |
183 | */ | |
184 | attr->sample_type = PERF_SAMPLE_IDENTIFIER; | |
6acd8e92 | 185 | |
67ccdecd JO |
186 | /* |
187 | * Disabling all counters initially, they will be enabled | |
188 | * either manually by us or by kernel via enable_on_exec | |
189 | * set later. | |
190 | */ | |
c8280cec | 191 | if (perf_evsel__is_group_leader(evsel)) { |
67ccdecd JO |
192 | attr->disabled = 1; |
193 | ||
c8280cec JO |
194 | /* |
195 | * In case of initial_delay we enable tracee | |
196 | * events manually. | |
197 | */ | |
198 | if (target__none(&target) && !initial_delay) | |
41191688 | 199 | attr->enable_on_exec = 1; |
ddcacfa0 | 200 | } |
084ab9f8 | 201 | |
c8280cec JO |
202 | if (target__has_cpu(&target)) |
203 | return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel)); | |
204 | ||
594ac61a | 205 | return perf_evsel__open_per_thread(evsel, evsel_list->threads); |
ddcacfa0 IM |
206 | } |
207 | ||
c04f5e5d IM |
208 | /* |
209 | * Does the counter have nsecs as a unit? | |
210 | */ | |
daec78a0 | 211 | static inline int nsec_counter(struct perf_evsel *evsel) |
c04f5e5d | 212 | { |
daec78a0 ACM |
213 | if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) || |
214 | perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK)) | |
c04f5e5d IM |
215 | return 1; |
216 | ||
217 | return 0; | |
218 | } | |
219 | ||
8b99b1a4 JO |
220 | static int process_synthesized_event(struct perf_tool *tool __maybe_unused, |
221 | union perf_event *event, | |
222 | struct perf_sample *sample __maybe_unused, | |
223 | struct machine *machine __maybe_unused) | |
4979d0c7 | 224 | { |
8b99b1a4 | 225 | if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) { |
4979d0c7 JO |
226 | pr_err("failed to write perf data, error: %m\n"); |
227 | return -1; | |
228 | } | |
229 | ||
8b99b1a4 | 230 | perf_stat.bytes_written += event->header.size; |
4979d0c7 JO |
231 | return 0; |
232 | } | |
233 | ||
f5b4a9c3 SE |
234 | /* |
235 | * Read out the results of a single counter: | |
236 | * do not aggregate counts across CPUs in system-wide mode | |
237 | */ | |
c52b12ed | 238 | static int read_counter(struct perf_evsel *counter) |
f5b4a9c3 | 239 | { |
9bf1a529 JO |
240 | int nthreads = thread_map__nr(evsel_list->threads); |
241 | int ncpus = perf_evsel__nr_cpus(counter); | |
242 | int cpu, thread; | |
f5b4a9c3 | 243 | |
3b4331d9 SP |
244 | if (!counter->supported) |
245 | return -ENOENT; | |
246 | ||
9bf1a529 JO |
247 | if (counter->system_wide) |
248 | nthreads = 1; | |
249 | ||
250 | for (thread = 0; thread < nthreads; thread++) { | |
251 | for (cpu = 0; cpu < ncpus; cpu++) { | |
3b3eb044 JO |
252 | struct perf_counts_values *count; |
253 | ||
254 | count = perf_counts(counter->counts, cpu, thread); | |
255 | if (perf_evsel__read(counter, cpu, thread, count)) | |
9bf1a529 JO |
256 | return -1; |
257 | } | |
f5b4a9c3 | 258 | } |
c52b12ed ACM |
259 | |
260 | return 0; | |
2996f5dd IM |
261 | } |
262 | ||
5fc472a6 | 263 | static void read_counters(bool close_counters) |
13370a9b | 264 | { |
13370a9b | 265 | struct perf_evsel *counter; |
13370a9b | 266 | |
106a94a0 | 267 | evlist__for_each(evsel_list, counter) { |
3b3eb044 | 268 | if (read_counter(counter)) |
245bad8e | 269 | pr_debug("failed to read counter %s\n", counter->name); |
3b3eb044 | 270 | |
f80010eb | 271 | if (perf_stat_process_counter(&stat_config, counter)) |
3b3eb044 | 272 | pr_warning("failed to process counter %s\n", counter->name); |
106a94a0 | 273 | |
5fc472a6 | 274 | if (close_counters) { |
106a94a0 JO |
275 | perf_evsel__close_fd(counter, perf_evsel__nr_cpus(counter), |
276 | thread_map__nr(evsel_list->threads)); | |
13370a9b SE |
277 | } |
278 | } | |
106a94a0 JO |
279 | } |
280 | ||
ba411a95 | 281 | static void process_interval(void) |
106a94a0 | 282 | { |
106a94a0 | 283 | struct timespec ts, rs; |
106a94a0 JO |
284 | |
285 | read_counters(false); | |
86ee6e18 | 286 | |
13370a9b SE |
287 | clock_gettime(CLOCK_MONOTONIC, &ts); |
288 | diff_timespec(&rs, &ts, &ref_time); | |
13370a9b | 289 | |
d4f63a47 | 290 | print_counters(&rs, 0, NULL); |
13370a9b SE |
291 | } |
292 | ||
67ccdecd | 293 | static void enable_counters(void) |
41191688 | 294 | { |
67ccdecd | 295 | if (initial_delay) |
41191688 | 296 | usleep(initial_delay * 1000); |
67ccdecd JO |
297 | |
298 | /* | |
299 | * We need to enable counters only if: | |
300 | * - we don't have tracee (attaching to task or cpu) | |
301 | * - we have initial delay configured | |
302 | */ | |
303 | if (!target__none(&target) || initial_delay) | |
ab46db0a | 304 | perf_evlist__enable(evsel_list); |
41191688 AK |
305 | } |
306 | ||
f33cbe72 | 307 | static volatile int workload_exec_errno; |
6af206fd ACM |
308 | |
309 | /* | |
310 | * perf_evlist__prepare_workload will send a SIGUSR1 | |
311 | * if the fork fails, since we asked by setting its | |
312 | * want_signal to true. | |
313 | */ | |
f33cbe72 ACM |
314 | static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info, |
315 | void *ucontext __maybe_unused) | |
6af206fd | 316 | { |
f33cbe72 | 317 | workload_exec_errno = info->si_value.sival_int; |
6af206fd ACM |
318 | } |
319 | ||
8b99b1a4 JO |
320 | static int perf_stat_synthesize_config(void) |
321 | { | |
322 | int err; | |
323 | ||
324 | err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads, | |
325 | process_synthesized_event, | |
326 | NULL); | |
327 | if (err < 0) { | |
328 | pr_err("Couldn't synthesize thread map.\n"); | |
329 | return err; | |
330 | } | |
331 | ||
332 | err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus, | |
333 | process_synthesized_event, NULL); | |
334 | if (err < 0) { | |
335 | pr_err("Couldn't synthesize thread map.\n"); | |
336 | return err; | |
337 | } | |
338 | ||
339 | err = perf_event__synthesize_stat_config(NULL, &stat_config, | |
340 | process_synthesized_event, NULL); | |
341 | if (err < 0) { | |
342 | pr_err("Couldn't synthesize config.\n"); | |
343 | return err; | |
344 | } | |
345 | ||
346 | return 0; | |
347 | } | |
348 | ||
acf28922 | 349 | static int __run_perf_stat(int argc, const char **argv) |
42202dd5 | 350 | { |
ec0d3d1f | 351 | int interval = stat_config.interval; |
56e52e85 | 352 | char msg[512]; |
42202dd5 | 353 | unsigned long long t0, t1; |
cac21425 | 354 | struct perf_evsel *counter; |
13370a9b | 355 | struct timespec ts; |
410136f5 | 356 | size_t l; |
42202dd5 | 357 | int status = 0; |
6be2850e | 358 | const bool forks = (argc > 0); |
42202dd5 | 359 | |
13370a9b SE |
360 | if (interval) { |
361 | ts.tv_sec = interval / 1000; | |
362 | ts.tv_nsec = (interval % 1000) * 1000000; | |
363 | } else { | |
364 | ts.tv_sec = 1; | |
365 | ts.tv_nsec = 0; | |
366 | } | |
367 | ||
60666c63 | 368 | if (forks) { |
735f7e0b ACM |
369 | if (perf_evlist__prepare_workload(evsel_list, &target, argv, false, |
370 | workload_exec_failed_signal) < 0) { | |
acf28922 NK |
371 | perror("failed to prepare workload"); |
372 | return -1; | |
60666c63 | 373 | } |
d20a47e7 | 374 | child_pid = evsel_list->workload.pid; |
051ae7f7 PM |
375 | } |
376 | ||
6a4bb04c | 377 | if (group) |
63dab225 | 378 | perf_evlist__set_leader(evsel_list); |
6a4bb04c | 379 | |
0050f7aa | 380 | evlist__for_each(evsel_list, counter) { |
cac21425 | 381 | if (create_perf_stat_counter(counter) < 0) { |
979987a5 DA |
382 | /* |
383 | * PPC returns ENXIO for HW counters until 2.6.37 | |
384 | * (behavior changed with commit b0a873e). | |
385 | */ | |
38f6ae1e | 386 | if (errno == EINVAL || errno == ENOSYS || |
979987a5 DA |
387 | errno == ENOENT || errno == EOPNOTSUPP || |
388 | errno == ENXIO) { | |
c63ca0c0 DA |
389 | if (verbose) |
390 | ui__warning("%s event is not supported by the kernel.\n", | |
7289f83c | 391 | perf_evsel__name(counter)); |
2cee77c4 | 392 | counter->supported = false; |
cb5ef600 KL |
393 | |
394 | if ((counter->leader != counter) || | |
395 | !(counter->leader->nr_members > 1)) | |
396 | continue; | |
c63ca0c0 | 397 | } |
ede70290 | 398 | |
56e52e85 ACM |
399 | perf_evsel__open_strerror(counter, &target, |
400 | errno, msg, sizeof(msg)); | |
401 | ui__error("%s\n", msg); | |
402 | ||
48290609 ACM |
403 | if (child_pid != -1) |
404 | kill(child_pid, SIGTERM); | |
fceda7fe | 405 | |
48290609 ACM |
406 | return -1; |
407 | } | |
2cee77c4 | 408 | counter->supported = true; |
410136f5 SE |
409 | |
410 | l = strlen(counter->unit); | |
411 | if (l > unit_width) | |
412 | unit_width = l; | |
084ab9f8 | 413 | } |
42202dd5 | 414 | |
23d4aad4 ACM |
415 | if (perf_evlist__apply_filters(evsel_list, &counter)) { |
416 | error("failed to set filter \"%s\" on event %s with %d (%s)\n", | |
417 | counter->filter, perf_evsel__name(counter), errno, | |
759e612b | 418 | strerror_r(errno, msg, sizeof(msg))); |
cfd748ae FW |
419 | return -1; |
420 | } | |
421 | ||
4979d0c7 JO |
422 | if (STAT_RECORD) { |
423 | int err, fd = perf_data_file__fd(&perf_stat.file); | |
424 | ||
425 | err = perf_session__write_header(perf_stat.session, evsel_list, | |
426 | fd, false); | |
427 | if (err < 0) | |
428 | return err; | |
8b99b1a4 JO |
429 | |
430 | err = perf_stat_synthesize_config(); | |
431 | if (err < 0) | |
432 | return err; | |
4979d0c7 JO |
433 | } |
434 | ||
42202dd5 IM |
435 | /* |
436 | * Enable counters and exec the command: | |
437 | */ | |
438 | t0 = rdclock(); | |
13370a9b | 439 | clock_gettime(CLOCK_MONOTONIC, &ref_time); |
42202dd5 | 440 | |
60666c63 | 441 | if (forks) { |
acf28922 | 442 | perf_evlist__start_workload(evsel_list); |
67ccdecd | 443 | enable_counters(); |
acf28922 | 444 | |
13370a9b SE |
445 | if (interval) { |
446 | while (!waitpid(child_pid, &status, WNOHANG)) { | |
447 | nanosleep(&ts, NULL); | |
ba411a95 | 448 | process_interval(); |
13370a9b SE |
449 | } |
450 | } | |
60666c63 | 451 | wait(&status); |
6af206fd | 452 | |
f33cbe72 ACM |
453 | if (workload_exec_errno) { |
454 | const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg)); | |
455 | pr_err("Workload failed: %s\n", emsg); | |
6af206fd | 456 | return -1; |
f33cbe72 | 457 | } |
6af206fd | 458 | |
33e49ea7 AK |
459 | if (WIFSIGNALED(status)) |
460 | psignal(WTERMSIG(status), argv[0]); | |
60666c63 | 461 | } else { |
67ccdecd | 462 | enable_counters(); |
13370a9b SE |
463 | while (!done) { |
464 | nanosleep(&ts, NULL); | |
465 | if (interval) | |
ba411a95 | 466 | process_interval(); |
13370a9b | 467 | } |
60666c63 | 468 | } |
42202dd5 | 469 | |
42202dd5 IM |
470 | t1 = rdclock(); |
471 | ||
9e9772c4 | 472 | update_stats(&walltime_nsecs_stats, t1 - t0); |
42202dd5 | 473 | |
106a94a0 | 474 | read_counters(true); |
c52b12ed | 475 | |
42202dd5 IM |
476 | return WEXITSTATUS(status); |
477 | } | |
478 | ||
41cde476 | 479 | static int run_perf_stat(int argc, const char **argv) |
1f16c575 PZ |
480 | { |
481 | int ret; | |
482 | ||
483 | if (pre_cmd) { | |
484 | ret = system(pre_cmd); | |
485 | if (ret) | |
486 | return ret; | |
487 | } | |
488 | ||
489 | if (sync_run) | |
490 | sync(); | |
491 | ||
492 | ret = __run_perf_stat(argc, argv); | |
493 | if (ret) | |
494 | return ret; | |
495 | ||
496 | if (post_cmd) { | |
497 | ret = system(post_cmd); | |
498 | if (ret) | |
499 | return ret; | |
500 | } | |
501 | ||
502 | return ret; | |
503 | } | |
504 | ||
d73515c0 AK |
505 | static void print_running(u64 run, u64 ena) |
506 | { | |
507 | if (csv_output) { | |
5821522e | 508 | fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f", |
d73515c0 AK |
509 | csv_sep, |
510 | run, | |
511 | csv_sep, | |
512 | ena ? 100.0 * run / ena : 100.0); | |
513 | } else if (run != ena) { | |
5821522e | 514 | fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena); |
d73515c0 AK |
515 | } |
516 | } | |
517 | ||
f99844cb IM |
518 | static void print_noise_pct(double total, double avg) |
519 | { | |
0007ecea | 520 | double pct = rel_stddev_stats(total, avg); |
f99844cb | 521 | |
3ae9a34d | 522 | if (csv_output) |
5821522e | 523 | fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct); |
a1bca6cc | 524 | else if (pct) |
5821522e | 525 | fprintf(stat_config.output, " ( +-%6.2f%% )", pct); |
f99844cb IM |
526 | } |
527 | ||
69aad6f1 | 528 | static void print_noise(struct perf_evsel *evsel, double avg) |
42202dd5 | 529 | { |
581cc8a2 | 530 | struct perf_stat_evsel *ps; |
69aad6f1 | 531 | |
849abde9 PZ |
532 | if (run_count == 1) |
533 | return; | |
534 | ||
69aad6f1 | 535 | ps = evsel->priv; |
f99844cb | 536 | print_noise_pct(stddev_stats(&ps->res_stats[0]), avg); |
42202dd5 IM |
537 | } |
538 | ||
12c08a9f | 539 | static void aggr_printout(struct perf_evsel *evsel, int id, int nr) |
44175b6f | 540 | { |
421a50f3 | 541 | switch (stat_config.aggr_mode) { |
12c08a9f | 542 | case AGGR_CORE: |
5821522e | 543 | fprintf(stat_config.output, "S%d-C%*d%s%*d%s", |
12c08a9f SE |
544 | cpu_map__id_to_socket(id), |
545 | csv_output ? 0 : -8, | |
546 | cpu_map__id_to_cpu(id), | |
547 | csv_sep, | |
548 | csv_output ? 0 : 4, | |
549 | nr, | |
550 | csv_sep); | |
551 | break; | |
86ee6e18 | 552 | case AGGR_SOCKET: |
5821522e | 553 | fprintf(stat_config.output, "S%*d%s%*d%s", |
d7e7a451 | 554 | csv_output ? 0 : -5, |
12c08a9f | 555 | id, |
d7e7a451 SE |
556 | csv_sep, |
557 | csv_output ? 0 : 4, | |
558 | nr, | |
559 | csv_sep); | |
86ee6e18 SE |
560 | break; |
561 | case AGGR_NONE: | |
5821522e | 562 | fprintf(stat_config.output, "CPU%*d%s", |
d7470b6a | 563 | csv_output ? 0 : -4, |
12c08a9f | 564 | perf_evsel__cpus(evsel)->map[id], csv_sep); |
86ee6e18 | 565 | break; |
32b8af82 | 566 | case AGGR_THREAD: |
5821522e | 567 | fprintf(stat_config.output, "%*s-%*d%s", |
32b8af82 JO |
568 | csv_output ? 0 : 16, |
569 | thread_map__comm(evsel->threads, id), | |
570 | csv_output ? 0 : -8, | |
571 | thread_map__pid(evsel->threads, id), | |
572 | csv_sep); | |
573 | break; | |
86ee6e18 | 574 | case AGGR_GLOBAL: |
208df99e | 575 | case AGGR_UNSET: |
86ee6e18 SE |
576 | default: |
577 | break; | |
578 | } | |
579 | } | |
580 | ||
da88c7f7 | 581 | static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg) |
86ee6e18 | 582 | { |
5821522e | 583 | FILE *output = stat_config.output; |
86ee6e18 | 584 | double msecs = avg / 1e6; |
410136f5 | 585 | const char *fmt_v, *fmt_n; |
4bbe5a61 | 586 | char name[25]; |
86ee6e18 | 587 | |
410136f5 SE |
588 | fmt_v = csv_output ? "%.6f%s" : "%18.6f%s"; |
589 | fmt_n = csv_output ? "%s" : "%-25s"; | |
590 | ||
da88c7f7 | 591 | aggr_printout(evsel, id, nr); |
d7470b6a | 592 | |
4bbe5a61 DA |
593 | scnprintf(name, sizeof(name), "%s%s", |
594 | perf_evsel__name(evsel), csv_output ? "" : " (msec)"); | |
410136f5 SE |
595 | |
596 | fprintf(output, fmt_v, msecs, csv_sep); | |
597 | ||
598 | if (csv_output) | |
599 | fprintf(output, "%s%s", evsel->unit, csv_sep); | |
600 | else | |
601 | fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep); | |
602 | ||
603 | fprintf(output, fmt_n, name); | |
d7470b6a | 604 | |
023695d9 | 605 | if (evsel->cgrp) |
4aa9015f | 606 | fprintf(output, "%s%s", csv_sep, evsel->cgrp->name); |
44175b6f IM |
607 | } |
608 | ||
556b1fb7 JO |
609 | static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg) |
610 | { | |
5821522e | 611 | FILE *output = stat_config.output; |
556b1fb7 JO |
612 | double sc = evsel->scale; |
613 | const char *fmt; | |
556b1fb7 JO |
614 | |
615 | if (csv_output) { | |
616 | fmt = sc != 1.0 ? "%.2f%s" : "%.0f%s"; | |
617 | } else { | |
618 | if (big_num) | |
619 | fmt = sc != 1.0 ? "%'18.2f%s" : "%'18.0f%s"; | |
620 | else | |
621 | fmt = sc != 1.0 ? "%18.2f%s" : "%18.0f%s"; | |
622 | } | |
623 | ||
624 | aggr_printout(evsel, id, nr); | |
625 | ||
556b1fb7 JO |
626 | fprintf(output, fmt, avg, csv_sep); |
627 | ||
628 | if (evsel->unit) | |
629 | fprintf(output, "%-*s%s", | |
630 | csv_output ? 0 : unit_width, | |
631 | evsel->unit, csv_sep); | |
632 | ||
633 | fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel)); | |
634 | ||
635 | if (evsel->cgrp) | |
636 | fprintf(output, "%s%s", csv_sep, evsel->cgrp->name); | |
eedfcb4b | 637 | } |
556b1fb7 | 638 | |
eedfcb4b AK |
639 | static void printout(int id, int nr, struct perf_evsel *counter, double uval) |
640 | { | |
641 | int cpu = cpu_map__id_to_cpu(id); | |
642 | ||
643 | if (stat_config.aggr_mode == AGGR_GLOBAL) | |
644 | cpu = 0; | |
645 | ||
646 | if (nsec_counter(counter)) | |
647 | nsec_printout(id, nr, counter, uval); | |
648 | else | |
649 | abs_printout(id, nr, counter, uval); | |
556b1fb7 | 650 | |
eedfcb4b AK |
651 | if (!csv_output && !stat_config.interval) |
652 | perf_stat__print_shadow_stats(stat_config.output, counter, | |
653 | uval, cpu, | |
654 | stat_config.aggr_mode); | |
556b1fb7 JO |
655 | } |
656 | ||
86ee6e18 | 657 | static void print_aggr(char *prefix) |
d7e7a451 | 658 | { |
5821522e | 659 | FILE *output = stat_config.output; |
d7e7a451 | 660 | struct perf_evsel *counter; |
601083cf | 661 | int cpu, s, s2, id, nr; |
410136f5 | 662 | double uval; |
d7e7a451 | 663 | u64 ena, run, val; |
d7e7a451 | 664 | |
86ee6e18 | 665 | if (!(aggr_map || aggr_get_id)) |
d7e7a451 SE |
666 | return; |
667 | ||
86ee6e18 SE |
668 | for (s = 0; s < aggr_map->nr; s++) { |
669 | id = aggr_map->map[s]; | |
0050f7aa | 670 | evlist__for_each(evsel_list, counter) { |
d7e7a451 SE |
671 | val = ena = run = 0; |
672 | nr = 0; | |
673 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { | |
601083cf | 674 | s2 = aggr_get_id(perf_evsel__cpus(counter), cpu); |
86ee6e18 | 675 | if (s2 != id) |
d7e7a451 | 676 | continue; |
a6fa0038 JO |
677 | val += perf_counts(counter->counts, cpu, 0)->val; |
678 | ena += perf_counts(counter->counts, cpu, 0)->ena; | |
679 | run += perf_counts(counter->counts, cpu, 0)->run; | |
d7e7a451 SE |
680 | nr++; |
681 | } | |
682 | if (prefix) | |
683 | fprintf(output, "%s", prefix); | |
684 | ||
685 | if (run == 0 || ena == 0) { | |
582ec082 | 686 | aggr_printout(counter, id, nr); |
86ee6e18 | 687 | |
410136f5 | 688 | fprintf(output, "%*s%s", |
d7e7a451 SE |
689 | csv_output ? 0 : 18, |
690 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, | |
410136f5 SE |
691 | csv_sep); |
692 | ||
693 | fprintf(output, "%-*s%s", | |
694 | csv_output ? 0 : unit_width, | |
695 | counter->unit, csv_sep); | |
696 | ||
697 | fprintf(output, "%*s", | |
698 | csv_output ? 0 : -25, | |
d7e7a451 | 699 | perf_evsel__name(counter)); |
86ee6e18 | 700 | |
d7e7a451 SE |
701 | if (counter->cgrp) |
702 | fprintf(output, "%s%s", | |
703 | csv_sep, counter->cgrp->name); | |
704 | ||
d73515c0 | 705 | print_running(run, ena); |
d7e7a451 SE |
706 | fputc('\n', output); |
707 | continue; | |
708 | } | |
410136f5 | 709 | uval = val * counter->scale; |
eedfcb4b | 710 | printout(id, nr, counter, uval); |
d73515c0 | 711 | if (!csv_output) |
d7e7a451 SE |
712 | print_noise(counter, 1.0); |
713 | ||
d73515c0 | 714 | print_running(run, ena); |
d7e7a451 SE |
715 | fputc('\n', output); |
716 | } | |
717 | } | |
718 | } | |
719 | ||
32b8af82 JO |
720 | static void print_aggr_thread(struct perf_evsel *counter, char *prefix) |
721 | { | |
5821522e | 722 | FILE *output = stat_config.output; |
32b8af82 JO |
723 | int nthreads = thread_map__nr(counter->threads); |
724 | int ncpus = cpu_map__nr(counter->cpus); | |
725 | int cpu, thread; | |
726 | double uval; | |
727 | ||
728 | for (thread = 0; thread < nthreads; thread++) { | |
729 | u64 ena = 0, run = 0, val = 0; | |
730 | ||
731 | for (cpu = 0; cpu < ncpus; cpu++) { | |
732 | val += perf_counts(counter->counts, cpu, thread)->val; | |
733 | ena += perf_counts(counter->counts, cpu, thread)->ena; | |
734 | run += perf_counts(counter->counts, cpu, thread)->run; | |
735 | } | |
736 | ||
737 | if (prefix) | |
738 | fprintf(output, "%s", prefix); | |
739 | ||
740 | uval = val * counter->scale; | |
eedfcb4b | 741 | printout(thread, 0, counter, uval); |
32b8af82 JO |
742 | |
743 | if (!csv_output) | |
744 | print_noise(counter, 1.0); | |
745 | ||
746 | print_running(run, ena); | |
747 | fputc('\n', output); | |
748 | } | |
749 | } | |
750 | ||
2996f5dd IM |
751 | /* |
752 | * Print out the results of a single counter: | |
f5b4a9c3 | 753 | * aggregated counts in system-wide mode |
2996f5dd | 754 | */ |
13370a9b | 755 | static void print_counter_aggr(struct perf_evsel *counter, char *prefix) |
2996f5dd | 756 | { |
5821522e | 757 | FILE *output = stat_config.output; |
581cc8a2 | 758 | struct perf_stat_evsel *ps = counter->priv; |
69aad6f1 | 759 | double avg = avg_stats(&ps->res_stats[0]); |
c52b12ed | 760 | int scaled = counter->counts->scaled; |
410136f5 | 761 | double uval; |
d73515c0 AK |
762 | double avg_enabled, avg_running; |
763 | ||
764 | avg_enabled = avg_stats(&ps->res_stats[1]); | |
765 | avg_running = avg_stats(&ps->res_stats[2]); | |
2996f5dd | 766 | |
13370a9b SE |
767 | if (prefix) |
768 | fprintf(output, "%s", prefix); | |
769 | ||
3b4331d9 | 770 | if (scaled == -1 || !counter->supported) { |
410136f5 | 771 | fprintf(output, "%*s%s", |
d7470b6a | 772 | csv_output ? 0 : 18, |
2cee77c4 | 773 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, |
410136f5 SE |
774 | csv_sep); |
775 | fprintf(output, "%-*s%s", | |
776 | csv_output ? 0 : unit_width, | |
777 | counter->unit, csv_sep); | |
778 | fprintf(output, "%*s", | |
779 | csv_output ? 0 : -25, | |
7289f83c | 780 | perf_evsel__name(counter)); |
023695d9 SE |
781 | |
782 | if (counter->cgrp) | |
4aa9015f | 783 | fprintf(output, "%s%s", csv_sep, counter->cgrp->name); |
023695d9 | 784 | |
d73515c0 | 785 | print_running(avg_running, avg_enabled); |
4aa9015f | 786 | fputc('\n', output); |
2996f5dd IM |
787 | return; |
788 | } | |
c04f5e5d | 789 | |
410136f5 | 790 | uval = avg * counter->scale; |
eedfcb4b | 791 | printout(-1, 0, counter, uval); |
849abde9 | 792 | |
3ae9a34d ZH |
793 | print_noise(counter, avg); |
794 | ||
d73515c0 | 795 | print_running(avg_running, avg_enabled); |
4aa9015f | 796 | fprintf(output, "\n"); |
c04f5e5d IM |
797 | } |
798 | ||
f5b4a9c3 SE |
799 | /* |
800 | * Print out the results of a single counter: | |
801 | * does not use aggregated count in system-wide | |
802 | */ | |
13370a9b | 803 | static void print_counter(struct perf_evsel *counter, char *prefix) |
f5b4a9c3 | 804 | { |
5821522e | 805 | FILE *output = stat_config.output; |
f5b4a9c3 | 806 | u64 ena, run, val; |
410136f5 | 807 | double uval; |
f5b4a9c3 SE |
808 | int cpu; |
809 | ||
7ae92e74 | 810 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { |
a6fa0038 JO |
811 | val = perf_counts(counter->counts, cpu, 0)->val; |
812 | ena = perf_counts(counter->counts, cpu, 0)->ena; | |
813 | run = perf_counts(counter->counts, cpu, 0)->run; | |
13370a9b SE |
814 | |
815 | if (prefix) | |
816 | fprintf(output, "%s", prefix); | |
817 | ||
f5b4a9c3 | 818 | if (run == 0 || ena == 0) { |
410136f5 | 819 | fprintf(output, "CPU%*d%s%*s%s", |
d7470b6a | 820 | csv_output ? 0 : -4, |
7ae92e74 | 821 | perf_evsel__cpus(counter)->map[cpu], csv_sep, |
d7470b6a | 822 | csv_output ? 0 : 18, |
2cee77c4 | 823 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, |
410136f5 SE |
824 | csv_sep); |
825 | ||
826 | fprintf(output, "%-*s%s", | |
827 | csv_output ? 0 : unit_width, | |
828 | counter->unit, csv_sep); | |
829 | ||
830 | fprintf(output, "%*s", | |
831 | csv_output ? 0 : -25, | |
832 | perf_evsel__name(counter)); | |
f5b4a9c3 | 833 | |
023695d9 | 834 | if (counter->cgrp) |
4aa9015f SE |
835 | fprintf(output, "%s%s", |
836 | csv_sep, counter->cgrp->name); | |
023695d9 | 837 | |
d73515c0 | 838 | print_running(run, ena); |
4aa9015f | 839 | fputc('\n', output); |
f5b4a9c3 SE |
840 | continue; |
841 | } | |
842 | ||
410136f5 | 843 | uval = val * counter->scale; |
eedfcb4b | 844 | printout(cpu, 0, counter, uval); |
d73515c0 | 845 | if (!csv_output) |
d7470b6a | 846 | print_noise(counter, 1.0); |
d73515c0 | 847 | print_running(run, ena); |
f5b4a9c3 | 848 | |
4aa9015f | 849 | fputc('\n', output); |
f5b4a9c3 SE |
850 | } |
851 | } | |
852 | ||
d4f63a47 JO |
853 | static void print_interval(char *prefix, struct timespec *ts) |
854 | { | |
5821522e | 855 | FILE *output = stat_config.output; |
d4f63a47 JO |
856 | static int num_print_interval; |
857 | ||
858 | sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep); | |
859 | ||
860 | if (num_print_interval == 0 && !csv_output) { | |
421a50f3 | 861 | switch (stat_config.aggr_mode) { |
d4f63a47 JO |
862 | case AGGR_SOCKET: |
863 | fprintf(output, "# time socket cpus counts %*s events\n", unit_width, "unit"); | |
864 | break; | |
865 | case AGGR_CORE: | |
866 | fprintf(output, "# time core cpus counts %*s events\n", unit_width, "unit"); | |
867 | break; | |
868 | case AGGR_NONE: | |
869 | fprintf(output, "# time CPU counts %*s events\n", unit_width, "unit"); | |
870 | break; | |
32b8af82 JO |
871 | case AGGR_THREAD: |
872 | fprintf(output, "# time comm-pid counts %*s events\n", unit_width, "unit"); | |
873 | break; | |
d4f63a47 JO |
874 | case AGGR_GLOBAL: |
875 | default: | |
876 | fprintf(output, "# time counts %*s events\n", unit_width, "unit"); | |
208df99e JO |
877 | case AGGR_UNSET: |
878 | break; | |
d4f63a47 JO |
879 | } |
880 | } | |
881 | ||
882 | if (++num_print_interval == 25) | |
883 | num_print_interval = 0; | |
884 | } | |
885 | ||
886 | static void print_header(int argc, const char **argv) | |
42202dd5 | 887 | { |
5821522e | 888 | FILE *output = stat_config.output; |
69aad6f1 | 889 | int i; |
42202dd5 | 890 | |
ddcacfa0 IM |
891 | fflush(stdout); |
892 | ||
d7470b6a | 893 | if (!csv_output) { |
4aa9015f SE |
894 | fprintf(output, "\n"); |
895 | fprintf(output, " Performance counter stats for "); | |
62d3b617 DA |
896 | if (target.system_wide) |
897 | fprintf(output, "\'system wide"); | |
898 | else if (target.cpu_list) | |
899 | fprintf(output, "\'CPU(s) %s", target.cpu_list); | |
602ad878 | 900 | else if (!target__has_task(&target)) { |
4aa9015f | 901 | fprintf(output, "\'%s", argv[0]); |
d7470b6a | 902 | for (i = 1; i < argc; i++) |
4aa9015f | 903 | fprintf(output, " %s", argv[i]); |
20f946b4 NK |
904 | } else if (target.pid) |
905 | fprintf(output, "process id \'%s", target.pid); | |
d7470b6a | 906 | else |
20f946b4 | 907 | fprintf(output, "thread id \'%s", target.tid); |
44db76c8 | 908 | |
4aa9015f | 909 | fprintf(output, "\'"); |
d7470b6a | 910 | if (run_count > 1) |
4aa9015f SE |
911 | fprintf(output, " (%d runs)", run_count); |
912 | fprintf(output, ":\n\n"); | |
d7470b6a | 913 | } |
d4f63a47 JO |
914 | } |
915 | ||
916 | static void print_footer(void) | |
917 | { | |
5821522e JO |
918 | FILE *output = stat_config.output; |
919 | ||
d4f63a47 JO |
920 | if (!null_run) |
921 | fprintf(output, "\n"); | |
922 | fprintf(output, " %17.9f seconds time elapsed", | |
923 | avg_stats(&walltime_nsecs_stats)/1e9); | |
924 | if (run_count > 1) { | |
925 | fprintf(output, " "); | |
926 | print_noise_pct(stddev_stats(&walltime_nsecs_stats), | |
927 | avg_stats(&walltime_nsecs_stats)); | |
928 | } | |
929 | fprintf(output, "\n\n"); | |
930 | } | |
931 | ||
932 | static void print_counters(struct timespec *ts, int argc, const char **argv) | |
933 | { | |
ec0d3d1f | 934 | int interval = stat_config.interval; |
d4f63a47 JO |
935 | struct perf_evsel *counter; |
936 | char buf[64], *prefix = NULL; | |
937 | ||
938 | if (interval) | |
939 | print_interval(prefix = buf, ts); | |
940 | else | |
941 | print_header(argc, argv); | |
2996f5dd | 942 | |
421a50f3 | 943 | switch (stat_config.aggr_mode) { |
12c08a9f | 944 | case AGGR_CORE: |
86ee6e18 | 945 | case AGGR_SOCKET: |
d4f63a47 | 946 | print_aggr(prefix); |
86ee6e18 | 947 | break; |
32b8af82 JO |
948 | case AGGR_THREAD: |
949 | evlist__for_each(evsel_list, counter) | |
950 | print_aggr_thread(counter, prefix); | |
951 | break; | |
86ee6e18 | 952 | case AGGR_GLOBAL: |
0050f7aa | 953 | evlist__for_each(evsel_list, counter) |
d4f63a47 | 954 | print_counter_aggr(counter, prefix); |
86ee6e18 SE |
955 | break; |
956 | case AGGR_NONE: | |
0050f7aa | 957 | evlist__for_each(evsel_list, counter) |
d4f63a47 | 958 | print_counter(counter, prefix); |
86ee6e18 | 959 | break; |
208df99e | 960 | case AGGR_UNSET: |
86ee6e18 SE |
961 | default: |
962 | break; | |
f5b4a9c3 | 963 | } |
ddcacfa0 | 964 | |
d4f63a47 JO |
965 | if (!interval && !csv_output) |
966 | print_footer(); | |
967 | ||
5821522e | 968 | fflush(stat_config.output); |
ddcacfa0 IM |
969 | } |
970 | ||
f7b7c26e PZ |
971 | static volatile int signr = -1; |
972 | ||
5242519b | 973 | static void skip_signal(int signo) |
ddcacfa0 | 974 | { |
ec0d3d1f | 975 | if ((child_pid == -1) || stat_config.interval) |
60666c63 LW |
976 | done = 1; |
977 | ||
f7b7c26e | 978 | signr = signo; |
d07f0b12 SE |
979 | /* |
980 | * render child_pid harmless | |
981 | * won't send SIGTERM to a random | |
982 | * process in case of race condition | |
983 | * and fast PID recycling | |
984 | */ | |
985 | child_pid = -1; | |
f7b7c26e PZ |
986 | } |
987 | ||
988 | static void sig_atexit(void) | |
989 | { | |
d07f0b12 SE |
990 | sigset_t set, oset; |
991 | ||
992 | /* | |
993 | * avoid race condition with SIGCHLD handler | |
994 | * in skip_signal() which is modifying child_pid | |
995 | * goal is to avoid send SIGTERM to a random | |
996 | * process | |
997 | */ | |
998 | sigemptyset(&set); | |
999 | sigaddset(&set, SIGCHLD); | |
1000 | sigprocmask(SIG_BLOCK, &set, &oset); | |
1001 | ||
933da83a CW |
1002 | if (child_pid != -1) |
1003 | kill(child_pid, SIGTERM); | |
1004 | ||
d07f0b12 SE |
1005 | sigprocmask(SIG_SETMASK, &oset, NULL); |
1006 | ||
f7b7c26e PZ |
1007 | if (signr == -1) |
1008 | return; | |
1009 | ||
1010 | signal(signr, SIG_DFL); | |
1011 | kill(getpid(), signr); | |
5242519b IM |
1012 | } |
1013 | ||
1d037ca1 IT |
1014 | static int stat__set_big_num(const struct option *opt __maybe_unused, |
1015 | const char *s __maybe_unused, int unset) | |
d7470b6a SE |
1016 | { |
1017 | big_num_opt = unset ? 0 : 1; | |
1018 | return 0; | |
1019 | } | |
1020 | ||
e0547311 JO |
1021 | static const struct option stat_options[] = { |
1022 | OPT_BOOLEAN('T', "transaction", &transaction_run, | |
1023 | "hardware transaction statistics"), | |
1024 | OPT_CALLBACK('e', "event", &evsel_list, "event", | |
1025 | "event selector. use 'perf list' to list available events", | |
1026 | parse_events_option), | |
1027 | OPT_CALLBACK(0, "filter", &evsel_list, "filter", | |
1028 | "event filter", parse_filter), | |
1029 | OPT_BOOLEAN('i', "no-inherit", &no_inherit, | |
1030 | "child tasks do not inherit counters"), | |
1031 | OPT_STRING('p', "pid", &target.pid, "pid", | |
1032 | "stat events on existing process id"), | |
1033 | OPT_STRING('t', "tid", &target.tid, "tid", | |
1034 | "stat events on existing thread id"), | |
1035 | OPT_BOOLEAN('a', "all-cpus", &target.system_wide, | |
1036 | "system-wide collection from all CPUs"), | |
1037 | OPT_BOOLEAN('g', "group", &group, | |
1038 | "put the counters into a counter group"), | |
1039 | OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"), | |
1040 | OPT_INCR('v', "verbose", &verbose, | |
1041 | "be more verbose (show counter open errors, etc)"), | |
1042 | OPT_INTEGER('r', "repeat", &run_count, | |
1043 | "repeat command and print average + stddev (max: 100, forever: 0)"), | |
1044 | OPT_BOOLEAN('n', "null", &null_run, | |
1045 | "null run - dont start any counters"), | |
1046 | OPT_INCR('d', "detailed", &detailed_run, | |
1047 | "detailed run - start a lot of events"), | |
1048 | OPT_BOOLEAN('S', "sync", &sync_run, | |
1049 | "call sync() before starting a run"), | |
1050 | OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL, | |
1051 | "print large numbers with thousands\' separators", | |
1052 | stat__set_big_num), | |
1053 | OPT_STRING('C', "cpu", &target.cpu_list, "cpu", | |
1054 | "list of cpus to monitor in system-wide"), | |
1055 | OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode, | |
1056 | "disable CPU count aggregation", AGGR_NONE), | |
1057 | OPT_STRING('x', "field-separator", &csv_sep, "separator", | |
1058 | "print counts with custom separator"), | |
1059 | OPT_CALLBACK('G', "cgroup", &evsel_list, "name", | |
1060 | "monitor event in cgroup name only", parse_cgroups), | |
1061 | OPT_STRING('o', "output", &output_name, "file", "output file name"), | |
1062 | OPT_BOOLEAN(0, "append", &append_file, "append to the output file"), | |
1063 | OPT_INTEGER(0, "log-fd", &output_fd, | |
1064 | "log output to fd, instead of stderr"), | |
1065 | OPT_STRING(0, "pre", &pre_cmd, "command", | |
1066 | "command to run prior to the measured command"), | |
1067 | OPT_STRING(0, "post", &post_cmd, "command", | |
1068 | "command to run after to the measured command"), | |
1069 | OPT_UINTEGER('I', "interval-print", &stat_config.interval, | |
1070 | "print counts at regular interval in ms (>= 10)"), | |
1071 | OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode, | |
1072 | "aggregate counts per processor socket", AGGR_SOCKET), | |
1073 | OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode, | |
1074 | "aggregate counts per physical processor core", AGGR_CORE), | |
1075 | OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode, | |
1076 | "aggregate counts per thread", AGGR_THREAD), | |
1077 | OPT_UINTEGER('D', "delay", &initial_delay, | |
1078 | "ms to wait before starting measurement after program start"), | |
1079 | OPT_END() | |
1080 | }; | |
1081 | ||
1fe7a300 JO |
1082 | static int perf_stat__get_socket(struct cpu_map *map, int cpu) |
1083 | { | |
1084 | return cpu_map__get_socket(map, cpu, NULL); | |
1085 | } | |
1086 | ||
1087 | static int perf_stat__get_core(struct cpu_map *map, int cpu) | |
1088 | { | |
1089 | return cpu_map__get_core(map, cpu, NULL); | |
1090 | } | |
1091 | ||
1e5a2931 JO |
1092 | static int cpu_map__get_max(struct cpu_map *map) |
1093 | { | |
1094 | int i, max = -1; | |
1095 | ||
1096 | for (i = 0; i < map->nr; i++) { | |
1097 | if (map->map[i] > max) | |
1098 | max = map->map[i]; | |
1099 | } | |
1100 | ||
1101 | return max; | |
1102 | } | |
1103 | ||
1104 | static struct cpu_map *cpus_aggr_map; | |
1105 | ||
1106 | static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx) | |
1107 | { | |
1108 | int cpu; | |
1109 | ||
1110 | if (idx >= map->nr) | |
1111 | return -1; | |
1112 | ||
1113 | cpu = map->map[idx]; | |
1114 | ||
1115 | if (cpus_aggr_map->map[cpu] == -1) | |
1116 | cpus_aggr_map->map[cpu] = get_id(map, idx); | |
1117 | ||
1118 | return cpus_aggr_map->map[cpu]; | |
1119 | } | |
1120 | ||
1121 | static int perf_stat__get_socket_cached(struct cpu_map *map, int idx) | |
1122 | { | |
1123 | return perf_stat__get_aggr(perf_stat__get_socket, map, idx); | |
1124 | } | |
1125 | ||
1126 | static int perf_stat__get_core_cached(struct cpu_map *map, int idx) | |
1127 | { | |
1128 | return perf_stat__get_aggr(perf_stat__get_core, map, idx); | |
1129 | } | |
1130 | ||
86ee6e18 SE |
1131 | static int perf_stat_init_aggr_mode(void) |
1132 | { | |
1e5a2931 JO |
1133 | int nr; |
1134 | ||
421a50f3 | 1135 | switch (stat_config.aggr_mode) { |
86ee6e18 SE |
1136 | case AGGR_SOCKET: |
1137 | if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) { | |
1138 | perror("cannot build socket map"); | |
1139 | return -1; | |
1140 | } | |
1e5a2931 | 1141 | aggr_get_id = perf_stat__get_socket_cached; |
86ee6e18 | 1142 | break; |
12c08a9f SE |
1143 | case AGGR_CORE: |
1144 | if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) { | |
1145 | perror("cannot build core map"); | |
1146 | return -1; | |
1147 | } | |
1e5a2931 | 1148 | aggr_get_id = perf_stat__get_core_cached; |
12c08a9f | 1149 | break; |
86ee6e18 SE |
1150 | case AGGR_NONE: |
1151 | case AGGR_GLOBAL: | |
32b8af82 | 1152 | case AGGR_THREAD: |
208df99e | 1153 | case AGGR_UNSET: |
86ee6e18 SE |
1154 | default: |
1155 | break; | |
1156 | } | |
1e5a2931 JO |
1157 | |
1158 | /* | |
1159 | * The evsel_list->cpus is the base we operate on, | |
1160 | * taking the highest cpu number to be the size of | |
1161 | * the aggregation translate cpumap. | |
1162 | */ | |
1163 | nr = cpu_map__get_max(evsel_list->cpus); | |
1164 | cpus_aggr_map = cpu_map__empty_new(nr + 1); | |
1165 | return cpus_aggr_map ? 0 : -ENOMEM; | |
86ee6e18 SE |
1166 | } |
1167 | ||
544c2ae7 MH |
1168 | static void perf_stat__exit_aggr_mode(void) |
1169 | { | |
1170 | cpu_map__put(aggr_map); | |
1171 | cpu_map__put(cpus_aggr_map); | |
1172 | aggr_map = NULL; | |
1173 | cpus_aggr_map = NULL; | |
1174 | } | |
1175 | ||
2cba3ffb IM |
1176 | /* |
1177 | * Add default attributes, if there were no attributes specified or | |
1178 | * if -d/--detailed, -d -d or -d -d -d is used: | |
1179 | */ | |
1180 | static int add_default_attributes(void) | |
1181 | { | |
b070a547 ACM |
1182 | struct perf_event_attr default_attrs[] = { |
1183 | ||
1184 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK }, | |
1185 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES }, | |
1186 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS }, | |
1187 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS }, | |
1188 | ||
1189 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES }, | |
1190 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND }, | |
1191 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND }, | |
1192 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS }, | |
1193 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, | |
1194 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES }, | |
1195 | ||
1196 | }; | |
1197 | ||
1198 | /* | |
1199 | * Detailed stats (-d), covering the L1 and last level data caches: | |
1200 | */ | |
1201 | struct perf_event_attr detailed_attrs[] = { | |
1202 | ||
1203 | { .type = PERF_TYPE_HW_CACHE, | |
1204 | .config = | |
1205 | PERF_COUNT_HW_CACHE_L1D << 0 | | |
1206 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1207 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
1208 | ||
1209 | { .type = PERF_TYPE_HW_CACHE, | |
1210 | .config = | |
1211 | PERF_COUNT_HW_CACHE_L1D << 0 | | |
1212 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1213 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
1214 | ||
1215 | { .type = PERF_TYPE_HW_CACHE, | |
1216 | .config = | |
1217 | PERF_COUNT_HW_CACHE_LL << 0 | | |
1218 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1219 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
1220 | ||
1221 | { .type = PERF_TYPE_HW_CACHE, | |
1222 | .config = | |
1223 | PERF_COUNT_HW_CACHE_LL << 0 | | |
1224 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1225 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
1226 | }; | |
1227 | ||
1228 | /* | |
1229 | * Very detailed stats (-d -d), covering the instruction cache and the TLB caches: | |
1230 | */ | |
1231 | struct perf_event_attr very_detailed_attrs[] = { | |
1232 | ||
1233 | { .type = PERF_TYPE_HW_CACHE, | |
1234 | .config = | |
1235 | PERF_COUNT_HW_CACHE_L1I << 0 | | |
1236 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1237 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
1238 | ||
1239 | { .type = PERF_TYPE_HW_CACHE, | |
1240 | .config = | |
1241 | PERF_COUNT_HW_CACHE_L1I << 0 | | |
1242 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1243 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
1244 | ||
1245 | { .type = PERF_TYPE_HW_CACHE, | |
1246 | .config = | |
1247 | PERF_COUNT_HW_CACHE_DTLB << 0 | | |
1248 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1249 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
1250 | ||
1251 | { .type = PERF_TYPE_HW_CACHE, | |
1252 | .config = | |
1253 | PERF_COUNT_HW_CACHE_DTLB << 0 | | |
1254 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1255 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
1256 | ||
1257 | { .type = PERF_TYPE_HW_CACHE, | |
1258 | .config = | |
1259 | PERF_COUNT_HW_CACHE_ITLB << 0 | | |
1260 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1261 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
1262 | ||
1263 | { .type = PERF_TYPE_HW_CACHE, | |
1264 | .config = | |
1265 | PERF_COUNT_HW_CACHE_ITLB << 0 | | |
1266 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
1267 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
1268 | ||
1269 | }; | |
1270 | ||
1271 | /* | |
1272 | * Very, very detailed stats (-d -d -d), adding prefetch events: | |
1273 | */ | |
1274 | struct perf_event_attr very_very_detailed_attrs[] = { | |
1275 | ||
1276 | { .type = PERF_TYPE_HW_CACHE, | |
1277 | .config = | |
1278 | PERF_COUNT_HW_CACHE_L1D << 0 | | |
1279 | (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) | | |
1280 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
1281 | ||
1282 | { .type = PERF_TYPE_HW_CACHE, | |
1283 | .config = | |
1284 | PERF_COUNT_HW_CACHE_L1D << 0 | | |
1285 | (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) | | |
1286 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
1287 | }; | |
1288 | ||
2cba3ffb IM |
1289 | /* Set attrs if no event is selected and !null_run: */ |
1290 | if (null_run) | |
1291 | return 0; | |
1292 | ||
4cabc3d1 AK |
1293 | if (transaction_run) { |
1294 | int err; | |
1295 | if (pmu_have_event("cpu", "cycles-ct") && | |
1296 | pmu_have_event("cpu", "el-start")) | |
a454742c | 1297 | err = parse_events(evsel_list, transaction_attrs, NULL); |
4cabc3d1 | 1298 | else |
a454742c JO |
1299 | err = parse_events(evsel_list, transaction_limited_attrs, NULL); |
1300 | if (err) { | |
4cabc3d1 AK |
1301 | fprintf(stderr, "Cannot set up transaction events\n"); |
1302 | return -1; | |
1303 | } | |
1304 | return 0; | |
1305 | } | |
1306 | ||
2cba3ffb | 1307 | if (!evsel_list->nr_entries) { |
79695e1b | 1308 | if (perf_evlist__add_default_attrs(evsel_list, default_attrs) < 0) |
50d08e47 | 1309 | return -1; |
2cba3ffb IM |
1310 | } |
1311 | ||
1312 | /* Detailed events get appended to the event list: */ | |
1313 | ||
1314 | if (detailed_run < 1) | |
1315 | return 0; | |
1316 | ||
1317 | /* Append detailed run extra attributes: */ | |
79695e1b | 1318 | if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0) |
50d08e47 | 1319 | return -1; |
2cba3ffb IM |
1320 | |
1321 | if (detailed_run < 2) | |
1322 | return 0; | |
1323 | ||
1324 | /* Append very detailed run extra attributes: */ | |
79695e1b | 1325 | if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0) |
50d08e47 | 1326 | return -1; |
2cba3ffb IM |
1327 | |
1328 | if (detailed_run < 3) | |
1329 | return 0; | |
1330 | ||
1331 | /* Append very, very detailed run extra attributes: */ | |
79695e1b | 1332 | return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs); |
2cba3ffb IM |
1333 | } |
1334 | ||
4979d0c7 JO |
1335 | static const char * const recort_usage[] = { |
1336 | "perf stat record [<options>]", | |
1337 | NULL, | |
1338 | }; | |
1339 | ||
3ba78bd0 JO |
1340 | static void init_features(struct perf_session *session) |
1341 | { | |
1342 | int feat; | |
1343 | ||
1344 | for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) | |
1345 | perf_header__set_feat(&session->header, feat); | |
1346 | ||
1347 | perf_header__clear_feat(&session->header, HEADER_BUILD_ID); | |
1348 | perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); | |
1349 | perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); | |
1350 | perf_header__clear_feat(&session->header, HEADER_AUXTRACE); | |
1351 | } | |
1352 | ||
4979d0c7 JO |
1353 | static int __cmd_record(int argc, const char **argv) |
1354 | { | |
1355 | struct perf_session *session; | |
1356 | struct perf_data_file *file = &perf_stat.file; | |
1357 | ||
1358 | argc = parse_options(argc, argv, stat_options, record_usage, | |
1359 | PARSE_OPT_STOP_AT_NON_OPTION); | |
1360 | ||
1361 | if (output_name) | |
1362 | file->path = output_name; | |
1363 | ||
1364 | session = perf_session__new(file, false, NULL); | |
1365 | if (session == NULL) { | |
1366 | pr_err("Perf session creation failed.\n"); | |
1367 | return -1; | |
1368 | } | |
1369 | ||
1370 | /* No pipe support ATM */ | |
1371 | if (perf_stat.file.is_pipe) | |
1372 | return -EINVAL; | |
1373 | ||
3ba78bd0 JO |
1374 | init_features(session); |
1375 | ||
4979d0c7 JO |
1376 | session->evlist = evsel_list; |
1377 | perf_stat.session = session; | |
1378 | perf_stat.record = true; | |
1379 | return argc; | |
1380 | } | |
1381 | ||
1d037ca1 | 1382 | int cmd_stat(int argc, const char **argv, const char *prefix __maybe_unused) |
5242519b | 1383 | { |
b070a547 ACM |
1384 | const char * const stat_usage[] = { |
1385 | "perf stat [<options>] [<command>]", | |
1386 | NULL | |
1387 | }; | |
cc03c542 | 1388 | int status = -EINVAL, run_idx; |
4aa9015f | 1389 | const char *mode; |
5821522e | 1390 | FILE *output = stderr; |
ec0d3d1f | 1391 | unsigned int interval; |
4979d0c7 | 1392 | const char * const stat_subcommands[] = { "record" }; |
42202dd5 | 1393 | |
5af52b51 SE |
1394 | setlocale(LC_ALL, ""); |
1395 | ||
334fe7a3 | 1396 | evsel_list = perf_evlist__new(); |
361c99a6 ACM |
1397 | if (evsel_list == NULL) |
1398 | return -ENOMEM; | |
1399 | ||
4979d0c7 JO |
1400 | argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands, |
1401 | (const char **) stat_usage, | |
1402 | PARSE_OPT_STOP_AT_NON_OPTION); | |
1403 | ||
1404 | if (argc && !strncmp(argv[0], "rec", 3)) { | |
1405 | argc = __cmd_record(argc, argv); | |
1406 | if (argc < 0) | |
1407 | return -1; | |
1408 | } | |
d7470b6a | 1409 | |
ec0d3d1f JO |
1410 | interval = stat_config.interval; |
1411 | ||
4979d0c7 JO |
1412 | /* |
1413 | * For record command the -o is already taken care of. | |
1414 | */ | |
1415 | if (!STAT_RECORD && output_name && strcmp(output_name, "-")) | |
4aa9015f SE |
1416 | output = NULL; |
1417 | ||
56f3bae7 JC |
1418 | if (output_name && output_fd) { |
1419 | fprintf(stderr, "cannot use both --output and --log-fd\n"); | |
e0547311 JO |
1420 | parse_options_usage(stat_usage, stat_options, "o", 1); |
1421 | parse_options_usage(NULL, stat_options, "log-fd", 0); | |
cc03c542 | 1422 | goto out; |
56f3bae7 | 1423 | } |
fc3e4d07 SE |
1424 | |
1425 | if (output_fd < 0) { | |
1426 | fprintf(stderr, "argument to --log-fd must be a > 0\n"); | |
e0547311 | 1427 | parse_options_usage(stat_usage, stat_options, "log-fd", 0); |
cc03c542 | 1428 | goto out; |
fc3e4d07 SE |
1429 | } |
1430 | ||
4aa9015f SE |
1431 | if (!output) { |
1432 | struct timespec tm; | |
1433 | mode = append_file ? "a" : "w"; | |
1434 | ||
1435 | output = fopen(output_name, mode); | |
1436 | if (!output) { | |
1437 | perror("failed to create output file"); | |
fceda7fe | 1438 | return -1; |
4aa9015f SE |
1439 | } |
1440 | clock_gettime(CLOCK_REALTIME, &tm); | |
1441 | fprintf(output, "# started on %s\n", ctime(&tm.tv_sec)); | |
fc3e4d07 | 1442 | } else if (output_fd > 0) { |
56f3bae7 JC |
1443 | mode = append_file ? "a" : "w"; |
1444 | output = fdopen(output_fd, mode); | |
1445 | if (!output) { | |
1446 | perror("Failed opening logfd"); | |
1447 | return -errno; | |
1448 | } | |
4aa9015f SE |
1449 | } |
1450 | ||
5821522e JO |
1451 | stat_config.output = output; |
1452 | ||
d4ffd04d | 1453 | if (csv_sep) { |
d7470b6a | 1454 | csv_output = true; |
d4ffd04d JC |
1455 | if (!strcmp(csv_sep, "\\t")) |
1456 | csv_sep = "\t"; | |
1457 | } else | |
d7470b6a SE |
1458 | csv_sep = DEFAULT_SEPARATOR; |
1459 | ||
1460 | /* | |
1461 | * let the spreadsheet do the pretty-printing | |
1462 | */ | |
1463 | if (csv_output) { | |
61a9f324 | 1464 | /* User explicitly passed -B? */ |
d7470b6a SE |
1465 | if (big_num_opt == 1) { |
1466 | fprintf(stderr, "-B option not supported with -x\n"); | |
e0547311 JO |
1467 | parse_options_usage(stat_usage, stat_options, "B", 1); |
1468 | parse_options_usage(NULL, stat_options, "x", 1); | |
cc03c542 | 1469 | goto out; |
d7470b6a SE |
1470 | } else /* Nope, so disable big number formatting */ |
1471 | big_num = false; | |
1472 | } else if (big_num_opt == 0) /* User passed --no-big-num */ | |
1473 | big_num = false; | |
1474 | ||
602ad878 | 1475 | if (!argc && target__none(&target)) |
e0547311 | 1476 | usage_with_options(stat_usage, stat_options); |
ac3063bd | 1477 | |
a7e191c3 | 1478 | if (run_count < 0) { |
cc03c542 | 1479 | pr_err("Run count must be a positive number\n"); |
e0547311 | 1480 | parse_options_usage(stat_usage, stat_options, "r", 1); |
cc03c542 | 1481 | goto out; |
a7e191c3 FD |
1482 | } else if (run_count == 0) { |
1483 | forever = true; | |
1484 | run_count = 1; | |
1485 | } | |
ddcacfa0 | 1486 | |
421a50f3 | 1487 | if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) { |
32b8af82 JO |
1488 | fprintf(stderr, "The --per-thread option is only available " |
1489 | "when monitoring via -p -t options.\n"); | |
e0547311 JO |
1490 | parse_options_usage(NULL, stat_options, "p", 1); |
1491 | parse_options_usage(NULL, stat_options, "t", 1); | |
32b8af82 JO |
1492 | goto out; |
1493 | } | |
1494 | ||
1495 | /* | |
1496 | * no_aggr, cgroup are for system-wide only | |
1497 | * --per-thread is aggregated per thread, we dont mix it with cpu mode | |
1498 | */ | |
421a50f3 JO |
1499 | if (((stat_config.aggr_mode != AGGR_GLOBAL && |
1500 | stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) && | |
602ad878 | 1501 | !target__has_cpu(&target)) { |
023695d9 SE |
1502 | fprintf(stderr, "both cgroup and no-aggregation " |
1503 | "modes only available in system-wide mode\n"); | |
1504 | ||
e0547311 JO |
1505 | parse_options_usage(stat_usage, stat_options, "G", 1); |
1506 | parse_options_usage(NULL, stat_options, "A", 1); | |
1507 | parse_options_usage(NULL, stat_options, "a", 1); | |
cc03c542 | 1508 | goto out; |
d7e7a451 SE |
1509 | } |
1510 | ||
2cba3ffb IM |
1511 | if (add_default_attributes()) |
1512 | goto out; | |
ddcacfa0 | 1513 | |
602ad878 | 1514 | target__validate(&target); |
5c98d466 | 1515 | |
77a6f014 | 1516 | if (perf_evlist__create_maps(evsel_list, &target) < 0) { |
602ad878 | 1517 | if (target__has_task(&target)) { |
77a6f014 | 1518 | pr_err("Problems finding threads of monitor\n"); |
e0547311 JO |
1519 | parse_options_usage(stat_usage, stat_options, "p", 1); |
1520 | parse_options_usage(NULL, stat_options, "t", 1); | |
602ad878 | 1521 | } else if (target__has_cpu(&target)) { |
77a6f014 | 1522 | perror("failed to parse CPUs map"); |
e0547311 JO |
1523 | parse_options_usage(stat_usage, stat_options, "C", 1); |
1524 | parse_options_usage(NULL, stat_options, "a", 1); | |
cc03c542 NK |
1525 | } |
1526 | goto out; | |
60d567e2 | 1527 | } |
32b8af82 JO |
1528 | |
1529 | /* | |
1530 | * Initialize thread_map with comm names, | |
1531 | * so we could print it out on output. | |
1532 | */ | |
421a50f3 | 1533 | if (stat_config.aggr_mode == AGGR_THREAD) |
32b8af82 JO |
1534 | thread_map__read_comms(evsel_list->threads); |
1535 | ||
13370a9b | 1536 | if (interval && interval < 100) { |
19afd104 KL |
1537 | if (interval < 10) { |
1538 | pr_err("print interval must be >= 10ms\n"); | |
e0547311 | 1539 | parse_options_usage(stat_usage, stat_options, "I", 1); |
19afd104 KL |
1540 | goto out; |
1541 | } else | |
1542 | pr_warning("print interval < 100ms. " | |
1543 | "The overhead percentage could be high in some cases. " | |
1544 | "Please proceed with caution.\n"); | |
13370a9b | 1545 | } |
c45c6ea2 | 1546 | |
d134ffb9 | 1547 | if (perf_evlist__alloc_stats(evsel_list, interval)) |
03ad9747 | 1548 | goto out; |
d6d901c2 | 1549 | |
86ee6e18 | 1550 | if (perf_stat_init_aggr_mode()) |
03ad9747 | 1551 | goto out; |
86ee6e18 | 1552 | |
58d7e993 IM |
1553 | /* |
1554 | * We dont want to block the signals - that would cause | |
1555 | * child tasks to inherit that and Ctrl-C would not work. | |
1556 | * What we want is for Ctrl-C to work in the exec()-ed | |
1557 | * task, but being ignored by perf stat itself: | |
1558 | */ | |
f7b7c26e | 1559 | atexit(sig_atexit); |
a7e191c3 FD |
1560 | if (!forever) |
1561 | signal(SIGINT, skip_signal); | |
13370a9b | 1562 | signal(SIGCHLD, skip_signal); |
58d7e993 IM |
1563 | signal(SIGALRM, skip_signal); |
1564 | signal(SIGABRT, skip_signal); | |
1565 | ||
42202dd5 | 1566 | status = 0; |
a7e191c3 | 1567 | for (run_idx = 0; forever || run_idx < run_count; run_idx++) { |
42202dd5 | 1568 | if (run_count != 1 && verbose) |
4aa9015f SE |
1569 | fprintf(output, "[ perf stat: executing run #%d ... ]\n", |
1570 | run_idx + 1); | |
f9cef0a9 | 1571 | |
42202dd5 | 1572 | status = run_perf_stat(argc, argv); |
a7e191c3 | 1573 | if (forever && status != -1) { |
d4f63a47 | 1574 | print_counters(NULL, argc, argv); |
254ecbc7 | 1575 | perf_stat__reset_stats(); |
a7e191c3 | 1576 | } |
42202dd5 IM |
1577 | } |
1578 | ||
a7e191c3 | 1579 | if (!forever && status != -1 && !interval) |
d4f63a47 | 1580 | print_counters(NULL, argc, argv); |
d134ffb9 | 1581 | |
4979d0c7 JO |
1582 | if (STAT_RECORD) { |
1583 | /* | |
1584 | * We synthesize the kernel mmap record just so that older tools | |
1585 | * don't emit warnings about not being able to resolve symbols | |
1586 | * due to /proc/sys/kernel/kptr_restrict settings and instear provide | |
1587 | * a saner message about no samples being in the perf.data file. | |
1588 | * | |
1589 | * This also serves to suppress a warning about f_header.data.size == 0 | |
8b99b1a4 JO |
1590 | * in header.c at the moment 'perf stat record' gets introduced, which |
1591 | * is not really needed once we start adding the stat specific PERF_RECORD_ | |
1592 | * records, but the need to suppress the kptr_restrict messages in older | |
1593 | * tools remain -acme | |
4979d0c7 JO |
1594 | */ |
1595 | int fd = perf_data_file__fd(&perf_stat.file); | |
1596 | int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat, | |
1597 | process_synthesized_event, | |
1598 | &perf_stat.session->machines.host); | |
1599 | if (err) { | |
1600 | pr_warning("Couldn't synthesize the kernel mmap record, harmless, " | |
1601 | "older tools may produce warnings about this file\n."); | |
1602 | } | |
1603 | ||
1604 | perf_stat.session->header.data_size += perf_stat.bytes_written; | |
1605 | perf_session__write_header(perf_stat.session, evsel_list, fd, true); | |
1606 | ||
1607 | perf_session__delete(perf_stat.session); | |
1608 | } | |
1609 | ||
544c2ae7 | 1610 | perf_stat__exit_aggr_mode(); |
d134ffb9 | 1611 | perf_evlist__free_stats(evsel_list); |
0015e2e1 ACM |
1612 | out: |
1613 | perf_evlist__delete(evsel_list); | |
42202dd5 | 1614 | return status; |
ddcacfa0 | 1615 | } |