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
30 * - AIO support must be compiled into the kernel (see sys/<arch>/NOTES for
33 * Note: it is a good idea to run this against a physical drive to
34 * exercise the physio fast path (ie. aio_kqueue /dev/<something safe>)
37 #include <sys/types.h>
38 #include <sys/event.h>
49 #include "freebsd_test_suite/macros.h"
52 #define PATH_TEMPLATE "aio.XXXXXXXXXX"
59 main (int argc, char *argv[])
61 struct aiocb *iocb[MAX_IOCBS], *kq_iocb;
62 char *file, pathname[sizeof(PATH_TEMPLATE)+1];
63 struct kevent ke, kq_returned;
69 int failed = 0, fd, kq, pending, result, run;
73 PLAIN_REQUIRE_KERNEL_MODULE("aio", 0);
74 PLAIN_REQUIRE_UNSAFE_AIO(0);
78 perror("No kqeueue\n");
83 strcpy(pathname, PATH_TEMPLATE);
84 fd = mkstemp(pathname);
89 fd = open(file, O_RDWR|O_CREAT, 0666);
92 err(1, "Can't open %s\n", file);
94 for (run = 0; run < MAX_RUNS; run++){
96 printf("Run %d\n", run);
98 for (i = 0; i < nitems(iocb); i++) {
99 iocb[i] = (struct aiocb *)calloc(1,
100 sizeof(struct aiocb));
106 for (i = 0; i < nitems(iocb); i++) {
108 iocb[i]->aio_nbytes = sizeof(buffer);
109 iocb[i]->aio_buf = buffer;
110 iocb[i]->aio_fildes = fd;
111 iocb[i]->aio_offset = iocb[i]->aio_nbytes * i * run;
113 iocb[i]->aio_sigevent.sigev_notify_kqueue = kq;
114 iocb[i]->aio_sigevent.sigev_value.sival_ptr = iocb[i];
115 iocb[i]->aio_sigevent.sigev_notify = SIGEV_KEVENT;
117 result = aio_write(iocb[i]);
120 printf("Result %d iteration %d\n", result, i);
124 printf("WRITE %d is at %p\n", i, iocb[i]);
127 if (result < RAND_MAX/32) {
128 if (result > RAND_MAX/64) {
129 result = aio_cancel(fd, iocb[i]);
131 printf("Cancel %d %p result %d\n", i, iocb[i], result);
133 if (result == AIO_CANCELED) {
142 cancel = nitems(iocb) - pending;
150 bzero(&ke, sizeof(ke));
151 bzero(&kq_returned, sizeof(ke));
154 result = kevent(kq, NULL, 0,
155 &kq_returned, 1, &ts);
160 perror("kevent error: ");
161 kq_iocb = kq_returned.udata;
163 printf("kevent %d %d errno %d return.ident %p "
164 "return.data %p return.udata %p %p\n",
166 (void*)kq_returned.ident,
167 (void*)kq_returned.data,
175 printf("Try again left %d out of %lu %d\n",
176 pending, nitems(iocb), cancel);
180 for (j = 0; j < nitems(iocb) && iocb[j] != kq_iocb;
183 printf("kq_iocb %p\n", kq_iocb);
185 printf("Error Result for %d is %d pending %d\n",
188 result = aio_return(kq_iocb);
190 printf("Return Result for %d is %d\n\n", j, result);
192 if (result != sizeof(buffer)) {
193 printf("FAIL: run %d, operation %d, result %d "
194 " (errno=%d) should be %zu\n", run, pending,
195 result, errno, sizeof(buffer));
198 printf("PASS: run %d, left %d\n", run,
207 for (i = 0; i < nitems(iocb); i++)
216 printf("FAIL: %d tests failed\n", failed);
218 printf("PASS: All tests passed\n");
220 exit (failed == 0 ? 0 : 1);