xref: /linux/tools/perf/util/counts.c (revision d257f9bf06129613de539ea71ecea60848b662cd)
1 #include <errno.h>
2 #include <stdlib.h>
3 #include "evsel.h"
4 #include "counts.h"
5 #include "util.h"
6 
7 struct perf_counts *perf_counts__new(int ncpus, int nthreads)
8 {
9 	struct perf_counts *counts = zalloc(sizeof(*counts));
10 
11 	if (counts) {
12 		struct xyarray *values;
13 
14 		values = xyarray__new(ncpus, nthreads, sizeof(struct perf_counts_values));
15 		if (!values) {
16 			free(counts);
17 			return NULL;
18 		}
19 
20 		counts->values = values;
21 	}
22 
23 	return counts;
24 }
25 
26 void perf_counts__delete(struct perf_counts *counts)
27 {
28 	if (counts) {
29 		xyarray__delete(counts->values);
30 		free(counts);
31 	}
32 }
33 
34 static void perf_counts__reset(struct perf_counts *counts)
35 {
36 	xyarray__reset(counts->values);
37 }
38 
39 void perf_evsel__reset_counts(struct perf_evsel *evsel)
40 {
41 	perf_counts__reset(evsel->counts);
42 }
43 
44 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus, int nthreads)
45 {
46 	evsel->counts = perf_counts__new(ncpus, nthreads);
47 	return evsel->counts != NULL ? 0 : -ENOMEM;
48 }
49 
50 void perf_evsel__free_counts(struct perf_evsel *evsel)
51 {
52 	perf_counts__delete(evsel->counts);
53 	evsel->counts = NULL;
54 }
55