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 26 /* 27 * Prerequisities: 28 * - AIO support must be compiled into the kernel (see sys/<arch>/NOTES for 29 * more details). 30 * 31 * Note: it is a good idea to run this against a physical drive to 32 * exercise the physio fast path (ie. aio_kqueue /dev/<something safe>) 33 */ 34 35 #include <sys/types.h> 36 #include <sys/event.h> 37 #include <sys/time.h> 38 #include <aio.h> 39 #include <err.h> 40 #include <errno.h> 41 #include <fcntl.h> 42 #include <stdlib.h> 43 #include <stdio.h> 44 #include <string.h> 45 #include <unistd.h> 46 47 #include "freebsd_test_suite/macros.h" 48 #include "local.h" 49 50 #define PATH_TEMPLATE "aio.XXXXXXXXXX" 51 52 #define MAX_RUNS 300 53 /* #define DEBUG */ 54 55 int 56 main (int argc, char *argv[]) 57 { 58 struct aiocb **iocb, *kq_iocb; 59 char *file, pathname[sizeof(PATH_TEMPLATE)+1]; 60 struct kevent kq_returned; 61 struct timespec ts; 62 char buffer[32768]; 63 int max_queue_per_proc; 64 size_t max_queue_per_proc_size; 65 #ifdef DEBUG 66 int cancel, error; 67 #endif 68 int failed = 0, fd, kq, pending, result, run; 69 int tmp_file = 0; 70 int i, j; 71 72 PLAIN_REQUIRE_KERNEL_MODULE("aio", 0); 73 PLAIN_REQUIRE_UNSAFE_AIO(0); 74 75 max_queue_per_proc_size = sizeof(max_queue_per_proc); 76 if (sysctlbyname("vfs.aio.max_aio_queue_per_proc", 77 &max_queue_per_proc, &max_queue_per_proc_size, NULL, 0) != 0) 78 err(1, "sysctlbyname"); 79 iocb = calloc(max_queue_per_proc, sizeof(struct aiocb*)); 80 if (iocb == NULL) 81 err(1, "calloc"); 82 83 kq = kqueue(); 84 if (kq < 0) { 85 perror("No kqeueue\n"); 86 exit(1); 87 } 88 89 if (argc == 1) { 90 strcpy(pathname, PATH_TEMPLATE); 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 == -1) 99 err(1, "Can't open %s\n", file); 100 101 for (run = 0; run < MAX_RUNS; run++){ 102 #ifdef DEBUG 103 printf("Run %d\n", run); 104 #endif 105 for (i = 0; i < max_queue_per_proc; i++) { 106 iocb[i] = (struct aiocb *)calloc(1, 107 sizeof(struct aiocb)); 108 if (iocb[i] == NULL) 109 err(1, "calloc"); 110 } 111 112 pending = 0; 113 for (i = 0; i < max_queue_per_proc; i++) { 114 pending++; 115 iocb[i]->aio_nbytes = sizeof(buffer); 116 iocb[i]->aio_buf = buffer; 117 iocb[i]->aio_fildes = fd; 118 iocb[i]->aio_offset = iocb[i]->aio_nbytes * i * run; 119 120 iocb[i]->aio_sigevent.sigev_notify_kqueue = kq; 121 iocb[i]->aio_sigevent.sigev_value.sival_ptr = iocb[i]; 122 iocb[i]->aio_sigevent.sigev_notify = SIGEV_KEVENT; 123 124 result = aio_write(iocb[i]); 125 if (result != 0) { 126 perror("aio_write"); 127 printf("Result %d iteration %d\n", result, i); 128 exit(1); 129 } 130 #ifdef DEBUG 131 printf("WRITE %d is at %p\n", i, iocb[i]); 132 #endif 133 result = rand(); 134 if (result < RAND_MAX/32) { 135 if (result > RAND_MAX/64) { 136 result = aio_cancel(fd, iocb[i]); 137 #ifdef DEBUG 138 printf("Cancel %d %p result %d\n", i, iocb[i], result); 139 #endif 140 if (result == AIO_CANCELED) { 141 aio_return(iocb[i]); 142 iocb[i] = NULL; 143 pending--; 144 } 145 } 146 } 147 } 148 #ifdef DEBUG 149 cancel = max_queue_per_proc - pending; 150 #endif 151 152 i = 0; 153 while (pending) { 154 155 for (;;) { 156 157 bzero(&kq_returned, sizeof(kq_returned)); 158 ts.tv_sec = 0; 159 ts.tv_nsec = 1; 160 result = kevent(kq, NULL, 0, 161 &kq_returned, 1, &ts); 162 #ifdef DEBUG 163 error = errno; 164 #endif 165 if (result < 0) 166 perror("kevent error: "); 167 kq_iocb = kq_returned.udata; 168 #ifdef DEBUG 169 printf("kevent %d %d errno %d return.ident %p " 170 "return.data %p return.udata %p %p" 171 " filter %d flags %#x fflags %#x\n", 172 i, result, error, 173 (void*)kq_returned.ident, 174 (void*)kq_returned.data, 175 kq_returned.udata, 176 kq_iocb, 177 kq_returned.filter, 178 kq_returned.flags, 179 kq_returned.fflags); 180 if (result > 0) 181 printf("\tsigev_notify_kevent_flags %#x\n", 182 ((struct aiocb*)(kq_returned.ident))->aio_sigevent.sigev_notify_kevent_flags); 183 #endif 184 185 if (kq_iocb) 186 break; 187 #ifdef DEBUG 188 printf("Try again left %d out of %d %d\n", 189 pending, max_queue_per_proc, cancel); 190 #endif 191 } 192 193 for (j = 0; j < max_queue_per_proc && iocb[j] != kq_iocb; 194 j++) ; 195 #ifdef DEBUG 196 printf("kq_iocb %p\n", kq_iocb); 197 198 printf("Error Result for %d is %d pending %d\n", 199 j, result, pending); 200 #endif 201 result = aio_return(kq_iocb); 202 #ifdef DEBUG 203 printf("Return Result for %d is %d\n\n", j, result); 204 #endif 205 if (result != sizeof(buffer)) { 206 printf("FAIL: run %d, operation %d, result %d " 207 " (errno=%d) should be %zu\n", run, pending, 208 result, errno, sizeof(buffer)); 209 failed++; 210 } else 211 printf("PASS: run %d, left %d\n", run, 212 pending - 1); 213 214 free(kq_iocb); 215 iocb[j] = NULL; 216 pending--; 217 i++; 218 } 219 220 for (i = 0; i < max_queue_per_proc; i++) 221 free(iocb[i]); 222 223 } 224 225 if (tmp_file) 226 unlink(pathname); 227 228 if (failed != 0) 229 printf("FAIL: %d tests failed\n", failed); 230 else 231 printf("PASS: All tests passed\n"); 232 233 exit (failed == 0 ? 0 : 1); 234 } 235