xref: /linux/tools/perf/builtin-stat.c (revision 3daee2e4b3568f0ed88b0598df96547fcf21cb9b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * builtin-stat.c
4  *
5  * Builtin stat command: Give a precise performance counters summary
6  * overview about any workload, CPU or specific PID.
7  *
8  * Sample output:
9 
10    $ perf stat ./hackbench 10
11 
12   Time: 0.118
13 
14   Performance counter stats for './hackbench 10':
15 
16        1708.761321 task-clock                #   11.037 CPUs utilized
17             41,190 context-switches          #    0.024 M/sec
18              6,735 CPU-migrations            #    0.004 M/sec
19             17,318 page-faults               #    0.010 M/sec
20      5,205,202,243 cycles                    #    3.046 GHz
21      3,856,436,920 stalled-cycles-frontend   #   74.09% frontend cycles idle
22      1,600,790,871 stalled-cycles-backend    #   30.75% backend  cycles idle
23      2,603,501,247 instructions              #    0.50  insns per cycle
24                                              #    1.48  stalled cycles per insn
25        484,357,498 branches                  #  283.455 M/sec
26          6,388,934 branch-misses             #    1.32% of all branches
27 
28         0.154822978  seconds time elapsed
29 
30  *
31  * Copyright (C) 2008-2011, Red Hat Inc, Ingo Molnar <mingo@redhat.com>
32  *
33  * Improvements and fixes by:
34  *
35  *   Arjan van de Ven <arjan@linux.intel.com>
36  *   Yanmin Zhang <yanmin.zhang@intel.com>
37  *   Wu Fengguang <fengguang.wu@intel.com>
38  *   Mike Galbraith <efault@gmx.de>
39  *   Paul Mackerras <paulus@samba.org>
40  *   Jaswinder Singh Rajput <jaswinder@kernel.org>
41  */
42 
43 #include "builtin.h"
44 #include "util/cgroup.h"
45 #include <subcmd/parse-options.h>
46 #include "util/parse-events.h"
47 #include "util/pmus.h"
48 #include "util/pmu.h"
49 #include "util/event.h"
50 #include "util/evlist.h"
51 #include "util/evsel.h"
52 #include "util/debug.h"
53 #include "util/color.h"
54 #include "util/stat.h"
55 #include "util/header.h"
56 #include "util/cpumap.h"
57 #include "util/thread_map.h"
58 #include "util/counts.h"
59 #include "util/topdown.h"
60 #include "util/session.h"
61 #include "util/tool.h"
62 #include "util/string2.h"
63 #include "util/metricgroup.h"
64 #include "util/synthetic-events.h"
65 #include "util/target.h"
66 #include "util/time-utils.h"
67 #include "util/top.h"
68 #include "util/affinity.h"
69 #include "util/pfm.h"
70 #include "util/bpf_counter.h"
71 #include "util/iostat.h"
72 #include "util/util.h"
73 #include "asm/bug.h"
74 
75 #include <linux/time64.h>
76 #include <linux/zalloc.h>
77 #include <api/fs/fs.h>
78 #include <errno.h>
79 #include <signal.h>
80 #include <stdlib.h>
81 #include <sys/prctl.h>
82 #include <inttypes.h>
83 #include <locale.h>
84 #include <math.h>
85 #include <sys/types.h>
86 #include <sys/stat.h>
87 #include <sys/wait.h>
88 #include <unistd.h>
89 #include <sys/time.h>
90 #include <sys/resource.h>
91 #include <linux/err.h>
92 
93 #include <linux/ctype.h>
94 #include <perf/evlist.h>
95 #include <internal/threadmap.h>
96 
97 #define DEFAULT_SEPARATOR	" "
98 #define FREEZE_ON_SMI_PATH	"devices/cpu/freeze_on_smi"
99 
100 static void print_counters(struct timespec *ts, int argc, const char **argv);
101 
102 static struct evlist	*evsel_list;
103 static struct parse_events_option_args parse_events_option_args = {
104 	.evlistp = &evsel_list,
105 };
106 
107 static bool all_counters_use_bpf = true;
108 
109 static struct target target = {
110 	.uid	= UINT_MAX,
111 };
112 
113 #define METRIC_ONLY_LEN 20
114 
115 static volatile sig_atomic_t	child_pid			= -1;
116 static int			detailed_run			=  0;
117 static bool			transaction_run;
118 static bool			topdown_run			= false;
119 static bool			smi_cost			= false;
120 static bool			smi_reset			= false;
121 static int			big_num_opt			=  -1;
122 static const char		*pre_cmd			= NULL;
123 static const char		*post_cmd			= NULL;
124 static bool			sync_run			= false;
125 static bool			forever				= false;
126 static bool			force_metric_only		= false;
127 static struct timespec		ref_time;
128 static bool			append_file;
129 static bool			interval_count;
130 static const char		*output_name;
131 static int			output_fd;
132 static char			*metrics;
133 
134 struct perf_stat {
135 	bool			 record;
136 	struct perf_data	 data;
137 	struct perf_session	*session;
138 	u64			 bytes_written;
139 	struct perf_tool	 tool;
140 	bool			 maps_allocated;
141 	struct perf_cpu_map	*cpus;
142 	struct perf_thread_map *threads;
143 	enum aggr_mode		 aggr_mode;
144 	u32			 aggr_level;
145 };
146 
147 static struct perf_stat		perf_stat;
148 #define STAT_RECORD		perf_stat.record
149 
150 static volatile sig_atomic_t done = 0;
151 
152 static struct perf_stat_config stat_config = {
153 	.aggr_mode		= AGGR_GLOBAL,
154 	.aggr_level		= MAX_CACHE_LVL + 1,
155 	.scale			= true,
156 	.unit_width		= 4, /* strlen("unit") */
157 	.run_count		= 1,
158 	.metric_only_len	= METRIC_ONLY_LEN,
159 	.walltime_nsecs_stats	= &walltime_nsecs_stats,
160 	.ru_stats		= &ru_stats,
161 	.big_num		= true,
162 	.ctl_fd			= -1,
163 	.ctl_fd_ack		= -1,
164 	.iostat_run		= false,
165 };
166 
167 static void evlist__check_cpu_maps(struct evlist *evlist)
168 {
169 	struct evsel *evsel, *warned_leader = NULL;
170 
171 	evlist__for_each_entry(evlist, evsel) {
172 		struct evsel *leader = evsel__leader(evsel);
173 
174 		/* Check that leader matches cpus with each member. */
175 		if (leader == evsel)
176 			continue;
177 		if (perf_cpu_map__equal(leader->core.cpus, evsel->core.cpus))
178 			continue;
179 
180 		/* If there's mismatch disable the group and warn user. */
181 		if (warned_leader != leader) {
182 			char buf[200];
183 
184 			pr_warning("WARNING: grouped events cpus do not match.\n"
185 				"Events with CPUs not matching the leader will "
186 				"be removed from the group.\n");
187 			evsel__group_desc(leader, buf, sizeof(buf));
188 			pr_warning("  %s\n", buf);
189 			warned_leader = leader;
190 		}
191 		if (verbose > 0) {
192 			char buf[200];
193 
194 			cpu_map__snprint(leader->core.cpus, buf, sizeof(buf));
195 			pr_warning("     %s: %s\n", leader->name, buf);
196 			cpu_map__snprint(evsel->core.cpus, buf, sizeof(buf));
197 			pr_warning("     %s: %s\n", evsel->name, buf);
198 		}
199 
200 		evsel__remove_from_group(evsel, leader);
201 	}
202 }
203 
204 static inline void diff_timespec(struct timespec *r, struct timespec *a,
205 				 struct timespec *b)
206 {
207 	r->tv_sec = a->tv_sec - b->tv_sec;
208 	if (a->tv_nsec < b->tv_nsec) {
209 		r->tv_nsec = a->tv_nsec + NSEC_PER_SEC - b->tv_nsec;
210 		r->tv_sec--;
211 	} else {
212 		r->tv_nsec = a->tv_nsec - b->tv_nsec ;
213 	}
214 }
215 
216 static void perf_stat__reset_stats(void)
217 {
218 	evlist__reset_stats(evsel_list);
219 	perf_stat__reset_shadow_stats();
220 }
221 
222 static int process_synthesized_event(struct perf_tool *tool __maybe_unused,
223 				     union perf_event *event,
224 				     struct perf_sample *sample __maybe_unused,
225 				     struct machine *machine __maybe_unused)
226 {
227 	if (perf_data__write(&perf_stat.data, event, event->header.size) < 0) {
228 		pr_err("failed to write perf data, error: %m\n");
229 		return -1;
230 	}
231 
232 	perf_stat.bytes_written += event->header.size;
233 	return 0;
234 }
235 
236 static int write_stat_round_event(u64 tm, u64 type)
237 {
238 	return perf_event__synthesize_stat_round(NULL, tm, type,
239 						 process_synthesized_event,
240 						 NULL);
241 }
242 
243 #define WRITE_STAT_ROUND_EVENT(time, interval) \
244 	write_stat_round_event(time, PERF_STAT_ROUND_TYPE__ ## interval)
245 
246 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
247 
248 static int evsel__write_stat_event(struct evsel *counter, int cpu_map_idx, u32 thread,
249 				   struct perf_counts_values *count)
250 {
251 	struct perf_sample_id *sid = SID(counter, cpu_map_idx, thread);
252 	struct perf_cpu cpu = perf_cpu_map__cpu(evsel__cpus(counter), cpu_map_idx);
253 
254 	return perf_event__synthesize_stat(NULL, cpu, thread, sid->id, count,
255 					   process_synthesized_event, NULL);
256 }
257 
258 static int read_single_counter(struct evsel *counter, int cpu_map_idx,
259 			       int thread, struct timespec *rs)
260 {
261 	switch(counter->tool_event) {
262 		case PERF_TOOL_DURATION_TIME: {
263 			u64 val = rs->tv_nsec + rs->tv_sec*1000000000ULL;
264 			struct perf_counts_values *count =
265 				perf_counts(counter->counts, cpu_map_idx, thread);
266 			count->ena = count->run = val;
267 			count->val = val;
268 			return 0;
269 		}
270 		case PERF_TOOL_USER_TIME:
271 		case PERF_TOOL_SYSTEM_TIME: {
272 			u64 val;
273 			struct perf_counts_values *count =
274 				perf_counts(counter->counts, cpu_map_idx, thread);
275 			if (counter->tool_event == PERF_TOOL_USER_TIME)
276 				val = ru_stats.ru_utime_usec_stat.mean;
277 			else
278 				val = ru_stats.ru_stime_usec_stat.mean;
279 			count->ena = count->run = val;
280 			count->val = val;
281 			return 0;
282 		}
283 		default:
284 		case PERF_TOOL_NONE:
285 			return evsel__read_counter(counter, cpu_map_idx, thread);
286 		case PERF_TOOL_MAX:
287 			/* This should never be reached */
288 			return 0;
289 	}
290 }
291 
292 /*
293  * Read out the results of a single counter:
294  * do not aggregate counts across CPUs in system-wide mode
295  */
296 static int read_counter_cpu(struct evsel *counter, struct timespec *rs, int cpu_map_idx)
297 {
298 	int nthreads = perf_thread_map__nr(evsel_list->core.threads);
299 	int thread;
300 
301 	if (!counter->supported)
302 		return -ENOENT;
303 
304 	for (thread = 0; thread < nthreads; thread++) {
305 		struct perf_counts_values *count;
306 
307 		count = perf_counts(counter->counts, cpu_map_idx, thread);
308 
309 		/*
310 		 * The leader's group read loads data into its group members
311 		 * (via evsel__read_counter()) and sets their count->loaded.
312 		 */
313 		if (!perf_counts__is_loaded(counter->counts, cpu_map_idx, thread) &&
314 		    read_single_counter(counter, cpu_map_idx, thread, rs)) {
315 			counter->counts->scaled = -1;
316 			perf_counts(counter->counts, cpu_map_idx, thread)->ena = 0;
317 			perf_counts(counter->counts, cpu_map_idx, thread)->run = 0;
318 			return -1;
319 		}
320 
321 		perf_counts__set_loaded(counter->counts, cpu_map_idx, thread, false);
322 
323 		if (STAT_RECORD) {
324 			if (evsel__write_stat_event(counter, cpu_map_idx, thread, count)) {
325 				pr_err("failed to write stat event\n");
326 				return -1;
327 			}
328 		}
329 
330 		if (verbose > 1) {
331 			fprintf(stat_config.output,
332 				"%s: %d: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
333 					evsel__name(counter),
334 					perf_cpu_map__cpu(evsel__cpus(counter),
335 							  cpu_map_idx).cpu,
336 					count->val, count->ena, count->run);
337 		}
338 	}
339 
340 	return 0;
341 }
342 
343 static int read_affinity_counters(struct timespec *rs)
344 {
345 	struct evlist_cpu_iterator evlist_cpu_itr;
346 	struct affinity saved_affinity, *affinity;
347 
348 	if (all_counters_use_bpf)
349 		return 0;
350 
351 	if (!target__has_cpu(&target) || target__has_per_thread(&target))
352 		affinity = NULL;
353 	else if (affinity__setup(&saved_affinity) < 0)
354 		return -1;
355 	else
356 		affinity = &saved_affinity;
357 
358 	evlist__for_each_cpu(evlist_cpu_itr, evsel_list, affinity) {
359 		struct evsel *counter = evlist_cpu_itr.evsel;
360 
361 		if (evsel__is_bpf(counter))
362 			continue;
363 
364 		if (!counter->err) {
365 			counter->err = read_counter_cpu(counter, rs,
366 							evlist_cpu_itr.cpu_map_idx);
367 		}
368 	}
369 	if (affinity)
370 		affinity__cleanup(&saved_affinity);
371 
372 	return 0;
373 }
374 
375 static int read_bpf_map_counters(void)
376 {
377 	struct evsel *counter;
378 	int err;
379 
380 	evlist__for_each_entry(evsel_list, counter) {
381 		if (!evsel__is_bpf(counter))
382 			continue;
383 
384 		err = bpf_counter__read(counter);
385 		if (err)
386 			return err;
387 	}
388 	return 0;
389 }
390 
391 static int read_counters(struct timespec *rs)
392 {
393 	if (!stat_config.stop_read_counter) {
394 		if (read_bpf_map_counters() ||
395 		    read_affinity_counters(rs))
396 			return -1;
397 	}
398 	return 0;
399 }
400 
401 static void process_counters(void)
402 {
403 	struct evsel *counter;
404 
405 	evlist__for_each_entry(evsel_list, counter) {
406 		if (counter->err)
407 			pr_debug("failed to read counter %s\n", counter->name);
408 		if (counter->err == 0 && perf_stat_process_counter(&stat_config, counter))
409 			pr_warning("failed to process counter %s\n", counter->name);
410 		counter->err = 0;
411 	}
412 
413 	perf_stat_merge_counters(&stat_config, evsel_list);
414 	perf_stat_process_percore(&stat_config, evsel_list);
415 }
416 
417 static void process_interval(void)
418 {
419 	struct timespec ts, rs;
420 
421 	clock_gettime(CLOCK_MONOTONIC, &ts);
422 	diff_timespec(&rs, &ts, &ref_time);
423 
424 	evlist__reset_aggr_stats(evsel_list);
425 
426 	if (read_counters(&rs) == 0)
427 		process_counters();
428 
429 	if (STAT_RECORD) {
430 		if (WRITE_STAT_ROUND_EVENT(rs.tv_sec * NSEC_PER_SEC + rs.tv_nsec, INTERVAL))
431 			pr_err("failed to write stat round event\n");
432 	}
433 
434 	init_stats(&walltime_nsecs_stats);
435 	update_stats(&walltime_nsecs_stats, stat_config.interval * 1000000ULL);
436 	print_counters(&rs, 0, NULL);
437 }
438 
439 static bool handle_interval(unsigned int interval, int *times)
440 {
441 	if (interval) {
442 		process_interval();
443 		if (interval_count && !(--(*times)))
444 			return true;
445 	}
446 	return false;
447 }
448 
449 static int enable_counters(void)
450 {
451 	struct evsel *evsel;
452 	int err;
453 
454 	evlist__for_each_entry(evsel_list, evsel) {
455 		if (!evsel__is_bpf(evsel))
456 			continue;
457 
458 		err = bpf_counter__enable(evsel);
459 		if (err)
460 			return err;
461 	}
462 
463 	if (!target__enable_on_exec(&target)) {
464 		if (!all_counters_use_bpf)
465 			evlist__enable(evsel_list);
466 	}
467 	return 0;
468 }
469 
470 static void disable_counters(void)
471 {
472 	struct evsel *counter;
473 
474 	/*
475 	 * If we don't have tracee (attaching to task or cpu), counters may
476 	 * still be running. To get accurate group ratios, we must stop groups
477 	 * from counting before reading their constituent counters.
478 	 */
479 	if (!target__none(&target)) {
480 		evlist__for_each_entry(evsel_list, counter)
481 			bpf_counter__disable(counter);
482 		if (!all_counters_use_bpf)
483 			evlist__disable(evsel_list);
484 	}
485 }
486 
487 static volatile sig_atomic_t workload_exec_errno;
488 
489 /*
490  * evlist__prepare_workload will send a SIGUSR1
491  * if the fork fails, since we asked by setting its
492  * want_signal to true.
493  */
494 static void workload_exec_failed_signal(int signo __maybe_unused, siginfo_t *info,
495 					void *ucontext __maybe_unused)
496 {
497 	workload_exec_errno = info->si_value.sival_int;
498 }
499 
500 static bool evsel__should_store_id(struct evsel *counter)
501 {
502 	return STAT_RECORD || counter->core.attr.read_format & PERF_FORMAT_ID;
503 }
504 
505 static bool is_target_alive(struct target *_target,
506 			    struct perf_thread_map *threads)
507 {
508 	struct stat st;
509 	int i;
510 
511 	if (!target__has_task(_target))
512 		return true;
513 
514 	for (i = 0; i < threads->nr; i++) {
515 		char path[PATH_MAX];
516 
517 		scnprintf(path, PATH_MAX, "%s/%d", procfs__mountpoint(),
518 			  threads->map[i].pid);
519 
520 		if (!stat(path, &st))
521 			return true;
522 	}
523 
524 	return false;
525 }
526 
527 static void process_evlist(struct evlist *evlist, unsigned int interval)
528 {
529 	enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED;
530 
531 	if (evlist__ctlfd_process(evlist, &cmd) > 0) {
532 		switch (cmd) {
533 		case EVLIST_CTL_CMD_ENABLE:
534 			fallthrough;
535 		case EVLIST_CTL_CMD_DISABLE:
536 			if (interval)
537 				process_interval();
538 			break;
539 		case EVLIST_CTL_CMD_SNAPSHOT:
540 		case EVLIST_CTL_CMD_ACK:
541 		case EVLIST_CTL_CMD_UNSUPPORTED:
542 		case EVLIST_CTL_CMD_EVLIST:
543 		case EVLIST_CTL_CMD_STOP:
544 		case EVLIST_CTL_CMD_PING:
545 		default:
546 			break;
547 		}
548 	}
549 }
550 
551 static void compute_tts(struct timespec *time_start, struct timespec *time_stop,
552 			int *time_to_sleep)
553 {
554 	int tts = *time_to_sleep;
555 	struct timespec time_diff;
556 
557 	diff_timespec(&time_diff, time_stop, time_start);
558 
559 	tts -= time_diff.tv_sec * MSEC_PER_SEC +
560 	       time_diff.tv_nsec / NSEC_PER_MSEC;
561 
562 	if (tts < 0)
563 		tts = 0;
564 
565 	*time_to_sleep = tts;
566 }
567 
568 static int dispatch_events(bool forks, int timeout, int interval, int *times)
569 {
570 	int child_exited = 0, status = 0;
571 	int time_to_sleep, sleep_time;
572 	struct timespec time_start, time_stop;
573 
574 	if (interval)
575 		sleep_time = interval;
576 	else if (timeout)
577 		sleep_time = timeout;
578 	else
579 		sleep_time = 1000;
580 
581 	time_to_sleep = sleep_time;
582 
583 	while (!done) {
584 		if (forks)
585 			child_exited = waitpid(child_pid, &status, WNOHANG);
586 		else
587 			child_exited = !is_target_alive(&target, evsel_list->core.threads) ? 1 : 0;
588 
589 		if (child_exited)
590 			break;
591 
592 		clock_gettime(CLOCK_MONOTONIC, &time_start);
593 		if (!(evlist__poll(evsel_list, time_to_sleep) > 0)) { /* poll timeout or EINTR */
594 			if (timeout || handle_interval(interval, times))
595 				break;
596 			time_to_sleep = sleep_time;
597 		} else { /* fd revent */
598 			process_evlist(evsel_list, interval);
599 			clock_gettime(CLOCK_MONOTONIC, &time_stop);
600 			compute_tts(&time_start, &time_stop, &time_to_sleep);
601 		}
602 	}
603 
604 	return status;
605 }
606 
607 enum counter_recovery {
608 	COUNTER_SKIP,
609 	COUNTER_RETRY,
610 	COUNTER_FATAL,
611 };
612 
613 static enum counter_recovery stat_handle_error(struct evsel *counter)
614 {
615 	char msg[BUFSIZ];
616 	/*
617 	 * PPC returns ENXIO for HW counters until 2.6.37
618 	 * (behavior changed with commit b0a873e).
619 	 */
620 	if (errno == EINVAL || errno == ENOSYS ||
621 	    errno == ENOENT || errno == EOPNOTSUPP ||
622 	    errno == ENXIO) {
623 		if (verbose > 0)
624 			ui__warning("%s event is not supported by the kernel.\n",
625 				    evsel__name(counter));
626 		counter->supported = false;
627 		/*
628 		 * errored is a sticky flag that means one of the counter's
629 		 * cpu event had a problem and needs to be reexamined.
630 		 */
631 		counter->errored = true;
632 
633 		if ((evsel__leader(counter) != counter) ||
634 		    !(counter->core.leader->nr_members > 1))
635 			return COUNTER_SKIP;
636 	} else if (evsel__fallback(counter, &target, errno, msg, sizeof(msg))) {
637 		if (verbose > 0)
638 			ui__warning("%s\n", msg);
639 		return COUNTER_RETRY;
640 	} else if (target__has_per_thread(&target) &&
641 		   evsel_list->core.threads &&
642 		   evsel_list->core.threads->err_thread != -1) {
643 		/*
644 		 * For global --per-thread case, skip current
645 		 * error thread.
646 		 */
647 		if (!thread_map__remove(evsel_list->core.threads,
648 					evsel_list->core.threads->err_thread)) {
649 			evsel_list->core.threads->err_thread = -1;
650 			return COUNTER_RETRY;
651 		}
652 	} else if (counter->skippable) {
653 		if (verbose > 0)
654 			ui__warning("skipping event %s that kernel failed to open .\n",
655 				    evsel__name(counter));
656 		counter->supported = false;
657 		counter->errored = true;
658 		return COUNTER_SKIP;
659 	}
660 
661 	evsel__open_strerror(counter, &target, errno, msg, sizeof(msg));
662 	ui__error("%s\n", msg);
663 
664 	if (child_pid != -1)
665 		kill(child_pid, SIGTERM);
666 	return COUNTER_FATAL;
667 }
668 
669 static int __run_perf_stat(int argc, const char **argv, int run_idx)
670 {
671 	int interval = stat_config.interval;
672 	int times = stat_config.times;
673 	int timeout = stat_config.timeout;
674 	char msg[BUFSIZ];
675 	unsigned long long t0, t1;
676 	struct evsel *counter;
677 	size_t l;
678 	int status = 0;
679 	const bool forks = (argc > 0);
680 	bool is_pipe = STAT_RECORD ? perf_stat.data.is_pipe : false;
681 	struct evlist_cpu_iterator evlist_cpu_itr;
682 	struct affinity saved_affinity, *affinity = NULL;
683 	int err;
684 	bool second_pass = false;
685 
686 	if (forks) {
687 		if (evlist__prepare_workload(evsel_list, &target, argv, is_pipe, workload_exec_failed_signal) < 0) {
688 			perror("failed to prepare workload");
689 			return -1;
690 		}
691 		child_pid = evsel_list->workload.pid;
692 	}
693 
694 	if (!cpu_map__is_dummy(evsel_list->core.user_requested_cpus)) {
695 		if (affinity__setup(&saved_affinity) < 0)
696 			return -1;
697 		affinity = &saved_affinity;
698 	}
699 
700 	evlist__for_each_entry(evsel_list, counter) {
701 		counter->reset_group = false;
702 		if (bpf_counter__load(counter, &target))
703 			return -1;
704 		if (!(evsel__is_bperf(counter)))
705 			all_counters_use_bpf = false;
706 	}
707 
708 	evlist__reset_aggr_stats(evsel_list);
709 
710 	evlist__for_each_cpu(evlist_cpu_itr, evsel_list, affinity) {
711 		counter = evlist_cpu_itr.evsel;
712 
713 		/*
714 		 * bperf calls evsel__open_per_cpu() in bperf__load(), so
715 		 * no need to call it again here.
716 		 */
717 		if (target.use_bpf)
718 			break;
719 
720 		if (counter->reset_group || counter->errored)
721 			continue;
722 		if (evsel__is_bperf(counter))
723 			continue;
724 try_again:
725 		if (create_perf_stat_counter(counter, &stat_config, &target,
726 					     evlist_cpu_itr.cpu_map_idx) < 0) {
727 
728 			/*
729 			 * Weak group failed. We cannot just undo this here
730 			 * because earlier CPUs might be in group mode, and the kernel
731 			 * doesn't support mixing group and non group reads. Defer
732 			 * it to later.
733 			 * Don't close here because we're in the wrong affinity.
734 			 */
735 			if ((errno == EINVAL || errno == EBADF) &&
736 				evsel__leader(counter) != counter &&
737 				counter->weak_group) {
738 				evlist__reset_weak_group(evsel_list, counter, false);
739 				assert(counter->reset_group);
740 				second_pass = true;
741 				continue;
742 			}
743 
744 			switch (stat_handle_error(counter)) {
745 			case COUNTER_FATAL:
746 				return -1;
747 			case COUNTER_RETRY:
748 				goto try_again;
749 			case COUNTER_SKIP:
750 				continue;
751 			default:
752 				break;
753 			}
754 
755 		}
756 		counter->supported = true;
757 	}
758 
759 	if (second_pass) {
760 		/*
761 		 * Now redo all the weak group after closing them,
762 		 * and also close errored counters.
763 		 */
764 
765 		/* First close errored or weak retry */
766 		evlist__for_each_cpu(evlist_cpu_itr, evsel_list, affinity) {
767 			counter = evlist_cpu_itr.evsel;
768 
769 			if (!counter->reset_group && !counter->errored)
770 				continue;
771 
772 			perf_evsel__close_cpu(&counter->core, evlist_cpu_itr.cpu_map_idx);
773 		}
774 		/* Now reopen weak */
775 		evlist__for_each_cpu(evlist_cpu_itr, evsel_list, affinity) {
776 			counter = evlist_cpu_itr.evsel;
777 
778 			if (!counter->reset_group)
779 				continue;
780 try_again_reset:
781 			pr_debug2("reopening weak %s\n", evsel__name(counter));
782 			if (create_perf_stat_counter(counter, &stat_config, &target,
783 						     evlist_cpu_itr.cpu_map_idx) < 0) {
784 
785 				switch (stat_handle_error(counter)) {
786 				case COUNTER_FATAL:
787 					return -1;
788 				case COUNTER_RETRY:
789 					goto try_again_reset;
790 				case COUNTER_SKIP:
791 					continue;
792 				default:
793 					break;
794 				}
795 			}
796 			counter->supported = true;
797 		}
798 	}
799 	affinity__cleanup(affinity);
800 
801 	evlist__for_each_entry(evsel_list, counter) {
802 		if (!counter->supported) {
803 			perf_evsel__free_fd(&counter->core);
804 			continue;
805 		}
806 
807 		l = strlen(counter->unit);
808 		if (l > stat_config.unit_width)
809 			stat_config.unit_width = l;
810 
811 		if (evsel__should_store_id(counter) &&
812 		    evsel__store_ids(counter, evsel_list))
813 			return -1;
814 	}
815 
816 	if (evlist__apply_filters(evsel_list, &counter)) {
817 		pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n",
818 			counter->filter, evsel__name(counter), errno,
819 			str_error_r(errno, msg, sizeof(msg)));
820 		return -1;
821 	}
822 
823 	if (STAT_RECORD) {
824 		int fd = perf_data__fd(&perf_stat.data);
825 
826 		if (is_pipe) {
827 			err = perf_header__write_pipe(perf_data__fd(&perf_stat.data));
828 		} else {
829 			err = perf_session__write_header(perf_stat.session, evsel_list,
830 							 fd, false);
831 		}
832 
833 		if (err < 0)
834 			return err;
835 
836 		err = perf_event__synthesize_stat_events(&stat_config, NULL, evsel_list,
837 							 process_synthesized_event, is_pipe);
838 		if (err < 0)
839 			return err;
840 	}
841 
842 	if (target.initial_delay) {
843 		pr_info(EVLIST_DISABLED_MSG);
844 	} else {
845 		err = enable_counters();
846 		if (err)
847 			return -1;
848 	}
849 
850 	/* Exec the command, if any */
851 	if (forks)
852 		evlist__start_workload(evsel_list);
853 
854 	if (target.initial_delay > 0) {
855 		usleep(target.initial_delay * USEC_PER_MSEC);
856 		err = enable_counters();
857 		if (err)
858 			return -1;
859 
860 		pr_info(EVLIST_ENABLED_MSG);
861 	}
862 
863 	t0 = rdclock();
864 	clock_gettime(CLOCK_MONOTONIC, &ref_time);
865 
866 	if (forks) {
867 		if (interval || timeout || evlist__ctlfd_initialized(evsel_list))
868 			status = dispatch_events(forks, timeout, interval, &times);
869 		if (child_pid != -1) {
870 			if (timeout)
871 				kill(child_pid, SIGTERM);
872 			wait4(child_pid, &status, 0, &stat_config.ru_data);
873 		}
874 
875 		if (workload_exec_errno) {
876 			const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
877 			pr_err("Workload failed: %s\n", emsg);
878 			return -1;
879 		}
880 
881 		if (WIFSIGNALED(status))
882 			psignal(WTERMSIG(status), argv[0]);
883 	} else {
884 		status = dispatch_events(forks, timeout, interval, &times);
885 	}
886 
887 	disable_counters();
888 
889 	t1 = rdclock();
890 
891 	if (stat_config.walltime_run_table)
892 		stat_config.walltime_run[run_idx] = t1 - t0;
893 
894 	if (interval && stat_config.summary) {
895 		stat_config.interval = 0;
896 		stat_config.stop_read_counter = true;
897 		init_stats(&walltime_nsecs_stats);
898 		update_stats(&walltime_nsecs_stats, t1 - t0);
899 
900 		evlist__copy_prev_raw_counts(evsel_list);
901 		evlist__reset_prev_raw_counts(evsel_list);
902 		evlist__reset_aggr_stats(evsel_list);
903 	} else {
904 		update_stats(&walltime_nsecs_stats, t1 - t0);
905 		update_rusage_stats(&ru_stats, &stat_config.ru_data);
906 	}
907 
908 	/*
909 	 * Closing a group leader splits the group, and as we only disable
910 	 * group leaders, results in remaining events becoming enabled. To
911 	 * avoid arbitrary skew, we must read all counters before closing any
912 	 * group leaders.
913 	 */
914 	if (read_counters(&(struct timespec) { .tv_nsec = t1-t0 }) == 0)
915 		process_counters();
916 
917 	/*
918 	 * We need to keep evsel_list alive, because it's processed
919 	 * later the evsel_list will be closed after.
920 	 */
921 	if (!STAT_RECORD)
922 		evlist__close(evsel_list);
923 
924 	return WEXITSTATUS(status);
925 }
926 
927 static int run_perf_stat(int argc, const char **argv, int run_idx)
928 {
929 	int ret;
930 
931 	if (pre_cmd) {
932 		ret = system(pre_cmd);
933 		if (ret)
934 			return ret;
935 	}
936 
937 	if (sync_run)
938 		sync();
939 
940 	ret = __run_perf_stat(argc, argv, run_idx);
941 	if (ret)
942 		return ret;
943 
944 	if (post_cmd) {
945 		ret = system(post_cmd);
946 		if (ret)
947 			return ret;
948 	}
949 
950 	return ret;
951 }
952 
953 static void print_counters(struct timespec *ts, int argc, const char **argv)
954 {
955 	/* Do not print anything if we record to the pipe. */
956 	if (STAT_RECORD && perf_stat.data.is_pipe)
957 		return;
958 	if (quiet)
959 		return;
960 
961 	evlist__print_counters(evsel_list, &stat_config, &target, ts, argc, argv);
962 }
963 
964 static volatile sig_atomic_t signr = -1;
965 
966 static void skip_signal(int signo)
967 {
968 	if ((child_pid == -1) || stat_config.interval)
969 		done = 1;
970 
971 	signr = signo;
972 	/*
973 	 * render child_pid harmless
974 	 * won't send SIGTERM to a random
975 	 * process in case of race condition
976 	 * and fast PID recycling
977 	 */
978 	child_pid = -1;
979 }
980 
981 static void sig_atexit(void)
982 {
983 	sigset_t set, oset;
984 
985 	/*
986 	 * avoid race condition with SIGCHLD handler
987 	 * in skip_signal() which is modifying child_pid
988 	 * goal is to avoid send SIGTERM to a random
989 	 * process
990 	 */
991 	sigemptyset(&set);
992 	sigaddset(&set, SIGCHLD);
993 	sigprocmask(SIG_BLOCK, &set, &oset);
994 
995 	if (child_pid != -1)
996 		kill(child_pid, SIGTERM);
997 
998 	sigprocmask(SIG_SETMASK, &oset, NULL);
999 
1000 	if (signr == -1)
1001 		return;
1002 
1003 	signal(signr, SIG_DFL);
1004 	kill(getpid(), signr);
1005 }
1006 
1007 void perf_stat__set_big_num(int set)
1008 {
1009 	stat_config.big_num = (set != 0);
1010 }
1011 
1012 void perf_stat__set_no_csv_summary(int set)
1013 {
1014 	stat_config.no_csv_summary = (set != 0);
1015 }
1016 
1017 static int stat__set_big_num(const struct option *opt __maybe_unused,
1018 			     const char *s __maybe_unused, int unset)
1019 {
1020 	big_num_opt = unset ? 0 : 1;
1021 	perf_stat__set_big_num(!unset);
1022 	return 0;
1023 }
1024 
1025 static int enable_metric_only(const struct option *opt __maybe_unused,
1026 			      const char *s __maybe_unused, int unset)
1027 {
1028 	force_metric_only = true;
1029 	stat_config.metric_only = !unset;
1030 	return 0;
1031 }
1032 
1033 static int append_metric_groups(const struct option *opt __maybe_unused,
1034 			       const char *str,
1035 			       int unset __maybe_unused)
1036 {
1037 	if (metrics) {
1038 		char *tmp;
1039 
1040 		if (asprintf(&tmp, "%s,%s", metrics, str) < 0)
1041 			return -ENOMEM;
1042 		free(metrics);
1043 		metrics = tmp;
1044 	} else {
1045 		metrics = strdup(str);
1046 		if (!metrics)
1047 			return -ENOMEM;
1048 	}
1049 	return 0;
1050 }
1051 
1052 static int parse_control_option(const struct option *opt,
1053 				const char *str,
1054 				int unset __maybe_unused)
1055 {
1056 	struct perf_stat_config *config = opt->value;
1057 
1058 	return evlist__parse_control(str, &config->ctl_fd, &config->ctl_fd_ack, &config->ctl_fd_close);
1059 }
1060 
1061 static int parse_stat_cgroups(const struct option *opt,
1062 			      const char *str, int unset)
1063 {
1064 	if (stat_config.cgroup_list) {
1065 		pr_err("--cgroup and --for-each-cgroup cannot be used together\n");
1066 		return -1;
1067 	}
1068 
1069 	return parse_cgroups(opt, str, unset);
1070 }
1071 
1072 static int parse_cputype(const struct option *opt,
1073 			     const char *str,
1074 			     int unset __maybe_unused)
1075 {
1076 	const struct perf_pmu *pmu;
1077 	struct evlist *evlist = *(struct evlist **)opt->value;
1078 
1079 	if (!list_empty(&evlist->core.entries)) {
1080 		fprintf(stderr, "Must define cputype before events/metrics\n");
1081 		return -1;
1082 	}
1083 
1084 	pmu = perf_pmus__pmu_for_pmu_filter(str);
1085 	if (!pmu) {
1086 		fprintf(stderr, "--cputype %s is not supported!\n", str);
1087 		return -1;
1088 	}
1089 	parse_events_option_args.pmu_filter = pmu->name;
1090 
1091 	return 0;
1092 }
1093 
1094 static int parse_cache_level(const struct option *opt,
1095 			     const char *str,
1096 			     int unset __maybe_unused)
1097 {
1098 	int level;
1099 	u32 *aggr_mode = (u32 *)opt->value;
1100 	u32 *aggr_level = (u32 *)opt->data;
1101 
1102 	/*
1103 	 * If no string is specified, aggregate based on the topology of
1104 	 * Last Level Cache (LLC). Since the LLC level can change from
1105 	 * architecture to architecture, set level greater than
1106 	 * MAX_CACHE_LVL which will be interpreted as LLC.
1107 	 */
1108 	if (str == NULL) {
1109 		level = MAX_CACHE_LVL + 1;
1110 		goto out;
1111 	}
1112 
1113 	/*
1114 	 * The format to specify cache level is LX or lX where X is the
1115 	 * cache level.
1116 	 */
1117 	if (strlen(str) != 2 || (str[0] != 'l' && str[0] != 'L')) {
1118 		pr_err("Cache level must be of form L[1-%d], or l[1-%d]\n",
1119 		       MAX_CACHE_LVL,
1120 		       MAX_CACHE_LVL);
1121 		return -EINVAL;
1122 	}
1123 
1124 	level = atoi(&str[1]);
1125 	if (level < 1) {
1126 		pr_err("Cache level must be of form L[1-%d], or l[1-%d]\n",
1127 		       MAX_CACHE_LVL,
1128 		       MAX_CACHE_LVL);
1129 		return -EINVAL;
1130 	}
1131 
1132 	if (level > MAX_CACHE_LVL) {
1133 		pr_err("perf only supports max cache level of %d.\n"
1134 		       "Consider increasing MAX_CACHE_LVL\n", MAX_CACHE_LVL);
1135 		return -EINVAL;
1136 	}
1137 out:
1138 	*aggr_mode = AGGR_CACHE;
1139 	*aggr_level = level;
1140 	return 0;
1141 }
1142 
1143 static struct option stat_options[] = {
1144 	OPT_BOOLEAN('T', "transaction", &transaction_run,
1145 		    "hardware transaction statistics"),
1146 	OPT_CALLBACK('e', "event", &parse_events_option_args, "event",
1147 		     "event selector. use 'perf list' to list available events",
1148 		     parse_events_option),
1149 	OPT_CALLBACK(0, "filter", &evsel_list, "filter",
1150 		     "event filter", parse_filter),
1151 	OPT_BOOLEAN('i', "no-inherit", &stat_config.no_inherit,
1152 		    "child tasks do not inherit counters"),
1153 	OPT_STRING('p', "pid", &target.pid, "pid",
1154 		   "stat events on existing process id"),
1155 	OPT_STRING('t', "tid", &target.tid, "tid",
1156 		   "stat events on existing thread id"),
1157 #ifdef HAVE_BPF_SKEL
1158 	OPT_STRING('b', "bpf-prog", &target.bpf_str, "bpf-prog-id",
1159 		   "stat events on existing bpf program id"),
1160 	OPT_BOOLEAN(0, "bpf-counters", &target.use_bpf,
1161 		    "use bpf program to count events"),
1162 	OPT_STRING(0, "bpf-attr-map", &target.attr_map, "attr-map-path",
1163 		   "path to perf_event_attr map"),
1164 #endif
1165 	OPT_BOOLEAN('a', "all-cpus", &target.system_wide,
1166 		    "system-wide collection from all CPUs"),
1167 	OPT_BOOLEAN(0, "scale", &stat_config.scale,
1168 		    "Use --no-scale to disable counter scaling for multiplexing"),
1169 	OPT_INCR('v', "verbose", &verbose,
1170 		    "be more verbose (show counter open errors, etc)"),
1171 	OPT_INTEGER('r', "repeat", &stat_config.run_count,
1172 		    "repeat command and print average + stddev (max: 100, forever: 0)"),
1173 	OPT_BOOLEAN(0, "table", &stat_config.walltime_run_table,
1174 		    "display details about each run (only with -r option)"),
1175 	OPT_BOOLEAN('n', "null", &stat_config.null_run,
1176 		    "null run - dont start any counters"),
1177 	OPT_INCR('d', "detailed", &detailed_run,
1178 		    "detailed run - start a lot of events"),
1179 	OPT_BOOLEAN('S', "sync", &sync_run,
1180 		    "call sync() before starting a run"),
1181 	OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL,
1182 			   "print large numbers with thousands\' separators",
1183 			   stat__set_big_num),
1184 	OPT_STRING('C', "cpu", &target.cpu_list, "cpu",
1185 		    "list of cpus to monitor in system-wide"),
1186 	OPT_SET_UINT('A', "no-aggr", &stat_config.aggr_mode,
1187 		    "disable aggregation across CPUs or PMUs", AGGR_NONE),
1188 	OPT_SET_UINT(0, "no-merge", &stat_config.aggr_mode,
1189 		    "disable aggregation the same as -A or -no-aggr", AGGR_NONE),
1190 	OPT_BOOLEAN(0, "hybrid-merge", &stat_config.hybrid_merge,
1191 		    "Merge identical named hybrid events"),
1192 	OPT_STRING('x', "field-separator", &stat_config.csv_sep, "separator",
1193 		   "print counts with custom separator"),
1194 	OPT_BOOLEAN('j', "json-output", &stat_config.json_output,
1195 		   "print counts in JSON format"),
1196 	OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
1197 		     "monitor event in cgroup name only", parse_stat_cgroups),
1198 	OPT_STRING(0, "for-each-cgroup", &stat_config.cgroup_list, "name",
1199 		    "expand events for each cgroup"),
1200 	OPT_STRING('o', "output", &output_name, "file", "output file name"),
1201 	OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
1202 	OPT_INTEGER(0, "log-fd", &output_fd,
1203 		    "log output to fd, instead of stderr"),
1204 	OPT_STRING(0, "pre", &pre_cmd, "command",
1205 			"command to run prior to the measured command"),
1206 	OPT_STRING(0, "post", &post_cmd, "command",
1207 			"command to run after to the measured command"),
1208 	OPT_UINTEGER('I', "interval-print", &stat_config.interval,
1209 		    "print counts at regular interval in ms "
1210 		    "(overhead is possible for values <= 100ms)"),
1211 	OPT_INTEGER(0, "interval-count", &stat_config.times,
1212 		    "print counts for fixed number of times"),
1213 	OPT_BOOLEAN(0, "interval-clear", &stat_config.interval_clear,
1214 		    "clear screen in between new interval"),
1215 	OPT_UINTEGER(0, "timeout", &stat_config.timeout,
1216 		    "stop workload and print counts after a timeout period in ms (>= 10ms)"),
1217 	OPT_SET_UINT(0, "per-socket", &stat_config.aggr_mode,
1218 		     "aggregate counts per processor socket", AGGR_SOCKET),
1219 	OPT_SET_UINT(0, "per-die", &stat_config.aggr_mode,
1220 		     "aggregate counts per processor die", AGGR_DIE),
1221 	OPT_SET_UINT(0, "per-cluster", &stat_config.aggr_mode,
1222 		     "aggregate counts per processor cluster", AGGR_CLUSTER),
1223 	OPT_CALLBACK_OPTARG(0, "per-cache", &stat_config.aggr_mode, &stat_config.aggr_level,
1224 			    "cache level", "aggregate count at this cache level (Default: LLC)",
1225 			    parse_cache_level),
1226 	OPT_SET_UINT(0, "per-core", &stat_config.aggr_mode,
1227 		     "aggregate counts per physical processor core", AGGR_CORE),
1228 	OPT_SET_UINT(0, "per-thread", &stat_config.aggr_mode,
1229 		     "aggregate counts per thread", AGGR_THREAD),
1230 	OPT_SET_UINT(0, "per-node", &stat_config.aggr_mode,
1231 		     "aggregate counts per numa node", AGGR_NODE),
1232 	OPT_INTEGER('D', "delay", &target.initial_delay,
1233 		    "ms to wait before starting measurement after program start (-1: start with events disabled)"),
1234 	OPT_CALLBACK_NOOPT(0, "metric-only", &stat_config.metric_only, NULL,
1235 			"Only print computed metrics. No raw values", enable_metric_only),
1236 	OPT_BOOLEAN(0, "metric-no-group", &stat_config.metric_no_group,
1237 		       "don't group metric events, impacts multiplexing"),
1238 	OPT_BOOLEAN(0, "metric-no-merge", &stat_config.metric_no_merge,
1239 		       "don't try to share events between metrics in a group"),
1240 	OPT_BOOLEAN(0, "metric-no-threshold", &stat_config.metric_no_threshold,
1241 		       "disable adding events for the metric threshold calculation"),
1242 	OPT_BOOLEAN(0, "topdown", &topdown_run,
1243 			"measure top-down statistics"),
1244 	OPT_UINTEGER(0, "td-level", &stat_config.topdown_level,
1245 			"Set the metrics level for the top-down statistics (0: max level)"),
1246 	OPT_BOOLEAN(0, "smi-cost", &smi_cost,
1247 			"measure SMI cost"),
1248 	OPT_CALLBACK('M', "metrics", &evsel_list, "metric/metric group list",
1249 		     "monitor specified metrics or metric groups (separated by ,)",
1250 		     append_metric_groups),
1251 	OPT_BOOLEAN_FLAG(0, "all-kernel", &stat_config.all_kernel,
1252 			 "Configure all used events to run in kernel space.",
1253 			 PARSE_OPT_EXCLUSIVE),
1254 	OPT_BOOLEAN_FLAG(0, "all-user", &stat_config.all_user,
1255 			 "Configure all used events to run in user space.",
1256 			 PARSE_OPT_EXCLUSIVE),
1257 	OPT_BOOLEAN(0, "percore-show-thread", &stat_config.percore_show_thread,
1258 		    "Use with 'percore' event qualifier to show the event "
1259 		    "counts of one hardware thread by sum up total hardware "
1260 		    "threads of same physical core"),
1261 	OPT_BOOLEAN(0, "summary", &stat_config.summary,
1262 		       "print summary for interval mode"),
1263 	OPT_BOOLEAN(0, "no-csv-summary", &stat_config.no_csv_summary,
1264 		       "don't print 'summary' for CSV summary output"),
1265 	OPT_BOOLEAN(0, "quiet", &quiet,
1266 			"don't print any output, messages or warnings (useful with record)"),
1267 	OPT_CALLBACK(0, "cputype", &evsel_list, "hybrid cpu type",
1268 		     "Only enable events on applying cpu with this type "
1269 		     "for hybrid platform (e.g. core or atom)",
1270 		     parse_cputype),
1271 #ifdef HAVE_LIBPFM
1272 	OPT_CALLBACK(0, "pfm-events", &evsel_list, "event",
1273 		"libpfm4 event selector. use 'perf list' to list available events",
1274 		parse_libpfm_events_option),
1275 #endif
1276 	OPT_CALLBACK(0, "control", &stat_config, "fd:ctl-fd[,ack-fd] or fifo:ctl-fifo[,ack-fifo]",
1277 		     "Listen on ctl-fd descriptor for command to control measurement ('enable': enable events, 'disable': disable events).\n"
1278 		     "\t\t\t  Optionally send control command completion ('ack\\n') to ack-fd descriptor.\n"
1279 		     "\t\t\t  Alternatively, ctl-fifo / ack-fifo will be opened and used as ctl-fd / ack-fd.",
1280 		      parse_control_option),
1281 	OPT_CALLBACK_OPTARG(0, "iostat", &evsel_list, &stat_config, "default",
1282 			    "measure I/O performance metrics provided by arch/platform",
1283 			    iostat_parse),
1284 	OPT_END()
1285 };
1286 
1287 /**
1288  * Calculate the cache instance ID from the map in
1289  * /sys/devices/system/cpu/cpuX/cache/indexY/shared_cpu_list
1290  * Cache instance ID is the first CPU reported in the shared_cpu_list file.
1291  */
1292 static int cpu__get_cache_id_from_map(struct perf_cpu cpu, char *map)
1293 {
1294 	int id;
1295 	struct perf_cpu_map *cpu_map = perf_cpu_map__new(map);
1296 
1297 	/*
1298 	 * If the map contains no CPU, consider the current CPU to
1299 	 * be the first online CPU in the cache domain else use the
1300 	 * first online CPU of the cache domain as the ID.
1301 	 */
1302 	id = perf_cpu_map__min(cpu_map).cpu;
1303 	if (id == -1)
1304 		id = cpu.cpu;
1305 
1306 	/* Free the perf_cpu_map used to find the cache ID */
1307 	perf_cpu_map__put(cpu_map);
1308 
1309 	return id;
1310 }
1311 
1312 /**
1313  * cpu__get_cache_id - Returns 0 if successful in populating the
1314  * cache level and cache id. Cache level is read from
1315  * /sys/devices/system/cpu/cpuX/cache/indexY/level where as cache instance ID
1316  * is the first CPU reported by
1317  * /sys/devices/system/cpu/cpuX/cache/indexY/shared_cpu_list
1318  */
1319 static int cpu__get_cache_details(struct perf_cpu cpu, struct perf_cache *cache)
1320 {
1321 	int ret = 0;
1322 	u32 cache_level = stat_config.aggr_level;
1323 	struct cpu_cache_level caches[MAX_CACHE_LVL];
1324 	u32 i = 0, caches_cnt = 0;
1325 
1326 	cache->cache_lvl = (cache_level > MAX_CACHE_LVL) ? 0 : cache_level;
1327 	cache->cache = -1;
1328 
1329 	ret = build_caches_for_cpu(cpu.cpu, caches, &caches_cnt);
1330 	if (ret) {
1331 		/*
1332 		 * If caches_cnt is not 0, cpu_cache_level data
1333 		 * was allocated when building the topology.
1334 		 * Free the allocated data before returning.
1335 		 */
1336 		if (caches_cnt)
1337 			goto free_caches;
1338 
1339 		return ret;
1340 	}
1341 
1342 	if (!caches_cnt)
1343 		return -1;
1344 
1345 	/*
1346 	 * Save the data for the highest level if no
1347 	 * level was specified by the user.
1348 	 */
1349 	if (cache_level > MAX_CACHE_LVL) {
1350 		int max_level_index = 0;
1351 
1352 		for (i = 1; i < caches_cnt; ++i) {
1353 			if (caches[i].level > caches[max_level_index].level)
1354 				max_level_index = i;
1355 		}
1356 
1357 		cache->cache_lvl = caches[max_level_index].level;
1358 		cache->cache = cpu__get_cache_id_from_map(cpu, caches[max_level_index].map);
1359 
1360 		/* Reset i to 0 to free entire caches[] */
1361 		i = 0;
1362 		goto free_caches;
1363 	}
1364 
1365 	for (i = 0; i < caches_cnt; ++i) {
1366 		if (caches[i].level == cache_level) {
1367 			cache->cache_lvl = cache_level;
1368 			cache->cache = cpu__get_cache_id_from_map(cpu, caches[i].map);
1369 		}
1370 
1371 		cpu_cache_level__free(&caches[i]);
1372 	}
1373 
1374 free_caches:
1375 	/*
1376 	 * Free all the allocated cpu_cache_level data.
1377 	 */
1378 	while (i < caches_cnt)
1379 		cpu_cache_level__free(&caches[i++]);
1380 
1381 	return ret;
1382 }
1383 
1384 /**
1385  * aggr_cpu_id__cache - Create an aggr_cpu_id with cache instache ID, cache
1386  * level, die and socket populated with the cache instache ID, cache level,
1387  * die and socket for cpu. The function signature is compatible with
1388  * aggr_cpu_id_get_t.
1389  */
1390 static struct aggr_cpu_id aggr_cpu_id__cache(struct perf_cpu cpu, void *data)
1391 {
1392 	int ret;
1393 	struct aggr_cpu_id id;
1394 	struct perf_cache cache;
1395 
1396 	id = aggr_cpu_id__die(cpu, data);
1397 	if (aggr_cpu_id__is_empty(&id))
1398 		return id;
1399 
1400 	ret = cpu__get_cache_details(cpu, &cache);
1401 	if (ret)
1402 		return id;
1403 
1404 	id.cache_lvl = cache.cache_lvl;
1405 	id.cache = cache.cache;
1406 	return id;
1407 }
1408 
1409 static const char *const aggr_mode__string[] = {
1410 	[AGGR_CORE] = "core",
1411 	[AGGR_CACHE] = "cache",
1412 	[AGGR_CLUSTER] = "cluster",
1413 	[AGGR_DIE] = "die",
1414 	[AGGR_GLOBAL] = "global",
1415 	[AGGR_NODE] = "node",
1416 	[AGGR_NONE] = "none",
1417 	[AGGR_SOCKET] = "socket",
1418 	[AGGR_THREAD] = "thread",
1419 	[AGGR_UNSET] = "unset",
1420 };
1421 
1422 static struct aggr_cpu_id perf_stat__get_socket(struct perf_stat_config *config __maybe_unused,
1423 						struct perf_cpu cpu)
1424 {
1425 	return aggr_cpu_id__socket(cpu, /*data=*/NULL);
1426 }
1427 
1428 static struct aggr_cpu_id perf_stat__get_die(struct perf_stat_config *config __maybe_unused,
1429 					     struct perf_cpu cpu)
1430 {
1431 	return aggr_cpu_id__die(cpu, /*data=*/NULL);
1432 }
1433 
1434 static struct aggr_cpu_id perf_stat__get_cache_id(struct perf_stat_config *config __maybe_unused,
1435 						  struct perf_cpu cpu)
1436 {
1437 	return aggr_cpu_id__cache(cpu, /*data=*/NULL);
1438 }
1439 
1440 static struct aggr_cpu_id perf_stat__get_cluster(struct perf_stat_config *config __maybe_unused,
1441 						 struct perf_cpu cpu)
1442 {
1443 	return aggr_cpu_id__cluster(cpu, /*data=*/NULL);
1444 }
1445 
1446 static struct aggr_cpu_id perf_stat__get_core(struct perf_stat_config *config __maybe_unused,
1447 					      struct perf_cpu cpu)
1448 {
1449 	return aggr_cpu_id__core(cpu, /*data=*/NULL);
1450 }
1451 
1452 static struct aggr_cpu_id perf_stat__get_node(struct perf_stat_config *config __maybe_unused,
1453 					      struct perf_cpu cpu)
1454 {
1455 	return aggr_cpu_id__node(cpu, /*data=*/NULL);
1456 }
1457 
1458 static struct aggr_cpu_id perf_stat__get_global(struct perf_stat_config *config __maybe_unused,
1459 						struct perf_cpu cpu)
1460 {
1461 	return aggr_cpu_id__global(cpu, /*data=*/NULL);
1462 }
1463 
1464 static struct aggr_cpu_id perf_stat__get_cpu(struct perf_stat_config *config __maybe_unused,
1465 					     struct perf_cpu cpu)
1466 {
1467 	return aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1468 }
1469 
1470 static struct aggr_cpu_id perf_stat__get_aggr(struct perf_stat_config *config,
1471 					      aggr_get_id_t get_id, struct perf_cpu cpu)
1472 {
1473 	struct aggr_cpu_id id;
1474 
1475 	/* per-process mode - should use global aggr mode */
1476 	if (cpu.cpu == -1)
1477 		return get_id(config, cpu);
1478 
1479 	if (aggr_cpu_id__is_empty(&config->cpus_aggr_map->map[cpu.cpu]))
1480 		config->cpus_aggr_map->map[cpu.cpu] = get_id(config, cpu);
1481 
1482 	id = config->cpus_aggr_map->map[cpu.cpu];
1483 	return id;
1484 }
1485 
1486 static struct aggr_cpu_id perf_stat__get_socket_cached(struct perf_stat_config *config,
1487 						       struct perf_cpu cpu)
1488 {
1489 	return perf_stat__get_aggr(config, perf_stat__get_socket, cpu);
1490 }
1491 
1492 static struct aggr_cpu_id perf_stat__get_die_cached(struct perf_stat_config *config,
1493 						    struct perf_cpu cpu)
1494 {
1495 	return perf_stat__get_aggr(config, perf_stat__get_die, cpu);
1496 }
1497 
1498 static struct aggr_cpu_id perf_stat__get_cluster_cached(struct perf_stat_config *config,
1499 							struct perf_cpu cpu)
1500 {
1501 	return perf_stat__get_aggr(config, perf_stat__get_cluster, cpu);
1502 }
1503 
1504 static struct aggr_cpu_id perf_stat__get_cache_id_cached(struct perf_stat_config *config,
1505 							 struct perf_cpu cpu)
1506 {
1507 	return perf_stat__get_aggr(config, perf_stat__get_cache_id, cpu);
1508 }
1509 
1510 static struct aggr_cpu_id perf_stat__get_core_cached(struct perf_stat_config *config,
1511 						     struct perf_cpu cpu)
1512 {
1513 	return perf_stat__get_aggr(config, perf_stat__get_core, cpu);
1514 }
1515 
1516 static struct aggr_cpu_id perf_stat__get_node_cached(struct perf_stat_config *config,
1517 						     struct perf_cpu cpu)
1518 {
1519 	return perf_stat__get_aggr(config, perf_stat__get_node, cpu);
1520 }
1521 
1522 static struct aggr_cpu_id perf_stat__get_global_cached(struct perf_stat_config *config,
1523 						       struct perf_cpu cpu)
1524 {
1525 	return perf_stat__get_aggr(config, perf_stat__get_global, cpu);
1526 }
1527 
1528 static struct aggr_cpu_id perf_stat__get_cpu_cached(struct perf_stat_config *config,
1529 						    struct perf_cpu cpu)
1530 {
1531 	return perf_stat__get_aggr(config, perf_stat__get_cpu, cpu);
1532 }
1533 
1534 static aggr_cpu_id_get_t aggr_mode__get_aggr(enum aggr_mode aggr_mode)
1535 {
1536 	switch (aggr_mode) {
1537 	case AGGR_SOCKET:
1538 		return aggr_cpu_id__socket;
1539 	case AGGR_DIE:
1540 		return aggr_cpu_id__die;
1541 	case AGGR_CLUSTER:
1542 		return aggr_cpu_id__cluster;
1543 	case AGGR_CACHE:
1544 		return aggr_cpu_id__cache;
1545 	case AGGR_CORE:
1546 		return aggr_cpu_id__core;
1547 	case AGGR_NODE:
1548 		return aggr_cpu_id__node;
1549 	case AGGR_NONE:
1550 		return aggr_cpu_id__cpu;
1551 	case AGGR_GLOBAL:
1552 		return aggr_cpu_id__global;
1553 	case AGGR_THREAD:
1554 	case AGGR_UNSET:
1555 	case AGGR_MAX:
1556 	default:
1557 		return NULL;
1558 	}
1559 }
1560 
1561 static aggr_get_id_t aggr_mode__get_id(enum aggr_mode aggr_mode)
1562 {
1563 	switch (aggr_mode) {
1564 	case AGGR_SOCKET:
1565 		return perf_stat__get_socket_cached;
1566 	case AGGR_DIE:
1567 		return perf_stat__get_die_cached;
1568 	case AGGR_CLUSTER:
1569 		return perf_stat__get_cluster_cached;
1570 	case AGGR_CACHE:
1571 		return perf_stat__get_cache_id_cached;
1572 	case AGGR_CORE:
1573 		return perf_stat__get_core_cached;
1574 	case AGGR_NODE:
1575 		return perf_stat__get_node_cached;
1576 	case AGGR_NONE:
1577 		return perf_stat__get_cpu_cached;
1578 	case AGGR_GLOBAL:
1579 		return perf_stat__get_global_cached;
1580 	case AGGR_THREAD:
1581 	case AGGR_UNSET:
1582 	case AGGR_MAX:
1583 	default:
1584 		return NULL;
1585 	}
1586 }
1587 
1588 static int perf_stat_init_aggr_mode(void)
1589 {
1590 	int nr;
1591 	aggr_cpu_id_get_t get_id = aggr_mode__get_aggr(stat_config.aggr_mode);
1592 
1593 	if (get_id) {
1594 		bool needs_sort = stat_config.aggr_mode != AGGR_NONE;
1595 		stat_config.aggr_map = cpu_aggr_map__new(evsel_list->core.user_requested_cpus,
1596 							 get_id, /*data=*/NULL, needs_sort);
1597 		if (!stat_config.aggr_map) {
1598 			pr_err("cannot build %s map\n", aggr_mode__string[stat_config.aggr_mode]);
1599 			return -1;
1600 		}
1601 		stat_config.aggr_get_id = aggr_mode__get_id(stat_config.aggr_mode);
1602 	}
1603 
1604 	if (stat_config.aggr_mode == AGGR_THREAD) {
1605 		nr = perf_thread_map__nr(evsel_list->core.threads);
1606 		stat_config.aggr_map = cpu_aggr_map__empty_new(nr);
1607 		if (stat_config.aggr_map == NULL)
1608 			return -ENOMEM;
1609 
1610 		for (int s = 0; s < nr; s++) {
1611 			struct aggr_cpu_id id = aggr_cpu_id__empty();
1612 
1613 			id.thread_idx = s;
1614 			stat_config.aggr_map->map[s] = id;
1615 		}
1616 		return 0;
1617 	}
1618 
1619 	/*
1620 	 * The evsel_list->cpus is the base we operate on,
1621 	 * taking the highest cpu number to be the size of
1622 	 * the aggregation translate cpumap.
1623 	 */
1624 	if (!perf_cpu_map__is_any_cpu_or_is_empty(evsel_list->core.user_requested_cpus))
1625 		nr = perf_cpu_map__max(evsel_list->core.user_requested_cpus).cpu;
1626 	else
1627 		nr = 0;
1628 	stat_config.cpus_aggr_map = cpu_aggr_map__empty_new(nr + 1);
1629 	return stat_config.cpus_aggr_map ? 0 : -ENOMEM;
1630 }
1631 
1632 static void cpu_aggr_map__delete(struct cpu_aggr_map *map)
1633 {
1634 	free(map);
1635 }
1636 
1637 static void perf_stat__exit_aggr_mode(void)
1638 {
1639 	cpu_aggr_map__delete(stat_config.aggr_map);
1640 	cpu_aggr_map__delete(stat_config.cpus_aggr_map);
1641 	stat_config.aggr_map = NULL;
1642 	stat_config.cpus_aggr_map = NULL;
1643 }
1644 
1645 static struct aggr_cpu_id perf_env__get_socket_aggr_by_cpu(struct perf_cpu cpu, void *data)
1646 {
1647 	struct perf_env *env = data;
1648 	struct aggr_cpu_id id = aggr_cpu_id__empty();
1649 
1650 	if (cpu.cpu != -1)
1651 		id.socket = env->cpu[cpu.cpu].socket_id;
1652 
1653 	return id;
1654 }
1655 
1656 static struct aggr_cpu_id perf_env__get_die_aggr_by_cpu(struct perf_cpu cpu, void *data)
1657 {
1658 	struct perf_env *env = data;
1659 	struct aggr_cpu_id id = aggr_cpu_id__empty();
1660 
1661 	if (cpu.cpu != -1) {
1662 		/*
1663 		 * die_id is relative to socket, so start
1664 		 * with the socket ID and then add die to
1665 		 * make a unique ID.
1666 		 */
1667 		id.socket = env->cpu[cpu.cpu].socket_id;
1668 		id.die = env->cpu[cpu.cpu].die_id;
1669 	}
1670 
1671 	return id;
1672 }
1673 
1674 static void perf_env__get_cache_id_for_cpu(struct perf_cpu cpu, struct perf_env *env,
1675 					   u32 cache_level, struct aggr_cpu_id *id)
1676 {
1677 	int i;
1678 	int caches_cnt = env->caches_cnt;
1679 	struct cpu_cache_level *caches = env->caches;
1680 
1681 	id->cache_lvl = (cache_level > MAX_CACHE_LVL) ? 0 : cache_level;
1682 	id->cache = -1;
1683 
1684 	if (!caches_cnt)
1685 		return;
1686 
1687 	for (i = caches_cnt - 1; i > -1; --i) {
1688 		struct perf_cpu_map *cpu_map;
1689 		int map_contains_cpu;
1690 
1691 		/*
1692 		 * If user has not specified a level, find the fist level with
1693 		 * the cpu in the map. Since building the map is expensive, do
1694 		 * this only if levels match.
1695 		 */
1696 		if (cache_level <= MAX_CACHE_LVL && caches[i].level != cache_level)
1697 			continue;
1698 
1699 		cpu_map = perf_cpu_map__new(caches[i].map);
1700 		map_contains_cpu = perf_cpu_map__idx(cpu_map, cpu);
1701 		perf_cpu_map__put(cpu_map);
1702 
1703 		if (map_contains_cpu != -1) {
1704 			id->cache_lvl = caches[i].level;
1705 			id->cache = cpu__get_cache_id_from_map(cpu, caches[i].map);
1706 			return;
1707 		}
1708 	}
1709 }
1710 
1711 static struct aggr_cpu_id perf_env__get_cache_aggr_by_cpu(struct perf_cpu cpu,
1712 							  void *data)
1713 {
1714 	struct perf_env *env = data;
1715 	struct aggr_cpu_id id = aggr_cpu_id__empty();
1716 
1717 	if (cpu.cpu != -1) {
1718 		u32 cache_level = (perf_stat.aggr_level) ?: stat_config.aggr_level;
1719 
1720 		id.socket = env->cpu[cpu.cpu].socket_id;
1721 		id.die = env->cpu[cpu.cpu].die_id;
1722 		perf_env__get_cache_id_for_cpu(cpu, env, cache_level, &id);
1723 	}
1724 
1725 	return id;
1726 }
1727 
1728 static struct aggr_cpu_id perf_env__get_cluster_aggr_by_cpu(struct perf_cpu cpu,
1729 							    void *data)
1730 {
1731 	struct perf_env *env = data;
1732 	struct aggr_cpu_id id = aggr_cpu_id__empty();
1733 
1734 	if (cpu.cpu != -1) {
1735 		id.socket = env->cpu[cpu.cpu].socket_id;
1736 		id.die = env->cpu[cpu.cpu].die_id;
1737 		id.cluster = env->cpu[cpu.cpu].cluster_id;
1738 	}
1739 
1740 	return id;
1741 }
1742 
1743 static struct aggr_cpu_id perf_env__get_core_aggr_by_cpu(struct perf_cpu cpu, void *data)
1744 {
1745 	struct perf_env *env = data;
1746 	struct aggr_cpu_id id = aggr_cpu_id__empty();
1747 
1748 	if (cpu.cpu != -1) {
1749 		/*
1750 		 * core_id is relative to socket, die and cluster, we need a
1751 		 * global id. So we set socket, die id, cluster id and core id.
1752 		 */
1753 		id.socket = env->cpu[cpu.cpu].socket_id;
1754 		id.die = env->cpu[cpu.cpu].die_id;
1755 		id.cluster = env->cpu[cpu.cpu].cluster_id;
1756 		id.core = env->cpu[cpu.cpu].core_id;
1757 	}
1758 
1759 	return id;
1760 }
1761 
1762 static struct aggr_cpu_id perf_env__get_cpu_aggr_by_cpu(struct perf_cpu cpu, void *data)
1763 {
1764 	struct perf_env *env = data;
1765 	struct aggr_cpu_id id = aggr_cpu_id__empty();
1766 
1767 	if (cpu.cpu != -1) {
1768 		/*
1769 		 * core_id is relative to socket and die,
1770 		 * we need a global id. So we set
1771 		 * socket, die id and core id
1772 		 */
1773 		id.socket = env->cpu[cpu.cpu].socket_id;
1774 		id.die = env->cpu[cpu.cpu].die_id;
1775 		id.core = env->cpu[cpu.cpu].core_id;
1776 		id.cpu = cpu;
1777 	}
1778 
1779 	return id;
1780 }
1781 
1782 static struct aggr_cpu_id perf_env__get_node_aggr_by_cpu(struct perf_cpu cpu, void *data)
1783 {
1784 	struct aggr_cpu_id id = aggr_cpu_id__empty();
1785 
1786 	id.node = perf_env__numa_node(data, cpu);
1787 	return id;
1788 }
1789 
1790 static struct aggr_cpu_id perf_env__get_global_aggr_by_cpu(struct perf_cpu cpu __maybe_unused,
1791 							   void *data __maybe_unused)
1792 {
1793 	struct aggr_cpu_id id = aggr_cpu_id__empty();
1794 
1795 	/* it always aggregates to the cpu 0 */
1796 	id.cpu = (struct perf_cpu){ .cpu = 0 };
1797 	return id;
1798 }
1799 
1800 static struct aggr_cpu_id perf_stat__get_socket_file(struct perf_stat_config *config __maybe_unused,
1801 						     struct perf_cpu cpu)
1802 {
1803 	return perf_env__get_socket_aggr_by_cpu(cpu, &perf_stat.session->header.env);
1804 }
1805 static struct aggr_cpu_id perf_stat__get_die_file(struct perf_stat_config *config __maybe_unused,
1806 						  struct perf_cpu cpu)
1807 {
1808 	return perf_env__get_die_aggr_by_cpu(cpu, &perf_stat.session->header.env);
1809 }
1810 
1811 static struct aggr_cpu_id perf_stat__get_cluster_file(struct perf_stat_config *config __maybe_unused,
1812 						      struct perf_cpu cpu)
1813 {
1814 	return perf_env__get_cluster_aggr_by_cpu(cpu, &perf_stat.session->header.env);
1815 }
1816 
1817 static struct aggr_cpu_id perf_stat__get_cache_file(struct perf_stat_config *config __maybe_unused,
1818 						    struct perf_cpu cpu)
1819 {
1820 	return perf_env__get_cache_aggr_by_cpu(cpu, &perf_stat.session->header.env);
1821 }
1822 
1823 static struct aggr_cpu_id perf_stat__get_core_file(struct perf_stat_config *config __maybe_unused,
1824 						   struct perf_cpu cpu)
1825 {
1826 	return perf_env__get_core_aggr_by_cpu(cpu, &perf_stat.session->header.env);
1827 }
1828 
1829 static struct aggr_cpu_id perf_stat__get_cpu_file(struct perf_stat_config *config __maybe_unused,
1830 						  struct perf_cpu cpu)
1831 {
1832 	return perf_env__get_cpu_aggr_by_cpu(cpu, &perf_stat.session->header.env);
1833 }
1834 
1835 static struct aggr_cpu_id perf_stat__get_node_file(struct perf_stat_config *config __maybe_unused,
1836 						   struct perf_cpu cpu)
1837 {
1838 	return perf_env__get_node_aggr_by_cpu(cpu, &perf_stat.session->header.env);
1839 }
1840 
1841 static struct aggr_cpu_id perf_stat__get_global_file(struct perf_stat_config *config __maybe_unused,
1842 						     struct perf_cpu cpu)
1843 {
1844 	return perf_env__get_global_aggr_by_cpu(cpu, &perf_stat.session->header.env);
1845 }
1846 
1847 static aggr_cpu_id_get_t aggr_mode__get_aggr_file(enum aggr_mode aggr_mode)
1848 {
1849 	switch (aggr_mode) {
1850 	case AGGR_SOCKET:
1851 		return perf_env__get_socket_aggr_by_cpu;
1852 	case AGGR_DIE:
1853 		return perf_env__get_die_aggr_by_cpu;
1854 	case AGGR_CLUSTER:
1855 		return perf_env__get_cluster_aggr_by_cpu;
1856 	case AGGR_CACHE:
1857 		return perf_env__get_cache_aggr_by_cpu;
1858 	case AGGR_CORE:
1859 		return perf_env__get_core_aggr_by_cpu;
1860 	case AGGR_NODE:
1861 		return perf_env__get_node_aggr_by_cpu;
1862 	case AGGR_GLOBAL:
1863 		return perf_env__get_global_aggr_by_cpu;
1864 	case AGGR_NONE:
1865 		return perf_env__get_cpu_aggr_by_cpu;
1866 	case AGGR_THREAD:
1867 	case AGGR_UNSET:
1868 	case AGGR_MAX:
1869 	default:
1870 		return NULL;
1871 	}
1872 }
1873 
1874 static aggr_get_id_t aggr_mode__get_id_file(enum aggr_mode aggr_mode)
1875 {
1876 	switch (aggr_mode) {
1877 	case AGGR_SOCKET:
1878 		return perf_stat__get_socket_file;
1879 	case AGGR_DIE:
1880 		return perf_stat__get_die_file;
1881 	case AGGR_CLUSTER:
1882 		return perf_stat__get_cluster_file;
1883 	case AGGR_CACHE:
1884 		return perf_stat__get_cache_file;
1885 	case AGGR_CORE:
1886 		return perf_stat__get_core_file;
1887 	case AGGR_NODE:
1888 		return perf_stat__get_node_file;
1889 	case AGGR_GLOBAL:
1890 		return perf_stat__get_global_file;
1891 	case AGGR_NONE:
1892 		return perf_stat__get_cpu_file;
1893 	case AGGR_THREAD:
1894 	case AGGR_UNSET:
1895 	case AGGR_MAX:
1896 	default:
1897 		return NULL;
1898 	}
1899 }
1900 
1901 static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
1902 {
1903 	struct perf_env *env = &st->session->header.env;
1904 	aggr_cpu_id_get_t get_id = aggr_mode__get_aggr_file(stat_config.aggr_mode);
1905 	bool needs_sort = stat_config.aggr_mode != AGGR_NONE;
1906 
1907 	if (stat_config.aggr_mode == AGGR_THREAD) {
1908 		int nr = perf_thread_map__nr(evsel_list->core.threads);
1909 
1910 		stat_config.aggr_map = cpu_aggr_map__empty_new(nr);
1911 		if (stat_config.aggr_map == NULL)
1912 			return -ENOMEM;
1913 
1914 		for (int s = 0; s < nr; s++) {
1915 			struct aggr_cpu_id id = aggr_cpu_id__empty();
1916 
1917 			id.thread_idx = s;
1918 			stat_config.aggr_map->map[s] = id;
1919 		}
1920 		return 0;
1921 	}
1922 
1923 	if (!get_id)
1924 		return 0;
1925 
1926 	stat_config.aggr_map = cpu_aggr_map__new(evsel_list->core.user_requested_cpus,
1927 						 get_id, env, needs_sort);
1928 	if (!stat_config.aggr_map) {
1929 		pr_err("cannot build %s map\n", aggr_mode__string[stat_config.aggr_mode]);
1930 		return -1;
1931 	}
1932 	stat_config.aggr_get_id = aggr_mode__get_id_file(stat_config.aggr_mode);
1933 	return 0;
1934 }
1935 
1936 /*
1937  * Add default attributes, if there were no attributes specified or
1938  * if -d/--detailed, -d -d or -d -d -d is used:
1939  */
1940 static int add_default_attributes(void)
1941 {
1942 	struct perf_event_attr default_attrs0[] = {
1943 
1944   { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_TASK_CLOCK		},
1945   { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CONTEXT_SWITCHES	},
1946   { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_CPU_MIGRATIONS		},
1947   { .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_SW_PAGE_FAULTS		},
1948 
1949   { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_CPU_CYCLES		},
1950 };
1951 	struct perf_event_attr frontend_attrs[] = {
1952   { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND	},
1953 };
1954 	struct perf_event_attr backend_attrs[] = {
1955   { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND	},
1956 };
1957 	struct perf_event_attr default_attrs1[] = {
1958   { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_INSTRUCTIONS		},
1959   { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS	},
1960   { .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_HW_BRANCH_MISSES		},
1961 
1962 };
1963 
1964 /*
1965  * Detailed stats (-d), covering the L1 and last level data caches:
1966  */
1967 	struct perf_event_attr detailed_attrs[] = {
1968 
1969   { .type = PERF_TYPE_HW_CACHE,
1970     .config =
1971 	 PERF_COUNT_HW_CACHE_L1D		<<  0  |
1972 	(PERF_COUNT_HW_CACHE_OP_READ		<<  8) |
1973 	(PERF_COUNT_HW_CACHE_RESULT_ACCESS	<< 16)				},
1974 
1975   { .type = PERF_TYPE_HW_CACHE,
1976     .config =
1977 	 PERF_COUNT_HW_CACHE_L1D		<<  0  |
1978 	(PERF_COUNT_HW_CACHE_OP_READ		<<  8) |
1979 	(PERF_COUNT_HW_CACHE_RESULT_MISS	<< 16)				},
1980 
1981   { .type = PERF_TYPE_HW_CACHE,
1982     .config =
1983 	 PERF_COUNT_HW_CACHE_LL			<<  0  |
1984 	(PERF_COUNT_HW_CACHE_OP_READ		<<  8) |
1985 	(PERF_COUNT_HW_CACHE_RESULT_ACCESS	<< 16)				},
1986 
1987   { .type = PERF_TYPE_HW_CACHE,
1988     .config =
1989 	 PERF_COUNT_HW_CACHE_LL			<<  0  |
1990 	(PERF_COUNT_HW_CACHE_OP_READ		<<  8) |
1991 	(PERF_COUNT_HW_CACHE_RESULT_MISS	<< 16)				},
1992 };
1993 
1994 /*
1995  * Very detailed stats (-d -d), covering the instruction cache and the TLB caches:
1996  */
1997 	struct perf_event_attr very_detailed_attrs[] = {
1998 
1999   { .type = PERF_TYPE_HW_CACHE,
2000     .config =
2001 	 PERF_COUNT_HW_CACHE_L1I		<<  0  |
2002 	(PERF_COUNT_HW_CACHE_OP_READ		<<  8) |
2003 	(PERF_COUNT_HW_CACHE_RESULT_ACCESS	<< 16)				},
2004 
2005   { .type = PERF_TYPE_HW_CACHE,
2006     .config =
2007 	 PERF_COUNT_HW_CACHE_L1I		<<  0  |
2008 	(PERF_COUNT_HW_CACHE_OP_READ		<<  8) |
2009 	(PERF_COUNT_HW_CACHE_RESULT_MISS	<< 16)				},
2010 
2011   { .type = PERF_TYPE_HW_CACHE,
2012     .config =
2013 	 PERF_COUNT_HW_CACHE_DTLB		<<  0  |
2014 	(PERF_COUNT_HW_CACHE_OP_READ		<<  8) |
2015 	(PERF_COUNT_HW_CACHE_RESULT_ACCESS	<< 16)				},
2016 
2017   { .type = PERF_TYPE_HW_CACHE,
2018     .config =
2019 	 PERF_COUNT_HW_CACHE_DTLB		<<  0  |
2020 	(PERF_COUNT_HW_CACHE_OP_READ		<<  8) |
2021 	(PERF_COUNT_HW_CACHE_RESULT_MISS	<< 16)				},
2022 
2023   { .type = PERF_TYPE_HW_CACHE,
2024     .config =
2025 	 PERF_COUNT_HW_CACHE_ITLB		<<  0  |
2026 	(PERF_COUNT_HW_CACHE_OP_READ		<<  8) |
2027 	(PERF_COUNT_HW_CACHE_RESULT_ACCESS	<< 16)				},
2028 
2029   { .type = PERF_TYPE_HW_CACHE,
2030     .config =
2031 	 PERF_COUNT_HW_CACHE_ITLB		<<  0  |
2032 	(PERF_COUNT_HW_CACHE_OP_READ		<<  8) |
2033 	(PERF_COUNT_HW_CACHE_RESULT_MISS	<< 16)				},
2034 
2035 };
2036 
2037 /*
2038  * Very, very detailed stats (-d -d -d), adding prefetch events:
2039  */
2040 	struct perf_event_attr very_very_detailed_attrs[] = {
2041 
2042   { .type = PERF_TYPE_HW_CACHE,
2043     .config =
2044 	 PERF_COUNT_HW_CACHE_L1D		<<  0  |
2045 	(PERF_COUNT_HW_CACHE_OP_PREFETCH	<<  8) |
2046 	(PERF_COUNT_HW_CACHE_RESULT_ACCESS	<< 16)				},
2047 
2048   { .type = PERF_TYPE_HW_CACHE,
2049     .config =
2050 	 PERF_COUNT_HW_CACHE_L1D		<<  0  |
2051 	(PERF_COUNT_HW_CACHE_OP_PREFETCH	<<  8) |
2052 	(PERF_COUNT_HW_CACHE_RESULT_MISS	<< 16)				},
2053 };
2054 
2055 	struct perf_event_attr default_null_attrs[] = {};
2056 	const char *pmu = parse_events_option_args.pmu_filter ?: "all";
2057 
2058 	/* Set attrs if no event is selected and !null_run: */
2059 	if (stat_config.null_run)
2060 		return 0;
2061 
2062 	if (transaction_run) {
2063 		/* Handle -T as -M transaction. Once platform specific metrics
2064 		 * support has been added to the json files, all architectures
2065 		 * will use this approach. To determine transaction support
2066 		 * on an architecture test for such a metric name.
2067 		 */
2068 		if (!metricgroup__has_metric(pmu, "transaction")) {
2069 			pr_err("Missing transaction metrics\n");
2070 			return -1;
2071 		}
2072 		return metricgroup__parse_groups(evsel_list, pmu, "transaction",
2073 						stat_config.metric_no_group,
2074 						stat_config.metric_no_merge,
2075 						stat_config.metric_no_threshold,
2076 						stat_config.user_requested_cpu_list,
2077 						stat_config.system_wide,
2078 						stat_config.hardware_aware_grouping,
2079 						&stat_config.metric_events);
2080 	}
2081 
2082 	if (smi_cost) {
2083 		int smi;
2084 
2085 		if (sysfs__read_int(FREEZE_ON_SMI_PATH, &smi) < 0) {
2086 			pr_err("freeze_on_smi is not supported.\n");
2087 			return -1;
2088 		}
2089 
2090 		if (!smi) {
2091 			if (sysfs__write_int(FREEZE_ON_SMI_PATH, 1) < 0) {
2092 				fprintf(stderr, "Failed to set freeze_on_smi.\n");
2093 				return -1;
2094 			}
2095 			smi_reset = true;
2096 		}
2097 
2098 		if (!metricgroup__has_metric(pmu, "smi")) {
2099 			pr_err("Missing smi metrics\n");
2100 			return -1;
2101 		}
2102 
2103 		if (!force_metric_only)
2104 			stat_config.metric_only = true;
2105 
2106 		return metricgroup__parse_groups(evsel_list, pmu, "smi",
2107 						stat_config.metric_no_group,
2108 						stat_config.metric_no_merge,
2109 						stat_config.metric_no_threshold,
2110 						stat_config.user_requested_cpu_list,
2111 						stat_config.system_wide,
2112 						stat_config.hardware_aware_grouping,
2113 						&stat_config.metric_events);
2114 	}
2115 
2116 	if (topdown_run) {
2117 		unsigned int max_level = metricgroups__topdown_max_level();
2118 		char str[] = "TopdownL1";
2119 
2120 		if (!force_metric_only)
2121 			stat_config.metric_only = true;
2122 
2123 		if (!max_level) {
2124 			pr_err("Topdown requested but the topdown metric groups aren't present.\n"
2125 				"(See perf list the metric groups have names like TopdownL1)\n");
2126 			return -1;
2127 		}
2128 		if (stat_config.topdown_level > max_level) {
2129 			pr_err("Invalid top-down metrics level. The max level is %u.\n", max_level);
2130 			return -1;
2131 		} else if (!stat_config.topdown_level)
2132 			stat_config.topdown_level = 1;
2133 
2134 		if (!stat_config.interval && !stat_config.metric_only) {
2135 			fprintf(stat_config.output,
2136 				"Topdown accuracy may decrease when measuring long periods.\n"
2137 				"Please print the result regularly, e.g. -I1000\n");
2138 		}
2139 		str[8] = stat_config.topdown_level + '0';
2140 		if (metricgroup__parse_groups(evsel_list,
2141 						pmu, str,
2142 						/*metric_no_group=*/false,
2143 						/*metric_no_merge=*/false,
2144 						/*metric_no_threshold=*/true,
2145 						stat_config.user_requested_cpu_list,
2146 						stat_config.system_wide,
2147 						stat_config.hardware_aware_grouping,
2148 						&stat_config.metric_events) < 0)
2149 			return -1;
2150 	}
2151 
2152 	if (!stat_config.topdown_level)
2153 		stat_config.topdown_level = 1;
2154 
2155 	if (!evsel_list->core.nr_entries) {
2156 		/* No events so add defaults. */
2157 		if (target__has_cpu(&target))
2158 			default_attrs0[0].config = PERF_COUNT_SW_CPU_CLOCK;
2159 
2160 		if (evlist__add_default_attrs(evsel_list, default_attrs0) < 0)
2161 			return -1;
2162 		if (perf_pmus__have_event("cpu", "stalled-cycles-frontend")) {
2163 			if (evlist__add_default_attrs(evsel_list, frontend_attrs) < 0)
2164 				return -1;
2165 		}
2166 		if (perf_pmus__have_event("cpu", "stalled-cycles-backend")) {
2167 			if (evlist__add_default_attrs(evsel_list, backend_attrs) < 0)
2168 				return -1;
2169 		}
2170 		if (evlist__add_default_attrs(evsel_list, default_attrs1) < 0)
2171 			return -1;
2172 		/*
2173 		 * Add TopdownL1 metrics if they exist. To minimize
2174 		 * multiplexing, don't request threshold computation.
2175 		 */
2176 		if (metricgroup__has_metric(pmu, "Default")) {
2177 			struct evlist *metric_evlist = evlist__new();
2178 			struct evsel *metric_evsel;
2179 
2180 			if (!metric_evlist)
2181 				return -1;
2182 
2183 			if (metricgroup__parse_groups(metric_evlist, pmu, "Default",
2184 							/*metric_no_group=*/false,
2185 							/*metric_no_merge=*/false,
2186 							/*metric_no_threshold=*/true,
2187 							stat_config.user_requested_cpu_list,
2188 							stat_config.system_wide,
2189 							stat_config.hardware_aware_grouping,
2190 							&stat_config.metric_events) < 0)
2191 				return -1;
2192 
2193 			evlist__for_each_entry(metric_evlist, metric_evsel) {
2194 				metric_evsel->skippable = true;
2195 				metric_evsel->default_metricgroup = true;
2196 			}
2197 			evlist__splice_list_tail(evsel_list, &metric_evlist->core.entries);
2198 			evlist__delete(metric_evlist);
2199 		}
2200 
2201 		/* Platform specific attrs */
2202 		if (evlist__add_default_attrs(evsel_list, default_null_attrs) < 0)
2203 			return -1;
2204 	}
2205 
2206 	/* Detailed events get appended to the event list: */
2207 
2208 	if (detailed_run <  1)
2209 		return 0;
2210 
2211 	/* Append detailed run extra attributes: */
2212 	if (evlist__add_default_attrs(evsel_list, detailed_attrs) < 0)
2213 		return -1;
2214 
2215 	if (detailed_run < 2)
2216 		return 0;
2217 
2218 	/* Append very detailed run extra attributes: */
2219 	if (evlist__add_default_attrs(evsel_list, very_detailed_attrs) < 0)
2220 		return -1;
2221 
2222 	if (detailed_run < 3)
2223 		return 0;
2224 
2225 	/* Append very, very detailed run extra attributes: */
2226 	return evlist__add_default_attrs(evsel_list, very_very_detailed_attrs);
2227 }
2228 
2229 static const char * const stat_record_usage[] = {
2230 	"perf stat record [<options>]",
2231 	NULL,
2232 };
2233 
2234 static void init_features(struct perf_session *session)
2235 {
2236 	int feat;
2237 
2238 	for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
2239 		perf_header__set_feat(&session->header, feat);
2240 
2241 	perf_header__clear_feat(&session->header, HEADER_DIR_FORMAT);
2242 	perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
2243 	perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
2244 	perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
2245 	perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
2246 }
2247 
2248 static int __cmd_record(int argc, const char **argv)
2249 {
2250 	struct perf_session *session;
2251 	struct perf_data *data = &perf_stat.data;
2252 
2253 	argc = parse_options(argc, argv, stat_options, stat_record_usage,
2254 			     PARSE_OPT_STOP_AT_NON_OPTION);
2255 
2256 	if (output_name)
2257 		data->path = output_name;
2258 
2259 	if (stat_config.run_count != 1 || forever) {
2260 		pr_err("Cannot use -r option with perf stat record.\n");
2261 		return -1;
2262 	}
2263 
2264 	session = perf_session__new(data, NULL);
2265 	if (IS_ERR(session)) {
2266 		pr_err("Perf session creation failed\n");
2267 		return PTR_ERR(session);
2268 	}
2269 
2270 	init_features(session);
2271 
2272 	session->evlist   = evsel_list;
2273 	perf_stat.session = session;
2274 	perf_stat.record  = true;
2275 	return argc;
2276 }
2277 
2278 static int process_stat_round_event(struct perf_session *session,
2279 				    union perf_event *event)
2280 {
2281 	struct perf_record_stat_round *stat_round = &event->stat_round;
2282 	struct timespec tsh, *ts = NULL;
2283 	const char **argv = session->header.env.cmdline_argv;
2284 	int argc = session->header.env.nr_cmdline;
2285 
2286 	process_counters();
2287 
2288 	if (stat_round->type == PERF_STAT_ROUND_TYPE__FINAL)
2289 		update_stats(&walltime_nsecs_stats, stat_round->time);
2290 
2291 	if (stat_config.interval && stat_round->time) {
2292 		tsh.tv_sec  = stat_round->time / NSEC_PER_SEC;
2293 		tsh.tv_nsec = stat_round->time % NSEC_PER_SEC;
2294 		ts = &tsh;
2295 	}
2296 
2297 	print_counters(ts, argc, argv);
2298 	return 0;
2299 }
2300 
2301 static
2302 int process_stat_config_event(struct perf_session *session,
2303 			      union perf_event *event)
2304 {
2305 	struct perf_tool *tool = session->tool;
2306 	struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2307 
2308 	perf_event__read_stat_config(&stat_config, &event->stat_config);
2309 
2310 	if (perf_cpu_map__is_empty(st->cpus)) {
2311 		if (st->aggr_mode != AGGR_UNSET)
2312 			pr_warning("warning: processing task data, aggregation mode not set\n");
2313 	} else if (st->aggr_mode != AGGR_UNSET) {
2314 		stat_config.aggr_mode = st->aggr_mode;
2315 	}
2316 
2317 	if (perf_stat.data.is_pipe)
2318 		perf_stat_init_aggr_mode();
2319 	else
2320 		perf_stat_init_aggr_mode_file(st);
2321 
2322 	if (stat_config.aggr_map) {
2323 		int nr_aggr = stat_config.aggr_map->nr;
2324 
2325 		if (evlist__alloc_aggr_stats(session->evlist, nr_aggr) < 0) {
2326 			pr_err("cannot allocate aggr counts\n");
2327 			return -1;
2328 		}
2329 	}
2330 	return 0;
2331 }
2332 
2333 static int set_maps(struct perf_stat *st)
2334 {
2335 	if (!st->cpus || !st->threads)
2336 		return 0;
2337 
2338 	if (WARN_ONCE(st->maps_allocated, "stats double allocation\n"))
2339 		return -EINVAL;
2340 
2341 	perf_evlist__set_maps(&evsel_list->core, st->cpus, st->threads);
2342 
2343 	if (evlist__alloc_stats(&stat_config, evsel_list, /*alloc_raw=*/true))
2344 		return -ENOMEM;
2345 
2346 	st->maps_allocated = true;
2347 	return 0;
2348 }
2349 
2350 static
2351 int process_thread_map_event(struct perf_session *session,
2352 			     union perf_event *event)
2353 {
2354 	struct perf_tool *tool = session->tool;
2355 	struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2356 
2357 	if (st->threads) {
2358 		pr_warning("Extra thread map event, ignoring.\n");
2359 		return 0;
2360 	}
2361 
2362 	st->threads = thread_map__new_event(&event->thread_map);
2363 	if (!st->threads)
2364 		return -ENOMEM;
2365 
2366 	return set_maps(st);
2367 }
2368 
2369 static
2370 int process_cpu_map_event(struct perf_session *session,
2371 			  union perf_event *event)
2372 {
2373 	struct perf_tool *tool = session->tool;
2374 	struct perf_stat *st = container_of(tool, struct perf_stat, tool);
2375 	struct perf_cpu_map *cpus;
2376 
2377 	if (st->cpus) {
2378 		pr_warning("Extra cpu map event, ignoring.\n");
2379 		return 0;
2380 	}
2381 
2382 	cpus = cpu_map__new_data(&event->cpu_map.data);
2383 	if (!cpus)
2384 		return -ENOMEM;
2385 
2386 	st->cpus = cpus;
2387 	return set_maps(st);
2388 }
2389 
2390 static const char * const stat_report_usage[] = {
2391 	"perf stat report [<options>]",
2392 	NULL,
2393 };
2394 
2395 static struct perf_stat perf_stat = {
2396 	.tool = {
2397 		.attr		= perf_event__process_attr,
2398 		.event_update	= perf_event__process_event_update,
2399 		.thread_map	= process_thread_map_event,
2400 		.cpu_map	= process_cpu_map_event,
2401 		.stat_config	= process_stat_config_event,
2402 		.stat		= perf_event__process_stat_event,
2403 		.stat_round	= process_stat_round_event,
2404 	},
2405 	.aggr_mode	= AGGR_UNSET,
2406 	.aggr_level	= 0,
2407 };
2408 
2409 static int __cmd_report(int argc, const char **argv)
2410 {
2411 	struct perf_session *session;
2412 	const struct option options[] = {
2413 	OPT_STRING('i', "input", &input_name, "file", "input file name"),
2414 	OPT_SET_UINT(0, "per-socket", &perf_stat.aggr_mode,
2415 		     "aggregate counts per processor socket", AGGR_SOCKET),
2416 	OPT_SET_UINT(0, "per-die", &perf_stat.aggr_mode,
2417 		     "aggregate counts per processor die", AGGR_DIE),
2418 	OPT_SET_UINT(0, "per-cluster", &perf_stat.aggr_mode,
2419 		     "aggregate counts perf processor cluster", AGGR_CLUSTER),
2420 	OPT_CALLBACK_OPTARG(0, "per-cache", &perf_stat.aggr_mode, &perf_stat.aggr_level,
2421 			    "cache level",
2422 			    "aggregate count at this cache level (Default: LLC)",
2423 			    parse_cache_level),
2424 	OPT_SET_UINT(0, "per-core", &perf_stat.aggr_mode,
2425 		     "aggregate counts per physical processor core", AGGR_CORE),
2426 	OPT_SET_UINT(0, "per-node", &perf_stat.aggr_mode,
2427 		     "aggregate counts per numa node", AGGR_NODE),
2428 	OPT_SET_UINT('A', "no-aggr", &perf_stat.aggr_mode,
2429 		     "disable CPU count aggregation", AGGR_NONE),
2430 	OPT_END()
2431 	};
2432 	struct stat st;
2433 	int ret;
2434 
2435 	argc = parse_options(argc, argv, options, stat_report_usage, 0);
2436 
2437 	if (!input_name || !strlen(input_name)) {
2438 		if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
2439 			input_name = "-";
2440 		else
2441 			input_name = "perf.data";
2442 	}
2443 
2444 	perf_stat.data.path = input_name;
2445 	perf_stat.data.mode = PERF_DATA_MODE_READ;
2446 
2447 	session = perf_session__new(&perf_stat.data, &perf_stat.tool);
2448 	if (IS_ERR(session))
2449 		return PTR_ERR(session);
2450 
2451 	perf_stat.session  = session;
2452 	stat_config.output = stderr;
2453 	evlist__delete(evsel_list);
2454 	evsel_list         = session->evlist;
2455 
2456 	ret = perf_session__process_events(session);
2457 	if (ret)
2458 		return ret;
2459 
2460 	perf_session__delete(session);
2461 	return 0;
2462 }
2463 
2464 static void setup_system_wide(int forks)
2465 {
2466 	/*
2467 	 * Make system wide (-a) the default target if
2468 	 * no target was specified and one of following
2469 	 * conditions is met:
2470 	 *
2471 	 *   - there's no workload specified
2472 	 *   - there is workload specified but all requested
2473 	 *     events are system wide events
2474 	 */
2475 	if (!target__none(&target))
2476 		return;
2477 
2478 	if (!forks)
2479 		target.system_wide = true;
2480 	else {
2481 		struct evsel *counter;
2482 
2483 		evlist__for_each_entry(evsel_list, counter) {
2484 			if (!counter->core.requires_cpu &&
2485 			    !evsel__name_is(counter, "duration_time")) {
2486 				return;
2487 			}
2488 		}
2489 
2490 		if (evsel_list->core.nr_entries)
2491 			target.system_wide = true;
2492 	}
2493 }
2494 
2495 int cmd_stat(int argc, const char **argv)
2496 {
2497 	const char * const stat_usage[] = {
2498 		"perf stat [<options>] [<command>]",
2499 		NULL
2500 	};
2501 	int status = -EINVAL, run_idx, err;
2502 	const char *mode;
2503 	FILE *output = stderr;
2504 	unsigned int interval, timeout;
2505 	const char * const stat_subcommands[] = { "record", "report" };
2506 	char errbuf[BUFSIZ];
2507 
2508 	setlocale(LC_ALL, "");
2509 
2510 	evsel_list = evlist__new();
2511 	if (evsel_list == NULL)
2512 		return -ENOMEM;
2513 
2514 	parse_events__shrink_config_terms();
2515 
2516 	/* String-parsing callback-based options would segfault when negated */
2517 	set_option_flag(stat_options, 'e', "event", PARSE_OPT_NONEG);
2518 	set_option_flag(stat_options, 'M', "metrics", PARSE_OPT_NONEG);
2519 	set_option_flag(stat_options, 'G', "cgroup", PARSE_OPT_NONEG);
2520 
2521 	argc = parse_options_subcommand(argc, argv, stat_options, stat_subcommands,
2522 					(const char **) stat_usage,
2523 					PARSE_OPT_STOP_AT_NON_OPTION);
2524 
2525 	if (stat_config.csv_sep) {
2526 		stat_config.csv_output = true;
2527 		if (!strcmp(stat_config.csv_sep, "\\t"))
2528 			stat_config.csv_sep = "\t";
2529 	} else
2530 		stat_config.csv_sep = DEFAULT_SEPARATOR;
2531 
2532 	if (argc && strlen(argv[0]) > 2 && strstarts("record", argv[0])) {
2533 		argc = __cmd_record(argc, argv);
2534 		if (argc < 0)
2535 			return -1;
2536 	} else if (argc && strlen(argv[0]) > 2 && strstarts("report", argv[0]))
2537 		return __cmd_report(argc, argv);
2538 
2539 	interval = stat_config.interval;
2540 	timeout = stat_config.timeout;
2541 
2542 	/*
2543 	 * For record command the -o is already taken care of.
2544 	 */
2545 	if (!STAT_RECORD && output_name && strcmp(output_name, "-"))
2546 		output = NULL;
2547 
2548 	if (output_name && output_fd) {
2549 		fprintf(stderr, "cannot use both --output and --log-fd\n");
2550 		parse_options_usage(stat_usage, stat_options, "o", 1);
2551 		parse_options_usage(NULL, stat_options, "log-fd", 0);
2552 		goto out;
2553 	}
2554 
2555 	if (stat_config.metric_only && stat_config.aggr_mode == AGGR_THREAD) {
2556 		fprintf(stderr, "--metric-only is not supported with --per-thread\n");
2557 		goto out;
2558 	}
2559 
2560 	if (stat_config.metric_only && stat_config.run_count > 1) {
2561 		fprintf(stderr, "--metric-only is not supported with -r\n");
2562 		goto out;
2563 	}
2564 
2565 	if (stat_config.walltime_run_table && stat_config.run_count <= 1) {
2566 		fprintf(stderr, "--table is only supported with -r\n");
2567 		parse_options_usage(stat_usage, stat_options, "r", 1);
2568 		parse_options_usage(NULL, stat_options, "table", 0);
2569 		goto out;
2570 	}
2571 
2572 	if (output_fd < 0) {
2573 		fprintf(stderr, "argument to --log-fd must be a > 0\n");
2574 		parse_options_usage(stat_usage, stat_options, "log-fd", 0);
2575 		goto out;
2576 	}
2577 
2578 	if (!output && !quiet) {
2579 		struct timespec tm;
2580 		mode = append_file ? "a" : "w";
2581 
2582 		output = fopen(output_name, mode);
2583 		if (!output) {
2584 			perror("failed to create output file");
2585 			return -1;
2586 		}
2587 		if (!stat_config.json_output) {
2588 			clock_gettime(CLOCK_REALTIME, &tm);
2589 			fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
2590 		}
2591 	} else if (output_fd > 0) {
2592 		mode = append_file ? "a" : "w";
2593 		output = fdopen(output_fd, mode);
2594 		if (!output) {
2595 			perror("Failed opening logfd");
2596 			return -errno;
2597 		}
2598 	}
2599 
2600 	if (stat_config.interval_clear && !isatty(fileno(output))) {
2601 		fprintf(stderr, "--interval-clear does not work with output\n");
2602 		parse_options_usage(stat_usage, stat_options, "o", 1);
2603 		parse_options_usage(NULL, stat_options, "log-fd", 0);
2604 		parse_options_usage(NULL, stat_options, "interval-clear", 0);
2605 		return -1;
2606 	}
2607 
2608 	stat_config.output = output;
2609 
2610 	/*
2611 	 * let the spreadsheet do the pretty-printing
2612 	 */
2613 	if (stat_config.csv_output) {
2614 		/* User explicitly passed -B? */
2615 		if (big_num_opt == 1) {
2616 			fprintf(stderr, "-B option not supported with -x\n");
2617 			parse_options_usage(stat_usage, stat_options, "B", 1);
2618 			parse_options_usage(NULL, stat_options, "x", 1);
2619 			goto out;
2620 		} else /* Nope, so disable big number formatting */
2621 			stat_config.big_num = false;
2622 	} else if (big_num_opt == 0) /* User passed --no-big-num */
2623 		stat_config.big_num = false;
2624 
2625 	err = target__validate(&target);
2626 	if (err) {
2627 		target__strerror(&target, err, errbuf, BUFSIZ);
2628 		pr_warning("%s\n", errbuf);
2629 	}
2630 
2631 	setup_system_wide(argc);
2632 
2633 	/*
2634 	 * Display user/system times only for single
2635 	 * run and when there's specified tracee.
2636 	 */
2637 	if ((stat_config.run_count == 1) && target__none(&target))
2638 		stat_config.ru_display = true;
2639 
2640 	if (stat_config.run_count < 0) {
2641 		pr_err("Run count must be a positive number\n");
2642 		parse_options_usage(stat_usage, stat_options, "r", 1);
2643 		goto out;
2644 	} else if (stat_config.run_count == 0) {
2645 		forever = true;
2646 		stat_config.run_count = 1;
2647 	}
2648 
2649 	if (stat_config.walltime_run_table) {
2650 		stat_config.walltime_run = zalloc(stat_config.run_count * sizeof(stat_config.walltime_run[0]));
2651 		if (!stat_config.walltime_run) {
2652 			pr_err("failed to setup -r option");
2653 			goto out;
2654 		}
2655 	}
2656 
2657 	if ((stat_config.aggr_mode == AGGR_THREAD) &&
2658 		!target__has_task(&target)) {
2659 		if (!target.system_wide || target.cpu_list) {
2660 			fprintf(stderr, "The --per-thread option is only "
2661 				"available when monitoring via -p -t -a "
2662 				"options or only --per-thread.\n");
2663 			parse_options_usage(NULL, stat_options, "p", 1);
2664 			parse_options_usage(NULL, stat_options, "t", 1);
2665 			goto out;
2666 		}
2667 	}
2668 
2669 	/*
2670 	 * no_aggr, cgroup are for system-wide only
2671 	 * --per-thread is aggregated per thread, we dont mix it with cpu mode
2672 	 */
2673 	if (((stat_config.aggr_mode != AGGR_GLOBAL &&
2674 	      stat_config.aggr_mode != AGGR_THREAD) ||
2675 	     (nr_cgroups || stat_config.cgroup_list)) &&
2676 	    !target__has_cpu(&target)) {
2677 		fprintf(stderr, "both cgroup and no-aggregation "
2678 			"modes only available in system-wide mode\n");
2679 
2680 		parse_options_usage(stat_usage, stat_options, "G", 1);
2681 		parse_options_usage(NULL, stat_options, "A", 1);
2682 		parse_options_usage(NULL, stat_options, "a", 1);
2683 		parse_options_usage(NULL, stat_options, "for-each-cgroup", 0);
2684 		goto out;
2685 	}
2686 
2687 	if (stat_config.iostat_run) {
2688 		status = iostat_prepare(evsel_list, &stat_config);
2689 		if (status)
2690 			goto out;
2691 		if (iostat_mode == IOSTAT_LIST) {
2692 			iostat_list(evsel_list, &stat_config);
2693 			goto out;
2694 		} else if (verbose > 0)
2695 			iostat_list(evsel_list, &stat_config);
2696 		if (iostat_mode == IOSTAT_RUN && !target__has_cpu(&target))
2697 			target.system_wide = true;
2698 	}
2699 
2700 	if ((stat_config.aggr_mode == AGGR_THREAD) && (target.system_wide))
2701 		target.per_thread = true;
2702 
2703 	stat_config.system_wide = target.system_wide;
2704 	if (target.cpu_list) {
2705 		stat_config.user_requested_cpu_list = strdup(target.cpu_list);
2706 		if (!stat_config.user_requested_cpu_list) {
2707 			status = -ENOMEM;
2708 			goto out;
2709 		}
2710 	}
2711 
2712 	/*
2713 	 * Metric parsing needs to be delayed as metrics may optimize events
2714 	 * knowing the target is system-wide.
2715 	 */
2716 	if (metrics) {
2717 		const char *pmu = parse_events_option_args.pmu_filter ?: "all";
2718 		int ret = metricgroup__parse_groups(evsel_list, pmu, metrics,
2719 						stat_config.metric_no_group,
2720 						stat_config.metric_no_merge,
2721 						stat_config.metric_no_threshold,
2722 						stat_config.user_requested_cpu_list,
2723 						stat_config.system_wide,
2724 						stat_config.hardware_aware_grouping,
2725 						&stat_config.metric_events);
2726 
2727 		zfree(&metrics);
2728 		if (ret) {
2729 			status = ret;
2730 			goto out;
2731 		}
2732 	}
2733 
2734 	if (add_default_attributes())
2735 		goto out;
2736 
2737 	if (stat_config.cgroup_list) {
2738 		if (nr_cgroups > 0) {
2739 			pr_err("--cgroup and --for-each-cgroup cannot be used together\n");
2740 			parse_options_usage(stat_usage, stat_options, "G", 1);
2741 			parse_options_usage(NULL, stat_options, "for-each-cgroup", 0);
2742 			goto out;
2743 		}
2744 
2745 		if (evlist__expand_cgroup(evsel_list, stat_config.cgroup_list,
2746 					  &stat_config.metric_events, true) < 0) {
2747 			parse_options_usage(stat_usage, stat_options,
2748 					    "for-each-cgroup", 0);
2749 			goto out;
2750 		}
2751 	}
2752 
2753 	evlist__warn_user_requested_cpus(evsel_list, target.cpu_list);
2754 
2755 	if (evlist__create_maps(evsel_list, &target) < 0) {
2756 		if (target__has_task(&target)) {
2757 			pr_err("Problems finding threads of monitor\n");
2758 			parse_options_usage(stat_usage, stat_options, "p", 1);
2759 			parse_options_usage(NULL, stat_options, "t", 1);
2760 		} else if (target__has_cpu(&target)) {
2761 			perror("failed to parse CPUs map");
2762 			parse_options_usage(stat_usage, stat_options, "C", 1);
2763 			parse_options_usage(NULL, stat_options, "a", 1);
2764 		}
2765 		goto out;
2766 	}
2767 
2768 	evlist__check_cpu_maps(evsel_list);
2769 
2770 	/*
2771 	 * Initialize thread_map with comm names,
2772 	 * so we could print it out on output.
2773 	 */
2774 	if (stat_config.aggr_mode == AGGR_THREAD) {
2775 		thread_map__read_comms(evsel_list->core.threads);
2776 	}
2777 
2778 	if (stat_config.aggr_mode == AGGR_NODE)
2779 		cpu__setup_cpunode_map();
2780 
2781 	if (stat_config.times && interval)
2782 		interval_count = true;
2783 	else if (stat_config.times && !interval) {
2784 		pr_err("interval-count option should be used together with "
2785 				"interval-print.\n");
2786 		parse_options_usage(stat_usage, stat_options, "interval-count", 0);
2787 		parse_options_usage(stat_usage, stat_options, "I", 1);
2788 		goto out;
2789 	}
2790 
2791 	if (timeout && timeout < 100) {
2792 		if (timeout < 10) {
2793 			pr_err("timeout must be >= 10ms.\n");
2794 			parse_options_usage(stat_usage, stat_options, "timeout", 0);
2795 			goto out;
2796 		} else
2797 			pr_warning("timeout < 100ms. "
2798 				   "The overhead percentage could be high in some cases. "
2799 				   "Please proceed with caution.\n");
2800 	}
2801 	if (timeout && interval) {
2802 		pr_err("timeout option is not supported with interval-print.\n");
2803 		parse_options_usage(stat_usage, stat_options, "timeout", 0);
2804 		parse_options_usage(stat_usage, stat_options, "I", 1);
2805 		goto out;
2806 	}
2807 
2808 	if (perf_stat_init_aggr_mode())
2809 		goto out;
2810 
2811 	if (evlist__alloc_stats(&stat_config, evsel_list, interval))
2812 		goto out;
2813 
2814 	/*
2815 	 * Set sample_type to PERF_SAMPLE_IDENTIFIER, which should be harmless
2816 	 * while avoiding that older tools show confusing messages.
2817 	 *
2818 	 * However for pipe sessions we need to keep it zero,
2819 	 * because script's perf_evsel__check_attr is triggered
2820 	 * by attr->sample_type != 0, and we can't run it on
2821 	 * stat sessions.
2822 	 */
2823 	stat_config.identifier = !(STAT_RECORD && perf_stat.data.is_pipe);
2824 
2825 	/*
2826 	 * We dont want to block the signals - that would cause
2827 	 * child tasks to inherit that and Ctrl-C would not work.
2828 	 * What we want is for Ctrl-C to work in the exec()-ed
2829 	 * task, but being ignored by perf stat itself:
2830 	 */
2831 	atexit(sig_atexit);
2832 	if (!forever)
2833 		signal(SIGINT,  skip_signal);
2834 	signal(SIGCHLD, skip_signal);
2835 	signal(SIGALRM, skip_signal);
2836 	signal(SIGABRT, skip_signal);
2837 
2838 	if (evlist__initialize_ctlfd(evsel_list, stat_config.ctl_fd, stat_config.ctl_fd_ack))
2839 		goto out;
2840 
2841 	/* Enable ignoring missing threads when -p option is defined. */
2842 	evlist__first(evsel_list)->ignore_missing_thread = target.pid;
2843 	status = 0;
2844 	for (run_idx = 0; forever || run_idx < stat_config.run_count; run_idx++) {
2845 		if (stat_config.run_count != 1 && verbose > 0)
2846 			fprintf(output, "[ perf stat: executing run #%d ... ]\n",
2847 				run_idx + 1);
2848 
2849 		if (run_idx != 0)
2850 			evlist__reset_prev_raw_counts(evsel_list);
2851 
2852 		status = run_perf_stat(argc, argv, run_idx);
2853 		if (forever && status != -1 && !interval) {
2854 			print_counters(NULL, argc, argv);
2855 			perf_stat__reset_stats();
2856 		}
2857 	}
2858 
2859 	if (!forever && status != -1 && (!interval || stat_config.summary)) {
2860 		if (stat_config.run_count > 1)
2861 			evlist__copy_res_stats(&stat_config, evsel_list);
2862 		print_counters(NULL, argc, argv);
2863 	}
2864 
2865 	evlist__finalize_ctlfd(evsel_list);
2866 
2867 	if (STAT_RECORD) {
2868 		/*
2869 		 * We synthesize the kernel mmap record just so that older tools
2870 		 * don't emit warnings about not being able to resolve symbols
2871 		 * due to /proc/sys/kernel/kptr_restrict settings and instead provide
2872 		 * a saner message about no samples being in the perf.data file.
2873 		 *
2874 		 * This also serves to suppress a warning about f_header.data.size == 0
2875 		 * in header.c at the moment 'perf stat record' gets introduced, which
2876 		 * is not really needed once we start adding the stat specific PERF_RECORD_
2877 		 * records, but the need to suppress the kptr_restrict messages in older
2878 		 * tools remain  -acme
2879 		 */
2880 		int fd = perf_data__fd(&perf_stat.data);
2881 
2882 		err = perf_event__synthesize_kernel_mmap((void *)&perf_stat,
2883 							 process_synthesized_event,
2884 							 &perf_stat.session->machines.host);
2885 		if (err) {
2886 			pr_warning("Couldn't synthesize the kernel mmap record, harmless, "
2887 				   "older tools may produce warnings about this file\n.");
2888 		}
2889 
2890 		if (!interval) {
2891 			if (WRITE_STAT_ROUND_EVENT(walltime_nsecs_stats.max, FINAL))
2892 				pr_err("failed to write stat round event\n");
2893 		}
2894 
2895 		if (!perf_stat.data.is_pipe) {
2896 			perf_stat.session->header.data_size += perf_stat.bytes_written;
2897 			perf_session__write_header(perf_stat.session, evsel_list, fd, true);
2898 		}
2899 
2900 		evlist__close(evsel_list);
2901 		perf_session__delete(perf_stat.session);
2902 	}
2903 
2904 	perf_stat__exit_aggr_mode();
2905 	evlist__free_stats(evsel_list);
2906 out:
2907 	if (stat_config.iostat_run)
2908 		iostat_release(evsel_list);
2909 
2910 	zfree(&stat_config.walltime_run);
2911 	zfree(&stat_config.user_requested_cpu_list);
2912 
2913 	if (smi_cost && smi_reset)
2914 		sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
2915 
2916 	evlist__delete(evsel_list);
2917 
2918 	metricgroup__rblist_exit(&stat_config.metric_events);
2919 	evlist__close_control(stat_config.ctl_fd, stat_config.ctl_fd_ack, &stat_config.ctl_fd_close);
2920 
2921 	return status;
2922 }
2923