Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Native Windows async IO engine |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 3 | * Copyright (C) 2012 Bruce Cran <bruce@cran.org.uk> |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <unistd.h> |
| 9 | #include <signal.h> |
| 10 | #include <errno.h> |
| 11 | #include <windows.h> |
| 12 | |
| 13 | #include "../fio.h" |
| 14 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 15 | typedef BOOL (WINAPI *CANCELIOEX)(HANDLE hFile, LPOVERLAPPED lpOverlapped); |
| 16 | |
Bruce Cran | 66c098b | 2012-11-27 12:16:07 +0000 | [diff] [blame] | 17 | int geterrno_from_win_error (DWORD code, int deferrno); |
| 18 | |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 19 | struct fio_overlapped { |
| 20 | OVERLAPPED o; |
| 21 | struct io_u *io_u; |
| 22 | BOOL io_complete; |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 23 | }; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 24 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 25 | struct windowsaio_data { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 26 | struct io_u **aio_events; |
Bruce Cran | 5a90bb5 | 2012-12-06 19:59:42 +0000 | [diff] [blame] | 27 | HANDLE iocp; |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 28 | HANDLE iothread; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 29 | HANDLE iocomplete_event; |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 30 | CANCELIOEX pCancelIoEx; |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 31 | BOOL iothread_running; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 32 | }; |
| 33 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 34 | struct thread_ctx { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 35 | HANDLE iocp; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 36 | struct windowsaio_data *wd; |
| 37 | }; |
| 38 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 39 | static int fio_windowsaio_cancel(struct thread_data *td, |
| 40 | struct io_u *io_u); |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 41 | static BOOL timeout_expired(DWORD start_count, DWORD end_count); |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 42 | static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min, |
| 43 | unsigned int max, struct timespec *t); |
| 44 | static struct io_u *fio_windowsaio_event(struct thread_data *td, int event); |
| 45 | static int fio_windowsaio_queue(struct thread_data *td, |
| 46 | struct io_u *io_u); |
| 47 | static void fio_windowsaio_cleanup(struct thread_data *td); |
| 48 | static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter); |
| 49 | static int fio_windowsaio_init(struct thread_data *td); |
| 50 | static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f); |
| 51 | static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f); |
Bruce Cran | 2277d5d | 2012-12-06 20:33:14 +0100 | [diff] [blame] | 52 | static int win_to_posix_error(DWORD winerr); |
Bruce Cran | 66c098b | 2012-11-27 12:16:07 +0000 | [diff] [blame] | 53 | |
Bruce Cran | 2277d5d | 2012-12-06 20:33:14 +0100 | [diff] [blame] | 54 | static int win_to_posix_error(DWORD winerr) |
Bruce Cran | 66c098b | 2012-11-27 12:16:07 +0000 | [diff] [blame] | 55 | { |
| 56 | switch (winerr) |
| 57 | { |
| 58 | case ERROR_FILE_NOT_FOUND: return ENOENT; |
| 59 | case ERROR_PATH_NOT_FOUND: return ENOENT; |
| 60 | case ERROR_ACCESS_DENIED: return EACCES; |
| 61 | case ERROR_INVALID_HANDLE: return EBADF; |
| 62 | case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM; |
| 63 | case ERROR_INVALID_DATA: return EINVAL; |
| 64 | case ERROR_OUTOFMEMORY: return ENOMEM; |
| 65 | case ERROR_INVALID_DRIVE: return ENODEV; |
| 66 | case ERROR_NOT_SAME_DEVICE: return EXDEV; |
| 67 | case ERROR_WRITE_PROTECT: return EROFS; |
| 68 | case ERROR_BAD_UNIT: return ENODEV; |
| 69 | case ERROR_SHARING_VIOLATION: return EACCES; |
| 70 | case ERROR_LOCK_VIOLATION: return EACCES; |
| 71 | case ERROR_SHARING_BUFFER_EXCEEDED: return ENOLCK; |
| 72 | case ERROR_HANDLE_DISK_FULL: return ENOSPC; |
| 73 | case ERROR_NOT_SUPPORTED: return ENOSYS; |
| 74 | case ERROR_FILE_EXISTS: return EEXIST; |
| 75 | case ERROR_CANNOT_MAKE: return EPERM; |
| 76 | case ERROR_INVALID_PARAMETER: return EINVAL; |
| 77 | case ERROR_NO_PROC_SLOTS: return EAGAIN; |
| 78 | case ERROR_BROKEN_PIPE: return EPIPE; |
| 79 | case ERROR_OPEN_FAILED: return EIO; |
| 80 | case ERROR_NO_MORE_SEARCH_HANDLES: return ENFILE; |
| 81 | case ERROR_CALL_NOT_IMPLEMENTED: return ENOSYS; |
| 82 | case ERROR_INVALID_NAME: return ENOENT; |
| 83 | case ERROR_WAIT_NO_CHILDREN: return ECHILD; |
| 84 | case ERROR_CHILD_NOT_COMPLETE: return EBUSY; |
| 85 | case ERROR_DIR_NOT_EMPTY: return ENOTEMPTY; |
| 86 | case ERROR_SIGNAL_REFUSED: return EIO; |
| 87 | case ERROR_BAD_PATHNAME: return ENOENT; |
| 88 | case ERROR_SIGNAL_PENDING: return EBUSY; |
| 89 | case ERROR_MAX_THRDS_REACHED: return EAGAIN; |
| 90 | case ERROR_BUSY: return EBUSY; |
| 91 | case ERROR_ALREADY_EXISTS: return EEXIST; |
| 92 | case ERROR_NO_SIGNAL_SENT: return EIO; |
| 93 | case ERROR_FILENAME_EXCED_RANGE: return EINVAL; |
| 94 | case ERROR_META_EXPANSION_TOO_LONG: return EINVAL; |
| 95 | case ERROR_INVALID_SIGNAL_NUMBER: return EINVAL; |
| 96 | case ERROR_THREAD_1_INACTIVE: return EINVAL; |
| 97 | case ERROR_BAD_PIPE: return EINVAL; |
| 98 | case ERROR_PIPE_BUSY: return EBUSY; |
| 99 | case ERROR_NO_DATA: return EPIPE; |
| 100 | case ERROR_MORE_DATA: return EAGAIN; |
| 101 | case ERROR_DIRECTORY: return ENOTDIR; |
| 102 | case ERROR_PIPE_CONNECTED: return EBUSY; |
| 103 | case ERROR_NO_TOKEN: return EINVAL; |
| 104 | case ERROR_PROCESS_ABORTED: return EFAULT; |
| 105 | case ERROR_BAD_DEVICE: return ENODEV; |
| 106 | case ERROR_BAD_USERNAME: return EINVAL; |
| 107 | case ERROR_OPEN_FILES: return EAGAIN; |
| 108 | case ERROR_ACTIVE_CONNECTIONS: return EAGAIN; |
| 109 | case ERROR_DEVICE_IN_USE: return EAGAIN; |
| 110 | case ERROR_INVALID_AT_INTERRUPT_TIME: return EINTR; |
| 111 | case ERROR_IO_DEVICE: return EIO; |
| 112 | case ERROR_NOT_OWNER: return EPERM; |
| 113 | case ERROR_END_OF_MEDIA: return ENOSPC; |
| 114 | case ERROR_EOM_OVERFLOW: return ENOSPC; |
| 115 | case ERROR_BEGINNING_OF_MEDIA: return ESPIPE; |
| 116 | case ERROR_SETMARK_DETECTED: return ESPIPE; |
| 117 | case ERROR_NO_DATA_DETECTED: return ENOSPC; |
| 118 | case ERROR_POSSIBLE_DEADLOCK: return EDEADLOCK; |
| 119 | case ERROR_CRC: return EIO; |
| 120 | case ERROR_NEGATIVE_SEEK: return EINVAL; |
| 121 | case ERROR_DISK_FULL: return ENOSPC; |
| 122 | case ERROR_NOACCESS: return EFAULT; |
| 123 | case ERROR_FILE_INVALID: return ENXIO; |
| 124 | } |
| 125 | |
| 126 | return winerr; |
| 127 | } |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 128 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 129 | int sync_file_range(int fd, off64_t offset, off64_t nbytes, |
| 130 | unsigned int flags) |
| 131 | { |
| 132 | errno = ENOSYS; |
| 133 | return -1; |
| 134 | } |
| 135 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 136 | static int fio_windowsaio_init(struct thread_data *td) |
| 137 | { |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 138 | struct windowsaio_data *wd; |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 139 | HANDLE hKernel32Dll; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 140 | int rc = 0; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 141 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 142 | wd = malloc(sizeof(struct windowsaio_data)); |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 143 | if (wd != NULL) |
| 144 | ZeroMemory(wd, sizeof(struct windowsaio_data)); |
| 145 | else |
| 146 | rc = 1; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 147 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 148 | if (!rc) { |
| 149 | wd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u*)); |
| 150 | if (wd->aio_events == NULL) |
| 151 | rc = 1; |
Bruce Cran | e4db9fe | 2011-01-04 14:44:47 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 154 | if (!rc) { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 155 | /* Create an auto-reset event */ |
| 156 | wd->iocomplete_event = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 157 | if (wd->iocomplete_event == NULL) |
| 158 | rc = 1; |
| 159 | } |
| 160 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 161 | if (rc) { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 162 | if (wd != NULL) { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 163 | if (wd->aio_events != NULL) |
| 164 | free(wd->aio_events); |
| 165 | |
| 166 | free(wd); |
| 167 | } |
| 168 | } |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 169 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 170 | hKernel32Dll = GetModuleHandle("kernel32.dll"); |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 171 | wd->pCancelIoEx = (CANCELIOEX)GetProcAddress(hKernel32Dll, "CancelIoEx"); |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 172 | td->io_ops->data = wd; |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 173 | |
Bruce Cran | 5a90bb5 | 2012-12-06 19:59:42 +0000 | [diff] [blame] | 174 | |
| 175 | if (!rc) { |
| 176 | struct thread_ctx *ctx; |
| 177 | struct windowsaio_data *wd; |
| 178 | HANDLE hFile; |
| 179 | |
| 180 | hFile = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); |
| 181 | if (hFile == INVALID_HANDLE_VALUE) |
| 182 | rc = 1; |
| 183 | |
| 184 | wd = td->io_ops->data; |
| 185 | wd->iothread_running = TRUE; |
| 186 | wd->iocp = hFile; |
| 187 | |
| 188 | if (!rc) |
| 189 | ctx = malloc(sizeof(struct thread_ctx)); |
| 190 | |
| 191 | if (!rc && ctx == NULL) |
| 192 | { |
| 193 | log_err("fio: out of memory in windowsaio\n"); |
| 194 | CloseHandle(hFile); |
| 195 | rc = 1; |
| 196 | } |
| 197 | |
| 198 | if (!rc) |
| 199 | { |
| 200 | ctx->iocp = hFile; |
| 201 | ctx->wd = wd; |
| 202 | wd->iothread = CreateThread(NULL, 0, IoCompletionRoutine, ctx, 0, NULL); |
| 203 | } |
| 204 | |
| 205 | if (rc || wd->iothread == NULL) |
| 206 | rc = 1; |
| 207 | } |
| 208 | |
Bruce Cran | 4e79098 | 2011-07-09 08:20:47 +0200 | [diff] [blame] | 209 | return rc; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 210 | } |
| 211 | |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 212 | static void fio_windowsaio_cleanup(struct thread_data *td) |
| 213 | { |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 214 | struct windowsaio_data *wd; |
| 215 | |
| 216 | wd = td->io_ops->data; |
| 217 | |
| 218 | if (wd != NULL) { |
Jens Axboe | 40c5db3 | 2012-02-01 23:03:44 +0100 | [diff] [blame] | 219 | wd->iothread_running = FALSE; |
| 220 | WaitForSingleObject(wd->iothread, INFINITE); |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 221 | |
| 222 | CloseHandle(wd->iothread); |
| 223 | CloseHandle(wd->iocomplete_event); |
| 224 | |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 225 | free(wd->aio_events); |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 226 | free(wd); |
| 227 | |
| 228 | td->io_ops->data = NULL; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 233 | static int fio_windowsaio_open_file(struct thread_data *td, struct fio_file *f) |
| 234 | { |
| 235 | int rc = 0; |
Bruce Cran | 4e79098 | 2011-07-09 08:20:47 +0200 | [diff] [blame] | 236 | DWORD flags = FILE_FLAG_POSIX_SEMANTICS | FILE_FLAG_OVERLAPPED; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 237 | DWORD sharemode = FILE_SHARE_READ | FILE_SHARE_WRITE; |
| 238 | DWORD openmode = OPEN_ALWAYS; |
| 239 | DWORD access; |
| 240 | |
| 241 | dprint(FD_FILE, "fd open %s\n", f->file_name); |
| 242 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 243 | if (f->filetype == FIO_TYPE_PIPE) { |
| 244 | log_err("fio: windowsaio doesn't support pipes\n"); |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | if (!strcmp(f->file_name, "-")) { |
| 249 | log_err("fio: can't read/write to stdin/out\n"); |
| 250 | return 1; |
| 251 | } |
| 252 | |
| 253 | if (td->o.odirect) |
| 254 | flags |= FILE_FLAG_NO_BUFFERING; |
| 255 | if (td->o.sync_io) |
| 256 | flags |= FILE_FLAG_WRITE_THROUGH; |
| 257 | |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 258 | /* |
| 259 | * Inform Windows whether we're going to be doing sequential or |
| 260 | * random io so it can tune the Cache Manager |
| 261 | */ |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 262 | if (td->o.td_ddir == TD_DDIR_READ || |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 263 | td->o.td_ddir == TD_DDIR_WRITE) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 264 | flags |= FILE_FLAG_SEQUENTIAL_SCAN; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 265 | else |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 266 | flags |= FILE_FLAG_RANDOM_ACCESS; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 267 | |
Bruce Cran | ea4500d | 2011-05-04 07:54:13 -0600 | [diff] [blame] | 268 | if (!td_write(td) || read_only) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 269 | access = GENERIC_READ; |
| 270 | else |
| 271 | access = (GENERIC_READ | GENERIC_WRITE); |
| 272 | |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 273 | if (td->o.create_on_open) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 274 | openmode = OPEN_ALWAYS; |
| 275 | else |
| 276 | openmode = OPEN_EXISTING; |
| 277 | |
| 278 | f->hFile = CreateFile(f->file_name, access, sharemode, |
| 279 | NULL, openmode, flags, NULL); |
| 280 | |
Bruce Cran | 4e79098 | 2011-07-09 08:20:47 +0200 | [diff] [blame] | 281 | if (f->hFile == INVALID_HANDLE_VALUE) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 282 | rc = 1; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 283 | |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 284 | /* Only set up the completion port and thread if we're not just |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 285 | * querying the device size */ |
Jens Axboe | 40c5db3 | 2012-02-01 23:03:44 +0100 | [diff] [blame] | 286 | if (!rc && td->io_ops->data != NULL) { |
Jens Axboe | 40c5db3 | 2012-02-01 23:03:44 +0100 | [diff] [blame] | 287 | struct windowsaio_data *wd; |
| 288 | |
Jens Axboe | 40c5db3 | 2012-02-01 23:03:44 +0100 | [diff] [blame] | 289 | wd = td->io_ops->data; |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 290 | |
Bruce Cran | 5a90bb5 | 2012-12-06 19:59:42 +0000 | [diff] [blame] | 291 | if (CreateIoCompletionPort(f->hFile, wd->iocp, 0, 0) == NULL) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 292 | rc = 1; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 295 | return rc; |
| 296 | } |
| 297 | |
| 298 | static int fio_windowsaio_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
| 299 | { |
Bruce Cran | 4e79098 | 2011-07-09 08:20:47 +0200 | [diff] [blame] | 300 | int rc = 0; |
Bruce Cran | 1e42cc3 | 2012-02-01 21:46:55 +0100 | [diff] [blame] | 301 | |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 302 | dprint(FD_FILE, "fd close %s\n", f->file_name); |
| 303 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 304 | if (f->hFile != INVALID_HANDLE_VALUE) { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 305 | if (!CloseHandle(f->hFile)) |
Bruce Cran | 4e79098 | 2011-07-09 08:20:47 +0200 | [diff] [blame] | 306 | rc = 1; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | f->hFile = INVALID_HANDLE_VALUE; |
Bruce Cran | 4e79098 | 2011-07-09 08:20:47 +0200 | [diff] [blame] | 310 | return rc; |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 313 | static BOOL timeout_expired(DWORD start_count, DWORD end_count) |
| 314 | { |
| 315 | BOOL expired = FALSE; |
| 316 | DWORD current_time; |
| 317 | |
| 318 | current_time = GetTickCount(); |
| 319 | |
| 320 | if ((end_count > start_count) && current_time >= end_count) |
| 321 | expired = TRUE; |
| 322 | else if (current_time < start_count && current_time > end_count) |
| 323 | expired = TRUE; |
| 324 | |
| 325 | return expired; |
| 326 | } |
| 327 | |
| 328 | static struct io_u* fio_windowsaio_event(struct thread_data *td, int event) |
| 329 | { |
| 330 | struct windowsaio_data *wd = td->io_ops->data; |
| 331 | return wd->aio_events[event]; |
| 332 | } |
| 333 | |
| 334 | static int fio_windowsaio_getevents(struct thread_data *td, unsigned int min, |
| 335 | unsigned int max, struct timespec *t) |
| 336 | { |
| 337 | struct windowsaio_data *wd = td->io_ops->data; |
| 338 | struct flist_head *entry; |
| 339 | unsigned int dequeued = 0; |
| 340 | struct io_u *io_u; |
| 341 | struct fio_overlapped *fov; |
| 342 | DWORD start_count = 0; |
| 343 | DWORD end_count = 0; |
| 344 | DWORD status; |
| 345 | DWORD mswait = 250; |
| 346 | |
| 347 | if (t != NULL) { |
| 348 | mswait = (t->tv_sec * 1000) + (t->tv_nsec / 1000000); |
| 349 | start_count = GetTickCount(); |
| 350 | end_count = start_count + (t->tv_sec * 1000) + (t->tv_nsec / 1000000); |
| 351 | } |
| 352 | |
| 353 | do { |
| 354 | flist_for_each(entry, &td->io_u_busylist) { |
| 355 | io_u = flist_entry(entry, struct io_u, list); |
| 356 | fov = (struct fio_overlapped*)io_u->engine_data; |
| 357 | |
| 358 | if (fov->io_complete) { |
Jens Axboe | 40c5db3 | 2012-02-01 23:03:44 +0100 | [diff] [blame] | 359 | fov->io_complete = FALSE; |
Bruce Cran | f9a58c2 | 2012-04-04 08:35:13 -0600 | [diff] [blame] | 360 | ResetEvent(fov->o.hEvent); |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 361 | wd->aio_events[dequeued] = io_u; |
| 362 | dequeued++; |
| 363 | } |
| 364 | |
| 365 | if (dequeued >= min) |
| 366 | break; |
| 367 | } |
| 368 | |
Jens Axboe | 40c5db3 | 2012-02-01 23:03:44 +0100 | [diff] [blame] | 369 | if (dequeued < min) { |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 370 | status = WaitForSingleObject(wd->iocomplete_event, mswait); |
Bruce Cran | f9a58c2 | 2012-04-04 08:35:13 -0600 | [diff] [blame] | 371 | if (status != WAIT_OBJECT_0 && dequeued >= min) |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 372 | break; |
| 373 | } |
| 374 | |
| 375 | if (dequeued >= min || (t != NULL && timeout_expired(start_count, end_count))) |
| 376 | break; |
| 377 | } while (1); |
| 378 | |
| 379 | return dequeued; |
| 380 | } |
| 381 | |
Jens Axboe | 40c5db3 | 2012-02-01 23:03:44 +0100 | [diff] [blame] | 382 | static int fio_windowsaio_queue(struct thread_data *td, struct io_u *io_u) |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 383 | { |
Jens Axboe | c73ed24 | 2012-12-06 20:30:38 +0100 | [diff] [blame] | 384 | struct fio_overlapped *o = io_u->engine_data; |
| 385 | LPOVERLAPPED lpOvl = &o->o; |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 386 | DWORD iobytes; |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 387 | BOOL success = FALSE; |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 388 | int rc = FIO_Q_COMPLETED; |
| 389 | |
| 390 | fio_ro_check(td, io_u); |
| 391 | |
Bruce Cran | 4e79098 | 2011-07-09 08:20:47 +0200 | [diff] [blame] | 392 | lpOvl->Internal = STATUS_PENDING; |
| 393 | lpOvl->InternalHigh = 0; |
| 394 | lpOvl->Offset = io_u->offset & 0xFFFFFFFF; |
| 395 | lpOvl->OffsetHigh = io_u->offset >> 32; |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 396 | |
| 397 | switch (io_u->ddir) { |
Jens Axboe | 40c5db3 | 2012-02-01 23:03:44 +0100 | [diff] [blame] | 398 | case DDIR_WRITE: |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 399 | success = WriteFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl); |
| 400 | break; |
| 401 | case DDIR_READ: |
| 402 | success = ReadFile(io_u->file->hFile, io_u->xfer_buf, io_u->xfer_buflen, &iobytes, lpOvl); |
| 403 | break; |
| 404 | case DDIR_SYNC: |
| 405 | case DDIR_DATASYNC: |
| 406 | case DDIR_SYNC_FILE_RANGE: |
| 407 | success = FlushFileBuffers(io_u->file->hFile); |
| 408 | if (!success) |
Bruce Cran | 2277d5d | 2012-12-06 20:33:14 +0100 | [diff] [blame] | 409 | io_u->error = win_to_posix_error(GetLastError()); |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 410 | |
| 411 | return FIO_Q_COMPLETED; |
| 412 | break; |
| 413 | case DDIR_TRIM: |
| 414 | log_err("manual TRIM isn't supported on Windows"); |
| 415 | io_u->error = 1; |
| 416 | io_u->resid = io_u->xfer_buflen; |
| 417 | return FIO_Q_COMPLETED; |
| 418 | break; |
| 419 | default: |
| 420 | assert(0); |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 421 | break; |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Jens Axboe | 40c5db3 | 2012-02-01 23:03:44 +0100 | [diff] [blame] | 424 | if (success || GetLastError() == ERROR_IO_PENDING) |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 425 | rc = FIO_Q_QUEUED; |
Jens Axboe | 40c5db3 | 2012-02-01 23:03:44 +0100 | [diff] [blame] | 426 | else { |
Bruce Cran | 2277d5d | 2012-12-06 20:33:14 +0100 | [diff] [blame] | 427 | io_u->error = win_to_posix_error(GetLastError()); |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 428 | io_u->resid = io_u->xfer_buflen; |
| 429 | } |
| 430 | |
| 431 | return rc; |
| 432 | } |
| 433 | |
| 434 | /* Runs as a thread and waits for queued IO to complete */ |
| 435 | static DWORD WINAPI IoCompletionRoutine(LPVOID lpParameter) |
| 436 | { |
| 437 | OVERLAPPED *ovl; |
| 438 | struct fio_overlapped *fov; |
| 439 | struct io_u *io_u; |
| 440 | struct windowsaio_data *wd; |
| 441 | struct thread_ctx *ctx; |
| 442 | ULONG_PTR ulKey = 0; |
| 443 | DWORD bytes; |
| 444 | |
| 445 | ctx = (struct thread_ctx*)lpParameter; |
| 446 | wd = ctx->wd; |
| 447 | |
| 448 | do { |
Bruce Cran | 66c098b | 2012-11-27 12:16:07 +0000 | [diff] [blame] | 449 | if (!GetQueuedCompletionStatus(ctx->iocp, &bytes, &ulKey, &ovl, 250) && ovl == NULL) |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 450 | continue; |
| 451 | |
| 452 | fov = CONTAINING_RECORD(ovl, struct fio_overlapped, o); |
| 453 | io_u = fov->io_u; |
| 454 | |
| 455 | if (ovl->Internal == ERROR_SUCCESS) { |
| 456 | io_u->resid = io_u->xfer_buflen - ovl->InternalHigh; |
| 457 | io_u->error = 0; |
| 458 | } else { |
| 459 | io_u->resid = io_u->xfer_buflen; |
Bruce Cran | 2277d5d | 2012-12-06 20:33:14 +0100 | [diff] [blame] | 460 | io_u->error = win_to_posix_error(GetLastError()); |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 461 | } |
| 462 | |
Jens Axboe | 40c5db3 | 2012-02-01 23:03:44 +0100 | [diff] [blame] | 463 | fov->io_complete = TRUE; |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 464 | SetEvent(wd->iocomplete_event); |
| 465 | } while (ctx->wd->iothread_running); |
| 466 | |
| 467 | CloseHandle(ctx->iocp); |
| 468 | free(ctx); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static int fio_windowsaio_cancel(struct thread_data *td, |
| 473 | struct io_u *io_u) |
| 474 | { |
| 475 | int rc = 0; |
| 476 | |
| 477 | struct windowsaio_data *wd = td->io_ops->data; |
| 478 | |
| 479 | /* If we're running on Vista or newer, we can cancel individual IO requests */ |
| 480 | if (wd->pCancelIoEx != NULL) { |
| 481 | struct fio_overlapped *ovl = io_u->engine_data; |
Jens Axboe | 40c5db3 | 2012-02-01 23:03:44 +0100 | [diff] [blame] | 482 | |
Bruce Cran | 6789703 | 2011-05-12 10:06:10 +0200 | [diff] [blame] | 483 | if (!wd->pCancelIoEx(io_u->file->hFile, &ovl->o)) |
| 484 | rc = 1; |
| 485 | } else |
| 486 | rc = 1; |
| 487 | |
| 488 | return rc; |
| 489 | } |
| 490 | |
Jens Axboe | c73ed24 | 2012-12-06 20:30:38 +0100 | [diff] [blame] | 491 | static void fio_windowsaio_io_u_free(struct thread_data *td, struct io_u *io_u) |
| 492 | { |
| 493 | struct fio_overlapped *o = io_u->engine_data; |
| 494 | |
| 495 | if (o) { |
| 496 | CloseHandle(o->o.hEvent); |
| 497 | io_u->engine_data = NULL; |
| 498 | free(o); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | static int fio_windowsaio_io_u_init(struct thread_data *td, struct io_u *io_u) |
| 503 | { |
| 504 | struct fio_overlapped *o; |
| 505 | |
| 506 | o = malloc(sizeof(*o)); |
Jens Axboe | b010641 | 2012-12-18 11:53:46 +0100 | [diff] [blame] | 507 | o->io_complete = FALSE; |
Jens Axboe | c73ed24 | 2012-12-06 20:30:38 +0100 | [diff] [blame] | 508 | o->io_u = io_u; |
| 509 | o->o.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 510 | if (!o->o.hEvent) { |
| 511 | free(o); |
| 512 | return 1; |
| 513 | } |
| 514 | |
| 515 | io_u->engine_data = o; |
| 516 | return 0; |
| 517 | } |
| 518 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 519 | static struct ioengine_ops ioengine = { |
| 520 | .name = "windowsaio", |
| 521 | .version = FIO_IOOPS_VERSION, |
| 522 | .init = fio_windowsaio_init, |
| 523 | .queue = fio_windowsaio_queue, |
| 524 | .cancel = fio_windowsaio_cancel, |
| 525 | .getevents = fio_windowsaio_getevents, |
| 526 | .event = fio_windowsaio_event, |
| 527 | .cleanup = fio_windowsaio_cleanup, |
| 528 | .open_file = fio_windowsaio_open_file, |
| 529 | .close_file = fio_windowsaio_close_file, |
Jens Axboe | c73ed24 | 2012-12-06 20:30:38 +0100 | [diff] [blame] | 530 | .get_file_size = generic_get_file_size, |
| 531 | .io_u_init = fio_windowsaio_io_u_init, |
| 532 | .io_u_free = fio_windowsaio_io_u_free, |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 533 | }; |
| 534 | |
| 535 | static void fio_init fio_posixaio_register(void) |
| 536 | { |
| 537 | register_ioengine(&ioengine); |
| 538 | } |
| 539 | |
| 540 | static void fio_exit fio_posixaio_unregister(void) |
| 541 | { |
| 542 | unregister_ioengine(&ioengine); |
| 543 | } |