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