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