]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - tests/sys/aio/lio_kqueue_test.c
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.git] / tests / sys / aio / lio_kqueue_test.c
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  * Note: it is a good idea to run this against a physical drive to
30  * exercise the physio fast path (ie. lio_kqueue /dev/<something safe>)
31  * This will ensure op's counting is correct.  It is currently broken.
32  *
33  * Also note that LIO & kqueue is not implemented in FreeBSD yet, LIO
34  * is also broken with respect to op's and some paths.
35  *
36  * A patch to make this work is at:
37  *      http://www.ambrisko.com/doug/listio_kqueue/listio_kqueue.patch
38  */
39
40 #include <sys/types.h>
41 #include <sys/event.h>
42 #include <sys/time.h>
43 #include <aio.h>
44 #include <fcntl.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include "freebsd_test_suite/macros.h"
53
54 #define PATH_TEMPLATE   "aio.XXXXXXXXXX"
55
56 #define LIO_MAX 5
57 #define MAX_IOCBS LIO_MAX * 16
58 #define MAX_RUNS 300
59
60 int
61 main(int argc, char *argv[])
62 {
63         int fd;
64         struct aiocb *iocb[MAX_IOCBS];
65         struct aiocb **lio[LIO_MAX], **kq_lio;
66         int i, result, run, error, j, k;
67         char buffer[32768];
68         int kq;
69         struct kevent ke, kq_returned;
70         struct timespec ts;
71         struct sigevent sig;
72         time_t time1, time2;
73         char *file, pathname[sizeof(PATH_TEMPLATE)];
74         int tmp_file = 0, failed = 0;
75
76         PLAIN_REQUIRE_KERNEL_MODULE("aio", 0);
77
78         kq = kqueue();
79         if (kq < 0)
80                 err(1, "kqeueue(2) failed");
81
82         if (argc == 1) {
83                 strcpy(pathname, PATH_TEMPLATE);
84                 fd = mkstemp(pathname);
85                 file = pathname;
86                 tmp_file = 1;
87         } else {
88                 file = argv[1];
89                 fd = open(file, O_RDWR|O_CREAT, 0666);
90         }
91         if (fd < 0)
92                 err(1, "can't open %s", argv[1]);
93
94 #ifdef DEBUG
95         printf("Hello kq %d fd %d\n", kq, fd);
96 #endif
97
98         for (run = 0; run < MAX_RUNS; run++) {
99 #ifdef DEBUG
100                 printf("Run %d\n", run);
101 #endif
102                 for (j = 0; j < LIO_MAX; j++) {
103                         lio[j] =
104                             malloc(sizeof(struct aiocb *) * MAX_IOCBS/LIO_MAX);
105                         for (i = 0; i < MAX_IOCBS / LIO_MAX; i++) {
106                                 k = (MAX_IOCBS / LIO_MAX * j) + i;
107                                 lio[j][i] = iocb[k] =
108                                     calloc(1, sizeof(struct aiocb));
109                                 iocb[k]->aio_nbytes = sizeof(buffer);
110                                 iocb[k]->aio_buf = buffer;
111                                 iocb[k]->aio_fildes = fd;
112                                 iocb[k]->aio_offset
113                                     = iocb[k]->aio_nbytes * k * (run + 1);
114
115 #ifdef DEBUG
116                                 printf("hello iocb[k] %d\n",
117                                        iocb[k]->aio_offset);
118 #endif
119                                 iocb[k]->aio_lio_opcode = LIO_WRITE;
120                         }
121                         sig.sigev_notify_kqueue = kq;
122                         sig.sigev_value.sival_ptr = lio[j];
123                         sig.sigev_notify = SIGEV_KEVENT;
124                         time(&time1);
125                         result = lio_listio(LIO_NOWAIT, lio[j],
126                                             MAX_IOCBS / LIO_MAX, &sig);
127                         error = errno;
128                         time(&time2);
129 #ifdef DEBUG
130                         printf("Time %d %d %d result -> %d\n",
131                             time1, time2, time2-time1, result);
132 #endif
133                         if (result != 0) {
134                                 errno = error;
135                                 err(1, "FAIL: Result %d iteration %d\n",
136                                     result, j);
137                         }
138 #ifdef DEBUG
139                         printf("write %d is at %p\n", j, lio[j]);
140 #endif
141                 }
142
143                 for (i = 0; i < LIO_MAX; i++) {
144                         for (j = LIO_MAX - 1; j >=0; j--) {
145                                 if (lio[j])
146                                         break;
147                         }
148
149                         for (;;) {
150                                 bzero(&ke, sizeof(ke));
151                                 bzero(&kq_returned, sizeof(ke));
152                                 ts.tv_sec = 0;
153                                 ts.tv_nsec = 1;
154 #ifdef DEBUG
155                                 printf("FOO lio %d -> %p\n", j, lio[j]);
156 #endif
157                                 EV_SET(&ke, (uintptr_t)lio[j],
158                                        EVFILT_LIO, EV_ONESHOT, 0, 0, iocb[j]);
159                                 result = kevent(kq, NULL, 0,
160                                                 &kq_returned, 1, &ts);
161                                 error = errno;
162                                 if (result < 0) {
163                                         perror("kevent error: ");
164                                 }
165                                 kq_lio = kq_returned.udata;
166 #ifdef DEBUG
167                                 printf("kevent %d %d errno %d return.ident %p "
168                                        "return.data %p return.udata %p %p\n",
169                                        i, result, error,
170                                        kq_returned.ident, 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 Resulto 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 }