xref: /linux/tools/testing/selftests/user_events/perf_test.c (revision 34dc1baba215b826e454b8d19e4f24adbeb7d00d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * User Events Perf Events Test Program
4  *
5  * Copyright (c) 2021 Beau Belgrave <beaub@linux.microsoft.com>
6  */
7 
8 #include <errno.h>
9 #include <linux/user_events.h>
10 #include <linux/perf_event.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <fcntl.h>
14 #include <sys/ioctl.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <asm/unistd.h>
18 
19 #include "../kselftest_harness.h"
20 #include "user_events_selftests.h"
21 
22 const char *data_file = "/sys/kernel/tracing/user_events_data";
23 const char *id_file = "/sys/kernel/tracing/events/user_events/__test_event/id";
24 const char *fmt_file = "/sys/kernel/tracing/events/user_events/__test_event/format";
25 
26 struct event {
27 	__u32 index;
28 	__u32 field1;
29 	__u32 field2;
30 };
31 
32 static long perf_event_open(struct perf_event_attr *pe, pid_t pid,
33 			    int cpu, int group_fd, unsigned long flags)
34 {
35 	return syscall(__NR_perf_event_open, pe, pid, cpu, group_fd, flags);
36 }
37 
38 static int get_id(void)
39 {
40 	FILE *fp = fopen(id_file, "r");
41 	int ret, id = 0;
42 
43 	if (!fp)
44 		return -1;
45 
46 	ret = fscanf(fp, "%d", &id);
47 	fclose(fp);
48 
49 	if (ret != 1)
50 		return -1;
51 
52 	return id;
53 }
54 
55 static int get_offset(void)
56 {
57 	FILE *fp = fopen(fmt_file, "r");
58 	int ret, c, last = 0, offset = 0;
59 
60 	if (!fp)
61 		return -1;
62 
63 	/* Read until empty line */
64 	while (true) {
65 		c = getc(fp);
66 
67 		if (c == EOF)
68 			break;
69 
70 		if (last == '\n' && c == '\n')
71 			break;
72 
73 		last = c;
74 	}
75 
76 	ret = fscanf(fp, "\tfield:u32 field1;\toffset:%d;", &offset);
77 	fclose(fp);
78 
79 	if (ret != 1)
80 		return -1;
81 
82 	return offset;
83 }
84 
85 static int clear(int *check)
86 {
87 	struct user_unreg unreg = {0};
88 
89 	unreg.size = sizeof(unreg);
90 	unreg.disable_bit = 31;
91 	unreg.disable_addr = (__u64)check;
92 
93 	int fd = open(data_file, O_RDWR);
94 
95 	if (fd == -1)
96 		return -1;
97 
98 	if (ioctl(fd, DIAG_IOCSUNREG, &unreg) == -1)
99 		if (errno != ENOENT)
100 			return -1;
101 
102 	if (ioctl(fd, DIAG_IOCSDEL, "__test_event") == -1)
103 		if (errno != ENOENT)
104 			return -1;
105 
106 	close(fd);
107 
108 	return 0;
109 }
110 
111 FIXTURE(user) {
112 	int data_fd;
113 	int check;
114 };
115 
116 FIXTURE_SETUP(user) {
117 	USER_EVENT_FIXTURE_SETUP(return);
118 
119 	self->data_fd = open(data_file, O_RDWR);
120 	ASSERT_NE(-1, self->data_fd);
121 }
122 
123 FIXTURE_TEARDOWN(user) {
124 	close(self->data_fd);
125 
126 	if (clear(&self->check) != 0)
127 		printf("WARNING: Clear didn't work!\n");
128 }
129 
130 TEST_F(user, perf_write) {
131 	struct perf_event_attr pe = {0};
132 	struct user_reg reg = {0};
133 	struct event event;
134 	struct perf_event_mmap_page *perf_page;
135 	int page_size = sysconf(_SC_PAGESIZE);
136 	int id, fd, offset;
137 	__u32 *val;
138 
139 	reg.size = sizeof(reg);
140 	reg.name_args = (__u64)"__test_event u32 field1; u32 field2";
141 	reg.enable_bit = 31;
142 	reg.enable_addr = (__u64)&self->check;
143 	reg.enable_size = sizeof(self->check);
144 
145 	/* Register should work */
146 	ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, &reg));
147 	ASSERT_EQ(0, reg.write_index);
148 	ASSERT_EQ(0, self->check);
149 
150 	/* Id should be there */
151 	id = get_id();
152 	ASSERT_NE(-1, id);
153 	offset = get_offset();
154 	ASSERT_NE(-1, offset);
155 
156 	pe.type = PERF_TYPE_TRACEPOINT;
157 	pe.size = sizeof(pe);
158 	pe.config = id;
159 	pe.sample_type = PERF_SAMPLE_RAW;
160 	pe.sample_period = 1;
161 	pe.wakeup_events = 1;
162 
163 	/* Tracepoint attach should work */
164 	fd = perf_event_open(&pe, 0, -1, -1, 0);
165 	ASSERT_NE(-1, fd);
166 
167 	perf_page = mmap(NULL, page_size * 2, PROT_READ, MAP_SHARED, fd, 0);
168 	ASSERT_NE(MAP_FAILED, perf_page);
169 
170 	/* Status should be updated */
171 	ASSERT_EQ(1 << reg.enable_bit, self->check);
172 
173 	event.index = reg.write_index;
174 	event.field1 = 0xc001;
175 	event.field2 = 0xc01a;
176 
177 	/* Ensure write shows up at correct offset */
178 	ASSERT_NE(-1, write(self->data_fd, &event, sizeof(event)));
179 	val = (void *)(((char *)perf_page) + perf_page->data_offset);
180 	ASSERT_EQ(PERF_RECORD_SAMPLE, *val);
181 	/* Skip over header and size, move to offset */
182 	val += 3;
183 	val = (void *)((char *)val) + offset;
184 	/* Ensure correct */
185 	ASSERT_EQ(event.field1, *val++);
186 	ASSERT_EQ(event.field2, *val++);
187 
188 	munmap(perf_page, page_size * 2);
189 	close(fd);
190 
191 	/* Status should be updated */
192 	ASSERT_EQ(0, self->check);
193 }
194 
195 TEST_F(user, perf_empty_events) {
196 	struct perf_event_attr pe = {0};
197 	struct user_reg reg = {0};
198 	struct perf_event_mmap_page *perf_page;
199 	int page_size = sysconf(_SC_PAGESIZE);
200 	int id, fd;
201 	__u32 *val;
202 
203 	reg.size = sizeof(reg);
204 	reg.name_args = (__u64)"__test_event";
205 	reg.enable_bit = 31;
206 	reg.enable_addr = (__u64)&self->check;
207 	reg.enable_size = sizeof(self->check);
208 
209 	/* Register should work */
210 	ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, &reg));
211 	ASSERT_EQ(0, reg.write_index);
212 	ASSERT_EQ(0, self->check);
213 
214 	/* Id should be there */
215 	id = get_id();
216 	ASSERT_NE(-1, id);
217 
218 	pe.type = PERF_TYPE_TRACEPOINT;
219 	pe.size = sizeof(pe);
220 	pe.config = id;
221 	pe.sample_type = PERF_SAMPLE_RAW;
222 	pe.sample_period = 1;
223 	pe.wakeup_events = 1;
224 
225 	/* Tracepoint attach should work */
226 	fd = perf_event_open(&pe, 0, -1, -1, 0);
227 	ASSERT_NE(-1, fd);
228 
229 	perf_page = mmap(NULL, page_size * 2, PROT_READ, MAP_SHARED, fd, 0);
230 	ASSERT_NE(MAP_FAILED, perf_page);
231 
232 	/* Status should be updated */
233 	ASSERT_EQ(1 << reg.enable_bit, self->check);
234 
235 	/* Ensure write shows up at correct offset */
236 	ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
237 					sizeof(reg.write_index)));
238 	val = (void *)(((char *)perf_page) + perf_page->data_offset);
239 	ASSERT_EQ(PERF_RECORD_SAMPLE, *val);
240 
241 	munmap(perf_page, page_size * 2);
242 	close(fd);
243 
244 	/* Status should be updated */
245 	ASSERT_EQ(0, self->check);
246 }
247 
248 int main(int argc, char **argv)
249 {
250 	return test_harness_run(argc, argv);
251 }
252