Fix problem with treating ENOENT as an error
[fio.git] / ioengines.c
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>
15 #include <string.h>
16 #include <dlfcn.h>
17 #include <assert.h>
18
19 #include "fio.h"
20
21 static LIST_HEAD(engine_list);
22
23 static int check_engine_ops(struct ioengine_ops *ops)
24 {
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
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         
41         if (!ops->event) {
42                 log_err("%s: no event handler\n", ops->name);
43                 return 1;
44         }
45         if (!ops->getevents) {
46                 log_err("%s: no getevents handler\n", ops->name);
47                 return 1;
48         }
49         if (!ops->queue) {
50                 log_err("%s: no queue handler\n", ops->name);
51                 return 1;
52         }
53                 
54         return 0;
55 }
56
57 void unregister_ioengine(struct ioengine_ops *ops)
58 {
59         list_del(&ops->list);
60         INIT_LIST_HEAD(&ops->list);
61 }
62
63 void register_ioengine(struct ioengine_ops *ops)
64 {
65         INIT_LIST_HEAD(&ops->list);
66         list_add_tail(&ops->list, &engine_list);
67 }
68
69 static struct ioengine_ops *find_ioengine(const char *name)
70 {
71         struct ioengine_ops *ops;
72         struct list_head *entry;
73
74         list_for_each(entry, &engine_list) {
75                 ops = list_entry(entry, struct ioengine_ops, list);
76                 if (!strcmp(name, ops->name))
77                         return ops;
78         }
79
80         return NULL;
81 }
82
83 static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
84                                             const char *engine_lib)
85 {
86         struct ioengine_ops *ops;
87         void *dlhandle;
88
89         dlerror();
90         dlhandle = dlopen(engine_lib, RTLD_LAZY);
91         if (!dlhandle) {
92                 td_vmsg(td, -1, dlerror(), "dlopen");
93                 return NULL;
94         }
95
96         /*
97          * Unlike the included modules, external engines should have a
98          * non-static ioengine structure that we can reference.
99          */
100         ops = dlsym(dlhandle, "ioengine");
101         if (!ops) {
102                 td_vmsg(td, -1, dlerror(), "dlsym");
103                 dlclose(dlhandle);
104                 return NULL;
105         }
106
107         ops->dlhandle = dlhandle;
108         return ops;
109 }
110
111 struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
112 {
113         struct ioengine_ops *ops, *ret;
114         char engine[16];
115
116         strncpy(engine, name, sizeof(engine) - 1);
117
118         /*
119          * linux libaio has alias names, so convert to what we want
120          */
121         if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
122                 strcpy(engine, "libaio");
123
124         ops = find_ioengine(engine);
125         if (!ops)
126                 ops = dlopen_ioengine(td, name);
127
128         if (!ops) {
129                 log_err("fio: engine %s not loadable\n", name);
130                 return NULL;
131         }
132
133         /*
134          * Check that the required methods are there.
135          */
136         if (check_engine_ops(ops))
137                 return NULL;
138
139         ret = malloc(sizeof(*ret));
140         memcpy(ret, ops, sizeof(*ret));
141         ret->data = NULL;
142
143         return ret;
144 }
145
146 void close_ioengine(struct thread_data *td)
147 {
148         if (td->io_ops->cleanup)
149                 td->io_ops->cleanup(td);
150
151         if (td->io_ops->dlhandle)
152                 dlclose(td->io_ops->dlhandle);
153
154         free(td->io_ops);
155         td->io_ops = NULL;
156 }
157
158 int td_io_prep(struct thread_data *td, struct io_u *io_u)
159 {
160         if (td->io_ops->prep)
161                 return td->io_ops->prep(td, io_u);
162
163         return 0;
164 }
165
166 int td_io_getevents(struct thread_data *td, int min, int max,
167                     struct timespec *t)
168 {
169         if (min > 0 && td->io_ops->commit) {
170                 int r = td->io_ops->commit(td);
171
172                 if (r < 0)
173                         return r;
174         }
175         if (td->io_ops->getevents)
176                 return td->io_ops->getevents(td, min, max, t);
177
178         return 0;
179 }
180
181 int td_io_queue(struct thread_data *td, struct io_u *io_u)
182 {
183         int ret;
184
185         assert((io_u->flags & IO_U_F_FLIGHT) == 0);
186         io_u->flags |= IO_U_F_FLIGHT;
187
188         assert(io_u->file->flags & FIO_FILE_OPEN);
189
190         io_u->error = 0;
191         io_u->resid = 0;
192
193         if (td->io_ops->flags & FIO_SYNCIO) {
194                 fio_gettime(&io_u->issue_time, NULL);
195
196                 /*
197                  * for a sync engine, set the timeout upfront
198                  */
199                 if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
200                         io_u_set_timeout(td);
201         }
202
203         if (io_u->ddir != DDIR_SYNC)
204                 td->io_issues[io_u->ddir]++;
205
206         io_u_mark_depth(td, io_u);
207
208         ret = td->io_ops->queue(td, io_u);
209
210         if (ret == FIO_Q_QUEUED || ret == FIO_Q_COMPLETED)
211                 get_file(io_u->file);
212
213         if (ret == FIO_Q_QUEUED) {
214                 int r;
215
216                 td->io_u_queued++;
217                 if (td->io_u_queued > td->o.iodepth_batch) {
218                         r = td_io_commit(td);
219                         if (r < 0)
220                                 return r;
221                 }
222         }
223
224         if ((td->io_ops->flags & FIO_SYNCIO) == 0) {
225                 fio_gettime(&io_u->issue_time, NULL);
226
227                 /*
228                  * async engine, set the timeout here
229                  */
230                 if (ret == FIO_Q_QUEUED &&
231                     mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT)
232                         io_u_set_timeout(td);
233         }
234
235         return ret;
236 }
237
238 int td_io_init(struct thread_data *td)
239 {
240         int ret = 0;
241
242         if (td->io_ops->init) {
243                 ret = td->io_ops->init(td);
244                 if (ret && td->o.iodepth > 1)
245                         log_err("fio: io engine init failed. Perhaps try reducing io dpeth?\n");
246         }
247
248         return ret;
249 }
250
251 int td_io_commit(struct thread_data *td)
252 {
253         if (!td->cur_depth)
254                 return 0;
255
256         td->io_u_queued = 0;
257         if (td->io_ops->commit)
258                 return td->io_ops->commit(td);
259
260         return 0;
261 }
262
263 int td_io_open_file(struct thread_data *td, struct fio_file *f)
264 {
265         if (td->io_ops->open_file(td, f)) {
266                 if (td->error == EINVAL && td->o.odirect)
267                         log_err("fio: destination does not support O_DIRECT\n");
268                 if (td->error == EMFILE)
269                         log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files);
270
271                 return 1;
272         }
273
274         f->last_free_lookup = 0;
275         f->last_completed_pos = 0;
276         f->last_pos = 0;
277         f->flags |= FIO_FILE_OPEN;
278         f->flags &= ~FIO_FILE_CLOSING;
279
280         if (td->io_ops->flags & FIO_DISKLESSIO)
281                 goto done;
282
283         if (td->o.invalidate_cache && file_invalidate_cache(td, f))
284                 goto err;
285
286         if (td->o.fadvise_hint) {
287                 int flags;
288
289                 if (td_random(td))
290                         flags = POSIX_FADV_RANDOM;
291                 else
292                         flags = POSIX_FADV_SEQUENTIAL;
293
294                 if (fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
295                         td_verror(td, errno, "fadvise");
296                         goto err;
297                 }
298         }
299
300         if (f->file_map)
301                 memset(f->file_map, 0, f->num_maps * sizeof(long));
302
303 done:
304         td->nr_open_files++;
305         get_file(f);
306         return 0;
307 err:
308         td->io_ops->close_file(td, f);
309         return 1;
310 }
311
312 void td_io_close_file(struct thread_data *td, struct fio_file *f)
313 {
314         /*
315          * mark as closing, do real close when last io on it has completed
316          */
317         f->flags |= FIO_FILE_CLOSING;
318
319         put_file(td, f);
320 }
321