| 1 | /* |
| 2 | * sync/psync engine |
| 3 | * |
| 4 | * IO engine that does regular read(2)/write(2) with lseek(2) to transfer |
| 5 | * data and IO engine that does regular pread(2)/pwrite(2) to transfer data. |
| 6 | * |
| 7 | */ |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <unistd.h> |
| 11 | #include <sys/uio.h> |
| 12 | #include <errno.h> |
| 13 | #include <assert.h> |
| 14 | |
| 15 | #include "../fio.h" |
| 16 | #include "../optgroup.h" |
| 17 | #include "../lib/rand.h" |
| 18 | |
| 19 | /* |
| 20 | * Sync engine uses engine_data to store last offset |
| 21 | */ |
| 22 | #define LAST_POS(f) ((f)->engine_pos) |
| 23 | |
| 24 | struct syncio_data { |
| 25 | struct iovec *iovecs; |
| 26 | struct io_u **io_us; |
| 27 | unsigned int queued; |
| 28 | unsigned int events; |
| 29 | unsigned long queued_bytes; |
| 30 | |
| 31 | unsigned long long last_offset; |
| 32 | struct fio_file *last_file; |
| 33 | enum fio_ddir last_ddir; |
| 34 | |
| 35 | struct frand_state rand_state; |
| 36 | }; |
| 37 | |
| 38 | #ifdef FIO_HAVE_PWRITEV2 |
| 39 | struct psyncv2_options { |
| 40 | void *pad; |
| 41 | unsigned int hipri; |
| 42 | unsigned int hipri_percentage; |
| 43 | }; |
| 44 | |
| 45 | static struct fio_option options[] = { |
| 46 | { |
| 47 | .name = "hipri", |
| 48 | .lname = "RWF_HIPRI", |
| 49 | .type = FIO_OPT_STR_SET, |
| 50 | .off1 = offsetof(struct psyncv2_options, hipri), |
| 51 | .help = "Set RWF_HIPRI for pwritev2/preadv2", |
| 52 | .category = FIO_OPT_C_ENGINE, |
| 53 | .group = FIO_OPT_G_INVALID, |
| 54 | }, |
| 55 | { |
| 56 | .name = "hipri_percentage", |
| 57 | .lname = "RWF_HIPRI_PERCENTAGE", |
| 58 | .type = FIO_OPT_INT, |
| 59 | .off1 = offsetof(struct psyncv2_options, hipri_percentage), |
| 60 | .minval = 0, |
| 61 | .maxval = 100, |
| 62 | .def = "100", |
| 63 | .help = "Probabilistically set RWF_HIPRI for pwritev2/preadv2", |
| 64 | .category = FIO_OPT_C_ENGINE, |
| 65 | .group = FIO_OPT_G_INVALID, |
| 66 | }, |
| 67 | { |
| 68 | .name = NULL, |
| 69 | }, |
| 70 | }; |
| 71 | #endif |
| 72 | |
| 73 | static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u) |
| 74 | { |
| 75 | struct fio_file *f = io_u->file; |
| 76 | |
| 77 | if (!ddir_rw(io_u->ddir)) |
| 78 | return 0; |
| 79 | |
| 80 | if (LAST_POS(f) != -1ULL && LAST_POS(f) == io_u->offset) |
| 81 | return 0; |
| 82 | |
| 83 | if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) { |
| 84 | td_verror(td, errno, "lseek"); |
| 85 | return 1; |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret) |
| 92 | { |
| 93 | if (io_u->file && ret >= 0 && ddir_rw(io_u->ddir)) |
| 94 | LAST_POS(io_u->file) = io_u->offset + ret; |
| 95 | |
| 96 | if (ret != (int) io_u->xfer_buflen) { |
| 97 | if (ret >= 0) { |
| 98 | io_u->resid = io_u->xfer_buflen - ret; |
| 99 | io_u->error = 0; |
| 100 | return FIO_Q_COMPLETED; |
| 101 | } else |
| 102 | io_u->error = errno; |
| 103 | } |
| 104 | |
| 105 | if (io_u->error) { |
| 106 | io_u_log_error(td, io_u); |
| 107 | td_verror(td, io_u->error, "xfer"); |
| 108 | } |
| 109 | |
| 110 | return FIO_Q_COMPLETED; |
| 111 | } |
| 112 | |
| 113 | #ifdef CONFIG_PWRITEV |
| 114 | static int fio_pvsyncio_queue(struct thread_data *td, struct io_u *io_u) |
| 115 | { |
| 116 | struct syncio_data *sd = td->io_ops_data; |
| 117 | struct iovec *iov = &sd->iovecs[0]; |
| 118 | struct fio_file *f = io_u->file; |
| 119 | int ret; |
| 120 | |
| 121 | fio_ro_check(td, io_u); |
| 122 | |
| 123 | iov->iov_base = io_u->xfer_buf; |
| 124 | iov->iov_len = io_u->xfer_buflen; |
| 125 | |
| 126 | if (io_u->ddir == DDIR_READ) |
| 127 | ret = preadv(f->fd, iov, 1, io_u->offset); |
| 128 | else if (io_u->ddir == DDIR_WRITE) |
| 129 | ret = pwritev(f->fd, iov, 1, io_u->offset); |
| 130 | else if (io_u->ddir == DDIR_TRIM) { |
| 131 | do_io_u_trim(td, io_u); |
| 132 | return FIO_Q_COMPLETED; |
| 133 | } else |
| 134 | ret = do_io_u_sync(td, io_u); |
| 135 | |
| 136 | return fio_io_end(td, io_u, ret); |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | #ifdef FIO_HAVE_PWRITEV2 |
| 141 | static int fio_pvsyncio2_queue(struct thread_data *td, struct io_u *io_u) |
| 142 | { |
| 143 | struct syncio_data *sd = td->io_ops_data; |
| 144 | struct psyncv2_options *o = td->eo; |
| 145 | struct iovec *iov = &sd->iovecs[0]; |
| 146 | struct fio_file *f = io_u->file; |
| 147 | int ret, flags = 0; |
| 148 | |
| 149 | fio_ro_check(td, io_u); |
| 150 | |
| 151 | if (o->hipri && |
| 152 | (rand32_between(&sd->rand_state, 1, 100) <= o->hipri_percentage)) |
| 153 | flags |= RWF_HIPRI; |
| 154 | |
| 155 | iov->iov_base = io_u->xfer_buf; |
| 156 | iov->iov_len = io_u->xfer_buflen; |
| 157 | |
| 158 | if (io_u->ddir == DDIR_READ) |
| 159 | ret = preadv2(f->fd, iov, 1, io_u->offset, flags); |
| 160 | else if (io_u->ddir == DDIR_WRITE) |
| 161 | ret = pwritev2(f->fd, iov, 1, io_u->offset, flags); |
| 162 | else if (io_u->ddir == DDIR_TRIM) { |
| 163 | do_io_u_trim(td, io_u); |
| 164 | return FIO_Q_COMPLETED; |
| 165 | } else |
| 166 | ret = do_io_u_sync(td, io_u); |
| 167 | |
| 168 | return fio_io_end(td, io_u, ret); |
| 169 | } |
| 170 | #endif |
| 171 | |
| 172 | |
| 173 | static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u) |
| 174 | { |
| 175 | struct fio_file *f = io_u->file; |
| 176 | int ret; |
| 177 | |
| 178 | fio_ro_check(td, io_u); |
| 179 | |
| 180 | if (io_u->ddir == DDIR_READ) |
| 181 | ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
| 182 | else if (io_u->ddir == DDIR_WRITE) |
| 183 | ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
| 184 | else if (io_u->ddir == DDIR_TRIM) { |
| 185 | do_io_u_trim(td, io_u); |
| 186 | return FIO_Q_COMPLETED; |
| 187 | } else |
| 188 | ret = do_io_u_sync(td, io_u); |
| 189 | |
| 190 | return fio_io_end(td, io_u, ret); |
| 191 | } |
| 192 | |
| 193 | static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u) |
| 194 | { |
| 195 | struct fio_file *f = io_u->file; |
| 196 | int ret; |
| 197 | |
| 198 | fio_ro_check(td, io_u); |
| 199 | |
| 200 | if (io_u->ddir == DDIR_READ) |
| 201 | ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen); |
| 202 | else if (io_u->ddir == DDIR_WRITE) |
| 203 | ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen); |
| 204 | else if (io_u->ddir == DDIR_TRIM) { |
| 205 | do_io_u_trim(td, io_u); |
| 206 | return FIO_Q_COMPLETED; |
| 207 | } else |
| 208 | ret = do_io_u_sync(td, io_u); |
| 209 | |
| 210 | return fio_io_end(td, io_u, ret); |
| 211 | } |
| 212 | |
| 213 | static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min, |
| 214 | unsigned int max, |
| 215 | const struct timespec fio_unused *t) |
| 216 | { |
| 217 | struct syncio_data *sd = td->io_ops_data; |
| 218 | int ret; |
| 219 | |
| 220 | if (min) { |
| 221 | ret = sd->events; |
| 222 | sd->events = 0; |
| 223 | } else |
| 224 | ret = 0; |
| 225 | |
| 226 | dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret); |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | static struct io_u *fio_vsyncio_event(struct thread_data *td, int event) |
| 231 | { |
| 232 | struct syncio_data *sd = td->io_ops_data; |
| 233 | |
| 234 | return sd->io_us[event]; |
| 235 | } |
| 236 | |
| 237 | static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u) |
| 238 | { |
| 239 | struct syncio_data *sd = td->io_ops_data; |
| 240 | |
| 241 | if (ddir_sync(io_u->ddir)) |
| 242 | return 0; |
| 243 | |
| 244 | if (io_u->offset == sd->last_offset && io_u->file == sd->last_file && |
| 245 | io_u->ddir == sd->last_ddir) |
| 246 | return 1; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u, |
| 252 | int idx) |
| 253 | { |
| 254 | sd->io_us[idx] = io_u; |
| 255 | sd->iovecs[idx].iov_base = io_u->xfer_buf; |
| 256 | sd->iovecs[idx].iov_len = io_u->xfer_buflen; |
| 257 | sd->last_offset = io_u->offset + io_u->xfer_buflen; |
| 258 | sd->last_file = io_u->file; |
| 259 | sd->last_ddir = io_u->ddir; |
| 260 | sd->queued_bytes += io_u->xfer_buflen; |
| 261 | sd->queued++; |
| 262 | } |
| 263 | |
| 264 | static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u) |
| 265 | { |
| 266 | struct syncio_data *sd = td->io_ops_data; |
| 267 | |
| 268 | fio_ro_check(td, io_u); |
| 269 | |
| 270 | if (!fio_vsyncio_append(td, io_u)) { |
| 271 | dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued); |
| 272 | /* |
| 273 | * If we can't append and have stuff queued, tell fio to |
| 274 | * commit those first and then retry this io |
| 275 | */ |
| 276 | if (sd->queued) |
| 277 | return FIO_Q_BUSY; |
| 278 | if (ddir_sync(io_u->ddir)) { |
| 279 | int ret = do_io_u_sync(td, io_u); |
| 280 | |
| 281 | return fio_io_end(td, io_u, ret); |
| 282 | } |
| 283 | |
| 284 | sd->queued = 0; |
| 285 | sd->queued_bytes = 0; |
| 286 | fio_vsyncio_set_iov(sd, io_u, 0); |
| 287 | } else { |
| 288 | if (sd->queued == td->o.iodepth) { |
| 289 | dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued); |
| 290 | return FIO_Q_BUSY; |
| 291 | } |
| 292 | |
| 293 | dprint(FD_IO, "vsyncio_queue: append\n"); |
| 294 | fio_vsyncio_set_iov(sd, io_u, sd->queued); |
| 295 | } |
| 296 | |
| 297 | dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued); |
| 298 | return FIO_Q_QUEUED; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Check that we transferred all bytes, or saw an error, etc |
| 303 | */ |
| 304 | static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes) |
| 305 | { |
| 306 | struct syncio_data *sd = td->io_ops_data; |
| 307 | struct io_u *io_u; |
| 308 | unsigned int i; |
| 309 | int err; |
| 310 | |
| 311 | /* |
| 312 | * transferred everything, perfect |
| 313 | */ |
| 314 | if (bytes == sd->queued_bytes) |
| 315 | return 0; |
| 316 | |
| 317 | err = errno; |
| 318 | for (i = 0; i < sd->queued; i++) { |
| 319 | io_u = sd->io_us[i]; |
| 320 | |
| 321 | if (bytes == -1) { |
| 322 | io_u->error = err; |
| 323 | } else { |
| 324 | unsigned int this_io; |
| 325 | |
| 326 | this_io = bytes; |
| 327 | if (this_io > io_u->xfer_buflen) |
| 328 | this_io = io_u->xfer_buflen; |
| 329 | |
| 330 | io_u->resid = io_u->xfer_buflen - this_io; |
| 331 | io_u->error = 0; |
| 332 | bytes -= this_io; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | if (bytes == -1) { |
| 337 | td_verror(td, err, "xfer vsync"); |
| 338 | return -err; |
| 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | static int fio_vsyncio_commit(struct thread_data *td) |
| 345 | { |
| 346 | struct syncio_data *sd = td->io_ops_data; |
| 347 | struct fio_file *f; |
| 348 | ssize_t ret; |
| 349 | |
| 350 | if (!sd->queued) |
| 351 | return 0; |
| 352 | |
| 353 | io_u_mark_submit(td, sd->queued); |
| 354 | f = sd->last_file; |
| 355 | |
| 356 | if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) { |
| 357 | int err = -errno; |
| 358 | |
| 359 | td_verror(td, errno, "lseek"); |
| 360 | return err; |
| 361 | } |
| 362 | |
| 363 | if (sd->last_ddir == DDIR_READ) |
| 364 | ret = readv(f->fd, sd->iovecs, sd->queued); |
| 365 | else |
| 366 | ret = writev(f->fd, sd->iovecs, sd->queued); |
| 367 | |
| 368 | dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret); |
| 369 | sd->events = sd->queued; |
| 370 | sd->queued = 0; |
| 371 | return fio_vsyncio_end(td, ret); |
| 372 | } |
| 373 | |
| 374 | static int fio_vsyncio_init(struct thread_data *td) |
| 375 | { |
| 376 | struct syncio_data *sd; |
| 377 | |
| 378 | sd = malloc(sizeof(*sd)); |
| 379 | memset(sd, 0, sizeof(*sd)); |
| 380 | sd->last_offset = -1ULL; |
| 381 | sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec)); |
| 382 | sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *)); |
| 383 | init_rand(&sd->rand_state, 0); |
| 384 | |
| 385 | td->io_ops_data = sd; |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static void fio_vsyncio_cleanup(struct thread_data *td) |
| 390 | { |
| 391 | struct syncio_data *sd = td->io_ops_data; |
| 392 | |
| 393 | if (sd) { |
| 394 | free(sd->iovecs); |
| 395 | free(sd->io_us); |
| 396 | free(sd); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | static struct ioengine_ops ioengine_rw = { |
| 401 | .name = "sync", |
| 402 | .version = FIO_IOOPS_VERSION, |
| 403 | .prep = fio_syncio_prep, |
| 404 | .queue = fio_syncio_queue, |
| 405 | .open_file = generic_open_file, |
| 406 | .close_file = generic_close_file, |
| 407 | .get_file_size = generic_get_file_size, |
| 408 | .flags = FIO_SYNCIO, |
| 409 | }; |
| 410 | |
| 411 | static struct ioengine_ops ioengine_prw = { |
| 412 | .name = "psync", |
| 413 | .version = FIO_IOOPS_VERSION, |
| 414 | .queue = fio_psyncio_queue, |
| 415 | .open_file = generic_open_file, |
| 416 | .close_file = generic_close_file, |
| 417 | .get_file_size = generic_get_file_size, |
| 418 | .flags = FIO_SYNCIO, |
| 419 | }; |
| 420 | |
| 421 | static struct ioengine_ops ioengine_vrw = { |
| 422 | .name = "vsync", |
| 423 | .version = FIO_IOOPS_VERSION, |
| 424 | .init = fio_vsyncio_init, |
| 425 | .cleanup = fio_vsyncio_cleanup, |
| 426 | .queue = fio_vsyncio_queue, |
| 427 | .commit = fio_vsyncio_commit, |
| 428 | .event = fio_vsyncio_event, |
| 429 | .getevents = fio_vsyncio_getevents, |
| 430 | .open_file = generic_open_file, |
| 431 | .close_file = generic_close_file, |
| 432 | .get_file_size = generic_get_file_size, |
| 433 | .flags = FIO_SYNCIO, |
| 434 | }; |
| 435 | |
| 436 | #ifdef CONFIG_PWRITEV |
| 437 | static struct ioengine_ops ioengine_pvrw = { |
| 438 | .name = "pvsync", |
| 439 | .version = FIO_IOOPS_VERSION, |
| 440 | .init = fio_vsyncio_init, |
| 441 | .cleanup = fio_vsyncio_cleanup, |
| 442 | .queue = fio_pvsyncio_queue, |
| 443 | .open_file = generic_open_file, |
| 444 | .close_file = generic_close_file, |
| 445 | .get_file_size = generic_get_file_size, |
| 446 | .flags = FIO_SYNCIO, |
| 447 | }; |
| 448 | #endif |
| 449 | |
| 450 | #ifdef FIO_HAVE_PWRITEV2 |
| 451 | static struct ioengine_ops ioengine_pvrw2 = { |
| 452 | .name = "pvsync2", |
| 453 | .version = FIO_IOOPS_VERSION, |
| 454 | .init = fio_vsyncio_init, |
| 455 | .cleanup = fio_vsyncio_cleanup, |
| 456 | .queue = fio_pvsyncio2_queue, |
| 457 | .open_file = generic_open_file, |
| 458 | .close_file = generic_close_file, |
| 459 | .get_file_size = generic_get_file_size, |
| 460 | .flags = FIO_SYNCIO, |
| 461 | .options = options, |
| 462 | .option_struct_size = sizeof(struct psyncv2_options), |
| 463 | }; |
| 464 | #endif |
| 465 | |
| 466 | static void fio_init fio_syncio_register(void) |
| 467 | { |
| 468 | register_ioengine(&ioengine_rw); |
| 469 | register_ioengine(&ioengine_prw); |
| 470 | register_ioengine(&ioengine_vrw); |
| 471 | #ifdef CONFIG_PWRITEV |
| 472 | register_ioengine(&ioengine_pvrw); |
| 473 | #endif |
| 474 | #ifdef FIO_HAVE_PWRITEV2 |
| 475 | register_ioengine(&ioengine_pvrw2); |
| 476 | #endif |
| 477 | } |
| 478 | |
| 479 | static void fio_exit fio_syncio_unregister(void) |
| 480 | { |
| 481 | unregister_ioengine(&ioengine_rw); |
| 482 | unregister_ioengine(&ioengine_prw); |
| 483 | unregister_ioengine(&ioengine_vrw); |
| 484 | #ifdef CONFIG_PWRITEV |
| 485 | unregister_ioengine(&ioengine_pvrw); |
| 486 | #endif |
| 487 | #ifdef FIO_HAVE_PWRITEV2 |
| 488 | unregister_ioengine(&ioengine_pvrw2); |
| 489 | #endif |
| 490 | } |