| 1 | /* |
| 2 | * splice engine |
| 3 | * |
| 4 | * IO engine that transfers data by doing splices to/from pipes and |
| 5 | * the files. |
| 6 | * |
| 7 | */ |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <unistd.h> |
| 11 | #include <errno.h> |
| 12 | #include <assert.h> |
| 13 | #include <sys/poll.h> |
| 14 | |
| 15 | #include "../fio.h" |
| 16 | |
| 17 | #ifdef FIO_HAVE_SPLICE |
| 18 | |
| 19 | struct spliceio_data { |
| 20 | int pipe[2]; |
| 21 | }; |
| 22 | |
| 23 | /* |
| 24 | * For splice reading, we unfortunately cannot (yet) vmsplice the other way. |
| 25 | * So just splice the data from the file into the pipe, and use regular |
| 26 | * read to fill the buffer. Doesn't make a lot of sense, but... |
| 27 | */ |
| 28 | static int fio_splice_read(struct thread_data *td, struct io_u *io_u) |
| 29 | { |
| 30 | struct spliceio_data *sd = td->io_ops->data; |
| 31 | struct fio_file *f = io_u->file; |
| 32 | int ret, ret2, buflen; |
| 33 | off_t offset; |
| 34 | void *p; |
| 35 | |
| 36 | offset = io_u->offset; |
| 37 | buflen = io_u->xfer_buflen; |
| 38 | p = io_u->xfer_buf; |
| 39 | while (buflen) { |
| 40 | int this_len = buflen; |
| 41 | |
| 42 | if (this_len > SPLICE_DEF_SIZE) |
| 43 | this_len = SPLICE_DEF_SIZE; |
| 44 | |
| 45 | ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE); |
| 46 | if (ret < 0) { |
| 47 | if (errno == ENODATA || errno == EAGAIN) |
| 48 | continue; |
| 49 | |
| 50 | return -errno; |
| 51 | } |
| 52 | |
| 53 | buflen -= ret; |
| 54 | |
| 55 | while (ret) { |
| 56 | ret2 = read(sd->pipe[0], p, ret); |
| 57 | if (ret2 < 0) |
| 58 | return -errno; |
| 59 | |
| 60 | ret -= ret2; |
| 61 | p += ret2; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return io_u->xfer_buflen; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * For splice writing, we can vmsplice our data buffer directly into a |
| 70 | * pipe and then splice that to a file. |
| 71 | */ |
| 72 | static int fio_splice_write(struct thread_data *td, struct io_u *io_u) |
| 73 | { |
| 74 | struct spliceio_data *sd = td->io_ops->data; |
| 75 | struct iovec iov[1] = { |
| 76 | { |
| 77 | .iov_base = io_u->xfer_buf, |
| 78 | .iov_len = io_u->xfer_buflen, |
| 79 | } |
| 80 | }; |
| 81 | struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, }; |
| 82 | struct fio_file *f = io_u->file; |
| 83 | off_t off = io_u->offset; |
| 84 | int ret, ret2; |
| 85 | |
| 86 | while (iov[0].iov_len) { |
| 87 | if (poll(&pfd, 1, -1) < 0) |
| 88 | return errno; |
| 89 | |
| 90 | ret = vmsplice(sd->pipe[1], iov, 1, SPLICE_F_NONBLOCK); |
| 91 | if (ret < 0) |
| 92 | return -errno; |
| 93 | |
| 94 | iov[0].iov_len -= ret; |
| 95 | iov[0].iov_base += ret; |
| 96 | |
| 97 | while (ret) { |
| 98 | ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0); |
| 99 | if (ret2 < 0) |
| 100 | return -errno; |
| 101 | |
| 102 | ret -= ret2; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return io_u->xfer_buflen; |
| 107 | } |
| 108 | |
| 109 | static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u) |
| 110 | { |
| 111 | int ret; |
| 112 | |
| 113 | if (io_u->ddir == DDIR_READ) |
| 114 | ret = fio_splice_read(td, io_u); |
| 115 | else if (io_u->ddir == DDIR_WRITE) |
| 116 | ret = fio_splice_write(td, io_u); |
| 117 | else |
| 118 | ret = fsync(io_u->file->fd); |
| 119 | |
| 120 | if (ret != (int) io_u->xfer_buflen) { |
| 121 | if (ret >= 0) { |
| 122 | io_u->resid = io_u->xfer_buflen - ret; |
| 123 | io_u->error = 0; |
| 124 | return FIO_Q_COMPLETED; |
| 125 | } else |
| 126 | io_u->error = errno; |
| 127 | } |
| 128 | |
| 129 | if (io_u->error) |
| 130 | td_verror(td, io_u->error, "xfer"); |
| 131 | |
| 132 | return FIO_Q_COMPLETED; |
| 133 | } |
| 134 | |
| 135 | static void fio_spliceio_cleanup(struct thread_data *td) |
| 136 | { |
| 137 | struct spliceio_data *sd = td->io_ops->data; |
| 138 | |
| 139 | if (sd) { |
| 140 | close(sd->pipe[0]); |
| 141 | close(sd->pipe[1]); |
| 142 | free(sd); |
| 143 | td->io_ops->data = NULL; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static int fio_spliceio_init(struct thread_data *td) |
| 148 | { |
| 149 | struct spliceio_data *sd = malloc(sizeof(*sd)); |
| 150 | |
| 151 | if (pipe(sd->pipe) < 0) { |
| 152 | td_verror(td, errno, "pipe"); |
| 153 | free(sd); |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | td->io_ops->data = sd; |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static struct ioengine_ops ioengine = { |
| 162 | .name = "splice", |
| 163 | .version = FIO_IOOPS_VERSION, |
| 164 | .init = fio_spliceio_init, |
| 165 | .queue = fio_spliceio_queue, |
| 166 | .cleanup = fio_spliceio_cleanup, |
| 167 | .open_file = generic_open_file, |
| 168 | .close_file = generic_close_file, |
| 169 | .flags = FIO_SYNCIO, |
| 170 | }; |
| 171 | |
| 172 | #else /* FIO_HAVE_SPLICE */ |
| 173 | |
| 174 | /* |
| 175 | * When we have a proper configure system in place, we simply wont build |
| 176 | * and install this io engine. For now install a crippled version that |
| 177 | * just complains and fails to load. |
| 178 | */ |
| 179 | static int fio_spliceio_init(struct thread_data fio_unused *td) |
| 180 | { |
| 181 | fprintf(stderr, "fio: splice not available\n"); |
| 182 | return 1; |
| 183 | } |
| 184 | |
| 185 | static struct ioengine_ops ioengine = { |
| 186 | .name = "splice", |
| 187 | .version = FIO_IOOPS_VERSION, |
| 188 | .init = fio_spliceio_init, |
| 189 | }; |
| 190 | |
| 191 | #endif |
| 192 | |
| 193 | static void fio_init fio_spliceio_register(void) |
| 194 | { |
| 195 | register_ioengine(&ioengine); |
| 196 | } |
| 197 | |
| 198 | static void fio_exit fio_spliceio_unregister(void) |
| 199 | { |
| 200 | unregister_ioengine(&ioengine); |
| 201 | } |