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