]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - tools/regression/aio/kqueue/lio/lio_kqueue.c
MFC r280894,r280895:
[FreeBSD/stable/10.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 <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 #define PATH_TEMPLATE   "/tmp/aio.XXXXXXXXXX"
52
53 #define LIO_MAX 5
54 #define MAX LIO_MAX * 16
55 #define MAX_RUNS 300
56
57 int
58 main(int argc, char *argv[]){
59         int fd;
60         struct aiocb *iocb[MAX];
61         struct aiocb **lio[LIO_MAX], **lio_element, **kq_lio;
62         int i, result, run, error, j, k;
63         char buffer[32768];
64         int kq = kqueue();
65         struct kevent ke, kq_returned;
66         struct timespec ts;
67         struct sigevent sig;
68         time_t time1, time2;
69         char *file, pathname[sizeof(PATH_TEMPLATE)-1];
70         int tmp_file = 0, failed = 0;
71
72         if (kq < 0) {
73                 perror("No kqeueue\n");
74                 exit(1);
75         }
76
77         if (argc == 1) {
78                 strcpy(pathname, PATH_TEMPLATE);
79                 fd = mkstemp(pathname);
80                 file = pathname;
81                 tmp_file = 1;
82         } else {
83                 file = argv[1];
84                 fd = open(file, O_RDWR|O_CREAT, 0666);
85         }
86         if (fd < 0){
87                 fprintf(stderr, "Can't open %s\n", argv[1]);
88                 perror("");
89                 exit(1);
90         }
91
92 #ifdef DEBUG
93         printf("Hello kq %d fd %d\n", kq, fd);
94 #endif
95
96         for (run = 0; run < MAX_RUNS; run++){
97 #ifdef DEBUG
98                 printf("Run %d\n", run);
99 #endif
100                 for (j = 0; j < LIO_MAX; j++) {
101                         lio[j] = (struct aiocb **)
102                                 malloc(sizeof(struct aiocb *) * MAX/LIO_MAX);
103                         for(i = 0; i < MAX / LIO_MAX; i++) {
104                                 k = (MAX / LIO_MAX * j) + i;
105                                 lio_element = lio[j];
106                                 lio[j][i] = iocb[k] = (struct aiocb *)
107                                         malloc(sizeof(struct aiocb));
108                                 bzero(iocb[k], 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 / 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                                 perror("list_listio");
136                                 printf("FAIL: Result %d iteration %d\n",result, j);
137                                 exit(1);
138                         }
139 #ifdef DEBUG
140                         printf("write %d is at %p\n", j, lio[j]);
141 #endif
142                 }
143
144                 for(i = 0; i < LIO_MAX; i++) {
145                         for(j = LIO_MAX - 1; j >=0; j--) {
146                                 if (lio[j])
147                                         break;
148                         }
149
150                         for(;;) {
151                                 bzero(&ke, sizeof(ke));
152                                 bzero(&kq_returned, sizeof(ke));
153                                 ts.tv_sec = 0;
154                                 ts.tv_nsec = 1;
155 #ifdef DEBUG
156                                 printf("FOO lio %d -> %p\n", j, lio[j]);
157 #endif
158                                 EV_SET(&ke, (uintptr_t)lio[j], 
159                                        EVFILT_LIO, EV_ONESHOT, 0, 0, iocb[j]);
160                                 result = kevent(kq, NULL, 0, 
161                                                 &kq_returned, 1, &ts);
162                                 error = errno;
163                                 if (result < 0) {
164                                         perror("kevent error: ");
165                                 }
166                                 kq_lio = kq_returned.udata;
167 #ifdef DEBUG
168                                 printf("kevent %d %d errno %d return.ident %p "
169                                        "return.data %p return.udata %p %p\n", 
170                                        i, result, error, 
171                                        kq_returned.ident, kq_returned.data, 
172                                        kq_returned.udata, 
173                                        lio[j]);
174 #endif
175
176                                 if(kq_lio)
177                                         break;
178 #ifdef DEBUG
179                                 printf("Try again\n");
180 #endif
181                         }
182
183 #ifdef DEBUG
184                         printf("lio %p\n", lio);
185 #endif
186
187                         for (j = 0; j < LIO_MAX; j++) {
188                                 if (lio[j] == kq_lio) {
189                                         break;
190                                 }
191                         }
192                         if (j == LIO_MAX) {
193                           printf("FAIL:\n");
194                           exit(1);
195                         }
196
197 #ifdef DEBUG
198                         printf("Error Result for %d is %d\n", j, result);
199 #endif
200                         if (result < 0) {
201                                 printf("FAIL: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result);
202                                 failed = 1;
203                         } else {
204                                 printf("PASS: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result);
205                         }
206                         for(k = 0; k < MAX / LIO_MAX; k++){
207                                 result = aio_return(kq_lio[k]);
208 #ifdef DEBUG
209                                 printf("Return Resulto for %d %d is %d\n", j, k, result);
210 #endif
211                                 if (result != sizeof(buffer)) {
212                                         printf("FAIL: run %d, operation %d sub-opt %d  result %d (errno=%d) should be %zu\n",
213                                            run, LIO_MAX - i -1, k, result, errno, sizeof(buffer));
214                                 } else {
215                                         printf("PASS: run %d, operation %d sub-opt %d  result %d\n",
216                                            run, LIO_MAX - i -1, k, result);
217                                 }
218                         }
219 #ifdef DEBUG
220                         printf("\n");
221 #endif
222
223                         for(k = 0; k < MAX / LIO_MAX; k++) {
224                                 free(lio[j][k]);
225                         }
226                         free(lio[j]);
227                         lio[j] = NULL;
228                 }
229         }
230 #ifdef DEBUG
231         printf("Done\n");
232 #endif
233
234         if (tmp_file) {
235                 unlink(pathname);
236         }
237
238         if (failed) {
239                 printf("FAIL: Atleast one\n");
240                 exit(1);
241         } else {
242                 printf("PASS: All\n");
243                 exit(0);
244         }
245 }