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 | * libaio engine |
| 3 | * |
| 4 | * IO engine using the Linux native 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> |
| 11 | #include <assert.h> |
Jens Axboe | 67bf982 | 2013-01-10 11:23:19 +0100 | [diff] [blame^] | 12 | #include <libaio.h> |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 13 | |
| 14 | #include "../fio.h" |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 15 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 16 | struct libaio_data { |
| 17 | io_context_t aio_ctx; |
| 18 | struct io_event *aio_events; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 19 | struct iocb **iocbs; |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 20 | struct io_u **io_us; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 21 | int iocbs_nr; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 22 | }; |
| 23 | |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 24 | struct libaio_options { |
| 25 | struct thread_data *td; |
| 26 | unsigned int userspace_reap; |
| 27 | }; |
| 28 | |
| 29 | static struct fio_option options[] = { |
| 30 | { |
| 31 | .name = "userspace_reap", |
| 32 | .type = FIO_OPT_STR_SET, |
| 33 | .off1 = offsetof(struct libaio_options, userspace_reap), |
| 34 | .help = "Use alternative user-space reap implementation", |
| 35 | }, |
| 36 | { |
| 37 | .name = NULL, |
| 38 | }, |
| 39 | }; |
| 40 | |
Jens Axboe | 7a16dd0 | 2006-10-18 17:21:58 +0200 | [diff] [blame] | 41 | static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 42 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 43 | struct fio_file *f = io_u->file; |
| 44 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 45 | if (io_u->ddir == DDIR_READ) |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 46 | io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 47 | else if (io_u->ddir == DDIR_WRITE) |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 48 | io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
Jens Axboe | 5f9099e | 2009-06-16 22:40:26 +0200 | [diff] [blame] | 49 | else if (ddir_sync(io_u->ddir)) |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 50 | io_prep_fsync(&io_u->iocb, f->fd); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static struct io_u *fio_libaio_event(struct thread_data *td, int event) |
| 56 | { |
| 57 | struct libaio_data *ld = td->io_ops->data; |
Jens Axboe | f423479 | 2007-03-02 15:16:03 +0100 | [diff] [blame] | 58 | struct io_event *ev; |
| 59 | struct io_u *io_u; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 60 | |
Jens Axboe | f423479 | 2007-03-02 15:16:03 +0100 | [diff] [blame] | 61 | ev = ld->aio_events + event; |
Jens Axboe | 0cc6ee5 | 2013-01-02 12:55:41 +0100 | [diff] [blame] | 62 | io_u = container_of(ev->obj, struct io_u, iocb); |
Jens Axboe | f423479 | 2007-03-02 15:16:03 +0100 | [diff] [blame] | 63 | |
| 64 | if (ev->res != io_u->xfer_buflen) { |
| 65 | if (ev->res > io_u->xfer_buflen) |
| 66 | io_u->error = -ev->res; |
| 67 | else |
| 68 | io_u->resid = io_u->xfer_buflen - ev->res; |
| 69 | } else |
| 70 | io_u->error = 0; |
| 71 | |
| 72 | return io_u; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Dan Ehrenberg | 675012f | 2011-08-30 20:36:15 -0600 | [diff] [blame] | 75 | struct aio_ring { |
| 76 | unsigned id; /** kernel internal index number */ |
| 77 | unsigned nr; /** number of io_events */ |
| 78 | unsigned head; |
| 79 | unsigned tail; |
Jens Axboe | c44b1ff | 2011-08-30 20:43:53 -0600 | [diff] [blame] | 80 | |
Dan Ehrenberg | 675012f | 2011-08-30 20:36:15 -0600 | [diff] [blame] | 81 | unsigned magic; |
| 82 | unsigned compat_features; |
| 83 | unsigned incompat_features; |
| 84 | unsigned header_length; /** size of aio_ring */ |
| 85 | |
| 86 | struct io_event events[0]; |
| 87 | }; |
| 88 | |
| 89 | #define AIO_RING_MAGIC 0xa10a10a1 |
| 90 | |
| 91 | static int user_io_getevents(io_context_t aio_ctx, unsigned int max, |
Jens Axboe | c44b1ff | 2011-08-30 20:43:53 -0600 | [diff] [blame] | 92 | struct io_event *events) |
Dan Ehrenberg | 675012f | 2011-08-30 20:36:15 -0600 | [diff] [blame] | 93 | { |
| 94 | long i = 0; |
| 95 | unsigned head; |
Jens Axboe | c44b1ff | 2011-08-30 20:43:53 -0600 | [diff] [blame] | 96 | struct aio_ring *ring = (struct aio_ring*) aio_ctx; |
Dan Ehrenberg | 675012f | 2011-08-30 20:36:15 -0600 | [diff] [blame] | 97 | |
| 98 | while (i < max) { |
| 99 | head = ring->head; |
| 100 | |
| 101 | if (head == ring->tail) { |
| 102 | /* There are no more completions */ |
| 103 | break; |
| 104 | } else { |
| 105 | /* There is another completion to reap */ |
| 106 | events[i] = ring->events[head]; |
| 107 | read_barrier(); |
Jens Axboe | c44b1ff | 2011-08-30 20:43:53 -0600 | [diff] [blame] | 108 | ring->head = (head + 1) % ring->nr; |
Dan Ehrenberg | 675012f | 2011-08-30 20:36:15 -0600 | [diff] [blame] | 109 | i++; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return i; |
| 114 | } |
| 115 | |
Jens Axboe | e7d2e61 | 2007-12-11 10:49:39 +0100 | [diff] [blame] | 116 | static int fio_libaio_getevents(struct thread_data *td, unsigned int min, |
| 117 | unsigned int max, struct timespec *t) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 118 | { |
| 119 | struct libaio_data *ld = td->io_ops->data; |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 120 | struct libaio_options *o = td->eo; |
Dan Ehrenberg | 0b7fdba | 2011-07-23 13:36:46 +0200 | [diff] [blame] | 121 | unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min; |
| 122 | int r, events = 0; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 123 | |
| 124 | do { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 125 | if (o->userspace_reap == 1 |
Dan Ehrenberg | 675012f | 2011-08-30 20:36:15 -0600 | [diff] [blame] | 126 | && actual_min == 0 |
| 127 | && ((struct aio_ring *)(ld->aio_ctx))->magic |
| 128 | == AIO_RING_MAGIC) { |
| 129 | r = user_io_getevents(ld->aio_ctx, max, |
| 130 | ld->aio_events + events); |
| 131 | } else { |
| 132 | r = io_getevents(ld->aio_ctx, actual_min, |
| 133 | max, ld->aio_events + events, t); |
| 134 | } |
Dan Ehrenberg | 0b7fdba | 2011-07-23 13:36:46 +0200 | [diff] [blame] | 135 | if (r >= 0) |
| 136 | events += r; |
| 137 | else if (r == -EAGAIN) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 138 | usleep(100); |
Dan Ehrenberg | 0b7fdba | 2011-07-23 13:36:46 +0200 | [diff] [blame] | 139 | } while (events < min); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 140 | |
Dan Ehrenberg | 0b7fdba | 2011-07-23 13:36:46 +0200 | [diff] [blame] | 141 | return r < 0 ? r : events; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u) |
| 145 | { |
| 146 | struct libaio_data *ld = td->io_ops->data; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 147 | |
Jens Axboe | 7101d9c | 2007-09-12 13:12:39 +0200 | [diff] [blame] | 148 | fio_ro_check(td, io_u); |
| 149 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 150 | if (ld->iocbs_nr == (int) td->o.iodepth) |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 151 | return FIO_Q_BUSY; |
| 152 | |
| 153 | /* |
| 154 | * fsync is tricky, since it can fail and we need to do it |
| 155 | * serialized with other io. the reason is that linux doesn't |
| 156 | * support aio fsync yet. So return busy for the case where we |
| 157 | * have pending io, to let fio complete those first. |
| 158 | */ |
Jens Axboe | f011531 | 2010-03-09 21:47:15 +0100 | [diff] [blame] | 159 | if (ddir_sync(io_u->ddir)) { |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 160 | if (ld->iocbs_nr) |
| 161 | return FIO_Q_BUSY; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 162 | |
Jens Axboe | f011531 | 2010-03-09 21:47:15 +0100 | [diff] [blame] | 163 | do_io_u_sync(td, io_u); |
Jens Axboe | 5f9099e | 2009-06-16 22:40:26 +0200 | [diff] [blame] | 164 | return FIO_Q_COMPLETED; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Jens Axboe | a5f3027 | 2010-07-19 16:19:55 -0600 | [diff] [blame] | 167 | if (io_u->ddir == DDIR_TRIM) { |
| 168 | if (ld->iocbs_nr) |
| 169 | return FIO_Q_BUSY; |
| 170 | |
| 171 | do_io_u_trim(td, io_u); |
| 172 | return FIO_Q_COMPLETED; |
| 173 | } |
| 174 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 175 | ld->iocbs[ld->iocbs_nr] = &io_u->iocb; |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 176 | ld->io_us[ld->iocbs_nr] = io_u; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 177 | ld->iocbs_nr++; |
| 178 | return FIO_Q_QUEUED; |
| 179 | } |
| 180 | |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 181 | static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us, |
| 182 | unsigned int nr) |
| 183 | { |
| 184 | struct timeval now; |
| 185 | unsigned int i; |
| 186 | |
Jens Axboe | 12d9d84 | 2008-10-16 21:26:10 +0200 | [diff] [blame] | 187 | if (!fio_fill_issue_time(td)) |
| 188 | return; |
| 189 | |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 190 | fio_gettime(&now, NULL); |
| 191 | |
| 192 | for (i = 0; i < nr; i++) { |
| 193 | struct io_u *io_u = io_us[i]; |
| 194 | |
| 195 | memcpy(&io_u->issue_time, &now, sizeof(now)); |
| 196 | io_u_queued(td, io_u); |
| 197 | } |
| 198 | } |
| 199 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 200 | static int fio_libaio_commit(struct thread_data *td) |
| 201 | { |
| 202 | struct libaio_data *ld = td->io_ops->data; |
| 203 | struct iocb **iocbs; |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 204 | struct io_u **io_us; |
Jens Axboe | 5e00c2c | 2008-01-18 10:26:58 +0100 | [diff] [blame] | 205 | int ret; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 206 | |
| 207 | if (!ld->iocbs_nr) |
| 208 | return 0; |
| 209 | |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 210 | io_us = ld->io_us; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 211 | iocbs = ld->iocbs; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 212 | do { |
Jens Axboe | 5e00c2c | 2008-01-18 10:26:58 +0100 | [diff] [blame] | 213 | ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs); |
| 214 | if (ret > 0) { |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 215 | fio_libaio_queued(td, io_us, ret); |
Jens Axboe | 838bc70 | 2008-05-22 13:08:23 +0200 | [diff] [blame] | 216 | io_u_mark_submit(td, ret); |
Jens Axboe | 5e00c2c | 2008-01-18 10:26:58 +0100 | [diff] [blame] | 217 | ld->iocbs_nr -= ret; |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 218 | io_us += ret; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 219 | iocbs += ret; |
Jens Axboe | 5e00c2c | 2008-01-18 10:26:58 +0100 | [diff] [blame] | 220 | ret = 0; |
Jens Axboe | 838bc70 | 2008-05-22 13:08:23 +0200 | [diff] [blame] | 221 | } else if (!ret || ret == -EAGAIN || ret == -EINTR) { |
| 222 | if (!ret) |
| 223 | io_u_mark_submit(td, ret); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 224 | continue; |
Jens Axboe | 838bc70 | 2008-05-22 13:08:23 +0200 | [diff] [blame] | 225 | } else |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 226 | break; |
Jens Axboe | 5e00c2c | 2008-01-18 10:26:58 +0100 | [diff] [blame] | 227 | } while (ld->iocbs_nr); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 228 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 229 | return ret; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u) |
| 233 | { |
| 234 | struct libaio_data *ld = td->io_ops->data; |
| 235 | |
| 236 | return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events); |
| 237 | } |
| 238 | |
| 239 | static void fio_libaio_cleanup(struct thread_data *td) |
| 240 | { |
| 241 | struct libaio_data *ld = td->io_ops->data; |
| 242 | |
| 243 | if (ld) { |
| 244 | io_destroy(ld->aio_ctx); |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 245 | free(ld->aio_events); |
| 246 | free(ld->iocbs); |
| 247 | free(ld->io_us); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 248 | free(ld); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | static int fio_libaio_init(struct thread_data *td) |
| 253 | { |
| 254 | struct libaio_data *ld = malloc(sizeof(*ld)); |
Jens Axboe | c90e101 | 2012-09-04 11:47:00 -0600 | [diff] [blame] | 255 | struct libaio_options *o = td->eo; |
| 256 | int err = 0; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 257 | |
| 258 | memset(ld, 0, sizeof(*ld)); |
Jens Axboe | c1db2dc | 2007-03-20 10:42:07 +0100 | [diff] [blame] | 259 | |
Jens Axboe | c90e101 | 2012-09-04 11:47:00 -0600 | [diff] [blame] | 260 | /* |
| 261 | * First try passing in 0 for queue depth, since we don't |
| 262 | * care about the user ring. If that fails, the kernel is too old |
| 263 | * and we need the right depth. |
| 264 | */ |
| 265 | if (!o->userspace_reap) |
Jens Axboe | c224ec0 | 2012-09-06 09:07:44 -0600 | [diff] [blame] | 266 | err = io_queue_init(INT_MAX, &ld->aio_ctx); |
Jens Axboe | c90e101 | 2012-09-04 11:47:00 -0600 | [diff] [blame] | 267 | if (o->userspace_reap || err == -EINVAL) |
| 268 | err = io_queue_init(td->o.iodepth, &ld->aio_ctx); |
Jens Axboe | c1db2dc | 2007-03-20 10:42:07 +0100 | [diff] [blame] | 269 | if (err) { |
| 270 | td_verror(td, -err, "io_queue_init"); |
Jens Axboe | 75de55a | 2008-04-10 20:52:40 +0200 | [diff] [blame] | 271 | log_err("fio: check /proc/sys/fs/aio-max-nr\n"); |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame] | 272 | free(ld); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 273 | return 1; |
| 274 | } |
| 275 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 276 | ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event)); |
| 277 | memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event)); |
| 278 | ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *)); |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 279 | memset(ld->iocbs, 0, sizeof(struct iocb *)); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 280 | ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *)); |
| 281 | memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *)); |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 282 | ld->iocbs_nr = 0; |
| 283 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 284 | td->io_ops->data = ld; |
| 285 | return 0; |
| 286 | } |
| 287 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 288 | static struct ioengine_ops ioengine = { |
Steven Lang | de890a1 | 2011-11-09 14:03:34 +0100 | [diff] [blame] | 289 | .name = "libaio", |
| 290 | .version = FIO_IOOPS_VERSION, |
| 291 | .init = fio_libaio_init, |
| 292 | .prep = fio_libaio_prep, |
| 293 | .queue = fio_libaio_queue, |
| 294 | .commit = fio_libaio_commit, |
| 295 | .cancel = fio_libaio_cancel, |
| 296 | .getevents = fio_libaio_getevents, |
| 297 | .event = fio_libaio_event, |
| 298 | .cleanup = fio_libaio_cleanup, |
| 299 | .open_file = generic_open_file, |
| 300 | .close_file = generic_close_file, |
| 301 | .get_file_size = generic_get_file_size, |
| 302 | .options = options, |
| 303 | .option_struct_size = sizeof(struct libaio_options), |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 304 | }; |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 305 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 306 | static void fio_init fio_libaio_register(void) |
| 307 | { |
| 308 | register_ioengine(&ioengine); |
| 309 | } |
| 310 | |
| 311 | static void fio_exit fio_libaio_unregister(void) |
| 312 | { |
| 313 | unregister_ioengine(&ioengine); |
| 314 | } |