Add options for disabling slat/clat/bw measurements
[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
01743ee1 21static FLIST_HEAD(engine_list);
5f350952 22
8c16d840
JA
23static int check_engine_ops(struct ioengine_ops *ops)
24{
5f350952 25 if (ops->version != FIO_IOOPS_VERSION) {
5ec10eaa
JA
26 log_err("bad ioops version %d (want %d)\n", ops->version,
27 FIO_IOOPS_VERSION);
5f350952
JA
28 return 1;
29 }
30
36167d82
JA
31 if (!ops->queue) {
32 log_err("%s: no queue handler\n", ops->name);
33 return 1;
34 }
35
36 /*
37 * sync engines only need a ->queue()
38 */
39 if (ops->flags & FIO_SYNCIO)
40 return 0;
5ec10eaa 41
8c16d840 42 if (!ops->event) {
36167d82 43 log_err("%s: no event handler\n", ops->name);
8c16d840
JA
44 return 1;
45 }
46 if (!ops->getevents) {
36167d82 47 log_err("%s: no getevents handler\n", ops->name);
8c16d840
JA
48 return 1;
49 }
50 if (!ops->queue) {
36167d82 51 log_err("%s: no queue handler\n", ops->name);
8c16d840
JA
52 return 1;
53 }
5ec10eaa 54
8c16d840
JA
55 return 0;
56}
57
5f350952 58void unregister_ioengine(struct ioengine_ops *ops)
ebac4655 59{
ee56ad50 60 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
01743ee1
JA
61 flist_del(&ops->list);
62 INIT_FLIST_HEAD(&ops->list);
5f350952
JA
63}
64
b2fdda43 65void register_ioengine(struct ioengine_ops *ops)
5f350952 66{
ee56ad50 67 dprint(FD_IO, "ioengine %s registered\n", ops->name);
01743ee1
JA
68 INIT_FLIST_HEAD(&ops->list);
69 flist_add_tail(&ops->list, &engine_list);
5f350952
JA
70}
71
72static struct ioengine_ops *find_ioengine(const char *name)
73{
74 struct ioengine_ops *ops;
01743ee1 75 struct flist_head *entry;
ebac4655 76
01743ee1
JA
77 flist_for_each(entry, &engine_list) {
78 ops = flist_entry(entry, struct ioengine_ops, list);
bc5b77a8 79 if (!strcmp(name, ops->name))
5f350952
JA
80 return ops;
81 }
82
83 return NULL;
84}
85
86static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
87 const char *engine_lib)
88{
89 struct ioengine_ops *ops;
90 void *dlhandle;
91
ee56ad50
JA
92 dprint(FD_IO, "dload engine %s\n", engine_lib);
93
2866c82d
JA
94 dlerror();
95 dlhandle = dlopen(engine_lib, RTLD_LAZY);
d4dbaaa8 96 if (!dlhandle) {
e1161c32 97 td_vmsg(td, -1, dlerror(), "dlopen");
d4dbaaa8
JA
98 return NULL;
99 }
8756e4d4 100
da51c050
JA
101 /*
102 * Unlike the included modules, external engines should have a
103 * non-static ioengine structure that we can reference.
104 */
2866c82d 105 ops = dlsym(dlhandle, "ioengine");
d4dbaaa8 106 if (!ops) {
e1161c32 107 td_vmsg(td, -1, dlerror(), "dlsym");
d4dbaaa8
JA
108 dlclose(dlhandle);
109 return NULL;
110 }
8756e4d4 111
5f350952
JA
112 ops->dlhandle = dlhandle;
113 return ops;
114}
115
116struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
117{
118 struct ioengine_ops *ops, *ret;
119 char engine[16];
120
ee56ad50
JA
121 dprint(FD_IO, "load ioengine %s\n", name);
122
5f350952
JA
123 strncpy(engine, name, sizeof(engine) - 1);
124
125 /*
126 * linux libaio has alias names, so convert to what we want
127 */
128 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
129 strcpy(engine, "libaio");
130
131 ops = find_ioengine(engine);
132 if (!ops)
133 ops = dlopen_ioengine(td, name);
134
135 if (!ops) {
136 log_err("fio: engine %s not loadable\n", name);
b902ceb5
JA
137 return NULL;
138 }
139
8c16d840
JA
140 /*
141 * Check that the required methods are there.
142 */
5f350952 143 if (check_engine_ops(ops))
8c16d840 144 return NULL;
8c16d840 145
84585003
JA
146 ret = malloc(sizeof(*ret));
147 memcpy(ret, ops, sizeof(*ret));
148 ret->data = NULL;
84585003
JA
149
150 return ret;
8756e4d4
JA
151}
152
2866c82d 153void close_ioengine(struct thread_data *td)
8756e4d4 154{
ee56ad50
JA
155 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
156
2992b059 157 if (td->io_ops->cleanup) {
2866c82d 158 td->io_ops->cleanup(td);
2992b059
JA
159 td->io_ops->data = NULL;
160 }
b990b5c0 161
5f350952
JA
162 if (td->io_ops->dlhandle)
163 dlclose(td->io_ops->dlhandle);
164
84585003
JA
165 free(td->io_ops);
166 td->io_ops = NULL;
b990b5c0 167}
10ba535a
JA
168
169int td_io_prep(struct thread_data *td, struct io_u *io_u)
170{
ee56ad50 171 dprint_io_u(io_u, "prep");
7101d9c2
JA
172 fio_ro_check(td, io_u);
173
4d4e80f2 174 lock_file(td, io_u->file, io_u->ddir);
b2bd2bd9 175
2ba1c290
JA
176 if (td->io_ops->prep) {
177 int ret = td->io_ops->prep(td, io_u);
178
179 dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret);
b2bd2bd9 180 if (ret)
4d4e80f2 181 unlock_file(td, io_u->file);
2ba1c290
JA
182 return ret;
183 }
10ba535a
JA
184
185 return 0;
186}
187
e7d2e616 188int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
10ba535a
JA
189 struct timespec *t)
190{
ee56ad50 191 int r = 0;
face81b2 192
ee56ad50
JA
193 if (min > 0 && td->io_ops->commit) {
194 r = td->io_ops->commit(td);
face81b2 195 if (r < 0)
ee56ad50 196 goto out;
face81b2 197 }
4950421a
JA
198 if (max > td->cur_depth)
199 max = td->cur_depth;
200 if (min > max)
201 max = min;
36167d82 202
ee56ad50 203 r = 0;
4950421a 204 if (max && td->io_ops->getevents)
ee56ad50
JA
205 r = td->io_ops->getevents(td, min, max, t);
206out:
838bc709
JA
207 if (r >= 0)
208 io_u_mark_complete(td, r);
ee56ad50
JA
209 dprint(FD_IO, "getevents: %d\n", r);
210 return r;
10ba535a
JA
211}
212
9520ebb9
JA
213static inline int get_issue_time(struct thread_data *td)
214{
215 if (td->o.read_iolog_file ||
216 !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
217 return 1;
218
219 return 0;
220}
221
10ba535a
JA
222int td_io_queue(struct thread_data *td, struct io_u *io_u)
223{
7e77dd02
JA
224 int ret;
225
ee56ad50 226 dprint_io_u(io_u, "queue");
7101d9c2
JA
227 fio_ro_check(td, io_u);
228
0c6e7517
JA
229 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
230 io_u->flags |= IO_U_F_FLIGHT;
231
3d7b485f
JA
232 assert(io_u->file->flags & FIO_FILE_OPEN);
233
11786802
JA
234 io_u->error = 0;
235 io_u->resid = 0;
236
433afcb4 237 if (td->io_ops->flags & FIO_SYNCIO) {
9520ebb9
JA
238 if (get_issue_time(td))
239 fio_gettime(&io_u->issue_time, NULL);
d0c15328
JA
240
241 /*
242 * only used for iolog
243 */
244 if (td->o.read_iolog_file)
245 memcpy(&td->last_issue, &io_u->issue_time,
5ec10eaa 246 sizeof(struct timeval));
433afcb4
JA
247 }
248
755200a3
JA
249 if (io_u->ddir != DDIR_SYNC)
250 td->io_issues[io_u->ddir]++;
251
7e77dd02 252 ret = td->io_ops->queue(td, io_u);
5aeb77df 253
4d4e80f2 254 unlock_file(td, io_u->file);
b2bd2bd9 255
838bc709
JA
256 if (!td->io_ops->commit) {
257 io_u_mark_submit(td, 1);
258 io_u_mark_complete(td, 1);
259 }
260
d8005759
JA
261 if (ret == FIO_Q_COMPLETED) {
262 if (io_u->ddir != DDIR_SYNC) {
263 io_u_mark_depth(td, 1);
264 td->ts.total_io_u[io_u->ddir]++;
265 }
266 } else if (ret == FIO_Q_QUEUED) {
eb7c8ae2
JA
267 int r;
268
d8005759
JA
269 if (io_u->ddir != DDIR_SYNC) {
270 td->io_u_queued++;
271 td->ts.total_io_u[io_u->ddir]++;
272 }
273
274 if (td->io_u_queued >= td->o.iodepth_batch) {
eb7c8ae2
JA
275 r = td_io_commit(td);
276 if (r < 0)
277 return r;
278 }
279 }
cb5ab512 280
433afcb4 281 if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
9520ebb9
JA
282 if (get_issue_time(td))
283 fio_gettime(&io_u->issue_time, NULL);
d0c15328
JA
284
285 /*
286 * only used for iolog
287 */
288 if (td->o.read_iolog_file)
289 memcpy(&td->last_issue, &io_u->issue_time,
290 sizeof(struct timeval));
433afcb4
JA
291 }
292
7e77dd02 293 return ret;
10ba535a 294}
8c16d840
JA
295
296int td_io_init(struct thread_data *td)
297{
eeb12160 298 int ret = 0;
8c16d840 299
eeb12160
JA
300 if (td->io_ops->init) {
301 ret = td->io_ops->init(td);
5ec10eaa
JA
302 if (ret && td->o.iodepth > 1) {
303 log_err("fio: io engine init failed. Perhaps try"
304 " reducing io depth?\n");
305 }
eeb12160
JA
306 }
307
308 return ret;
8c16d840 309}
755200a3
JA
310
311int td_io_commit(struct thread_data *td)
312{
ee56ad50
JA
313 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
314
d8005759 315 if (!td->cur_depth || !td->io_u_queued)
e1161c32 316 return 0;
cb5ab512 317
d8005759 318 io_u_mark_depth(td, td->io_u_queued);
cb5ab512 319 td->io_u_queued = 0;
d8005759 320
755200a3
JA
321 if (td->io_ops->commit)
322 return td->io_ops->commit(td);
323
324 return 0;
325}
b5af8293
JA
326
327int td_io_open_file(struct thread_data *td, struct fio_file *f)
328{
413d6693
JA
329 if (td->io_ops->open_file(td, f)) {
330 if (td->error == EINVAL && td->o.odirect)
331 log_err("fio: destination does not support O_DIRECT\n");
5ec10eaa
JA
332 if (td->error == EMFILE) {
333 log_err("fio: try reducing/setting openfiles (failed"
334 " at %u of %u)\n", td->nr_open_files,
335 td->o.nr_files);
336 }
413d6693
JA
337
338 return 1;
339 }
340
d5707a35
JA
341 fio_file_reset(f);
342 f->flags |= FIO_FILE_OPEN;
343 f->flags &= ~FIO_FILE_CLOSING;
344
345 td->nr_open_files++;
346 get_file(f);
347
66159828
JA
348 if (f->filetype == FIO_TYPE_PIPE) {
349 if (td_random(td)) {
350 log_err("fio: can't seek on pipes (no random io)\n");
351 goto err;
352 }
353 }
354
413d6693
JA
355 if (td->io_ops->flags & FIO_DISKLESSIO)
356 goto done;
357
358 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
359 goto err;
360
66159828
JA
361 if (td->o.fadvise_hint &&
362 (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_FILE)) {
413d6693
JA
363 int flags;
364
365 if (td_random(td))
366 flags = POSIX_FADV_RANDOM;
367 else
368 flags = POSIX_FADV_SEQUENTIAL;
369
370 if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
371 td_verror(td, errno, "fadvise");
372 goto err;
373 }
7bb48f84 374 }
a978ba68 375
413d6693 376 if (f->file_map)
de605666 377 memset(f->file_map, 0, f->num_maps * sizeof(int));
a978ba68 378
e116f2b9
JA
379#ifdef FIO_OS_DIRECTIO
380 /*
381 * Some OS's have a distinct call to mark the file non-buffered,
382 * instead of using O_DIRECT (Solaris)
383 */
384 if (td->o.odirect) {
385 int ret = fio_set_odirect(f->fd);
386
387 if (ret) {
388 td_verror(td, ret, "fio_set_odirect");
389 goto err;
390 }
391 }
392#endif
393
413d6693 394done:
f29b25a3 395 log_file(td, f, FIO_LOG_OPEN_FILE);
413d6693
JA
396 return 0;
397err:
b284075a
JA
398 if (td->io_ops->close_file)
399 td->io_ops->close_file(td, f);
7bb48f84 400 return 1;
b5af8293
JA
401}
402
6977bcd0 403int td_io_close_file(struct thread_data *td, struct fio_file *f)
b5af8293 404{
f29b25a3
JA
405 if (!(f->flags & FIO_FILE_CLOSING))
406 log_file(td, f, FIO_LOG_CLOSE_FILE);
407
0ad920e7
JA
408 /*
409 * mark as closing, do real close when last io on it has completed
410 */
411 f->flags |= FIO_FILE_CLOSING;
412
4d4e80f2 413 unlock_file_all(td, f);
29c1349f 414
6977bcd0 415 return put_file(td, f);
b5af8293 416}