xref: /linux/tools/perf/util/stat.c (revision 8386f58f8deda81110283798a387fb53ec21957c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <linux/err.h>
4 #include <inttypes.h>
5 #include <math.h>
6 #include <string.h>
7 #include "counts.h"
8 #include "cpumap.h"
9 #include "debug.h"
10 #include "header.h"
11 #include "stat.h"
12 #include "session.h"
13 #include "target.h"
14 #include "evlist.h"
15 #include "evsel.h"
16 #include "thread_map.h"
17 #include "util/hashmap.h"
18 #include <linux/zalloc.h>
19 
20 void update_stats(struct stats *stats, u64 val)
21 {
22 	double delta;
23 
24 	stats->n++;
25 	delta = val - stats->mean;
26 	stats->mean += delta / stats->n;
27 	stats->M2 += delta*(val - stats->mean);
28 
29 	if (val > stats->max)
30 		stats->max = val;
31 
32 	if (val < stats->min)
33 		stats->min = val;
34 }
35 
36 double avg_stats(struct stats *stats)
37 {
38 	return stats->mean;
39 }
40 
41 /*
42  * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
43  *
44  *       (\Sum n_i^2) - ((\Sum n_i)^2)/n
45  * s^2 = -------------------------------
46  *                  n - 1
47  *
48  * http://en.wikipedia.org/wiki/Stddev
49  *
50  * The std dev of the mean is related to the std dev by:
51  *
52  *             s
53  * s_mean = -------
54  *          sqrt(n)
55  *
56  */
57 double stddev_stats(struct stats *stats)
58 {
59 	double variance, variance_mean;
60 
61 	if (stats->n < 2)
62 		return 0.0;
63 
64 	variance = stats->M2 / (stats->n - 1);
65 	variance_mean = variance / stats->n;
66 
67 	return sqrt(variance_mean);
68 }
69 
70 double rel_stddev_stats(double stddev, double avg)
71 {
72 	double pct = 0.0;
73 
74 	if (avg)
75 		pct = 100.0 * stddev/avg;
76 
77 	return pct;
78 }
79 
80 static void evsel__reset_aggr_stats(struct evsel *evsel)
81 {
82 	struct perf_stat_evsel *ps = evsel->stats;
83 	struct perf_stat_aggr *aggr = ps->aggr;
84 
85 	if (aggr)
86 		memset(aggr, 0, sizeof(*aggr) * ps->nr_aggr);
87 }
88 
89 static void evsel__reset_stat_priv(struct evsel *evsel)
90 {
91 	struct perf_stat_evsel *ps = evsel->stats;
92 
93 	init_stats(&ps->res_stats);
94 	evsel__reset_aggr_stats(evsel);
95 }
96 
97 static int evsel__alloc_aggr_stats(struct evsel *evsel, int nr_aggr)
98 {
99 	struct perf_stat_evsel *ps = evsel->stats;
100 
101 	if (ps == NULL)
102 		return 0;
103 
104 	ps->nr_aggr = nr_aggr;
105 	ps->aggr = calloc(nr_aggr, sizeof(*ps->aggr));
106 	if (ps->aggr == NULL)
107 		return -ENOMEM;
108 
109 	return 0;
110 }
111 
112 int evlist__alloc_aggr_stats(struct evlist *evlist, int nr_aggr)
113 {
114 	struct evsel *evsel;
115 
116 	evlist__for_each_entry(evlist, evsel) {
117 		if (evsel__alloc_aggr_stats(evsel, nr_aggr) < 0)
118 			return -1;
119 	}
120 	return 0;
121 }
122 
123 static int evsel__alloc_stat_priv(struct evsel *evsel, int nr_aggr)
124 {
125 	struct perf_stat_evsel *ps;
126 
127 	ps = zalloc(sizeof(*ps));
128 	if (ps == NULL)
129 		return -ENOMEM;
130 
131 	evsel->stats = ps;
132 
133 	if (nr_aggr && evsel__alloc_aggr_stats(evsel, nr_aggr) < 0) {
134 		evsel->stats = NULL;
135 		free(ps);
136 		return -ENOMEM;
137 	}
138 
139 	evsel__reset_stat_priv(evsel);
140 	return 0;
141 }
142 
143 static void evsel__free_stat_priv(struct evsel *evsel)
144 {
145 	struct perf_stat_evsel *ps = evsel->stats;
146 
147 	if (ps) {
148 		zfree(&ps->aggr);
149 		zfree(&ps->group_data);
150 	}
151 	zfree(&evsel->stats);
152 }
153 
154 static int evsel__alloc_prev_raw_counts(struct evsel *evsel)
155 {
156 	int cpu_map_nr = evsel__nr_cpus(evsel);
157 	int nthreads = perf_thread_map__nr(evsel->core.threads);
158 	struct perf_counts *counts;
159 
160 	counts = perf_counts__new(cpu_map_nr, nthreads);
161 	if (counts)
162 		evsel->prev_raw_counts = counts;
163 
164 	return counts ? 0 : -ENOMEM;
165 }
166 
167 static void evsel__free_prev_raw_counts(struct evsel *evsel)
168 {
169 	perf_counts__delete(evsel->prev_raw_counts);
170 	evsel->prev_raw_counts = NULL;
171 }
172 
173 static void evsel__reset_prev_raw_counts(struct evsel *evsel)
174 {
175 	if (evsel->prev_raw_counts)
176 		perf_counts__reset(evsel->prev_raw_counts);
177 }
178 
179 static int evsel__alloc_stats(struct evsel *evsel, int nr_aggr, bool alloc_raw)
180 {
181 	if (evsel__alloc_stat_priv(evsel, nr_aggr) < 0 ||
182 	    evsel__alloc_counts(evsel) < 0 ||
183 	    (alloc_raw && evsel__alloc_prev_raw_counts(evsel) < 0))
184 		return -ENOMEM;
185 
186 	return 0;
187 }
188 
189 int evlist__alloc_stats(struct perf_stat_config *config,
190 			struct evlist *evlist, bool alloc_raw)
191 {
192 	struct evsel *evsel;
193 	int nr_aggr = 0;
194 
195 	if (config && config->aggr_map)
196 		nr_aggr = config->aggr_map->nr;
197 
198 	evlist__for_each_entry(evlist, evsel) {
199 		if (evsel__alloc_stats(evsel, nr_aggr, alloc_raw))
200 			goto out_free;
201 	}
202 
203 	return 0;
204 
205 out_free:
206 	evlist__free_stats(evlist);
207 	return -1;
208 }
209 
210 void evlist__free_stats(struct evlist *evlist)
211 {
212 	struct evsel *evsel;
213 
214 	evlist__for_each_entry(evlist, evsel) {
215 		evsel__free_stat_priv(evsel);
216 		evsel__free_counts(evsel);
217 		evsel__free_prev_raw_counts(evsel);
218 	}
219 }
220 
221 void evlist__reset_stats(struct evlist *evlist)
222 {
223 	struct evsel *evsel;
224 
225 	evlist__for_each_entry(evlist, evsel) {
226 		evsel__reset_stat_priv(evsel);
227 		evsel__reset_counts(evsel);
228 	}
229 }
230 
231 void evlist__reset_aggr_stats(struct evlist *evlist)
232 {
233 	struct evsel *evsel;
234 
235 	evlist__for_each_entry(evlist, evsel)
236 		evsel__reset_aggr_stats(evsel);
237 }
238 
239 void evlist__reset_prev_raw_counts(struct evlist *evlist)
240 {
241 	struct evsel *evsel;
242 
243 	evlist__for_each_entry(evlist, evsel)
244 		evsel__reset_prev_raw_counts(evsel);
245 }
246 
247 static void evsel__copy_prev_raw_counts(struct evsel *evsel)
248 {
249 	int idx, nthreads = perf_thread_map__nr(evsel->core.threads);
250 
251 	for (int thread = 0; thread < nthreads; thread++) {
252 		perf_cpu_map__for_each_idx(idx, evsel__cpus(evsel)) {
253 			*perf_counts(evsel->counts, idx, thread) =
254 				*perf_counts(evsel->prev_raw_counts, idx, thread);
255 		}
256 	}
257 }
258 
259 void evlist__copy_prev_raw_counts(struct evlist *evlist)
260 {
261 	struct evsel *evsel;
262 
263 	evlist__for_each_entry(evlist, evsel)
264 		evsel__copy_prev_raw_counts(evsel);
265 }
266 
267 static size_t pkg_id_hash(long __key, void *ctx __maybe_unused)
268 {
269 	uint64_t *key = (uint64_t *) __key;
270 
271 	return *key & 0xffffffff;
272 }
273 
274 static bool pkg_id_equal(long __key1, long __key2, void *ctx __maybe_unused)
275 {
276 	uint64_t *key1 = (uint64_t *) __key1;
277 	uint64_t *key2 = (uint64_t *) __key2;
278 
279 	return *key1 == *key2;
280 }
281 
282 static int check_per_pkg(struct evsel *counter, struct perf_counts_values *vals,
283 			 int cpu_map_idx, bool *skip)
284 {
285 	struct hashmap *mask = counter->per_pkg_mask;
286 	struct perf_cpu_map *cpus = evsel__cpus(counter);
287 	struct perf_cpu cpu = perf_cpu_map__cpu(cpus, cpu_map_idx);
288 	int s, d, ret = 0;
289 	uint64_t *key;
290 
291 	*skip = false;
292 
293 	if (!counter->per_pkg)
294 		return 0;
295 
296 	if (perf_cpu_map__empty(cpus))
297 		return 0;
298 
299 	if (!mask) {
300 		mask = hashmap__new(pkg_id_hash, pkg_id_equal, NULL);
301 		if (IS_ERR(mask))
302 			return -ENOMEM;
303 
304 		counter->per_pkg_mask = mask;
305 	}
306 
307 	/*
308 	 * we do not consider an event that has not run as a good
309 	 * instance to mark a package as used (skip=1). Otherwise
310 	 * we may run into a situation where the first CPU in a package
311 	 * is not running anything, yet the second is, and this function
312 	 * would mark the package as used after the first CPU and would
313 	 * not read the values from the second CPU.
314 	 */
315 	if (!(vals->run && vals->ena))
316 		return 0;
317 
318 	s = cpu__get_socket_id(cpu);
319 	if (s < 0)
320 		return -1;
321 
322 	/*
323 	 * On multi-die system, die_id > 0. On no-die system, die_id = 0.
324 	 * We use hashmap(socket, die) to check the used socket+die pair.
325 	 */
326 	d = cpu__get_die_id(cpu);
327 	if (d < 0)
328 		return -1;
329 
330 	key = malloc(sizeof(*key));
331 	if (!key)
332 		return -ENOMEM;
333 
334 	*key = (uint64_t)d << 32 | s;
335 	if (hashmap__find(mask, key, NULL)) {
336 		*skip = true;
337 		free(key);
338 	} else
339 		ret = hashmap__add(mask, key, 1);
340 
341 	return ret;
342 }
343 
344 static bool evsel__count_has_error(struct evsel *evsel,
345 				   struct perf_counts_values *count,
346 				   struct perf_stat_config *config)
347 {
348 	/* the evsel was failed already */
349 	if (evsel->err || evsel->counts->scaled == -1)
350 		return true;
351 
352 	/* this is meaningful for CPU aggregation modes only */
353 	if (config->aggr_mode == AGGR_GLOBAL)
354 		return false;
355 
356 	/* it's considered ok when it actually ran */
357 	if (count->ena != 0 && count->run != 0)
358 		return false;
359 
360 	return true;
361 }
362 
363 static int
364 process_counter_values(struct perf_stat_config *config, struct evsel *evsel,
365 		       int cpu_map_idx, int thread,
366 		       struct perf_counts_values *count)
367 {
368 	struct perf_stat_evsel *ps = evsel->stats;
369 	static struct perf_counts_values zero;
370 	bool skip = false;
371 
372 	if (check_per_pkg(evsel, count, cpu_map_idx, &skip)) {
373 		pr_err("failed to read per-pkg counter\n");
374 		return -1;
375 	}
376 
377 	if (skip)
378 		count = &zero;
379 
380 	if (!evsel->snapshot)
381 		evsel__compute_deltas(evsel, cpu_map_idx, thread, count);
382 	perf_counts_values__scale(count, config->scale, NULL);
383 
384 	if (config->aggr_mode == AGGR_THREAD) {
385 		struct perf_counts_values *aggr_counts = &ps->aggr[thread].counts;
386 
387 		/*
388 		 * Skip value 0 when enabling --per-thread globally,
389 		 * otherwise too many 0 output.
390 		 */
391 		if (count->val == 0 && config->system_wide)
392 			return 0;
393 
394 		ps->aggr[thread].nr++;
395 
396 		aggr_counts->val += count->val;
397 		aggr_counts->ena += count->ena;
398 		aggr_counts->run += count->run;
399 		return 0;
400 	}
401 
402 	if (ps->aggr) {
403 		struct perf_cpu cpu = perf_cpu_map__cpu(evsel->core.cpus, cpu_map_idx);
404 		struct aggr_cpu_id aggr_id = config->aggr_get_id(config, cpu);
405 		struct perf_stat_aggr *ps_aggr;
406 		int i;
407 
408 		for (i = 0; i < ps->nr_aggr; i++) {
409 			if (!aggr_cpu_id__equal(&aggr_id, &config->aggr_map->map[i]))
410 				continue;
411 
412 			ps_aggr = &ps->aggr[i];
413 			ps_aggr->nr++;
414 
415 			/*
416 			 * When any result is bad, make them all to give consistent output
417 			 * in interval mode.  But per-task counters can have 0 enabled time
418 			 * when some tasks are idle.
419 			 */
420 			if (evsel__count_has_error(evsel, count, config) && !ps_aggr->failed) {
421 				ps_aggr->counts.val = 0;
422 				ps_aggr->counts.ena = 0;
423 				ps_aggr->counts.run = 0;
424 				ps_aggr->failed = true;
425 			}
426 
427 			if (!ps_aggr->failed) {
428 				ps_aggr->counts.val += count->val;
429 				ps_aggr->counts.ena += count->ena;
430 				ps_aggr->counts.run += count->run;
431 			}
432 			break;
433 		}
434 	}
435 
436 	return 0;
437 }
438 
439 static int process_counter_maps(struct perf_stat_config *config,
440 				struct evsel *counter)
441 {
442 	int nthreads = perf_thread_map__nr(counter->core.threads);
443 	int ncpus = evsel__nr_cpus(counter);
444 	int idx, thread;
445 
446 	for (thread = 0; thread < nthreads; thread++) {
447 		for (idx = 0; idx < ncpus; idx++) {
448 			if (process_counter_values(config, counter, idx, thread,
449 						   perf_counts(counter->counts, idx, thread)))
450 				return -1;
451 		}
452 	}
453 
454 	return 0;
455 }
456 
457 int perf_stat_process_counter(struct perf_stat_config *config,
458 			      struct evsel *counter)
459 {
460 	struct perf_stat_evsel *ps = counter->stats;
461 	u64 *count;
462 	int ret;
463 
464 	if (counter->per_pkg)
465 		evsel__zero_per_pkg(counter);
466 
467 	ret = process_counter_maps(config, counter);
468 	if (ret)
469 		return ret;
470 
471 	if (config->aggr_mode != AGGR_GLOBAL)
472 		return 0;
473 
474 	/*
475 	 * GLOBAL aggregation mode only has a single aggr counts,
476 	 * so we can use ps->aggr[0] as the actual output.
477 	 */
478 	count = ps->aggr[0].counts.values;
479 	update_stats(&ps->res_stats, *count);
480 
481 	if (verbose > 0) {
482 		fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
483 			evsel__name(counter), count[0], count[1], count[2]);
484 	}
485 
486 	return 0;
487 }
488 
489 static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias)
490 {
491 	struct perf_stat_evsel *ps_a = evsel->stats;
492 	struct perf_stat_evsel *ps_b = alias->stats;
493 	int i;
494 
495 	if (ps_a->aggr == NULL && ps_b->aggr == NULL)
496 		return 0;
497 
498 	if (ps_a->nr_aggr != ps_b->nr_aggr) {
499 		pr_err("Unmatched aggregation mode between aliases\n");
500 		return -1;
501 	}
502 
503 	for (i = 0; i < ps_a->nr_aggr; i++) {
504 		struct perf_counts_values *aggr_counts_a = &ps_a->aggr[i].counts;
505 		struct perf_counts_values *aggr_counts_b = &ps_b->aggr[i].counts;
506 
507 		/* NB: don't increase aggr.nr for aliases */
508 
509 		aggr_counts_a->val += aggr_counts_b->val;
510 		aggr_counts_a->ena += aggr_counts_b->ena;
511 		aggr_counts_a->run += aggr_counts_b->run;
512 	}
513 
514 	return 0;
515 }
516 /* events should have the same name, scale, unit, cgroup but on different PMUs */
517 static bool evsel__is_alias(struct evsel *evsel_a, struct evsel *evsel_b)
518 {
519 	if (strcmp(evsel__name(evsel_a), evsel__name(evsel_b)))
520 		return false;
521 
522 	if (evsel_a->scale != evsel_b->scale)
523 		return false;
524 
525 	if (evsel_a->cgrp != evsel_b->cgrp)
526 		return false;
527 
528 	if (strcmp(evsel_a->unit, evsel_b->unit))
529 		return false;
530 
531 	if (evsel__is_clock(evsel_a) != evsel__is_clock(evsel_b))
532 		return false;
533 
534 	return !!strcmp(evsel_a->pmu_name, evsel_b->pmu_name);
535 }
536 
537 static void evsel__merge_aliases(struct evsel *evsel)
538 {
539 	struct evlist *evlist = evsel->evlist;
540 	struct evsel *alias;
541 
542 	alias = list_prepare_entry(evsel, &(evlist->core.entries), core.node);
543 	list_for_each_entry_continue(alias, &evlist->core.entries, core.node) {
544 		/* Merge the same events on different PMUs. */
545 		if (evsel__is_alias(evsel, alias)) {
546 			evsel__merge_aggr_counters(evsel, alias);
547 			alias->merged_stat = true;
548 		}
549 	}
550 }
551 
552 static bool evsel__should_merge_hybrid(const struct evsel *evsel,
553 				       const struct perf_stat_config *config)
554 {
555 	return config->hybrid_merge && evsel__is_hybrid(evsel);
556 }
557 
558 static void evsel__merge_stats(struct evsel *evsel, struct perf_stat_config *config)
559 {
560 	/* this evsel is already merged */
561 	if (evsel->merged_stat)
562 		return;
563 
564 	if (evsel->auto_merge_stats || evsel__should_merge_hybrid(evsel, config))
565 		evsel__merge_aliases(evsel);
566 }
567 
568 /* merge the same uncore and hybrid events if requested */
569 void perf_stat_merge_counters(struct perf_stat_config *config, struct evlist *evlist)
570 {
571 	struct evsel *evsel;
572 
573 	if (config->no_merge)
574 		return;
575 
576 	evlist__for_each_entry(evlist, evsel)
577 		evsel__merge_stats(evsel, config);
578 }
579 
580 static void evsel__update_percore_stats(struct evsel *evsel, struct aggr_cpu_id *core_id)
581 {
582 	struct perf_stat_evsel *ps = evsel->stats;
583 	struct perf_counts_values counts = { 0, };
584 	struct aggr_cpu_id id;
585 	struct perf_cpu cpu;
586 	int idx;
587 
588 	/* collect per-core counts */
589 	perf_cpu_map__for_each_cpu(cpu, idx, evsel->core.cpus) {
590 		struct perf_stat_aggr *aggr = &ps->aggr[idx];
591 
592 		id = aggr_cpu_id__core(cpu, NULL);
593 		if (!aggr_cpu_id__equal(core_id, &id))
594 			continue;
595 
596 		counts.val += aggr->counts.val;
597 		counts.ena += aggr->counts.ena;
598 		counts.run += aggr->counts.run;
599 	}
600 
601 	/* update aggregated per-core counts for each CPU */
602 	perf_cpu_map__for_each_cpu(cpu, idx, evsel->core.cpus) {
603 		struct perf_stat_aggr *aggr = &ps->aggr[idx];
604 
605 		id = aggr_cpu_id__core(cpu, NULL);
606 		if (!aggr_cpu_id__equal(core_id, &id))
607 			continue;
608 
609 		aggr->counts.val = counts.val;
610 		aggr->counts.ena = counts.ena;
611 		aggr->counts.run = counts.run;
612 
613 		aggr->used = true;
614 	}
615 }
616 
617 /* we have an aggr_map for cpu, but want to aggregate the counters per-core */
618 static void evsel__process_percore(struct evsel *evsel)
619 {
620 	struct perf_stat_evsel *ps = evsel->stats;
621 	struct aggr_cpu_id core_id;
622 	struct perf_cpu cpu;
623 	int idx;
624 
625 	if (!evsel->percore)
626 		return;
627 
628 	perf_cpu_map__for_each_cpu(cpu, idx, evsel->core.cpus) {
629 		struct perf_stat_aggr *aggr = &ps->aggr[idx];
630 
631 		if (aggr->used)
632 			continue;
633 
634 		core_id = aggr_cpu_id__core(cpu, NULL);
635 		evsel__update_percore_stats(evsel, &core_id);
636 	}
637 }
638 
639 /* process cpu stats on per-core events */
640 void perf_stat_process_percore(struct perf_stat_config *config, struct evlist *evlist)
641 {
642 	struct evsel *evsel;
643 
644 	if (config->aggr_mode != AGGR_NONE)
645 		return;
646 
647 	evlist__for_each_entry(evlist, evsel)
648 		evsel__process_percore(evsel);
649 }
650 
651 int perf_event__process_stat_event(struct perf_session *session,
652 				   union perf_event *event)
653 {
654 	struct perf_counts_values count, *ptr;
655 	struct perf_record_stat *st = &event->stat;
656 	struct evsel *counter;
657 	int cpu_map_idx;
658 
659 	count.val = st->val;
660 	count.ena = st->ena;
661 	count.run = st->run;
662 
663 	counter = evlist__id2evsel(session->evlist, st->id);
664 	if (!counter) {
665 		pr_err("Failed to resolve counter for stat event.\n");
666 		return -EINVAL;
667 	}
668 	cpu_map_idx = perf_cpu_map__idx(evsel__cpus(counter), (struct perf_cpu){.cpu = st->cpu});
669 	if (cpu_map_idx == -1) {
670 		pr_err("Invalid CPU %d for event %s.\n", st->cpu, evsel__name(counter));
671 		return -EINVAL;
672 	}
673 	ptr = perf_counts(counter->counts, cpu_map_idx, st->thread);
674 	if (ptr == NULL) {
675 		pr_err("Failed to find perf count for CPU %d thread %d on event %s.\n",
676 			st->cpu, st->thread, evsel__name(counter));
677 		return -EINVAL;
678 	}
679 	*ptr = count;
680 	counter->supported = true;
681 	return 0;
682 }
683 
684 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp)
685 {
686 	struct perf_record_stat *st = (struct perf_record_stat *)event;
687 	size_t ret;
688 
689 	ret  = fprintf(fp, "\n... id %" PRI_lu64 ", cpu %d, thread %d\n",
690 		       st->id, st->cpu, st->thread);
691 	ret += fprintf(fp, "... value %" PRI_lu64 ", enabled %" PRI_lu64 ", running %" PRI_lu64 "\n",
692 		       st->val, st->ena, st->run);
693 
694 	return ret;
695 }
696 
697 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp)
698 {
699 	struct perf_record_stat_round *rd = (struct perf_record_stat_round *)event;
700 	size_t ret;
701 
702 	ret = fprintf(fp, "\n... time %" PRI_lu64 ", type %s\n", rd->time,
703 		      rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL");
704 
705 	return ret;
706 }
707 
708 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp)
709 {
710 	struct perf_stat_config sc;
711 	size_t ret;
712 
713 	perf_event__read_stat_config(&sc, &event->stat_config);
714 
715 	ret  = fprintf(fp, "\n");
716 	ret += fprintf(fp, "... aggr_mode %d\n", sc.aggr_mode);
717 	ret += fprintf(fp, "... scale     %d\n", sc.scale);
718 	ret += fprintf(fp, "... interval  %u\n", sc.interval);
719 
720 	return ret;
721 }
722 
723 int create_perf_stat_counter(struct evsel *evsel,
724 			     struct perf_stat_config *config,
725 			     struct target *target,
726 			     int cpu_map_idx)
727 {
728 	struct perf_event_attr *attr = &evsel->core.attr;
729 	struct evsel *leader = evsel__leader(evsel);
730 
731 	attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
732 			    PERF_FORMAT_TOTAL_TIME_RUNNING;
733 
734 	/*
735 	 * The event is part of non trivial group, let's enable
736 	 * the group read (for leader) and ID retrieval for all
737 	 * members.
738 	 */
739 	if (leader->core.nr_members > 1)
740 		attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
741 
742 	attr->inherit = !config->no_inherit && list_empty(&evsel->bpf_counter_list);
743 
744 	/*
745 	 * Some events get initialized with sample_(period/type) set,
746 	 * like tracepoints. Clear it up for counting.
747 	 */
748 	attr->sample_period = 0;
749 
750 	if (config->identifier)
751 		attr->sample_type = PERF_SAMPLE_IDENTIFIER;
752 
753 	if (config->all_user) {
754 		attr->exclude_kernel = 1;
755 		attr->exclude_user   = 0;
756 	}
757 
758 	if (config->all_kernel) {
759 		attr->exclude_kernel = 0;
760 		attr->exclude_user   = 1;
761 	}
762 
763 	/*
764 	 * Disabling all counters initially, they will be enabled
765 	 * either manually by us or by kernel via enable_on_exec
766 	 * set later.
767 	 */
768 	if (evsel__is_group_leader(evsel)) {
769 		attr->disabled = 1;
770 
771 		if (target__enable_on_exec(target))
772 			attr->enable_on_exec = 1;
773 	}
774 
775 	if (target__has_cpu(target) && !target__has_per_thread(target))
776 		return evsel__open_per_cpu(evsel, evsel__cpus(evsel), cpu_map_idx);
777 
778 	return evsel__open_per_thread(evsel, evsel->core.threads);
779 }
780