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" |
5d8bb1ec | 55 | #include "util/drv_configs.h" |
a5d243d0 | 56 | #include "util/color.h" |
0007ecea | 57 | #include "util/stat.h" |
60666c63 | 58 | #include "util/header.h" |
a12b51c4 | 59 | #include "util/cpumap.h" |
d6d901c2 | 60 | #include "util/thread.h" |
fd78260b | 61 | #include "util/thread_map.h" |
d809560b | 62 | #include "util/counts.h" |
44b1e60a | 63 | #include "util/group.h" |
4979d0c7 | 64 | #include "util/session.h" |
ba6039b6 | 65 | #include "util/tool.h" |
44b1e60a | 66 | #include "util/group.h" |
a067558e | 67 | #include "util/string2.h" |
ba6039b6 | 68 | #include "asm/bug.h" |
ddcacfa0 | 69 | |
bd48c63e | 70 | #include <linux/time64.h> |
44b1e60a | 71 | #include <api/fs/fs.h> |
a43783ae | 72 | #include <errno.h> |
9607ad3a | 73 | #include <signal.h> |
1f16c575 | 74 | #include <stdlib.h> |
ddcacfa0 | 75 | #include <sys/prctl.h> |
fd20e811 | 76 | #include <inttypes.h> |
5af52b51 | 77 | #include <locale.h> |
e3b03b6c | 78 | #include <math.h> |
16c8a109 | 79 | |
3d689ed6 ACM |
80 | #include "sane_ctype.h" |
81 | ||
d7470b6a | 82 | #define DEFAULT_SEPARATOR " " |
2cee77c4 DA |
83 | #define CNTR_NOT_SUPPORTED "<not supported>" |
84 | #define CNTR_NOT_COUNTED "<not counted>" | |
d7470b6a | 85 | |
d4f63a47 | 86 | static void print_counters(struct timespec *ts, int argc, const char **argv); |
13370a9b | 87 | |
4cabc3d1 | 88 | /* Default events used for perf stat -T */ |
a454742c JO |
89 | static const char *transaction_attrs = { |
90 | "task-clock," | |
4cabc3d1 AK |
91 | "{" |
92 | "instructions," | |
93 | "cycles," | |
94 | "cpu/cycles-t/," | |
95 | "cpu/tx-start/," | |
96 | "cpu/el-start/," | |
97 | "cpu/cycles-ct/" | |
98 | "}" | |
99 | }; | |
100 | ||
101 | /* More limited version when the CPU does not have all events. */ | |
a454742c JO |
102 | static const char * transaction_limited_attrs = { |
103 | "task-clock," | |
4cabc3d1 AK |
104 | "{" |
105 | "instructions," | |
106 | "cycles," | |
107 | "cpu/cycles-t/," | |
108 | "cpu/tx-start/" | |
109 | "}" | |
110 | }; | |
111 | ||
44b1e60a AK |
112 | static const char * topdown_attrs[] = { |
113 | "topdown-total-slots", | |
114 | "topdown-slots-retired", | |
115 | "topdown-recovery-bubbles", | |
116 | "topdown-fetch-bubbles", | |
117 | "topdown-slots-issued", | |
118 | NULL, | |
119 | }; | |
120 | ||
666e6d48 | 121 | static struct perf_evlist *evsel_list; |
361c99a6 | 122 | |
602ad878 | 123 | static struct target target = { |
77a6f014 NK |
124 | .uid = UINT_MAX, |
125 | }; | |
ddcacfa0 | 126 | |
1e5a2931 JO |
127 | typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu); |
128 | ||
3d632595 | 129 | static int run_count = 1; |
2e6cdf99 | 130 | static bool no_inherit = false; |
d07f0b12 | 131 | static volatile pid_t child_pid = -1; |
c0555642 | 132 | static bool null_run = false; |
2cba3ffb | 133 | static int detailed_run = 0; |
4cabc3d1 | 134 | static bool transaction_run; |
44b1e60a | 135 | static bool topdown_run = false; |
201e0b06 | 136 | static bool big_num = true; |
d7470b6a | 137 | static int big_num_opt = -1; |
d7470b6a SE |
138 | static const char *csv_sep = NULL; |
139 | static bool csv_output = false; | |
43bece79 | 140 | static bool group = false; |
1f16c575 PZ |
141 | static const char *pre_cmd = NULL; |
142 | static const char *post_cmd = NULL; | |
143 | static bool sync_run = false; | |
41191688 | 144 | static unsigned int initial_delay = 0; |
410136f5 | 145 | static unsigned int unit_width = 4; /* strlen("unit") */ |
a7e191c3 | 146 | static bool forever = false; |
54b50916 | 147 | static bool metric_only = false; |
44b1e60a | 148 | static bool force_metric_only = false; |
430daf2d | 149 | static bool no_merge = false; |
13370a9b | 150 | static struct timespec ref_time; |
86ee6e18 | 151 | static struct cpu_map *aggr_map; |
1e5a2931 | 152 | static aggr_get_id_t aggr_get_id; |
e0547311 JO |
153 | static bool append_file; |
154 | static const char *output_name; | |
155 | static int output_fd; | |
02d492e5 | 156 | static int print_free_counters_hint; |
5af52b51 | 157 | |
4979d0c7 JO |
158 | struct perf_stat { |
159 | bool record; | |
160 | struct perf_data_file file; | |
161 | struct perf_session *session; | |
162 | u64 bytes_written; | |
ba6039b6 | 163 | struct perf_tool tool; |
1975d36e JO |
164 | bool maps_allocated; |
165 | struct cpu_map *cpus; | |
166 | struct thread_map *threads; | |
89af4e05 | 167 | enum aggr_mode aggr_mode; |
4979d0c7 JO |
168 | }; |
169 | ||
170 | static struct perf_stat perf_stat; | |
171 | #define STAT_RECORD perf_stat.record | |
172 | ||
60666c63 LW |
173 | static volatile int done = 0; |
174 | ||
421a50f3 JO |
175 | static struct perf_stat_config stat_config = { |
176 | .aggr_mode = AGGR_GLOBAL, | |
711a572e | 177 | .scale = true, |
421a50f3 JO |
178 | }; |
179 | ||
13370a9b SE |
180 | static inline void diff_timespec(struct timespec *r, struct timespec *a, |
181 | struct timespec *b) | |
182 | { | |
183 | r->tv_sec = a->tv_sec - b->tv_sec; | |
184 | if (a->tv_nsec < b->tv_nsec) { | |
310ebb93 | 185 | r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec; |
13370a9b SE |
186 | r->tv_sec--; |
187 | } else { | |
188 | r->tv_nsec = a->tv_nsec - b->tv_nsec ; | |
189 | } | |
190 | } | |
191 | ||
254ecbc7 JO |
192 | static void perf_stat__reset_stats(void) |
193 | { | |
194 | perf_evlist__reset_stats(evsel_list); | |
f87027b9 | 195 | perf_stat__reset_shadow_stats(); |
1eda3b21 JO |
196 | } |
197 | ||
cac21425 | 198 | static int create_perf_stat_counter(struct perf_evsel *evsel) |
ddcacfa0 | 199 | { |
69aad6f1 | 200 | struct perf_event_attr *attr = &evsel->attr; |
727ab04e | 201 | |
711a572e | 202 | if (stat_config.scale) |
a21ca2ca IM |
203 | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | |
204 | PERF_FORMAT_TOTAL_TIME_RUNNING; | |
ddcacfa0 | 205 | |
5d2cd909 ACM |
206 | attr->inherit = !no_inherit; |
207 | ||
6acd8e92 JO |
208 | /* |
209 | * Some events get initialized with sample_(period/type) set, | |
210 | * like tracepoints. Clear it up for counting. | |
211 | */ | |
212 | attr->sample_period = 0; | |
6db1a5c1 | 213 | |
4979d0c7 JO |
214 | /* |
215 | * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless | |
216 | * while avoiding that older tools show confusing messages. | |
6db1a5c1 JO |
217 | * |
218 | * However for pipe sessions we need to keep it zero, | |
219 | * because script's perf_evsel__check_attr is triggered | |
220 | * by attr->sample_type != 0, and we can't run it on | |
221 | * stat sessions. | |
4979d0c7 | 222 | */ |
6db1a5c1 JO |
223 | if (!(STAT_RECORD && perf_stat.file.is_pipe)) |
224 | attr->sample_type = PERF_SAMPLE_IDENTIFIER; | |
6acd8e92 | 225 | |
67ccdecd JO |
226 | /* |
227 | * Disabling all counters initially, they will be enabled | |
228 | * either manually by us or by kernel via enable_on_exec | |
229 | * set later. | |
230 | */ | |
c8280cec | 231 | if (perf_evsel__is_group_leader(evsel)) { |
67ccdecd JO |
232 | attr->disabled = 1; |
233 | ||
c8280cec JO |
234 | /* |
235 | * In case of initial_delay we enable tracee | |
236 | * events manually. | |
237 | */ | |
238 | if (target__none(&target) && !initial_delay) | |
41191688 | 239 | attr->enable_on_exec = 1; |
ddcacfa0 | 240 | } |
084ab9f8 | 241 | |
c8280cec JO |
242 | if (target__has_cpu(&target)) |
243 | return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel)); | |
244 | ||
594ac61a | 245 | return perf_evsel__open_per_thread(evsel, evsel_list->threads); |
ddcacfa0 IM |
246 | } |
247 | ||
c04f5e5d IM |
248 | /* |
249 | * Does the counter have nsecs as a unit? | |
250 | */ | |
daec78a0 | 251 | static inline int nsec_counter(struct perf_evsel *evsel) |
c04f5e5d | 252 | { |
daec78a0 ACM |
253 | if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) || |
254 | perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK)) | |
c04f5e5d IM |
255 | return 1; |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
8b99b1a4 JO |
260 | static int process_synthesized_event(struct perf_tool *tool __maybe_unused, |
261 | union perf_event *event, | |
262 | struct perf_sample *sample __maybe_unused, | |
263 | struct machine *machine __maybe_unused) | |
4979d0c7 | 264 | { |
8b99b1a4 | 265 | if (perf_data_file__write(&perf_stat.file, event, event->header.size) < 0) { |
4979d0c7 JO |
266 | pr_err("failed to write perf data, error: %m\n"); |
267 | return -1; | |
268 | } | |
269 | ||
8b99b1a4 | 270 | perf_stat.bytes_written += event->header.size; |
4979d0c7 JO |
271 | return 0; |
272 | } | |
273 | ||
1975d36e | 274 | static int write_stat_round_event(u64 tm, u64 type) |
7aad0c32 | 275 | { |
1975d36e | 276 | return perf_event__synthesize_stat_round(NULL, tm, type, |
7aad0c32 JO |
277 | process_synthesized_event, |
278 | NULL); | |
279 | } | |
280 | ||
281 | #define WRITE_STAT_ROUND_EVENT(time, interval) \ | |
282 | write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval) | |
283 | ||
5a6ea81b JO |
284 | #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) |
285 | ||
286 | static int | |
287 | perf_evsel__write_stat_event(struct perf_evsel *counter, u32 cpu, u32 thread, | |
288 | struct perf_counts_values *count) | |
289 | { | |
290 | struct perf_sample_id *sid = SID(counter, cpu, thread); | |
291 | ||
292 | return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count, | |
293 | process_synthesized_event, NULL); | |
294 | } | |
295 | ||
f5b4a9c3 SE |
296 | /* |
297 | * Read out the results of a single counter: | |
298 | * do not aggregate counts across CPUs in system-wide mode | |
299 | */ | |
c52b12ed | 300 | static int read_counter(struct perf_evsel *counter) |
f5b4a9c3 | 301 | { |
9bf1a529 | 302 | int nthreads = thread_map__nr(evsel_list->threads); |
00e727bb MR |
303 | int ncpus, cpu, thread; |
304 | ||
305 | if (target__has_cpu(&target)) | |
306 | ncpus = perf_evsel__nr_cpus(counter); | |
307 | else | |
308 | ncpus = 1; | |
f5b4a9c3 | 309 | |
3b4331d9 SP |
310 | if (!counter->supported) |
311 | return -ENOENT; | |
312 | ||
9bf1a529 JO |
313 | if (counter->system_wide) |
314 | nthreads = 1; | |
315 | ||
316 | for (thread = 0; thread < nthreads; thread++) { | |
317 | for (cpu = 0; cpu < ncpus; cpu++) { | |
3b3eb044 JO |
318 | struct perf_counts_values *count; |
319 | ||
320 | count = perf_counts(counter->counts, cpu, thread); | |
db49a717 SE |
321 | if (perf_evsel__read(counter, cpu, thread, count)) { |
322 | counter->counts->scaled = -1; | |
323 | perf_counts(counter->counts, cpu, thread)->ena = 0; | |
324 | perf_counts(counter->counts, cpu, thread)->run = 0; | |
9bf1a529 | 325 | return -1; |
db49a717 | 326 | } |
5a6ea81b JO |
327 | |
328 | if (STAT_RECORD) { | |
329 | if (perf_evsel__write_stat_event(counter, cpu, thread, count)) { | |
330 | pr_err("failed to write stat event\n"); | |
331 | return -1; | |
332 | } | |
333 | } | |
0b1abbf4 AK |
334 | |
335 | if (verbose > 1) { | |
336 | fprintf(stat_config.output, | |
337 | "%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n", | |
338 | perf_evsel__name(counter), | |
339 | cpu, | |
340 | count->val, count->ena, count->run); | |
341 | } | |
9bf1a529 | 342 | } |
f5b4a9c3 | 343 | } |
c52b12ed ACM |
344 | |
345 | return 0; | |
2996f5dd IM |
346 | } |
347 | ||
3df33eff | 348 | static void read_counters(void) |
13370a9b | 349 | { |
13370a9b | 350 | struct perf_evsel *counter; |
db49a717 | 351 | int ret; |
13370a9b | 352 | |
e5cadb93 | 353 | evlist__for_each_entry(evsel_list, counter) { |
db49a717 SE |
354 | ret = read_counter(counter); |
355 | if (ret) | |
245bad8e | 356 | pr_debug("failed to read counter %s\n", counter->name); |
3b3eb044 | 357 | |
db49a717 | 358 | if (ret == 0 && perf_stat_process_counter(&stat_config, counter)) |
3b3eb044 | 359 | pr_warning("failed to process counter %s\n", counter->name); |
13370a9b | 360 | } |
106a94a0 JO |
361 | } |
362 | ||
ba411a95 | 363 | static void process_interval(void) |
106a94a0 | 364 | { |
106a94a0 | 365 | struct timespec ts, rs; |
106a94a0 | 366 | |
3df33eff | 367 | read_counters(); |
86ee6e18 | 368 | |
13370a9b SE |
369 | clock_gettime(CLOCK_MONOTONIC, &ts); |
370 | diff_timespec(&rs, &ts, &ref_time); | |
13370a9b | 371 | |
7aad0c32 | 372 | if (STAT_RECORD) { |
bd48c63e | 373 | if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL)) |
7aad0c32 JO |
374 | pr_err("failed to write stat round event\n"); |
375 | } | |
376 | ||
d4f63a47 | 377 | print_counters(&rs, 0, NULL); |
13370a9b SE |
378 | } |
379 | ||
67ccdecd | 380 | static void enable_counters(void) |
41191688 | 381 | { |
67ccdecd | 382 | if (initial_delay) |
310ebb93 | 383 | usleep(initial_delay * USEC_PER_MSEC); |
67ccdecd JO |
384 | |
385 | /* | |
386 | * We need to enable counters only if: | |
387 | * - we don't have tracee (attaching to task or cpu) | |
388 | * - we have initial delay configured | |
389 | */ | |
390 | if (!target__none(&target) || initial_delay) | |
ab46db0a | 391 | perf_evlist__enable(evsel_list); |
41191688 AK |
392 | } |
393 | ||
3df33eff MR |
394 | static void disable_counters(void) |
395 | { | |
396 | /* | |
397 | * If we don't have tracee (attaching to task or cpu), counters may | |
398 | * still be running. To get accurate group ratios, we must stop groups | |
399 | * from counting before reading their constituent counters. | |
400 | */ | |
401 | if (!target__none(&target)) | |
402 | perf_evlist__disable(evsel_list); | |
403 | } | |
404 | ||
f33cbe72 | 405 | static volatile int workload_exec_errno; |
6af206fd ACM |
406 | |
407 | /* | |
408 | * perf_evlist__prepare_workload will send a SIGUSR1 | |
409 | * if the fork fails, since we asked by setting its | |
410 | * want_signal to true. | |
411 | */ | |
f33cbe72 ACM |
412 | static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info, |
413 | void *ucontext __maybe_unused) | |
6af206fd | 414 | { |
f33cbe72 | 415 | workload_exec_errno = info->si_value.sival_int; |
6af206fd ACM |
416 | } |
417 | ||
7b60a7e3 JO |
418 | static bool has_unit(struct perf_evsel *counter) |
419 | { | |
420 | return counter->unit && *counter->unit; | |
421 | } | |
422 | ||
423 | static bool has_scale(struct perf_evsel *counter) | |
424 | { | |
425 | return counter->scale != 1; | |
426 | } | |
427 | ||
664c98d4 | 428 | static int perf_stat_synthesize_config(bool is_pipe) |
8b99b1a4 | 429 | { |
7b60a7e3 | 430 | struct perf_evsel *counter; |
8b99b1a4 JO |
431 | int err; |
432 | ||
664c98d4 JO |
433 | if (is_pipe) { |
434 | err = perf_event__synthesize_attrs(NULL, perf_stat.session, | |
435 | process_synthesized_event); | |
436 | if (err < 0) { | |
437 | pr_err("Couldn't synthesize attrs.\n"); | |
438 | return err; | |
439 | } | |
440 | } | |
441 | ||
7b60a7e3 JO |
442 | /* |
443 | * Synthesize other events stuff not carried within | |
444 | * attr event - unit, scale, name | |
445 | */ | |
e5cadb93 | 446 | evlist__for_each_entry(evsel_list, counter) { |
7b60a7e3 JO |
447 | if (!counter->supported) |
448 | continue; | |
449 | ||
450 | /* | |
451 | * Synthesize unit and scale only if it's defined. | |
452 | */ | |
453 | if (has_unit(counter)) { | |
454 | err = perf_event__synthesize_event_update_unit(NULL, counter, process_synthesized_event); | |
455 | if (err < 0) { | |
456 | pr_err("Couldn't synthesize evsel unit.\n"); | |
457 | return err; | |
458 | } | |
459 | } | |
460 | ||
461 | if (has_scale(counter)) { | |
462 | err = perf_event__synthesize_event_update_scale(NULL, counter, process_synthesized_event); | |
463 | if (err < 0) { | |
464 | pr_err("Couldn't synthesize evsel scale.\n"); | |
465 | return err; | |
466 | } | |
467 | } | |
468 | ||
469 | if (counter->own_cpus) { | |
470 | err = perf_event__synthesize_event_update_cpus(NULL, counter, process_synthesized_event); | |
471 | if (err < 0) { | |
472 | pr_err("Couldn't synthesize evsel scale.\n"); | |
473 | return err; | |
474 | } | |
475 | } | |
476 | ||
477 | /* | |
478 | * Name is needed only for pipe output, | |
479 | * perf.data carries event names. | |
480 | */ | |
481 | if (is_pipe) { | |
482 | err = perf_event__synthesize_event_update_name(NULL, counter, process_synthesized_event); | |
483 | if (err < 0) { | |
484 | pr_err("Couldn't synthesize evsel name.\n"); | |
485 | return err; | |
486 | } | |
487 | } | |
488 | } | |
489 | ||
8b99b1a4 JO |
490 | err = perf_event__synthesize_thread_map2(NULL, evsel_list->threads, |
491 | process_synthesized_event, | |
492 | NULL); | |
493 | if (err < 0) { | |
494 | pr_err("Couldn't synthesize thread map.\n"); | |
495 | return err; | |
496 | } | |
497 | ||
498 | err = perf_event__synthesize_cpu_map(NULL, evsel_list->cpus, | |
499 | process_synthesized_event, NULL); | |
500 | if (err < 0) { | |
501 | pr_err("Couldn't synthesize thread map.\n"); | |
502 | return err; | |
503 | } | |
504 | ||
505 | err = perf_event__synthesize_stat_config(NULL, &stat_config, | |
506 | process_synthesized_event, NULL); | |
507 | if (err < 0) { | |
508 | pr_err("Couldn't synthesize config.\n"); | |
509 | return err; | |
510 | } | |
511 | ||
512 | return 0; | |
513 | } | |
514 | ||
2af4646d JO |
515 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
516 | ||
517 | static int __store_counter_ids(struct perf_evsel *counter, | |
518 | struct cpu_map *cpus, | |
519 | struct thread_map *threads) | |
520 | { | |
521 | int cpu, thread; | |
522 | ||
523 | for (cpu = 0; cpu < cpus->nr; cpu++) { | |
524 | for (thread = 0; thread < threads->nr; thread++) { | |
525 | int fd = FD(counter, cpu, thread); | |
526 | ||
527 | if (perf_evlist__id_add_fd(evsel_list, counter, | |
528 | cpu, thread, fd) < 0) | |
529 | return -1; | |
530 | } | |
531 | } | |
532 | ||
533 | return 0; | |
534 | } | |
535 | ||
536 | static int store_counter_ids(struct perf_evsel *counter) | |
537 | { | |
538 | struct cpu_map *cpus = counter->cpus; | |
539 | struct thread_map *threads = counter->threads; | |
540 | ||
541 | if (perf_evsel__alloc_id(counter, cpus->nr, threads->nr)) | |
542 | return -ENOMEM; | |
543 | ||
544 | return __store_counter_ids(counter, cpus, threads); | |
545 | } | |
546 | ||
acf28922 | 547 | static int __run_perf_stat(int argc, const char **argv) |
42202dd5 | 548 | { |
ec0d3d1f | 549 | int interval = stat_config.interval; |
d6195a6a | 550 | char msg[BUFSIZ]; |
42202dd5 | 551 | unsigned long long t0, t1; |
cac21425 | 552 | struct perf_evsel *counter; |
13370a9b | 553 | struct timespec ts; |
410136f5 | 554 | size_t l; |
42202dd5 | 555 | int status = 0; |
6be2850e | 556 | const bool forks = (argc > 0); |
664c98d4 | 557 | bool is_pipe = STAT_RECORD ? perf_stat.file.is_pipe : false; |
5d8bb1ec | 558 | struct perf_evsel_config_term *err_term; |
42202dd5 | 559 | |
13370a9b | 560 | if (interval) { |
310ebb93 ACM |
561 | ts.tv_sec = interval / USEC_PER_MSEC; |
562 | ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC; | |
13370a9b SE |
563 | } else { |
564 | ts.tv_sec = 1; | |
565 | ts.tv_nsec = 0; | |
566 | } | |
567 | ||
60666c63 | 568 | if (forks) { |
664c98d4 | 569 | if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe, |
735f7e0b | 570 | workload_exec_failed_signal) < 0) { |
acf28922 NK |
571 | perror("failed to prepare workload"); |
572 | return -1; | |
60666c63 | 573 | } |
d20a47e7 | 574 | child_pid = evsel_list->workload.pid; |
051ae7f7 PM |
575 | } |
576 | ||
6a4bb04c | 577 | if (group) |
63dab225 | 578 | perf_evlist__set_leader(evsel_list); |
6a4bb04c | 579 | |
e5cadb93 | 580 | evlist__for_each_entry(evsel_list, counter) { |
42ef8a78 | 581 | try_again: |
cac21425 | 582 | if (create_perf_stat_counter(counter) < 0) { |
979987a5 DA |
583 | /* |
584 | * PPC returns ENXIO for HW counters until 2.6.37 | |
585 | * (behavior changed with commit b0a873e). | |
586 | */ | |
38f6ae1e | 587 | if (errno == EINVAL || errno == ENOSYS || |
979987a5 DA |
588 | errno == ENOENT || errno == EOPNOTSUPP || |
589 | errno == ENXIO) { | |
bb963e16 | 590 | if (verbose > 0) |
c63ca0c0 | 591 | ui__warning("%s event is not supported by the kernel.\n", |
7289f83c | 592 | perf_evsel__name(counter)); |
2cee77c4 | 593 | counter->supported = false; |
cb5ef600 KL |
594 | |
595 | if ((counter->leader != counter) || | |
596 | !(counter->leader->nr_members > 1)) | |
597 | continue; | |
42ef8a78 | 598 | } else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) { |
bb963e16 | 599 | if (verbose > 0) |
42ef8a78 ACM |
600 | ui__warning("%s\n", msg); |
601 | goto try_again; | |
602 | } | |
ede70290 | 603 | |
56e52e85 ACM |
604 | perf_evsel__open_strerror(counter, &target, |
605 | errno, msg, sizeof(msg)); | |
606 | ui__error("%s\n", msg); | |
607 | ||
48290609 ACM |
608 | if (child_pid != -1) |
609 | kill(child_pid, SIGTERM); | |
fceda7fe | 610 | |
48290609 ACM |
611 | return -1; |
612 | } | |
2cee77c4 | 613 | counter->supported = true; |
410136f5 SE |
614 | |
615 | l = strlen(counter->unit); | |
616 | if (l > unit_width) | |
617 | unit_width = l; | |
2af4646d JO |
618 | |
619 | if (STAT_RECORD && store_counter_ids(counter)) | |
620 | return -1; | |
084ab9f8 | 621 | } |
42202dd5 | 622 | |
23d4aad4 ACM |
623 | if (perf_evlist__apply_filters(evsel_list, &counter)) { |
624 | error("failed to set filter \"%s\" on event %s with %d (%s)\n", | |
625 | counter->filter, perf_evsel__name(counter), errno, | |
c8b5f2c9 | 626 | str_error_r(errno, msg, sizeof(msg))); |
cfd748ae FW |
627 | return -1; |
628 | } | |
629 | ||
5d8bb1ec MP |
630 | if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) { |
631 | error("failed to set config \"%s\" on event %s with %d (%s)\n", | |
632 | err_term->val.drv_cfg, perf_evsel__name(counter), errno, | |
633 | str_error_r(errno, msg, sizeof(msg))); | |
634 | return -1; | |
635 | } | |
636 | ||
4979d0c7 JO |
637 | if (STAT_RECORD) { |
638 | int err, fd = perf_data_file__fd(&perf_stat.file); | |
639 | ||
664c98d4 JO |
640 | if (is_pipe) { |
641 | err = perf_header__write_pipe(perf_data_file__fd(&perf_stat.file)); | |
642 | } else { | |
643 | err = perf_session__write_header(perf_stat.session, evsel_list, | |
644 | fd, false); | |
645 | } | |
646 | ||
4979d0c7 JO |
647 | if (err < 0) |
648 | return err; | |
8b99b1a4 | 649 | |
664c98d4 | 650 | err = perf_stat_synthesize_config(is_pipe); |
8b99b1a4 JO |
651 | if (err < 0) |
652 | return err; | |
4979d0c7 JO |
653 | } |
654 | ||
42202dd5 IM |
655 | /* |
656 | * Enable counters and exec the command: | |
657 | */ | |
658 | t0 = rdclock(); | |
13370a9b | 659 | clock_gettime(CLOCK_MONOTONIC, &ref_time); |
42202dd5 | 660 | |
60666c63 | 661 | if (forks) { |
acf28922 | 662 | perf_evlist__start_workload(evsel_list); |
67ccdecd | 663 | enable_counters(); |
acf28922 | 664 | |
13370a9b SE |
665 | if (interval) { |
666 | while (!waitpid(child_pid, &status, WNOHANG)) { | |
667 | nanosleep(&ts, NULL); | |
ba411a95 | 668 | process_interval(); |
13370a9b SE |
669 | } |
670 | } | |
60666c63 | 671 | wait(&status); |
6af206fd | 672 | |
f33cbe72 | 673 | if (workload_exec_errno) { |
c8b5f2c9 | 674 | const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); |
f33cbe72 | 675 | pr_err("Workload failed: %s\n", emsg); |
6af206fd | 676 | return -1; |
f33cbe72 | 677 | } |
6af206fd | 678 | |
33e49ea7 AK |
679 | if (WIFSIGNALED(status)) |
680 | psignal(WTERMSIG(status), argv[0]); | |
60666c63 | 681 | } else { |
67ccdecd | 682 | enable_counters(); |
13370a9b SE |
683 | while (!done) { |
684 | nanosleep(&ts, NULL); | |
685 | if (interval) | |
ba411a95 | 686 | process_interval(); |
13370a9b | 687 | } |
60666c63 | 688 | } |
42202dd5 | 689 | |
3df33eff MR |
690 | disable_counters(); |
691 | ||
42202dd5 IM |
692 | t1 = rdclock(); |
693 | ||
9e9772c4 | 694 | update_stats(&walltime_nsecs_stats, t1 - t0); |
42202dd5 | 695 | |
3df33eff MR |
696 | /* |
697 | * Closing a group leader splits the group, and as we only disable | |
698 | * group leaders, results in remaining events becoming enabled. To | |
699 | * avoid arbitrary skew, we must read all counters before closing any | |
700 | * group leaders. | |
701 | */ | |
702 | read_counters(); | |
703 | perf_evlist__close(evsel_list); | |
c52b12ed | 704 | |
42202dd5 IM |
705 | return WEXITSTATUS(status); |
706 | } | |
707 | ||
41cde476 | 708 | static int run_perf_stat(int argc, const char **argv) |
1f16c575 PZ |
709 | { |
710 | int ret; | |
711 | ||
712 | if (pre_cmd) { | |
713 | ret = system(pre_cmd); | |
714 | if (ret) | |
715 | return ret; | |
716 | } | |
717 | ||
718 | if (sync_run) | |
719 | sync(); | |
720 | ||
721 | ret = __run_perf_stat(argc, argv); | |
722 | if (ret) | |
723 | return ret; | |
724 | ||
725 | if (post_cmd) { | |
726 | ret = system(post_cmd); | |
727 | if (ret) | |
728 | return ret; | |
729 | } | |
730 | ||
731 | return ret; | |
732 | } | |
733 | ||
d73515c0 AK |
734 | static void print_running(u64 run, u64 ena) |
735 | { | |
736 | if (csv_output) { | |
5821522e | 737 | fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f", |
d73515c0 AK |
738 | csv_sep, |
739 | run, | |
740 | csv_sep, | |
741 | ena ? 100.0 * run / ena : 100.0); | |
742 | } else if (run != ena) { | |
5821522e | 743 | fprintf(stat_config.output, " (%.2f%%)", 100.0 * run / ena); |
d73515c0 AK |
744 | } |
745 | } | |
746 | ||
f99844cb IM |
747 | static void print_noise_pct(double total, double avg) |
748 | { | |
0007ecea | 749 | double pct = rel_stddev_stats(total, avg); |
f99844cb | 750 | |
3ae9a34d | 751 | if (csv_output) |
5821522e | 752 | fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct); |
a1bca6cc | 753 | else if (pct) |
5821522e | 754 | fprintf(stat_config.output, " ( +-%6.2f%% )", pct); |
f99844cb IM |
755 | } |
756 | ||
69aad6f1 | 757 | static void print_noise(struct perf_evsel *evsel, double avg) |
42202dd5 | 758 | { |
581cc8a2 | 759 | struct perf_stat_evsel *ps; |
69aad6f1 | 760 | |
849abde9 PZ |
761 | if (run_count == 1) |
762 | return; | |
763 | ||
69aad6f1 | 764 | ps = evsel->priv; |
f99844cb | 765 | print_noise_pct(stddev_stats(&ps->res_stats[0]), avg); |
42202dd5 IM |
766 | } |
767 | ||
12c08a9f | 768 | static void aggr_printout(struct perf_evsel *evsel, int id, int nr) |
44175b6f | 769 | { |
421a50f3 | 770 | switch (stat_config.aggr_mode) { |
12c08a9f | 771 | case AGGR_CORE: |
5821522e | 772 | fprintf(stat_config.output, "S%d-C%*d%s%*d%s", |
12c08a9f SE |
773 | cpu_map__id_to_socket(id), |
774 | csv_output ? 0 : -8, | |
775 | cpu_map__id_to_cpu(id), | |
776 | csv_sep, | |
777 | csv_output ? 0 : 4, | |
778 | nr, | |
779 | csv_sep); | |
780 | break; | |
86ee6e18 | 781 | case AGGR_SOCKET: |
5821522e | 782 | fprintf(stat_config.output, "S%*d%s%*d%s", |
d7e7a451 | 783 | csv_output ? 0 : -5, |
12c08a9f | 784 | id, |
d7e7a451 SE |
785 | csv_sep, |
786 | csv_output ? 0 : 4, | |
787 | nr, | |
788 | csv_sep); | |
86ee6e18 SE |
789 | break; |
790 | case AGGR_NONE: | |
5821522e | 791 | fprintf(stat_config.output, "CPU%*d%s", |
d7470b6a | 792 | csv_output ? 0 : -4, |
12c08a9f | 793 | perf_evsel__cpus(evsel)->map[id], csv_sep); |
86ee6e18 | 794 | break; |
32b8af82 | 795 | case AGGR_THREAD: |
5821522e | 796 | fprintf(stat_config.output, "%*s-%*d%s", |
32b8af82 JO |
797 | csv_output ? 0 : 16, |
798 | thread_map__comm(evsel->threads, id), | |
799 | csv_output ? 0 : -8, | |
800 | thread_map__pid(evsel->threads, id), | |
801 | csv_sep); | |
802 | break; | |
86ee6e18 | 803 | case AGGR_GLOBAL: |
208df99e | 804 | case AGGR_UNSET: |
86ee6e18 SE |
805 | default: |
806 | break; | |
807 | } | |
808 | } | |
809 | ||
140aeadc AK |
810 | struct outstate { |
811 | FILE *fh; | |
812 | bool newline; | |
f9483392 | 813 | const char *prefix; |
92a61f64 | 814 | int nfields; |
44d49a60 AK |
815 | int id, nr; |
816 | struct perf_evsel *evsel; | |
140aeadc AK |
817 | }; |
818 | ||
819 | #define METRIC_LEN 35 | |
820 | ||
821 | static void new_line_std(void *ctx) | |
822 | { | |
823 | struct outstate *os = ctx; | |
824 | ||
825 | os->newline = true; | |
826 | } | |
827 | ||
828 | static void do_new_line_std(struct outstate *os) | |
829 | { | |
830 | fputc('\n', os->fh); | |
f9483392 | 831 | fputs(os->prefix, os->fh); |
44d49a60 | 832 | aggr_printout(os->evsel, os->id, os->nr); |
140aeadc AK |
833 | if (stat_config.aggr_mode == AGGR_NONE) |
834 | fprintf(os->fh, " "); | |
140aeadc AK |
835 | fprintf(os->fh, " "); |
836 | } | |
837 | ||
838 | static void print_metric_std(void *ctx, const char *color, const char *fmt, | |
839 | const char *unit, double val) | |
840 | { | |
841 | struct outstate *os = ctx; | |
842 | FILE *out = os->fh; | |
843 | int n; | |
844 | bool newline = os->newline; | |
845 | ||
846 | os->newline = false; | |
847 | ||
848 | if (unit == NULL || fmt == NULL) { | |
849 | fprintf(out, "%-*s", METRIC_LEN, ""); | |
850 | return; | |
851 | } | |
852 | ||
853 | if (newline) | |
854 | do_new_line_std(os); | |
855 | ||
856 | n = fprintf(out, " # "); | |
857 | if (color) | |
858 | n += color_fprintf(out, color, fmt, val); | |
859 | else | |
860 | n += fprintf(out, fmt, val); | |
861 | fprintf(out, " %-*s", METRIC_LEN - n - 1, unit); | |
862 | } | |
863 | ||
92a61f64 AK |
864 | static void new_line_csv(void *ctx) |
865 | { | |
866 | struct outstate *os = ctx; | |
867 | int i; | |
868 | ||
869 | fputc('\n', os->fh); | |
870 | if (os->prefix) | |
871 | fprintf(os->fh, "%s%s", os->prefix, csv_sep); | |
44d49a60 | 872 | aggr_printout(os->evsel, os->id, os->nr); |
92a61f64 AK |
873 | for (i = 0; i < os->nfields; i++) |
874 | fputs(csv_sep, os->fh); | |
875 | } | |
876 | ||
877 | static void print_metric_csv(void *ctx, | |
878 | const char *color __maybe_unused, | |
879 | const char *fmt, const char *unit, double val) | |
880 | { | |
881 | struct outstate *os = ctx; | |
882 | FILE *out = os->fh; | |
883 | char buf[64], *vals, *ends; | |
884 | ||
885 | if (unit == NULL || fmt == NULL) { | |
886 | fprintf(out, "%s%s%s%s", csv_sep, csv_sep, csv_sep, csv_sep); | |
887 | return; | |
888 | } | |
889 | snprintf(buf, sizeof(buf), fmt, val); | |
b07c40df | 890 | ends = vals = ltrim(buf); |
92a61f64 AK |
891 | while (isdigit(*ends) || *ends == '.') |
892 | ends++; | |
893 | *ends = 0; | |
894 | while (isspace(*unit)) | |
895 | unit++; | |
896 | fprintf(out, "%s%s%s%s", csv_sep, vals, csv_sep, unit); | |
897 | } | |
898 | ||
54b50916 AK |
899 | #define METRIC_ONLY_LEN 20 |
900 | ||
901 | /* Filter out some columns that don't work well in metrics only mode */ | |
902 | ||
903 | static bool valid_only_metric(const char *unit) | |
904 | { | |
905 | if (!unit) | |
906 | return false; | |
907 | if (strstr(unit, "/sec") || | |
908 | strstr(unit, "hz") || | |
909 | strstr(unit, "Hz") || | |
910 | strstr(unit, "CPUs utilized")) | |
911 | return false; | |
912 | return true; | |
913 | } | |
914 | ||
915 | static const char *fixunit(char *buf, struct perf_evsel *evsel, | |
916 | const char *unit) | |
917 | { | |
918 | if (!strncmp(unit, "of all", 6)) { | |
919 | snprintf(buf, 1024, "%s %s", perf_evsel__name(evsel), | |
920 | unit); | |
921 | return buf; | |
922 | } | |
923 | return unit; | |
924 | } | |
925 | ||
926 | static void print_metric_only(void *ctx, const char *color, const char *fmt, | |
927 | const char *unit, double val) | |
928 | { | |
929 | struct outstate *os = ctx; | |
930 | FILE *out = os->fh; | |
931 | int n; | |
932 | char buf[1024]; | |
933 | unsigned mlen = METRIC_ONLY_LEN; | |
934 | ||
935 | if (!valid_only_metric(unit)) | |
936 | return; | |
937 | unit = fixunit(buf, os->evsel, unit); | |
938 | if (color) | |
939 | n = color_fprintf(out, color, fmt, val); | |
940 | else | |
941 | n = fprintf(out, fmt, val); | |
942 | if (n > METRIC_ONLY_LEN) | |
943 | n = METRIC_ONLY_LEN; | |
944 | if (mlen < strlen(unit)) | |
945 | mlen = strlen(unit) + 1; | |
946 | fprintf(out, "%*s", mlen - n, ""); | |
947 | } | |
948 | ||
949 | static void print_metric_only_csv(void *ctx, const char *color __maybe_unused, | |
950 | const char *fmt, | |
951 | const char *unit, double val) | |
952 | { | |
953 | struct outstate *os = ctx; | |
954 | FILE *out = os->fh; | |
955 | char buf[64], *vals, *ends; | |
956 | char tbuf[1024]; | |
957 | ||
958 | if (!valid_only_metric(unit)) | |
959 | return; | |
960 | unit = fixunit(tbuf, os->evsel, unit); | |
961 | snprintf(buf, sizeof buf, fmt, val); | |
b07c40df | 962 | ends = vals = ltrim(buf); |
54b50916 AK |
963 | while (isdigit(*ends) || *ends == '.') |
964 | ends++; | |
965 | *ends = 0; | |
966 | fprintf(out, "%s%s", vals, csv_sep); | |
967 | } | |
968 | ||
969 | static void new_line_metric(void *ctx __maybe_unused) | |
970 | { | |
971 | } | |
972 | ||
973 | static void print_metric_header(void *ctx, const char *color __maybe_unused, | |
974 | const char *fmt __maybe_unused, | |
975 | const char *unit, double val __maybe_unused) | |
976 | { | |
977 | struct outstate *os = ctx; | |
978 | char tbuf[1024]; | |
979 | ||
980 | if (!valid_only_metric(unit)) | |
981 | return; | |
982 | unit = fixunit(tbuf, os->evsel, unit); | |
983 | if (csv_output) | |
984 | fprintf(os->fh, "%s%s", unit, csv_sep); | |
985 | else | |
986 | fprintf(os->fh, "%-*s ", METRIC_ONLY_LEN, unit); | |
987 | } | |
988 | ||
da88c7f7 | 989 | static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg) |
86ee6e18 | 990 | { |
5821522e | 991 | FILE *output = stat_config.output; |
310ebb93 | 992 | double msecs = avg / NSEC_PER_MSEC; |
410136f5 | 993 | const char *fmt_v, *fmt_n; |
4bbe5a61 | 994 | char name[25]; |
86ee6e18 | 995 | |
410136f5 SE |
996 | fmt_v = csv_output ? "%.6f%s" : "%18.6f%s"; |
997 | fmt_n = csv_output ? "%s" : "%-25s"; | |
998 | ||
da88c7f7 | 999 | aggr_printout(evsel, id, nr); |
d7470b6a | 1000 | |
4bbe5a61 DA |
1001 | scnprintf(name, sizeof(name), "%s%s", |
1002 | perf_evsel__name(evsel), csv_output ? "" : " (msec)"); | |
410136f5 SE |
1003 | |
1004 | fprintf(output, fmt_v, msecs, csv_sep); | |
1005 | ||
1006 | if (csv_output) | |
1007 | fprintf(output, "%s%s", evsel->unit, csv_sep); | |
1008 | else | |
1009 | fprintf(output, "%-*s%s", unit_width, evsel->unit, csv_sep); | |
1010 | ||
1011 | fprintf(output, fmt_n, name); | |
d7470b6a | 1012 | |
023695d9 | 1013 | if (evsel->cgrp) |
4aa9015f | 1014 | fprintf(output, "%s%s", csv_sep, evsel->cgrp->name); |
44175b6f IM |
1015 | } |
1016 | ||
44d49a60 AK |
1017 | static int first_shadow_cpu(struct perf_evsel *evsel, int id) |
1018 | { | |
1019 | int i; | |
1020 | ||
1021 | if (!aggr_get_id) | |
1022 | return 0; | |
1023 | ||
1024 | if (stat_config.aggr_mode == AGGR_NONE) | |
1025 | return id; | |
1026 | ||
1027 | if (stat_config.aggr_mode == AGGR_GLOBAL) | |
1028 | return 0; | |
1029 | ||
1030 | for (i = 0; i < perf_evsel__nr_cpus(evsel); i++) { | |
1031 | int cpu2 = perf_evsel__cpus(evsel)->map[i]; | |
1032 | ||
1033 | if (aggr_get_id(evsel_list->cpus, cpu2) == id) | |
1034 | return cpu2; | |
1035 | } | |
1036 | return 0; | |
1037 | } | |
1038 | ||
556b1fb7 JO |
1039 | static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg) |
1040 | { | |
5821522e | 1041 | FILE *output = stat_config.output; |
556b1fb7 JO |
1042 | double sc = evsel->scale; |
1043 | const char *fmt; | |
556b1fb7 JO |
1044 | |
1045 | if (csv_output) { | |
e3b03b6c | 1046 | fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s"; |
556b1fb7 JO |
1047 | } else { |
1048 | if (big_num) | |
e3b03b6c | 1049 | fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s"; |
556b1fb7 | 1050 | else |
e3b03b6c | 1051 | fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s"; |
556b1fb7 JO |
1052 | } |
1053 | ||
1054 | aggr_printout(evsel, id, nr); | |
1055 | ||
556b1fb7 JO |
1056 | fprintf(output, fmt, avg, csv_sep); |
1057 | ||
1058 | if (evsel->unit) | |
1059 | fprintf(output, "%-*s%s", | |
1060 | csv_output ? 0 : unit_width, | |
1061 | evsel->unit, csv_sep); | |
1062 | ||
1063 | fprintf(output, "%-*s", csv_output ? 0 : 25, perf_evsel__name(evsel)); | |
1064 | ||
1065 | if (evsel->cgrp) | |
1066 | fprintf(output, "%s%s", csv_sep, evsel->cgrp->name); | |
eedfcb4b | 1067 | } |
556b1fb7 | 1068 | |
f9483392 | 1069 | static void printout(int id, int nr, struct perf_evsel *counter, double uval, |
cb110f47 | 1070 | char *prefix, u64 run, u64 ena, double noise) |
eedfcb4b | 1071 | { |
140aeadc | 1072 | struct perf_stat_output_ctx out; |
f9483392 AK |
1073 | struct outstate os = { |
1074 | .fh = stat_config.output, | |
44d49a60 AK |
1075 | .prefix = prefix ? prefix : "", |
1076 | .id = id, | |
1077 | .nr = nr, | |
1078 | .evsel = counter, | |
f9483392 | 1079 | }; |
140aeadc AK |
1080 | print_metric_t pm = print_metric_std; |
1081 | void (*nl)(void *); | |
eedfcb4b | 1082 | |
54b50916 AK |
1083 | if (metric_only) { |
1084 | nl = new_line_metric; | |
1085 | if (csv_output) | |
1086 | pm = print_metric_only_csv; | |
1087 | else | |
1088 | pm = print_metric_only; | |
1089 | } else | |
1090 | nl = new_line_std; | |
eedfcb4b | 1091 | |
54b50916 | 1092 | if (csv_output && !metric_only) { |
92a61f64 AK |
1093 | static int aggr_fields[] = { |
1094 | [AGGR_GLOBAL] = 0, | |
1095 | [AGGR_THREAD] = 1, | |
1096 | [AGGR_NONE] = 1, | |
1097 | [AGGR_SOCKET] = 2, | |
1098 | [AGGR_CORE] = 2, | |
1099 | }; | |
1100 | ||
1101 | pm = print_metric_csv; | |
1102 | nl = new_line_csv; | |
1103 | os.nfields = 3; | |
1104 | os.nfields += aggr_fields[stat_config.aggr_mode]; | |
1105 | if (counter->cgrp) | |
1106 | os.nfields++; | |
1107 | } | |
b002f3bb | 1108 | if (run == 0 || ena == 0 || counter->counts->scaled == -1) { |
54b50916 AK |
1109 | if (metric_only) { |
1110 | pm(&os, NULL, "", "", 0); | |
1111 | return; | |
1112 | } | |
cb110f47 AK |
1113 | aggr_printout(counter, id, nr); |
1114 | ||
1115 | fprintf(stat_config.output, "%*s%s", | |
1116 | csv_output ? 0 : 18, | |
1117 | counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED, | |
1118 | csv_sep); | |
1119 | ||
02d492e5 BP |
1120 | if (counter->supported) |
1121 | print_free_counters_hint = 1; | |
1122 | ||
cb110f47 AK |
1123 | fprintf(stat_config.output, "%-*s%s", |
1124 | csv_output ? 0 : unit_width, | |
1125 | counter->unit, csv_sep); | |
1126 | ||
1127 | fprintf(stat_config.output, "%*s", | |
1128 | csv_output ? 0 : -25, | |
1129 | perf_evsel__name(counter)); | |
1130 | ||
1131 | if (counter->cgrp) | |
1132 | fprintf(stat_config.output, "%s%s", | |
1133 | csv_sep, counter->cgrp->name); | |
1134 | ||
92a61f64 AK |
1135 | if (!csv_output) |
1136 | pm(&os, NULL, NULL, "", 0); | |
1137 | print_noise(counter, noise); | |
cb110f47 | 1138 | print_running(run, ena); |
92a61f64 AK |
1139 | if (csv_output) |
1140 | pm(&os, NULL, NULL, "", 0); | |
cb110f47 AK |
1141 | return; |
1142 | } | |
1143 | ||
54b50916 AK |
1144 | if (metric_only) |
1145 | /* nothing */; | |
1146 | else if (nsec_counter(counter)) | |
eedfcb4b AK |
1147 | nsec_printout(id, nr, counter, uval); |
1148 | else | |
1149 | abs_printout(id, nr, counter, uval); | |
556b1fb7 | 1150 | |
140aeadc AK |
1151 | out.print_metric = pm; |
1152 | out.new_line = nl; | |
1153 | out.ctx = &os; | |
37932c18 | 1154 | out.force_header = false; |
140aeadc | 1155 | |
54b50916 | 1156 | if (csv_output && !metric_only) { |
92a61f64 AK |
1157 | print_noise(counter, noise); |
1158 | print_running(run, ena); | |
1159 | } | |
1160 | ||
1161 | perf_stat__print_shadow_stats(counter, uval, | |
44d49a60 | 1162 | first_shadow_cpu(counter, id), |
140aeadc | 1163 | &out); |
54b50916 | 1164 | if (!csv_output && !metric_only) { |
92a61f64 AK |
1165 | print_noise(counter, noise); |
1166 | print_running(run, ena); | |
1167 | } | |
556b1fb7 JO |
1168 | } |
1169 | ||
44d49a60 AK |
1170 | static void aggr_update_shadow(void) |
1171 | { | |
1172 | int cpu, s2, id, s; | |
1173 | u64 val; | |
1174 | struct perf_evsel *counter; | |
1175 | ||
1176 | for (s = 0; s < aggr_map->nr; s++) { | |
1177 | id = aggr_map->map[s]; | |
e5cadb93 | 1178 | evlist__for_each_entry(evsel_list, counter) { |
44d49a60 AK |
1179 | val = 0; |
1180 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { | |
1181 | s2 = aggr_get_id(evsel_list->cpus, cpu); | |
1182 | if (s2 != id) | |
1183 | continue; | |
1184 | val += perf_counts(counter->counts, cpu, 0)->val; | |
1185 | } | |
1186 | val = val * counter->scale; | |
1187 | perf_stat__update_shadow_stats(counter, &val, | |
1188 | first_shadow_cpu(counter, id)); | |
1189 | } | |
1190 | } | |
1191 | } | |
1192 | ||
430daf2d | 1193 | static void collect_all_aliases(struct perf_evsel *counter, |
fbe51fba AK |
1194 | void (*cb)(struct perf_evsel *counter, void *data, |
1195 | bool first), | |
1196 | void *data) | |
1197 | { | |
430daf2d AK |
1198 | struct perf_evsel *alias; |
1199 | ||
1200 | alias = list_prepare_entry(counter, &(evsel_list->entries), node); | |
1201 | list_for_each_entry_continue (alias, &evsel_list->entries, node) { | |
1202 | if (strcmp(perf_evsel__name(alias), perf_evsel__name(counter)) || | |
1203 | alias->scale != counter->scale || | |
1204 | alias->cgrp != counter->cgrp || | |
1205 | strcmp(alias->unit, counter->unit) || | |
1206 | nsec_counter(alias) != nsec_counter(counter)) | |
1207 | break; | |
1208 | alias->merged_stat = true; | |
1209 | cb(alias, data, false); | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | static bool collect_data(struct perf_evsel *counter, | |
1214 | void (*cb)(struct perf_evsel *counter, void *data, | |
1215 | bool first), | |
1216 | void *data) | |
1217 | { | |
1218 | if (counter->merged_stat) | |
1219 | return false; | |
fbe51fba | 1220 | cb(counter, data, true); |
430daf2d AK |
1221 | if (!no_merge) |
1222 | collect_all_aliases(counter, cb, data); | |
1223 | return true; | |
fbe51fba AK |
1224 | } |
1225 | ||
1226 | struct aggr_data { | |
1227 | u64 ena, run, val; | |
1228 | int id; | |
1229 | int nr; | |
1230 | int cpu; | |
1231 | }; | |
1232 | ||
1233 | static void aggr_cb(struct perf_evsel *counter, void *data, bool first) | |
1234 | { | |
1235 | struct aggr_data *ad = data; | |
1236 | int cpu, s2; | |
1237 | ||
1238 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { | |
1239 | struct perf_counts_values *counts; | |
1240 | ||
1241 | s2 = aggr_get_id(perf_evsel__cpus(counter), cpu); | |
1242 | if (s2 != ad->id) | |
1243 | continue; | |
1244 | if (first) | |
1245 | ad->nr++; | |
1246 | counts = perf_counts(counter->counts, cpu, 0); | |
b4229e9d AK |
1247 | /* |
1248 | * When any result is bad, make them all to give | |
1249 | * consistent output in interval mode. | |
1250 | */ | |
1251 | if (counts->ena == 0 || counts->run == 0 || | |
1252 | counter->counts->scaled == -1) { | |
1253 | ad->ena = 0; | |
1254 | ad->run = 0; | |
1255 | break; | |
1256 | } | |
fbe51fba AK |
1257 | ad->val += counts->val; |
1258 | ad->ena += counts->ena; | |
1259 | ad->run += counts->run; | |
1260 | } | |
1261 | } | |
1262 | ||
86ee6e18 | 1263 | static void print_aggr(char *prefix) |
d7e7a451 | 1264 | { |
5821522e | 1265 | FILE *output = stat_config.output; |
d7e7a451 | 1266 | struct perf_evsel *counter; |
fbe51fba | 1267 | int s, id, nr; |
410136f5 | 1268 | double uval; |
d7e7a451 | 1269 | u64 ena, run, val; |
54b50916 | 1270 | bool first; |
d7e7a451 | 1271 | |
86ee6e18 | 1272 | if (!(aggr_map || aggr_get_id)) |
d7e7a451 SE |
1273 | return; |
1274 | ||
44d49a60 AK |
1275 | aggr_update_shadow(); |
1276 | ||
54b50916 AK |
1277 | /* |
1278 | * With metric_only everything is on a single line. | |
1279 | * Without each counter has its own line. | |
1280 | */ | |
86ee6e18 | 1281 | for (s = 0; s < aggr_map->nr; s++) { |
fbe51fba | 1282 | struct aggr_data ad; |
54b50916 AK |
1283 | if (prefix && metric_only) |
1284 | fprintf(output, "%s", prefix); | |
1285 | ||
fbe51fba | 1286 | ad.id = id = aggr_map->map[s]; |
54b50916 | 1287 | first = true; |
e5cadb93 | 1288 | evlist__for_each_entry(evsel_list, counter) { |
fbe51fba AK |
1289 | ad.val = ad.ena = ad.run = 0; |
1290 | ad.nr = 0; | |
430daf2d AK |
1291 | if (!collect_data(counter, aggr_cb, &ad)) |
1292 | continue; | |
fbe51fba AK |
1293 | nr = ad.nr; |
1294 | ena = ad.ena; | |
1295 | run = ad.run; | |
1296 | val = ad.val; | |
54b50916 AK |
1297 | if (first && metric_only) { |
1298 | first = false; | |
1299 | aggr_printout(counter, id, nr); | |
1300 | } | |
1301 | if (prefix && !metric_only) | |
d7e7a451 SE |
1302 | fprintf(output, "%s", prefix); |
1303 | ||
410136f5 | 1304 | uval = val * counter->scale; |
cb110f47 | 1305 | printout(id, nr, counter, uval, prefix, run, ena, 1.0); |
54b50916 AK |
1306 | if (!metric_only) |
1307 | fputc('\n', output); | |
d7e7a451 | 1308 | } |
54b50916 AK |
1309 | if (metric_only) |
1310 | fputc('\n', output); | |
d7e7a451 SE |
1311 | } |
1312 | } | |
1313 | ||
32b8af82 JO |
1314 | static void print_aggr_thread(struct perf_evsel *counter, char *prefix) |
1315 | { | |
5821522e | 1316 | FILE *output = stat_config.output; |
32b8af82 JO |
1317 | int nthreads = thread_map__nr(counter->threads); |
1318 | int ncpus = cpu_map__nr(counter->cpus); | |
1319 | int cpu, thread; | |
1320 | double uval; | |
1321 | ||
1322 | for (thread = 0; thread < nthreads; thread++) { | |
1323 | u64 ena = 0, run = 0, val = 0; | |
1324 | ||
1325 | for (cpu = 0; cpu < ncpus; cpu++) { | |
1326 | val += perf_counts(counter->counts, cpu, thread)->val; | |
1327 | ena += perf_counts(counter->counts, cpu, thread)->ena; | |
1328 | run += perf_counts(counter->counts, cpu, thread)->run; | |
1329 | } | |
1330 | ||
1331 | if (prefix) | |
1332 | fprintf(output, "%s", prefix); | |
1333 | ||
1334 | uval = val * counter->scale; | |
cb110f47 | 1335 | printout(thread, 0, counter, uval, prefix, run, ena, 1.0); |
32b8af82 JO |
1336 | fputc('\n', output); |
1337 | } | |
1338 | } | |
1339 | ||
fbe51fba AK |
1340 | struct caggr_data { |
1341 | double avg, avg_enabled, avg_running; | |
1342 | }; | |
1343 | ||
1344 | static void counter_aggr_cb(struct perf_evsel *counter, void *data, | |
1345 | bool first __maybe_unused) | |
1346 | { | |
1347 | struct caggr_data *cd = data; | |
1348 | struct perf_stat_evsel *ps = counter->priv; | |
1349 | ||
1350 | cd->avg += avg_stats(&ps->res_stats[0]); | |
1351 | cd->avg_enabled += avg_stats(&ps->res_stats[1]); | |
1352 | cd->avg_running += avg_stats(&ps->res_stats[2]); | |
1353 | } | |
1354 | ||
2996f5dd IM |
1355 | /* |
1356 | * Print out the results of a single counter: | |
f5b4a9c3 | 1357 | * aggregated counts in system-wide mode |
2996f5dd | 1358 | */ |
13370a9b | 1359 | static void print_counter_aggr(struct perf_evsel *counter, char *prefix) |
2996f5dd | 1360 | { |
5821522e | 1361 | FILE *output = stat_config.output; |
410136f5 | 1362 | double uval; |
fbe51fba | 1363 | struct caggr_data cd = { .avg = 0.0 }; |
d73515c0 | 1364 | |
430daf2d AK |
1365 | if (!collect_data(counter, counter_aggr_cb, &cd)) |
1366 | return; | |
2996f5dd | 1367 | |
54b50916 | 1368 | if (prefix && !metric_only) |
13370a9b SE |
1369 | fprintf(output, "%s", prefix); |
1370 | ||
fbe51fba AK |
1371 | uval = cd.avg * counter->scale; |
1372 | printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled, cd.avg); | |
54b50916 AK |
1373 | if (!metric_only) |
1374 | fprintf(output, "\n"); | |
c04f5e5d IM |
1375 | } |
1376 | ||
fbe51fba AK |
1377 | static void counter_cb(struct perf_evsel *counter, void *data, |
1378 | bool first __maybe_unused) | |
1379 | { | |
1380 | struct aggr_data *ad = data; | |
1381 | ||
1382 | ad->val += perf_counts(counter->counts, ad->cpu, 0)->val; | |
1383 | ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena; | |
1384 | ad->run += perf_counts(counter->counts, ad->cpu, 0)->run; | |
1385 | } | |
1386 | ||
f5b4a9c3 SE |
1387 | /* |
1388 | * Print out the results of a single counter: | |
1389 | * does not use aggregated count in system-wide | |
1390 | */ | |
13370a9b | 1391 | static void print_counter(struct perf_evsel *counter, char *prefix) |
f5b4a9c3 | 1392 | { |
5821522e | 1393 | FILE *output = stat_config.output; |
f5b4a9c3 | 1394 | u64 ena, run, val; |
410136f5 | 1395 | double uval; |
f5b4a9c3 SE |
1396 | int cpu; |
1397 | ||
7ae92e74 | 1398 | for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) { |
fbe51fba AK |
1399 | struct aggr_data ad = { .cpu = cpu }; |
1400 | ||
430daf2d AK |
1401 | if (!collect_data(counter, counter_cb, &ad)) |
1402 | return; | |
fbe51fba AK |
1403 | val = ad.val; |
1404 | ena = ad.ena; | |
1405 | run = ad.run; | |
13370a9b SE |
1406 | |
1407 | if (prefix) | |
1408 | fprintf(output, "%s", prefix); | |
1409 | ||
410136f5 | 1410 | uval = val * counter->scale; |
cb110f47 | 1411 | printout(cpu, 0, counter, uval, prefix, run, ena, 1.0); |
f5b4a9c3 | 1412 | |
4aa9015f | 1413 | fputc('\n', output); |
f5b4a9c3 SE |
1414 | } |
1415 | } | |
1416 | ||
206cab65 AK |
1417 | static void print_no_aggr_metric(char *prefix) |
1418 | { | |
1419 | int cpu; | |
1420 | int nrcpus = 0; | |
1421 | struct perf_evsel *counter; | |
1422 | u64 ena, run, val; | |
1423 | double uval; | |
1424 | ||
1425 | nrcpus = evsel_list->cpus->nr; | |
1426 | for (cpu = 0; cpu < nrcpus; cpu++) { | |
1427 | bool first = true; | |
1428 | ||
1429 | if (prefix) | |
1430 | fputs(prefix, stat_config.output); | |
e5cadb93 | 1431 | evlist__for_each_entry(evsel_list, counter) { |
206cab65 AK |
1432 | if (first) { |
1433 | aggr_printout(counter, cpu, 0); | |
1434 | first = false; | |
1435 | } | |
1436 | val = perf_counts(counter->counts, cpu, 0)->val; | |
1437 | ena = perf_counts(counter->counts, cpu, 0)->ena; | |
1438 | run = perf_counts(counter->counts, cpu, 0)->run; | |
1439 | ||
1440 | uval = val * counter->scale; | |
1441 | printout(cpu, 0, counter, uval, prefix, run, ena, 1.0); | |
1442 | } | |
1443 | fputc('\n', stat_config.output); | |
1444 | } | |
1445 | } | |
1446 | ||
54b50916 AK |
1447 | static int aggr_header_lens[] = { |
1448 | [AGGR_CORE] = 18, | |
1449 | [AGGR_SOCKET] = 12, | |
206cab65 | 1450 | [AGGR_NONE] = 6, |
54b50916 AK |
1451 | [AGGR_THREAD] = 24, |
1452 | [AGGR_GLOBAL] = 0, | |
1453 | }; | |
1454 | ||
c51fd639 AK |
1455 | static const char *aggr_header_csv[] = { |
1456 | [AGGR_CORE] = "core,cpus,", | |
1457 | [AGGR_SOCKET] = "socket,cpus", | |
1458 | [AGGR_NONE] = "cpu,", | |
1459 | [AGGR_THREAD] = "comm-pid,", | |
1460 | [AGGR_GLOBAL] = "" | |
1461 | }; | |
1462 | ||
41c8ca2a | 1463 | static void print_metric_headers(const char *prefix, bool no_indent) |
54b50916 AK |
1464 | { |
1465 | struct perf_stat_output_ctx out; | |
1466 | struct perf_evsel *counter; | |
1467 | struct outstate os = { | |
1468 | .fh = stat_config.output | |
1469 | }; | |
1470 | ||
1471 | if (prefix) | |
1472 | fprintf(stat_config.output, "%s", prefix); | |
1473 | ||
41c8ca2a | 1474 | if (!csv_output && !no_indent) |
54b50916 AK |
1475 | fprintf(stat_config.output, "%*s", |
1476 | aggr_header_lens[stat_config.aggr_mode], ""); | |
c51fd639 AK |
1477 | if (csv_output) { |
1478 | if (stat_config.interval) | |
1479 | fputs("time,", stat_config.output); | |
1480 | fputs(aggr_header_csv[stat_config.aggr_mode], | |
1481 | stat_config.output); | |
1482 | } | |
54b50916 AK |
1483 | |
1484 | /* Print metrics headers only */ | |
e5cadb93 | 1485 | evlist__for_each_entry(evsel_list, counter) { |
54b50916 AK |
1486 | os.evsel = counter; |
1487 | out.ctx = &os; | |
1488 | out.print_metric = print_metric_header; | |
1489 | out.new_line = new_line_metric; | |
37932c18 | 1490 | out.force_header = true; |
54b50916 AK |
1491 | os.evsel = counter; |
1492 | perf_stat__print_shadow_stats(counter, 0, | |
1493 | 0, | |
1494 | &out); | |
1495 | } | |
1496 | fputc('\n', stat_config.output); | |
1497 | } | |
1498 | ||
d4f63a47 JO |
1499 | static void print_interval(char *prefix, struct timespec *ts) |
1500 | { | |
5821522e | 1501 | FILE *output = stat_config.output; |
d4f63a47 JO |
1502 | static int num_print_interval; |
1503 | ||
1504 | sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep); | |
1505 | ||
41c8ca2a | 1506 | if (num_print_interval == 0 && !csv_output) { |
421a50f3 | 1507 | switch (stat_config.aggr_mode) { |
d4f63a47 | 1508 | case AGGR_SOCKET: |
41c8ca2a AK |
1509 | fprintf(output, "# time socket cpus"); |
1510 | if (!metric_only) | |
1511 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | |
d4f63a47 JO |
1512 | break; |
1513 | case AGGR_CORE: | |
41c8ca2a AK |
1514 | fprintf(output, "# time core cpus"); |
1515 | if (!metric_only) | |
1516 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | |
d4f63a47 JO |
1517 | break; |
1518 | case AGGR_NONE: | |
41c8ca2a AK |
1519 | fprintf(output, "# time CPU"); |
1520 | if (!metric_only) | |
1521 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | |
d4f63a47 | 1522 | break; |
32b8af82 | 1523 | case AGGR_THREAD: |
41c8ca2a AK |
1524 | fprintf(output, "# time comm-pid"); |
1525 | if (!metric_only) | |
1526 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | |
32b8af82 | 1527 | break; |
d4f63a47 JO |
1528 | case AGGR_GLOBAL: |
1529 | default: | |
41c8ca2a AK |
1530 | fprintf(output, "# time"); |
1531 | if (!metric_only) | |
1532 | fprintf(output, " counts %*s events\n", unit_width, "unit"); | |
208df99e JO |
1533 | case AGGR_UNSET: |
1534 | break; | |
d4f63a47 JO |
1535 | } |
1536 | } | |
1537 | ||
41c8ca2a AK |
1538 | if (num_print_interval == 0 && metric_only) |
1539 | print_metric_headers(" ", true); | |
d4f63a47 JO |
1540 | if (++num_print_interval == 25) |
1541 | num_print_interval = 0; | |
1542 | } | |
1543 | ||
1544 | static void print_header(int argc, const char **argv) | |
42202dd5 | 1545 | { |
5821522e | 1546 | FILE *output = stat_config.output; |
69aad6f1 | 1547 | int i; |
42202dd5 | 1548 | |
ddcacfa0 IM |
1549 | fflush(stdout); |
1550 | ||
d7470b6a | 1551 | if (!csv_output) { |
4aa9015f SE |
1552 | fprintf(output, "\n"); |
1553 | fprintf(output, " Performance counter stats for "); | |
62d3b617 DA |
1554 | if (target.system_wide) |
1555 | fprintf(output, "\'system wide"); | |
1556 | else if (target.cpu_list) | |
1557 | fprintf(output, "\'CPU(s) %s", target.cpu_list); | |
602ad878 | 1558 | else if (!target__has_task(&target)) { |
ba6039b6 JO |
1559 | fprintf(output, "\'%s", argv ? argv[0] : "pipe"); |
1560 | for (i = 1; argv && (i < argc); i++) | |
4aa9015f | 1561 | fprintf(output, " %s", argv[i]); |
20f946b4 NK |
1562 | } else if (target.pid) |
1563 | fprintf(output, "process id \'%s", target.pid); | |
d7470b6a | 1564 | else |
20f946b4 | 1565 | fprintf(output, "thread id \'%s", target.tid); |
44db76c8 | 1566 | |
4aa9015f | 1567 | fprintf(output, "\'"); |
d7470b6a | 1568 | if (run_count > 1) |
4aa9015f SE |
1569 | fprintf(output, " (%d runs)", run_count); |
1570 | fprintf(output, ":\n\n"); | |
d7470b6a | 1571 | } |
d4f63a47 JO |
1572 | } |
1573 | ||
1574 | static void print_footer(void) | |
1575 | { | |
5821522e JO |
1576 | FILE *output = stat_config.output; |
1577 | ||
d4f63a47 JO |
1578 | if (!null_run) |
1579 | fprintf(output, "\n"); | |
1580 | fprintf(output, " %17.9f seconds time elapsed", | |
310ebb93 | 1581 | avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC); |
d4f63a47 JO |
1582 | if (run_count > 1) { |
1583 | fprintf(output, " "); | |
1584 | print_noise_pct(stddev_stats(&walltime_nsecs_stats), | |
1585 | avg_stats(&walltime_nsecs_stats)); | |
1586 | } | |
1587 | fprintf(output, "\n\n"); | |
02d492e5 BP |
1588 | |
1589 | if (print_free_counters_hint) | |
1590 | fprintf(output, | |
1591 | "Some events weren't counted. Try disabling the NMI watchdog:\n" | |
1592 | " echo 0 > /proc/sys/kernel/nmi_watchdog\n" | |
1593 | " perf stat ...\n" | |
1594 | " echo 1 > /proc/sys/kernel/nmi_watchdog\n"); | |
d4f63a47 JO |
1595 | } |
1596 | ||
1597 | static void print_counters(struct timespec *ts, int argc, const char **argv) | |
1598 | { | |
ec0d3d1f | 1599 | int interval = stat_config.interval; |
d4f63a47 JO |
1600 | struct perf_evsel *counter; |
1601 | char buf[64], *prefix = NULL; | |
1602 | ||
664c98d4 JO |
1603 | /* Do not print anything if we record to the pipe. */ |
1604 | if (STAT_RECORD && perf_stat.file.is_pipe) | |
1605 | return; | |
1606 | ||
d4f63a47 JO |
1607 | if (interval) |
1608 | print_interval(prefix = buf, ts); | |
1609 | else | |
1610 | print_header(argc, argv); | |
2996f5dd | 1611 | |
54b50916 AK |
1612 | if (metric_only) { |
1613 | static int num_print_iv; | |
1614 | ||
41c8ca2a AK |
1615 | if (num_print_iv == 0 && !interval) |
1616 | print_metric_headers(prefix, false); | |
54b50916 AK |
1617 | if (num_print_iv++ == 25) |
1618 | num_print_iv = 0; | |
1619 | if (stat_config.aggr_mode == AGGR_GLOBAL && prefix) | |
1620 | fprintf(stat_config.output, "%s", prefix); | |
1621 | } | |
1622 | ||
421a50f3 | 1623 | switch (stat_config.aggr_mode) { |
12c08a9f | 1624 | case AGGR_CORE: |
86ee6e18 | 1625 | case AGGR_SOCKET: |
d4f63a47 | 1626 | print_aggr(prefix); |
86ee6e18 | 1627 | break; |
32b8af82 | 1628 | case AGGR_THREAD: |
e5cadb93 | 1629 | evlist__for_each_entry(evsel_list, counter) |
32b8af82 JO |
1630 | print_aggr_thread(counter, prefix); |
1631 | break; | |
86ee6e18 | 1632 | case AGGR_GLOBAL: |
e5cadb93 | 1633 | evlist__for_each_entry(evsel_list, counter) |
d4f63a47 | 1634 | print_counter_aggr(counter, prefix); |
54b50916 AK |
1635 | if (metric_only) |
1636 | fputc('\n', stat_config.output); | |
86ee6e18 SE |
1637 | break; |
1638 | case AGGR_NONE: | |
206cab65 AK |
1639 | if (metric_only) |
1640 | print_no_aggr_metric(prefix); | |
1641 | else { | |
e5cadb93 | 1642 | evlist__for_each_entry(evsel_list, counter) |
206cab65 AK |
1643 | print_counter(counter, prefix); |
1644 | } | |
86ee6e18 | 1645 | break; |
208df99e | 1646 | case AGGR_UNSET: |
86ee6e18 SE |
1647 | default: |
1648 | break; | |
f5b4a9c3 | 1649 | } |
ddcacfa0 | 1650 | |
d4f63a47 JO |
1651 | if (!interval && !csv_output) |
1652 | print_footer(); | |
1653 | ||
5821522e | 1654 | fflush(stat_config.output); |
ddcacfa0 IM |
1655 | } |
1656 | ||
f7b7c26e PZ |
1657 | static volatile int signr = -1; |
1658 | ||
5242519b | 1659 | static void skip_signal(int signo) |
ddcacfa0 | 1660 | { |
ec0d3d1f | 1661 | if ((child_pid == -1) || stat_config.interval) |
60666c63 LW |
1662 | done = 1; |
1663 | ||
f7b7c26e | 1664 | signr = signo; |
d07f0b12 SE |
1665 | /* |
1666 | * render child_pid harmless | |
1667 | * won't send SIGTERM to a random | |
1668 | * process in case of race condition | |
1669 | * and fast PID recycling | |
1670 | */ | |
1671 | child_pid = -1; | |
f7b7c26e PZ |
1672 | } |
1673 | ||
1674 | static void sig_atexit(void) | |
1675 | { | |
d07f0b12 SE |
1676 | sigset_t set, oset; |
1677 | ||
1678 | /* | |
1679 | * avoid race condition with SIGCHLD handler | |
1680 | * in skip_signal() which is modifying child_pid | |
1681 | * goal is to avoid send SIGTERM to a random | |
1682 | * process | |
1683 | */ | |
1684 | sigemptyset(&set); | |
1685 | sigaddset(&set, SIGCHLD); | |
1686 | sigprocmask(SIG_BLOCK, &set, &oset); | |
1687 | ||
933da83a CW |
1688 | if (child_pid != -1) |
1689 | kill(child_pid, SIGTERM); | |
1690 | ||
d07f0b12 SE |
1691 | sigprocmask(SIG_SETMASK, &oset, NULL); |
1692 | ||
f7b7c26e PZ |
1693 | if (signr == -1) |
1694 | return; | |
1695 | ||
1696 | signal(signr, SIG_DFL); | |
1697 | kill(getpid(), signr); | |
5242519b IM |
1698 | } |
1699 | ||
1d037ca1 IT |
1700 | static int stat__set_big_num(const struct option *opt __maybe_unused, |
1701 | const char *s __maybe_unused, int unset) | |
d7470b6a SE |
1702 | { |
1703 | big_num_opt = unset ? 0 : 1; | |
1704 | return 0; | |
1705 | } | |
1706 | ||
44b1e60a AK |
1707 | static int enable_metric_only(const struct option *opt __maybe_unused, |
1708 | const char *s __maybe_unused, int unset) | |
1709 | { | |
1710 | force_metric_only = true; | |
1711 | metric_only = !unset; | |
1712 | return 0; | |
1713 | } | |
1714 | ||
e0547311 JO |
1715 | static const struct option stat_options[] = { |
1716 | OPT_BOOLEAN('T', "transaction", &transaction_run, | |
1717 | "hardware transaction statistics"), | |
1718 | OPT_CALLBACK('e', "event", &evsel_list, "event", | |
1719 | "event selector. use 'perf list' to list available events", | |
1720 | parse_events_option), | |
1721 | OPT_CALLBACK(0, "filter", &evsel_list, "filter", | |
1722 | "event filter", parse_filter), | |
1723 | OPT_BOOLEAN('i', "no-inherit", &no_inherit, | |
1724 | "child tasks do not inherit counters"), | |
1725 | OPT_STRING('p', "pid", &target.pid, "pid", | |
1726 | "stat events on existing process id"), | |
1727 | OPT_STRING('t', "tid", &target.tid, "tid", | |
1728 | "stat events on existing thread id"), | |
1729 | OPT_BOOLEAN('a', "all-cpus", &target.system_wide, | |
1730 | "system-wide collection from all CPUs"), | |
1731 | OPT_BOOLEAN('g', "group", &group, | |
1732 | "put the counters into a counter group"), | |
1733 | OPT_BOOLEAN('c', "scale", &stat_config.scale, "scale/normalize counters"), | |
1734 | OPT_INCR('v', "verbose", &verbose, | |
1735 | "be more verbose (show counter open errors, etc)"), | |
1736 | OPT_INTEGER('r', "repeat", &run_count, | |
1737 | "repeat command and print average + stddev (max: 100, forever: 0)"), | |
1738 | OPT_BOOLEAN('n', "null", &null_run, | |
1739 | "null run - dont start any counters"), | |
1740 | OPT_INCR('d', "detailed", &detailed_run, | |
1741 | "detailed run - start a lot of events"), | |
1742 | OPT_BOOLEAN('S', "sync", &sync_run, | |
1743 | "call sync() before starting a run"), | |
1744 | OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL, | |
1745 | "print large numbers with thousands\' separators", | |
1746 | stat__set_big_num), | |
1747 | OPT_STRING('C', "cpu", &target.cpu_list, "cpu", | |
1748 | "list of cpus to monitor in system-wide"), | |
1749 | OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode, | |
1750 | "disable CPU count aggregation", AGGR_NONE), | |
430daf2d | 1751 | OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"), |
e0547311 JO |
1752 | OPT_STRING('x', "field-separator", &csv_sep, "separator", |
1753 | "print counts with custom separator"), | |
1754 | OPT_CALLBACK('G', "cgroup", &evsel_list, "name", | |
1755 | "monitor event in cgroup name only", parse_cgroups), | |
1756 | OPT_STRING('o', "output", &output_name, "file", "output file name"), | |
1757 | OPT_BOOLEAN(0, "append", &append_file, "append to the output file"), | |
1758 | OPT_INTEGER(0, "log-fd", &output_fd, | |
1759 | "log output to fd, instead of stderr"), | |
1760 | OPT_STRING(0, "pre", &pre_cmd, "command", | |
1761 | "command to run prior to the measured command"), | |
1762 | OPT_STRING(0, "post", &post_cmd, "command", | |
1763 | "command to run after to the measured command"), | |
1764 | OPT_UINTEGER('I', "interval-print", &stat_config.interval, | |
1765 | "print counts at regular interval in ms (>= 10)"), | |
1766 | OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode, | |
1767 | "aggregate counts per processor socket", AGGR_SOCKET), | |
1768 | OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode, | |
1769 | "aggregate counts per physical processor core", AGGR_CORE), | |
1770 | OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode, | |
1771 | "aggregate counts per thread", AGGR_THREAD), | |
1772 | OPT_UINTEGER('D', "delay", &initial_delay, | |
1773 | "ms to wait before starting measurement after program start"), | |
44b1e60a AK |
1774 | OPT_CALLBACK_NOOPT(0, "metric-only", &metric_only, NULL, |
1775 | "Only print computed metrics. No raw values", enable_metric_only), | |
1776 | OPT_BOOLEAN(0, "topdown", &topdown_run, | |
1777 | "measure topdown level 1 statistics"), | |
e0547311 JO |
1778 | OPT_END() |
1779 | }; | |
1780 | ||
1fe7a300 JO |
1781 | static int perf_stat__get_socket(struct cpu_map *map, int cpu) |
1782 | { | |
1783 | return cpu_map__get_socket(map, cpu, NULL); | |
1784 | } | |
1785 | ||
1786 | static int perf_stat__get_core(struct cpu_map *map, int cpu) | |
1787 | { | |
1788 | return cpu_map__get_core(map, cpu, NULL); | |
1789 | } | |
1790 | ||
1e5a2931 JO |
1791 | static int cpu_map__get_max(struct cpu_map *map) |
1792 | { | |
1793 | int i, max = -1; | |
1794 | ||
1795 | for (i = 0; i < map->nr; i++) { | |
1796 | if (map->map[i] > max) | |
1797 | max = map->map[i]; | |
1798 | } | |
1799 | ||
1800 | return max; | |
1801 | } | |
1802 | ||
1803 | static struct cpu_map *cpus_aggr_map; | |
1804 | ||
1805 | static int perf_stat__get_aggr(aggr_get_id_t get_id, struct cpu_map *map, int idx) | |
1806 | { | |
1807 | int cpu; | |
1808 | ||
1809 | if (idx >= map->nr) | |
1810 | return -1; | |
1811 | ||
1812 | cpu = map->map[idx]; | |
1813 | ||
1814 | if (cpus_aggr_map->map[cpu] == -1) | |
1815 | cpus_aggr_map->map[cpu] = get_id(map, idx); | |
1816 | ||
1817 | return cpus_aggr_map->map[cpu]; | |
1818 | } | |
1819 | ||
1820 | static int perf_stat__get_socket_cached(struct cpu_map *map, int idx) | |
1821 | { | |
1822 | return perf_stat__get_aggr(perf_stat__get_socket, map, idx); | |
1823 | } | |
1824 | ||
1825 | static int perf_stat__get_core_cached(struct cpu_map *map, int idx) | |
1826 | { | |
1827 | return perf_stat__get_aggr(perf_stat__get_core, map, idx); | |
1828 | } | |
1829 | ||
86ee6e18 SE |
1830 | static int perf_stat_init_aggr_mode(void) |
1831 | { | |
1e5a2931 JO |
1832 | int nr; |
1833 | ||
421a50f3 | 1834 | switch (stat_config.aggr_mode) { |
86ee6e18 SE |
1835 | case AGGR_SOCKET: |
1836 | if (cpu_map__build_socket_map(evsel_list->cpus, &aggr_map)) { | |
1837 | perror("cannot build socket map"); | |
1838 | return -1; | |
1839 | } | |
1e5a2931 | 1840 | aggr_get_id = perf_stat__get_socket_cached; |
86ee6e18 | 1841 | break; |
12c08a9f SE |
1842 | case AGGR_CORE: |
1843 | if (cpu_map__build_core_map(evsel_list->cpus, &aggr_map)) { | |
1844 | perror("cannot build core map"); | |
1845 | return -1; | |
1846 | } | |
1e5a2931 | 1847 | aggr_get_id = perf_stat__get_core_cached; |
12c08a9f | 1848 | break; |
86ee6e18 SE |
1849 | case AGGR_NONE: |
1850 | case AGGR_GLOBAL: | |
32b8af82 | 1851 | case AGGR_THREAD: |
208df99e | 1852 | case AGGR_UNSET: |
86ee6e18 SE |
1853 | default: |
1854 | break; | |
1855 | } | |
1e5a2931 JO |
1856 | |
1857 | /* | |
1858 | * The evsel_list->cpus is the base we operate on, | |
1859 | * taking the highest cpu number to be the size of | |
1860 | * the aggregation translate cpumap. | |
1861 | */ | |
1862 | nr = cpu_map__get_max(evsel_list->cpus); | |
1863 | cpus_aggr_map = cpu_map__empty_new(nr + 1); | |
1864 | return cpus_aggr_map ? 0 : -ENOMEM; | |
86ee6e18 SE |
1865 | } |
1866 | ||
544c2ae7 MH |
1867 | static void perf_stat__exit_aggr_mode(void) |
1868 | { | |
1869 | cpu_map__put(aggr_map); | |
1870 | cpu_map__put(cpus_aggr_map); | |
1871 | aggr_map = NULL; | |
1872 | cpus_aggr_map = NULL; | |
1873 | } | |
1874 | ||
68d702f7 JO |
1875 | static inline int perf_env__get_cpu(struct perf_env *env, struct cpu_map *map, int idx) |
1876 | { | |
1877 | int cpu; | |
1878 | ||
1879 | if (idx > map->nr) | |
1880 | return -1; | |
1881 | ||
1882 | cpu = map->map[idx]; | |
1883 | ||
da8a58b5 | 1884 | if (cpu >= env->nr_cpus_avail) |
68d702f7 JO |
1885 | return -1; |
1886 | ||
1887 | return cpu; | |
1888 | } | |
1889 | ||
1890 | static int perf_env__get_socket(struct cpu_map *map, int idx, void *data) | |
1891 | { | |
1892 | struct perf_env *env = data; | |
1893 | int cpu = perf_env__get_cpu(env, map, idx); | |
1894 | ||
1895 | return cpu == -1 ? -1 : env->cpu[cpu].socket_id; | |
1896 | } | |
1897 | ||
1898 | static int perf_env__get_core(struct cpu_map *map, int idx, void *data) | |
1899 | { | |
1900 | struct perf_env *env = data; | |
1901 | int core = -1, cpu = perf_env__get_cpu(env, map, idx); | |
1902 | ||
1903 | if (cpu != -1) { | |
1904 | int socket_id = env->cpu[cpu].socket_id; | |
1905 | ||
1906 | /* | |
1907 | * Encode socket in upper 16 bits | |
1908 | * core_id is relative to socket, and | |
1909 | * we need a global id. So we combine | |
1910 | * socket + core id. | |
1911 | */ | |
1912 | core = (socket_id << 16) | (env->cpu[cpu].core_id & 0xffff); | |
1913 | } | |
1914 | ||
1915 | return core; | |
1916 | } | |
1917 | ||
1918 | static int perf_env__build_socket_map(struct perf_env *env, struct cpu_map *cpus, | |
1919 | struct cpu_map **sockp) | |
1920 | { | |
1921 | return cpu_map__build_map(cpus, sockp, perf_env__get_socket, env); | |
1922 | } | |
1923 | ||
1924 | static int perf_env__build_core_map(struct perf_env *env, struct cpu_map *cpus, | |
1925 | struct cpu_map **corep) | |
1926 | { | |
1927 | return cpu_map__build_map(cpus, corep, perf_env__get_core, env); | |
1928 | } | |
1929 | ||
1930 | static int perf_stat__get_socket_file(struct cpu_map *map, int idx) | |
1931 | { | |
1932 | return perf_env__get_socket(map, idx, &perf_stat.session->header.env); | |
1933 | } | |
1934 | ||
1935 | static int perf_stat__get_core_file(struct cpu_map *map, int idx) | |
1936 | { | |
1937 | return perf_env__get_core(map, idx, &perf_stat.session->header.env); | |
1938 | } | |
1939 | ||
1940 | static int perf_stat_init_aggr_mode_file(struct perf_stat *st) | |
1941 | { | |
1942 | struct perf_env *env = &st->session->header.env; | |
1943 | ||
1944 | switch (stat_config.aggr_mode) { | |
1945 | case AGGR_SOCKET: | |
1946 | if (perf_env__build_socket_map(env, evsel_list->cpus, &aggr_map)) { | |
1947 | perror("cannot build socket map"); | |
1948 | return -1; | |
1949 | } | |
1950 | aggr_get_id = perf_stat__get_socket_file; | |
1951 | break; | |
1952 | case AGGR_CORE: | |
1953 | if (perf_env__build_core_map(env, evsel_list->cpus, &aggr_map)) { | |
1954 | perror("cannot build core map"); | |
1955 | return -1; | |
1956 | } | |
1957 | aggr_get_id = perf_stat__get_core_file; | |
1958 | break; | |
1959 | case AGGR_NONE: | |
1960 | case AGGR_GLOBAL: | |
1961 | case AGGR_THREAD: | |
1962 | case AGGR_UNSET: | |
1963 | default: | |
1964 | break; | |
1965 | } | |
1966 | ||
1967 | return 0; | |
1968 | } | |
1969 | ||
44b1e60a AK |
1970 | static int topdown_filter_events(const char **attr, char **str, bool use_group) |
1971 | { | |
1972 | int off = 0; | |
1973 | int i; | |
1974 | int len = 0; | |
1975 | char *s; | |
1976 | ||
1977 | for (i = 0; attr[i]; i++) { | |
1978 | if (pmu_have_event("cpu", attr[i])) { | |
1979 | len += strlen(attr[i]) + 1; | |
1980 | attr[i - off] = attr[i]; | |
1981 | } else | |
1982 | off++; | |
1983 | } | |
1984 | attr[i - off] = NULL; | |
1985 | ||
1986 | *str = malloc(len + 1 + 2); | |
1987 | if (!*str) | |
1988 | return -1; | |
1989 | s = *str; | |
1990 | if (i - off == 0) { | |
1991 | *s = 0; | |
1992 | return 0; | |
1993 | } | |
1994 | if (use_group) | |
1995 | *s++ = '{'; | |
1996 | for (i = 0; attr[i]; i++) { | |
1997 | strcpy(s, attr[i]); | |
1998 | s += strlen(s); | |
1999 | *s++ = ','; | |
2000 | } | |
2001 | if (use_group) { | |
2002 | s[-1] = '}'; | |
2003 | *s = 0; | |
2004 | } else | |
2005 | s[-1] = 0; | |
2006 | return 0; | |
2007 | } | |
2008 | ||
2009 | __weak bool arch_topdown_check_group(bool *warn) | |
2010 | { | |
2011 | *warn = false; | |
2012 | return false; | |
2013 | } | |
2014 | ||
2015 | __weak void arch_topdown_group_warn(void) | |
2016 | { | |
2017 | } | |
2018 | ||
2cba3ffb IM |
2019 | /* |
2020 | * Add default attributes, if there were no attributes specified or | |
2021 | * if -d/--detailed, -d -d or -d -d -d is used: | |
2022 | */ | |
2023 | static int add_default_attributes(void) | |
2024 | { | |
44b1e60a | 2025 | int err; |
9dec4473 | 2026 | struct perf_event_attr default_attrs0[] = { |
b070a547 ACM |
2027 | |
2028 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK }, | |
2029 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES }, | |
2030 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS }, | |
2031 | { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS }, | |
2032 | ||
2033 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES }, | |
9dec4473 AK |
2034 | }; |
2035 | struct perf_event_attr frontend_attrs[] = { | |
b070a547 | 2036 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND }, |
9dec4473 AK |
2037 | }; |
2038 | struct perf_event_attr backend_attrs[] = { | |
b070a547 | 2039 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND }, |
9dec4473 AK |
2040 | }; |
2041 | struct perf_event_attr default_attrs1[] = { | |
b070a547 ACM |
2042 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS }, |
2043 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, | |
2044 | { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES }, | |
2045 | ||
2046 | }; | |
2047 | ||
2048 | /* | |
2049 | * Detailed stats (-d), covering the L1 and last level data caches: | |
2050 | */ | |
2051 | struct perf_event_attr detailed_attrs[] = { | |
2052 | ||
2053 | { .type = PERF_TYPE_HW_CACHE, | |
2054 | .config = | |
2055 | PERF_COUNT_HW_CACHE_L1D << 0 | | |
2056 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
2057 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
2058 | ||
2059 | { .type = PERF_TYPE_HW_CACHE, | |
2060 | .config = | |
2061 | PERF_COUNT_HW_CACHE_L1D << 0 | | |
2062 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
2063 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
2064 | ||
2065 | { .type = PERF_TYPE_HW_CACHE, | |
2066 | .config = | |
2067 | PERF_COUNT_HW_CACHE_LL << 0 | | |
2068 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
2069 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
2070 | ||
2071 | { .type = PERF_TYPE_HW_CACHE, | |
2072 | .config = | |
2073 | PERF_COUNT_HW_CACHE_LL << 0 | | |
2074 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
2075 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
2076 | }; | |
2077 | ||
2078 | /* | |
2079 | * Very detailed stats (-d -d), covering the instruction cache and the TLB caches: | |
2080 | */ | |
2081 | struct perf_event_attr very_detailed_attrs[] = { | |
2082 | ||
2083 | { .type = PERF_TYPE_HW_CACHE, | |
2084 | .config = | |
2085 | PERF_COUNT_HW_CACHE_L1I << 0 | | |
2086 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
2087 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
2088 | ||
2089 | { .type = PERF_TYPE_HW_CACHE, | |
2090 | .config = | |
2091 | PERF_COUNT_HW_CACHE_L1I << 0 | | |
2092 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
2093 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
2094 | ||
2095 | { .type = PERF_TYPE_HW_CACHE, | |
2096 | .config = | |
2097 | PERF_COUNT_HW_CACHE_DTLB << 0 | | |
2098 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
2099 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
2100 | ||
2101 | { .type = PERF_TYPE_HW_CACHE, | |
2102 | .config = | |
2103 | PERF_COUNT_HW_CACHE_DTLB << 0 | | |
2104 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
2105 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
2106 | ||
2107 | { .type = PERF_TYPE_HW_CACHE, | |
2108 | .config = | |
2109 | PERF_COUNT_HW_CACHE_ITLB << 0 | | |
2110 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
2111 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
2112 | ||
2113 | { .type = PERF_TYPE_HW_CACHE, | |
2114 | .config = | |
2115 | PERF_COUNT_HW_CACHE_ITLB << 0 | | |
2116 | (PERF_COUNT_HW_CACHE_OP_READ << 8) | | |
2117 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
2118 | ||
2119 | }; | |
2120 | ||
2121 | /* | |
2122 | * Very, very detailed stats (-d -d -d), adding prefetch events: | |
2123 | */ | |
2124 | struct perf_event_attr very_very_detailed_attrs[] = { | |
2125 | ||
2126 | { .type = PERF_TYPE_HW_CACHE, | |
2127 | .config = | |
2128 | PERF_COUNT_HW_CACHE_L1D << 0 | | |
2129 | (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) | | |
2130 | (PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16) }, | |
2131 | ||
2132 | { .type = PERF_TYPE_HW_CACHE, | |
2133 | .config = | |
2134 | PERF_COUNT_HW_CACHE_L1D << 0 | | |
2135 | (PERF_COUNT_HW_CACHE_OP_PREFETCH << 8) | | |
2136 | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16) }, | |
2137 | }; | |
2138 | ||
2cba3ffb IM |
2139 | /* Set attrs if no event is selected and !null_run: */ |
2140 | if (null_run) | |
2141 | return 0; | |
2142 | ||
4cabc3d1 | 2143 | if (transaction_run) { |
4cabc3d1 AK |
2144 | if (pmu_have_event("cpu", "cycles-ct") && |
2145 | pmu_have_event("cpu", "el-start")) | |
a454742c | 2146 | err = parse_events(evsel_list, transaction_attrs, NULL); |
4cabc3d1 | 2147 | else |
a454742c JO |
2148 | err = parse_events(evsel_list, transaction_limited_attrs, NULL); |
2149 | if (err) { | |
4cabc3d1 AK |
2150 | fprintf(stderr, "Cannot set up transaction events\n"); |
2151 | return -1; | |
2152 | } | |
2153 | return 0; | |
2154 | } | |
2155 | ||
44b1e60a AK |
2156 | if (topdown_run) { |
2157 | char *str = NULL; | |
2158 | bool warn = false; | |
2159 | ||
2160 | if (stat_config.aggr_mode != AGGR_GLOBAL && | |
2161 | stat_config.aggr_mode != AGGR_CORE) { | |
2162 | pr_err("top down event configuration requires --per-core mode\n"); | |
2163 | return -1; | |
2164 | } | |
2165 | stat_config.aggr_mode = AGGR_CORE; | |
2166 | if (nr_cgroups || !target__has_cpu(&target)) { | |
2167 | pr_err("top down event configuration requires system-wide mode (-a)\n"); | |
2168 | return -1; | |
2169 | } | |
2170 | ||
2171 | if (!force_metric_only) | |
2172 | metric_only = true; | |
2173 | if (topdown_filter_events(topdown_attrs, &str, | |
2174 | arch_topdown_check_group(&warn)) < 0) { | |
2175 | pr_err("Out of memory\n"); | |
2176 | return -1; | |
2177 | } | |
2178 | if (topdown_attrs[0] && str) { | |
2179 | if (warn) | |
2180 | arch_topdown_group_warn(); | |
2181 | err = parse_events(evsel_list, str, NULL); | |
2182 | if (err) { | |
2183 | fprintf(stderr, | |
2184 | "Cannot set up top down events %s: %d\n", | |
2185 | str, err); | |
2186 | free(str); | |
2187 | return -1; | |
2188 | } | |
2189 | } else { | |
2190 | fprintf(stderr, "System does not support topdown\n"); | |
2191 | return -1; | |
2192 | } | |
2193 | free(str); | |
2194 | } | |
2195 | ||
2cba3ffb | 2196 | if (!evsel_list->nr_entries) { |
a1f3d567 NK |
2197 | if (target__has_cpu(&target)) |
2198 | default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK; | |
2199 | ||
9dec4473 AK |
2200 | if (perf_evlist__add_default_attrs(evsel_list, default_attrs0) < 0) |
2201 | return -1; | |
2202 | if (pmu_have_event("cpu", "stalled-cycles-frontend")) { | |
2203 | if (perf_evlist__add_default_attrs(evsel_list, | |
2204 | frontend_attrs) < 0) | |
2205 | return -1; | |
2206 | } | |
2207 | if (pmu_have_event("cpu", "stalled-cycles-backend")) { | |
2208 | if (perf_evlist__add_default_attrs(evsel_list, | |
2209 | backend_attrs) < 0) | |
2210 | return -1; | |
2211 | } | |
2212 | if (perf_evlist__add_default_attrs(evsel_list, default_attrs1) < 0) | |
50d08e47 | 2213 | return -1; |
2cba3ffb IM |
2214 | } |
2215 | ||
2216 | /* Detailed events get appended to the event list: */ | |
2217 | ||
2218 | if (detailed_run < 1) | |
2219 | return 0; | |
2220 | ||
2221 | /* Append detailed run extra attributes: */ | |
79695e1b | 2222 | if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0) |
50d08e47 | 2223 | return -1; |
2cba3ffb IM |
2224 | |
2225 | if (detailed_run < 2) | |
2226 | return 0; | |
2227 | ||
2228 | /* Append very detailed run extra attributes: */ | |
79695e1b | 2229 | if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0) |
50d08e47 | 2230 | return -1; |
2cba3ffb IM |
2231 | |
2232 | if (detailed_run < 3) | |
2233 | return 0; | |
2234 | ||
2235 | /* Append very, very detailed run extra attributes: */ | |
79695e1b | 2236 | return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs); |
2cba3ffb IM |
2237 | } |
2238 | ||
8a59f3cc | 2239 | static const char * const stat_record_usage[] = { |
4979d0c7 JO |
2240 | "perf stat record [<options>]", |
2241 | NULL, | |
2242 | }; | |
2243 | ||
3ba78bd0 JO |
2244 | static void init_features(struct perf_session *session) |
2245 | { | |
2246 | int feat; | |
2247 | ||
2248 | for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) | |
2249 | perf_header__set_feat(&session->header, feat); | |
2250 | ||
2251 | perf_header__clear_feat(&session->header, HEADER_BUILD_ID); | |
2252 | perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); | |
2253 | perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); | |
2254 | perf_header__clear_feat(&session->header, HEADER_AUXTRACE); | |
2255 | } | |
2256 | ||
4979d0c7 JO |
2257 | static int __cmd_record(int argc, const char **argv) |
2258 | { | |
2259 | struct perf_session *session; | |
2260 | struct perf_data_file *file = &perf_stat.file; | |
2261 | ||
8a59f3cc | 2262 | argc = parse_options(argc, argv, stat_options, stat_record_usage, |
4979d0c7 JO |
2263 | PARSE_OPT_STOP_AT_NON_OPTION); |
2264 | ||
2265 | if (output_name) | |
2266 | file->path = output_name; | |
2267 | ||
e9d6db8e JO |
2268 | if (run_count != 1 || forever) { |
2269 | pr_err("Cannot use -r option with perf stat record.\n"); | |
2270 | return -1; | |
2271 | } | |
2272 | ||
4979d0c7 JO |
2273 | session = perf_session__new(file, false, NULL); |
2274 | if (session == NULL) { | |
2275 | pr_err("Perf session creation failed.\n"); | |
2276 | return -1; | |
2277 | } | |
2278 | ||
3ba78bd0 JO |
2279 | init_features(session); |
2280 | ||
4979d0c7 JO |
2281 | session->evlist = evsel_list; |
2282 | perf_stat.session = session; | |
2283 | perf_stat.record = true; | |
2284 | return argc; | |
2285 | } | |
2286 | ||
a56f9390 JO |
2287 | static int process_stat_round_event(struct perf_tool *tool __maybe_unused, |
2288 | union perf_event *event, | |
2289 | struct perf_session *session) | |
2290 | { | |
e3b03b6c | 2291 | struct stat_round_event *stat_round = &event->stat_round; |
a56f9390 JO |
2292 | struct perf_evsel *counter; |
2293 | struct timespec tsh, *ts = NULL; | |
2294 | const char **argv = session->header.env.cmdline_argv; | |
2295 | int argc = session->header.env.nr_cmdline; | |
2296 | ||
e5cadb93 | 2297 | evlist__for_each_entry(evsel_list, counter) |
a56f9390 JO |
2298 | perf_stat_process_counter(&stat_config, counter); |
2299 | ||
e3b03b6c AK |
2300 | if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL) |
2301 | update_stats(&walltime_nsecs_stats, stat_round->time); | |
a56f9390 | 2302 | |
e3b03b6c | 2303 | if (stat_config.interval && stat_round->time) { |
bd48c63e ACM |
2304 | tsh.tv_sec = stat_round->time / NSEC_PER_SEC; |
2305 | tsh.tv_nsec = stat_round->time % NSEC_PER_SEC; | |
a56f9390 JO |
2306 | ts = &tsh; |
2307 | } | |
2308 | ||
2309 | print_counters(ts, argc, argv); | |
2310 | return 0; | |
2311 | } | |
2312 | ||
62ba18ba | 2313 | static |
7e6a7998 | 2314 | int process_stat_config_event(struct perf_tool *tool, |
62ba18ba JO |
2315 | union perf_event *event, |
2316 | struct perf_session *session __maybe_unused) | |
2317 | { | |
68d702f7 JO |
2318 | struct perf_stat *st = container_of(tool, struct perf_stat, tool); |
2319 | ||
62ba18ba | 2320 | perf_event__read_stat_config(&stat_config, &event->stat_config); |
68d702f7 | 2321 | |
89af4e05 JO |
2322 | if (cpu_map__empty(st->cpus)) { |
2323 | if (st->aggr_mode != AGGR_UNSET) | |
2324 | pr_warning("warning: processing task data, aggregation mode not set\n"); | |
2325 | return 0; | |
2326 | } | |
2327 | ||
2328 | if (st->aggr_mode != AGGR_UNSET) | |
2329 | stat_config.aggr_mode = st->aggr_mode; | |
2330 | ||
68d702f7 JO |
2331 | if (perf_stat.file.is_pipe) |
2332 | perf_stat_init_aggr_mode(); | |
2333 | else | |
2334 | perf_stat_init_aggr_mode_file(st); | |
2335 | ||
62ba18ba JO |
2336 | return 0; |
2337 | } | |
2338 | ||
1975d36e JO |
2339 | static int set_maps(struct perf_stat *st) |
2340 | { | |
2341 | if (!st->cpus || !st->threads) | |
2342 | return 0; | |
2343 | ||
2344 | if (WARN_ONCE(st->maps_allocated, "stats double allocation\n")) | |
2345 | return -EINVAL; | |
2346 | ||
2347 | perf_evlist__set_maps(evsel_list, st->cpus, st->threads); | |
2348 | ||
2349 | if (perf_evlist__alloc_stats(evsel_list, true)) | |
2350 | return -ENOMEM; | |
2351 | ||
2352 | st->maps_allocated = true; | |
2353 | return 0; | |
2354 | } | |
2355 | ||
2356 | static | |
7e6a7998 | 2357 | int process_thread_map_event(struct perf_tool *tool, |
1975d36e JO |
2358 | union perf_event *event, |
2359 | struct perf_session *session __maybe_unused) | |
2360 | { | |
2361 | struct perf_stat *st = container_of(tool, struct perf_stat, tool); | |
2362 | ||
2363 | if (st->threads) { | |
2364 | pr_warning("Extra thread map event, ignoring.\n"); | |
2365 | return 0; | |
2366 | } | |
2367 | ||
2368 | st->threads = thread_map__new_event(&event->thread_map); | |
2369 | if (!st->threads) | |
2370 | return -ENOMEM; | |
2371 | ||
2372 | return set_maps(st); | |
2373 | } | |
2374 | ||
2375 | static | |
7e6a7998 | 2376 | int process_cpu_map_event(struct perf_tool *tool, |
1975d36e JO |
2377 | union perf_event *event, |
2378 | struct perf_session *session __maybe_unused) | |
2379 | { | |
2380 | struct perf_stat *st = container_of(tool, struct perf_stat, tool); | |
2381 | struct cpu_map *cpus; | |
2382 | ||
2383 | if (st->cpus) { | |
2384 | pr_warning("Extra cpu map event, ignoring.\n"); | |
2385 | return 0; | |
2386 | } | |
2387 | ||
2388 | cpus = cpu_map__new_data(&event->cpu_map.data); | |
2389 | if (!cpus) | |
2390 | return -ENOMEM; | |
2391 | ||
2392 | st->cpus = cpus; | |
2393 | return set_maps(st); | |
2394 | } | |
2395 | ||
8a59f3cc | 2396 | static const char * const stat_report_usage[] = { |
ba6039b6 JO |
2397 | "perf stat report [<options>]", |
2398 | NULL, | |
2399 | }; | |
2400 | ||
2401 | static struct perf_stat perf_stat = { | |
2402 | .tool = { | |
2403 | .attr = perf_event__process_attr, | |
fa6ea781 | 2404 | .event_update = perf_event__process_event_update, |
1975d36e JO |
2405 | .thread_map = process_thread_map_event, |
2406 | .cpu_map = process_cpu_map_event, | |
62ba18ba | 2407 | .stat_config = process_stat_config_event, |
a56f9390 JO |
2408 | .stat = perf_event__process_stat_event, |
2409 | .stat_round = process_stat_round_event, | |
ba6039b6 | 2410 | }, |
89af4e05 | 2411 | .aggr_mode = AGGR_UNSET, |
ba6039b6 JO |
2412 | }; |
2413 | ||
2414 | static int __cmd_report(int argc, const char **argv) | |
2415 | { | |
2416 | struct perf_session *session; | |
2417 | const struct option options[] = { | |
2418 | OPT_STRING('i', "input", &input_name, "file", "input file name"), | |
89af4e05 JO |
2419 | OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode, |
2420 | "aggregate counts per processor socket", AGGR_SOCKET), | |
2421 | OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode, | |
2422 | "aggregate counts per physical processor core", AGGR_CORE), | |
2423 | OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode, | |
2424 | "disable CPU count aggregation", AGGR_NONE), | |
ba6039b6 JO |
2425 | OPT_END() |
2426 | }; | |
2427 | struct stat st; | |
2428 | int ret; | |
2429 | ||
8a59f3cc | 2430 | argc = parse_options(argc, argv, options, stat_report_usage, 0); |
ba6039b6 JO |
2431 | |
2432 | if (!input_name || !strlen(input_name)) { | |
2433 | if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode)) | |
2434 | input_name = "-"; | |
2435 | else | |
2436 | input_name = "perf.data"; | |
2437 | } | |
2438 | ||
2439 | perf_stat.file.path = input_name; | |
2440 | perf_stat.file.mode = PERF_DATA_MODE_READ; | |
2441 | ||
2442 | session = perf_session__new(&perf_stat.file, false, &perf_stat.tool); | |
2443 | if (session == NULL) | |
2444 | return -1; | |
2445 | ||
2446 | perf_stat.session = session; | |
2447 | stat_config.output = stderr; | |
2448 | evsel_list = session->evlist; | |
2449 | ||
2450 | ret = perf_session__process_events(session); | |
2451 | if (ret) | |
2452 | return ret; | |
2453 | ||
2454 | perf_session__delete(session); | |
2455 | return 0; | |
2456 | } | |
2457 | ||
e3ba76de JO |
2458 | static void setup_system_wide(int forks) |
2459 | { | |
2460 | /* | |
2461 | * Make system wide (-a) the default target if | |
2462 | * no target was specified and one of following | |
2463 | * conditions is met: | |
2464 | * | |
2465 | * - there's no workload specified | |
2466 | * - there is workload specified but all requested | |
2467 | * events are system wide events | |
2468 | */ | |
2469 | if (!target__none(&target)) | |
2470 | return; | |
2471 | ||
2472 | if (!forks) | |
2473 | target.system_wide = true; | |
2474 | else { | |
2475 | struct perf_evsel *counter; | |
2476 | ||
2477 | evlist__for_each_entry(evsel_list, counter) { | |
2478 | if (!counter->system_wide) | |
2479 | return; | |
2480 | } | |
2481 | ||
2482 | if (evsel_list->nr_entries) | |
2483 | target.system_wide = true; | |
2484 | } | |
2485 | } | |
2486 | ||
b0ad8ea6 | 2487 | int cmd_stat(int argc, const char **argv) |
5242519b | 2488 | { |
b070a547 ACM |
2489 | const char * const stat_usage[] = { |
2490 | "perf stat [<options>] [<command>]", | |
2491 | NULL | |
2492 | }; | |
cc03c542 | 2493 | int status = -EINVAL, run_idx; |
4aa9015f | 2494 | const char *mode; |
5821522e | 2495 | FILE *output = stderr; |
ec0d3d1f | 2496 | unsigned int interval; |
ba6039b6 | 2497 | const char * const stat_subcommands[] = { "record", "report" }; |
42202dd5 | 2498 | |
5af52b51 SE |
2499 | setlocale(LC_ALL, ""); |
2500 | ||
334fe7a3 | 2501 | evsel_list = perf_evlist__new(); |
361c99a6 ACM |
2502 | if (evsel_list == NULL) |
2503 | return -ENOMEM; | |
2504 | ||
1669e509 | 2505 | parse_events__shrink_config_terms(); |
4979d0c7 JO |
2506 | argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands, |
2507 | (const char **) stat_usage, | |
2508 | PARSE_OPT_STOP_AT_NON_OPTION); | |
37932c18 | 2509 | perf_stat__collect_metric_expr(evsel_list); |
fb4605ba | 2510 | perf_stat__init_shadow_stats(); |
4979d0c7 | 2511 | |
6edb78a2 JO |
2512 | if (csv_sep) { |
2513 | csv_output = true; | |
2514 | if (!strcmp(csv_sep, "\\t")) | |
2515 | csv_sep = "\t"; | |
2516 | } else | |
2517 | csv_sep = DEFAULT_SEPARATOR; | |
2518 | ||
4979d0c7 JO |
2519 | if (argc && !strncmp(argv[0], "rec", 3)) { |
2520 | argc = __cmd_record(argc, argv); | |
2521 | if (argc < 0) | |
2522 | return -1; | |
ba6039b6 JO |
2523 | } else if (argc && !strncmp(argv[0], "rep", 3)) |
2524 | return __cmd_report(argc, argv); | |
d7470b6a | 2525 | |
ec0d3d1f JO |
2526 | interval = stat_config.interval; |
2527 | ||
4979d0c7 JO |
2528 | /* |
2529 | * For record command the -o is already taken care of. | |
2530 | */ | |
2531 | if (!STAT_RECORD && output_name && strcmp(output_name, "-")) | |
4aa9015f SE |
2532 | output = NULL; |
2533 | ||
56f3bae7 JC |
2534 | if (output_name && output_fd) { |
2535 | fprintf(stderr, "cannot use both --output and --log-fd\n"); | |
e0547311 JO |
2536 | parse_options_usage(stat_usage, stat_options, "o", 1); |
2537 | parse_options_usage(NULL, stat_options, "log-fd", 0); | |
cc03c542 | 2538 | goto out; |
56f3bae7 | 2539 | } |
fc3e4d07 | 2540 | |
54b50916 AK |
2541 | if (metric_only && stat_config.aggr_mode == AGGR_THREAD) { |
2542 | fprintf(stderr, "--metric-only is not supported with --per-thread\n"); | |
2543 | goto out; | |
2544 | } | |
2545 | ||
54b50916 AK |
2546 | if (metric_only && run_count > 1) { |
2547 | fprintf(stderr, "--metric-only is not supported with -r\n"); | |
2548 | goto out; | |
2549 | } | |
2550 | ||
fc3e4d07 SE |
2551 | if (output_fd < 0) { |
2552 | fprintf(stderr, "argument to --log-fd must be a > 0\n"); | |
e0547311 | 2553 | parse_options_usage(stat_usage, stat_options, "log-fd", 0); |
cc03c542 | 2554 | goto out; |
fc3e4d07 SE |
2555 | } |
2556 | ||
4aa9015f SE |
2557 | if (!output) { |
2558 | struct timespec tm; | |
2559 | mode = append_file ? "a" : "w"; | |
2560 | ||
2561 | output = fopen(output_name, mode); | |
2562 | if (!output) { | |
2563 | perror("failed to create output file"); | |
fceda7fe | 2564 | return -1; |
4aa9015f SE |
2565 | } |
2566 | clock_gettime(CLOCK_REALTIME, &tm); | |
2567 | fprintf(output, "# started on %s\n", ctime(&tm.tv_sec)); | |
fc3e4d07 | 2568 | } else if (output_fd > 0) { |
56f3bae7 JC |
2569 | mode = append_file ? "a" : "w"; |
2570 | output = fdopen(output_fd, mode); | |
2571 | if (!output) { | |
2572 | perror("Failed opening logfd"); | |
2573 | return -errno; | |
2574 | } | |
4aa9015f SE |
2575 | } |
2576 | ||
5821522e JO |
2577 | stat_config.output = output; |
2578 | ||
d7470b6a SE |
2579 | /* |
2580 | * let the spreadsheet do the pretty-printing | |
2581 | */ | |
2582 | if (csv_output) { | |
61a9f324 | 2583 | /* User explicitly passed -B? */ |
d7470b6a SE |
2584 | if (big_num_opt == 1) { |
2585 | fprintf(stderr, "-B option not supported with -x\n"); | |
e0547311 JO |
2586 | parse_options_usage(stat_usage, stat_options, "B", 1); |
2587 | parse_options_usage(NULL, stat_options, "x", 1); | |
cc03c542 | 2588 | goto out; |
d7470b6a SE |
2589 | } else /* Nope, so disable big number formatting */ |
2590 | big_num = false; | |
2591 | } else if (big_num_opt == 0) /* User passed --no-big-num */ | |
2592 | big_num = false; | |
2593 | ||
e3ba76de | 2594 | setup_system_wide(argc); |
ac3063bd | 2595 | |
a7e191c3 | 2596 | if (run_count < 0) { |
cc03c542 | 2597 | pr_err("Run count must be a positive number\n"); |
e0547311 | 2598 | parse_options_usage(stat_usage, stat_options, "r", 1); |
cc03c542 | 2599 | goto out; |
a7e191c3 FD |
2600 | } else if (run_count == 0) { |
2601 | forever = true; | |
2602 | run_count = 1; | |
2603 | } | |
ddcacfa0 | 2604 | |
421a50f3 | 2605 | if ((stat_config.aggr_mode == AGGR_THREAD) && !target__has_task(&target)) { |
32b8af82 JO |
2606 | fprintf(stderr, "The --per-thread option is only available " |
2607 | "when monitoring via -p -t options.\n"); | |
e0547311 JO |
2608 | parse_options_usage(NULL, stat_options, "p", 1); |
2609 | parse_options_usage(NULL, stat_options, "t", 1); | |
32b8af82 JO |
2610 | goto out; |
2611 | } | |
2612 | ||
2613 | /* | |
2614 | * no_aggr, cgroup are for system-wide only | |
2615 | * --per-thread is aggregated per thread, we dont mix it with cpu mode | |
2616 | */ | |
421a50f3 JO |
2617 | if (((stat_config.aggr_mode != AGGR_GLOBAL && |
2618 | stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) && | |
602ad878 | 2619 | !target__has_cpu(&target)) { |
023695d9 SE |
2620 | fprintf(stderr, "both cgroup and no-aggregation " |
2621 | "modes only available in system-wide mode\n"); | |
2622 | ||
e0547311 JO |
2623 | parse_options_usage(stat_usage, stat_options, "G", 1); |
2624 | parse_options_usage(NULL, stat_options, "A", 1); | |
2625 | parse_options_usage(NULL, stat_options, "a", 1); | |
cc03c542 | 2626 | goto out; |
d7e7a451 SE |
2627 | } |
2628 | ||
2cba3ffb IM |
2629 | if (add_default_attributes()) |
2630 | goto out; | |
ddcacfa0 | 2631 | |
602ad878 | 2632 | target__validate(&target); |
5c98d466 | 2633 | |
77a6f014 | 2634 | if (perf_evlist__create_maps(evsel_list, &target) < 0) { |
602ad878 | 2635 | if (target__has_task(&target)) { |
77a6f014 | 2636 | pr_err("Problems finding threads of monitor\n"); |
e0547311 JO |
2637 | parse_options_usage(stat_usage, stat_options, "p", 1); |
2638 | parse_options_usage(NULL, stat_options, "t", 1); | |
602ad878 | 2639 | } else if (target__has_cpu(&target)) { |
77a6f014 | 2640 | perror("failed to parse CPUs map"); |
e0547311 JO |
2641 | parse_options_usage(stat_usage, stat_options, "C", 1); |
2642 | parse_options_usage(NULL, stat_options, "a", 1); | |
cc03c542 NK |
2643 | } |
2644 | goto out; | |
60d567e2 | 2645 | } |
32b8af82 JO |
2646 | |
2647 | /* | |
2648 | * Initialize thread_map with comm names, | |
2649 | * so we could print it out on output. | |
2650 | */ | |
421a50f3 | 2651 | if (stat_config.aggr_mode == AGGR_THREAD) |
32b8af82 JO |
2652 | thread_map__read_comms(evsel_list->threads); |
2653 | ||
13370a9b | 2654 | if (interval && interval < 100) { |
19afd104 KL |
2655 | if (interval < 10) { |
2656 | pr_err("print interval must be >= 10ms\n"); | |
e0547311 | 2657 | parse_options_usage(stat_usage, stat_options, "I", 1); |
19afd104 KL |
2658 | goto out; |
2659 | } else | |
2660 | pr_warning("print interval < 100ms. " | |
2661 | "The overhead percentage could be high in some cases. " | |
2662 | "Please proceed with caution.\n"); | |
13370a9b | 2663 | } |
c45c6ea2 | 2664 | |
d134ffb9 | 2665 | if (perf_evlist__alloc_stats(evsel_list, interval)) |
03ad9747 | 2666 | goto out; |
d6d901c2 | 2667 | |
86ee6e18 | 2668 | if (perf_stat_init_aggr_mode()) |
03ad9747 | 2669 | goto out; |
86ee6e18 | 2670 | |
58d7e993 IM |
2671 | /* |
2672 | * We dont want to block the signals - that would cause | |
2673 | * child tasks to inherit that and Ctrl-C would not work. | |
2674 | * What we want is for Ctrl-C to work in the exec()-ed | |
2675 | * task, but being ignored by perf stat itself: | |
2676 | */ | |
f7b7c26e | 2677 | atexit(sig_atexit); |
a7e191c3 FD |
2678 | if (!forever) |
2679 | signal(SIGINT, skip_signal); | |
13370a9b | 2680 | signal(SIGCHLD, skip_signal); |
58d7e993 IM |
2681 | signal(SIGALRM, skip_signal); |
2682 | signal(SIGABRT, skip_signal); | |
2683 | ||
42202dd5 | 2684 | status = 0; |
a7e191c3 | 2685 | for (run_idx = 0; forever || run_idx < run_count; run_idx++) { |
bb963e16 | 2686 | if (run_count != 1 && verbose > 0) |
4aa9015f SE |
2687 | fprintf(output, "[ perf stat: executing run #%d ... ]\n", |
2688 | run_idx + 1); | |
f9cef0a9 | 2689 | |
42202dd5 | 2690 | status = run_perf_stat(argc, argv); |
a7e191c3 | 2691 | if (forever && status != -1) { |
d4f63a47 | 2692 | print_counters(NULL, argc, argv); |
254ecbc7 | 2693 | perf_stat__reset_stats(); |
a7e191c3 | 2694 | } |
42202dd5 IM |
2695 | } |
2696 | ||
a7e191c3 | 2697 | if (!forever && status != -1 && !interval) |
d4f63a47 | 2698 | print_counters(NULL, argc, argv); |
d134ffb9 | 2699 | |
4979d0c7 JO |
2700 | if (STAT_RECORD) { |
2701 | /* | |
2702 | * We synthesize the kernel mmap record just so that older tools | |
2703 | * don't emit warnings about not being able to resolve symbols | |
2704 | * due to /proc/sys/kernel/kptr_restrict settings and instear provide | |
2705 | * a saner message about no samples being in the perf.data file. | |
2706 | * | |
2707 | * This also serves to suppress a warning about f_header.data.size == 0 | |
8b99b1a4 JO |
2708 | * in header.c at the moment 'perf stat record' gets introduced, which |
2709 | * is not really needed once we start adding the stat specific PERF_RECORD_ | |
2710 | * records, but the need to suppress the kptr_restrict messages in older | |
2711 | * tools remain -acme | |
4979d0c7 JO |
2712 | */ |
2713 | int fd = perf_data_file__fd(&perf_stat.file); | |
2714 | int err = perf_event__synthesize_kernel_mmap((void *)&perf_stat, | |
2715 | process_synthesized_event, | |
2716 | &perf_stat.session->machines.host); | |
2717 | if (err) { | |
2718 | pr_warning("Couldn't synthesize the kernel mmap record, harmless, " | |
2719 | "older tools may produce warnings about this file\n."); | |
2720 | } | |
2721 | ||
7aad0c32 JO |
2722 | if (!interval) { |
2723 | if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL)) | |
2724 | pr_err("failed to write stat round event\n"); | |
2725 | } | |
2726 | ||
664c98d4 JO |
2727 | if (!perf_stat.file.is_pipe) { |
2728 | perf_stat.session->header.data_size += perf_stat.bytes_written; | |
2729 | perf_session__write_header(perf_stat.session, evsel_list, fd, true); | |
2730 | } | |
4979d0c7 JO |
2731 | |
2732 | perf_session__delete(perf_stat.session); | |
2733 | } | |
2734 | ||
544c2ae7 | 2735 | perf_stat__exit_aggr_mode(); |
d134ffb9 | 2736 | perf_evlist__free_stats(evsel_list); |
0015e2e1 ACM |
2737 | out: |
2738 | perf_evlist__delete(evsel_list); | |
42202dd5 | 2739 | return status; |
ddcacfa0 | 2740 | } |