Add specific knob for controlling fallocate() usage
[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>
5921e80c 11#include <sys/uio.h>
2866c82d
JA
12#include <errno.h>
13#include <assert.h>
5f350952
JA
14
15#include "../fio.h"
2866c82d 16
1d2af02a
JA
17struct syncio_data {
18 struct iovec *iovecs;
19 struct io_u **io_us;
20 unsigned int queued;
e51cf72c 21 unsigned int events;
1d2af02a
JA
22 unsigned long queued_bytes;
23
24 unsigned long long last_offset;
25 struct fio_file *last_file;
26 enum fio_ddir last_ddir;
27};
28
2866c82d
JA
29static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
30{
53cdc686
JA
31 struct fio_file *f = io_u->file;
32
5f9099ea 33 if (ddir_sync(io_u->ddir))
87dc1ab1
JA
34 return 0;
35
53cdc686 36 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
e1161c32 37 td_verror(td, errno, "lseek");
2866c82d
JA
38 return 1;
39 }
40
41 return 0;
42}
43
2bd3eabc 44static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret)
2866c82d 45{
cec6b55d 46 if (ret != (int) io_u->xfer_buflen) {
22819ec2 47 if (ret >= 0) {
cec6b55d
JA
48 io_u->resid = io_u->xfer_buflen - ret;
49 io_u->error = 0;
36167d82 50 return FIO_Q_COMPLETED;
2866c82d
JA
51 } else
52 io_u->error = errno;
53 }
54
36167d82 55 if (io_u->error)
e1161c32 56 td_verror(td, io_u->error, "xfer");
2866c82d 57
36167d82 58 return FIO_Q_COMPLETED;
2866c82d
JA
59}
60
2bd3eabc 61static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u)
a31041ea 62{
2bd3eabc
JA
63 struct fio_file *f = io_u->file;
64 int ret;
65
66 fio_ro_check(td, io_u);
67
68 if (io_u->ddir == DDIR_READ)
69 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
70 else if (io_u->ddir == DDIR_WRITE)
71 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
72 else
73 ret = fsync(f->fd);
74
75 return fio_io_end(td, io_u, ret);
76}
77
78static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
79{
80 struct fio_file *f = io_u->file;
81 int ret;
82
83 fio_ro_check(td, io_u);
84
85 if (io_u->ddir == DDIR_READ)
86 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
87 else if (io_u->ddir == DDIR_WRITE)
88 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
89 else
90 ret = fsync(f->fd);
91
92 return fio_io_end(td, io_u, ret);
a31041ea 93}
94
1d2af02a
JA
95static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
96 unsigned int max,
97 struct timespec fio_unused *t)
98{
99 struct syncio_data *sd = td->io_ops->data;
100 int ret;
101
102 if (min) {
e51cf72c
JA
103 ret = sd->events;
104 sd->events = 0;
1d2af02a
JA
105 } else
106 ret = 0;
107
108 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
109 return ret;
110}
111
112static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
113{
114 struct syncio_data *sd = td->io_ops->data;
115
116 return sd->io_us[event];
117}
118
119static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
120{
121 struct syncio_data *sd = td->io_ops->data;
122
5f9099ea 123 if (ddir_sync(io_u->ddir))
1d2af02a
JA
124 return 0;
125
126 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
127 io_u->ddir == sd->last_ddir)
128 return 1;
129
130 return 0;
131}
132
133static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
134 int index)
135{
136 sd->io_us[index] = io_u;
137 sd->iovecs[index].iov_base = io_u->xfer_buf;
138 sd->iovecs[index].iov_len = io_u->xfer_buflen;
139 sd->last_offset = io_u->offset + io_u->xfer_buflen;
140 sd->last_file = io_u->file;
141 sd->last_ddir = io_u->ddir;
142 sd->queued_bytes += io_u->xfer_buflen;
143 sd->queued++;
144}
145
146static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u)
147{
148 struct syncio_data *sd = td->io_ops->data;
149
150 fio_ro_check(td, io_u);
151
152 if (!fio_vsyncio_append(td, io_u)) {
153 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
154 /*
155 * If we can't append and have stuff queued, tell fio to
156 * commit those first and then retry this io
157 */
158 if (sd->queued)
159 return FIO_Q_BUSY;
cc9159c3
JA
160 if (io_u->ddir == DDIR_SYNC) {
161 int ret = fsync(io_u->file->fd);
162
163 return fio_io_end(td, io_u, ret);
5f9099ea 164 } else if (io_u->ddir == DDIR_DATASYNC) {
c36d16f5
JA
165 int ret;
166#ifdef FIO_HAVE_FDATASYNC
167 ret = fdatasync(io_u->file->fd);
168#else
169 ret = io_u->xfer_buflen;
170 io_u->error = EINVAL;
171#endif
5f9099ea 172 return fio_io_end(td, io_u, ret);
c36d16f5 173
5f9099ea
JA
174 }
175
1d2af02a
JA
176 sd->queued = 0;
177 sd->queued_bytes = 0;
178 fio_vsyncio_set_iov(sd, io_u, 0);
179 } else {
180 if (sd->queued == td->o.iodepth) {
181 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
182 return FIO_Q_BUSY;
183 }
184
185 dprint(FD_IO, "vsyncio_queue: append\n");
186 fio_vsyncio_set_iov(sd, io_u, sd->queued);
187 }
188
189 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
190 return FIO_Q_QUEUED;
191}
192
193/*
194 * Check that we transferred all bytes, or saw an error, etc
195 */
196static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
197{
198 struct syncio_data *sd = td->io_ops->data;
199 struct io_u *io_u;
200 unsigned int i;
201 int err;
202
203 /*
204 * transferred everything, perfect
205 */
206 if (bytes == sd->queued_bytes)
207 return 0;
208
209 err = errno;
210 for (i = 0; i < sd->queued; i++) {
211 io_u = sd->io_us[i];
212
213 if (bytes == -1) {
214 io_u->error = err;
215 } else {
216 unsigned int this_io;
217
218 this_io = bytes;
219 if (this_io > io_u->xfer_buflen)
220 this_io = io_u->xfer_buflen;
221
222 io_u->resid = io_u->xfer_buflen - this_io;
223 io_u->error = 0;
224 bytes -= this_io;
225 }
226 }
227
228 if (bytes == -1) {
229 td_verror(td, err, "xfer vsync");
230 return -err;
231 }
232
233 return 0;
234}
235
236static int fio_vsyncio_commit(struct thread_data *td)
237{
238 struct syncio_data *sd = td->io_ops->data;
239 struct fio_file *f;
240 ssize_t ret;
241
242 if (!sd->queued)
243 return 0;
244
838bc709 245 io_u_mark_submit(td, sd->queued);
1d2af02a
JA
246 f = sd->last_file;
247
248 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
249 int err = -errno;
250
251 td_verror(td, errno, "lseek");
252 return err;
253 }
254
255 if (sd->last_ddir == DDIR_READ)
256 ret = readv(f->fd, sd->iovecs, sd->queued);
257 else
258 ret = writev(f->fd, sd->iovecs, sd->queued);
259
260 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
e51cf72c
JA
261 sd->events = sd->queued;
262 sd->queued = 0;
1d2af02a
JA
263 return fio_vsyncio_end(td, ret);
264}
265
266static int fio_vsyncio_init(struct thread_data *td)
267{
268 struct syncio_data *sd;
269
270 sd = malloc(sizeof(*sd));
271 memset(sd, 0, sizeof(*sd));
272 sd->last_offset = -1ULL;
273 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
274 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
275
276 td->io_ops->data = sd;
277 return 0;
278}
279
280static void fio_vsyncio_cleanup(struct thread_data *td)
281{
282 struct syncio_data *sd = td->io_ops->data;
283
284 free(sd->iovecs);
285 free(sd->io_us);
286 free(sd);
1d2af02a
JA
287}
288
a31041ea 289static struct ioengine_ops ioengine_rw = {
2866c82d
JA
290 .name = "sync",
291 .version = FIO_IOOPS_VERSION,
2866c82d
JA
292 .prep = fio_syncio_prep,
293 .queue = fio_syncio_queue,
b5af8293
JA
294 .open_file = generic_open_file,
295 .close_file = generic_close_file,
df9c26b1 296 .get_file_size = generic_get_file_size,
2866c82d
JA
297 .flags = FIO_SYNCIO,
298};
5f350952 299
a31041ea 300static struct ioengine_ops ioengine_prw = {
301 .name = "psync",
302 .version = FIO_IOOPS_VERSION,
2bd3eabc 303 .queue = fio_psyncio_queue,
a31041ea 304 .open_file = generic_open_file,
305 .close_file = generic_close_file,
df9c26b1 306 .get_file_size = generic_get_file_size,
a31041ea 307 .flags = FIO_SYNCIO,
308};
309
1d2af02a
JA
310static struct ioengine_ops ioengine_vrw = {
311 .name = "vsync",
312 .version = FIO_IOOPS_VERSION,
313 .init = fio_vsyncio_init,
314 .cleanup = fio_vsyncio_cleanup,
315 .queue = fio_vsyncio_queue,
316 .commit = fio_vsyncio_commit,
317 .event = fio_vsyncio_event,
318 .getevents = fio_vsyncio_getevents,
319 .open_file = generic_open_file,
320 .close_file = generic_close_file,
df9c26b1 321 .get_file_size = generic_get_file_size,
1d2af02a
JA
322 .flags = FIO_SYNCIO,
323};
324
5f350952
JA
325static void fio_init fio_syncio_register(void)
326{
a31041ea 327 register_ioengine(&ioengine_rw);
328 register_ioengine(&ioengine_prw);
1d2af02a 329 register_ioengine(&ioengine_vrw);
5f350952
JA
330}
331
332static void fio_exit fio_syncio_unregister(void)
333{
a31041ea 334 unregister_ioengine(&ioengine_rw);
335 unregister_ioengine(&ioengine_prw);
1d2af02a 336 unregister_ioengine(&ioengine_vrw);
5f350952 337}