blob: 582f3aeaf5e45091314476128f6127833cea002a [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2#include <errno.h>
3#include <stdlib.h>
Olivier Deprez157378f2022-04-04 15:47:50 +02004#include <string.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005#include "evsel.h"
6#include "counts.h"
David Brazdil0f672f62019-12-10 10:32:29 +00007#include <linux/zalloc.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008
9struct 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 Brazdil0f672f62019-12-10 10:32:29 +000023
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 Scullb4b6d4a2019-01-02 15:54:55 +000032 }
33
34 return counts;
35}
36
37void perf_counts__delete(struct perf_counts *counts)
38{
39 if (counts) {
David Brazdil0f672f62019-12-10 10:32:29 +000040 xyarray__delete(counts->loaded);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000041 xyarray__delete(counts->values);
42 free(counts);
43 }
44}
45
Olivier Deprez157378f2022-04-04 15:47:50 +020046void perf_counts__reset(struct perf_counts *counts)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047{
David Brazdil0f672f62019-12-10 10:32:29 +000048 xyarray__reset(counts->loaded);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049 xyarray__reset(counts->values);
Olivier Deprez157378f2022-04-04 15:47:50 +020050 memset(&counts->aggr, 0, sizeof(struct perf_counts_values));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000051}
52
Olivier Deprez157378f2022-04-04 15:47:50 +020053void evsel__reset_counts(struct evsel *evsel)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054{
55 perf_counts__reset(evsel->counts);
56}
57
Olivier Deprez157378f2022-04-04 15:47:50 +020058int evsel__alloc_counts(struct evsel *evsel, int ncpus, int nthreads)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059{
60 evsel->counts = perf_counts__new(ncpus, nthreads);
61 return evsel->counts != NULL ? 0 : -ENOMEM;
62}
63
Olivier Deprez157378f2022-04-04 15:47:50 +020064void evsel__free_counts(struct evsel *evsel)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065{
66 perf_counts__delete(evsel->counts);
67 evsel->counts = NULL;
68}