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