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 | 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_LIBAIO |
| 16 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 17 | #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj) |
| 18 | |
| 19 | struct libaio_data { |
| 20 | io_context_t aio_ctx; |
| 21 | struct io_event *aio_events; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 22 | struct iocb **iocbs; |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 23 | struct io_u **io_us; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 24 | int iocbs_nr; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 25 | }; |
| 26 | |
Jens Axboe | 7a16dd0 | 2006-10-18 17:21:58 +0200 | [diff] [blame] | 27 | 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] | 28 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 29 | struct fio_file *f = io_u->file; |
| 30 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 31 | if (io_u->ddir == DDIR_READ) |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 32 | 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] | 33 | else if (io_u->ddir == DDIR_WRITE) |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 34 | io_prep_pwrite(&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] | 35 | else if (io_u->ddir == DDIR_SYNC) |
| 36 | io_prep_fsync(&io_u->iocb, f->fd); |
| 37 | else |
| 38 | return 1; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 39 | |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static struct io_u *fio_libaio_event(struct thread_data *td, int event) |
| 44 | { |
| 45 | struct libaio_data *ld = td->io_ops->data; |
Jens Axboe | f423479 | 2007-03-02 15:16:03 +0100 | [diff] [blame] | 46 | struct io_event *ev; |
| 47 | struct io_u *io_u; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 48 | |
Jens Axboe | f423479 | 2007-03-02 15:16:03 +0100 | [diff] [blame] | 49 | ev = ld->aio_events + event; |
| 50 | io_u = ev_to_iou(ev); |
| 51 | |
| 52 | if (ev->res != io_u->xfer_buflen) { |
| 53 | if (ev->res > io_u->xfer_buflen) |
| 54 | io_u->error = -ev->res; |
| 55 | else |
| 56 | io_u->resid = io_u->xfer_buflen - ev->res; |
| 57 | } else |
| 58 | io_u->error = 0; |
| 59 | |
| 60 | return io_u; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Jens Axboe | e7d2e61 | 2007-12-11 10:49:39 +0100 | [diff] [blame] | 63 | static int fio_libaio_getevents(struct thread_data *td, unsigned int min, |
| 64 | unsigned int max, struct timespec *t) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 65 | { |
| 66 | struct libaio_data *ld = td->io_ops->data; |
| 67 | long r; |
| 68 | |
| 69 | do { |
| 70 | r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t); |
Jens Axboe | ccbb91c | 2007-01-08 22:27:05 +0100 | [diff] [blame] | 71 | if (r >= min) |
| 72 | break; |
| 73 | else if (r == -EAGAIN) { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 74 | usleep(100); |
| 75 | continue; |
| 76 | } else if (r == -EINTR) |
| 77 | continue; |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 78 | else if (r != 0) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 79 | break; |
| 80 | } while (1); |
| 81 | |
Jens Axboe | 22819ec | 2007-02-18 07:47:14 +0100 | [diff] [blame] | 82 | return r; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u) |
| 86 | { |
| 87 | struct libaio_data *ld = td->io_ops->data; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 88 | |
Jens Axboe | 7101d9c | 2007-09-12 13:12:39 +0200 | [diff] [blame] | 89 | fio_ro_check(td, io_u); |
| 90 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 91 | if (ld->iocbs_nr == (int) td->o.iodepth) |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 92 | return FIO_Q_BUSY; |
| 93 | |
| 94 | /* |
| 95 | * fsync is tricky, since it can fail and we need to do it |
| 96 | * serialized with other io. the reason is that linux doesn't |
| 97 | * support aio fsync yet. So return busy for the case where we |
| 98 | * have pending io, to let fio complete those first. |
| 99 | */ |
| 100 | if (io_u->ddir == DDIR_SYNC) { |
| 101 | if (ld->iocbs_nr) |
| 102 | return FIO_Q_BUSY; |
| 103 | if (fsync(io_u->file->fd) < 0) |
| 104 | io_u->error = errno; |
| 105 | |
| 106 | return FIO_Q_COMPLETED; |
| 107 | } |
| 108 | |
| 109 | ld->iocbs[ld->iocbs_nr] = &io_u->iocb; |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 110 | ld->io_us[ld->iocbs_nr] = io_u; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 111 | ld->iocbs_nr++; |
| 112 | return FIO_Q_QUEUED; |
| 113 | } |
| 114 | |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 115 | static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us, |
| 116 | unsigned int nr) |
| 117 | { |
| 118 | struct timeval now; |
| 119 | unsigned int i; |
| 120 | |
| 121 | fio_gettime(&now, NULL); |
| 122 | |
| 123 | for (i = 0; i < nr; i++) { |
| 124 | struct io_u *io_u = io_us[i]; |
| 125 | |
| 126 | memcpy(&io_u->issue_time, &now, sizeof(now)); |
| 127 | io_u_queued(td, io_u); |
| 128 | } |
| 129 | } |
| 130 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 131 | static int fio_libaio_commit(struct thread_data *td) |
| 132 | { |
| 133 | struct libaio_data *ld = td->io_ops->data; |
| 134 | struct iocb **iocbs; |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 135 | struct io_u **io_us; |
Jens Axboe | 5e00c2c | 2008-01-18 10:26:58 +0100 | [diff] [blame] | 136 | int ret; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 137 | |
| 138 | if (!ld->iocbs_nr) |
| 139 | return 0; |
| 140 | |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 141 | io_us = ld->io_us; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 142 | iocbs = ld->iocbs; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 143 | do { |
Jens Axboe | 5e00c2c | 2008-01-18 10:26:58 +0100 | [diff] [blame] | 144 | ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs); |
| 145 | if (ret > 0) { |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 146 | fio_libaio_queued(td, io_us, ret); |
Jens Axboe | 838bc70 | 2008-05-22 13:08:23 +0200 | [diff] [blame] | 147 | io_u_mark_submit(td, ret); |
Jens Axboe | 5e00c2c | 2008-01-18 10:26:58 +0100 | [diff] [blame] | 148 | ld->iocbs_nr -= ret; |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 149 | io_us += ret; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 150 | iocbs += ret; |
Jens Axboe | 5e00c2c | 2008-01-18 10:26:58 +0100 | [diff] [blame] | 151 | ret = 0; |
Jens Axboe | 838bc70 | 2008-05-22 13:08:23 +0200 | [diff] [blame] | 152 | } else if (!ret || ret == -EAGAIN || ret == -EINTR) { |
| 153 | if (!ret) |
| 154 | io_u_mark_submit(td, ret); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 155 | continue; |
Jens Axboe | 838bc70 | 2008-05-22 13:08:23 +0200 | [diff] [blame] | 156 | } else |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 157 | break; |
Jens Axboe | 5e00c2c | 2008-01-18 10:26:58 +0100 | [diff] [blame] | 158 | } while (ld->iocbs_nr); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 159 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 160 | return ret; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u) |
| 164 | { |
| 165 | struct libaio_data *ld = td->io_ops->data; |
| 166 | |
| 167 | return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events); |
| 168 | } |
| 169 | |
| 170 | static void fio_libaio_cleanup(struct thread_data *td) |
| 171 | { |
| 172 | struct libaio_data *ld = td->io_ops->data; |
| 173 | |
| 174 | if (ld) { |
| 175 | io_destroy(ld->aio_ctx); |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 176 | free(ld->aio_events); |
| 177 | free(ld->iocbs); |
| 178 | free(ld->io_us); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 179 | free(ld); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | static int fio_libaio_init(struct thread_data *td) |
| 184 | { |
| 185 | struct libaio_data *ld = malloc(sizeof(*ld)); |
Jens Axboe | 7d059f8 | 2007-06-18 09:48:57 +0200 | [diff] [blame] | 186 | static int warn_print; |
Jens Axboe | c1db2dc | 2007-03-20 10:42:07 +0100 | [diff] [blame] | 187 | int err; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 188 | |
Jens Axboe | 7d059f8 | 2007-06-18 09:48:57 +0200 | [diff] [blame] | 189 | if (td->o.iodepth > 1 && !td->o.odirect && !warn_print) { |
| 190 | log_info("fio: libaio engine is only async for non-buffered IO\n"); |
| 191 | warn_print = 1; |
| 192 | } |
| 193 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 194 | memset(ld, 0, sizeof(*ld)); |
Jens Axboe | c1db2dc | 2007-03-20 10:42:07 +0100 | [diff] [blame] | 195 | |
| 196 | err = io_queue_init(td->o.iodepth, &ld->aio_ctx); |
| 197 | if (err) { |
| 198 | td_verror(td, -err, "io_queue_init"); |
Jens Axboe | 75de55a | 2008-04-10 20:52:40 +0200 | [diff] [blame] | 199 | log_err("fio: check /proc/sys/fs/aio-max-nr\n"); |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame] | 200 | free(ld); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 201 | return 1; |
| 202 | } |
| 203 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 204 | ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event)); |
| 205 | memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event)); |
| 206 | ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *)); |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 207 | memset(ld->iocbs, 0, sizeof(struct iocb *)); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 208 | ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *)); |
| 209 | memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *)); |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 210 | ld->iocbs_nr = 0; |
| 211 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 212 | td->io_ops->data = ld; |
| 213 | return 0; |
| 214 | } |
| 215 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 216 | static struct ioengine_ops ioengine = { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 217 | .name = "libaio", |
| 218 | .version = FIO_IOOPS_VERSION, |
| 219 | .init = fio_libaio_init, |
| 220 | .prep = fio_libaio_prep, |
| 221 | .queue = fio_libaio_queue, |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 222 | .commit = fio_libaio_commit, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 223 | .cancel = fio_libaio_cancel, |
| 224 | .getevents = fio_libaio_getevents, |
| 225 | .event = fio_libaio_event, |
| 226 | .cleanup = fio_libaio_cleanup, |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 227 | .open_file = generic_open_file, |
| 228 | .close_file = generic_close_file, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 229 | }; |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 230 | |
| 231 | #else /* FIO_HAVE_LIBAIO */ |
| 232 | |
| 233 | /* |
| 234 | * When we have a proper configure system in place, we simply wont build |
| 235 | * and install this io engine. For now install a crippled version that |
| 236 | * just complains and fails to load. |
| 237 | */ |
| 238 | static int fio_libaio_init(struct thread_data fio_unused *td) |
| 239 | { |
| 240 | fprintf(stderr, "fio: libaio not available\n"); |
| 241 | return 1; |
| 242 | } |
| 243 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 244 | static struct ioengine_ops ioengine = { |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 245 | .name = "libaio", |
| 246 | .version = FIO_IOOPS_VERSION, |
| 247 | .init = fio_libaio_init, |
| 248 | }; |
| 249 | |
| 250 | #endif |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 251 | |
| 252 | static void fio_init fio_libaio_register(void) |
| 253 | { |
| 254 | register_ioengine(&ioengine); |
| 255 | } |
| 256 | |
| 257 | static void fio_exit fio_libaio_unregister(void) |
| 258 | { |
| 259 | unregister_ioengine(&ioengine); |
| 260 | } |