blob: 7c1cca63c700c4f318d1dd2003cc0fca5d52653f [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>
Jens Axboe5921e802008-05-30 15:02:38 +020011#include <sys/uio.h>
Jens Axboe2866c822006-10-09 15:57:48 +020012#include <errno.h>
13#include <assert.h>
Jens Axboe5f350952006-11-07 15:20:59 +010014
15#include "../fio.h"
Jens Axboe2866c822006-10-09 15:57:48 +020016
Jens Axboe1d2af022008-02-04 10:59:07 +010017struct syncio_data {
18 struct iovec *iovecs;
19 struct io_u **io_us;
20 unsigned int queued;
Jens Axboee51cf722008-05-30 22:03:46 +020021 unsigned int events;
Jens Axboe1d2af022008-02-04 10:59:07 +010022 unsigned long queued_bytes;
23
24 unsigned long long last_offset;
25 struct fio_file *last_file;
26 enum fio_ddir last_ddir;
27};
28
Jens Axboe2866c822006-10-09 15:57:48 +020029static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
30{
Jens Axboe53cdc682006-10-18 11:50:58 +020031 struct fio_file *f = io_u->file;
32
Jens Axboe5f9099e2009-06-16 22:40:26 +020033 if (ddir_sync(io_u->ddir))
Jens Axboe87dc1ab2006-10-24 14:41:26 +020034 return 0;
35
Jens Axboe53cdc682006-10-18 11:50:58 +020036 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +010037 td_verror(td, errno, "lseek");
Jens Axboe2866c822006-10-09 15:57:48 +020038 return 1;
39 }
40
41 return 0;
42}
43
Jens Axboe2bd3eab2008-02-04 09:35:30 +010044static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret)
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 Axboe2bd3eab2008-02-04 09:35:30 +010061static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u)
gurudas paia31041e2007-10-23 15:12:30 +020062{
Jens Axboe2bd3eab2008-02-04 09:35:30 +010063 struct fio_file *f = io_u->file;
64 int ret;
65
66 fio_ro_check(td, io_u);
67
68 if (io_u->ddir == DDIR_READ)
69 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
70 else if (io_u->ddir == DDIR_WRITE)
71 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
72 else
73 ret = fsync(f->fd);
74
75 return fio_io_end(td, io_u, ret);
76}
77
78static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
79{
80 struct fio_file *f = io_u->file;
81 int ret;
82
83 fio_ro_check(td, io_u);
84
85 if (io_u->ddir == DDIR_READ)
86 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
87 else if (io_u->ddir == DDIR_WRITE)
88 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
89 else
90 ret = fsync(f->fd);
91
92 return fio_io_end(td, io_u, ret);
gurudas paia31041e2007-10-23 15:12:30 +020093}
94
Jens Axboe1d2af022008-02-04 10:59:07 +010095static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
96 unsigned int max,
97 struct timespec fio_unused *t)
98{
99 struct syncio_data *sd = td->io_ops->data;
100 int ret;
101
102 if (min) {
Jens Axboee51cf722008-05-30 22:03:46 +0200103 ret = sd->events;
104 sd->events = 0;
Jens Axboe1d2af022008-02-04 10:59:07 +0100105 } else
106 ret = 0;
107
108 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
109 return ret;
110}
111
112static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
113{
114 struct syncio_data *sd = td->io_ops->data;
115
116 return sd->io_us[event];
117}
118
119static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
120{
121 struct syncio_data *sd = td->io_ops->data;
122
Jens Axboe5f9099e2009-06-16 22:40:26 +0200123 if (ddir_sync(io_u->ddir))
Jens Axboe1d2af022008-02-04 10:59:07 +0100124 return 0;
125
126 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
127 io_u->ddir == sd->last_ddir)
128 return 1;
129
130 return 0;
131}
132
133static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
134 int index)
135{
136 sd->io_us[index] = io_u;
137 sd->iovecs[index].iov_base = io_u->xfer_buf;
138 sd->iovecs[index].iov_len = io_u->xfer_buflen;
139 sd->last_offset = io_u->offset + io_u->xfer_buflen;
140 sd->last_file = io_u->file;
141 sd->last_ddir = io_u->ddir;
142 sd->queued_bytes += io_u->xfer_buflen;
143 sd->queued++;
144}
145
146static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u)
147{
148 struct syncio_data *sd = td->io_ops->data;
149
150 fio_ro_check(td, io_u);
151
152 if (!fio_vsyncio_append(td, io_u)) {
153 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
154 /*
155 * If we can't append and have stuff queued, tell fio to
156 * commit those first and then retry this io
157 */
158 if (sd->queued)
159 return FIO_Q_BUSY;
Jens Axboecc9159c2008-02-04 15:58:24 +0100160 if (io_u->ddir == DDIR_SYNC) {
161 int ret = fsync(io_u->file->fd);
162
163 return fio_io_end(td, io_u, ret);
Jens Axboe5f9099e2009-06-16 22:40:26 +0200164 } else if (io_u->ddir == DDIR_DATASYNC) {
165 int ret = fdatasync(io_u->file->fd);
Jens Axboe1d2af022008-02-04 10:59:07 +0100166
Jens Axboe5f9099e2009-06-16 22:40:26 +0200167 return fio_io_end(td, io_u, ret);
168 }
169
Jens Axboe1d2af022008-02-04 10:59:07 +0100170 sd->queued = 0;
171 sd->queued_bytes = 0;
172 fio_vsyncio_set_iov(sd, io_u, 0);
173 } else {
174 if (sd->queued == td->o.iodepth) {
175 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
176 return FIO_Q_BUSY;
177 }
178
179 dprint(FD_IO, "vsyncio_queue: append\n");
180 fio_vsyncio_set_iov(sd, io_u, sd->queued);
181 }
182
183 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
184 return FIO_Q_QUEUED;
185}
186
187/*
188 * Check that we transferred all bytes, or saw an error, etc
189 */
190static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
191{
192 struct syncio_data *sd = td->io_ops->data;
193 struct io_u *io_u;
194 unsigned int i;
195 int err;
196
197 /*
198 * transferred everything, perfect
199 */
200 if (bytes == sd->queued_bytes)
201 return 0;
202
203 err = errno;
204 for (i = 0; i < sd->queued; i++) {
205 io_u = sd->io_us[i];
206
207 if (bytes == -1) {
208 io_u->error = err;
209 } else {
210 unsigned int this_io;
211
212 this_io = bytes;
213 if (this_io > io_u->xfer_buflen)
214 this_io = io_u->xfer_buflen;
215
216 io_u->resid = io_u->xfer_buflen - this_io;
217 io_u->error = 0;
218 bytes -= this_io;
219 }
220 }
221
222 if (bytes == -1) {
223 td_verror(td, err, "xfer vsync");
224 return -err;
225 }
226
227 return 0;
228}
229
230static int fio_vsyncio_commit(struct thread_data *td)
231{
232 struct syncio_data *sd = td->io_ops->data;
233 struct fio_file *f;
234 ssize_t ret;
235
236 if (!sd->queued)
237 return 0;
238
Jens Axboe838bc702008-05-22 13:08:23 +0200239 io_u_mark_submit(td, sd->queued);
Jens Axboe1d2af022008-02-04 10:59:07 +0100240 f = sd->last_file;
241
242 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
243 int err = -errno;
244
245 td_verror(td, errno, "lseek");
246 return err;
247 }
248
249 if (sd->last_ddir == DDIR_READ)
250 ret = readv(f->fd, sd->iovecs, sd->queued);
251 else
252 ret = writev(f->fd, sd->iovecs, sd->queued);
253
254 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
Jens Axboee51cf722008-05-30 22:03:46 +0200255 sd->events = sd->queued;
256 sd->queued = 0;
Jens Axboe1d2af022008-02-04 10:59:07 +0100257 return fio_vsyncio_end(td, ret);
258}
259
260static int fio_vsyncio_init(struct thread_data *td)
261{
262 struct syncio_data *sd;
263
264 sd = malloc(sizeof(*sd));
265 memset(sd, 0, sizeof(*sd));
266 sd->last_offset = -1ULL;
267 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
268 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
269
270 td->io_ops->data = sd;
271 return 0;
272}
273
274static void fio_vsyncio_cleanup(struct thread_data *td)
275{
276 struct syncio_data *sd = td->io_ops->data;
277
278 free(sd->iovecs);
279 free(sd->io_us);
280 free(sd);
Jens Axboe1d2af022008-02-04 10:59:07 +0100281}
282
gurudas paia31041e2007-10-23 15:12:30 +0200283static struct ioengine_ops ioengine_rw = {
Jens Axboe2866c822006-10-09 15:57:48 +0200284 .name = "sync",
285 .version = FIO_IOOPS_VERSION,
Jens Axboe2866c822006-10-09 15:57:48 +0200286 .prep = fio_syncio_prep,
287 .queue = fio_syncio_queue,
Jens Axboeb5af8292007-03-08 12:43:13 +0100288 .open_file = generic_open_file,
289 .close_file = generic_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100290 .get_file_size = generic_get_file_size,
Jens Axboe2866c822006-10-09 15:57:48 +0200291 .flags = FIO_SYNCIO,
292};
Jens Axboe5f350952006-11-07 15:20:59 +0100293
gurudas paia31041e2007-10-23 15:12:30 +0200294static struct ioengine_ops ioengine_prw = {
295 .name = "psync",
296 .version = FIO_IOOPS_VERSION,
Jens Axboe2bd3eab2008-02-04 09:35:30 +0100297 .queue = fio_psyncio_queue,
gurudas paia31041e2007-10-23 15:12:30 +0200298 .open_file = generic_open_file,
299 .close_file = generic_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100300 .get_file_size = generic_get_file_size,
gurudas paia31041e2007-10-23 15:12:30 +0200301 .flags = FIO_SYNCIO,
302};
303
Jens Axboe1d2af022008-02-04 10:59:07 +0100304static struct ioengine_ops ioengine_vrw = {
305 .name = "vsync",
306 .version = FIO_IOOPS_VERSION,
307 .init = fio_vsyncio_init,
308 .cleanup = fio_vsyncio_cleanup,
309 .queue = fio_vsyncio_queue,
310 .commit = fio_vsyncio_commit,
311 .event = fio_vsyncio_event,
312 .getevents = fio_vsyncio_getevents,
313 .open_file = generic_open_file,
314 .close_file = generic_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100315 .get_file_size = generic_get_file_size,
Jens Axboe1d2af022008-02-04 10:59:07 +0100316 .flags = FIO_SYNCIO,
317};
318
Jens Axboe5f350952006-11-07 15:20:59 +0100319static void fio_init fio_syncio_register(void)
320{
gurudas paia31041e2007-10-23 15:12:30 +0200321 register_ioengine(&ioengine_rw);
322 register_ioengine(&ioengine_prw);
Jens Axboe1d2af022008-02-04 10:59:07 +0100323 register_ioengine(&ioengine_vrw);
Jens Axboe5f350952006-11-07 15:20:59 +0100324}
325
326static void fio_exit fio_syncio_unregister(void)
327{
gurudas paia31041e2007-10-23 15:12:30 +0200328 unregister_ioengine(&ioengine_rw);
329 unregister_ioengine(&ioengine_prw);
Jens Axboe1d2af022008-02-04 10:59:07 +0100330 unregister_ioengine(&ioengine_vrw);
Jens Axboe5f350952006-11-07 15:20:59 +0100331}