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> |
| 4 | #include "evsel.h" |
| 5 | #include "counts.h" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 6 | #include <linux/zalloc.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 7 | |
| 8 | struct perf_counts *perf_counts__new(int ncpus, int nthreads) |
| 9 | { |
| 10 | struct perf_counts *counts = zalloc(sizeof(*counts)); |
| 11 | |
| 12 | if (counts) { |
| 13 | struct xyarray *values; |
| 14 | |
| 15 | values = xyarray__new(ncpus, nthreads, sizeof(struct perf_counts_values)); |
| 16 | if (!values) { |
| 17 | free(counts); |
| 18 | return NULL; |
| 19 | } |
| 20 | |
| 21 | counts->values = values; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 22 | |
| 23 | values = xyarray__new(ncpus, nthreads, sizeof(bool)); |
| 24 | if (!values) { |
| 25 | xyarray__delete(counts->values); |
| 26 | free(counts); |
| 27 | return NULL; |
| 28 | } |
| 29 | |
| 30 | counts->loaded = values; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | return counts; |
| 34 | } |
| 35 | |
| 36 | void perf_counts__delete(struct perf_counts *counts) |
| 37 | { |
| 38 | if (counts) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 39 | xyarray__delete(counts->loaded); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 40 | xyarray__delete(counts->values); |
| 41 | free(counts); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | static void perf_counts__reset(struct perf_counts *counts) |
| 46 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 47 | xyarray__reset(counts->loaded); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 48 | xyarray__reset(counts->values); |
| 49 | } |
| 50 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 51 | void perf_evsel__reset_counts(struct evsel *evsel) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 52 | { |
| 53 | perf_counts__reset(evsel->counts); |
| 54 | } |
| 55 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 56 | int perf_evsel__alloc_counts(struct evsel *evsel, int ncpus, int nthreads) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 57 | { |
| 58 | evsel->counts = perf_counts__new(ncpus, nthreads); |
| 59 | return evsel->counts != NULL ? 0 : -ENOMEM; |
| 60 | } |
| 61 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 62 | void perf_evsel__free_counts(struct evsel *evsel) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 63 | { |
| 64 | perf_counts__delete(evsel->counts); |
| 65 | evsel->counts = NULL; |
| 66 | } |