drm/bridge: adv7511: Switch to atomic operations
[drm/drm-misc.git] / tools / testing / selftests / user_events / perf_test.c
blob5288e768b207a9c57fa7ca020f7560cda4d1bc51
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * User Events Perf Events Test Program
5 * Copyright (c) 2021 Beau Belgrave <beaub@linux.microsoft.com>
6 */
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>
19 #include "../kselftest_harness.h"
20 #include "user_events_selftests.h"
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";
26 struct event {
27 __u32 index;
28 __u32 field1;
29 __u32 field2;
32 static long perf_event_open(struct perf_event_attr *pe, pid_t pid,
33 int cpu, int group_fd, unsigned long flags)
35 return syscall(__NR_perf_event_open, pe, pid, cpu, group_fd, flags);
38 static int get_id(void)
40 FILE *fp = fopen(id_file, "r");
41 int ret, id = 0;
43 if (!fp)
44 return -1;
46 ret = fscanf(fp, "%d", &id);
47 fclose(fp);
49 if (ret != 1)
50 return -1;
52 return id;
55 static int get_offset(void)
57 FILE *fp = fopen(fmt_file, "r");
58 int ret, c, last = 0, offset = 0;
60 if (!fp)
61 return -1;
63 /* Read until empty line */
64 while (true) {
65 c = getc(fp);
67 if (c == EOF)
68 break;
70 if (last == '\n' && c == '\n')
71 break;
73 last = c;
76 ret = fscanf(fp, "\tfield:u32 field1;\toffset:%d;", &offset);
77 fclose(fp);
79 if (ret != 1)
80 return -1;
82 return offset;
85 static int clear(int *check)
87 struct user_unreg unreg = {0};
89 unreg.size = sizeof(unreg);
90 unreg.disable_bit = 31;
91 unreg.disable_addr = (__u64)check;
93 int fd = open(data_file, O_RDWR);
95 if (fd == -1)
96 return -1;
98 if (ioctl(fd, DIAG_IOCSUNREG, &unreg) == -1)
99 if (errno != ENOENT)
100 return -1;
102 if (ioctl(fd, DIAG_IOCSDEL, "__test_event") == -1)
103 if (errno != ENOENT)
104 return -1;
106 close(fd);
108 return 0;
111 FIXTURE(user) {
112 int data_fd;
113 int check;
114 bool umount;
117 FIXTURE_SETUP(user) {
118 USER_EVENT_FIXTURE_SETUP(return, self->umount);
120 self->data_fd = open(data_file, O_RDWR);
121 ASSERT_NE(-1, self->data_fd);
124 FIXTURE_TEARDOWN(user) {
125 USER_EVENT_FIXTURE_TEARDOWN(self->umount);
127 close(self->data_fd);
129 if (clear(&self->check) != 0)
130 printf("WARNING: Clear didn't work!\n");
133 TEST_F(user, perf_write) {
134 struct perf_event_attr pe = {0};
135 struct user_reg reg = {0};
136 struct event event;
137 struct perf_event_mmap_page *perf_page;
138 int page_size = sysconf(_SC_PAGESIZE);
139 int id, fd, offset;
140 __u32 *val;
142 reg.size = sizeof(reg);
143 reg.name_args = (__u64)"__test_event u32 field1; u32 field2";
144 reg.enable_bit = 31;
145 reg.enable_addr = (__u64)&self->check;
146 reg.enable_size = sizeof(self->check);
148 /* Register should work */
149 ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, &reg));
150 ASSERT_EQ(0, reg.write_index);
151 ASSERT_EQ(0, self->check);
153 /* Id should be there */
154 id = get_id();
155 ASSERT_NE(-1, id);
156 offset = get_offset();
157 ASSERT_NE(-1, offset);
159 pe.type = PERF_TYPE_TRACEPOINT;
160 pe.size = sizeof(pe);
161 pe.config = id;
162 pe.sample_type = PERF_SAMPLE_RAW;
163 pe.sample_period = 1;
164 pe.wakeup_events = 1;
166 /* Tracepoint attach should work */
167 fd = perf_event_open(&pe, 0, -1, -1, 0);
168 ASSERT_NE(-1, fd);
170 perf_page = mmap(NULL, page_size * 2, PROT_READ, MAP_SHARED, fd, 0);
171 ASSERT_NE(MAP_FAILED, perf_page);
173 /* Status should be updated */
174 ASSERT_EQ(1 << reg.enable_bit, self->check);
176 event.index = reg.write_index;
177 event.field1 = 0xc001;
178 event.field2 = 0xc01a;
180 /* Ensure write shows up at correct offset */
181 ASSERT_NE(-1, write(self->data_fd, &event, sizeof(event)));
182 val = (void *)(((char *)perf_page) + perf_page->data_offset);
183 ASSERT_EQ(PERF_RECORD_SAMPLE, *val);
184 /* Skip over header and size, move to offset */
185 val += 3;
186 val = (void *)((char *)val) + offset;
187 /* Ensure correct */
188 ASSERT_EQ(event.field1, *val++);
189 ASSERT_EQ(event.field2, *val++);
191 munmap(perf_page, page_size * 2);
192 close(fd);
194 /* Status should be updated */
195 ASSERT_EQ(0, self->check);
198 TEST_F(user, perf_empty_events) {
199 struct perf_event_attr pe = {0};
200 struct user_reg reg = {0};
201 struct perf_event_mmap_page *perf_page;
202 int page_size = sysconf(_SC_PAGESIZE);
203 int id, fd;
204 __u32 *val;
206 reg.size = sizeof(reg);
207 reg.name_args = (__u64)"__test_event";
208 reg.enable_bit = 31;
209 reg.enable_addr = (__u64)&self->check;
210 reg.enable_size = sizeof(self->check);
212 /* Register should work */
213 ASSERT_EQ(0, ioctl(self->data_fd, DIAG_IOCSREG, &reg));
214 ASSERT_EQ(0, reg.write_index);
215 ASSERT_EQ(0, self->check);
217 /* Id should be there */
218 id = get_id();
219 ASSERT_NE(-1, id);
221 pe.type = PERF_TYPE_TRACEPOINT;
222 pe.size = sizeof(pe);
223 pe.config = id;
224 pe.sample_type = PERF_SAMPLE_RAW;
225 pe.sample_period = 1;
226 pe.wakeup_events = 1;
228 /* Tracepoint attach should work */
229 fd = perf_event_open(&pe, 0, -1, -1, 0);
230 ASSERT_NE(-1, fd);
232 perf_page = mmap(NULL, page_size * 2, PROT_READ, MAP_SHARED, fd, 0);
233 ASSERT_NE(MAP_FAILED, perf_page);
235 /* Status should be updated */
236 ASSERT_EQ(1 << reg.enable_bit, self->check);
238 /* Ensure write shows up at correct offset */
239 ASSERT_NE(-1, write(self->data_fd, &reg.write_index,
240 sizeof(reg.write_index)));
241 val = (void *)(((char *)perf_page) + perf_page->data_offset);
242 ASSERT_EQ(PERF_RECORD_SAMPLE, *val);
244 munmap(perf_page, page_size * 2);
245 close(fd);
247 /* Status should be updated */
248 ASSERT_EQ(0, self->check);
251 int main(int argc, char **argv)
253 return test_harness_run(argc, argv);