xref: /openbmc/linux/tools/perf/builtin-stat.c (revision 6396bb221514d2876fd6dc0aa2a1f240d99b37bb)
1 /*
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:
8 
9    $ perf stat ./hackbench 10
10 
11   Time: 0.118
12 
13   Performance counter stats for './hackbench 10':
14 
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
28 
29  *
30  * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
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>
39  *   Jaswinder Singh Rajput <jaswinder@kernel.org>
40  *
41  * Released under the GPL v2. (and only v2, not any later version)
42  */
43 
44 #include "perf.h"
45 #include "builtin.h"
46 #include "util/cgroup.h"
47 #include "util/util.h"
48 #include <subcmd/parse-options.h>
49 #include "util/parse-events.h"
50 #include "util/pmu.h"
51 #include "util/event.h"
52 #include "util/evlist.h"
53 #include "util/evsel.h"
54 #include "util/debug.h"
55 #include "util/drv_configs.h"
56 #include "util/color.h"
57 #include "util/stat.h"
58 #include "util/header.h"
59 #include "util/cpumap.h"
60 #include "util/thread.h"
61 #include "util/thread_map.h"
62 #include "util/counts.h"
63 #include "util/group.h"
64 #include "util/session.h"
65 #include "util/tool.h"
66 #include "util/string2.h"
67 #include "util/metricgroup.h"
68 #include "asm/bug.h"
69 
70 #include <linux/time64.h>
71 #include <api/fs/fs.h>
72 #include <errno.h>
73 #include <signal.h>
74 #include <stdlib.h>
75 #include <sys/prctl.h>
76 #include <inttypes.h>
77 #include <locale.h>
78 #include <math.h>
79 #include <sys/types.h>
80 #include <sys/stat.h>
81 #include <sys/wait.h>
82 #include <unistd.h>
83 #include <sys/time.h>
84 #include <sys/resource.h>
85 #include <sys/wait.h>
86 
87 #include "sane_ctype.h"
88 
89 #define DEFAULT_SEPARATOR	" "
90 #define CNTR_NOT_SUPPORTED	"<not supported>"
91 #define CNTR_NOT_COUNTED	"<not counted>"
92 #define FREEZE_ON_SMI_PATH	"devices/cpu/freeze_on_smi"
93 
94 static void print_counters(struct timespec *ts, int argc, const char **argv);
95 
96 /* Default events used for perf stat -T */
97 static const char *transaction_attrs = {
98 	"task-clock,"
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. */
110 static const char * transaction_limited_attrs = {
111 	"task-clock,"
112 	"{"
113 	"instructions,"
114 	"cycles,"
115 	"cpu/cycles-t/,"
116 	"cpu/tx-start/"
117 	"}"
118 };
119 
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 
129 static const char *smi_cost_attrs = {
130 	"{"
131 	"msr/aperf/,"
132 	"msr/smi/,"
133 	"cycles"
134 	"}"
135 };
136 
137 static struct perf_evlist	*evsel_list;
138 
139 static struct rblist		 metric_events;
140 
141 static struct target target = {
142 	.uid	= UINT_MAX,
143 };
144 
145 typedef int (*aggr_get_id_t)(struct cpu_map *m, int cpu);
146 
147 static int			run_count			=  1;
148 static bool			no_inherit			= false;
149 static volatile pid_t		child_pid			= -1;
150 static bool			null_run			=  false;
151 static int			detailed_run			=  0;
152 static bool			transaction_run;
153 static bool			topdown_run			= false;
154 static bool			smi_cost			= false;
155 static bool			smi_reset			= false;
156 static bool			big_num				=  true;
157 static int			big_num_opt			=  -1;
158 static const char		*csv_sep			= NULL;
159 static bool			csv_output			= false;
160 static bool			group				= false;
161 static const char		*pre_cmd			= NULL;
162 static const char		*post_cmd			= NULL;
163 static bool			sync_run			= false;
164 static unsigned int		initial_delay			= 0;
165 static unsigned int		unit_width			= 4; /* strlen("unit") */
166 static bool			forever				= false;
167 static bool			metric_only			= false;
168 static bool			force_metric_only		= false;
169 static bool			no_merge			= false;
170 static bool			walltime_run_table		= false;
171 static struct timespec		ref_time;
172 static struct cpu_map		*aggr_map;
173 static aggr_get_id_t		aggr_get_id;
174 static bool			append_file;
175 static bool			interval_count;
176 static const char		*output_name;
177 static int			output_fd;
178 static int			print_free_counters_hint;
179 static int			print_mixed_hw_group_error;
180 static u64			*walltime_run;
181 static bool			ru_display			= false;
182 static struct rusage		ru_data;
183 
184 struct perf_stat {
185 	bool			 record;
186 	struct perf_data	 data;
187 	struct perf_session	*session;
188 	u64			 bytes_written;
189 	struct perf_tool	 tool;
190 	bool			 maps_allocated;
191 	struct cpu_map		*cpus;
192 	struct thread_map	*threads;
193 	enum aggr_mode		 aggr_mode;
194 };
195 
196 static struct perf_stat		perf_stat;
197 #define STAT_RECORD		perf_stat.record
198 
199 static volatile int done = 0;
200 
201 static struct perf_stat_config stat_config = {
202 	.aggr_mode	= AGGR_GLOBAL,
203 	.scale		= true,
204 };
205 
206 static bool is_duration_time(struct perf_evsel *evsel)
207 {
208 	return !strcmp(evsel->name, "duration_time");
209 }
210 
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) {
216 		r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
217 		r->tv_sec--;
218 	} else {
219 		r->tv_nsec = a->tv_nsec - b->tv_nsec ;
220 	}
221 }
222 
223 static void perf_stat__reset_stats(void)
224 {
225 	int i;
226 
227 	perf_evlist__reset_stats(evsel_list);
228 	perf_stat__reset_shadow_stats();
229 
230 	for (i = 0; i < stat_config.stats_num; i++)
231 		perf_stat__reset_shadow_per_stat(&stat_config.stats[i]);
232 }
233 
234 static int create_perf_stat_counter(struct perf_evsel *evsel)
235 {
236 	struct perf_event_attr *attr = &evsel->attr;
237 	struct perf_evsel *leader = evsel->leader;
238 
239 	if (stat_config.scale) {
240 		attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
241 				    PERF_FORMAT_TOTAL_TIME_RUNNING;
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;
251 
252 	attr->inherit = !no_inherit;
253 
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;
259 
260 	/*
261 	 * But set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
262 	 * while avoiding that older tools show confusing messages.
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.
268 	 */
269 	if (!(STAT_RECORD && perf_stat.data.is_pipe))
270 		attr->sample_type = PERF_SAMPLE_IDENTIFIER;
271 
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 	 */
277 	if (perf_evsel__is_group_leader(evsel)) {
278 		attr->disabled = 1;
279 
280 		/*
281 		 * In case of initial_delay we enable tracee
282 		 * events manually.
283 		 */
284 		if (target__none(&target) && !initial_delay)
285 			attr->enable_on_exec = 1;
286 	}
287 
288 	if (target__has_cpu(&target) && !target__has_per_thread(&target))
289 		return perf_evsel__open_per_cpu(evsel, perf_evsel__cpus(evsel));
290 
291 	return perf_evsel__open_per_thread(evsel, evsel_list->threads);
292 }
293 
294 /*
295  * Does the counter have nsecs as a unit?
296  */
297 static inline int nsec_counter(struct perf_evsel *evsel)
298 {
299 	if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
300 	    perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
301 		return 1;
302 
303 	return 0;
304 }
305 
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)
310 {
311 	if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
312 		pr_err("failed to write perf data, error: %m\n");
313 		return -1;
314 	}
315 
316 	perf_stat.bytes_written += event->header.size;
317 	return 0;
318 }
319 
320 static int write_stat_round_event(u64 tm, u64 type)
321 {
322 	return perf_event__synthesize_stat_round(NULL, tm, type,
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 
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 
342 /*
343  * Read out the results of a single counter:
344  * do not aggregate counts across CPUs in system-wide mode
345  */
346 static int read_counter(struct perf_evsel *counter)
347 {
348 	int nthreads = thread_map__nr(evsel_list->threads);
349 	int ncpus, cpu, thread;
350 
351 	if (target__has_cpu(&target) && !target__has_per_thread(&target))
352 		ncpus = perf_evsel__nr_cpus(counter);
353 	else
354 		ncpus = 1;
355 
356 	if (!counter->supported)
357 		return -ENOENT;
358 
359 	if (counter->system_wide)
360 		nthreads = 1;
361 
362 	for (thread = 0; thread < nthreads; thread++) {
363 		for (cpu = 0; cpu < ncpus; cpu++) {
364 			struct perf_counts_values *count;
365 
366 			count = perf_counts(counter->counts, cpu, thread);
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)) {
374 				counter->counts->scaled = -1;
375 				perf_counts(counter->counts, cpu, thread)->ena = 0;
376 				perf_counts(counter->counts, cpu, thread)->run = 0;
377 				return -1;
378 			}
379 
380 			count->loaded = false;
381 
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 			}
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 			}
396 		}
397 	}
398 
399 	return 0;
400 }
401 
402 static void read_counters(void)
403 {
404 	struct perf_evsel *counter;
405 	int ret;
406 
407 	evlist__for_each_entry(evsel_list, counter) {
408 		ret = read_counter(counter);
409 		if (ret)
410 			pr_debug("failed to read counter %s\n", counter->name);
411 
412 		if (ret == 0 && perf_stat_process_counter(&stat_config, counter))
413 			pr_warning("failed to process counter %s\n", counter->name);
414 	}
415 }
416 
417 static void process_interval(void)
418 {
419 	struct timespec ts, rs;
420 
421 	read_counters();
422 
423 	clock_gettime(CLOCK_MONOTONIC, &ts);
424 	diff_timespec(&rs, &ts, &ref_time);
425 
426 	if (STAT_RECORD) {
427 		if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
428 			pr_err("failed to write stat round event\n");
429 	}
430 
431 	init_stats(&walltime_nsecs_stats);
432 	update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000);
433 	print_counters(&rs, 0, NULL);
434 }
435 
436 static void enable_counters(void)
437 {
438 	if (initial_delay)
439 		usleep(initial_delay * USEC_PER_MSEC);
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)
447 		perf_evlist__enable(evsel_list);
448 }
449 
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 
461 static volatile int workload_exec_errno;
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  */
468 static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
469 					void *ucontext __maybe_unused)
470 {
471 	workload_exec_errno = info->si_value.sival_int;
472 }
473 
474 static int perf_stat_synthesize_config(bool is_pipe)
475 {
476 	int err;
477 
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 
487 	err = perf_event__synthesize_extra_attr(NULL,
488 						evsel_list,
489 						process_synthesized_event,
490 						is_pipe);
491 
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 
517 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
518 
519 static int __store_counter_ids(struct perf_evsel *counter)
520 {
521 	int cpu, thread;
522 
523 	for (cpu = 0; cpu < xyarray__max_x(counter->fd); cpu++) {
524 		for (thread = 0; thread < xyarray__max_y(counter->fd);
525 		     thread++) {
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 
545 	return __store_counter_ids(counter);
546 }
547 
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 
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 
579 static int __run_perf_stat(int argc, const char **argv, int run_idx)
580 {
581 	int interval = stat_config.interval;
582 	int times = stat_config.times;
583 	int timeout = stat_config.timeout;
584 	char msg[BUFSIZ];
585 	unsigned long long t0, t1;
586 	struct perf_evsel *counter;
587 	struct timespec ts;
588 	size_t l;
589 	int status = 0;
590 	const bool forks = (argc > 0);
591 	bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
592 	struct perf_evsel_config_term *err_term;
593 
594 	if (interval) {
595 		ts.tv_sec  = interval / USEC_PER_MSEC;
596 		ts.tv_nsec = (interval % USEC_PER_MSEC) * NSEC_PER_MSEC;
597 	} else if (timeout) {
598 		ts.tv_sec  = timeout / USEC_PER_MSEC;
599 		ts.tv_nsec = (timeout % USEC_PER_MSEC) * NSEC_PER_MSEC;
600 	} else {
601 		ts.tv_sec  = 1;
602 		ts.tv_nsec = 0;
603 	}
604 
605 	if (forks) {
606 		if (perf_evlist__prepare_workload(evsel_list, &target, argv, is_pipe,
607 						  workload_exec_failed_signal) < 0) {
608 			perror("failed to prepare workload");
609 			return -1;
610 		}
611 		child_pid = evsel_list->workload.pid;
612 	}
613 
614 	if (group)
615 		perf_evlist__set_leader(evsel_list);
616 
617 	evlist__for_each_entry(evsel_list, counter) {
618 try_again:
619 		if (create_perf_stat_counter(counter) < 0) {
620 
621 			/* Weak group failed. Reset the group. */
622 			if ((errno == EINVAL || errno == EBADF) &&
623 			    counter->leader != counter &&
624 			    counter->weak_group) {
625 				counter = perf_evsel__reset_weak_group(counter);
626 				goto try_again;
627 			}
628 
629 			/*
630 			 * PPC returns ENXIO for HW counters until 2.6.37
631 			 * (behavior changed with commit b0a873e).
632 			 */
633 			if (errno == EINVAL || errno == ENOSYS ||
634 			    errno == ENOENT || errno == EOPNOTSUPP ||
635 			    errno == ENXIO) {
636 				if (verbose > 0)
637 					ui__warning("%s event is not supported by the kernel.\n",
638 						    perf_evsel__name(counter));
639 				counter->supported = false;
640 
641 				if ((counter->leader != counter) ||
642 				    !(counter->leader->nr_members > 1))
643 					continue;
644 			} else if (perf_evsel__fallback(counter, errno, msg, sizeof(msg))) {
645                                 if (verbose > 0)
646                                         ui__warning("%s\n", msg);
647                                 goto try_again;
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 			}
661 
662 			perf_evsel__open_strerror(counter, &target,
663 						  errno, msg, sizeof(msg));
664 			ui__error("%s\n", msg);
665 
666 			if (child_pid != -1)
667 				kill(child_pid, SIGTERM);
668 
669 			return -1;
670 		}
671 		counter->supported = true;
672 
673 		l = strlen(counter->unit);
674 		if (l > unit_width)
675 			unit_width = l;
676 
677 		if (perf_evsel__should_store_id(counter) &&
678 		    store_counter_ids(counter))
679 			return -1;
680 	}
681 
682 	if (perf_evlist__apply_filters(evsel_list, &counter)) {
683 		pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
684 			counter->filter, perf_evsel__name(counter), errno,
685 			str_error_r(errno, msg, sizeof(msg)));
686 		return -1;
687 	}
688 
689 	if (perf_evlist__apply_drv_configs(evsel_list, &counter, &err_term)) {
690 		pr_err("failed to set config \"%s\" on event %s with %d (%s)\n",
691 		      err_term->val.drv_cfg, perf_evsel__name(counter), errno,
692 		      str_error_r(errno, msg, sizeof(msg)));
693 		return -1;
694 	}
695 
696 	if (STAT_RECORD) {
697 		int err, fd = perf_data__fd(&perf_stat.data);
698 
699 		if (is_pipe) {
700 			err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
701 		} else {
702 			err = perf_session__write_header(perf_stat.session, evsel_list,
703 							 fd, false);
704 		}
705 
706 		if (err < 0)
707 			return err;
708 
709 		err = perf_stat_synthesize_config(is_pipe);
710 		if (err < 0)
711 			return err;
712 	}
713 
714 	/*
715 	 * Enable counters and exec the command:
716 	 */
717 	t0 = rdclock();
718 	clock_gettime(CLOCK_MONOTONIC, &ref_time);
719 
720 	if (forks) {
721 		perf_evlist__start_workload(evsel_list);
722 		enable_counters();
723 
724 		if (interval || timeout) {
725 			while (!waitpid(child_pid, &status, WNOHANG)) {
726 				nanosleep(&ts, NULL);
727 				if (timeout)
728 					break;
729 				process_interval();
730 				if (interval_count && !(--times))
731 					break;
732 			}
733 		}
734 		wait4(child_pid, &status, 0, &ru_data);
735 
736 		if (workload_exec_errno) {
737 			const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
738 			pr_err("Workload failed: %s\n", emsg);
739 			return -1;
740 		}
741 
742 		if (WIFSIGNALED(status))
743 			psignal(WTERMSIG(status), argv[0]);
744 	} else {
745 		enable_counters();
746 		while (!done) {
747 			nanosleep(&ts, NULL);
748 			if (timeout)
749 				break;
750 			if (interval) {
751 				process_interval();
752 				if (interval_count && !(--times))
753 					break;
754 			}
755 		}
756 	}
757 
758 	disable_counters();
759 
760 	t1 = rdclock();
761 
762 	if (walltime_run_table)
763 		walltime_run[run_idx] = t1 - t0;
764 
765 	update_stats(&walltime_nsecs_stats, t1 - t0);
766 
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);
775 
776 	return WEXITSTATUS(status);
777 }
778 
779 static int run_perf_stat(int argc, const char **argv, int run_idx)
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 
792 	ret = __run_perf_stat(argc, argv, run_idx);
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 
805 static void print_running(u64 run, u64 ena)
806 {
807 	if (csv_output) {
808 		fprintf(stat_config.output, "%s%" PRIu64 "%s%.2f",
809 					csv_sep,
810 					run,
811 					csv_sep,
812 					ena ? 100.0 * run / ena : 100.0);
813 	} else if (run != ena) {
814 		fprintf(stat_config.output, "  (%.2f%%)", 100.0 * run / ena);
815 	}
816 }
817 
818 static void print_noise_pct(double total, double avg)
819 {
820 	double pct = rel_stddev_stats(total, avg);
821 
822 	if (csv_output)
823 		fprintf(stat_config.output, "%s%.2f%%", csv_sep, pct);
824 	else if (pct)
825 		fprintf(stat_config.output, "  ( +-%6.2f%% )", pct);
826 }
827 
828 static void print_noise(struct perf_evsel *evsel, double avg)
829 {
830 	struct perf_stat_evsel *ps;
831 
832 	if (run_count == 1)
833 		return;
834 
835 	ps = evsel->stats;
836 	print_noise_pct(stddev_stats(&ps->res_stats[0]), avg);
837 }
838 
839 static void aggr_printout(struct perf_evsel *evsel, int id, int nr)
840 {
841 	switch (stat_config.aggr_mode) {
842 	case AGGR_CORE:
843 		fprintf(stat_config.output, "S%d-C%*d%s%*d%s",
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;
852 	case AGGR_SOCKET:
853 		fprintf(stat_config.output, "S%*d%s%*d%s",
854 			csv_output ? 0 : -5,
855 			id,
856 			csv_sep,
857 			csv_output ? 0 : 4,
858 			nr,
859 			csv_sep);
860 			break;
861 	case AGGR_NONE:
862 		fprintf(stat_config.output, "CPU%*d%s",
863 			csv_output ? 0 : -4,
864 			perf_evsel__cpus(evsel)->map[id], csv_sep);
865 		break;
866 	case AGGR_THREAD:
867 		fprintf(stat_config.output, "%*s-%*d%s",
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;
874 	case AGGR_GLOBAL:
875 	case AGGR_UNSET:
876 	default:
877 		break;
878 	}
879 }
880 
881 struct outstate {
882 	FILE *fh;
883 	bool newline;
884 	const char *prefix;
885 	int  nfields;
886 	int  id, nr;
887 	struct perf_evsel *evsel;
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);
902 	fputs(os->prefix, os->fh);
903 	aggr_printout(os->evsel, os->id, os->nr);
904 	if (stat_config.aggr_mode == AGGR_NONE)
905 		fprintf(os->fh, "        ");
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 
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);
943 	aggr_printout(os->evsel, os->id, os->nr);
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) {
957 		fprintf(out, "%s%s", csv_sep, csv_sep);
958 		return;
959 	}
960 	snprintf(buf, sizeof(buf), fmt, val);
961 	ends = vals = ltrim(buf);
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 
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);
1033 	ends = vals = ltrim(buf);
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 
1060 static void nsec_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1061 {
1062 	FILE *output = stat_config.output;
1063 	double msecs = avg / NSEC_PER_MSEC;
1064 	const char *fmt_v, *fmt_n;
1065 	char name[25];
1066 
1067 	fmt_v = csv_output ? "%.6f%s" : "%18.6f%s";
1068 	fmt_n = csv_output ? "%s" : "%-25s";
1069 
1070 	aggr_printout(evsel, id, nr);
1071 
1072 	scnprintf(name, sizeof(name), "%s%s",
1073 		  perf_evsel__name(evsel), csv_output ? "" : " (msec)");
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);
1083 
1084 	if (evsel->cgrp)
1085 		fprintf(output, "%s%s", csv_sep, evsel->cgrp->name);
1086 }
1087 
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 
1110 static void abs_printout(int id, int nr, struct perf_evsel *evsel, double avg)
1111 {
1112 	FILE *output = stat_config.output;
1113 	double sc =  evsel->scale;
1114 	const char *fmt;
1115 
1116 	if (csv_output) {
1117 		fmt = floor(sc) != sc ?  "%.2f%s" : "%.0f%s";
1118 	} else {
1119 		if (big_num)
1120 			fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
1121 		else
1122 			fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
1123 	}
1124 
1125 	aggr_printout(evsel, id, nr);
1126 
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);
1138 }
1139 
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 
1164 static void printout(int id, int nr, struct perf_evsel *counter, double uval,
1165 		     char *prefix, u64 run, u64 ena, double noise,
1166 		     struct runtime_stat *st)
1167 {
1168 	struct perf_stat_output_ctx out;
1169 	struct outstate os = {
1170 		.fh = stat_config.output,
1171 		.prefix = prefix ? prefix : "",
1172 		.id = id,
1173 		.nr = nr,
1174 		.evsel = counter,
1175 	};
1176 	print_metric_t pm = print_metric_std;
1177 	void (*nl)(void *);
1178 
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;
1187 
1188 	if (csv_output && !metric_only) {
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 	}
1204 	if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
1205 		if (metric_only) {
1206 			pm(&os, NULL, "", "", 0);
1207 			return;
1208 		}
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 
1216 		if (counter->supported) {
1217 			print_free_counters_hint = 1;
1218 			if (is_mixed_hw_group(counter))
1219 				print_mixed_hw_group_error = 1;
1220 		}
1221 
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 
1234 		if (!csv_output)
1235 			pm(&os, NULL, NULL, "", 0);
1236 		print_noise(counter, noise);
1237 		print_running(run, ena);
1238 		if (csv_output)
1239 			pm(&os, NULL, NULL, "", 0);
1240 		return;
1241 	}
1242 
1243 	if (metric_only)
1244 		/* nothing */;
1245 	else if (nsec_counter(counter))
1246 		nsec_printout(id, nr, counter, uval);
1247 	else
1248 		abs_printout(id, nr, counter, uval);
1249 
1250 	out.print_metric = pm;
1251 	out.new_line = nl;
1252 	out.ctx = &os;
1253 	out.force_header = false;
1254 
1255 	if (csv_output && !metric_only) {
1256 		print_noise(counter, noise);
1257 		print_running(run, ena);
1258 	}
1259 
1260 	perf_stat__print_shadow_stats(counter, uval,
1261 				first_shadow_cpu(counter, id),
1262 				&out, &metric_events, st);
1263 	if (!csv_output && !metric_only) {
1264 		print_noise(counter, noise);
1265 		print_running(run, ena);
1266 	}
1267 }
1268 
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];
1277 		evlist__for_each_entry(evsel_list, counter) {
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 			}
1285 			perf_stat__update_shadow_stats(counter, val,
1286 					first_shadow_cpu(counter, id),
1287 					&rt_stat);
1288 		}
1289 	}
1290 }
1291 
1292 static void uniquify_event_name(struct perf_evsel *counter)
1293 {
1294 	char *new_name;
1295 	char *config;
1296 
1297 	if (counter->uniquified_name ||
1298 	    !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
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 	}
1316 
1317 	counter->uniquified_name = true;
1318 }
1319 
1320 static void collect_all_aliases(struct perf_evsel *counter,
1321 			    void (*cb)(struct perf_evsel *counter, void *data,
1322 				       bool first),
1323 			    void *data)
1324 {
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;
1347 	cb(counter, data, true);
1348 	if (no_merge)
1349 		uniquify_event_name(counter);
1350 	else if (counter->auto_merge_stats)
1351 		collect_all_aliases(counter, cb, data);
1352 	return true;
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);
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 		}
1386 		ad->val += counts->val;
1387 		ad->ena += counts->ena;
1388 		ad->run += counts->run;
1389 	}
1390 }
1391 
1392 static void print_aggr(char *prefix)
1393 {
1394 	FILE *output = stat_config.output;
1395 	struct perf_evsel *counter;
1396 	int s, id, nr;
1397 	double uval;
1398 	u64 ena, run, val;
1399 	bool first;
1400 
1401 	if (!(aggr_map || aggr_get_id))
1402 		return;
1403 
1404 	aggr_update_shadow();
1405 
1406 	/*
1407 	 * With metric_only everything is on a single line.
1408 	 * Without each counter has its own line.
1409 	 */
1410 	for (s = 0; s < aggr_map->nr; s++) {
1411 		struct aggr_data ad;
1412 		if (prefix && metric_only)
1413 			fprintf(output, "%s", prefix);
1414 
1415 		ad.id = id = aggr_map->map[s];
1416 		first = true;
1417 		evlist__for_each_entry(evsel_list, counter) {
1418 			if (is_duration_time(counter))
1419 				continue;
1420 
1421 			ad.val = ad.ena = ad.run = 0;
1422 			ad.nr = 0;
1423 			if (!collect_data(counter, aggr_cb, &ad))
1424 				continue;
1425 			nr = ad.nr;
1426 			ena = ad.ena;
1427 			run = ad.run;
1428 			val = ad.val;
1429 			if (first && metric_only) {
1430 				first = false;
1431 				aggr_printout(counter, id, nr);
1432 			}
1433 			if (prefix && !metric_only)
1434 				fprintf(output, "%s", prefix);
1435 
1436 			uval = val * counter->scale;
1437 			printout(id, nr, counter, uval, prefix, run, ena, 1.0,
1438 				 &rt_stat);
1439 			if (!metric_only)
1440 				fputc('\n', output);
1441 		}
1442 		if (metric_only)
1443 			fputc('\n', output);
1444 	}
1445 }
1446 
1447 static int cmp_val(const void *a, const void *b)
1448 {
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;
1459 	double uval;
1460 	struct perf_aggr_thread_value *buf;
1461 
1462 	buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
1463 	if (!buf)
1464 		return NULL;
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 
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++) {
1516 		if (prefix)
1517 			fprintf(output, "%s", prefix);
1518 
1519 		id = buf[thread].id;
1520 		if (stat_config.stats)
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]);
1524 		else
1525 			printout(id, 0, buf[thread].counter, buf[thread].uval,
1526 				 prefix, buf[thread].run, buf[thread].ena, 1.0,
1527 				 &rt_stat);
1528 		fputc('\n', output);
1529 	}
1530 
1531 	free(buf);
1532 }
1533 
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;
1542 	struct perf_stat_evsel *ps = counter->stats;
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 
1549 /*
1550  * Print out the results of a single counter:
1551  * aggregated counts in system-wide mode
1552  */
1553 static void print_counter_aggr(struct perf_evsel *counter, char *prefix)
1554 {
1555 	FILE *output = stat_config.output;
1556 	double uval;
1557 	struct caggr_data cd = { .avg = 0.0 };
1558 
1559 	if (!collect_data(counter, counter_aggr_cb, &cd))
1560 		return;
1561 
1562 	if (prefix && !metric_only)
1563 		fprintf(output, "%s", prefix);
1564 
1565 	uval = cd.avg * counter->scale;
1566 	printout(-1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
1567 		 cd.avg, &rt_stat);
1568 	if (!metric_only)
1569 		fprintf(output, "\n");
1570 }
1571 
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 
1582 /*
1583  * Print out the results of a single counter:
1584  * does not use aggregated count in system-wide
1585  */
1586 static void print_counter(struct perf_evsel *counter, char *prefix)
1587 {
1588 	FILE *output = stat_config.output;
1589 	u64 ena, run, val;
1590 	double uval;
1591 	int cpu;
1592 
1593 	for (cpu = 0; cpu < perf_evsel__nr_cpus(counter); cpu++) {
1594 		struct aggr_data ad = { .cpu = cpu };
1595 
1596 		if (!collect_data(counter, counter_cb, &ad))
1597 			return;
1598 		val = ad.val;
1599 		ena = ad.ena;
1600 		run = ad.run;
1601 
1602 		if (prefix)
1603 			fprintf(output, "%s", prefix);
1604 
1605 		uval = val * counter->scale;
1606 		printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1607 			 &rt_stat);
1608 
1609 		fputc('\n', output);
1610 	}
1611 }
1612 
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);
1627 		evlist__for_each_entry(evsel_list, counter) {
1628 			if (is_duration_time(counter))
1629 				continue;
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;
1639 			printout(cpu, 0, counter, uval, prefix, run, ena, 1.0,
1640 				 &rt_stat);
1641 		}
1642 		fputc('\n', stat_config.output);
1643 	}
1644 }
1645 
1646 static int aggr_header_lens[] = {
1647 	[AGGR_CORE] = 18,
1648 	[AGGR_SOCKET] = 12,
1649 	[AGGR_NONE] = 6,
1650 	[AGGR_THREAD] = 24,
1651 	[AGGR_GLOBAL] = 0,
1652 };
1653 
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 
1662 static void print_metric_headers(const char *prefix, bool no_indent)
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 
1673 	if (!csv_output && !no_indent)
1674 		fprintf(stat_config.output, "%*s",
1675 			aggr_header_lens[stat_config.aggr_mode], "");
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 	}
1682 
1683 	/* Print metrics headers only */
1684 	evlist__for_each_entry(evsel_list, counter) {
1685 		if (is_duration_time(counter))
1686 			continue;
1687 		os.evsel = counter;
1688 		out.ctx = &os;
1689 		out.print_metric = print_metric_header;
1690 		out.new_line = new_line_metric;
1691 		out.force_header = true;
1692 		os.evsel = counter;
1693 		perf_stat__print_shadow_stats(counter, 0,
1694 					      0,
1695 					      &out,
1696 					      &metric_events,
1697 					      &rt_stat);
1698 	}
1699 	fputc('\n', stat_config.output);
1700 }
1701 
1702 static void print_interval(char *prefix, struct timespec *ts)
1703 {
1704 	FILE *output = stat_config.output;
1705 	static int num_print_interval;
1706 
1707 	sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, csv_sep);
1708 
1709 	if (num_print_interval == 0 && !csv_output) {
1710 		switch (stat_config.aggr_mode) {
1711 		case AGGR_SOCKET:
1712 			fprintf(output, "#           time socket cpus");
1713 			if (!metric_only)
1714 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
1715 			break;
1716 		case AGGR_CORE:
1717 			fprintf(output, "#           time core         cpus");
1718 			if (!metric_only)
1719 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
1720 			break;
1721 		case AGGR_NONE:
1722 			fprintf(output, "#           time CPU");
1723 			if (!metric_only)
1724 				fprintf(output, "                counts %*s events\n", unit_width, "unit");
1725 			break;
1726 		case AGGR_THREAD:
1727 			fprintf(output, "#           time             comm-pid");
1728 			if (!metric_only)
1729 				fprintf(output, "                  counts %*s events\n", unit_width, "unit");
1730 			break;
1731 		case AGGR_GLOBAL:
1732 		default:
1733 			fprintf(output, "#           time");
1734 			if (!metric_only)
1735 				fprintf(output, "             counts %*s events\n", unit_width, "unit");
1736 		case AGGR_UNSET:
1737 			break;
1738 		}
1739 	}
1740 
1741 	if (num_print_interval == 0 && metric_only)
1742 		print_metric_headers(" ", true);
1743 	if (++num_print_interval == 25)
1744 		num_print_interval = 0;
1745 }
1746 
1747 static void print_header(int argc, const char **argv)
1748 {
1749 	FILE *output = stat_config.output;
1750 	int i;
1751 
1752 	fflush(stdout);
1753 
1754 	if (!csv_output) {
1755 		fprintf(output, "\n");
1756 		fprintf(output, " Performance counter stats for ");
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);
1761 		else if (!target__has_task(&target)) {
1762 			fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1763 			for (i = 1; argv && (i < argc); i++)
1764 				fprintf(output, " %s", argv[i]);
1765 		} else if (target.pid)
1766 			fprintf(output, "process id \'%s", target.pid);
1767 		else
1768 			fprintf(output, "thread id \'%s", target.tid);
1769 
1770 		fprintf(output, "\'");
1771 		if (run_count > 1)
1772 			fprintf(output, " (%d runs)", run_count);
1773 		fprintf(output, ":\n\n");
1774 	}
1775 }
1776 
1777 static int get_precision(double num)
1778 {
1779 	if (num > 1)
1780 		return 0;
1781 
1782 	return lround(ceil(-log10(num)));
1783 }
1784 
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;
1798 		int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1799 
1800 		fprintf(output, " %17.*f (%+.*f) ",
1801 			precision, run, precision, run - avg);
1802 
1803 		for (h = 0; h < n; h++)
1804 			fprintf(output, "#");
1805 
1806 		fprintf(output, "\n");
1807 	}
1808 
1809 	fprintf(output, "\n%*s# Final result:\n", indent, "");
1810 }
1811 
1812 static double timeval2double(struct timeval *t)
1813 {
1814 	return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1815 }
1816 
1817 static void print_footer(void)
1818 {
1819 	double avg = avg_stats(&walltime_nsecs_stats) / NSEC_PER_SEC;
1820 	FILE *output = stat_config.output;
1821 	int n;
1822 
1823 	if (!null_run)
1824 		fprintf(output, "\n");
1825 
1826 	if (run_count == 1) {
1827 		fprintf(output, " %17.9f seconds time elapsed", avg);
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 		}
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 
1845 		if (walltime_run_table)
1846 			print_table(output, precision, avg);
1847 
1848 		fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1849 			precision, avg, precision, sd);
1850 
1851 		print_noise_pct(sd, avg);
1852 	}
1853 	fprintf(output, "\n\n");
1854 
1855 	if (print_free_counters_hint &&
1856 	    sysctl__read_int("kernel/nmi_watchdog", &n) >= 0 &&
1857 	    n > 0)
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");
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");
1868 }
1869 
1870 static void print_counters(struct timespec *ts, int argc, const char **argv)
1871 {
1872 	int interval = stat_config.interval;
1873 	struct perf_evsel *counter;
1874 	char buf[64], *prefix = NULL;
1875 
1876 	/* Do not print anything if we record to the pipe. */
1877 	if (STAT_RECORD && perf_stat.data.is_pipe)
1878 		return;
1879 
1880 	if (interval)
1881 		print_interval(prefix = buf, ts);
1882 	else
1883 		print_header(argc, argv);
1884 
1885 	if (metric_only) {
1886 		static int num_print_iv;
1887 
1888 		if (num_print_iv == 0 && !interval)
1889 			print_metric_headers(prefix, false);
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 
1896 	switch (stat_config.aggr_mode) {
1897 	case AGGR_CORE:
1898 	case AGGR_SOCKET:
1899 		print_aggr(prefix);
1900 		break;
1901 	case AGGR_THREAD:
1902 		evlist__for_each_entry(evsel_list, counter) {
1903 			if (is_duration_time(counter))
1904 				continue;
1905 			print_aggr_thread(counter, prefix);
1906 		}
1907 		break;
1908 	case AGGR_GLOBAL:
1909 		evlist__for_each_entry(evsel_list, counter) {
1910 			if (is_duration_time(counter))
1911 				continue;
1912 			print_counter_aggr(counter, prefix);
1913 		}
1914 		if (metric_only)
1915 			fputc('\n', stat_config.output);
1916 		break;
1917 	case AGGR_NONE:
1918 		if (metric_only)
1919 			print_no_aggr_metric(prefix);
1920 		else {
1921 			evlist__for_each_entry(evsel_list, counter) {
1922 				if (is_duration_time(counter))
1923 					continue;
1924 				print_counter(counter, prefix);
1925 			}
1926 		}
1927 		break;
1928 	case AGGR_UNSET:
1929 	default:
1930 		break;
1931 	}
1932 
1933 	if (!interval && !csv_output)
1934 		print_footer();
1935 
1936 	fflush(stat_config.output);
1937 }
1938 
1939 static volatile int signr = -1;
1940 
1941 static void skip_signal(int signo)
1942 {
1943 	if ((child_pid == -1) || stat_config.interval)
1944 		done = 1;
1945 
1946 	signr = signo;
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;
1954 }
1955 
1956 static void sig_atexit(void)
1957 {
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 
1970 	if (child_pid != -1)
1971 		kill(child_pid, SIGTERM);
1972 
1973 	sigprocmask(SIG_SETMASK, &oset, NULL);
1974 
1975 	if (signr == -1)
1976 		return;
1977 
1978 	signal(signr, SIG_DFL);
1979 	kill(getpid(), signr);
1980 }
1981 
1982 static int stat__set_big_num(const struct option *opt __maybe_unused,
1983 			     const char *s __maybe_unused, int unset)
1984 {
1985 	big_num_opt = unset ? 0 : 1;
1986 	return 0;
1987 }
1988 
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 
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 
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)"),
2027 	OPT_BOOLEAN(0, "table", &walltime_run_table,
2028 		    "display details about each run (only with -r option)"),
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),
2042 	OPT_BOOLEAN(0, "no-merge", &no_merge, "Do not merge identical named events"),
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,
2056 		    "print counts at regular interval in ms "
2057 		    "(overhead is possible for values <= 100ms)"),
2058 	OPT_INTEGER(0, "interval-count", &stat_config.times,
2059 		    "print counts for fixed number of times"),
2060 	OPT_UINTEGER(0, "timeout", &stat_config.timeout,
2061 		    "stop workload and print counts after a timeout period in ms (>= 10ms)"),
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"),
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"),
2074 	OPT_BOOLEAN(0, "smi-cost", &smi_cost,
2075 			"measure SMI cost"),
2076 	OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
2077 		     "monitor specified metrics or metric groups (separated by ,)",
2078 		     parse_metric_groups),
2079 	OPT_END()
2080 };
2081 
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 
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 
2131 static int perf_stat_init_aggr_mode(void)
2132 {
2133 	int nr;
2134 
2135 	switch (stat_config.aggr_mode) {
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 		}
2141 		aggr_get_id = perf_stat__get_socket_cached;
2142 		break;
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 		}
2148 		aggr_get_id = perf_stat__get_core_cached;
2149 		break;
2150 	case AGGR_NONE:
2151 	case AGGR_GLOBAL:
2152 	case AGGR_THREAD:
2153 	case AGGR_UNSET:
2154 	default:
2155 		break;
2156 	}
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;
2166 }
2167 
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 
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 
2185 	if (cpu >= env->nr_cpus_avail)
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 
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 
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 {
2326 	int err;
2327 	struct perf_event_attr default_attrs0[] = {
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		},
2335 };
2336 	struct perf_event_attr frontend_attrs[] = {
2337   { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND	},
2338 };
2339 	struct perf_event_attr backend_attrs[] = {
2340   { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND	},
2341 };
2342 	struct perf_event_attr default_attrs1[] = {
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 
2440 	/* Set attrs if no event is selected and !null_run: */
2441 	if (null_run)
2442 		return 0;
2443 
2444 	if (transaction_run) {
2445 		struct parse_events_error errinfo;
2446 
2447 		if (pmu_have_event("cpu", "cycles-ct") &&
2448 		    pmu_have_event("cpu", "el-start"))
2449 			err = parse_events(evsel_list, transaction_attrs,
2450 					   &errinfo);
2451 		else
2452 			err = parse_events(evsel_list,
2453 					   transaction_limited_attrs,
2454 					   &errinfo);
2455 		if (err) {
2456 			fprintf(stderr, "Cannot set up transaction events\n");
2457 			return -1;
2458 		}
2459 		return 0;
2460 	}
2461 
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 
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 
2535 	if (!evsel_list->nr_entries) {
2536 		if (target__has_cpu(&target))
2537 			default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2538 
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)
2552 			return -1;
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: */
2561 	if (perf_evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
2562 		return -1;
2563 
2564 	if (detailed_run < 2)
2565 		return 0;
2566 
2567 	/* Append very detailed run extra attributes: */
2568 	if (perf_evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
2569 		return -1;
2570 
2571 	if (detailed_run < 3)
2572 		return 0;
2573 
2574 	/* Append very, very detailed run extra attributes: */
2575 	return perf_evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
2576 }
2577 
2578 static const char * const stat_record_usage[] = {
2579 	"perf stat record [<options>]",
2580 	NULL,
2581 };
2582 
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 
2596 static int __cmd_record(int argc, const char **argv)
2597 {
2598 	struct perf_session *session;
2599 	struct perf_data *data = &perf_stat.data;
2600 
2601 	argc = parse_options(argc, argv, stat_options, stat_record_usage,
2602 			     PARSE_OPT_STOP_AT_NON_OPTION);
2603 
2604 	if (output_name)
2605 		data->file.path = output_name;
2606 
2607 	if (run_count != 1 || forever) {
2608 		pr_err("Cannot use -r option with perf stat record.\n");
2609 		return -1;
2610 	}
2611 
2612 	session = perf_session__new(data, false, NULL);
2613 	if (session == NULL) {
2614 		pr_err("Perf session creation failed.\n");
2615 		return -1;
2616 	}
2617 
2618 	init_features(session);
2619 
2620 	session->evlist   = evsel_list;
2621 	perf_stat.session = session;
2622 	perf_stat.record  = true;
2623 	return argc;
2624 }
2625 
2626 static int process_stat_round_event(struct perf_tool *tool __maybe_unused,
2627 				    union perf_event *event,
2628 				    struct perf_session *session)
2629 {
2630 	struct stat_round_event *stat_round = &event->stat_round;
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 
2636 	evlist__for_each_entry(evsel_list, counter)
2637 		perf_stat_process_counter(&stat_config, counter);
2638 
2639 	if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2640 		update_stats(&walltime_nsecs_stats, stat_round->time);
2641 
2642 	if (stat_config.interval && stat_round->time) {
2643 		tsh.tv_sec  = stat_round->time / NSEC_PER_SEC;
2644 		tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
2645 		ts = &tsh;
2646 	}
2647 
2648 	print_counters(ts, argc, argv);
2649 	return 0;
2650 }
2651 
2652 static
2653 int process_stat_config_event(struct perf_tool *tool,
2654 			      union perf_event *event,
2655 			      struct perf_session *session __maybe_unused)
2656 {
2657 	struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2658 
2659 	perf_event__read_stat_config(&stat_config, &event->stat_config);
2660 
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 
2670 	if (perf_stat.data.is_pipe)
2671 		perf_stat_init_aggr_mode();
2672 	else
2673 		perf_stat_init_aggr_mode_file(st);
2674 
2675 	return 0;
2676 }
2677 
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
2696 int process_thread_map_event(struct perf_tool *tool,
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
2715 int process_cpu_map_event(struct perf_tool *tool,
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 
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 
2764 static const char * const stat_report_usage[] = {
2765 	"perf stat report [<options>]",
2766 	NULL,
2767 };
2768 
2769 static struct perf_stat perf_stat = {
2770 	.tool = {
2771 		.attr		= perf_event__process_attr,
2772 		.event_update	= perf_event__process_event_update,
2773 		.thread_map	= process_thread_map_event,
2774 		.cpu_map	= process_cpu_map_event,
2775 		.stat_config	= process_stat_config_event,
2776 		.stat		= perf_event__process_stat_event,
2777 		.stat_round	= process_stat_round_event,
2778 	},
2779 	.aggr_mode = AGGR_UNSET,
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"),
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),
2793 	OPT_END()
2794 	};
2795 	struct stat st;
2796 	int ret;
2797 
2798 	argc = parse_options(argc, argv, options, stat_report_usage, 0);
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 
2807 	perf_stat.data.file.path = input_name;
2808 	perf_stat.data.mode      = PERF_DATA_MODE_READ;
2809 
2810 	session = perf_session__new(&perf_stat.data, false, &perf_stat.tool);
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 
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 
2855 int cmd_stat(int argc, const char **argv)
2856 {
2857 	const char * const stat_usage[] = {
2858 		"perf stat [<options>] [<command>]",
2859 		NULL
2860 	};
2861 	int status = -EINVAL, run_idx;
2862 	const char *mode;
2863 	FILE *output = stderr;
2864 	unsigned int interval, timeout;
2865 	const char * const stat_subcommands[] = { "record", "report" };
2866 
2867 	setlocale(LC_ALL, "");
2868 
2869 	evsel_list = perf_evlist__new();
2870 	if (evsel_list == NULL)
2871 		return -ENOMEM;
2872 
2873 	parse_events__shrink_config_terms();
2874 	argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2875 					(const char **) stat_usage,
2876 					PARSE_OPT_STOP_AT_NON_OPTION);
2877 	perf_stat__collect_metric_expr(evsel_list);
2878 	perf_stat__init_shadow_stats();
2879 
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 
2887 	if (argc && !strncmp(argv[0], "rec", 3)) {
2888 		argc = __cmd_record(argc, argv);
2889 		if (argc < 0)
2890 			return -1;
2891 	} else if (argc && !strncmp(argv[0], "rep", 3))
2892 		return __cmd_report(argc, argv);
2893 
2894 	interval = stat_config.interval;
2895 	timeout = stat_config.timeout;
2896 
2897 	/*
2898 	 * For record command the -o is already taken care of.
2899 	 */
2900 	if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
2901 		output = NULL;
2902 
2903 	if (output_name && output_fd) {
2904 		fprintf(stderr, "cannot use both --output and --log-fd\n");
2905 		parse_options_usage(stat_usage, stat_options, "o", 1);
2906 		parse_options_usage(NULL, stat_options, "log-fd", 0);
2907 		goto out;
2908 	}
2909 
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 
2915 	if (metric_only && run_count > 1) {
2916 		fprintf(stderr, "--metric-only is not supported with -r\n");
2917 		goto out;
2918 	}
2919 
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 
2927 	if (output_fd < 0) {
2928 		fprintf(stderr, "argument to --log-fd must be a > 0\n");
2929 		parse_options_usage(stat_usage, stat_options, "log-fd", 0);
2930 		goto out;
2931 	}
2932 
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");
2940 			return -1;
2941 		}
2942 		clock_gettime(CLOCK_REALTIME, &tm);
2943 		fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
2944 	} else if (output_fd > 0) {
2945 		mode = append_file ? "a" : "w";
2946 		output = fdopen(output_fd, mode);
2947 		if (!output) {
2948 			perror("Failed opening logfd");
2949 			return -errno;
2950 		}
2951 	}
2952 
2953 	stat_config.output = output;
2954 
2955 	/*
2956 	 * let the spreadsheet do the pretty-printing
2957 	 */
2958 	if (csv_output) {
2959 		/* User explicitly passed -B? */
2960 		if (big_num_opt == 1) {
2961 			fprintf(stderr, "-B option not supported with -x\n");
2962 			parse_options_usage(stat_usage, stat_options, "B", 1);
2963 			parse_options_usage(NULL, stat_options, "x", 1);
2964 			goto out;
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 
2970 	setup_system_wide(argc);
2971 
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 
2979 	if (run_count < 0) {
2980 		pr_err("Run count must be a positive number\n");
2981 		parse_options_usage(stat_usage, stat_options, "r", 1);
2982 		goto out;
2983 	} else if (run_count == 0) {
2984 		forever = true;
2985 		run_count = 1;
2986 	}
2987 
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 
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 		}
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 	 */
3012 	if (((stat_config.aggr_mode != AGGR_GLOBAL &&
3013 	      stat_config.aggr_mode != AGGR_THREAD) || nr_cgroups) &&
3014 	    !target__has_cpu(&target)) {
3015 		fprintf(stderr, "both cgroup and no-aggregation "
3016 			"modes only available in system-wide mode\n");
3017 
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);
3021 		goto out;
3022 	}
3023 
3024 	if (add_default_attributes())
3025 		goto out;
3026 
3027 	target__validate(&target);
3028 
3029 	if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
3030 		target.per_thread = true;
3031 
3032 	if (perf_evlist__create_maps(evsel_list, &target) < 0) {
3033 		if (target__has_task(&target)) {
3034 			pr_err("Problems finding threads of monitor\n");
3035 			parse_options_usage(stat_usage, stat_options, "p", 1);
3036 			parse_options_usage(NULL, stat_options, "t", 1);
3037 		} else if (target__has_cpu(&target)) {
3038 			perror("failed to parse CPUs map");
3039 			parse_options_usage(stat_usage, stat_options, "C", 1);
3040 			parse_options_usage(NULL, stat_options, "a", 1);
3041 		}
3042 		goto out;
3043 	}
3044 
3045 	/*
3046 	 * Initialize thread_map with comm names,
3047 	 * so we could print it out on output.
3048 	 */
3049 	if (stat_config.aggr_mode == AGGR_THREAD) {
3050 		thread_map__read_comms(evsel_list->threads);
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 	}
3058 
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 	}
3068 
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 
3086 	if (perf_evlist__alloc_stats(evsel_list, interval))
3087 		goto out;
3088 
3089 	if (perf_stat_init_aggr_mode())
3090 		goto out;
3091 
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 	 */
3098 	atexit(sig_atexit);
3099 	if (!forever)
3100 		signal(SIGINT,  skip_signal);
3101 	signal(SIGCHLD, skip_signal);
3102 	signal(SIGALRM, skip_signal);
3103 	signal(SIGABRT, skip_signal);
3104 
3105 	status = 0;
3106 	for (run_idx = 0; forever || run_idx < run_count; run_idx++) {
3107 		if (run_count != 1 && verbose > 0)
3108 			fprintf(output, "[ perf stat: executing run #%d ... ]\n",
3109 				run_idx + 1);
3110 
3111 		status = run_perf_stat(argc, argv, run_idx);
3112 		if (forever && status != -1) {
3113 			print_counters(NULL, argc, argv);
3114 			perf_stat__reset_stats();
3115 		}
3116 	}
3117 
3118 	if (!forever && status != -1 && !interval)
3119 		print_counters(NULL, argc, argv);
3120 
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
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
3133 		 */
3134 		int fd = perf_data__fd(&perf_stat.data);
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 
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 
3148 		if (!perf_stat.data.is_pipe) {
3149 			perf_stat.session->header.data_size += perf_stat.bytes_written;
3150 			perf_session__write_header(perf_stat.session, evsel_list, fd, true);
3151 		}
3152 
3153 		perf_session__delete(perf_stat.session);
3154 	}
3155 
3156 	perf_stat__exit_aggr_mode();
3157 	perf_evlist__free_stats(evsel_list);
3158 out:
3159 	free(walltime_run);
3160 
3161 	if (smi_cost && smi_reset)
3162 		sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
3163 
3164 	perf_evlist__delete(evsel_list);
3165 
3166 	runtime_stat_delete(&stat_config);
3167 
3168 	return status;
3169 }
3170