xref: /linux/tools/lib/perf/tests/test-evsel.c (revision 808094fcbf4196be0feb17afbbdc182ec95c8cec)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdarg.h>
3 #include <stdio.h>
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>
9 
10 static int libperf_print(enum libperf_print_level level,
11 			 const char *fmt, va_list ap)
12 {
13 	return vfprintf(stderr, fmt, ap);
14 }
15 
16 static int test_stat_cpu(void)
17 {
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,
23 	};
24 	int err, cpu, tmp;
25 
26 	cpus = perf_cpu_map__new(NULL);
27 	__T("failed to create cpus", cpus);
28 
29 	evsel = perf_evsel__new(&attr);
30 	__T("failed to create evsel", evsel);
31 
32 	err = perf_evsel__open(evsel, cpus, NULL);
33 	__T("failed to open evsel", err == 0);
34 
35 	perf_cpu_map__for_each_cpu(cpu, tmp, cpus) {
36 		struct perf_counts_values counts = { .val = 0 };
37 
38 		perf_evsel__read(evsel, cpu, 0, &counts);
39 		__T("failed to read value for evsel", counts.val != 0);
40 	}
41 
42 	perf_evsel__close(evsel);
43 	perf_evsel__delete(evsel);
44 
45 	perf_cpu_map__put(cpus);
46 	return 0;
47 }
48 
49 static int test_stat_thread(void)
50 {
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,
57 	};
58 	int err;
59 
60 	threads = perf_thread_map__new_dummy();
61 	__T("failed to create threads", threads);
62 
63 	perf_thread_map__set_pid(threads, 0, 0);
64 
65 	evsel = perf_evsel__new(&attr);
66 	__T("failed to create evsel", evsel);
67 
68 	err = perf_evsel__open(evsel, NULL, threads);
69 	__T("failed to open evsel", err == 0);
70 
71 	perf_evsel__read(evsel, 0, 0, &counts);
72 	__T("failed to read value for evsel", counts.val != 0);
73 
74 	perf_evsel__close(evsel);
75 	perf_evsel__delete(evsel);
76 
77 	perf_thread_map__put(threads);
78 	return 0;
79 }
80 
81 static int test_stat_thread_enable(void)
82 {
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,
89 		.disabled = 1,
90 	};
91 	int err;
92 
93 	threads = perf_thread_map__new_dummy();
94 	__T("failed to create threads", threads);
95 
96 	perf_thread_map__set_pid(threads, 0, 0);
97 
98 	evsel = perf_evsel__new(&attr);
99 	__T("failed to create evsel", evsel);
100 
101 	err = perf_evsel__open(evsel, NULL, threads);
102 	__T("failed to open evsel", err == 0);
103 
104 	perf_evsel__read(evsel, 0, 0, &counts);
105 	__T("failed to read value for evsel", counts.val == 0);
106 
107 	err = perf_evsel__enable(evsel);
108 	__T("failed to enable evsel", err == 0);
109 
110 	perf_evsel__read(evsel, 0, 0, &counts);
111 	__T("failed to read value for evsel", counts.val != 0);
112 
113 	err = perf_evsel__disable(evsel);
114 	__T("failed to enable evsel", err == 0);
115 
116 	perf_evsel__close(evsel);
117 	perf_evsel__delete(evsel);
118 
119 	perf_thread_map__put(threads);
120 	return 0;
121 }
122 
123 int main(int argc, char **argv)
124 {
125 	__T_START;
126 
127 	libperf_init(libperf_print);
128 
129 	test_stat_cpu();
130 	test_stat_thread();
131 	test_stat_thread_enable();
132 
133 	__T_END;
134 	return tests_failed == 0 ? 0 : -1;
135 }
136