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