[PATCH] First cut at supporting > 1 file per job
[fio.git] / engines / fio-engine-sync.c
... / ...
CommitLineData
1/*
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>
10#include "fio.h"
11#include "os.h"
12
13struct syncio_data {
14 struct io_u *last_io_u;
15};
16
17static int fio_syncio_sync(struct thread_data *td, struct fio_file *f)
18{
19 return fsync(f->fd);
20}
21
22static int fio_syncio_getevents(struct thread_data *td, int fio_unused min,
23 int max, struct timespec fio_unused *t)
24{
25 assert(max <= 1);
26
27 /*
28 * we can only have one finished io_u for sync io, since the depth
29 * is always 1
30 */
31 if (list_empty(&td->io_u_busylist))
32 return 0;
33
34 return 1;
35}
36
37static struct io_u *fio_syncio_event(struct thread_data *td, int event)
38{
39 struct syncio_data *sd = td->io_ops->data;
40
41 assert(event == 0);
42
43 return sd->last_io_u;
44}
45
46static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
47{
48 struct fio_file *f = io_u->file;
49
50 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
51 td_verror(td, errno);
52 return 1;
53 }
54
55 return 0;
56}
57
58static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
59{
60 struct syncio_data *sd = td->io_ops->data;
61 struct fio_file *f = io_u->file;
62 int ret;
63
64 if (io_u->ddir == DDIR_READ)
65 ret = read(f->fd, io_u->buf, io_u->buflen);
66 else
67 ret = write(f->fd, io_u->buf, io_u->buflen);
68
69 if ((unsigned int) ret != io_u->buflen) {
70 if (ret > 0) {
71 io_u->resid = io_u->buflen - ret;
72 io_u->error = EIO;
73 } else
74 io_u->error = errno;
75 }
76
77 if (!io_u->error)
78 sd->last_io_u = io_u;
79
80 return io_u->error;
81}
82
83static void fio_syncio_cleanup(struct thread_data *td)
84{
85 if (td->io_ops->data) {
86 free(td->io_ops->data);
87 td->io_ops->data = NULL;
88 }
89}
90
91static int fio_syncio_init(struct thread_data *td)
92{
93 struct syncio_data *sd = malloc(sizeof(*sd));
94
95 sd->last_io_u = NULL;
96 td->io_ops->data = sd;
97 return 0;
98}
99
100struct ioengine_ops ioengine = {
101 .name = "sync",
102 .version = FIO_IOOPS_VERSION,
103 .init = fio_syncio_init,
104 .prep = fio_syncio_prep,
105 .queue = fio_syncio_queue,
106 .getevents = fio_syncio_getevents,
107 .event = fio_syncio_event,
108 .cleanup = fio_syncio_cleanup,
109 .sync = fio_syncio_sync,
110 .flags = FIO_SYNCIO,
111};