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