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