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> |
| 11 | #include <assert.h> |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 12 | |
| 13 | #include "../fio.h" |
| 14 | #include "../os.h" |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 15 | |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 16 | #ifdef FIO_HAVE_POSIXAIO |
| 17 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 18 | struct posixaio_data { |
| 19 | struct io_u **aio_events; |
| 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 | |
| 59 | if (r == 1 || r == AIO_CANCELED) |
| 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; |
| 75 | |
| 76 | io_u->seen = 0; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int fio_posixaio_getevents(struct thread_data *td, int min, int max, |
| 81 | struct timespec *t) |
| 82 | { |
| 83 | struct posixaio_data *pd = td->io_ops->data; |
| 84 | struct list_head *entry; |
| 85 | struct timespec start; |
| 86 | int r, have_timeout = 0; |
| 87 | |
| 88 | if (t && !fill_timespec(&start)) |
| 89 | have_timeout = 1; |
| 90 | |
| 91 | r = 0; |
| 92 | restart: |
| 93 | list_for_each(entry, &td->io_u_busylist) { |
| 94 | struct io_u *io_u = list_entry(entry, struct io_u, list); |
| 95 | int err; |
| 96 | |
| 97 | if (io_u->seen) |
| 98 | continue; |
| 99 | |
| 100 | err = aio_error(&io_u->aiocb); |
Jens Axboe | 3f34431 | 2007-03-14 14:14:48 +0100 | [diff] [blame^] | 101 | if (err == EINPROGRESS) |
| 102 | continue; |
| 103 | |
| 104 | io_u->seen = 1; |
| 105 | pd->aio_events[r++] = io_u; |
| 106 | |
| 107 | if (err == ECANCELED) |
| 108 | io_u->resid = io_u->xfer_buflen; |
| 109 | else if (!err) { |
| 110 | ssize_t retval = aio_return(&io_u->aiocb); |
| 111 | |
| 112 | io_u->resid = io_u->xfer_buflen - retval; |
| 113 | } else |
| 114 | io_u->error = err; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 115 | |
| 116 | if (r >= max) |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | if (r >= min) |
| 121 | return r; |
| 122 | |
| 123 | if (have_timeout) { |
| 124 | unsigned long long usec; |
| 125 | |
| 126 | usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000); |
| 127 | if (ts_utime_since_now(&start) > usec) |
| 128 | return r; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * hrmpf, we need to wait for more. we should use aio_suspend, for |
| 133 | * now just sleep a little and recheck status of busy-and-not-seen |
| 134 | */ |
| 135 | usleep(1000); |
| 136 | goto restart; |
| 137 | } |
| 138 | |
| 139 | static struct io_u *fio_posixaio_event(struct thread_data *td, int event) |
| 140 | { |
| 141 | struct posixaio_data *pd = td->io_ops->data; |
| 142 | |
| 143 | return pd->aio_events[event]; |
| 144 | } |
| 145 | |
| 146 | static int fio_posixaio_queue(struct thread_data fio_unused *td, |
| 147 | struct io_u *io_u) |
| 148 | { |
| 149 | struct aiocb *aiocb = &io_u->aiocb; |
| 150 | int ret; |
| 151 | |
| 152 | if (io_u->ddir == DDIR_READ) |
| 153 | ret = aio_read(aiocb); |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 154 | else if (io_u->ddir == DDIR_WRITE) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 155 | ret = aio_write(aiocb); |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 156 | else |
| 157 | ret = aio_fsync(O_SYNC, aiocb); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 158 | |
Jens Axboe | 95bcd81 | 2007-02-11 01:01:57 +0100 | [diff] [blame] | 159 | if (ret) { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 160 | io_u->error = errno; |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 161 | td_verror(td, io_u->error, "xfer"); |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 162 | return FIO_Q_COMPLETED; |
Jens Axboe | 95bcd81 | 2007-02-11 01:01:57 +0100 | [diff] [blame] | 163 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 164 | |
| 165 | return FIO_Q_QUEUED; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static void fio_posixaio_cleanup(struct thread_data *td) |
| 169 | { |
| 170 | struct posixaio_data *pd = td->io_ops->data; |
| 171 | |
| 172 | if (pd) { |
| 173 | free(pd->aio_events); |
| 174 | free(pd); |
| 175 | td->io_ops->data = NULL; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static int fio_posixaio_init(struct thread_data *td) |
| 180 | { |
| 181 | struct posixaio_data *pd = malloc(sizeof(*pd)); |
| 182 | |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame] | 183 | memset(pd, 0, sizeof(*pd)); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 184 | pd->aio_events = malloc(td->iodepth * sizeof(struct io_u *)); |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame] | 185 | memset(pd->aio_events, 0, td->iodepth * sizeof(struct io_u *)); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 186 | |
| 187 | td->io_ops->data = pd; |
| 188 | return 0; |
| 189 | } |
| 190 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 191 | static struct ioengine_ops ioengine = { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 192 | .name = "posixaio", |
| 193 | .version = FIO_IOOPS_VERSION, |
| 194 | .init = fio_posixaio_init, |
| 195 | .prep = fio_posixaio_prep, |
| 196 | .queue = fio_posixaio_queue, |
| 197 | .cancel = fio_posixaio_cancel, |
| 198 | .getevents = fio_posixaio_getevents, |
| 199 | .event = fio_posixaio_event, |
| 200 | .cleanup = fio_posixaio_cleanup, |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 201 | .open_file = generic_open_file, |
| 202 | .close_file = generic_close_file, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 203 | }; |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 204 | |
| 205 | #else /* FIO_HAVE_POSIXAIO */ |
| 206 | |
| 207 | /* |
| 208 | * When we have a proper configure system in place, we simply wont build |
| 209 | * and install this io engine. For now install a crippled version that |
| 210 | * just complains and fails to load. |
| 211 | */ |
| 212 | static int fio_posixaio_init(struct thread_data fio_unused *td) |
| 213 | { |
| 214 | fprintf(stderr, "fio: posixaio not available\n"); |
| 215 | return 1; |
| 216 | } |
| 217 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 218 | static struct ioengine_ops ioengine = { |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 219 | .name = "posixaio", |
| 220 | .version = FIO_IOOPS_VERSION, |
| 221 | .init = fio_posixaio_init, |
| 222 | }; |
| 223 | |
| 224 | #endif |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 225 | |
| 226 | static void fio_init fio_posixaio_register(void) |
| 227 | { |
| 228 | register_ioengine(&ioengine); |
| 229 | } |
| 230 | |
| 231 | static void fio_exit fio_posixaio_unregister(void) |
| 232 | { |
| 233 | unregister_ioengine(&ioengine); |
| 234 | } |