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