]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/aio/lio_kqueue_test.c
Fix Coverity warnings about mkstemp in tests
[FreeBSD/FreeBSD.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_test /dev/<something safe>)
31  */
32
33 #include <sys/types.h>
34 #include <sys/event.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <aio.h>
38 #include <fcntl.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45
46 #include "freebsd_test_suite/macros.h"
47 #include "local.h"
48
49 #define PATH_TEMPLATE   "aio.XXXXXXXXXX"
50
51 #define LIO_MAX 5
52 #define MAX_IOCBS_PER_LIO       64
53 #define MAX_IOCBS (LIO_MAX * MAX_IOCBS_PER_LIO)
54 #define MAX_RUNS 300
55
56 int
57 main(int argc, char *argv[])
58 {
59         int fd;
60         struct aiocb *iocb[MAX_IOCBS];
61         struct aiocb **lio[LIO_MAX], **kq_lio;
62         int i, result, run, error, j, k, max_queue_per_proc;
63         int max_iocbs, iocbs_per_lio;
64         size_t max_queue_per_proc_size;
65         char buffer[32768];
66         int kq;
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)];
72         int tmp_file = 0, failed = 0;
73
74         PLAIN_REQUIRE_KERNEL_MODULE("aio", 0);
75         PLAIN_REQUIRE_UNSAFE_AIO(0);
76
77         max_queue_per_proc_size = sizeof(max_queue_per_proc);
78         if (sysctlbyname("vfs.aio.max_aio_queue_per_proc",
79             &max_queue_per_proc, &max_queue_per_proc_size, NULL, 0) != 0)
80                 err(1, "sysctlbyname");
81         iocbs_per_lio = max_queue_per_proc / LIO_MAX;
82         max_iocbs = LIO_MAX * iocbs_per_lio;
83
84         kq = kqueue();
85         if (kq < 0)
86                 err(1, "kqeueue(2) failed");
87
88         if (argc == 1) {
89                 strcpy(pathname, PATH_TEMPLATE);
90                 umask(0077);
91                 fd = mkstemp(pathname);
92                 file = pathname;
93                 tmp_file = 1;
94         } else {
95                 file = argv[1];
96                 fd = open(file, O_RDWR|O_CREAT, 0666);
97         }
98         if (fd < 0)
99                 err(1, "can't open %s", argv[1]);
100
101 #ifdef DEBUG
102         printf("Hello kq %d fd %d\n", kq, fd);
103 #endif
104
105         for (run = 0; run < MAX_RUNS; run++) {
106 #ifdef DEBUG
107                 printf("Run %d\n", run);
108 #endif
109                 for (j = 0; j < LIO_MAX; j++) {
110                         lio[j] =
111                             malloc(sizeof(struct aiocb *) * iocbs_per_lio);
112                         for (i = 0; i < iocbs_per_lio; i++) {
113                                 k = (iocbs_per_lio * j) + i;
114                                 lio[j][i] = iocb[k] =
115                                     calloc(1, sizeof(struct aiocb));
116                                 iocb[k]->aio_nbytes = sizeof(buffer);
117                                 iocb[k]->aio_buf = buffer;
118                                 iocb[k]->aio_fildes = fd;
119                                 iocb[k]->aio_offset
120                                     = iocb[k]->aio_nbytes * k * (run + 1);
121
122 #ifdef DEBUG
123                                 printf("hello iocb[k] %jd\n",
124                                        (intmax_t)iocb[k]->aio_offset);
125 #endif
126                                 iocb[k]->aio_lio_opcode = LIO_WRITE;
127                         }
128                         sig.sigev_notify_kqueue = kq;
129                         sig.sigev_value.sival_ptr = lio[j];
130                         sig.sigev_notify = SIGEV_KEVENT;
131                         time(&time1);
132                         result = lio_listio(LIO_NOWAIT, lio[j],
133                                             iocbs_per_lio, &sig);
134                         error = errno;
135                         time(&time2);
136 #ifdef DEBUG
137                         printf("Time %jd %jd %jd result -> %d\n",
138                             (intmax_t)time1, (intmax_t)time2,
139                             (intmax_t)time2-time1, result);
140 #endif
141                         if (result != 0) {
142                                 errno = error;
143                                 err(1, "FAIL: Result %d iteration %d\n",
144                                     result, j);
145                         }
146 #ifdef DEBUG
147                         printf("write %d is at %p\n", j, lio[j]);
148 #endif
149                 }
150
151                 for (i = 0; i < LIO_MAX; i++) {
152                         for (j = LIO_MAX - 1; j >=0; j--) {
153                                 if (lio[j])
154                                         break;
155                         }
156
157                         for (;;) {
158                                 bzero(&ke, sizeof(ke));
159                                 bzero(&kq_returned, sizeof(ke));
160                                 ts.tv_sec = 0;
161                                 ts.tv_nsec = 1;
162 #ifdef DEBUG
163                                 printf("FOO lio %d -> %p\n", j, lio[j]);
164 #endif
165                                 EV_SET(&ke, (uintptr_t)lio[j],
166                                        EVFILT_LIO, EV_ONESHOT, 0, 0, iocb[j]);
167                                 result = kevent(kq, NULL, 0,
168                                                 &kq_returned, 1, &ts);
169                                 error = errno;
170                                 if (result < 0) {
171                                         perror("kevent error: ");
172                                 }
173                                 kq_lio = kq_returned.udata;
174 #ifdef DEBUG
175                                 printf("kevent %d %d errno %d return.ident %p "
176                                        "return.data %p return.udata %p %p\n",
177                                        i, result, error,
178                                        (void*)kq_returned.ident,
179                                        (void*)kq_returned.data,
180                                        kq_returned.udata,
181                                        lio[j]);
182 #endif
183
184                                 if (kq_lio)
185                                         break;
186 #ifdef DEBUG
187                                 printf("Try again\n");
188 #endif
189                         }
190
191 #ifdef DEBUG
192                         printf("lio %p\n", lio);
193 #endif
194
195                         for (j = 0; j < LIO_MAX; j++) {
196                                 if (lio[j] == kq_lio)
197                                         break;
198                         }
199                         if (j == LIO_MAX)
200                                 errx(1, "FAIL: ");
201
202 #ifdef DEBUG
203                         printf("Error Result for %d is %d\n", j, result);
204 #endif
205                         if (result < 0) {
206                                 printf("FAIL: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result);
207                                 failed++;
208                         } else
209                                 printf("PASS: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result);
210                         for (k = 0; k < max_iocbs / LIO_MAX; k++) {
211                                 result = aio_return(kq_lio[k]);
212 #ifdef DEBUG
213                                 printf("Return Result 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                         free(lio[j]);
230                         lio[j] = NULL;
231                 }
232         }
233 #ifdef DEBUG
234         printf("Done\n");
235 #endif
236
237         if (tmp_file)
238                 unlink(pathname);
239
240         if (failed)
241                 errx(1, "FAIL: %d testcases failed", failed);
242         else
243                 errx(0, "PASS: All\n");
244
245 }