Merge branch 'master' of ssh://git.kernel.dk/data/git/fio
[fio.git] / engines / sync.c
CommitLineData
2866c82d 1/*
a31041ea 2 * sync/psync engine
da751ca9
JA
3 *
4 * IO engine that does regular read(2)/write(2) with lseek(2) to transfer
a31041ea 5 * data and IO engine that does regular pread(2)/pwrite(2) to transfer data.
2866c82d
JA
6 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
12#include <assert.h>
5f350952
JA
13
14#include "../fio.h"
2866c82d 15
a31041ea 16#define is_psync(td) ((td)->io_ops->data == (void *) 1)
17
2866c82d
JA
18static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
19{
53cdc686
JA
20 struct fio_file *f = io_u->file;
21
87dc1ab1
JA
22 if (io_u->ddir == DDIR_SYNC)
23 return 0;
02bcaa8c
JA
24 if (io_u->offset == f->last_completed_pos)
25 return 0;
87dc1ab1 26
53cdc686 27 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
e1161c32 28 td_verror(td, errno, "lseek");
2866c82d
JA
29 return 1;
30 }
31
32 return 0;
33}
34
35static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
36{
53cdc686 37 struct fio_file *f = io_u->file;
cec6b55d 38 int ret;
2866c82d 39
7101d9c2
JA
40 fio_ro_check(td, io_u);
41
a31041ea 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
87dc1ab1 53 ret = fsync(f->fd);
2866c82d 54
cec6b55d 55 if (ret != (int) io_u->xfer_buflen) {
22819ec2 56 if (ret >= 0) {
cec6b55d
JA
57 io_u->resid = io_u->xfer_buflen - ret;
58 io_u->error = 0;
36167d82 59 return FIO_Q_COMPLETED;
2866c82d
JA
60 } else
61 io_u->error = errno;
62 }
63
36167d82 64 if (io_u->error)
e1161c32 65 td_verror(td, io_u->error, "xfer");
2866c82d 66
36167d82 67 return FIO_Q_COMPLETED;
2866c82d
JA
68}
69
a31041ea 70static int fio_psyncio_init(struct thread_data *td)
71{
72 td->io_ops->data = (void *) 1;
73 return 0;
74}
75
76static struct ioengine_ops ioengine_rw = {
2866c82d
JA
77 .name = "sync",
78 .version = FIO_IOOPS_VERSION,
2866c82d
JA
79 .prep = fio_syncio_prep,
80 .queue = fio_syncio_queue,
b5af8293
JA
81 .open_file = generic_open_file,
82 .close_file = generic_close_file,
2866c82d
JA
83 .flags = FIO_SYNCIO,
84};
5f350952 85
a31041ea 86static 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
5f350952
JA
96static void fio_init fio_syncio_register(void)
97{
a31041ea 98 register_ioengine(&ioengine_rw);
99 register_ioengine(&ioengine_prw);
5f350952
JA
100}
101
102static void fio_exit fio_syncio_unregister(void)
103{
a31041ea 104 unregister_ioengine(&ioengine_rw);
105 unregister_ioengine(&ioengine_prw);
5f350952 106}