Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1 | /* |
Jens Axboe | da751ca | 2007-03-14 10:59:33 +0100 | [diff] [blame] | 2 | * posixaio engine |
| 3 | * |
| 4 | * IO engine that uses the posix defined aio interface. |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 5 | * |
| 6 | */ |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <unistd.h> |
| 10 | #include <errno.h> |
Jens Axboe | bc733f3 | 2008-06-02 12:25:12 +0200 | [diff] [blame] | 11 | #include <fcntl.h> |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 12 | |
| 13 | #include "../fio.h" |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 14 | |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 15 | #ifdef FIO_HAVE_POSIXAIO |
| 16 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 17 | struct posixaio_data { |
| 18 | struct io_u **aio_events; |
Jens Axboe | 207cb0f | 2008-06-02 12:28:02 +0200 | [diff] [blame] | 19 | unsigned int queued; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | static int fill_timespec(struct timespec *ts) |
| 23 | { |
| 24 | #ifdef _POSIX_TIMERS |
| 25 | if (!clock_gettime(CLOCK_MONOTONIC, ts)) |
| 26 | return 0; |
| 27 | |
| 28 | perror("clock_gettime"); |
| 29 | #endif |
| 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | static unsigned long long ts_utime_since_now(struct timespec *t) |
| 34 | { |
| 35 | long long sec, nsec; |
| 36 | struct timespec now; |
| 37 | |
| 38 | if (fill_timespec(&now)) |
| 39 | return 0; |
| 40 | |
| 41 | sec = now.tv_sec - t->tv_sec; |
| 42 | nsec = now.tv_nsec - t->tv_nsec; |
| 43 | if (sec > 0 && nsec < 0) { |
| 44 | sec--; |
| 45 | nsec += 1000000000; |
| 46 | } |
| 47 | |
| 48 | sec *= 1000000; |
| 49 | nsec /= 1000; |
| 50 | return sec + nsec; |
| 51 | } |
| 52 | |
Jens Axboe | 7a16dd0 | 2006-10-18 17:21:58 +0200 | [diff] [blame] | 53 | static int fio_posixaio_cancel(struct thread_data fio_unused *td, |
| 54 | struct io_u *io_u) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 55 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 56 | struct fio_file *f = io_u->file; |
| 57 | int r = aio_cancel(f->fd, &io_u->aiocb); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 58 | |
YAMAMOTO Takashi | 2faf9ec | 2010-06-11 09:21:33 +0200 | [diff] [blame] | 59 | if (r == AIO_ALLDONE || r == AIO_CANCELED) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 60 | return 0; |
| 61 | |
| 62 | return 1; |
| 63 | } |
| 64 | |
Jens Axboe | 7a16dd0 | 2006-10-18 17:21:58 +0200 | [diff] [blame] | 65 | static int fio_posixaio_prep(struct thread_data fio_unused *td, |
| 66 | struct io_u *io_u) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 67 | { |
| 68 | struct aiocb *aiocb = &io_u->aiocb; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 69 | struct fio_file *f = io_u->file; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 70 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 71 | aiocb->aio_fildes = f->fd; |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 72 | aiocb->aio_buf = io_u->xfer_buf; |
| 73 | aiocb->aio_nbytes = io_u->xfer_buflen; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 74 | aiocb->aio_offset = io_u->offset; |
Jens Axboe | 9918be5 | 2011-07-09 13:33:38 +0200 | [diff] [blame^] | 75 | aiocb->aio_sigevent.sigev_notify = SIGEV_NONE; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 76 | |
| 77 | io_u->seen = 0; |
| 78 | return 0; |
| 79 | } |
| 80 | |
Jens Axboe | 3c77037 | 2008-03-10 18:45:57 +0100 | [diff] [blame] | 81 | #define SUSPEND_ENTRIES 8 |
| 82 | |
Jens Axboe | e7d2e61 | 2007-12-11 10:49:39 +0100 | [diff] [blame] | 83 | static int fio_posixaio_getevents(struct thread_data *td, unsigned int min, |
| 84 | unsigned int max, struct timespec *t) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 85 | { |
| 86 | struct posixaio_data *pd = td->io_ops->data; |
Jens Axboe | 3c77037 | 2008-03-10 18:45:57 +0100 | [diff] [blame] | 87 | struct aiocb *suspend_list[SUSPEND_ENTRIES]; |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 88 | struct flist_head *entry; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 89 | struct timespec start; |
Jens Axboe | a3cc770 | 2007-12-11 13:23:27 +0100 | [diff] [blame] | 90 | int have_timeout = 0; |
Jens Axboe | 3c77037 | 2008-03-10 18:45:57 +0100 | [diff] [blame] | 91 | int suspend_entries = 0; |
Jens Axboe | a3cc770 | 2007-12-11 13:23:27 +0100 | [diff] [blame] | 92 | unsigned int r; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 93 | |
| 94 | if (t && !fill_timespec(&start)) |
| 95 | have_timeout = 1; |
| 96 | |
| 97 | r = 0; |
Jens Axboe | 565cc35 | 2008-03-10 18:47:53 +0100 | [diff] [blame] | 98 | memset(suspend_list, 0, sizeof(*suspend_list)); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 99 | restart: |
Jens Axboe | 01743ee | 2008-06-02 12:19:19 +0200 | [diff] [blame] | 100 | flist_for_each(entry, &td->io_u_busylist) { |
| 101 | struct io_u *io_u = flist_entry(entry, struct io_u, list); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 102 | int err; |
| 103 | |
| 104 | if (io_u->seen) |
| 105 | continue; |
| 106 | |
| 107 | err = aio_error(&io_u->aiocb); |
Jens Axboe | 3c77037 | 2008-03-10 18:45:57 +0100 | [diff] [blame] | 108 | if (err == EINPROGRESS) { |
| 109 | if (suspend_entries < SUSPEND_ENTRIES) { |
| 110 | suspend_list[suspend_entries] = &io_u->aiocb; |
| 111 | suspend_entries++; |
| 112 | } |
Jens Axboe | 3f34431 | 2007-03-14 14:14:48 +0100 | [diff] [blame] | 113 | continue; |
Jens Axboe | 3c77037 | 2008-03-10 18:45:57 +0100 | [diff] [blame] | 114 | } |
Jens Axboe | 3f34431 | 2007-03-14 14:14:48 +0100 | [diff] [blame] | 115 | |
| 116 | io_u->seen = 1; |
Jens Axboe | 207cb0f | 2008-06-02 12:28:02 +0200 | [diff] [blame] | 117 | pd->queued--; |
Jens Axboe | 3f34431 | 2007-03-14 14:14:48 +0100 | [diff] [blame] | 118 | pd->aio_events[r++] = io_u; |
| 119 | |
| 120 | if (err == ECANCELED) |
| 121 | io_u->resid = io_u->xfer_buflen; |
| 122 | else if (!err) { |
| 123 | ssize_t retval = aio_return(&io_u->aiocb); |
| 124 | |
| 125 | io_u->resid = io_u->xfer_buflen - retval; |
| 126 | } else |
| 127 | io_u->error = err; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (r >= min) |
| 131 | return r; |
| 132 | |
| 133 | if (have_timeout) { |
| 134 | unsigned long long usec; |
| 135 | |
| 136 | usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000); |
| 137 | if (ts_utime_since_now(&start) > usec) |
| 138 | return r; |
| 139 | } |
| 140 | |
| 141 | /* |
Jens Axboe | 3c77037 | 2008-03-10 18:45:57 +0100 | [diff] [blame] | 142 | * must have some in-flight, wait for at least one |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 143 | */ |
Jens Axboe | 3c77037 | 2008-03-10 18:45:57 +0100 | [diff] [blame] | 144 | aio_suspend((const struct aiocb * const *)suspend_list, |
| 145 | suspend_entries, t); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 146 | goto restart; |
| 147 | } |
| 148 | |
| 149 | static struct io_u *fio_posixaio_event(struct thread_data *td, int event) |
| 150 | { |
| 151 | struct posixaio_data *pd = td->io_ops->data; |
| 152 | |
| 153 | return pd->aio_events[event]; |
| 154 | } |
| 155 | |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 156 | static int fio_posixaio_queue(struct thread_data *td, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 157 | struct io_u *io_u) |
| 158 | { |
Jens Axboe | 207cb0f | 2008-06-02 12:28:02 +0200 | [diff] [blame] | 159 | struct posixaio_data *pd = td->io_ops->data; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 160 | struct aiocb *aiocb = &io_u->aiocb; |
| 161 | int ret; |
| 162 | |
Jens Axboe | 7101d9c | 2007-09-12 13:12:39 +0200 | [diff] [blame] | 163 | fio_ro_check(td, io_u); |
| 164 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 165 | if (io_u->ddir == DDIR_READ) |
| 166 | ret = aio_read(aiocb); |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 167 | else if (io_u->ddir == DDIR_WRITE) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 168 | ret = aio_write(aiocb); |
Jens Axboe | a5f3027 | 2010-07-19 16:19:55 -0600 | [diff] [blame] | 169 | else if (io_u->ddir == DDIR_TRIM) { |
| 170 | if (pd->queued) |
| 171 | return FIO_Q_BUSY; |
| 172 | |
| 173 | do_io_u_trim(td, io_u); |
| 174 | return FIO_Q_COMPLETED; |
| 175 | } else { |
Jens Axboe | 207cb0f | 2008-06-02 12:28:02 +0200 | [diff] [blame] | 176 | #ifdef FIO_HAVE_POSIXAIO_FSYNC |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 177 | ret = aio_fsync(O_SYNC, aiocb); |
Jens Axboe | 207cb0f | 2008-06-02 12:28:02 +0200 | [diff] [blame] | 178 | #else |
| 179 | if (pd->queued) |
| 180 | return FIO_Q_BUSY; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 181 | |
Jens Axboe | f011531 | 2010-03-09 21:47:15 +0100 | [diff] [blame] | 182 | do_io_u_sync(td, io_u); |
Jens Axboe | 207cb0f | 2008-06-02 12:28:02 +0200 | [diff] [blame] | 183 | return FIO_Q_COMPLETED; |
| 184 | #endif |
| 185 | } |
| 186 | |
Jens Axboe | 95bcd81 | 2007-02-11 01:01:57 +0100 | [diff] [blame] | 187 | if (ret) { |
Jens Axboe | afa1640 | 2011-07-07 21:06:40 +0200 | [diff] [blame] | 188 | /* |
| 189 | * At least OSX has a very low limit on the number of pending |
Jens Axboe | def1d8e | 2011-07-08 08:33:37 +0200 | [diff] [blame] | 190 | * IOs, so if it returns EAGAIN, we are out of resources |
| 191 | * to queue more. Just return FIO_Q_BUSY to naturally |
| 192 | * drop off at this depth. |
Jens Axboe | afa1640 | 2011-07-07 21:06:40 +0200 | [diff] [blame] | 193 | */ |
| 194 | if (errno == EAGAIN) |
| 195 | return FIO_Q_BUSY; |
| 196 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 197 | io_u->error = errno; |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 198 | td_verror(td, io_u->error, "xfer"); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 199 | return FIO_Q_COMPLETED; |
Jens Axboe | 95bcd81 | 2007-02-11 01:01:57 +0100 | [diff] [blame] | 200 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 201 | |
Jens Axboe | 207cb0f | 2008-06-02 12:28:02 +0200 | [diff] [blame] | 202 | pd->queued++; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 203 | return FIO_Q_QUEUED; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static void fio_posixaio_cleanup(struct thread_data *td) |
| 207 | { |
| 208 | struct posixaio_data *pd = td->io_ops->data; |
| 209 | |
| 210 | if (pd) { |
| 211 | free(pd->aio_events); |
| 212 | free(pd); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | static int fio_posixaio_init(struct thread_data *td) |
| 217 | { |
| 218 | struct posixaio_data *pd = malloc(sizeof(*pd)); |
| 219 | |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame] | 220 | memset(pd, 0, sizeof(*pd)); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 221 | pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *)); |
| 222 | memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *)); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 223 | |
| 224 | td->io_ops->data = pd; |
| 225 | return 0; |
| 226 | } |
| 227 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 228 | static struct ioengine_ops ioengine = { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 229 | .name = "posixaio", |
| 230 | .version = FIO_IOOPS_VERSION, |
| 231 | .init = fio_posixaio_init, |
| 232 | .prep = fio_posixaio_prep, |
| 233 | .queue = fio_posixaio_queue, |
| 234 | .cancel = fio_posixaio_cancel, |
| 235 | .getevents = fio_posixaio_getevents, |
| 236 | .event = fio_posixaio_event, |
| 237 | .cleanup = fio_posixaio_cleanup, |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 238 | .open_file = generic_open_file, |
| 239 | .close_file = generic_close_file, |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 240 | .get_file_size = generic_get_file_size, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 241 | }; |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 242 | |
| 243 | #else /* FIO_HAVE_POSIXAIO */ |
| 244 | |
| 245 | /* |
| 246 | * When we have a proper configure system in place, we simply wont build |
| 247 | * and install this io engine. For now install a crippled version that |
| 248 | * just complains and fails to load. |
| 249 | */ |
| 250 | static int fio_posixaio_init(struct thread_data fio_unused *td) |
| 251 | { |
Jens Axboe | a3edaf7 | 2010-09-26 10:53:40 +0900 | [diff] [blame] | 252 | log_err("fio: posixaio not available\n"); |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 253 | return 1; |
| 254 | } |
| 255 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 256 | static struct ioengine_ops ioengine = { |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 257 | .name = "posixaio", |
| 258 | .version = FIO_IOOPS_VERSION, |
| 259 | .init = fio_posixaio_init, |
| 260 | }; |
| 261 | |
| 262 | #endif |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 263 | |
| 264 | static void fio_init fio_posixaio_register(void) |
| 265 | { |
| 266 | register_ioengine(&ioengine); |
| 267 | } |
| 268 | |
| 269 | static void fio_exit fio_posixaio_unregister(void) |
| 270 | { |
| 271 | unregister_ioengine(&ioengine); |
| 272 | } |