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