Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1 | /* |
gurudas pai | a31041e | 2007-10-23 15:12:30 +0200 | [diff] [blame] | 2 | * sync/psync engine |
Jens Axboe | da751ca | 2007-03-14 10:59:33 +0100 | [diff] [blame] | 3 | * |
| 4 | * IO engine that does regular read(2)/write(2) with lseek(2) to transfer |
gurudas pai | a31041e | 2007-10-23 15:12:30 +0200 | [diff] [blame] | 5 | * data and IO engine that does regular pread(2)/pwrite(2) to transfer data. |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 6 | * |
| 7 | */ |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <unistd.h> |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 11 | #include <sys/uio.h> |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 12 | #include <errno.h> |
| 13 | #include <assert.h> |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 14 | |
| 15 | #include "../fio.h" |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 16 | |
Jens Axboe | 1d2af02 | 2008-02-04 10:59:07 +0100 | [diff] [blame] | 17 | struct syncio_data { |
| 18 | struct iovec *iovecs; |
| 19 | struct io_u **io_us; |
| 20 | unsigned int queued; |
Jens Axboe | e51cf72 | 2008-05-30 22:03:46 +0200 | [diff] [blame] | 21 | unsigned int events; |
Jens Axboe | 1d2af02 | 2008-02-04 10:59:07 +0100 | [diff] [blame] | 22 | unsigned long queued_bytes; |
| 23 | |
| 24 | unsigned long long last_offset; |
| 25 | struct fio_file *last_file; |
| 26 | enum fio_ddir last_ddir; |
| 27 | }; |
| 28 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 29 | static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u) |
| 30 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 31 | struct fio_file *f = io_u->file; |
| 32 | |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 33 | if (io_u->ddir == DDIR_SYNC) |
| 34 | return 0; |
| 35 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 36 | if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 37 | td_verror(td, errno, "lseek"); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Jens Axboe | 2bd3eab | 2008-02-04 09:35:30 +0100 | [diff] [blame] | 44 | static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 45 | { |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 46 | if (ret != (int) io_u->xfer_buflen) { |
Jens Axboe | 22819ec | 2007-02-18 07:47:14 +0100 | [diff] [blame] | 47 | if (ret >= 0) { |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 48 | io_u->resid = io_u->xfer_buflen - ret; |
| 49 | io_u->error = 0; |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 50 | return FIO_Q_COMPLETED; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 51 | } else |
| 52 | io_u->error = errno; |
| 53 | } |
| 54 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 55 | if (io_u->error) |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 56 | td_verror(td, io_u->error, "xfer"); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 57 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 58 | return FIO_Q_COMPLETED; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Jens Axboe | 2bd3eab | 2008-02-04 09:35:30 +0100 | [diff] [blame] | 61 | static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u) |
gurudas pai | a31041e | 2007-10-23 15:12:30 +0200 | [diff] [blame] | 62 | { |
Jens Axboe | 2bd3eab | 2008-02-04 09:35:30 +0100 | [diff] [blame] | 63 | struct fio_file *f = io_u->file; |
| 64 | int ret; |
| 65 | |
| 66 | fio_ro_check(td, io_u); |
| 67 | |
| 68 | if (io_u->ddir == DDIR_READ) |
| 69 | ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
| 70 | else if (io_u->ddir == DDIR_WRITE) |
| 71 | ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
| 72 | else |
| 73 | ret = fsync(f->fd); |
| 74 | |
| 75 | return fio_io_end(td, io_u, ret); |
| 76 | } |
| 77 | |
| 78 | static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u) |
| 79 | { |
| 80 | struct fio_file *f = io_u->file; |
| 81 | int ret; |
| 82 | |
| 83 | fio_ro_check(td, io_u); |
| 84 | |
| 85 | if (io_u->ddir == DDIR_READ) |
| 86 | ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen); |
| 87 | else if (io_u->ddir == DDIR_WRITE) |
| 88 | ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen); |
| 89 | else |
| 90 | ret = fsync(f->fd); |
| 91 | |
| 92 | return fio_io_end(td, io_u, ret); |
gurudas pai | a31041e | 2007-10-23 15:12:30 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Jens Axboe | 1d2af02 | 2008-02-04 10:59:07 +0100 | [diff] [blame] | 95 | static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min, |
| 96 | unsigned int max, |
| 97 | struct timespec fio_unused *t) |
| 98 | { |
| 99 | struct syncio_data *sd = td->io_ops->data; |
| 100 | int ret; |
| 101 | |
| 102 | if (min) { |
Jens Axboe | e51cf72 | 2008-05-30 22:03:46 +0200 | [diff] [blame] | 103 | ret = sd->events; |
| 104 | sd->events = 0; |
Jens Axboe | 1d2af02 | 2008-02-04 10:59:07 +0100 | [diff] [blame] | 105 | } else |
| 106 | ret = 0; |
| 107 | |
| 108 | dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret); |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | static struct io_u *fio_vsyncio_event(struct thread_data *td, int event) |
| 113 | { |
| 114 | struct syncio_data *sd = td->io_ops->data; |
| 115 | |
| 116 | return sd->io_us[event]; |
| 117 | } |
| 118 | |
| 119 | static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u) |
| 120 | { |
| 121 | struct syncio_data *sd = td->io_ops->data; |
| 122 | |
| 123 | if (io_u->ddir == DDIR_SYNC) |
| 124 | return 0; |
| 125 | |
| 126 | if (io_u->offset == sd->last_offset && io_u->file == sd->last_file && |
| 127 | io_u->ddir == sd->last_ddir) |
| 128 | return 1; |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u, |
| 134 | int index) |
| 135 | { |
| 136 | sd->io_us[index] = io_u; |
| 137 | sd->iovecs[index].iov_base = io_u->xfer_buf; |
| 138 | sd->iovecs[index].iov_len = io_u->xfer_buflen; |
| 139 | sd->last_offset = io_u->offset + io_u->xfer_buflen; |
| 140 | sd->last_file = io_u->file; |
| 141 | sd->last_ddir = io_u->ddir; |
| 142 | sd->queued_bytes += io_u->xfer_buflen; |
| 143 | sd->queued++; |
| 144 | } |
| 145 | |
| 146 | static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u) |
| 147 | { |
| 148 | struct syncio_data *sd = td->io_ops->data; |
| 149 | |
| 150 | fio_ro_check(td, io_u); |
| 151 | |
| 152 | if (!fio_vsyncio_append(td, io_u)) { |
| 153 | dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued); |
| 154 | /* |
| 155 | * If we can't append and have stuff queued, tell fio to |
| 156 | * commit those first and then retry this io |
| 157 | */ |
| 158 | if (sd->queued) |
| 159 | return FIO_Q_BUSY; |
Jens Axboe | cc9159c | 2008-02-04 15:58:24 +0100 | [diff] [blame] | 160 | if (io_u->ddir == DDIR_SYNC) { |
| 161 | int ret = fsync(io_u->file->fd); |
| 162 | |
| 163 | return fio_io_end(td, io_u, ret); |
| 164 | } |
Jens Axboe | 1d2af02 | 2008-02-04 10:59:07 +0100 | [diff] [blame] | 165 | |
| 166 | sd->queued = 0; |
| 167 | sd->queued_bytes = 0; |
| 168 | fio_vsyncio_set_iov(sd, io_u, 0); |
| 169 | } else { |
| 170 | if (sd->queued == td->o.iodepth) { |
| 171 | dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued); |
| 172 | return FIO_Q_BUSY; |
| 173 | } |
| 174 | |
| 175 | dprint(FD_IO, "vsyncio_queue: append\n"); |
| 176 | fio_vsyncio_set_iov(sd, io_u, sd->queued); |
| 177 | } |
| 178 | |
| 179 | dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued); |
| 180 | return FIO_Q_QUEUED; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Check that we transferred all bytes, or saw an error, etc |
| 185 | */ |
| 186 | static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes) |
| 187 | { |
| 188 | struct syncio_data *sd = td->io_ops->data; |
| 189 | struct io_u *io_u; |
| 190 | unsigned int i; |
| 191 | int err; |
| 192 | |
| 193 | /* |
| 194 | * transferred everything, perfect |
| 195 | */ |
| 196 | if (bytes == sd->queued_bytes) |
| 197 | return 0; |
| 198 | |
| 199 | err = errno; |
| 200 | for (i = 0; i < sd->queued; i++) { |
| 201 | io_u = sd->io_us[i]; |
| 202 | |
| 203 | if (bytes == -1) { |
| 204 | io_u->error = err; |
| 205 | } else { |
| 206 | unsigned int this_io; |
| 207 | |
| 208 | this_io = bytes; |
| 209 | if (this_io > io_u->xfer_buflen) |
| 210 | this_io = io_u->xfer_buflen; |
| 211 | |
| 212 | io_u->resid = io_u->xfer_buflen - this_io; |
| 213 | io_u->error = 0; |
| 214 | bytes -= this_io; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (bytes == -1) { |
| 219 | td_verror(td, err, "xfer vsync"); |
| 220 | return -err; |
| 221 | } |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int fio_vsyncio_commit(struct thread_data *td) |
| 227 | { |
| 228 | struct syncio_data *sd = td->io_ops->data; |
| 229 | struct fio_file *f; |
| 230 | ssize_t ret; |
| 231 | |
| 232 | if (!sd->queued) |
| 233 | return 0; |
| 234 | |
Jens Axboe | 838bc70 | 2008-05-22 13:08:23 +0200 | [diff] [blame] | 235 | io_u_mark_submit(td, sd->queued); |
Jens Axboe | 1d2af02 | 2008-02-04 10:59:07 +0100 | [diff] [blame] | 236 | f = sd->last_file; |
| 237 | |
| 238 | if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) { |
| 239 | int err = -errno; |
| 240 | |
| 241 | td_verror(td, errno, "lseek"); |
| 242 | return err; |
| 243 | } |
| 244 | |
| 245 | if (sd->last_ddir == DDIR_READ) |
| 246 | ret = readv(f->fd, sd->iovecs, sd->queued); |
| 247 | else |
| 248 | ret = writev(f->fd, sd->iovecs, sd->queued); |
| 249 | |
| 250 | dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret); |
Jens Axboe | e51cf72 | 2008-05-30 22:03:46 +0200 | [diff] [blame] | 251 | sd->events = sd->queued; |
| 252 | sd->queued = 0; |
Jens Axboe | 1d2af02 | 2008-02-04 10:59:07 +0100 | [diff] [blame] | 253 | return fio_vsyncio_end(td, ret); |
| 254 | } |
| 255 | |
| 256 | static int fio_vsyncio_init(struct thread_data *td) |
| 257 | { |
| 258 | struct syncio_data *sd; |
| 259 | |
| 260 | sd = malloc(sizeof(*sd)); |
| 261 | memset(sd, 0, sizeof(*sd)); |
| 262 | sd->last_offset = -1ULL; |
| 263 | sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec)); |
| 264 | sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *)); |
| 265 | |
| 266 | td->io_ops->data = sd; |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static void fio_vsyncio_cleanup(struct thread_data *td) |
| 271 | { |
| 272 | struct syncio_data *sd = td->io_ops->data; |
| 273 | |
| 274 | free(sd->iovecs); |
| 275 | free(sd->io_us); |
| 276 | free(sd); |
Jens Axboe | 1d2af02 | 2008-02-04 10:59:07 +0100 | [diff] [blame] | 277 | } |
| 278 | |
gurudas pai | a31041e | 2007-10-23 15:12:30 +0200 | [diff] [blame] | 279 | static struct ioengine_ops ioengine_rw = { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 280 | .name = "sync", |
| 281 | .version = FIO_IOOPS_VERSION, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 282 | .prep = fio_syncio_prep, |
| 283 | .queue = fio_syncio_queue, |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 284 | .open_file = generic_open_file, |
| 285 | .close_file = generic_close_file, |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 286 | .get_file_size = generic_get_file_size, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 287 | .flags = FIO_SYNCIO, |
| 288 | }; |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 289 | |
gurudas pai | a31041e | 2007-10-23 15:12:30 +0200 | [diff] [blame] | 290 | static struct ioengine_ops ioengine_prw = { |
| 291 | .name = "psync", |
| 292 | .version = FIO_IOOPS_VERSION, |
Jens Axboe | 2bd3eab | 2008-02-04 09:35:30 +0100 | [diff] [blame] | 293 | .queue = fio_psyncio_queue, |
gurudas pai | a31041e | 2007-10-23 15:12:30 +0200 | [diff] [blame] | 294 | .open_file = generic_open_file, |
| 295 | .close_file = generic_close_file, |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 296 | .get_file_size = generic_get_file_size, |
gurudas pai | a31041e | 2007-10-23 15:12:30 +0200 | [diff] [blame] | 297 | .flags = FIO_SYNCIO, |
| 298 | }; |
| 299 | |
Jens Axboe | 1d2af02 | 2008-02-04 10:59:07 +0100 | [diff] [blame] | 300 | static struct ioengine_ops ioengine_vrw = { |
| 301 | .name = "vsync", |
| 302 | .version = FIO_IOOPS_VERSION, |
| 303 | .init = fio_vsyncio_init, |
| 304 | .cleanup = fio_vsyncio_cleanup, |
| 305 | .queue = fio_vsyncio_queue, |
| 306 | .commit = fio_vsyncio_commit, |
| 307 | .event = fio_vsyncio_event, |
| 308 | .getevents = fio_vsyncio_getevents, |
| 309 | .open_file = generic_open_file, |
| 310 | .close_file = generic_close_file, |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 311 | .get_file_size = generic_get_file_size, |
Jens Axboe | 1d2af02 | 2008-02-04 10:59:07 +0100 | [diff] [blame] | 312 | .flags = FIO_SYNCIO, |
| 313 | }; |
| 314 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 315 | static void fio_init fio_syncio_register(void) |
| 316 | { |
gurudas pai | a31041e | 2007-10-23 15:12:30 +0200 | [diff] [blame] | 317 | register_ioengine(&ioengine_rw); |
| 318 | register_ioengine(&ioengine_prw); |
Jens Axboe | 1d2af02 | 2008-02-04 10:59:07 +0100 | [diff] [blame] | 319 | register_ioengine(&ioengine_vrw); |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void fio_exit fio_syncio_unregister(void) |
| 323 | { |
gurudas pai | a31041e | 2007-10-23 15:12:30 +0200 | [diff] [blame] | 324 | unregister_ioengine(&ioengine_rw); |
| 325 | unregister_ioengine(&ioengine_prw); |
Jens Axboe | 1d2af02 | 2008-02-04 10:59:07 +0100 | [diff] [blame] | 326 | unregister_ioengine(&ioengine_vrw); |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 327 | } |