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