Move os/arch/compiler headers into directories
[fio.git] / engines / sync.c
... / ...
CommitLineData
1/*
2 * sync engine
3 *
4 * IO engine that does regular read(2)/write(2) with lseek(2) to transfer
5 * 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
16static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
17{
18 struct fio_file *f = io_u->file;
19
20 if (io_u->ddir == DDIR_SYNC)
21 return 0;
22 if (io_u->offset == f->last_completed_pos)
23 return 0;
24
25 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
26 td_verror(td, errno, "lseek");
27 return 1;
28 }
29
30 return 0;
31}
32
33static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
34{
35 struct fio_file *f = io_u->file;
36 int ret;
37
38 if (io_u->ddir == DDIR_READ)
39 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
40 else if (io_u->ddir == DDIR_WRITE)
41 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
42 else
43 ret = fsync(f->fd);
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
60static struct ioengine_ops ioengine = {
61 .name = "sync",
62 .version = FIO_IOOPS_VERSION,
63 .prep = fio_syncio_prep,
64 .queue = fio_syncio_queue,
65 .open_file = generic_open_file,
66 .close_file = generic_close_file,
67 .flags = FIO_SYNCIO,
68};
69
70static void fio_init fio_syncio_register(void)
71{
72 register_ioengine(&ioengine);
73}
74
75static void fio_exit fio_syncio_unregister(void)
76{
77 unregister_ioengine(&ioengine);
78}