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 static void evsel__reset_aggr_stats(struct evsel *evsel)
82 struct perf_stat_evsel *ps = evsel->stats;
83 struct perf_stat_aggr *aggr = ps->aggr;
86 memset(aggr, 0, sizeof(*aggr) * ps->nr_aggr);
89 static void evsel__reset_stat_priv(struct evsel *evsel)
91 struct perf_stat_evsel *ps = evsel->stats;
93 init_stats(&ps->res_stats);
94 evsel__reset_aggr_stats(evsel);
97 static int evsel__alloc_aggr_stats(struct evsel *evsel, int nr_aggr)
99 struct perf_stat_evsel *ps = evsel->stats;
104 ps->nr_aggr = nr_aggr;
105 ps->aggr = calloc(nr_aggr, sizeof(*ps->aggr));
106 if (ps->aggr == NULL)
112 int evlist__alloc_aggr_stats(struct evlist *evlist, int nr_aggr)
116 evlist__for_each_entry(evlist, evsel) {
117 if (evsel__alloc_aggr_stats(evsel, nr_aggr) < 0)
123 static int evsel__alloc_stat_priv(struct evsel *evsel, int nr_aggr)
125 struct perf_stat_evsel *ps;
127 ps = zalloc(sizeof(*ps));
133 if (nr_aggr && evsel__alloc_aggr_stats(evsel, nr_aggr) < 0) {
139 evsel__reset_stat_priv(evsel);
143 static void evsel__free_stat_priv(struct evsel *evsel)
145 struct perf_stat_evsel *ps = evsel->stats;
149 zfree(&ps->group_data);
151 zfree(&evsel->stats);
154 static int evsel__alloc_prev_raw_counts(struct evsel *evsel)
156 int cpu_map_nr = evsel__nr_cpus(evsel);
157 int nthreads = perf_thread_map__nr(evsel->core.threads);
158 struct perf_counts *counts;
160 counts = perf_counts__new(cpu_map_nr, nthreads);
162 evsel->prev_raw_counts = counts;
164 return counts ? 0 : -ENOMEM;
167 static void evsel__free_prev_raw_counts(struct evsel *evsel)
169 perf_counts__delete(evsel->prev_raw_counts);
170 evsel->prev_raw_counts = NULL;
173 static void evsel__reset_prev_raw_counts(struct evsel *evsel)
175 if (evsel->prev_raw_counts)
176 perf_counts__reset(evsel->prev_raw_counts);
179 static int evsel__alloc_stats(struct evsel *evsel, int nr_aggr, bool alloc_raw)
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))
189 int evlist__alloc_stats(struct perf_stat_config *config,
190 struct evlist *evlist, bool alloc_raw)
195 if (config && config->aggr_map)
196 nr_aggr = config->aggr_map->nr;
198 evlist__for_each_entry(evlist, evsel) {
199 if (evsel__alloc_stats(evsel, nr_aggr, alloc_raw))
206 evlist__free_stats(evlist);
210 void evlist__free_stats(struct evlist *evlist)
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);
221 void evlist__reset_stats(struct evlist *evlist)
225 evlist__for_each_entry(evlist, evsel) {
226 evsel__reset_stat_priv(evsel);
227 evsel__reset_counts(evsel);
231 void evlist__reset_aggr_stats(struct evlist *evlist)
235 evlist__for_each_entry(evlist, evsel)
236 evsel__reset_aggr_stats(evsel);
239 void evlist__reset_prev_raw_counts(struct evlist *evlist)
243 evlist__for_each_entry(evlist, evsel)
244 evsel__reset_prev_raw_counts(evsel);
247 static void evsel__copy_prev_raw_counts(struct evsel *evsel)
249 int idx, nthreads = perf_thread_map__nr(evsel->core.threads);
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);
259 void evlist__copy_prev_raw_counts(struct evlist *evlist)
263 evlist__for_each_entry(evlist, evsel)
264 evsel__copy_prev_raw_counts(evsel);
267 static size_t pkg_id_hash(long __key, void *ctx __maybe_unused)
269 uint64_t *key = (uint64_t *) __key;
271 return *key & 0xffffffff;
274 static bool pkg_id_equal(long __key1, long __key2, void *ctx __maybe_unused)
276 uint64_t *key1 = (uint64_t *) __key1;
277 uint64_t *key2 = (uint64_t *) __key2;
279 return *key1 == *key2;
282 static int check_per_pkg(struct evsel *counter, struct perf_counts_values *vals,
283 int cpu_map_idx, bool *skip)
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);
293 if (!counter->per_pkg)
296 if (perf_cpu_map__empty(cpus))
300 mask = hashmap__new(pkg_id_hash, pkg_id_equal, NULL);
304 counter->per_pkg_mask = mask;
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.
315 if (!(vals->run && vals->ena))
318 s = cpu__get_socket_id(cpu);
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.
326 d = cpu__get_die_id(cpu);
330 key = malloc(sizeof(*key));
334 *key = (uint64_t)d << 32 | s;
335 if (hashmap__find(mask, key, NULL)) {
339 ret = hashmap__add(mask, key, 1);
344 static bool evsel__count_has_error(struct evsel *evsel,
345 struct perf_counts_values *count,
346 struct perf_stat_config *config)
348 /* the evsel was failed already */
349 if (evsel->err || evsel->counts->scaled == -1)
352 /* this is meaningful for CPU aggregation modes only */
353 if (config->aggr_mode == AGGR_GLOBAL)
356 /* it's considered ok when it actually ran */
357 if (count->ena != 0 && count->run != 0)
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)
368 struct perf_stat_evsel *ps = evsel->stats;
369 static struct perf_counts_values zero;
372 if (check_per_pkg(evsel, count, cpu_map_idx, &skip)) {
373 pr_err("failed to read per-pkg counter\n");
380 if (!evsel->snapshot)
381 evsel__compute_deltas(evsel, cpu_map_idx, thread, count);
382 perf_counts_values__scale(count, config->scale, NULL);
384 if (config->aggr_mode == AGGR_THREAD) {
385 struct perf_counts_values *aggr_counts = &ps->aggr[thread].counts;
388 * Skip value 0 when enabling --per-thread globally,
389 * otherwise too many 0 output.
391 if (count->val == 0 && config->system_wide)
394 ps->aggr[thread].nr++;
396 aggr_counts->val += count->val;
397 aggr_counts->ena += count->ena;
398 aggr_counts->run += count->run;
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;
408 for (i = 0; i < ps->nr_aggr; i++) {
409 if (!aggr_cpu_id__equal(&aggr_id, &config->aggr_map->map[i]))
412 ps_aggr = &ps->aggr[i];
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.
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;
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;
439 static int process_counter_maps(struct perf_stat_config *config,
440 struct evsel *counter)
442 int nthreads = perf_thread_map__nr(counter->core.threads);
443 int ncpus = evsel__nr_cpus(counter);
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)))
457 int perf_stat_process_counter(struct perf_stat_config *config,
458 struct evsel *counter)
460 struct perf_stat_evsel *ps = counter->stats;
464 if (counter->per_pkg)
465 evsel__zero_per_pkg(counter);
467 ret = process_counter_maps(config, counter);
471 if (config->aggr_mode != AGGR_GLOBAL)
475 * GLOBAL aggregation mode only has a single aggr counts,
476 * so we can use ps->aggr[0] as the actual output.
478 count = ps->aggr[0].counts.values;
479 update_stats(&ps->res_stats, *count);
482 fprintf(config->output, "%s: %" PRIu64 " %" PRIu64 " %" PRIu64 "\n",
483 evsel__name(counter), count[0], count[1], count[2]);
489 static int evsel__merge_aggr_counters(struct evsel *evsel, struct evsel *alias)
491 struct perf_stat_evsel *ps_a = evsel->stats;
492 struct perf_stat_evsel *ps_b = alias->stats;
495 if (ps_a->aggr == NULL && ps_b->aggr == NULL)
498 if (ps_a->nr_aggr != ps_b->nr_aggr) {
499 pr_err("Unmatched aggregation mode between aliases\n");
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;
507 /* NB: don't increase aggr.nr for aliases */
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;
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)
519 if (strcmp(evsel__name(evsel_a), evsel__name(evsel_b)))
522 if (evsel_a->scale != evsel_b->scale)
525 if (evsel_a->cgrp != evsel_b->cgrp)
528 if (strcmp(evsel_a->unit, evsel_b->unit))
531 if (evsel__is_clock(evsel_a) != evsel__is_clock(evsel_b))
534 return !!strcmp(evsel_a->pmu_name, evsel_b->pmu_name);
537 static void evsel__merge_aliases(struct evsel *evsel)
539 struct evlist *evlist = evsel->evlist;
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;
552 static bool evsel__should_merge_hybrid(const struct evsel *evsel,
553 const struct perf_stat_config *config)
555 return config->hybrid_merge && evsel__is_hybrid(evsel);
558 static void evsel__merge_stats(struct evsel *evsel, struct perf_stat_config *config)
560 /* this evsel is already merged */
561 if (evsel->merged_stat)
564 if (evsel->auto_merge_stats || evsel__should_merge_hybrid(evsel, config))
565 evsel__merge_aliases(evsel);
568 /* merge the same uncore and hybrid events if requested */
569 void perf_stat_merge_counters(struct perf_stat_config *config, struct evlist *evlist)
573 if (config->no_merge)
576 evlist__for_each_entry(evlist, evsel)
577 evsel__merge_stats(evsel, config);
580 static void evsel__update_percore_stats(struct evsel *evsel, struct aggr_cpu_id *core_id)
582 struct perf_stat_evsel *ps = evsel->stats;
583 struct perf_counts_values counts = { 0, };
584 struct aggr_cpu_id id;
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];
592 id = aggr_cpu_id__core(cpu, NULL);
593 if (!aggr_cpu_id__equal(core_id, &id))
596 counts.val += aggr->counts.val;
597 counts.ena += aggr->counts.ena;
598 counts.run += aggr->counts.run;
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];
605 id = aggr_cpu_id__core(cpu, NULL);
606 if (!aggr_cpu_id__equal(core_id, &id))
609 aggr->counts.val = counts.val;
610 aggr->counts.ena = counts.ena;
611 aggr->counts.run = counts.run;
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)
620 struct perf_stat_evsel *ps = evsel->stats;
621 struct aggr_cpu_id core_id;
628 perf_cpu_map__for_each_cpu(cpu, idx, evsel->core.cpus) {
629 struct perf_stat_aggr *aggr = &ps->aggr[idx];
634 core_id = aggr_cpu_id__core(cpu, NULL);
635 evsel__update_percore_stats(evsel, &core_id);
639 /* process cpu stats on per-core events */
640 void perf_stat_process_percore(struct perf_stat_config *config, struct evlist *evlist)
644 if (config->aggr_mode != AGGR_NONE)
647 evlist__for_each_entry(evlist, evsel)
648 evsel__process_percore(evsel);
651 int perf_event__process_stat_event(struct perf_session *session,
652 union perf_event *event)
654 struct perf_counts_values count, *ptr;
655 struct perf_record_stat *st = &event->stat;
656 struct evsel *counter;
663 counter = evlist__id2evsel(session->evlist, st->id);
665 pr_err("Failed to resolve counter for stat event.\n");
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));
673 ptr = perf_counts(counter->counts, cpu_map_idx, st->thread);
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));
680 counter->supported = true;
684 size_t perf_event__fprintf_stat(union perf_event *event, FILE *fp)
686 struct perf_record_stat *st = (struct perf_record_stat *)event;
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);
697 size_t perf_event__fprintf_stat_round(union perf_event *event, FILE *fp)
699 struct perf_record_stat_round *rd = (struct perf_record_stat_round *)event;
702 ret = fprintf(fp, "\n... time %" PRI_lu64 ", type %s\n", rd->time,
703 rd->type == PERF_STAT_ROUND_TYPE__FINAL ? "FINAL" : "INTERVAL");
708 size_t perf_event__fprintf_stat_config(union perf_event *event, FILE *fp)
710 struct perf_stat_config sc;
713 perf_event__read_stat_config(&sc, &event->stat_config);
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);
723 int create_perf_stat_counter(struct evsel *evsel,
724 struct perf_stat_config *config,
725 struct target *target,
728 struct perf_event_attr *attr = &evsel->core.attr;
729 struct evsel *leader = evsel__leader(evsel);
731 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
732 PERF_FORMAT_TOTAL_TIME_RUNNING;
735 * The event is part of non trivial group, let's enable
736 * the group read (for leader) and ID retrieval for all
739 if (leader->core.nr_members > 1)
740 attr->read_format |= PERF_FORMAT_ID|PERF_FORMAT_GROUP;
742 attr->inherit = !config->no_inherit && list_empty(&evsel->bpf_counter_list);
745 * Some events get initialized with sample_(period/type) set,
746 * like tracepoints. Clear it up for counting.
748 attr->sample_period = 0;
750 if (config->identifier)
751 attr->sample_type = PERF_SAMPLE_IDENTIFIER;
753 if (config->all_user) {
754 attr->exclude_kernel = 1;
755 attr->exclude_user = 0;
758 if (config->all_kernel) {
759 attr->exclude_kernel = 0;
760 attr->exclude_user = 1;
764 * Disabling all counters initially, they will be enabled
765 * either manually by us or by kernel via enable_on_exec
768 if (evsel__is_group_leader(evsel)) {
771 if (target__enable_on_exec(target))
772 attr->enable_on_exec = 1;
775 if (target__has_cpu(target) && !target__has_per_thread(target))
776 return evsel__open_per_cpu(evsel, evsel__cpus(evsel), cpu_map_idx);
778 return evsel__open_per_thread(evsel, evsel->core.threads);