blob: 1704418735367e92e48dfb42f5fb85e0902eed6a [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>
Jens Axboe0c6e7512007-02-22 11:19:39 +010017#include <assert.h>
Jens Axboe8c16d842006-10-20 11:48:33 +020018
Jens Axboeebac4652005-12-08 15:25:21 +010019#include "fio.h"
20#include "os.h"
21
Jens Axboe5f350952006-11-07 15:20:59 +010022static LIST_HEAD(engine_list);
23
Jens Axboe8c16d842006-10-20 11:48:33 +020024static int check_engine_ops(struct ioengine_ops *ops)
25{
Jens Axboe5f350952006-11-07 15:20:59 +010026 if (ops->version != FIO_IOOPS_VERSION) {
27 log_err("bad ioops version %d (want %d)\n", ops->version, FIO_IOOPS_VERSION);
28 return 1;
29 }
30
Jens Axboe8c16d842006-10-20 11:48:33 +020031 /*
32 * cpu thread doesn't need to provide anything
33 */
34 if (ops->flags & FIO_CPUIO)
35 return 0;
36
Jens Axboe36167d82007-02-18 05:41:31 +010037 if (!ops->queue) {
38 log_err("%s: no queue handler\n", ops->name);
39 return 1;
40 }
41
42 /*
43 * sync engines only need a ->queue()
44 */
45 if (ops->flags & FIO_SYNCIO)
46 return 0;
47
Jens Axboe8c16d842006-10-20 11:48:33 +020048 if (!ops->event) {
Jens Axboe36167d82007-02-18 05:41:31 +010049 log_err("%s: no event handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020050 return 1;
51 }
52 if (!ops->getevents) {
Jens Axboe36167d82007-02-18 05:41:31 +010053 log_err("%s: no getevents handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020054 return 1;
55 }
56 if (!ops->queue) {
Jens Axboe36167d82007-02-18 05:41:31 +010057 log_err("%s: no queue handler\n", ops->name);
Jens Axboe8c16d842006-10-20 11:48:33 +020058 return 1;
59 }
60
61 return 0;
62}
63
Jens Axboe5f350952006-11-07 15:20:59 +010064void unregister_ioengine(struct ioengine_ops *ops)
Jens Axboeebac4652005-12-08 15:25:21 +010065{
Jens Axboe5f350952006-11-07 15:20:59 +010066 list_del(&ops->list);
67 INIT_LIST_HEAD(&ops->list);
68}
69
Jens Axboeb2fdda42007-02-20 20:52:51 +010070void register_ioengine(struct ioengine_ops *ops)
Jens Axboe5f350952006-11-07 15:20:59 +010071{
Jens Axboe5f350952006-11-07 15:20:59 +010072 INIT_LIST_HEAD(&ops->list);
73 list_add_tail(&ops->list, &engine_list);
Jens Axboe5f350952006-11-07 15:20:59 +010074}
75
76static struct ioengine_ops *find_ioengine(const char *name)
77{
78 struct ioengine_ops *ops;
79 struct list_head *entry;
80 char engine[16];
Jens Axboeebac4652005-12-08 15:25:21 +010081
Jens Axboe0bbab0e2006-11-02 09:18:36 +010082 strncpy(engine, name, sizeof(engine) - 1);
Jens Axboeebac4652005-12-08 15:25:21 +010083
Jens Axboe2866c822006-10-09 15:57:48 +020084 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
85 strcpy(engine, "libaio");
86
Jens Axboe5f350952006-11-07 15:20:59 +010087 list_for_each(entry, &engine_list) {
88 ops = list_entry(entry, struct ioengine_ops, list);
89 if (!strcmp(engine, ops->name))
90 return ops;
91 }
92
93 return NULL;
94}
95
96static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
97 const char *engine_lib)
98{
99 struct ioengine_ops *ops;
100 void *dlhandle;
101
Jens Axboe2866c822006-10-09 15:57:48 +0200102 dlerror();
103 dlhandle = dlopen(engine_lib, RTLD_LAZY);
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200104 if (!dlhandle) {
105 td_vmsg(td, -1, dlerror());
106 return NULL;
107 }
Jens Axboe2866c822006-10-09 15:57:48 +0200108
Jens Axboeda51c052006-11-07 16:02:11 +0100109 /*
110 * Unlike the included modules, external engines should have a
111 * non-static ioengine structure that we can reference.
112 */
Jens Axboe2866c822006-10-09 15:57:48 +0200113 ops = dlsym(dlhandle, "ioengine");
Jens Axboed4dbaaa2006-10-09 16:01:27 +0200114 if (!ops) {
115 td_vmsg(td, -1, dlerror());
116 dlclose(dlhandle);
117 return NULL;
118 }
Jens Axboe2866c822006-10-09 15:57:48 +0200119
Jens Axboe5f350952006-11-07 15:20:59 +0100120 ops->dlhandle = dlhandle;
121 return ops;
122}
123
124struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name)
125{
126 struct ioengine_ops *ops, *ret;
127 char engine[16];
128
129 strncpy(engine, name, sizeof(engine) - 1);
130
131 /*
132 * linux libaio has alias names, so convert to what we want
133 */
134 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3))
135 strcpy(engine, "libaio");
136
137 ops = find_ioengine(engine);
138 if (!ops)
139 ops = dlopen_ioengine(td, name);
140
141 if (!ops) {
142 log_err("fio: engine %s not loadable\n", name);
Jens Axboeb902ceb2006-10-09 16:11:45 +0200143 return NULL;
144 }
145
Jens Axboe8c16d842006-10-20 11:48:33 +0200146 /*
147 * Check that the required methods are there.
148 */
Jens Axboe5f350952006-11-07 15:20:59 +0100149 if (check_engine_ops(ops))
Jens Axboe8c16d842006-10-20 11:48:33 +0200150 return NULL;
Jens Axboe8c16d842006-10-20 11:48:33 +0200151
Jens Axboe84585002006-10-19 20:26:22 +0200152 ret = malloc(sizeof(*ret));
153 memcpy(ret, ops, sizeof(*ret));
154 ret->data = NULL;
Jens Axboe84585002006-10-19 20:26:22 +0200155
156 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100157}
158
Jens Axboe2866c822006-10-09 15:57:48 +0200159void close_ioengine(struct thread_data *td)
Jens Axboeebac4652005-12-08 15:25:21 +0100160{
Jens Axboe2866c822006-10-09 15:57:48 +0200161 if (td->io_ops->cleanup)
162 td->io_ops->cleanup(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100163
Jens Axboe5f350952006-11-07 15:20:59 +0100164 if (td->io_ops->dlhandle)
165 dlclose(td->io_ops->dlhandle);
166
Jens Axboe84585002006-10-19 20:26:22 +0200167 free(td->io_ops);
168 td->io_ops = NULL;
Jens Axboeb990b5c2006-09-14 09:48:22 +0200169}
Jens Axboe10ba5352006-10-20 11:39:27 +0200170
171int td_io_prep(struct thread_data *td, struct io_u *io_u)
172{
Jens Axboe36167d82007-02-18 05:41:31 +0100173 if (td->io_ops->prep)
174 return td->io_ops->prep(td, io_u);
Jens Axboe10ba5352006-10-20 11:39:27 +0200175
176 return 0;
177}
178
Jens Axboe10ba5352006-10-20 11:39:27 +0200179int td_io_getevents(struct thread_data *td, int min, int max,
180 struct timespec *t)
181{
Jens Axboe36167d82007-02-18 05:41:31 +0100182 if (td->io_ops->getevents)
183 return td->io_ops->getevents(td, min, max, t);
184
185 return 0;
Jens Axboe10ba5352006-10-20 11:39:27 +0200186}
187
188int td_io_queue(struct thread_data *td, struct io_u *io_u)
189{
Jens Axboe7e77dd02007-02-20 10:57:34 +0100190 int ret;
191
Jens Axboe0c6e7512007-02-22 11:19:39 +0100192 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
193 io_u->flags |= IO_U_F_FLIGHT;
194
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100195 if (td->io_ops->flags & FIO_SYNCIO)
196 fio_gettime(&io_u->issue_time, NULL);
Jens Axboe10ba5352006-10-20 11:39:27 +0200197
Jens Axboe755200a2007-02-19 13:08:12 +0100198 if (io_u->ddir != DDIR_SYNC)
199 td->io_issues[io_u->ddir]++;
200
Jens Axboe7e77dd02007-02-20 10:57:34 +0100201 ret = td->io_ops->queue(td, io_u);
Jens Axboe5aeb77d2007-02-20 11:34:54 +0100202
203 if ((td->io_ops->flags & FIO_SYNCIO) == 0)
204 fio_gettime(&io_u->issue_time, NULL);
205
Jens Axboe7e77dd02007-02-20 10:57:34 +0100206 return ret;
Jens Axboe10ba5352006-10-20 11:39:27 +0200207}
Jens Axboe8c16d842006-10-20 11:48:33 +0200208
209int td_io_init(struct thread_data *td)
210{
211 if (td->io_ops->init)
212 return td->io_ops->init(td);
213
214 return 0;
215}
Jens Axboe755200a2007-02-19 13:08:12 +0100216
217int td_io_commit(struct thread_data *td)
218{
219 if (td->io_ops->commit)
220 return td->io_ops->commit(td);
221
222 return 0;
223}