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. aio_kqueue /dev/<something safe>)
38 #include <sys/types.h>
39 #include <sys/event.h>
43 #define PATH_TEMPLATE "/tmp/aio.XXXXXXXXXX"
49 main(int argc, char *argv[]){
51 struct aiocb *iocb[MAX], *kq_iocb;
52 int i, result, run, error, j;
55 struct kevent ke, kq_returned;
57 int cancel, pending, tmp_file = 0, failed = 0;
58 char *file, pathname[sizeof(PATH_TEMPLATE)-1];
61 perror("No kqeueue\n");
66 strcpy(pathname, PATH_TEMPLATE);
67 fd = mkstemp(pathname);
72 fd = open(file, O_RDWR|O_CREAT, 0666);
75 fprintf(stderr, "Can't open %s\n", file);
80 for (run = 0; run < MAX_RUNS; run++){
82 printf("Run %d\n", run);
84 for(i = 0; i < MAX; i++) {
85 iocb[i] = (struct aiocb *)malloc(sizeof(struct aiocb));
86 bzero(iocb[i], sizeof(struct aiocb));
90 for(i = 0; i < MAX; i++) {
92 iocb[i]->aio_nbytes = sizeof(buffer);
93 iocb[i]->aio_buf = buffer;
94 iocb[i]->aio_fildes = fd;
95 iocb[i]->aio_offset = iocb[i]->aio_nbytes * i * run;
97 iocb[i]->aio_sigevent.sigev_notify_kqueue = kq;
98 iocb[i]->aio_sigevent.sigev_value.sival_ptr = iocb[i];
99 iocb[i]->aio_sigevent.sigev_notify = SIGEV_KEVENT;
101 result = aio_write(iocb[i]);
104 printf("Result %d iteration %d\n",result, i);
108 printf("WRITE %d is at %p\n", i, iocb[i]);
111 if (result < RAND_MAX/32) {
112 if (result > RAND_MAX/64) {
113 result = aio_cancel(fd, iocb[i]);
115 printf("Cancel %d %p result %d\n", i, iocb[i], result);
117 if (result == AIO_CANCELED) {
125 cancel = MAX - pending;
130 bzero(&ke, sizeof(ke));
131 bzero(&kq_returned, sizeof(ke));
134 result = kevent(kq, NULL, 0,
135 &kq_returned, 1, &ts);
138 perror("kevent error: ");
140 kq_iocb = kq_returned.udata;
142 printf("kevent %d %d errno %d return.ident %p "
143 "return.data %p return.udata %p %p\n",
145 kq_returned.ident, kq_returned.data,
153 printf("Try again left %d out of %d %d\n",pending, MAX, cancel);
157 for(j = 0; j < MAX; j++) {
158 if (iocb[j] == kq_iocb) {
163 printf("kq_iocb %p\n", kq_iocb);
165 printf("Error Result for %d is %d pending %d\n", j, result, pending);
167 result = aio_return(kq_iocb);
169 printf("Return Result for %d is %d\n", j, result);
172 if (result != sizeof(buffer)) {
173 printf("FAIL: run %d, operation %d, result %d (errno=%d) should be %d\n", run, pending, result, errno, sizeof(buffer));
176 printf("PASS: run %d, left %d\n", run, pending - 1);
191 printf("FAIL: Atleast one\n");
194 printf("PASS: All\n");