blob: a137282786e8759f0c175230a2c45206a814235d [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
Jens Axboeda751ca2007-03-14 10:59:33 +01002 * sync engine
3 *
4 * IO engine that does regular read(2)/write(2) with lseek(2) to transfer
5 * data.
Jens Axboe2866c822006-10-09 15:57:48 +02006 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
12#include <assert.h>
Jens Axboe5f350952006-11-07 15:20:59 +010013
14#include "../fio.h"
15#include "../os.h"
Jens Axboe2866c822006-10-09 15:57:48 +020016
Jens Axboe2866c822006-10-09 15:57:48 +020017static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
18{
Jens Axboe53cdc682006-10-18 11:50:58 +020019 struct fio_file *f = io_u->file;
20
Jens Axboe87dc1ab2006-10-24 14:41:26 +020021 if (io_u->ddir == DDIR_SYNC)
22 return 0;
Jens Axboe02bcaa82006-11-24 10:42:00 +010023 if (io_u->offset == f->last_completed_pos)
24 return 0;
Jens Axboe87dc1ab2006-10-24 14:41:26 +020025
Jens Axboe53cdc682006-10-18 11:50:58 +020026 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010027 td_verror(td, errno, "lseek");
Jens Axboe2866c822006-10-09 15:57:48 +020028 return 1;
29 }
30
31 return 0;
32}
33
34static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
35{
Jens Axboe53cdc682006-10-18 11:50:58 +020036 struct fio_file *f = io_u->file;
Jens Axboecec6b552007-02-06 20:15:38 +010037 int ret;
Jens Axboe2866c822006-10-09 15:57:48 +020038
39 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010040 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
Jens Axboe87dc1ab2006-10-24 14:41:26 +020041 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010042 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
Jens Axboe87dc1ab2006-10-24 14:41:26 +020043 else
44 ret = fsync(f->fd);
Jens Axboe2866c822006-10-09 15:57:48 +020045
Jens Axboecec6b552007-02-06 20:15:38 +010046 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +010047 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +010048 io_u->resid = io_u->xfer_buflen - ret;
49 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +010050 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +020051 } else
52 io_u->error = errno;
53 }
54
Jens Axboe36167d82007-02-18 05:41:31 +010055 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +010056 td_verror(td, io_u->error, "xfer");
Jens Axboe2866c822006-10-09 15:57:48 +020057
Jens Axboe36167d82007-02-18 05:41:31 +010058 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +020059}
60
Jens Axboe5f350952006-11-07 15:20:59 +010061static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +020062 .name = "sync",
63 .version = FIO_IOOPS_VERSION,
Jens Axboe2866c822006-10-09 15:57:48 +020064 .prep = fio_syncio_prep,
65 .queue = fio_syncio_queue,
Jens Axboeb5af8292007-03-08 12:43:13 +010066 .open_file = generic_open_file,
67 .close_file = generic_close_file,
Jens Axboe2866c822006-10-09 15:57:48 +020068 .flags = FIO_SYNCIO,
69};
Jens Axboe5f350952006-11-07 15:20:59 +010070
71static void fio_init fio_syncio_register(void)
72{
73 register_ioengine(&ioengine);
74}
75
76static void fio_exit fio_syncio_unregister(void)
77{
78 unregister_ioengine(&ioengine);
79}