xref: /freebsd/tests/sys/aio/lio_kqueue_test.c (revision 26d3dfa8733892adf107b045a8c98d480b4bb21d)
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  * Note: it is a good idea to run this against a physical drive to
28  * exercise the physio fast path (ie. lio_kqueue_test /dev/<something safe>)
29  */
30 
31 #include <sys/types.h>
32 #include <sys/event.h>
33 #include <sys/time.h>
34 #include <aio.h>
35 #include <fcntl.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "local.h"
44 
45 #define PATH_TEMPLATE   "aio.XXXXXXXXXX"
46 
47 #define LIO_MAX 5
48 #define MAX_IOCBS_PER_LIO	64
49 #define MAX_IOCBS (LIO_MAX * MAX_IOCBS_PER_LIO)
50 #define MAX_RUNS 300
51 
52 int
main(int argc,char * argv[])53 main(int argc, char *argv[])
54 {
55 	int fd;
56 	struct aiocb *iocb[MAX_IOCBS];
57 	struct aiocb **lio[LIO_MAX], **kq_lio;
58 	int i, result, run, error, j, k, max_queue_per_proc;
59 	int max_iocbs, iocbs_per_lio;
60 	size_t max_queue_per_proc_size;
61 	char buffer[32768];
62 	int kq;
63 	struct kevent kq_returned;
64 	struct timespec ts;
65 	struct sigevent sig;
66 	time_t time1, time2;
67 	char *file, pathname[sizeof(PATH_TEMPLATE)];
68 	int tmp_file = 0, failed = 0;
69 
70 	PLAIN_REQUIRE_UNSAFE_AIO(0);
71 
72 	max_queue_per_proc_size = sizeof(max_queue_per_proc);
73 	if (sysctlbyname("vfs.aio.max_aio_queue_per_proc",
74 	    &max_queue_per_proc, &max_queue_per_proc_size, NULL, 0) != 0)
75 		err(1, "sysctlbyname");
76 	iocbs_per_lio = max_queue_per_proc / LIO_MAX;
77 	max_iocbs = LIO_MAX * iocbs_per_lio;
78 
79 	kq = kqueue();
80 	if (kq < 0)
81 		err(1, "kqeueue(2) failed");
82 
83 	if (argc == 1) {
84 		strcpy(pathname, PATH_TEMPLATE);
85 		fd = mkstemp(pathname);
86 		file = pathname;
87 		tmp_file = 1;
88 	} else {
89 		file = argv[1];
90 		fd = open(file, O_RDWR|O_CREAT, 0666);
91         }
92 	if (fd < 0)
93 		err(1, "can't open %s", argv[1]);
94 
95 #ifdef DEBUG
96 	printf("Hello kq %d fd %d\n", kq, fd);
97 #endif
98 
99 	for (run = 0; run < MAX_RUNS; run++) {
100 #ifdef DEBUG
101 		printf("Run %d\n", run);
102 #endif
103 		for (j = 0; j < LIO_MAX; j++) {
104 			lio[j] =
105 			    malloc(sizeof(struct aiocb *) * iocbs_per_lio);
106 			for (i = 0; i < iocbs_per_lio; i++) {
107 				k = (iocbs_per_lio * j) + i;
108 				lio[j][i] = iocb[k] =
109 				    calloc(1, sizeof(struct aiocb));
110 				iocb[k]->aio_nbytes = sizeof(buffer);
111 				iocb[k]->aio_buf = buffer;
112 				iocb[k]->aio_fildes = fd;
113 				iocb[k]->aio_offset
114 				    = iocb[k]->aio_nbytes * k * (run + 1);
115 
116 #ifdef DEBUG
117 				printf("hello iocb[k] %jd\n",
118 				       (intmax_t)iocb[k]->aio_offset);
119 #endif
120 				iocb[k]->aio_lio_opcode = LIO_WRITE;
121 			}
122 			sig.sigev_notify_kqueue = kq;
123 			sig.sigev_value.sival_ptr = lio[j];
124 			sig.sigev_notify = SIGEV_KEVENT;
125 			time(&time1);
126 			result = lio_listio(LIO_NOWAIT, lio[j],
127 					    iocbs_per_lio, &sig);
128 			error = errno;
129 			time(&time2);
130 #ifdef DEBUG
131 			printf("Time %jd %jd %jd result -> %d\n",
132 			    (intmax_t)time1, (intmax_t)time2,
133 			    (intmax_t)time2-time1, result);
134 #endif
135 			if (result != 0) {
136 			        errno = error;
137 				err(1, "FAIL: Result %d iteration %d\n",
138 				    result, j);
139 			}
140 #ifdef DEBUG
141 			printf("write %d is at %p\n", j, lio[j]);
142 #endif
143 		}
144 
145 		for (i = 0; i < LIO_MAX; i++) {
146 			for (j = LIO_MAX - 1; j >=0; j--) {
147 				if (lio[j])
148 					break;
149 			}
150 
151 			for (;;) {
152 				bzero(&kq_returned, sizeof(kq_returned));
153 				ts.tv_sec = 0;
154 				ts.tv_nsec = 1;
155 #ifdef DEBUG
156 				printf("FOO lio %d -> %p\n", j, lio[j]);
157 #endif
158 				result = kevent(kq, NULL, 0,
159 						&kq_returned, 1, &ts);
160 				error = errno;
161 				if (result < 0) {
162 					perror("kevent error: ");
163 				}
164 				kq_lio = kq_returned.udata;
165 #ifdef DEBUG
166 				printf("kevent %d %d errno %d return.ident %p "
167 				       "return.data %p return.udata %p %p\n",
168 				       i, result, error,
169 				       (void*)kq_returned.ident,
170 				       (void*)kq_returned.data,
171 				       kq_returned.udata,
172 				       lio[j]);
173 #endif
174 
175 				if (kq_lio)
176 					break;
177 #ifdef DEBUG
178 				printf("Try again\n");
179 #endif
180 			}
181 
182 #ifdef DEBUG
183 			printf("lio %p\n", lio);
184 #endif
185 
186 			for (j = 0; j < LIO_MAX; j++) {
187 				if (lio[j] == kq_lio)
188 					break;
189 			}
190 			if (j == LIO_MAX)
191 				errx(1, "FAIL: ");
192 
193 #ifdef DEBUG
194 			printf("Error Result for %d is %d\n", j, result);
195 #endif
196 			if (result < 0) {
197 				printf("FAIL: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result);
198 				failed++;
199 			} else
200 				printf("PASS: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result);
201 			for (k = 0; k < max_iocbs / LIO_MAX; k++) {
202 				result = aio_return(kq_lio[k]);
203 #ifdef DEBUG
204 				printf("Return Result for %d %d is %d\n", j, k, result);
205 #endif
206 				if (result != sizeof(buffer)) {
207 					printf("FAIL: run %d, operation %d sub-opt %d  result %d (errno=%d) should be %zu\n",
208 					   run, LIO_MAX - i -1, k, result, errno, sizeof(buffer));
209 				} else {
210 					printf("PASS: run %d, operation %d sub-opt %d  result %d\n",
211 					   run, LIO_MAX - i -1, k, result);
212 				}
213 			}
214 #ifdef DEBUG
215 			printf("\n");
216 #endif
217 
218 			for (k = 0; k < max_iocbs / LIO_MAX; k++)
219 				free(lio[j][k]);
220 			free(lio[j]);
221 			lio[j] = NULL;
222 		}
223 	}
224 #ifdef DEBUG
225 	printf("Done\n");
226 #endif
227 
228 	if (tmp_file)
229 		unlink(pathname);
230 
231 	if (failed)
232 		errx(1, "FAIL: %d testcases failed", failed);
233 	else
234 		errx(0, "PASS: All\n");
235 
236 }
237