Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * regular read/write sync io engine |
| 3 | * |
| 4 | */ |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
| 9 | #include <assert.h> |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 10 | |
| 11 | #include "../fio.h" |
| 12 | #include "../os.h" |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 13 | |
| 14 | struct syncio_data { |
| 15 | struct io_u *last_io_u; |
| 16 | }; |
| 17 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 18 | static int fio_syncio_getevents(struct thread_data *td, int fio_unused min, |
| 19 | int max, struct timespec fio_unused *t) |
| 20 | { |
| 21 | assert(max <= 1); |
| 22 | |
| 23 | /* |
| 24 | * we can only have one finished io_u for sync io, since the depth |
| 25 | * is always 1 |
| 26 | */ |
| 27 | if (list_empty(&td->io_u_busylist)) |
| 28 | return 0; |
| 29 | |
| 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | static struct io_u *fio_syncio_event(struct thread_data *td, int event) |
| 34 | { |
| 35 | struct syncio_data *sd = td->io_ops->data; |
| 36 | |
| 37 | assert(event == 0); |
| 38 | |
| 39 | return sd->last_io_u; |
| 40 | } |
| 41 | |
| 42 | static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u) |
| 43 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 44 | struct fio_file *f = io_u->file; |
| 45 | |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 46 | if (io_u->ddir == DDIR_SYNC) |
| 47 | return 0; |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 48 | if (io_u->offset == f->last_completed_pos) |
| 49 | return 0; |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 50 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 51 | if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 52 | td_verror(td, errno); |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u) |
| 60 | { |
| 61 | struct syncio_data *sd = td->io_ops->data; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 62 | struct fio_file *f = io_u->file; |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame^] | 63 | int ret; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 64 | |
| 65 | if (io_u->ddir == DDIR_READ) |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame^] | 66 | ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen); |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 67 | else if (io_u->ddir == DDIR_WRITE) |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame^] | 68 | ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen); |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 69 | else |
| 70 | ret = fsync(f->fd); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 71 | |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame^] | 72 | if (ret != (int) io_u->xfer_buflen) { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 73 | if (ret > 0) { |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame^] | 74 | io_u->resid = io_u->xfer_buflen - ret; |
| 75 | io_u->error = 0; |
| 76 | return ret; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 77 | } else |
| 78 | io_u->error = errno; |
| 79 | } |
| 80 | |
| 81 | if (!io_u->error) |
| 82 | sd->last_io_u = io_u; |
| 83 | |
| 84 | return io_u->error; |
| 85 | } |
| 86 | |
| 87 | static void fio_syncio_cleanup(struct thread_data *td) |
| 88 | { |
| 89 | if (td->io_ops->data) { |
| 90 | free(td->io_ops->data); |
| 91 | td->io_ops->data = NULL; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static int fio_syncio_init(struct thread_data *td) |
| 96 | { |
| 97 | struct syncio_data *sd = malloc(sizeof(*sd)); |
| 98 | |
| 99 | sd->last_io_u = NULL; |
| 100 | td->io_ops->data = sd; |
| 101 | return 0; |
| 102 | } |
| 103 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 104 | static struct ioengine_ops ioengine = { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 105 | .name = "sync", |
| 106 | .version = FIO_IOOPS_VERSION, |
| 107 | .init = fio_syncio_init, |
| 108 | .prep = fio_syncio_prep, |
| 109 | .queue = fio_syncio_queue, |
| 110 | .getevents = fio_syncio_getevents, |
| 111 | .event = fio_syncio_event, |
| 112 | .cleanup = fio_syncio_cleanup, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 113 | .flags = FIO_SYNCIO, |
| 114 | }; |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 115 | |
| 116 | static void fio_init fio_syncio_register(void) |
| 117 | { |
| 118 | register_ioengine(&ioengine); |
| 119 | } |
| 120 | |
| 121 | static void fio_exit fio_syncio_unregister(void) |
| 122 | { |
| 123 | unregister_ioengine(&ioengine); |
| 124 | } |