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