blob: c90a2ca5bde30e75dec5261130b6b77fb6a1999b [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001/*
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>
Jens Axboe5c4e1db2006-06-07 14:17:08 +020015#include <string.h>
Jens Axboe2866c822006-10-09 15:57:48 +020016#include <dlfcn.h>
Bruce Cranecc314b2011-01-04 10:59:30 +010017#include <fcntl.h>
Jens Axboe0c6e7512007-02-22 11:19:39 +010018#include <assert.h>
Jens Axboe8c16d842006-10-20 11:48:33 +020019
Jens Axboeebac4652005-12-08 15:25:21 +010020#include "fio.h"
Jens Axboe7c9b1bc2009-06-03 09:25:57 +020021#include "diskutil.h"
Jens Axboeebac4652005-12-08 15:25:21 +010022
Jens Axboe01743ee2008-06-02 12:19:19 +020023static FLIST_HEAD(engine_list);
Jens Axboe5f350952006-11-07 15:20:59 +010024
Elliott Hugheseda3a602017-05-19 18:53:02 -070025static bool check_engine_ops(struct ioengine_ops *ops)
Jens Axboe8c16d842006-10-20 11:48:33 +020026{
Jens Axboe5f350952006-11-07 15:20:59 +010027 if (ops->version != FIO_IOOPS_VERSION) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010028 log_err("bad ioops version %d (want %d)\n", ops->version,
29 FIO_IOOPS_VERSION);
Elliott Hugheseda3a602017-05-19 18:53:02 -070030 return true;
Jens Axboe5f350952006-11-07 15:20:59 +010031 }
32
Jens Axboe36167d82007-02-18 05:41:31 +010033 if (!ops->queue) {
34 log_err("%s: no queue handler\n", ops->name);
Elliott Hugheseda3a602017-05-19 18:53:02 -070035 return true;
Jens Axboe36167d82007-02-18 05:41:31 +010036 }
37
38 /*
39 * sync engines only need a ->queue()
40 */
41 if (ops->flags & FIO_SYNCIO)
Elliott Hugheseda3a602017-05-19 18:53:02 -070042 return false;
Jens Axboe5ec10ea2008-03-06 15:42:00 +010043
Elliott Hugheseda3a602017-05-19 18:53:02 -070044 if (!ops->event || !ops->getevents) {
45 log_err("%s: no event/getevents handler\n", ops->name);
46 return true;
Jens Axboe8c16d842006-10-20 11:48:33 +020047 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +010048
Elliott Hugheseda3a602017-05-19 18:53:02 -070049 return false;
Jens Axboe8c16d842006-10-20 11:48:33 +020050}
51
Jens Axboe5f350952006-11-07 15:20:59 +010052void unregister_ioengine(struct ioengine_ops *ops)
Jens Axboeebac4652005-12-08 15:25:21 +010053{
Jens Axboeee56ad52008-02-01 10:30:20 +010054 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
Jens Axboe01743ee2008-06-02 12:19:19 +020055 flist_del(&ops->list);
56 INIT_FLIST_HEAD(&ops->list);
Jens Axboe5f350952006-11-07 15:20:59 +010057}
58
Jens Axboeb2fdda42007-02-20 20:52:51 +010059void register_ioengine(struct ioengine_ops *ops)
Jens Axboe5f350952006-11-07 15:20:59 +010060{
Jens Axboeee56ad52008-02-01 10:30:20 +010061 dprint(FD_IO, "ioengine %s registered\n", ops->name);
Jens Axboe01743ee2008-06-02 12:19:19 +020062 INIT_FLIST_HEAD(&ops->list);
63 flist_add_tail(&ops->list, &engine_list);
Jens Axboe5f350952006-11-07 15:20:59 +010064}
65
66static struct ioengine_ops *find_ioengine(const char *name)
67{
68 struct ioengine_ops *ops;
Jens Axboe01743ee2008-06-02 12:19:19 +020069 struct flist_head *entry;
Jens Axboe2866c822006-10-09 15:57:48 +020070
Jens Axboe01743ee2008-06-02 12:19:19 +020071 flist_for_each(entry, &engine_list) {
72 ops = flist_entry(entry, struct ioengine_ops, list);
Jens Axboebc5b77a2007-02-28 09:32:54 +010073 if (!strcmp(name, ops->name))
Jens Axboe5f350952006-11-07 15:20:59 +010074 return ops;
75 }
76
77 return NULL;
78}
79
80static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
81 const char *engine_lib)
82{
83 struct ioengine_ops *ops;
84 void *dlhandle;
85
Jens Axboeee56ad52008-02-01 10:30:20 +010086 dprint(FD_IO, "dload engine %s\n", engine_lib);
87
Jens Axboe2866c822006-10-09 15:57:48 +020088 dlerror();
89 dlhandle = dlopen(engine_lib, RTLD_LAZY);
Jens Axboed4dbaaa2006-10-09 16:01:27 +020090 if (!dlhandle) {
Jens Axboee1161c32007-02-22 19:36:48 +010091 td_vmsg(td, -1, dlerror(), "dlopen");
Jens Axboed4dbaaa2006-10-09 16:01:27 +020092 return NULL;
93 }
Jens Axboe2866c822006-10-09 15:57:48 +020094
Jens Axboeda51c052006-11-07 16:02:11 +010095 /*
96 * Unlike the included modules, external engines should have a
97 * non-static ioengine structure that we can reference.
98 */
Dmitry Monakhov0abea0b2012-09-19 23:22:54 +040099 ops = dlsym(dlhandle, engine_lib);
100 if (!ops)
101 ops = dlsym(dlhandle, "ioengine");
Daniel Golluba8075702014-02-12 21:23:31 -0700102
103 /*
104 * For some external engines (like C++ ones) it is not that trivial
105 * to provide a non-static ionengine structure that we can reference.
106 * Instead we call a method which allocates the required ioengine
107 * structure.
108 */
109 if (!ops) {
110 get_ioengine_t get_ioengine = dlsym(dlhandle, "get_ioengine");
111
112 if (get_ioengine)
113 get_ioengine(&ops);
114 }
115
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200116 if (!ops) {
Jens Axboee1161c32007-02-22 19:36:48 +0100117 td_vmsg(td, -1, dlerror(), "dlsym");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200118 dlclose(dlhandle);
119 return NULL;
120 }
Jens Axboe2866c822006-10-09 15:57:48 +0200121
Elliott Hugheseda3a602017-05-19 18:53:02 -0700122 td->io_ops_dlhandle = dlhandle;
Jens Axboe5f350952006-11-07 15:20:59 +0100123 return ops;
124}
125
126struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
127{
Elliott Hugheseda3a602017-05-19 18:53:02 -0700128 struct ioengine_ops *ops;
129 char engine[64];
Jens Axboe5f350952006-11-07 15:20:59 +0100130
Jens Axboeee56ad52008-02-01 10:30:20 +0100131 dprint(FD_IO, "load ioengine %s\n", name);
132
Elliott Hugheseda3a602017-05-19 18:53:02 -0700133 engine[sizeof(engine) - 1] = '\0';
Jens Axboe5f350952006-11-07 15:20:59 +0100134 strncpy(engine, name, sizeof(engine) - 1);
135
136 /*
137 * linux libaio has alias names, so convert to what we want
138 */
139 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
140 strcpy(engine, "libaio");
141
142 ops = find_ioengine(engine);
143 if (!ops)
144 ops = dlopen_ioengine(td, name);
145
146 if (!ops) {
147 log_err("fio: engine %s not loadable\n", name);
Jens Axboeb902ceb2006-10-09 16:11:45 +0200148 return NULL;
149 }
150
Jens Axboe8c16d842006-10-20 11:48:33 +0200151 /*
152 * Check that the required methods are there.
153 */
Jens Axboe5f350952006-11-07 15:20:59 +0100154 if (check_engine_ops(ops))
Jens Axboe8c16d842006-10-20 11:48:33 +0200155 return NULL;
Jens Axboe8c16d842006-10-20 11:48:33 +0200156
Elliott Hugheseda3a602017-05-19 18:53:02 -0700157 return ops;
Jens Axboeebac4652005-12-08 15:25:21 +0100158}
159
Steven Langde890a12011-11-09 14:03:34 +0100160/*
161 * For cleaning up an ioengine which never made it to init().
162 */
163void free_ioengine(struct thread_data *td)
164{
165 dprint(FD_IO, "free ioengine %s\n", td->io_ops->name);
166
167 if (td->eo && td->io_ops->options) {
168 options_free(td->io_ops->options, td->eo);
169 free(td->eo);
170 td->eo = NULL;
171 }
172
Elliott Hugheseda3a602017-05-19 18:53:02 -0700173 if (td->io_ops_dlhandle)
174 dlclose(td->io_ops_dlhandle);
Steven Langde890a12011-11-09 14:03:34 +0100175
Steven Langde890a12011-11-09 14:03:34 +0100176 td->io_ops = NULL;
177}
178
Jens Axboe2866c822006-10-09 15:57:48 +0200179void close_ioengine(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100180{
Jens Axboeee56ad52008-02-01 10:30:20 +0100181 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
182
Jens Axboe2992b052008-05-30 22:07:12 +0200183 if (td->io_ops->cleanup) {
Jens Axboe2866c822006-10-09 15:57:48 +0200184 td->io_ops->cleanup(td);
Elliott Hugheseda3a602017-05-19 18:53:02 -0700185 td->io_ops_data = NULL;
Jens Axboe2992b052008-05-30 22:07:12 +0200186 }
Jens Axboeebac4652005-12-08 15:25:21 +0100187
Steven Langde890a12011-11-09 14:03:34 +0100188 free_ioengine(td);
Jens Axboeb990b5c2006-09-14 09:48:22 +0200189}
Jens Axboe10ba5352006-10-20 11:39:27 +0200190
191int td_io_prep(struct thread_data *td, struct io_u *io_u)
192{
Jens Axboeee56ad52008-02-01 10:30:20 +0100193 dprint_io_u(io_u, "prep");
Jens Axboe7101d9c2007-09-12 13:12:39 +0200194 fio_ro_check(td, io_u);
195
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100196 lock_file(td, io_u->file, io_u->ddir);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100197
Jens Axboe2ba1c292008-02-01 13:16:38 +0100198 if (td->io_ops->prep) {
199 int ret = td->io_ops->prep(td, io_u);
200
201 dprint(FD_IO, "->prep(%p)=%d\n", io_u, ret);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100202 if (ret)
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100203 unlock_file(td, io_u->file);
Jens Axboe2ba1c292008-02-01 13:16:38 +0100204 return ret;
205 }
Jens Axboe10ba5352006-10-20 11:39:27 +0200206
207 return 0;
208}
209
Jens Axboee7d2e612007-12-11 10:49:39 +0100210int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
Jens Axboe0cbbc392014-09-30 16:04:12 -0600211 const struct timespec *t)
Jens Axboe10ba5352006-10-20 11:39:27 +0200212{
Jens Axboeee56ad52008-02-01 10:30:20 +0100213 int r = 0;
214
Yufei Rena05d62b2012-03-15 14:44:47 +0100215 /*
216 * For ioengine=rdma one side operation RDMA_WRITE or RDMA_READ,
217 * server side gets a message from the client
218 * side that the task is finished, and
219 * td->done is set to 1 after td_io_commit(). In this case,
220 * there is no need to reap complete event in server side.
221 */
222 if (td->done)
223 return 0;
224
Jens Axboeface81b2007-02-26 10:40:03 +0100225 if (min > 0 && td->io_ops->commit) {
Jens Axboeee56ad52008-02-01 10:30:20 +0100226 r = td->io_ops->commit(td);
Jens Axboeface81b2007-02-26 10:40:03 +0100227 if (r < 0)
Jens Axboeee56ad52008-02-01 10:30:20 +0100228 goto out;
Jens Axboeface81b2007-02-26 10:40:03 +0100229 }
Jens Axboe49504212008-06-05 09:03:30 +0200230 if (max > td->cur_depth)
231 max = td->cur_depth;
232 if (min > max)
233 max = min;
Jens Axboe36167d82007-02-18 05:41:31 +0100234
Jens Axboeee56ad52008-02-01 10:30:20 +0100235 r = 0;
Jens Axboe49504212008-06-05 09:03:30 +0200236 if (max && td->io_ops->getevents)
Jens Axboeee56ad52008-02-01 10:30:20 +0100237 r = td->io_ops->getevents(td, min, max, t);
238out:
Ryan Marchand422f9e42012-01-31 14:05:32 +0100239 if (r >= 0) {
240 /*
Jens Axboe3fd9efb2013-04-11 08:53:24 +0200241 * Reflect that our submitted requests were retrieved with
Ryan Marchand422f9e42012-01-31 14:05:32 +0100242 * whatever OS async calls are in the underlying engine.
243 */
244 td->io_u_in_flight -= r;
Jens Axboe838bc702008-05-22 13:08:23 +0200245 io_u_mark_complete(td, r);
Ryan Marchand422f9e42012-01-31 14:05:32 +0100246 } else
Jens Axboe7c639b12010-03-19 08:48:05 +0100247 td_verror(td, r, "get_events");
Jens Axboef3e11d02010-03-18 19:31:06 +0100248
Jens Axboeee56ad52008-02-01 10:30:20 +0100249 dprint(FD_IO, "getevents: %d\n", r);
250 return r;
Jens Axboe10ba5352006-10-20 11:39:27 +0200251}
252
253int td_io_queue(struct thread_data *td, struct io_u *io_u)
254{
Elliott Hugheseda3a602017-05-19 18:53:02 -0700255 const enum fio_ddir ddir = acct_ddir(io_u);
256 unsigned long buflen = io_u->xfer_buflen;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100257 int ret;
258
Jens Axboeee56ad52008-02-01 10:30:20 +0100259 dprint_io_u(io_u, "queue");
Jens Axboe7101d9c2007-09-12 13:12:39 +0200260 fio_ro_check(td, io_u);
261
Jens Axboe0c6e7512007-02-22 11:19:39 +0100262 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
Elliott Hugheseda3a602017-05-19 18:53:02 -0700263 io_u_set(td, io_u, IO_U_F_FLIGHT);
Jens Axboe0c6e7512007-02-22 11:19:39 +0100264
Jens Axboed6aed792009-06-03 08:41:15 +0200265 assert(fio_file_open(io_u->file));
Jens Axboe3d7b4852007-03-13 14:50:28 +0100266
Jens Axboebcd5abf2013-01-23 09:27:25 -0700267 /*
268 * If using a write iolog, store this entry.
269 */
270 log_io_u(td, io_u);
271
Jens Axboe11786802007-03-05 18:33:22 +0100272 io_u->error = 0;
273 io_u->resid = 0;
274
Elliott Hugheseda3a602017-05-19 18:53:02 -0700275 if (td_ioengine_flagged(td, FIO_SYNCIO)) {
Jens Axboe12d9d842008-10-16 21:26:10 +0200276 if (fio_fill_issue_time(td))
Jens Axboe9520ebb2008-10-16 21:03:27 +0200277 fio_gettime(&io_u->issue_time, NULL);
Jens Axboed0c15322008-10-16 20:33:51 +0200278
279 /*
280 * only used for iolog
281 */
282 if (td->o.read_iolog_file)
283 memcpy(&td->last_issue, &io_u->issue_time,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100284 sizeof(struct timeval));
Jens Axboe433afcb2007-02-22 10:39:01 +0100285 }
286
Elliott Hugheseda3a602017-05-19 18:53:02 -0700287 if (ddir_rw(ddir)) {
288 td->io_issues[ddir]++;
289 td->io_issue_bytes[ddir] += buflen;
290 td->rate_io_issue_bytes[ddir] += buflen;
Jens Axboe5d61d112015-01-16 10:03:11 -0700291 }
Jens Axboe755200a2007-02-19 13:08:12 +0100292
Jens Axboe7e77dd02007-02-20 10:57:34 +0100293 ret = td->io_ops->queue(td, io_u);
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100294
Jens Axboe4d4e80f2008-03-04 10:18:56 +0100295 unlock_file(td, io_u->file);
Jens Axboeb2bd2bd2008-03-01 18:19:52 +0100296
Elliott Hugheseda3a602017-05-19 18:53:02 -0700297 if (ret == FIO_Q_BUSY && ddir_rw(ddir)) {
298 td->io_issues[ddir]--;
299 td->io_issue_bytes[ddir] -= buflen;
300 td->rate_io_issue_bytes[ddir] -= buflen;
301 io_u_clear(td, io_u, IO_U_F_FLIGHT);
Jens Axboe72d300c2015-01-16 10:06:02 -0700302 }
303
Jens Axboecb211682009-07-03 08:38:50 +0200304 /*
Jens Axboe39a43d32013-04-17 19:20:28 +0200305 * If an error was seen and the io engine didn't propagate it
306 * back to 'td', do so.
307 */
308 if (io_u->error && !td->error)
309 td_verror(td, io_u->error, "td_io_queue");
310
311 /*
Jens Axboecb211682009-07-03 08:38:50 +0200312 * Add warning for O_DIRECT so that users have an easier time
313 * spotting potentially bad alignment. If this triggers for the first
314 * IO, then it's likely an alignment problem or because the host fs
315 * does not support O_DIRECT
316 */
Jens Axboeff58fce2010-08-25 12:02:08 +0200317 if (io_u->error == EINVAL && td->io_issues[io_u->ddir & 1] == 1 &&
Jens Axboecb211682009-07-03 08:38:50 +0200318 td->o.odirect) {
Dan Ehrenberg214ac7e2012-03-15 14:44:26 +0100319
Jens Axboecb211682009-07-03 08:38:50 +0200320 log_info("fio: first direct IO errored. File system may not "
Elliott Hugheseda3a602017-05-19 18:53:02 -0700321 "support direct IO, or iomem_align= is bad. Try "
322 "setting direct=0.\n");
Jens Axboecb211682009-07-03 08:38:50 +0200323 }
324
Jens Axboed4eb4652014-11-10 15:40:24 -0700325 if (!td->io_ops->commit || io_u->ddir == DDIR_TRIM) {
Jens Axboe838bc702008-05-22 13:08:23 +0200326 io_u_mark_submit(td, 1);
327 io_u_mark_complete(td, 1);
328 }
329
Jens Axboed8005752008-05-15 09:49:09 +0200330 if (ret == FIO_Q_COMPLETED) {
Jens Axboeff58fce2010-08-25 12:02:08 +0200331 if (ddir_rw(io_u->ddir)) {
Jens Axboed8005752008-05-15 09:49:09 +0200332 io_u_mark_depth(td, 1);
333 td->ts.total_io_u[io_u->ddir]++;
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200334 }
Jens Axboed8005752008-05-15 09:49:09 +0200335 } else if (ret == FIO_Q_QUEUED) {
Jens Axboeeb7c8ae2007-02-26 14:45:12 +0100336 int r;
337
Elliott Hugheseda3a602017-05-19 18:53:02 -0700338 td->io_u_queued++;
339
340 if (ddir_rw(io_u->ddir))
Jens Axboed8005752008-05-15 09:49:09 +0200341 td->ts.total_io_u[io_u->ddir]++;
Jens Axboed8005752008-05-15 09:49:09 +0200342
343 if (td->io_u_queued >= td->o.iodepth_batch) {
Jens Axboeeb7c8ae2007-02-26 14:45:12 +0100344 r = td_io_commit(td);
345 if (r < 0)
346 return r;
347 }
348 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100349
Elliott Hugheseda3a602017-05-19 18:53:02 -0700350 if (!td_ioengine_flagged(td, FIO_SYNCIO)) {
Jens Axboe12d9d842008-10-16 21:26:10 +0200351 if (fio_fill_issue_time(td))
Jens Axboe9520ebb2008-10-16 21:03:27 +0200352 fio_gettime(&io_u->issue_time, NULL);
Jens Axboed0c15322008-10-16 20:33:51 +0200353
354 /*
355 * only used for iolog
356 */
357 if (td->o.read_iolog_file)
358 memcpy(&td->last_issue, &io_u->issue_time,
359 sizeof(struct timeval));
Jens Axboe433afcb2007-02-22 10:39:01 +0100360 }
361
Jens Axboe7e77dd02007-02-20 10:57:34 +0100362 return ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200363}
Jens Axboe8c16d842006-10-20 11:48:33 +0200364
365int td_io_init(struct thread_data *td)
366{
Jens Axboeeeb12162007-03-20 10:47:45 +0100367 int ret = 0;
Jens Axboe8c16d842006-10-20 11:48:33 +0200368
Jens Axboeeeb12162007-03-20 10:47:45 +0100369 if (td->io_ops->init) {
370 ret = td->io_ops->init(td);
Elliott Hugheseda3a602017-05-19 18:53:02 -0700371 if (ret)
372 log_err("fio: io engine %s init failed.%s\n",
373 td->io_ops->name,
374 td->o.iodepth > 1 ?
375 " Perhaps try reducing io depth?" : "");
376 else
377 td->io_ops_init = 1;
Jens Axboe7c973892011-01-22 15:11:03 -0700378 if (!td->error)
379 td->error = ret;
Jens Axboeeeb12162007-03-20 10:47:45 +0100380 }
381
382 return ret;
Jens Axboe8c16d842006-10-20 11:48:33 +0200383}
Jens Axboe755200a2007-02-19 13:08:12 +0100384
385int td_io_commit(struct thread_data *td)
386{
Jens Axboef3e11d02010-03-18 19:31:06 +0100387 int ret;
388
Jens Axboeee56ad52008-02-01 10:30:20 +0100389 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
390
Jens Axboed8005752008-05-15 09:49:09 +0200391 if (!td->cur_depth || !td->io_u_queued)
Jens Axboee1161c32007-02-22 19:36:48 +0100392 return 0;
Jens Axboecb5ab512007-02-26 12:57:09 +0100393
Jens Axboe3fd9efb2013-04-11 08:53:24 +0200394 io_u_mark_depth(td, td->io_u_queued);
Jens Axboed8005752008-05-15 09:49:09 +0200395
Jens Axboef3e11d02010-03-18 19:31:06 +0100396 if (td->io_ops->commit) {
397 ret = td->io_ops->commit(td);
398 if (ret)
399 td_verror(td, -ret, "io commit");
400 }
Jens Axboe3fd9efb2013-04-11 08:53:24 +0200401
Ryan Marchand422f9e42012-01-31 14:05:32 +0100402 /*
403 * Reflect that events were submitted as async IO requests.
404 */
405 td->io_u_in_flight += td->io_u_queued;
406 td->io_u_queued = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100407
408 return 0;
409}
Jens Axboeb5af8292007-03-08 12:43:13 +0100410
411int td_io_open_file(struct thread_data *td, struct fio_file *f)
412{
Jens Axboe22a57ba2009-06-09 14:34:35 +0200413 assert(!fio_file_open(f));
414 assert(f->fd == -1);
415
Jens Axboe413d6692007-03-27 15:38:21 +0200416 if (td->io_ops->open_file(td, f)) {
417 if (td->error == EINVAL && td->o.odirect)
418 log_err("fio: destination does not support O_DIRECT\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100419 if (td->error == EMFILE) {
420 log_err("fio: try reducing/setting openfiles (failed"
421 " at %u of %u)\n", td->nr_open_files,
422 td->o.nr_files);
423 }
Jens Axboeb5af8292007-03-08 12:43:13 +0100424
Jens Axboe22a57ba2009-06-09 14:34:35 +0200425 assert(f->fd == -1);
426 assert(!fio_file_open(f));
Jens Axboe413d6692007-03-27 15:38:21 +0200427 return 1;
Jens Axboe7bb48f82007-03-27 15:30:28 +0200428 }
Jens Axboea978ba62007-03-08 14:29:03 +0100429
Jens Axboe33c48812013-01-21 09:46:06 -0700430 fio_file_reset(td, f);
Jens Axboed6aed792009-06-03 08:41:15 +0200431 fio_file_set_open(f);
432 fio_file_clear_closing(f);
Jens Axboec97bd0f2009-05-27 13:11:20 +0200433 disk_util_inc(f->du);
Jens Axboed5707a32008-06-04 15:13:02 +0200434
435 td->nr_open_files++;
436 get_file(f);
437
Jens Axboe66159822007-04-16 21:54:24 +0200438 if (f->filetype == FIO_TYPE_PIPE) {
439 if (td_random(td)) {
440 log_err("fio: can't seek on pipes (no random io)\n");
441 goto err;
442 }
443 }
444
Elliott Hugheseda3a602017-05-19 18:53:02 -0700445 if (td_ioengine_flagged(td, FIO_DISKLESSIO))
Jens Axboe413d6692007-03-27 15:38:21 +0200446 goto done;
447
448 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
449 goto err;
450
Elliott Hugheseda3a602017-05-19 18:53:02 -0700451 if (td->o.fadvise_hint != F_ADV_NONE &&
452 (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
Jens Axboe413d6692007-03-27 15:38:21 +0200453 int flags;
454
Elliott Hugheseda3a602017-05-19 18:53:02 -0700455 if (td->o.fadvise_hint == F_ADV_TYPE) {
456 if (td_random(td))
457 flags = POSIX_FADV_RANDOM;
458 else
459 flags = POSIX_FADV_SEQUENTIAL;
460 } else if (td->o.fadvise_hint == F_ADV_RANDOM)
Jens Axboe413d6692007-03-27 15:38:21 +0200461 flags = POSIX_FADV_RANDOM;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700462 else if (td->o.fadvise_hint == F_ADV_SEQUENTIAL)
Jens Axboe413d6692007-03-27 15:38:21 +0200463 flags = POSIX_FADV_SEQUENTIAL;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700464 else {
465 log_err("fio: unknown fadvise type %d\n",
466 td->o.fadvise_hint);
467 flags = POSIX_FADV_NORMAL;
468 }
Jens Axboe413d6692007-03-27 15:38:21 +0200469
Bruce Cranecc314b2011-01-04 10:59:30 +0100470 if (posix_fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
Jens Axboe413d6692007-03-27 15:38:21 +0200471 td_verror(td, errno, "fadvise");
472 goto err;
473 }
474 }
Elliott Hugheseda3a602017-05-19 18:53:02 -0700475#ifdef FIO_HAVE_STREAMID
476 if (td->o.fadvise_stream &&
477 (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
478 off_t stream = td->o.fadvise_stream;
479
480 if (posix_fadvise(f->fd, stream, f->io_size, POSIX_FADV_STREAMID) < 0) {
481 td_verror(td, errno, "fadvise streamid");
482 goto err;
483 }
484 }
485#endif
Jens Axboe413d6692007-03-27 15:38:21 +0200486
Jens Axboee116f2b2008-06-04 15:14:24 +0200487#ifdef FIO_OS_DIRECTIO
488 /*
489 * Some OS's have a distinct call to mark the file non-buffered,
490 * instead of using O_DIRECT (Solaris)
491 */
492 if (td->o.odirect) {
493 int ret = fio_set_odirect(f->fd);
494
495 if (ret) {
496 td_verror(td, ret, "fio_set_odirect");
Elliott Hugheseda3a602017-05-19 18:53:02 -0700497 if (ret == ENOTTY) { /* ENOTTY suggests RAW device or ZFS */
498 log_err("fio: doing directIO to RAW devices or ZFS not supported\n");
499 } else {
500 log_err("fio: the file system does not seem to support direct IO\n");
501 }
502
Jens Axboee116f2b2008-06-04 15:14:24 +0200503 goto err;
504 }
505 }
506#endif
507
Jens Axboe413d6692007-03-27 15:38:21 +0200508done:
Jens Axboef29b25a2007-07-23 08:56:43 +0200509 log_file(td, f, FIO_LOG_OPEN_FILE);
Jens Axboe413d6692007-03-27 15:38:21 +0200510 return 0;
511err:
Jens Axboec97bd0f2009-05-27 13:11:20 +0200512 disk_util_dec(f->du);
Jens Axboeb2840752007-04-12 12:57:27 +0200513 if (td->io_ops->close_file)
514 td->io_ops->close_file(td, f);
Jens Axboe7bb48f82007-03-27 15:30:28 +0200515 return 1;
Jens Axboeb5af8292007-03-08 12:43:13 +0100516}
517
Jens Axboe6977bcd2008-03-01 15:55:36 +0100518int td_io_close_file(struct thread_data *td, struct fio_file *f)
Jens Axboeb5af8292007-03-08 12:43:13 +0100519{
Jens Axboed6aed792009-06-03 08:41:15 +0200520 if (!fio_file_closing(f))
Jens Axboef29b25a2007-07-23 08:56:43 +0200521 log_file(td, f, FIO_LOG_CLOSE_FILE);
522
Jens Axboe0ad920e2007-03-13 11:06:45 +0100523 /*
524 * mark as closing, do real close when last io on it has completed
525 */
Jens Axboed6aed792009-06-03 08:41:15 +0200526 fio_file_set_closing(f);
Jens Axboe0ad920e2007-03-13 11:06:45 +0100527
Jens Axboec97bd0f2009-05-27 13:11:20 +0200528 disk_util_dec(f->du);
Jens Axboecba52432013-03-21 10:06:58 -0600529
530 if (td->o.file_lock_mode != FILE_LOCK_NONE)
531 unlock_file_all(td, f);
Jens Axboe29c13492008-03-01 19:25:20 +0100532
Jens Axboe6977bcd2008-03-01 15:55:36 +0100533 return put_file(td, f);
Jens Axboeb5af8292007-03-08 12:43:13 +0100534}
Jens Axboedf9c26b2009-03-05 10:13:58 +0100535
Castor Fu9187a262014-08-19 09:28:53 -0700536int td_io_unlink_file(struct thread_data *td, struct fio_file *f)
537{
538 if (td->io_ops->unlink_file)
539 return td->io_ops->unlink_file(td, f);
Elliott Hugheseda3a602017-05-19 18:53:02 -0700540 else {
541 int ret;
542
543 ret = unlink(f->file_name);
544 if (ret < 0)
545 return errno;
546
547 return 0;
548 }
Castor Fu9187a262014-08-19 09:28:53 -0700549}
550
Jens Axboedf9c26b2009-03-05 10:13:58 +0100551int td_io_get_file_size(struct thread_data *td, struct fio_file *f)
552{
553 if (!td->io_ops->get_file_size)
554 return 0;
555
556 return td->io_ops->get_file_size(td, f);
557}
Jens Axboe44f29692010-03-09 20:09:44 +0100558
Steven Langde890a12011-11-09 14:03:34 +0100559int fio_show_ioengine_help(const char *engine)
560{
561 struct flist_head *entry;
562 struct thread_data td;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700563 struct ioengine_ops *io_ops;
Steven Langde890a12011-11-09 14:03:34 +0100564 char *sep;
565 int ret = 1;
566
567 if (!engine || !*engine) {
568 log_info("Available IO engines:\n");
569 flist_for_each(entry, &engine_list) {
Elliott Hugheseda3a602017-05-19 18:53:02 -0700570 io_ops = flist_entry(entry, struct ioengine_ops, list);
571 log_info("\t%s\n", io_ops->name);
Steven Langde890a12011-11-09 14:03:34 +0100572 }
573 return 0;
574 }
575 sep = strchr(engine, ',');
576 if (sep) {
577 *sep = 0;
578 sep++;
579 }
580
581 memset(&td, 0, sizeof(td));
582
Elliott Hugheseda3a602017-05-19 18:53:02 -0700583 io_ops = load_ioengine(&td, engine);
584 if (!io_ops) {
Steven Langde890a12011-11-09 14:03:34 +0100585 log_info("IO engine %s not found\n", engine);
586 return 1;
587 }
588
Elliott Hugheseda3a602017-05-19 18:53:02 -0700589 if (io_ops->options)
590 ret = show_cmd_help(io_ops->options, sep);
Steven Langde890a12011-11-09 14:03:34 +0100591 else
Elliott Hugheseda3a602017-05-19 18:53:02 -0700592 log_info("IO engine %s has no options\n", io_ops->name);
Steven Langde890a12011-11-09 14:03:34 +0100593
594 free_ioengine(&td);
595
596 return ret;
597}