Merge branch 'doc-patches' of https://github.com/vincentkfu/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>
5921e80c 11#include <sys/uio.h>
2866c82d
JA
12#include <errno.h>
13#include <assert.h>
5f350952
JA
14
15#include "../fio.h"
2cafffbe 16#include "../optgroup.h"
a0679ce5 17#include "../lib/rand.h"
2866c82d 18
ef5f5a3a
JA
19/*
20 * Sync engine uses engine_data to store last offset
21 */
710bf9c5 22#define LAST_POS(f) ((f)->engine_pos)
ef5f5a3a 23
1d2af02a
JA
24struct syncio_data {
25 struct iovec *iovecs;
26 struct io_u **io_us;
27 unsigned int queued;
e51cf72c 28 unsigned int events;
1d2af02a
JA
29 unsigned long queued_bytes;
30
31 unsigned long long last_offset;
32 struct fio_file *last_file;
33 enum fio_ddir last_ddir;
a0679ce5
SB
34
35 struct frand_state rand_state;
1d2af02a
JA
36};
37
6562685f 38#ifdef FIO_HAVE_PWRITEV2
2cafffbe
JA
39struct psyncv2_options {
40 void *pad;
41 unsigned int hipri;
a0679ce5 42 unsigned int hipri_percentage;
2cafffbe
JA
43};
44
45static struct fio_option options[] = {
46 {
47 .name = "hipri",
48 .lname = "RWF_HIPRI",
49 .type = FIO_OPT_STR_SET,
50 .off1 = offsetof(struct psyncv2_options, hipri),
51 .help = "Set RWF_HIPRI for pwritev2/preadv2",
52 .category = FIO_OPT_C_ENGINE,
53 .group = FIO_OPT_G_INVALID,
54 },
a0679ce5
SB
55 {
56 .name = "hipri_percentage",
57 .lname = "RWF_HIPRI_PERCENTAGE",
58 .type = FIO_OPT_INT,
59 .off1 = offsetof(struct psyncv2_options, hipri_percentage),
60 .minval = 0,
61 .maxval = 100,
62 .def = "100",
63 .help = "Probabilistically set RWF_HIPRI for pwritev2/preadv2",
64 .category = FIO_OPT_C_ENGINE,
65 .group = FIO_OPT_G_INVALID,
66 },
2cafffbe
JA
67 {
68 .name = NULL,
69 },
70};
71#endif
72
2866c82d
JA
73static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
74{
53cdc686
JA
75 struct fio_file *f = io_u->file;
76
ff58fced 77 if (!ddir_rw(io_u->ddir))
87dc1ab1
JA
78 return 0;
79
ef5f5a3a 80 if (LAST_POS(f) != -1ULL && LAST_POS(f) == io_u->offset)
e943b878
JA
81 return 0;
82
53cdc686 83 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
e1161c32 84 td_verror(td, errno, "lseek");
2866c82d
JA
85 return 1;
86 }
87
88 return 0;
89}
90
2bd3eabc 91static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret)
2866c82d 92{
ff58fced 93 if (io_u->file && ret >= 0 && ddir_rw(io_u->ddir))
ef5f5a3a 94 LAST_POS(io_u->file) = io_u->offset + ret;
e943b878 95
cec6b55d 96 if (ret != (int) io_u->xfer_buflen) {
22819ec2 97 if (ret >= 0) {
cec6b55d
JA
98 io_u->resid = io_u->xfer_buflen - ret;
99 io_u->error = 0;
36167d82 100 return FIO_Q_COMPLETED;
2866c82d
JA
101 } else
102 io_u->error = errno;
103 }
104
bfd197cb
JA
105 if (io_u->error) {
106 io_u_log_error(td, io_u);
e1161c32 107 td_verror(td, io_u->error, "xfer");
bfd197cb 108 }
2866c82d 109
36167d82 110 return FIO_Q_COMPLETED;
2866c82d
JA
111}
112
07fc0acd
JA
113#ifdef CONFIG_PWRITEV
114static int fio_pvsyncio_queue(struct thread_data *td, struct io_u *io_u)
115{
565e784d 116 struct syncio_data *sd = td->io_ops_data;
07fc0acd
JA
117 struct iovec *iov = &sd->iovecs[0];
118 struct fio_file *f = io_u->file;
119 int ret;
120
121 fio_ro_check(td, io_u);
122
123 iov->iov_base = io_u->xfer_buf;
124 iov->iov_len = io_u->xfer_buflen;
125
126 if (io_u->ddir == DDIR_READ)
127 ret = preadv(f->fd, iov, 1, io_u->offset);
128 else if (io_u->ddir == DDIR_WRITE)
129 ret = pwritev(f->fd, iov, 1, io_u->offset);
130 else if (io_u->ddir == DDIR_TRIM) {
131 do_io_u_trim(td, io_u);
132 return FIO_Q_COMPLETED;
133 } else
134 ret = do_io_u_sync(td, io_u);
135
136 return fio_io_end(td, io_u, ret);
137}
138#endif
139
6562685f 140#ifdef FIO_HAVE_PWRITEV2
2cafffbe
JA
141static int fio_pvsyncio2_queue(struct thread_data *td, struct io_u *io_u)
142{
565e784d 143 struct syncio_data *sd = td->io_ops_data;
2cafffbe
JA
144 struct psyncv2_options *o = td->eo;
145 struct iovec *iov = &sd->iovecs[0];
146 struct fio_file *f = io_u->file;
147 int ret, flags = 0;
148
149 fio_ro_check(td, io_u);
150
a0679ce5
SB
151 if (o->hipri &&
152 (rand32_between(&sd->rand_state, 1, 100) <= o->hipri_percentage))
2cafffbe
JA
153 flags |= RWF_HIPRI;
154
155 iov->iov_base = io_u->xfer_buf;
156 iov->iov_len = io_u->xfer_buflen;
157
158 if (io_u->ddir == DDIR_READ)
159 ret = preadv2(f->fd, iov, 1, io_u->offset, flags);
160 else if (io_u->ddir == DDIR_WRITE)
161 ret = pwritev2(f->fd, iov, 1, io_u->offset, flags);
162 else if (io_u->ddir == DDIR_TRIM) {
163 do_io_u_trim(td, io_u);
164 return FIO_Q_COMPLETED;
165 } else
166 ret = do_io_u_sync(td, io_u);
167
168 return fio_io_end(td, io_u, ret);
169}
170#endif
171
172
2bd3eabc 173static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u)
a31041ea 174{
2bd3eabc
JA
175 struct fio_file *f = io_u->file;
176 int ret;
177
178 fio_ro_check(td, io_u);
179
180 if (io_u->ddir == DDIR_READ)
181 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
182 else if (io_u->ddir == DDIR_WRITE)
183 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
6eaf09d6
SL
184 else if (io_u->ddir == DDIR_TRIM) {
185 do_io_u_trim(td, io_u);
186 return FIO_Q_COMPLETED;
187 } else
0a28ecda 188 ret = do_io_u_sync(td, io_u);
2bd3eabc
JA
189
190 return fio_io_end(td, io_u, ret);
191}
192
193static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
194{
195 struct fio_file *f = io_u->file;
196 int ret;
197
198 fio_ro_check(td, io_u);
199
200 if (io_u->ddir == DDIR_READ)
201 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
202 else if (io_u->ddir == DDIR_WRITE)
203 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
6eaf09d6
SL
204 else if (io_u->ddir == DDIR_TRIM) {
205 do_io_u_trim(td, io_u);
206 return FIO_Q_COMPLETED;
207 } else
0a28ecda 208 ret = do_io_u_sync(td, io_u);
2bd3eabc
JA
209
210 return fio_io_end(td, io_u, ret);
a31041ea 211}
212
1d2af02a
JA
213static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
214 unsigned int max,
1f440ece 215 const struct timespec fio_unused *t)
1d2af02a 216{
565e784d 217 struct syncio_data *sd = td->io_ops_data;
1d2af02a
JA
218 int ret;
219
220 if (min) {
e51cf72c
JA
221 ret = sd->events;
222 sd->events = 0;
1d2af02a
JA
223 } else
224 ret = 0;
225
226 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
227 return ret;
228}
229
230static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
231{
565e784d 232 struct syncio_data *sd = td->io_ops_data;
1d2af02a
JA
233
234 return sd->io_us[event];
235}
236
237static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
238{
565e784d 239 struct syncio_data *sd = td->io_ops_data;
1d2af02a 240
5f9099ea 241 if (ddir_sync(io_u->ddir))
1d2af02a
JA
242 return 0;
243
244 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
245 io_u->ddir == sd->last_ddir)
246 return 1;
247
248 return 0;
249}
250
251static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
2b13e716 252 int idx)
1d2af02a 253{
2b13e716
JA
254 sd->io_us[idx] = io_u;
255 sd->iovecs[idx].iov_base = io_u->xfer_buf;
256 sd->iovecs[idx].iov_len = io_u->xfer_buflen;
1d2af02a
JA
257 sd->last_offset = io_u->offset + io_u->xfer_buflen;
258 sd->last_file = io_u->file;
259 sd->last_ddir = io_u->ddir;
260 sd->queued_bytes += io_u->xfer_buflen;
261 sd->queued++;
262}
263
264static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u)
265{
565e784d 266 struct syncio_data *sd = td->io_ops_data;
1d2af02a
JA
267
268 fio_ro_check(td, io_u);
269
270 if (!fio_vsyncio_append(td, io_u)) {
271 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
272 /*
273 * If we can't append and have stuff queued, tell fio to
274 * commit those first and then retry this io
275 */
276 if (sd->queued)
277 return FIO_Q_BUSY;
0a28ecda
JA
278 if (ddir_sync(io_u->ddir)) {
279 int ret = do_io_u_sync(td, io_u);
280
281 return fio_io_end(td, io_u, ret);
282 }
cc9159c3 283
1d2af02a
JA
284 sd->queued = 0;
285 sd->queued_bytes = 0;
286 fio_vsyncio_set_iov(sd, io_u, 0);
287 } else {
288 if (sd->queued == td->o.iodepth) {
289 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
290 return FIO_Q_BUSY;
291 }
292
293 dprint(FD_IO, "vsyncio_queue: append\n");
294 fio_vsyncio_set_iov(sd, io_u, sd->queued);
295 }
296
297 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
298 return FIO_Q_QUEUED;
299}
300
301/*
302 * Check that we transferred all bytes, or saw an error, etc
303 */
304static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
305{
565e784d 306 struct syncio_data *sd = td->io_ops_data;
1d2af02a
JA
307 struct io_u *io_u;
308 unsigned int i;
309 int err;
310
311 /*
312 * transferred everything, perfect
313 */
314 if (bytes == sd->queued_bytes)
315 return 0;
316
317 err = errno;
318 for (i = 0; i < sd->queued; i++) {
319 io_u = sd->io_us[i];
320
321 if (bytes == -1) {
322 io_u->error = err;
323 } else {
324 unsigned int this_io;
325
326 this_io = bytes;
327 if (this_io > io_u->xfer_buflen)
328 this_io = io_u->xfer_buflen;
329
330 io_u->resid = io_u->xfer_buflen - this_io;
331 io_u->error = 0;
332 bytes -= this_io;
333 }
334 }
335
336 if (bytes == -1) {
337 td_verror(td, err, "xfer vsync");
338 return -err;
339 }
340
341 return 0;
342}
343
344static int fio_vsyncio_commit(struct thread_data *td)
345{
565e784d 346 struct syncio_data *sd = td->io_ops_data;
1d2af02a
JA
347 struct fio_file *f;
348 ssize_t ret;
349
350 if (!sd->queued)
351 return 0;
352
838bc709 353 io_u_mark_submit(td, sd->queued);
1d2af02a
JA
354 f = sd->last_file;
355
356 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
357 int err = -errno;
358
359 td_verror(td, errno, "lseek");
360 return err;
361 }
362
363 if (sd->last_ddir == DDIR_READ)
364 ret = readv(f->fd, sd->iovecs, sd->queued);
365 else
366 ret = writev(f->fd, sd->iovecs, sd->queued);
367
368 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
e51cf72c
JA
369 sd->events = sd->queued;
370 sd->queued = 0;
1d2af02a
JA
371 return fio_vsyncio_end(td, ret);
372}
373
374static int fio_vsyncio_init(struct thread_data *td)
375{
376 struct syncio_data *sd;
377
378 sd = malloc(sizeof(*sd));
379 memset(sd, 0, sizeof(*sd));
380 sd->last_offset = -1ULL;
381 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
382 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
a0679ce5 383 init_rand(&sd->rand_state, 0);
1d2af02a 384
565e784d 385 td->io_ops_data = sd;
1d2af02a
JA
386 return 0;
387}
388
389static void fio_vsyncio_cleanup(struct thread_data *td)
390{
565e784d 391 struct syncio_data *sd = td->io_ops_data;
1d2af02a 392
02a3d83f
TK
393 if (sd) {
394 free(sd->iovecs);
395 free(sd->io_us);
396 free(sd);
397 }
1d2af02a
JA
398}
399
a31041ea 400static struct ioengine_ops ioengine_rw = {
2866c82d
JA
401 .name = "sync",
402 .version = FIO_IOOPS_VERSION,
2866c82d
JA
403 .prep = fio_syncio_prep,
404 .queue = fio_syncio_queue,
b5af8293
JA
405 .open_file = generic_open_file,
406 .close_file = generic_close_file,
df9c26b1 407 .get_file_size = generic_get_file_size,
2866c82d
JA
408 .flags = FIO_SYNCIO,
409};
5f350952 410
a31041ea 411static struct ioengine_ops ioengine_prw = {
412 .name = "psync",
413 .version = FIO_IOOPS_VERSION,
2bd3eabc 414 .queue = fio_psyncio_queue,
a31041ea 415 .open_file = generic_open_file,
416 .close_file = generic_close_file,
df9c26b1 417 .get_file_size = generic_get_file_size,
a31041ea 418 .flags = FIO_SYNCIO,
419};
420
1d2af02a
JA
421static struct ioengine_ops ioengine_vrw = {
422 .name = "vsync",
423 .version = FIO_IOOPS_VERSION,
424 .init = fio_vsyncio_init,
425 .cleanup = fio_vsyncio_cleanup,
426 .queue = fio_vsyncio_queue,
427 .commit = fio_vsyncio_commit,
428 .event = fio_vsyncio_event,
429 .getevents = fio_vsyncio_getevents,
430 .open_file = generic_open_file,
431 .close_file = generic_close_file,
df9c26b1 432 .get_file_size = generic_get_file_size,
1d2af02a
JA
433 .flags = FIO_SYNCIO,
434};
435
07fc0acd
JA
436#ifdef CONFIG_PWRITEV
437static struct ioengine_ops ioengine_pvrw = {
438 .name = "pvsync",
439 .version = FIO_IOOPS_VERSION,
440 .init = fio_vsyncio_init,
441 .cleanup = fio_vsyncio_cleanup,
442 .queue = fio_pvsyncio_queue,
443 .open_file = generic_open_file,
444 .close_file = generic_close_file,
445 .get_file_size = generic_get_file_size,
446 .flags = FIO_SYNCIO,
447};
448#endif
449
6562685f 450#ifdef FIO_HAVE_PWRITEV2
2cafffbe
JA
451static struct ioengine_ops ioengine_pvrw2 = {
452 .name = "pvsync2",
453 .version = FIO_IOOPS_VERSION,
454 .init = fio_vsyncio_init,
455 .cleanup = fio_vsyncio_cleanup,
456 .queue = fio_pvsyncio2_queue,
457 .open_file = generic_open_file,
458 .close_file = generic_close_file,
459 .get_file_size = generic_get_file_size,
460 .flags = FIO_SYNCIO,
461 .options = options,
462 .option_struct_size = sizeof(struct psyncv2_options),
463};
464#endif
465
5f350952
JA
466static void fio_init fio_syncio_register(void)
467{
a31041ea 468 register_ioengine(&ioengine_rw);
469 register_ioengine(&ioengine_prw);
1d2af02a 470 register_ioengine(&ioengine_vrw);
9a0ced5a 471#ifdef CONFIG_PWRITEV
07fc0acd 472 register_ioengine(&ioengine_pvrw);
9a0ced5a 473#endif
6562685f 474#ifdef FIO_HAVE_PWRITEV2
526c403d
JD
475 register_ioengine(&ioengine_pvrw2);
476#endif
5f350952
JA
477}
478
479static void fio_exit fio_syncio_unregister(void)
480{
a31041ea 481 unregister_ioengine(&ioengine_rw);
482 unregister_ioengine(&ioengine_prw);
1d2af02a 483 unregister_ioengine(&ioengine_vrw);
9a0ced5a 484#ifdef CONFIG_PWRITEV
07fc0acd 485 unregister_ioengine(&ioengine_pvrw);
9a0ced5a 486#endif
6562685f 487#ifdef FIO_HAVE_PWRITEV2
526c403d
JD
488 unregister_ioengine(&ioengine_pvrw2);
489#endif
5f350952 490}