[PATCH] Rename io engines
[fio.git] / engines / sync.c
... / ...
CommitLineData
1/*
2 * regular read/write sync io engine
3 *
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <assert.h>
10
11#include "../fio.h"
12#include "../os.h"
13
14struct syncio_data {
15 struct io_u *last_io_u;
16};
17
18static int fio_syncio_getevents(struct thread_data *td, int fio_unused min,
19 int max, struct timespec fio_unused *t)
20{
21 assert(max <= 1);
22
23 /*
24 * we can only have one finished io_u for sync io, since the depth
25 * is always 1
26 */
27 if (list_empty(&td->io_u_busylist))
28 return 0;
29
30 return 1;
31}
32
33static struct io_u *fio_syncio_event(struct thread_data *td, int event)
34{
35 struct syncio_data *sd = td->io_ops->data;
36
37 assert(event == 0);
38
39 return sd->last_io_u;
40}
41
42static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
43{
44 struct fio_file *f = io_u->file;
45
46 if (io_u->ddir == DDIR_SYNC)
47 return 0;
48 if (io_u->offset == f->last_completed_pos)
49 return 0;
50
51 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
52 td_verror(td, errno);
53 return 1;
54 }
55
56 return 0;
57}
58
59static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
60{
61 struct syncio_data *sd = td->io_ops->data;
62 struct fio_file *f = io_u->file;
63 unsigned int ret;
64
65 if (io_u->ddir == DDIR_READ)
66 ret = read(f->fd, io_u->buf, io_u->buflen);
67 else if (io_u->ddir == DDIR_WRITE)
68 ret = write(f->fd, io_u->buf, io_u->buflen);
69 else
70 ret = fsync(f->fd);
71
72 if (ret != io_u->buflen) {
73 if (ret > 0) {
74 io_u->resid = io_u->buflen - ret;
75 io_u->error = EIO;
76 } else
77 io_u->error = errno;
78 }
79
80 if (!io_u->error)
81 sd->last_io_u = io_u;
82
83 return io_u->error;
84}
85
86static void fio_syncio_cleanup(struct thread_data *td)
87{
88 if (td->io_ops->data) {
89 free(td->io_ops->data);
90 td->io_ops->data = NULL;
91 }
92}
93
94static int fio_syncio_init(struct thread_data *td)
95{
96 struct syncio_data *sd = malloc(sizeof(*sd));
97
98 sd->last_io_u = NULL;
99 td->io_ops->data = sd;
100 return 0;
101}
102
103static struct ioengine_ops ioengine = {
104 .name = "sync",
105 .version = FIO_IOOPS_VERSION,
106 .init = fio_syncio_init,
107 .prep = fio_syncio_prep,
108 .queue = fio_syncio_queue,
109 .getevents = fio_syncio_getevents,
110 .event = fio_syncio_event,
111 .cleanup = fio_syncio_cleanup,
112 .flags = FIO_SYNCIO,
113};
114
115static void fio_init fio_syncio_register(void)
116{
117 register_ioengine(&ioengine);
118}
119
120static void fio_exit fio_syncio_unregister(void)
121{
122 unregister_ioengine(&ioengine);
123}