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/time.h>
40 #include <aio.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 #include "freebsd_test_suite/macros.h"
50
51 #define PATH_TEMPLATE "aio.XXXXXXXXXX"
52
53 #define MAX_IOCBS 128
54 #define MAX_RUNS 300
55 /* #define DEBUG */
56
57 int
main(int argc,char * argv[])58 main (int argc, char *argv[])
59 {
60 struct aiocb *iocb[MAX_IOCBS], *kq_iocb;
61 char *file, pathname[sizeof(PATH_TEMPLATE)+1];
62 struct kevent ke, kq_returned;
63 struct timespec ts;
64 char buffer[32768];
65 #ifdef DEBUG
66 int cancel, error;
67 #endif
68 int failed = 0, fd, kq, pending, result, run;
69 int tmp_file = 0;
70 unsigned i, j;
71
72 PLAIN_REQUIRE_KERNEL_MODULE("aio", 0);
73
74 kq = kqueue();
75 if (kq < 0) {
76 perror("No kqeueue\n");
77 exit(1);
78 }
79
80 if (argc == 1) {
81 strcpy(pathname, PATH_TEMPLATE);
82 fd = mkstemp(pathname);
83 file = pathname;
84 tmp_file = 1;
85 } else {
86 file = argv[1];
87 fd = open(file, O_RDWR|O_CREAT, 0666);
88 }
89 if (fd == -1)
90 err(1, "Can't open %s\n", file);
91
92 for (run = 0; run < MAX_RUNS; run++){
93 #ifdef DEBUG
94 printf("Run %d\n", run);
95 #endif
96 for (i = 0; i < nitems(iocb); i++) {
97 iocb[i] = (struct aiocb *)calloc(1,
98 sizeof(struct aiocb));
99 if (iocb[i] == NULL)
100 err(1, "calloc");
101 }
102
103 pending = 0;
104 for (i = 0; i < nitems(iocb); i++) {
105 pending++;
106 iocb[i]->aio_nbytes = sizeof(buffer);
107 iocb[i]->aio_buf = buffer;
108 iocb[i]->aio_fildes = fd;
109 iocb[i]->aio_offset = iocb[i]->aio_nbytes * i * run;
110
111 iocb[i]->aio_sigevent.sigev_notify_kqueue = kq;
112 iocb[i]->aio_sigevent.sigev_value.sival_ptr = iocb[i];
113 iocb[i]->aio_sigevent.sigev_notify = SIGEV_KEVENT;
114
115 result = aio_write(iocb[i]);
116 if (result != 0) {
117 perror("aio_write");
118 printf("Result %d iteration %d\n", result, i);
119 exit(1);
120 }
121 #ifdef DEBUG
122 printf("WRITE %d is at %p\n", i, iocb[i]);
123 #endif
124 result = rand();
125 if (result < RAND_MAX/32) {
126 if (result > RAND_MAX/64) {
127 result = aio_cancel(fd, iocb[i]);
128 #ifdef DEBUG
129 printf("Cancel %d %p result %d\n", i, iocb[i], result);
130 #endif
131 if (result == AIO_CANCELED) {
132 aio_return(iocb[i]);
133 iocb[i] = NULL;
134 pending--;
135 }
136 }
137 }
138 }
139 #ifdef DEBUG
140 cancel = nitems(iocb) - pending;
141 #endif
142
143 i = 0;
144 while (pending) {
145
146 for (;;) {
147
148 bzero(&ke, sizeof(ke));
149 bzero(&kq_returned, sizeof(ke));
150 ts.tv_sec = 0;
151 ts.tv_nsec = 1;
152 result = kevent(kq, NULL, 0,
153 &kq_returned, 1, &ts);
154 #ifdef DEBUG
155 error = errno;
156 #endif
157 if (result < 0)
158 perror("kevent error: ");
159 kq_iocb = kq_returned.udata;
160 #ifdef DEBUG
161 printf("kevent %d %d errno %d return.ident %p "
162 "return.data %p return.udata %p %p\n",
163 i, result, error,
164 kq_returned.ident, kq_returned.data,
165 kq_returned.udata,
166 kq_iocb);
167 #endif
168
169 if (kq_iocb)
170 break;
171 #ifdef DEBUG
172 printf("Try again left %d out of %d %d\n",
173 pending, nitems(iocb), cancel);
174 #endif
175 }
176
177 for (j = 0; j < nitems(iocb) && iocb[j] != kq_iocb;
178 j++) ;
179 #ifdef DEBUG
180 printf("kq_iocb %p\n", kq_iocb);
181
182 printf("Error Result for %d is %d pending %d\n",
183 j, result, pending);
184 #endif
185 result = aio_return(kq_iocb);
186 #ifdef DEBUG
187 printf("Return Result for %d is %d\n\n", j, result);
188 #endif
189 if (result != sizeof(buffer)) {
190 printf("FAIL: run %d, operation %d, result %d "
191 " (errno=%d) should be %zu\n", run, pending,
192 result, errno, sizeof(buffer));
193 failed++;
194 } else
195 printf("PASS: run %d, left %d\n", run,
196 pending - 1);
197
198 free(kq_iocb);
199 iocb[j] = NULL;
200 pending--;
201 i++;
202 }
203
204 for (i = 0; i < nitems(iocb); i++)
205 free(iocb[i]);
206
207 }
208
209 if (tmp_file)
210 unlink(pathname);
211
212 if (failed != 0)
213 printf("FAIL: %d tests failed\n", failed);
214 else
215 printf("PASS: All tests passed\n");
216
217 exit (failed == 0 ? 0 : 1);
218 }
219