blob: ad02e09464f1935de33dc4a726ecef4edc3a8db8 [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
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>
Jens Axboe5f350952006-11-07 15:20:59 +010010
11#include "../fio.h"
12#include "../os.h"
Jens Axboe2866c822006-10-09 15:57:48 +020013
Jens Axboe2866c822006-10-09 15:57:48 +020014static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
15{
Jens Axboe53cdc682006-10-18 11:50:58 +020016 struct fio_file *f = io_u->file;
17
Jens Axboe87dc1ab2006-10-24 14:41:26 +020018 if (io_u->ddir == DDIR_SYNC)
19 return 0;
Jens Axboe02bcaa82006-11-24 10:42:00 +010020 if (io_u->offset == f->last_completed_pos)
21 return 0;
Jens Axboe87dc1ab2006-10-24 14:41:26 +020022
Jens Axboe53cdc682006-10-18 11:50:58 +020023 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010024 td_verror(td, errno, "lseek");
Jens Axboe2866c822006-10-09 15:57:48 +020025 return 1;
26 }
27
28 return 0;
29}
30
31static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
32{
Jens Axboe53cdc682006-10-18 11:50:58 +020033 struct fio_file *f = io_u->file;
Jens Axboecec6b552007-02-06 20:15:38 +010034 int ret;
Jens Axboe2866c822006-10-09 15:57:48 +020035
36 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010037 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
Jens Axboe87dc1ab2006-10-24 14:41:26 +020038 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010039 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
Jens Axboe87dc1ab2006-10-24 14:41:26 +020040 else
41 ret = fsync(f->fd);
Jens Axboe2866c822006-10-09 15:57:48 +020042
Jens Axboecec6b552007-02-06 20:15:38 +010043 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +010044 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +010045 io_u->resid = io_u->xfer_buflen - ret;
46 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +010047 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +020048 } else
49 io_u->error = errno;
50 }
51
Jens Axboe36167d82007-02-18 05:41:31 +010052 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +010053 td_verror(td, io_u->error, "xfer");
Jens Axboe2866c822006-10-09 15:57:48 +020054
Jens Axboe36167d82007-02-18 05:41:31 +010055 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +020056}
57
Jens Axboe5f350952006-11-07 15:20:59 +010058static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +020059 .name = "sync",
60 .version = FIO_IOOPS_VERSION,
Jens Axboe2866c822006-10-09 15:57:48 +020061 .prep = fio_syncio_prep,
62 .queue = fio_syncio_queue,
Jens Axboe2866c822006-10-09 15:57:48 +020063 .flags = FIO_SYNCIO,
64};
Jens Axboe5f350952006-11-07 15:20:59 +010065
66static void fio_init fio_syncio_register(void)
67{
68 register_ioengine(&ioengine);
69}
70
71static void fio_exit fio_syncio_unregister(void)
72{
73 unregister_ioengine(&ioengine);
74}