1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/perf_event.h>
5 #include <perf/cpumap.h>
6 #include <perf/threadmap.h>
7 #include <perf/evsel.h>
8 #include <internal/tests.h>
10 static int libperf_print(enum libperf_print_level level
,
11 const char *fmt
, va_list ap
)
13 return vfprintf(stderr
, fmt
, ap
);
16 static int test_stat_cpu(void)
18 struct perf_cpu_map
*cpus
;
19 struct perf_evsel
*evsel
;
20 struct perf_event_attr attr
= {
21 .type
= PERF_TYPE_SOFTWARE
,
22 .config
= PERF_COUNT_SW_CPU_CLOCK
,
26 cpus
= perf_cpu_map__new(NULL
);
27 __T("failed to create cpus", cpus
);
29 evsel
= perf_evsel__new(&attr
);
30 __T("failed to create evsel", evsel
);
32 err
= perf_evsel__open(evsel
, cpus
, NULL
);
33 __T("failed to open evsel", err
== 0);
35 perf_cpu_map__for_each_cpu(cpu
, tmp
, cpus
) {
36 struct perf_counts_values counts
= { .val
= 0 };
38 perf_evsel__read(evsel
, cpu
, 0, &counts
);
39 __T("failed to read value for evsel", counts
.val
!= 0);
42 perf_evsel__close(evsel
);
43 perf_evsel__delete(evsel
);
45 perf_cpu_map__put(cpus
);
49 static int test_stat_thread(void)
51 struct perf_counts_values counts
= { .val
= 0 };
52 struct perf_thread_map
*threads
;
53 struct perf_evsel
*evsel
;
54 struct perf_event_attr attr
= {
55 .type
= PERF_TYPE_SOFTWARE
,
56 .config
= PERF_COUNT_SW_TASK_CLOCK
,
60 threads
= perf_thread_map__new_dummy();
61 __T("failed to create threads", threads
);
63 perf_thread_map__set_pid(threads
, 0, 0);
65 evsel
= perf_evsel__new(&attr
);
66 __T("failed to create evsel", evsel
);
68 err
= perf_evsel__open(evsel
, NULL
, threads
);
69 __T("failed to open evsel", err
== 0);
71 perf_evsel__read(evsel
, 0, 0, &counts
);
72 __T("failed to read value for evsel", counts
.val
!= 0);
74 perf_evsel__close(evsel
);
75 perf_evsel__delete(evsel
);
77 perf_thread_map__put(threads
);
81 static int test_stat_thread_enable(void)
83 struct perf_counts_values counts
= { .val
= 0 };
84 struct perf_thread_map
*threads
;
85 struct perf_evsel
*evsel
;
86 struct perf_event_attr attr
= {
87 .type
= PERF_TYPE_SOFTWARE
,
88 .config
= PERF_COUNT_SW_TASK_CLOCK
,
93 threads
= perf_thread_map__new_dummy();
94 __T("failed to create threads", threads
);
96 perf_thread_map__set_pid(threads
, 0, 0);
98 evsel
= perf_evsel__new(&attr
);
99 __T("failed to create evsel", evsel
);
101 err
= perf_evsel__open(evsel
, NULL
, threads
);
102 __T("failed to open evsel", err
== 0);
104 perf_evsel__read(evsel
, 0, 0, &counts
);
105 __T("failed to read value for evsel", counts
.val
== 0);
107 err
= perf_evsel__enable(evsel
);
108 __T("failed to enable evsel", err
== 0);
110 perf_evsel__read(evsel
, 0, 0, &counts
);
111 __T("failed to read value for evsel", counts
.val
!= 0);
113 err
= perf_evsel__disable(evsel
);
114 __T("failed to enable evsel", err
== 0);
116 perf_evsel__close(evsel
);
117 perf_evsel__delete(evsel
);
119 perf_thread_map__put(threads
);
123 int main(int argc
, char **argv
)
127 libperf_init(libperf_print
);
131 test_stat_thread_enable();