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