blob: 582f3aeaf5e45091314476128f6127833cea002a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -03002#include <errno.h>
Jiri Olsad8095602015-08-07 12:51:03 +02003#include <stdlib.h>
Jin Yaocf4d9bd2020-05-20 12:27:34 +08004#include <string.h>
Jiri Olsad8095602015-08-07 12:51:03 +02005#include "evsel.h"
6#include "counts.h"
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -03007#include <linux/zalloc.h>
Jiri Olsad8095602015-08-07 12:51:03 +02008
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;
Jiri Olsadf1d6852019-07-21 13:23:48 +020023
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;
Jiri Olsad8095602015-08-07 12:51:03 +020032 }
33
34 return counts;
35}
36
37void perf_counts__delete(struct perf_counts *counts)
38{
39 if (counts) {
Jiri Olsadf1d6852019-07-21 13:23:48 +020040 xyarray__delete(counts->loaded);
Jiri Olsad8095602015-08-07 12:51:03 +020041 xyarray__delete(counts->values);
42 free(counts);
43 }
44}
45
Jin Yaocf4d9bd2020-05-20 12:27:34 +080046void perf_counts__reset(struct perf_counts *counts)
Jiri Olsad8095602015-08-07 12:51:03 +020047{
Jiri Olsadf1d6852019-07-21 13:23:48 +020048 xyarray__reset(counts->loaded);
Jiri Olsad8095602015-08-07 12:51:03 +020049 xyarray__reset(counts->values);
Jin Yaocf4d9bd2020-05-20 12:27:34 +080050 memset(&counts->aggr, 0, sizeof(struct perf_counts_values));
Jiri Olsad8095602015-08-07 12:51:03 +020051}
52
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -030053void evsel__reset_counts(struct evsel *evsel)
Jiri Olsad8095602015-08-07 12:51:03 +020054{
55 perf_counts__reset(evsel->counts);
56}
57
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -030058int evsel__alloc_counts(struct evsel *evsel, int ncpus, int nthreads)
Jiri Olsad8095602015-08-07 12:51:03 +020059{
60 evsel->counts = perf_counts__new(ncpus, nthreads);
61 return evsel->counts != NULL ? 0 : -ENOMEM;
62}
63
Arnaldo Carvalho de Melo7d1e2392020-05-06 13:38:26 -030064void evsel__free_counts(struct evsel *evsel)
Jiri Olsad8095602015-08-07 12:51:03 +020065{
66 perf_counts__delete(evsel->counts);
67 evsel->counts = NULL;
68}