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