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