xref: /freebsd/tests/sys/aio/aio_kqueue_test.c (revision dd21556857e8d40f66bf5ad54754d9d52669ebf7)
1 /*-
2  * Copyright (C) 2005 IronPort Systems, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 /*
27  * Prerequisities:
28  * - AIO support must be compiled into the kernel (see sys/<arch>/NOTES for
29  *   more details).
30  *
31  * Note: it is a good idea to run this against a physical drive to
32  * exercise the physio fast path (ie. aio_kqueue /dev/<something safe>)
33  */
34 
35 #include <sys/types.h>
36 #include <sys/event.h>
37 #include <sys/time.h>
38 #include <assert.h>
39 #include <aio.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 #include "freebsd_test_suite/macros.h"
49 #include "local.h"
50 
51 #define PATH_TEMPLATE   "aio.XXXXXXXXXX"
52 
53 #define MAX_RUNS 300
54 /* #define DEBUG */
55 
56 int
57 main (int argc, char *argv[])
58 {
59 	struct aiocb **iocb, *kq_iocb;
60 	char *file, pathname[sizeof(PATH_TEMPLATE)+1];
61 	struct kevent kq_returned;
62 	struct timespec ts;
63 	char buffer[32768];
64 	int max_queue_per_proc;
65 	size_t max_queue_per_proc_size;
66 #ifdef DEBUG
67 	int cancel, error;
68 #endif
69 	int failed = 0, fd, kq, pending, result, run;
70 	int tmp_file = 0;
71 	int i, j;
72 
73 	PLAIN_REQUIRE_KERNEL_MODULE("aio", 0);
74 	PLAIN_REQUIRE_UNSAFE_AIO(0);
75 
76 	max_queue_per_proc_size = sizeof(max_queue_per_proc);
77 	if (sysctlbyname("vfs.aio.max_aio_queue_per_proc",
78 	    &max_queue_per_proc, &max_queue_per_proc_size, NULL, 0) != 0)
79 		err(1, "sysctlbyname");
80 	iocb = calloc(max_queue_per_proc, sizeof(struct aiocb*));
81 	if (iocb == NULL)
82 		err(1, "calloc");
83 
84 	kq = kqueue();
85 	if (kq < 0) {
86 		perror("No kqeueue\n");
87 		exit(1);
88 	}
89 
90 	if (argc == 1) {
91 		strcpy(pathname, PATH_TEMPLATE);
92 		fd = mkstemp(pathname);
93 		file = pathname;
94 		tmp_file = 1;
95 	} else {
96 		file = argv[1];
97 		fd = open(file, O_RDWR|O_CREAT, 0666);
98 	}
99 	if (fd == -1)
100 		err(1, "Can't open %s\n", file);
101 
102 	for (run = 0; run < MAX_RUNS; run++){
103 #ifdef DEBUG
104 		printf("Run %d\n", run);
105 #endif
106 		for (i = 0; i < max_queue_per_proc; i++) {
107 			iocb[i] = (struct aiocb *)calloc(1,
108 			    sizeof(struct aiocb));
109 			if (iocb[i] == NULL)
110 				err(1, "calloc");
111 		}
112 
113 		pending = 0;
114 		for (i = 0; i < max_queue_per_proc; i++) {
115 			pending++;
116 			iocb[i]->aio_nbytes = sizeof(buffer);
117 			iocb[i]->aio_buf = buffer;
118 			iocb[i]->aio_fildes = fd;
119 			iocb[i]->aio_offset = iocb[i]->aio_nbytes * i * run;
120 
121 			iocb[i]->aio_sigevent.sigev_notify_kqueue = kq;
122 			iocb[i]->aio_sigevent.sigev_value.sival_ptr = iocb[i];
123 			iocb[i]->aio_sigevent.sigev_notify = SIGEV_KEVENT;
124 
125 			result = aio_write(iocb[i]);
126 			if (result != 0) {
127 				perror("aio_write");
128 				printf("Result %d iteration %d\n", result, i);
129 				exit(1);
130 			}
131 #ifdef DEBUG
132 			printf("WRITE %d is at %p\n", i, iocb[i]);
133 #endif
134 			result = rand();
135 			if (result < RAND_MAX/32) {
136 				if (result > RAND_MAX/64) {
137 					result = aio_cancel(fd, iocb[i]);
138 #ifdef DEBUG
139 					printf("Cancel %d %p result %d\n", i, iocb[i], result);
140 #endif
141 					if (result == AIO_CANCELED) {
142 						aio_return(iocb[i]);
143 						iocb[i] = NULL;
144 						pending--;
145 					}
146 				}
147 			}
148 		}
149 #ifdef DEBUG
150 		cancel = max_queue_per_proc - pending;
151 #endif
152 
153 		i = 0;
154 		while (pending) {
155 
156 			for (;;) {
157 
158 				bzero(&kq_returned, sizeof(kq_returned));
159 				ts.tv_sec = 0;
160 				ts.tv_nsec = 1;
161 				result = kevent(kq, NULL, 0,
162 						&kq_returned, 1, &ts);
163 #ifdef DEBUG
164 				error = errno;
165 #endif
166 				if (result < 0)
167 					perror("kevent error: ");
168 				kq_iocb = kq_returned.udata;
169 #ifdef DEBUG
170 				printf("kevent %d %d errno %d return.ident %p "
171 				       "return.data %p return.udata %p %p"
172 				       " filter %d flags %#x fflags %#x\n",
173 				       i, result, error,
174 				       (void*)kq_returned.ident,
175 				       (void*)kq_returned.data,
176 				       kq_returned.udata,
177 				       kq_iocb,
178 				       kq_returned.filter,
179 				       kq_returned.flags,
180 				       kq_returned.fflags);
181 				if (result > 0)
182 					printf("\tsigev_notify_kevent_flags %#x\n",
183 				       ((struct aiocb*)(kq_returned.ident))->aio_sigevent.sigev_notify_kevent_flags);
184 #endif
185 
186 				if (kq_iocb)
187 					break;
188 #ifdef DEBUG
189 				printf("Try again left %d out of %d %d\n",
190 				    pending, max_queue_per_proc, cancel);
191 #endif
192 			}
193 
194 			for (j = 0; j < max_queue_per_proc && iocb[j] != kq_iocb;
195 			   j++) ;
196 			assert(j < max_queue_per_proc);
197 #ifdef DEBUG
198 			printf("kq_iocb %p\n", kq_iocb);
199 
200 			printf("Error Result for %d is %d pending %d\n",
201 			    j, result, pending);
202 #endif
203 			result = aio_return(kq_iocb);
204 #ifdef DEBUG
205 			printf("Return Result for %d is %d\n\n", j, result);
206 #endif
207 			if (result != sizeof(buffer)) {
208 				printf("FAIL: run %d, operation %d, result %d "
209 				    " (errno=%d) should be %zu\n", run, pending,
210 				    result, errno, sizeof(buffer));
211 				failed++;
212 			} else
213 				printf("PASS: run %d, left %d\n", run,
214 				    pending - 1);
215 
216 			free(kq_iocb);
217 			iocb[j] = NULL;
218 			pending--;
219 			i++;
220 		}
221 
222 		for (i = 0; i < max_queue_per_proc; i++)
223 			free(iocb[i]);
224 
225 	}
226 
227 	if (tmp_file)
228 		unlink(pathname);
229 
230 	if (failed != 0)
231 		printf("FAIL: %d tests failed\n", failed);
232 	else
233 		printf("PASS: All tests passed\n");
234 
235 	exit (failed == 0 ? 0 : 1);
236 }
237