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