Add the file sharing bits
[fio.git] / ioengines.c
CommitLineData
ebac4655
JA
1/*
2 * The io parts of the fio tool, includes workers for sync and mmap'ed
3 * io, as well as both posix and linux libaio support.
4 *
5 * sync io is implemented on top of aio.
6 *
7 * This is not really specific to fio, if the get_io_u/put_io_u and
8 * structures was pulled into this as well it would be a perfectly
9 * generic io engine that could be used for other projects.
10 *
11 */
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
5c4e1dbc 15#include <string.h>
2866c82d 16#include <dlfcn.h>
0c6e7517 17#include <assert.h>
8c16d840 18
ebac4655 19#include "fio.h"
ebac4655 20
5f350952
JA
21static LIST_HEAD(engine_list);
22
8c16d840
JA
23static int check_engine_ops(struct ioengine_ops *ops)
24{
5f350952
JA
25 if (ops->version != FIO_IOOPS_VERSION) {
26 log_err("bad ioops version %d (want %d)\n", ops->version, FIO_IOOPS_VERSION);
27 return 1;
28 }
29
36167d82
JA
30 if (!ops->queue) {
31 log_err("%s: no queue handler\n", ops->name);
32 return 1;
33 }
34
35 /*
36 * sync engines only need a ->queue()
37 */
38 if (ops->flags & FIO_SYNCIO)
39 return 0;
40
8c16d840 41 if (!ops->event) {
36167d82 42 log_err("%s: no event handler\n", ops->name);
8c16d840
JA
43 return 1;
44 }
45 if (!ops->getevents) {
36167d82 46 log_err("%s: no getevents handler\n", ops->name);
8c16d840
JA
47 return 1;
48 }
49 if (!ops->queue) {
36167d82 50 log_err("%s: no queue handler\n", ops->name);
8c16d840
JA
51 return 1;
52 }
53
54 return 0;
55}
56
5f350952 57void unregister_ioengine(struct ioengine_ops *ops)
ebac4655 58{
ee56ad50 59 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
5f350952
JA
60 list_del(&ops->list);
61 INIT_LIST_HEAD(&ops->list);
62}
63
b2fdda43 64void register_ioengine(struct ioengine_ops *ops)
5f350952 65{
ee56ad50 66 dprint(FD_IO, "ioengine %s registered\n", ops->name);
5f350952
JA
67 INIT_LIST_HEAD(&ops->list);
68 list_add_tail(&ops->list, &engine_list);
5f350952
JA
69}
70
71static struct ioengine_ops *find_ioengine(const char *name)
72{
73 struct ioengine_ops *ops;
74 struct list_head *entry;
ebac4655 75
5f350952
JA
76 list_for_each(entry, &engine_list) {
77 ops = list_entry(entry, struct ioengine_ops, list);
bc5b77a8 78 if (!strcmp(name, ops->name))
5f350952
JA
79 return ops;
80 }
81
82 return NULL;
83}
84
85static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
86 const char *engine_lib)
87{
88 struct ioengine_ops *ops;
89 void *dlhandle;
90
ee56ad50
JA
91 dprint(FD_IO, "dload engine %s\n", engine_lib);
92
2866c82d
JA
93 dlerror();
94 dlhandle = dlopen(engine_lib, RTLD_LAZY);
d4dbaaa8 95 if (!dlhandle) {
e1161c32 96 td_vmsg(td, -1, dlerror(), "dlopen");
d4dbaaa8
JA
97 return NULL;
98 }
8756e4d4 99
da51c050
JA
100 /*
101 * Unlike the included modules, external engines should have a
102 * non-static ioengine structure that we can reference.
103 */
2866c82d 104 ops = dlsym(dlhandle, "ioengine");
d4dbaaa8 105 if (!ops) {
e1161c32 106 td_vmsg(td, -1, dlerror(), "dlsym");
d4dbaaa8
JA
107 dlclose(dlhandle);
108 return NULL;
109 }
8756e4d4 110
5f350952
JA
111 ops->dlhandle = dlhandle;
112 return ops;
113}
114
115struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
116{
117 struct ioengine_ops *ops, *ret;
118 char engine[16];
119
ee56ad50
JA
120 dprint(FD_IO, "load ioengine %s\n", name);
121
5f350952
JA
122 strncpy(engine, name, sizeof(engine) - 1);
123
124 /*
125 * linux libaio has alias names, so convert to what we want
126 */
127 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
128 strcpy(engine, "libaio");
129
130 ops = find_ioengine(engine);
131 if (!ops)
132 ops = dlopen_ioengine(td, name);
133
134 if (!ops) {
135 log_err("fio: engine %s not loadable\n", name);
b902ceb5
JA
136 return NULL;
137 }
138
8c16d840
JA
139 /*
140 * Check that the required methods are there.
141 */
5f350952 142 if (check_engine_ops(ops))
8c16d840 143 return NULL;
8c16d840 144
84585003
JA
145 ret = malloc(sizeof(*ret));
146 memcpy(ret, ops, sizeof(*ret));
147 ret->data = NULL;
84585003
JA
148
149 return ret;
8756e4d4
JA
150}
151
2866c82d 152void close_ioengine(struct thread_data *td)
8756e4d4 153{
ee56ad50
JA
154 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
155
2866c82d
JA
156 if (td->io_ops->cleanup)
157 td->io_ops->cleanup(td);
b990b5c0 158
5f350952
JA
159 if (td->io_ops->dlhandle)
160 dlclose(td->io_ops->dlhandle);
161
84585003
JA
162 free(td->io_ops);
163 td->io_ops = NULL;
b990b5c0 164}
10ba535a
JA
165
166int td_io_prep(struct thread_data *td, struct io_u *io_u)
167{
ee56ad50 168 dprint_io_u(io_u, "prep");
7101d9c2
JA
169 fio_ro_check(td, io_u);
170
b2bd2bd9
JA
171 lock_file(td, io_u->file);
172
2ba1c290
JA
173 if (td->io_ops->prep) {
174 int ret = td->io_ops->prep(td, io_u);
175
176 dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret);
b2bd2bd9
JA
177 if (ret)
178 unlock_file(io_u->file);
2ba1c290
JA
179 return ret;
180 }
10ba535a
JA
181
182 return 0;
183}
184
e7d2e616 185int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
10ba535a
JA
186 struct timespec *t)
187{
ee56ad50 188 int r = 0;
face81b2 189
ee56ad50
JA
190 if (min > 0 && td->io_ops->commit) {
191 r = td->io_ops->commit(td);
face81b2 192 if (r < 0)
ee56ad50 193 goto out;
face81b2 194 }
36167d82 195
ee56ad50
JA
196 r = 0;
197 if (td->io_ops->getevents)
198 r = td->io_ops->getevents(td, min, max, t);
199out:
200 dprint(FD_IO, "getevents: %d\n", r);
201 return r;
10ba535a
JA
202}
203
204int td_io_queue(struct thread_data *td, struct io_u *io_u)
205{
7e77dd02
JA
206 int ret;
207
ee56ad50 208 dprint_io_u(io_u, "queue");
7101d9c2
JA
209 fio_ro_check(td, io_u);
210
0c6e7517
JA
211 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
212 io_u->flags |= IO_U_F_FLIGHT;
213
3d7b485f
JA
214 assert(io_u->file->flags & FIO_FILE_OPEN);
215
11786802
JA
216 io_u->error = 0;
217 io_u->resid = 0;
218
433afcb4 219 if (td->io_ops->flags & FIO_SYNCIO) {
5aeb77df 220 fio_gettime(&io_u->issue_time, NULL);
a61eddec 221 memcpy(&td->last_issue, &io_u->issue_time, sizeof(struct timeval));
10ba535a 222
433afcb4
JA
223 /*
224 * for a sync engine, set the timeout upfront
225 */
226 if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
227 io_u_set_timeout(td);
228 }
229
755200a3
JA
230 if (io_u->ddir != DDIR_SYNC)
231 td->io_issues[io_u->ddir]++;
232
7e77dd02 233 ret = td->io_ops->queue(td, io_u);
5aeb77df 234
b2bd2bd9
JA
235 unlock_file(io_u->file);
236
163f849e
JA
237 if (ret != FIO_Q_BUSY)
238 io_u_mark_depth(td, io_u);
239
eb7c8ae2
JA
240 if (ret == FIO_Q_QUEUED) {
241 int r;
242
cb5ab512 243 td->io_u_queued++;
2dc1bbeb 244 if (td->io_u_queued > td->o.iodepth_batch) {
eb7c8ae2
JA
245 r = td_io_commit(td);
246 if (r < 0)
247 return r;
248 }
249 }
cb5ab512 250
433afcb4 251 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
5aeb77df 252 fio_gettime(&io_u->issue_time, NULL);
a61eddec 253 memcpy(&td->last_issue, &io_u->issue_time, sizeof(struct timeval));
5aeb77df 254
433afcb4
JA
255 /*
256 * async engine, set the timeout here
257 */
258 if (ret == FIO_Q_QUEUED &&
259 mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
260 io_u_set_timeout(td);
261 }
262
7e77dd02 263 return ret;
10ba535a 264}
8c16d840
JA
265
266int td_io_init(struct thread_data *td)
267{
eeb12160 268 int ret = 0;
8c16d840 269
eeb12160
JA
270 if (td->io_ops->init) {
271 ret = td->io_ops->init(td);
272 if (ret && td->o.iodepth > 1)
9de6bebc 273 log_err("fio: io engine init failed. Perhaps try reducing io depth?\n");
eeb12160
JA
274 }
275
276 return ret;
8c16d840 277}
755200a3
JA
278
279int td_io_commit(struct thread_data *td)
280{
ee56ad50
JA
281 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
282
e1161c32
JA
283 if (!td->cur_depth)
284 return 0;
cb5ab512
JA
285
286 td->io_u_queued = 0;
755200a3
JA
287 if (td->io_ops->commit)
288 return td->io_ops->commit(td);
289
290 return 0;
291}
b5af8293
JA
292
293int td_io_open_file(struct thread_data *td, struct fio_file *f)
294{
413d6693
JA
295 if (td->io_ops->open_file(td, f)) {
296 if (td->error == EINVAL && td->o.odirect)
297 log_err("fio: destination does not support O_DIRECT\n");
298 if (td->error == EMFILE)
299 log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files);
300
301 return 1;
302 }
303
66159828
JA
304 if (f->filetype == FIO_TYPE_PIPE) {
305 if (td_random(td)) {
306 log_err("fio: can't seek on pipes (no random io)\n");
307 goto err;
308 }
309 }
310
413d6693 311 f->last_free_lookup = 0;
bcdedd0a 312 f->last_pos = f->file_offset;
413d6693
JA
313 f->flags |= FIO_FILE_OPEN;
314 f->flags &= ~FIO_FILE_CLOSING;
315
316 if (td->io_ops->flags & FIO_DISKLESSIO)
317 goto done;
318
319 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
320 goto err;
321
66159828
JA
322 if (td->o.fadvise_hint &&
323 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
324
413d6693
JA
325 int flags;
326
327 if (td_random(td))
328 flags = POSIX_FADV_RANDOM;
329 else
330 flags = POSIX_FADV_SEQUENTIAL;
331
332 if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
333 td_verror(td, errno, "fadvise");
334 goto err;
335 }
7bb48f84 336 }
a978ba68 337
413d6693
JA
338 if (f->file_map)
339 memset(f->file_map, 0, f->num_maps * sizeof(long));
a978ba68 340
413d6693 341done:
f29b25a3 342 log_file(td, f, FIO_LOG_OPEN_FILE);
413d6693
JA
343 td->nr_open_files++;
344 get_file(f);
345 return 0;
346err:
b284075a
JA
347 if (td->io_ops->close_file)
348 td->io_ops->close_file(td, f);
7bb48f84 349 return 1;
b5af8293
JA
350}
351
6977bcd0 352int td_io_close_file(struct thread_data *td, struct fio_file *f)
b5af8293 353{
f29b25a3
JA
354 if (!(f->flags & FIO_FILE_CLOSING))
355 log_file(td, f, FIO_LOG_CLOSE_FILE);
356
0ad920e7
JA
357 /*
358 * mark as closing, do real close when last io on it has completed
359 */
360 f->flags |= FIO_FILE_CLOSING;
361
29c1349f
JA
362 if (f->sem_owner == td && f->sem_batch) {
363 f->sem_batch = 0;
364 unlock_file(f);
365 }
366
6977bcd0 367 return put_file(td, f);
b5af8293 368}