1 // SPDX-License-Identifier: GPL-2.0
16 #include "thread_map.h"
17 #include "util/hashmap.h"
18 #include <linux/zalloc.h>
20 void update_stats(struct stats *stats, u64 val)
25 delta = val - stats->mean;
26 stats->mean += delta / stats->n;
27 stats->M2 += delta*(val - stats->mean);
36 double avg_stats(struct stats *stats)
42 * http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
44 * (\Sum n_i^2) - ((\Sum n_i)^2)/n
45 * s^2 = -------------------------------
48 * http://en.wikipedia.org/wiki/Stddev
50 * The std dev of the mean is related to the std dev by:
57 double stddev_stats(struct stats *stats)
59 double variance, variance_mean;
64 variance = stats->M2 / (stats->n - 1);
65 variance_mean = variance / stats->n;
67 return sqrt(variance_mean);
70 double rel_stddev_stats(double stddev, double avg)
75 pct = 100.0 * stddev/avg;
80 bool __perf_stat_evsel__is(struct evsel *evsel, enum perf_stat_evsel_id id)
82 struct perf_stat_evsel *ps = evsel->stats;
87 #define ID(id, name) [PERF_STAT_EVSEL_ID__##id] = #name
88 static const char *id_str[PERF_STAT_EVSEL_ID__MAX] = {
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/),
112 static void perf_stat_evsel_id_init(struct evsel *evsel)
114 struct perf_stat_evsel *ps = evsel->stats;
117 /* ps->id is 0 hence PERF_STAT_EVSEL_ID__NONE by default */
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))) {
129 static void evsel__reset_aggr_stats(struct evsel *evsel)
131 struct perf_stat_evsel *ps = evsel->stats;
132 struct perf_stat_aggr *aggr = ps->aggr;
135 memset(aggr, 0, sizeof(*aggr) * ps->nr_aggr);
138 static void evsel__reset_stat_priv(struct evsel *evsel)
140 struct perf_stat_evsel *ps = evsel->stats;
142 init_stats(&ps->res_stats);
143 evsel__reset_aggr_stats(evsel);
146 static int evsel__alloc_aggr_stats(struct evsel *evsel, int nr_aggr)
148 struct perf_stat_evsel *ps = evsel->stats;
153 ps->nr_aggr = nr_aggr;
154 ps->aggr = calloc(nr_aggr, sizeof(*ps->aggr));
155 if (ps->aggr == NULL)
161 int evlist__alloc_aggr_stats(struct evlist *evlist, int nr_aggr)
165 evlist__for_each_entry(evlist, evsel) {
166 if (evsel__alloc_aggr_stats(evsel, nr_aggr) < 0)
172 static int evsel__alloc_stat_priv(struct evsel *evsel, int nr_aggr)
174 struct perf_stat_evsel *ps;
176 ps = zalloc(sizeof(*ps));
182 if (nr_aggr && evsel__alloc_aggr_stats(evsel, nr_aggr) < 0) {
188 perf_stat_evsel_id_init(evsel);
189 evsel__reset_stat_priv(evsel);
193 static void evsel__free_stat_priv(struct evsel *evsel)
195 struct perf_stat_evsel *ps = evsel->stats;
199 zfree(&ps->group_data);
201 zfree(&evsel->stats);
204 static int evsel__alloc_prev_raw_counts(struct evsel *evsel)
206 int cpu_map_nr = evsel__nr_cpus(evsel);
207 int nthreads = perf_thread_map__nr(evsel->core.threads);
208 struct perf_counts *counts;
210 counts = perf_counts__new(cpu_map_nr, nthreads);
212 evsel->prev_raw_counts = counts;
214 return counts ? 0 : -ENOMEM;
217 static void evsel__free_prev_raw_counts(struct evsel *evsel)
219 perf_counts__delete(evsel->prev_raw_counts);
220 evsel->prev_raw_counts = NULL;
223 static void evsel__reset_prev_raw_counts(struct evsel *evsel)
225 if (evsel->prev_raw_counts)
226 perf_counts__reset(evsel->prev_raw_counts);
229 static int evsel__alloc_stats(struct evsel *evsel, int nr_aggr, bool alloc_raw)
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))
239 int evlist__alloc_stats(struct perf_stat_config *config,
240 struct evlist *evlist, bool alloc_raw)
245 if (config && config->aggr_map)
246 nr_aggr = config->aggr_map->nr;
248 evlist__for_each_entry(evlist, evsel) {
249 if (evsel__alloc_stats(evsel, nr_aggr, alloc_raw))
256 evlist__free_stats(evlist);
260 void evlist__free_stats(struct evlist *evlist)
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);
271 void evlist__reset_stats(struct evlist *evlist)
275 evlist__for_each_entry(evlist, evsel) {
276 evsel__reset_stat_priv(evsel);
277 evsel__reset_counts(evsel);
281 void evlist__reset_aggr_stats(struct evlist *evlist)
285 evlist__for_each_entry(evlist, evsel)
286 evsel__reset_aggr_stats(evsel);
289 void evlist__reset_prev_raw_counts(struct evlist *evlist)
293 evlist__for_each_entry(evlist, evsel)
294 evsel__reset_prev_raw_counts(evsel);
297 static void evsel__copy_prev_raw_counts(struct evsel *evsel)
299 int idx, nthreads = perf_thread_map__nr(evsel->core.threads);
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);
309 void evlist__copy_prev_raw_counts(struct evlist *evlist)
313 evlist__for_each_entry(evlist, evsel)
314 evsel__copy_prev_raw_counts(evsel);
317 static size_t pkg_id_hash(const void *__key, void *ctx __maybe_unused)
319 uint64_t *key = (uint64_t *) __key;
321 return *key & 0xffffffff;
324 static bool pkg_id_equal(const void *__key1, const void *__key2,
325 void *ctx __maybe_unused)
327 uint64_t *key1 = (uint64_t *) __key1;
328 uint64_t *key2 = (uint64_t *) __key2;
330 return *key1 == *key2;
333 static int check_per_pkg(struct evsel *counter, struct perf_counts_values *vals,
334 int cpu_map_idx, bool *skip)
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);
344 if (!counter->per_pkg)
347 if (perf_cpu_map__empty(cpus))
351 mask = hashmap__new(pkg_id_hash, pkg_id_equal, NULL);
355 counter->per_pkg_mask = mask;
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.
366 if (!(vals->run && vals->ena))
369 s = cpu__get_socket_id(cpu);
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.
377 d = cpu__get_die_id(cpu);
381 key = malloc(sizeof(*key));
385 *key = (uint64_t)d << 32 | s;
386 if (hashmap__find(mask, (void *)key, NULL)) {
390 ret = hashmap__add(mask, (void *)key, (void *)1);
395 static bool evsel__count_has_error(struct evsel *evsel,
396 struct perf_counts_values *count,
397 struct perf_stat_config *config)
399 /* the evsel was failed already */
400 if (evsel->err || evsel->counts->scaled == -1)
403 /* this is meaningful for CPU aggregation modes only */
404 if (config->aggr_mode == AGGR_GLOBAL)
407 /* it's considered ok when it actually ran */
408 if (count->ena != 0 && count->run != 0)
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)
419 struct perf_stat_evsel *ps = evsel->stats;
420 static struct perf_counts_values zero;
423 if (check_per_pkg(evsel, count, cpu_map_idx, &skip)) {
424 pr_err("failed to read per-pkg counter\n");
431 if (!evsel->snapshot)
432 evsel__compute_deltas(evsel, cpu_map_idx, thread, count);
433 perf_counts_values__scale(count, config->scale, NULL);
435 if (config->aggr_mode == AGGR_THREAD) {
436 struct perf_counts_values *aggr_counts = &ps->aggr[thread].counts;
439 * Skip value 0 when enabling --per-thread globally,
440 * otherwise too many 0 output.
442 if (count->val == 0 && config->system_wide)
445 ps->aggr[thread].nr++;
447 aggr_counts->val += count->val;
448 aggr_counts->ena += count->ena;
449 aggr_counts->run += count->run;
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;
459 for (i = 0; i < ps->nr_aggr; i++) {
460 if (!aggr_cpu_id__equal(&aggr_id, &config->aggr_map->map[i]))
463 ps_aggr = &ps->aggr[i];
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.
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;
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;
490 static int process_counter_maps(struct perf_stat_config *config,
491 struct evsel *counter)
493 int nthreads = perf_thread_map__nr(counter->core.threads);
494 int ncpus = evsel__nr_cpus(counter);
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)))
508 int perf_stat_process_counter(struct perf_stat_config *config,
509 struct evsel *counter)
511 struct perf_stat_evsel *ps = counter->stats;
515 if (counter->per_pkg)
516 evsel__zero_per_pkg(counter);
518 ret = process_counter_maps(config, counter);
522 if (config->aggr_mode != AGGR_GLOBAL)
526 * GLOBAL aggregation mode only has a single aggr counts,
527 * so we can use ps->aggr[0] as the actual output.
529 count = ps->aggr[0].counts.values;
530 update_stats(&ps->res_stats, *count);
533 fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
534 evsel__name(counter), count[0], count[1], count[2]);
540 static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias)
542 struct perf_stat_evsel *ps_a = evsel->stats;
543 struct perf_stat_evsel *ps_b = alias->stats;
546 if (ps_a->aggr == NULL && ps_b->aggr == NULL)
549 if (ps_a->nr_aggr != ps_b->nr_aggr) {
550 pr_err("Unmatched aggregation mode between aliases\n");
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;
558 /* NB: don't increase aggr.nr for aliases */
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;
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)
570 if (strcmp(evsel__name(evsel_a), evsel__name(evsel_b)))
573 if (evsel_a->scale != evsel_b->scale)
576 if (evsel_a->cgrp != evsel_b->cgrp)
579 if (strcmp(evsel_a->unit, evsel_b->unit))
582 if (evsel__is_clock(evsel_a) != evsel__is_clock(evsel_b))
585 return !!strcmp(evsel_a->pmu_name, evsel_b->pmu_name);
588 static void evsel__merge_aliases(struct evsel *evsel)
590 struct evlist *evlist = evsel->evlist;
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;
603 static bool evsel__should_merge_hybrid(const struct evsel *evsel,
604 const struct perf_stat_config *config)
606 return config->hybrid_merge && evsel__is_hybrid(evsel);
609 static void evsel__merge_stats(struct evsel *evsel, struct perf_stat_config *config)
611 /* this evsel is already merged */
612 if (evsel->merged_stat)
615 if (evsel->auto_merge_stats || evsel__should_merge_hybrid(evsel, config))
616 evsel__merge_aliases(evsel);
619 /* merge the same uncore and hybrid events if requested */
620 void perf_stat_merge_counters(struct perf_stat_config *config, struct evlist *evlist)
624 if (config->no_merge)
627 evlist__for_each_entry(evlist, evsel)
628 evsel__merge_stats(evsel, config);
631 static void evsel__update_percore_stats(struct evsel *evsel, struct aggr_cpu_id *core_id)
633 struct perf_stat_evsel *ps = evsel->stats;
634 struct perf_counts_values counts = { 0, };
635 struct aggr_cpu_id id;
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];
643 id = aggr_cpu_id__core(cpu, NULL);
644 if (!aggr_cpu_id__equal(core_id, &id))
647 counts.val += aggr->counts.val;
648 counts.ena += aggr->counts.ena;
649 counts.run += aggr->counts.run;
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];
656 id = aggr_cpu_id__core(cpu, NULL);
657 if (!aggr_cpu_id__equal(core_id, &id))
660 aggr->counts.val = counts.val;
661 aggr->counts.ena = counts.ena;
662 aggr->counts.run = counts.run;
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)
671 struct perf_stat_evsel *ps = evsel->stats;
672 struct aggr_cpu_id core_id;
679 perf_cpu_map__for_each_cpu(cpu, idx, evsel->core.cpus) {
680 struct perf_stat_aggr *aggr = &ps->aggr[idx];
685 core_id = aggr_cpu_id__core(cpu, NULL);
686 evsel__update_percore_stats(evsel, &core_id);
690 /* process cpu stats on per-core events */
691 void perf_stat_process_percore(struct perf_stat_config *config, struct evlist *evlist)
695 if (config->aggr_mode != AGGR_NONE)
698 evlist__for_each_entry(evlist, evsel)
699 evsel__process_percore(evsel);
702 static void evsel__update_shadow_stats(struct evsel *evsel)
704 struct perf_stat_evsel *ps = evsel->stats;
707 if (ps->aggr == NULL)
710 for (i = 0; i < ps->nr_aggr; i++) {
711 struct perf_counts_values *aggr_counts = &ps->aggr[i].counts;
713 perf_stat__update_shadow_stats(evsel, aggr_counts->val, i, &rt_stat);
717 void perf_stat_process_shadow_stats(struct perf_stat_config *config __maybe_unused,
718 struct evlist *evlist)
722 evlist__for_each_entry(evlist, evsel)
723 evsel__update_shadow_stats(evsel);
726 int perf_event__process_stat_event(struct perf_session *session,
727 union perf_event *event)
729 struct perf_counts_values count, *ptr;
730 struct perf_record_stat *st = &event->stat;
731 struct evsel *counter;
738 counter = evlist__id2evsel(session->evlist, st->id);
740 pr_err("Failed to resolve counter for stat event.\n");
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));
748 ptr = perf_counts(counter->counts, cpu_map_idx, st->thread);
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));
755 counter->supported = true;
759 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp)
761 struct perf_record_stat *st = (struct perf_record_stat *)event;
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);
772 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp)
774 struct perf_record_stat_round *rd = (struct perf_record_stat_round *)event;
777 ret = fprintf(fp, "\n... time %" PRI_lu64 ", type %s\n", rd->time,
778 rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL");
783 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp)
785 struct perf_stat_config sc;
788 perf_event__read_stat_config(&sc, &event->stat_config);
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);
798 int create_perf_stat_counter(struct evsel *evsel,
799 struct perf_stat_config *config,
800 struct target *target,
803 struct perf_event_attr *attr = &evsel->core.attr;
804 struct evsel *leader = evsel__leader(evsel);
806 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
807 PERF_FORMAT_TOTAL_TIME_RUNNING;
810 * The event is part of non trivial group, let's enable
811 * the group read (for leader) and ID retrieval for all
814 if (leader->core.nr_members > 1)
815 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
817 attr->inherit = !config->no_inherit && list_empty(&evsel->bpf_counter_list);
820 * Some events get initialized with sample_(period/type) set,
821 * like tracepoints. Clear it up for counting.
823 attr->sample_period = 0;
825 if (config->identifier)
826 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
828 if (config->all_user) {
829 attr->exclude_kernel = 1;
830 attr->exclude_user = 0;
833 if (config->all_kernel) {
834 attr->exclude_kernel = 0;
835 attr->exclude_user = 1;
839 * Disabling all counters initially, they will be enabled
840 * either manually by us or by kernel via enable_on_exec
843 if (evsel__is_group_leader(evsel)) {
847 * In case of initial_delay we enable tracee
850 if (target__none(target) && !config->initial_delay)
851 attr->enable_on_exec = 1;
854 if (target__has_cpu(target) && !target__has_per_thread(target))
855 return evsel__open_per_cpu(evsel, evsel__cpus(evsel), cpu_map_idx);
857 return evsel__open_per_thread(evsel, evsel->core.threads);