2 * Copyright (C) 2005 IronPort Systems, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
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>)
33 #include <sys/types.h>
34 #include <sys/event.h>
46 #include "freebsd_test_suite/macros.h"
49 #define PATH_TEMPLATE "aio.XXXXXXXXXX"
52 #define MAX_IOCBS_PER_LIO 64
53 #define MAX_IOCBS (LIO_MAX * MAX_IOCBS_PER_LIO)
57 main(int argc, char *argv[])
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;
67 struct kevent ke, kq_returned;
71 char *file, pathname[sizeof(PATH_TEMPLATE)];
72 int tmp_file = 0, failed = 0;
74 PLAIN_REQUIRE_KERNEL_MODULE("aio", 0);
75 PLAIN_REQUIRE_UNSAFE_AIO(0);
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;
86 err(1, "kqeueue(2) failed");
89 strcpy(pathname, PATH_TEMPLATE);
91 fd = mkstemp(pathname);
96 fd = open(file, O_RDWR|O_CREAT, 0666);
99 err(1, "can't open %s", argv[1]);
102 printf("Hello kq %d fd %d\n", kq, fd);
105 for (run = 0; run < MAX_RUNS; run++) {
107 printf("Run %d\n", run);
109 for (j = 0; j < LIO_MAX; 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;
120 = iocb[k]->aio_nbytes * k * (run + 1);
123 printf("hello iocb[k] %jd\n",
124 (intmax_t)iocb[k]->aio_offset);
126 iocb[k]->aio_lio_opcode = LIO_WRITE;
128 sig.sigev_notify_kqueue = kq;
129 sig.sigev_value.sival_ptr = lio[j];
130 sig.sigev_notify = SIGEV_KEVENT;
132 result = lio_listio(LIO_NOWAIT, lio[j],
133 iocbs_per_lio, &sig);
137 printf("Time %jd %jd %jd result -> %d\n",
138 (intmax_t)time1, (intmax_t)time2,
139 (intmax_t)time2-time1, result);
143 err(1, "FAIL: Result %d iteration %d\n",
147 printf("write %d is at %p\n", j, lio[j]);
151 for (i = 0; i < LIO_MAX; i++) {
152 for (j = LIO_MAX - 1; j >=0; j--) {
158 bzero(&ke, sizeof(ke));
159 bzero(&kq_returned, sizeof(ke));
163 printf("FOO lio %d -> %p\n", j, lio[j]);
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);
171 perror("kevent error: ");
173 kq_lio = kq_returned.udata;
175 printf("kevent %d %d errno %d return.ident %p "
176 "return.data %p return.udata %p %p\n",
178 (void*)kq_returned.ident,
179 (void*)kq_returned.data,
187 printf("Try again\n");
192 printf("lio %p\n", lio);
195 for (j = 0; j < LIO_MAX; j++) {
196 if (lio[j] == kq_lio)
203 printf("Error Result for %d is %d\n", j, result);
206 printf("FAIL: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result);
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]);
213 printf("Return Result for %d %d is %d\n", j, k, result);
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));
219 printf("PASS: run %d, operation %d sub-opt %d result %d\n",
220 run, LIO_MAX - i -1, k, result);
227 for (k = 0; k < max_iocbs / LIO_MAX; k++)
241 errx(1, "FAIL: %d testcases failed", failed);
243 errx(0, "PASS: All\n");