]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - tests/sys/aio/lio_kqueue_test.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.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 <errno.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "freebsd_test_suite/macros.h"
52
53 #define PATH_TEMPLATE   "aio.XXXXXXXXXX"
54
55 #define LIO_MAX 5
56 #define MAX_IOCBS LIO_MAX * 16
57 #define MAX_RUNS 300
58
59 int
60 main(int argc, char *argv[]){
61         int fd;
62         struct aiocb *iocb[MAX_IOCBS];
63         struct aiocb **lio[LIO_MAX], **lio_element, **kq_lio;
64         int i, result, run, error, j, k;
65         char buffer[32768];
66         int kq = kqueue();
67         struct kevent ke, kq_returned;
68         struct timespec ts;
69         struct sigevent sig;
70         time_t time1, time2;
71         char *file, pathname[sizeof(PATH_TEMPLATE)-1];
72         int tmp_file = 0, failed = 0;
73
74         PLAIN_REQUIRE_KERNEL_MODULE("aio", 0);
75
76         if (kq < 0) {
77                 perror("No kqeueue\n");
78                 exit(1);
79         }
80
81         if (argc == 1) {
82                 strcpy(pathname, PATH_TEMPLATE);
83                 fd = mkstemp(pathname);
84                 file = pathname;
85                 tmp_file = 1;
86         } else {
87                 file = argv[1];
88                 fd = open(file, O_RDWR|O_CREAT, 0666);
89         }
90         if (fd < 0){
91                 fprintf(stderr, "Can't open %s\n", argv[1]);
92                 perror("");
93                 exit(1);
94         }
95
96 #ifdef DEBUG
97         printf("Hello kq %d fd %d\n", kq, fd);
98 #endif
99
100         for (run = 0; run < MAX_RUNS; run++){
101 #ifdef DEBUG
102                 printf("Run %d\n", run);
103 #endif
104                 for (j = 0; j < LIO_MAX; j++) {
105                         lio[j] = (struct aiocb **)
106                             malloc(sizeof(struct aiocb *) * MAX_IOCBS/LIO_MAX);
107                         for(i = 0; i < MAX_IOCBS / LIO_MAX; i++) {
108                                 k = (MAX_IOCBS / LIO_MAX * j) + i;
109                                 lio_element = lio[j];
110                                 lio[j][i] = iocb[k] = (struct aiocb *)
111                                         malloc(sizeof(struct aiocb));
112                                 bzero(iocb[k], sizeof(struct aiocb));
113                                 iocb[k]->aio_nbytes = sizeof(buffer);
114                                 iocb[k]->aio_buf = buffer;
115                                 iocb[k]->aio_fildes = fd;
116                                 iocb[k]->aio_offset 
117                                   = iocb[k]->aio_nbytes * k * (run + 1);
118
119 #ifdef DEBUG
120                                 printf("hello iocb[k] %d\n",
121                                        iocb[k]->aio_offset);
122 #endif
123                                 iocb[k]->aio_lio_opcode = LIO_WRITE;
124                         }
125                         sig.sigev_notify_kqueue = kq;
126                         sig.sigev_value.sival_ptr = lio[j];
127                         sig.sigev_notify = SIGEV_KEVENT;
128                         time(&time1);
129                         result = lio_listio(LIO_NOWAIT, lio[j],
130                                             MAX_IOCBS / LIO_MAX, &sig);
131                         error = errno;
132                         time(&time2);
133 #ifdef DEBUG
134                         printf("Time %d %d %d result -> %d\n", 
135                             time1, time2, time2-time1, result);
136 #endif
137                         if (result != 0) {
138                                 errno = error;
139                                 perror("list_listio");
140                                 printf("FAIL: Result %d iteration %d\n",result, j);
141                                 exit(1);
142                         }
143 #ifdef DEBUG
144                         printf("write %d is at %p\n", j, lio[j]);
145 #endif
146                 }
147
148                 for(i = 0; i < LIO_MAX; i++) {
149                         for(j = LIO_MAX - 1; j >=0; j--) {
150                                 if (lio[j])
151                                         break;
152                         }
153
154                         for(;;) {
155                                 bzero(&ke, sizeof(ke));
156                                 bzero(&kq_returned, sizeof(ke));
157                                 ts.tv_sec = 0;
158                                 ts.tv_nsec = 1;
159 #ifdef DEBUG
160                                 printf("FOO lio %d -> %p\n", j, lio[j]);
161 #endif
162                                 EV_SET(&ke, (uintptr_t)lio[j], 
163                                        EVFILT_LIO, EV_ONESHOT, 0, 0, iocb[j]);
164                                 result = kevent(kq, NULL, 0, 
165                                                 &kq_returned, 1, &ts);
166                                 error = errno;
167                                 if (result < 0) {
168                                         perror("kevent error: ");
169                                 }
170                                 kq_lio = kq_returned.udata;
171 #ifdef DEBUG
172                                 printf("kevent %d %d errno %d return.ident %p "
173                                        "return.data %p return.udata %p %p\n", 
174                                        i, result, error, 
175                                        kq_returned.ident, kq_returned.data, 
176                                        kq_returned.udata, 
177                                        lio[j]);
178 #endif
179
180                                 if(kq_lio)
181                                         break;
182 #ifdef DEBUG
183                                 printf("Try again\n");
184 #endif
185                         }
186
187 #ifdef DEBUG
188                         printf("lio %p\n", lio);
189 #endif
190
191                         for (j = 0; j < LIO_MAX; j++) {
192                                 if (lio[j] == kq_lio) {
193                                         break;
194                                 }
195                         }
196                         if (j == LIO_MAX) {
197                           printf("FAIL:\n");
198                           exit(1);
199                         }
200
201 #ifdef DEBUG
202                         printf("Error Result for %d is %d\n", j, result);
203 #endif
204                         if (result < 0) {
205                                 printf("FAIL: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result);
206                                 failed = 1;
207                         } else {
208                                 printf("PASS: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result);
209                         }
210                         for(k = 0; k < MAX_IOCBS / LIO_MAX; k++){
211                                 result = aio_return(kq_lio[k]);
212 #ifdef DEBUG
213                                 printf("Return Resulto for %d %d is %d\n", j, k, result);
214 #endif
215                                 if (result != sizeof(buffer)) {
216                                         printf("FAIL: run %d, operation %d sub-opt %d  result %d (errno=%d) should be %zu\n",
217                                            run, LIO_MAX - i -1, k, result, errno, sizeof(buffer));
218                                 } else {
219                                         printf("PASS: run %d, operation %d sub-opt %d  result %d\n",
220                                            run, LIO_MAX - i -1, k, result);
221                                 }
222                         }
223 #ifdef DEBUG
224                         printf("\n");
225 #endif
226
227                         for(k = 0; k < MAX_IOCBS / LIO_MAX; k++) {
228                                 free(lio[j][k]);
229                         }
230                         free(lio[j]);
231                         lio[j] = NULL;
232                 }
233         }
234 #ifdef DEBUG
235         printf("Done\n");
236 #endif
237
238         if (tmp_file) {
239                 unlink(pathname);
240         }
241
242         if (failed) {
243                 printf("FAIL: Atleast one\n");
244                 exit(1);
245         } else {
246                 printf("PASS: All\n");
247                 exit(0);
248         }
249 }