Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 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> |
Jens Axboe | 5c4e1db | 2006-06-07 14:17:08 +0200 | [diff] [blame] | 15 | #include <string.h> |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 16 | #include <dlfcn.h> |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 17 | #include <assert.h> |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 18 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 19 | #include "fio.h" |
| 20 | #include "os.h" |
| 21 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 22 | static LIST_HEAD(engine_list); |
| 23 | |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 24 | static int check_engine_ops(struct ioengine_ops *ops) |
| 25 | { |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 26 | 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 Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 31 | /* |
| 32 | * cpu thread doesn't need to provide anything |
| 33 | */ |
| 34 | if (ops->flags & FIO_CPUIO) |
| 35 | return 0; |
| 36 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 37 | 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 Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 48 | if (!ops->event) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 49 | log_err("%s: no event handler\n", ops->name); |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 50 | return 1; |
| 51 | } |
| 52 | if (!ops->getevents) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 53 | log_err("%s: no getevents handler\n", ops->name); |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 54 | return 1; |
| 55 | } |
| 56 | if (!ops->queue) { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 57 | log_err("%s: no queue handler\n", ops->name); |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 64 | void unregister_ioengine(struct ioengine_ops *ops) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 65 | { |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 66 | list_del(&ops->list); |
| 67 | INIT_LIST_HEAD(&ops->list); |
| 68 | } |
| 69 | |
Jens Axboe | b2fdda4 | 2007-02-20 20:52:51 +0100 | [diff] [blame] | 70 | void register_ioengine(struct ioengine_ops *ops) |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 71 | { |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 72 | INIT_LIST_HEAD(&ops->list); |
| 73 | list_add_tail(&ops->list, &engine_list); |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static struct ioengine_ops *find_ioengine(const char *name) |
| 77 | { |
| 78 | struct ioengine_ops *ops; |
| 79 | struct list_head *entry; |
| 80 | char engine[16]; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 81 | |
Jens Axboe | 0bbab0e | 2006-11-02 09:18:36 +0100 | [diff] [blame] | 82 | strncpy(engine, name, sizeof(engine) - 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 83 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 84 | if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3)) |
| 85 | strcpy(engine, "libaio"); |
| 86 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 87 | 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 | |
| 96 | static 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 Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 102 | dlerror(); |
| 103 | dlhandle = dlopen(engine_lib, RTLD_LAZY); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 104 | if (!dlhandle) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 105 | td_vmsg(td, -1, dlerror(), "dlopen"); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 106 | return NULL; |
| 107 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 108 | |
Jens Axboe | da51c05 | 2006-11-07 16:02:11 +0100 | [diff] [blame] | 109 | /* |
| 110 | * Unlike the included modules, external engines should have a |
| 111 | * non-static ioengine structure that we can reference. |
| 112 | */ |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 113 | ops = dlsym(dlhandle, "ioengine"); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 114 | if (!ops) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 115 | td_vmsg(td, -1, dlerror(), "dlsym"); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 116 | dlclose(dlhandle); |
| 117 | return NULL; |
| 118 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 119 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 120 | ops->dlhandle = dlhandle; |
| 121 | return ops; |
| 122 | } |
| 123 | |
| 124 | struct 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 Axboe | b902ceb | 2006-10-09 16:11:45 +0200 | [diff] [blame] | 143 | return NULL; |
| 144 | } |
| 145 | |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 146 | /* |
| 147 | * Check that the required methods are there. |
| 148 | */ |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 149 | if (check_engine_ops(ops)) |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 150 | return NULL; |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 151 | |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 152 | ret = malloc(sizeof(*ret)); |
| 153 | memcpy(ret, ops, sizeof(*ret)); |
| 154 | ret->data = NULL; |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 155 | |
| 156 | return ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 159 | void close_ioengine(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 160 | { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 161 | if (td->io_ops->cleanup) |
| 162 | td->io_ops->cleanup(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 163 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 164 | if (td->io_ops->dlhandle) |
| 165 | dlclose(td->io_ops->dlhandle); |
| 166 | |
Jens Axboe | 19a98c4 | 2007-02-22 12:26:20 +0100 | [diff] [blame] | 167 | #if 0 |
| 168 | /* we can't do this for threads, so just leak it, it's exiting */ |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 169 | free(td->io_ops); |
Jens Axboe | 19a98c4 | 2007-02-22 12:26:20 +0100 | [diff] [blame] | 170 | #endif |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 171 | td->io_ops = NULL; |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 172 | } |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 173 | |
| 174 | int td_io_prep(struct thread_data *td, struct io_u *io_u) |
| 175 | { |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 176 | if (td->io_ops->prep) |
| 177 | return td->io_ops->prep(td, io_u); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 182 | int td_io_getevents(struct thread_data *td, int min, int max, |
| 183 | struct timespec *t) |
| 184 | { |
Jens Axboe | face81b | 2007-02-26 10:40:03 +0100 | [diff] [blame] | 185 | if (min > 0 && td->io_ops->commit) { |
| 186 | int r = td->io_ops->commit(td); |
| 187 | |
| 188 | if (r < 0) |
| 189 | return r; |
| 190 | } |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 191 | if (td->io_ops->getevents) |
| 192 | return td->io_ops->getevents(td, min, max, t); |
| 193 | |
| 194 | return 0; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | int td_io_queue(struct thread_data *td, struct io_u *io_u) |
| 198 | { |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 199 | int ret; |
| 200 | |
Jens Axboe | 0c6e751 | 2007-02-22 11:19:39 +0100 | [diff] [blame] | 201 | assert((io_u->flags & IO_U_F_FLIGHT) == 0); |
| 202 | io_u->flags |= IO_U_F_FLIGHT; |
| 203 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 204 | if (td->io_ops->flags & FIO_SYNCIO) { |
Jens Axboe | 5aeb77d | 2007-02-20 11:34:54 +0100 | [diff] [blame] | 205 | fio_gettime(&io_u->issue_time, NULL); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 206 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 207 | /* |
| 208 | * for a sync engine, set the timeout upfront |
| 209 | */ |
| 210 | if (mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT) |
| 211 | io_u_set_timeout(td); |
| 212 | } |
| 213 | |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 214 | if (io_u->ddir != DDIR_SYNC) |
| 215 | td->io_issues[io_u->ddir]++; |
| 216 | |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 217 | ret = td->io_ops->queue(td, io_u); |
Jens Axboe | 5aeb77d | 2007-02-20 11:34:54 +0100 | [diff] [blame] | 218 | |
Jens Axboe | eb7c8ae | 2007-02-26 14:45:12 +0100 | [diff] [blame^] | 219 | if (ret == FIO_Q_QUEUED) { |
| 220 | int r; |
| 221 | |
Jens Axboe | cb5ab51 | 2007-02-26 12:57:09 +0100 | [diff] [blame] | 222 | td->io_u_queued++; |
Jens Axboe | eb7c8ae | 2007-02-26 14:45:12 +0100 | [diff] [blame^] | 223 | if (td->io_u_queued > td->iodepth_batch) { |
| 224 | r = td_io_commit(td); |
| 225 | if (r < 0) |
| 226 | return r; |
| 227 | } |
| 228 | } |
Jens Axboe | cb5ab51 | 2007-02-26 12:57:09 +0100 | [diff] [blame] | 229 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 230 | if ((td->io_ops->flags & FIO_SYNCIO) == 0) { |
Jens Axboe | 5aeb77d | 2007-02-20 11:34:54 +0100 | [diff] [blame] | 231 | fio_gettime(&io_u->issue_time, NULL); |
| 232 | |
Jens Axboe | 433afcb | 2007-02-22 10:39:01 +0100 | [diff] [blame] | 233 | /* |
| 234 | * async engine, set the timeout here |
| 235 | */ |
| 236 | if (ret == FIO_Q_QUEUED && |
| 237 | mtime_since(&td->timeout_end, &io_u->issue_time) < IO_U_TIMEOUT) |
| 238 | io_u_set_timeout(td); |
| 239 | } |
| 240 | |
Jens Axboe | 7e77dd0 | 2007-02-20 10:57:34 +0100 | [diff] [blame] | 241 | return ret; |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 242 | } |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 243 | |
| 244 | int td_io_init(struct thread_data *td) |
| 245 | { |
| 246 | if (td->io_ops->init) |
| 247 | return td->io_ops->init(td); |
| 248 | |
| 249 | return 0; |
| 250 | } |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 251 | |
| 252 | int td_io_commit(struct thread_data *td) |
| 253 | { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 254 | if (!td->cur_depth) |
| 255 | return 0; |
Jens Axboe | cb5ab51 | 2007-02-26 12:57:09 +0100 | [diff] [blame] | 256 | |
| 257 | td->io_u_queued = 0; |
Jens Axboe | 755200a | 2007-02-19 13:08:12 +0100 | [diff] [blame] | 258 | if (td->io_ops->commit) |
| 259 | return td->io_ops->commit(td); |
| 260 | |
| 261 | return 0; |
| 262 | } |