blob: e966846cd34c3d560b3c03e2177e42d1a65cb0dc [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
gurudas paia31041e2007-10-23 15:12:30 +02002 * sync/psync engine
Jens Axboeda751ca2007-03-14 10:59:33 +01003 *
4 * IO engine that does regular read(2)/write(2) with lseek(2) to transfer
gurudas paia31041e2007-10-23 15:12:30 +02005 * data and IO engine that does regular pread(2)/pwrite(2) to transfer 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"
Jens Axboe2866c822006-10-09 15:57:48 +020015
Jens Axboe1d2af022008-02-04 10:59:07 +010016struct syncio_data {
17 struct iovec *iovecs;
18 struct io_u **io_us;
19 unsigned int queued;
20 unsigned long queued_bytes;
21
22 unsigned long long last_offset;
23 struct fio_file *last_file;
24 enum fio_ddir last_ddir;
25};
26
Jens Axboe2866c822006-10-09 15:57:48 +020027static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
28{
Jens Axboe53cdc682006-10-18 11:50:58 +020029 struct fio_file *f = io_u->file;
30
Jens Axboe87dc1ab2006-10-24 14:41:26 +020031 if (io_u->ddir == DDIR_SYNC)
32 return 0;
33
Jens Axboe53cdc682006-10-18 11:50:58 +020034 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010035 td_verror(td, errno, "lseek");
Jens Axboe2866c822006-10-09 15:57:48 +020036 return 1;
37 }
38
39 return 0;
40}
41
Jens Axboe2bd3eab2008-02-04 09:35:30 +010042static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret)
Jens Axboe2866c822006-10-09 15:57:48 +020043{
Jens Axboecec6b552007-02-06 20:15:38 +010044 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +010045 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +010046 io_u->resid = io_u->xfer_buflen - ret;
47 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +010048 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +020049 } else
50 io_u->error = errno;
51 }
52
Jens Axboe36167d82007-02-18 05:41:31 +010053 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +010054 td_verror(td, io_u->error, "xfer");
Jens Axboe2866c822006-10-09 15:57:48 +020055
Jens Axboe36167d82007-02-18 05:41:31 +010056 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +020057}
58
Jens Axboe2bd3eab2008-02-04 09:35:30 +010059static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u)
gurudas paia31041e2007-10-23 15:12:30 +020060{
Jens Axboe2bd3eab2008-02-04 09:35:30 +010061 struct fio_file *f = io_u->file;
62 int ret;
63
64 fio_ro_check(td, io_u);
65
66 if (io_u->ddir == DDIR_READ)
67 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
68 else if (io_u->ddir == DDIR_WRITE)
69 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
70 else
71 ret = fsync(f->fd);
72
73 return fio_io_end(td, io_u, ret);
74}
75
76static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
77{
78 struct fio_file *f = io_u->file;
79 int ret;
80
81 fio_ro_check(td, io_u);
82
83 if (io_u->ddir == DDIR_READ)
84 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
85 else if (io_u->ddir == DDIR_WRITE)
86 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
87 else
88 ret = fsync(f->fd);
89
90 return fio_io_end(td, io_u, ret);
gurudas paia31041e2007-10-23 15:12:30 +020091}
92
Jens Axboe1d2af022008-02-04 10:59:07 +010093static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
94 unsigned int max,
95 struct timespec fio_unused *t)
96{
97 struct syncio_data *sd = td->io_ops->data;
98 int ret;
99
100 if (min) {
101 ret = sd->queued;
102 sd->queued = 0;
103 } else
104 ret = 0;
105
106 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
107 return ret;
108}
109
110static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
111{
112 struct syncio_data *sd = td->io_ops->data;
113
114 return sd->io_us[event];
115}
116
117static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
118{
119 struct syncio_data *sd = td->io_ops->data;
120
121 if (io_u->ddir == DDIR_SYNC)
122 return 0;
123
124 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
125 io_u->ddir == sd->last_ddir)
126 return 1;
127
128 return 0;
129}
130
131static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
132 int index)
133{
134 sd->io_us[index] = io_u;
135 sd->iovecs[index].iov_base = io_u->xfer_buf;
136 sd->iovecs[index].iov_len = io_u->xfer_buflen;
137 sd->last_offset = io_u->offset + io_u->xfer_buflen;
138 sd->last_file = io_u->file;
139 sd->last_ddir = io_u->ddir;
140 sd->queued_bytes += io_u->xfer_buflen;
141 sd->queued++;
142}
143
144static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u)
145{
146 struct syncio_data *sd = td->io_ops->data;
147
148 fio_ro_check(td, io_u);
149
150 if (!fio_vsyncio_append(td, io_u)) {
151 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
152 /*
153 * If we can't append and have stuff queued, tell fio to
154 * commit those first and then retry this io
155 */
156 if (sd->queued)
157 return FIO_Q_BUSY;
Jens Axboecc9159c2008-02-04 15:58:24 +0100158 if (io_u->ddir == DDIR_SYNC) {
159 int ret = fsync(io_u->file->fd);
160
161 return fio_io_end(td, io_u, ret);
162 }
Jens Axboe1d2af022008-02-04 10:59:07 +0100163
164 sd->queued = 0;
165 sd->queued_bytes = 0;
166 fio_vsyncio_set_iov(sd, io_u, 0);
167 } else {
168 if (sd->queued == td->o.iodepth) {
169 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
170 return FIO_Q_BUSY;
171 }
172
173 dprint(FD_IO, "vsyncio_queue: append\n");
174 fio_vsyncio_set_iov(sd, io_u, sd->queued);
175 }
176
177 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
178 return FIO_Q_QUEUED;
179}
180
181/*
182 * Check that we transferred all bytes, or saw an error, etc
183 */
184static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
185{
186 struct syncio_data *sd = td->io_ops->data;
187 struct io_u *io_u;
188 unsigned int i;
189 int err;
190
191 /*
192 * transferred everything, perfect
193 */
194 if (bytes == sd->queued_bytes)
195 return 0;
196
197 err = errno;
198 for (i = 0; i < sd->queued; i++) {
199 io_u = sd->io_us[i];
200
201 if (bytes == -1) {
202 io_u->error = err;
203 } else {
204 unsigned int this_io;
205
206 this_io = bytes;
207 if (this_io > io_u->xfer_buflen)
208 this_io = io_u->xfer_buflen;
209
210 io_u->resid = io_u->xfer_buflen - this_io;
211 io_u->error = 0;
212 bytes -= this_io;
213 }
214 }
215
216 if (bytes == -1) {
217 td_verror(td, err, "xfer vsync");
218 return -err;
219 }
220
221 return 0;
222}
223
224static int fio_vsyncio_commit(struct thread_data *td)
225{
226 struct syncio_data *sd = td->io_ops->data;
227 struct fio_file *f;
228 ssize_t ret;
229
230 if (!sd->queued)
231 return 0;
232
Jens Axboe838bc702008-05-22 13:08:23 +0200233 io_u_mark_submit(td, sd->queued);
Jens Axboe1d2af022008-02-04 10:59:07 +0100234 f = sd->last_file;
235
236 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
237 int err = -errno;
238
239 td_verror(td, errno, "lseek");
240 return err;
241 }
242
243 if (sd->last_ddir == DDIR_READ)
244 ret = readv(f->fd, sd->iovecs, sd->queued);
245 else
246 ret = writev(f->fd, sd->iovecs, sd->queued);
247
248 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
249 return fio_vsyncio_end(td, ret);
250}
251
252static int fio_vsyncio_init(struct thread_data *td)
253{
254 struct syncio_data *sd;
255
256 sd = malloc(sizeof(*sd));
257 memset(sd, 0, sizeof(*sd));
258 sd->last_offset = -1ULL;
259 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
260 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
261
262 td->io_ops->data = sd;
263 return 0;
264}
265
266static void fio_vsyncio_cleanup(struct thread_data *td)
267{
268 struct syncio_data *sd = td->io_ops->data;
269
270 free(sd->iovecs);
271 free(sd->io_us);
272 free(sd);
273 td->io_ops->data = NULL;
274}
275
gurudas paia31041e2007-10-23 15:12:30 +0200276static struct ioengine_ops ioengine_rw = {
Jens Axboe2866c822006-10-09 15:57:48 +0200277 .name = "sync",
278 .version = FIO_IOOPS_VERSION,
Jens Axboe2866c822006-10-09 15:57:48 +0200279 .prep = fio_syncio_prep,
280 .queue = fio_syncio_queue,
Jens Axboeb5af8292007-03-08 12:43:13 +0100281 .open_file = generic_open_file,
282 .close_file = generic_close_file,
Jens Axboe2866c822006-10-09 15:57:48 +0200283 .flags = FIO_SYNCIO,
284};
Jens Axboe5f350952006-11-07 15:20:59 +0100285
gurudas paia31041e2007-10-23 15:12:30 +0200286static struct ioengine_ops ioengine_prw = {
287 .name = "psync",
288 .version = FIO_IOOPS_VERSION,
Jens Axboe2bd3eab2008-02-04 09:35:30 +0100289 .queue = fio_psyncio_queue,
gurudas paia31041e2007-10-23 15:12:30 +0200290 .open_file = generic_open_file,
291 .close_file = generic_close_file,
292 .flags = FIO_SYNCIO,
293};
294
Jens Axboe1d2af022008-02-04 10:59:07 +0100295static struct ioengine_ops ioengine_vrw = {
296 .name = "vsync",
297 .version = FIO_IOOPS_VERSION,
298 .init = fio_vsyncio_init,
299 .cleanup = fio_vsyncio_cleanup,
300 .queue = fio_vsyncio_queue,
301 .commit = fio_vsyncio_commit,
302 .event = fio_vsyncio_event,
303 .getevents = fio_vsyncio_getevents,
304 .open_file = generic_open_file,
305 .close_file = generic_close_file,
306 .flags = FIO_SYNCIO,
307};
308
Jens Axboe5f350952006-11-07 15:20:59 +0100309static void fio_init fio_syncio_register(void)
310{
gurudas paia31041e2007-10-23 15:12:30 +0200311 register_ioengine(&ioengine_rw);
312 register_ioengine(&ioengine_prw);
Jens Axboe1d2af022008-02-04 10:59:07 +0100313 register_ioengine(&ioengine_vrw);
Jens Axboe5f350952006-11-07 15:20:59 +0100314}
315
316static void fio_exit fio_syncio_unregister(void)
317{
gurudas paia31041e2007-10-23 15:12:30 +0200318 unregister_ioengine(&ioengine_rw);
319 unregister_ioengine(&ioengine_prw);
Jens Axboe1d2af022008-02-04 10:59:07 +0100320 unregister_ioengine(&ioengine_vrw);
Jens Axboe5f350952006-11-07 15:20:59 +0100321}