Add pread/pwrite support to sync engine
[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 <errno.h>
12 #include <assert.h>
13
14 #include "../fio.h"
15
16 #define is_psync(td)    ((td)->io_ops->data == (void *) 1)
17
18 static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
19 {
20         struct fio_file *f = io_u->file;
21
22         if (io_u->ddir == DDIR_SYNC)
23                 return 0;
24         if (io_u->offset == f->last_completed_pos)
25                 return 0;
26
27         if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
28                 td_verror(td, errno, "lseek");
29                 return 1;
30         }
31
32         return 0;
33 }
34
35 static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
36 {
37         struct fio_file *f = io_u->file;
38         int ret;
39
40         fio_ro_check(td, io_u);
41
42         if (io_u->ddir == DDIR_READ) {
43                 if (is_psync(td))
44                         ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen,io_u->offset);
45                 else
46                         ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
47         } else if (io_u->ddir == DDIR_WRITE) {
48                 if (is_psync(td))
49                         ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen,io_u->offset);
50                 else
51                         ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
52         } else
53                 ret = fsync(f->fd);
54
55         if (ret != (int) io_u->xfer_buflen) {
56                 if (ret >= 0) {
57                         io_u->resid = io_u->xfer_buflen - ret;
58                         io_u->error = 0;
59                         return FIO_Q_COMPLETED;
60                 } else
61                         io_u->error = errno;
62         }
63
64         if (io_u->error)
65                 td_verror(td, io_u->error, "xfer");
66
67         return FIO_Q_COMPLETED;
68 }
69
70 static int fio_psyncio_init(struct thread_data *td)
71 {
72         td->io_ops->data = (void *) 1;
73         return 0;
74 }
75
76 static struct ioengine_ops ioengine_rw = {
77         .name           = "sync",
78         .version        = FIO_IOOPS_VERSION,
79         .prep           = fio_syncio_prep,
80         .queue          = fio_syncio_queue,
81         .open_file      = generic_open_file,
82         .close_file     = generic_close_file,
83         .flags          = FIO_SYNCIO,
84 };
85
86 static struct ioengine_ops ioengine_prw = {
87         .name           = "psync",
88         .version        = FIO_IOOPS_VERSION,
89         .queue          = fio_syncio_queue,
90         .init           = fio_psyncio_init,
91         .open_file      = generic_open_file,
92         .close_file     = generic_close_file,
93         .flags          = FIO_SYNCIO,
94 };
95
96 static void fio_init fio_syncio_register(void)
97 {
98         register_ioengine(&ioengine_rw);
99         register_ioengine(&ioengine_prw);
100 }
101
102 static void fio_exit fio_syncio_unregister(void)
103 {
104         unregister_ioengine(&ioengine_rw);
105         unregister_ioengine(&ioengine_prw);
106 }