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