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