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 <err.h> 46 #include <errno.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <unistd.h> 51 52 #include "freebsd_test_suite/macros.h" 53 #include "local.h" 54 55 #define PATH_TEMPLATE "aio.XXXXXXXXXX" 56 57 #define LIO_MAX 5 58 #define IOCBS_PER_LIO 16 59 #define MAX_IOCBS (LIO_MAX * IOCBS_PER_LIO) 60 #define MAX_RUNS 300 61 62 int 63 main(int argc, char *argv[]) 64 { 65 int fd; 66 struct aiocb *iocb[MAX_IOCBS]; 67 struct aiocb **lio[LIO_MAX], **kq_lio; 68 int i, result, run, error, j, k; 69 char buffer[32768]; 70 int kq; 71 struct kevent ke, kq_returned; 72 struct timespec ts; 73 struct sigevent sig; 74 time_t time1, time2; 75 char *file, pathname[sizeof(PATH_TEMPLATE)]; 76 int tmp_file = 0, failed = 0; 77 78 PLAIN_REQUIRE_KERNEL_MODULE("aio", 0); 79 PLAIN_REQUIRE_UNSAFE_AIO(0); 80 81 kq = kqueue(); 82 if (kq < 0) 83 err(1, "kqeueue(2) failed"); 84 85 if (argc == 1) { 86 strcpy(pathname, PATH_TEMPLATE); 87 fd = mkstemp(pathname); 88 file = pathname; 89 tmp_file = 1; 90 } else { 91 file = argv[1]; 92 fd = open(file, O_RDWR|O_CREAT, 0666); 93 } 94 if (fd < 0) 95 err(1, "can't open %s", argv[1]); 96 97 #ifdef DEBUG 98 printf("Hello kq %d fd %d\n", kq, fd); 99 #endif 100 101 for (run = 0; run < MAX_RUNS; run++) { 102 #ifdef DEBUG 103 printf("Run %d\n", run); 104 #endif 105 for (j = 0; j < LIO_MAX; j++) { 106 lio[j] = 107 malloc(sizeof(struct aiocb *) * IOCBS_PER_LIO); 108 for (i = 0; i < IOCBS_PER_LIO; i++) { 109 k = (IOCBS_PER_LIO * j) + i; 110 lio[j][i] = iocb[k] = 111 calloc(1, sizeof(struct aiocb)); 112 iocb[k]->aio_nbytes = sizeof(buffer); 113 iocb[k]->aio_buf = buffer; 114 iocb[k]->aio_fildes = fd; 115 iocb[k]->aio_offset 116 = iocb[k]->aio_nbytes * k * (run + 1); 117 118 #ifdef DEBUG 119 printf("hello iocb[k] %ld\n", 120 iocb[k]->aio_offset); 121 #endif 122 iocb[k]->aio_lio_opcode = LIO_WRITE; 123 } 124 sig.sigev_notify_kqueue = kq; 125 sig.sigev_value.sival_ptr = lio[j]; 126 sig.sigev_notify = SIGEV_KEVENT; 127 time(&time1); 128 result = lio_listio(LIO_NOWAIT, lio[j], 129 IOCBS_PER_LIO, &sig); 130 error = errno; 131 time(&time2); 132 #ifdef DEBUG 133 printf("Time %ld %ld %ld result -> %d\n", 134 time1, time2, time2-time1, result); 135 #endif 136 if (result != 0) { 137 errno = error; 138 err(1, "FAIL: Result %d iteration %d\n", 139 result, j); 140 } 141 #ifdef DEBUG 142 printf("write %d is at %p\n", j, lio[j]); 143 #endif 144 } 145 146 for (i = 0; i < LIO_MAX; i++) { 147 for (j = LIO_MAX - 1; j >=0; j--) { 148 if (lio[j]) 149 break; 150 } 151 152 for (;;) { 153 bzero(&ke, sizeof(ke)); 154 bzero(&kq_returned, sizeof(ke)); 155 ts.tv_sec = 0; 156 ts.tv_nsec = 1; 157 #ifdef DEBUG 158 printf("FOO lio %d -> %p\n", j, lio[j]); 159 #endif 160 EV_SET(&ke, (uintptr_t)lio[j], 161 EVFILT_LIO, EV_ONESHOT, 0, 0, iocb[j]); 162 result = kevent(kq, NULL, 0, 163 &kq_returned, 1, &ts); 164 error = errno; 165 if (result < 0) { 166 perror("kevent error: "); 167 } 168 kq_lio = kq_returned.udata; 169 #ifdef DEBUG 170 printf("kevent %d %d errno %d return.ident %p " 171 "return.data %p return.udata %p %p\n", 172 i, result, error, 173 (void*)kq_returned.ident, 174 (void*)kq_returned.data, 175 kq_returned.udata, 176 lio[j]); 177 #endif 178 179 if (kq_lio) 180 break; 181 #ifdef DEBUG 182 printf("Try again\n"); 183 #endif 184 } 185 186 #ifdef DEBUG 187 printf("lio %p\n", lio); 188 #endif 189 190 for (j = 0; j < LIO_MAX; j++) { 191 if (lio[j] == kq_lio) 192 break; 193 } 194 if (j == LIO_MAX) 195 errx(1, "FAIL: "); 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++; 203 } else 204 printf("PASS: run %d, operation %d result %d \n", run, LIO_MAX - i -1, result); 205 for (k = 0; k < MAX_IOCBS / LIO_MAX; k++) { 206 result = aio_return(kq_lio[k]); 207 #ifdef DEBUG 208 printf("Return Resulto for %d %d is %d\n", j, k, result); 209 #endif 210 if (result != sizeof(buffer)) { 211 printf("FAIL: run %d, operation %d sub-opt %d result %d (errno=%d) should be %zu\n", 212 run, LIO_MAX - i -1, k, result, errno, sizeof(buffer)); 213 } else { 214 printf("PASS: run %d, operation %d sub-opt %d result %d\n", 215 run, LIO_MAX - i -1, k, result); 216 } 217 } 218 #ifdef DEBUG 219 printf("\n"); 220 #endif 221 222 for (k = 0; k < MAX_IOCBS / LIO_MAX; k++) 223 free(lio[j][k]); 224 free(lio[j]); 225 lio[j] = NULL; 226 } 227 } 228 #ifdef DEBUG 229 printf("Done\n"); 230 #endif 231 232 if (tmp_file) 233 unlink(pathname); 234 235 if (failed) 236 errx(1, "FAIL: %d testcases failed", failed); 237 else 238 errx(0, "PASS: All\n"); 239 240 } 241