^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include "evsel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "counts.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/zalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) struct perf_counts *perf_counts__new(int ncpus, int nthreads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) struct perf_counts *counts = zalloc(sizeof(*counts));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) if (counts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct xyarray *values;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) values = xyarray__new(ncpus, nthreads, sizeof(struct perf_counts_values));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) if (!values) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) free(counts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) counts->values = values;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) values = xyarray__new(ncpus, nthreads, sizeof(bool));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (!values) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) xyarray__delete(counts->values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) free(counts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) counts->loaded = values;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return counts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) void perf_counts__delete(struct perf_counts *counts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (counts) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) xyarray__delete(counts->loaded);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) xyarray__delete(counts->values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) free(counts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) void perf_counts__reset(struct perf_counts *counts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) xyarray__reset(counts->loaded);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) xyarray__reset(counts->values);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) memset(&counts->aggr, 0, sizeof(struct perf_counts_values));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) void evsel__reset_counts(struct evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) perf_counts__reset(evsel->counts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int evsel__alloc_counts(struct evsel *evsel, int ncpus, int nthreads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) evsel->counts = perf_counts__new(ncpus, nthreads);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return evsel->counts != NULL ? 0 : -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) void evsel__free_counts(struct evsel *evsel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) perf_counts__delete(evsel->counts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) evsel->counts = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }