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