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 | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 17 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 18 | #include "fio.h" |
| 19 | #include "os.h" |
| 20 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 21 | static LIST_HEAD(engine_list); |
| 22 | |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 23 | static int check_engine_ops(struct ioengine_ops *ops) |
| 24 | { |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 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 | |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 30 | /* |
| 31 | * cpu thread doesn't need to provide anything |
| 32 | */ |
| 33 | if (ops->flags & FIO_CPUIO) |
| 34 | return 0; |
| 35 | |
| 36 | if (!ops->event) { |
| 37 | log_err("%s: no event handler)\n", ops->name); |
| 38 | return 1; |
| 39 | } |
| 40 | if (!ops->getevents) { |
| 41 | log_err("%s: no getevents handler)\n", ops->name); |
| 42 | return 1; |
| 43 | } |
| 44 | if (!ops->queue) { |
| 45 | log_err("%s: no queue handler)\n", ops->name); |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 52 | void unregister_ioengine(struct ioengine_ops *ops) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 53 | { |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 54 | list_del(&ops->list); |
| 55 | INIT_LIST_HEAD(&ops->list); |
| 56 | } |
| 57 | |
| 58 | int register_ioengine(struct ioengine_ops *ops) |
| 59 | { |
| 60 | if (check_engine_ops(ops)) |
| 61 | return 1; |
| 62 | |
| 63 | INIT_LIST_HEAD(&ops->list); |
| 64 | list_add_tail(&ops->list, &engine_list); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static struct ioengine_ops *find_ioengine(const char *name) |
| 69 | { |
| 70 | struct ioengine_ops *ops; |
| 71 | struct list_head *entry; |
| 72 | char engine[16]; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 73 | |
Jens Axboe | 0bbab0e | 2006-11-02 09:18:36 +0100 | [diff] [blame] | 74 | strncpy(engine, name, sizeof(engine) - 1); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 75 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 76 | if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3)) |
| 77 | strcpy(engine, "libaio"); |
| 78 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 79 | list_for_each(entry, &engine_list) { |
| 80 | ops = list_entry(entry, struct ioengine_ops, list); |
| 81 | if (!strcmp(engine, ops->name)) |
| 82 | return ops; |
| 83 | } |
| 84 | |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | static struct ioengine_ops *dlopen_ioengine(struct thread_data *td, |
| 89 | const char *engine_lib) |
| 90 | { |
| 91 | struct ioengine_ops *ops; |
| 92 | void *dlhandle; |
| 93 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 94 | dlerror(); |
| 95 | dlhandle = dlopen(engine_lib, RTLD_LAZY); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 96 | if (!dlhandle) { |
| 97 | td_vmsg(td, -1, dlerror()); |
| 98 | return NULL; |
| 99 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 100 | |
Jens Axboe | da51c05 | 2006-11-07 16:02:11 +0100 | [diff] [blame] | 101 | /* |
| 102 | * Unlike the included modules, external engines should have a |
| 103 | * non-static ioengine structure that we can reference. |
| 104 | */ |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 105 | ops = dlsym(dlhandle, "ioengine"); |
Jens Axboe | d4dbaaa | 2006-10-09 16:01:27 +0200 | [diff] [blame] | 106 | if (!ops) { |
| 107 | td_vmsg(td, -1, dlerror()); |
| 108 | dlclose(dlhandle); |
| 109 | return NULL; |
| 110 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 111 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 112 | ops->dlhandle = dlhandle; |
| 113 | return ops; |
| 114 | } |
| 115 | |
| 116 | struct ioengine_ops *load_ioengine(struct thread_data *td, const char *name) |
| 117 | { |
| 118 | struct ioengine_ops *ops, *ret; |
| 119 | char engine[16]; |
| 120 | |
| 121 | strncpy(engine, name, sizeof(engine) - 1); |
| 122 | |
| 123 | /* |
| 124 | * linux libaio has alias names, so convert to what we want |
| 125 | */ |
| 126 | if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3)) |
| 127 | strcpy(engine, "libaio"); |
| 128 | |
| 129 | ops = find_ioengine(engine); |
| 130 | if (!ops) |
| 131 | ops = dlopen_ioengine(td, name); |
| 132 | |
| 133 | if (!ops) { |
| 134 | log_err("fio: engine %s not loadable\n", name); |
Jens Axboe | b902ceb | 2006-10-09 16:11:45 +0200 | [diff] [blame] | 135 | return NULL; |
| 136 | } |
| 137 | |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 138 | /* |
| 139 | * Check that the required methods are there. |
| 140 | */ |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 141 | if (check_engine_ops(ops)) |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 142 | return NULL; |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 143 | |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 144 | ret = malloc(sizeof(*ret)); |
| 145 | memcpy(ret, ops, sizeof(*ret)); |
| 146 | ret->data = NULL; |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 147 | |
| 148 | return ret; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 151 | void close_ioengine(struct thread_data *td) |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 152 | { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 153 | if (td->io_ops->cleanup) |
| 154 | td->io_ops->cleanup(td); |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 155 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 156 | if (td->io_ops->dlhandle) |
| 157 | dlclose(td->io_ops->dlhandle); |
| 158 | |
Jens Axboe | 8458500 | 2006-10-19 20:26:22 +0200 | [diff] [blame] | 159 | free(td->io_ops); |
| 160 | td->io_ops = NULL; |
Jens Axboe | b990b5c | 2006-09-14 09:48:22 +0200 | [diff] [blame] | 161 | } |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 162 | |
| 163 | int td_io_prep(struct thread_data *td, struct io_u *io_u) |
| 164 | { |
| 165 | if (td->io_ops->prep && td->io_ops->prep(td, io_u)) |
| 166 | return 1; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 171 | int td_io_getevents(struct thread_data *td, int min, int max, |
| 172 | struct timespec *t) |
| 173 | { |
| 174 | return td->io_ops->getevents(td, min, max, t); |
| 175 | } |
| 176 | |
| 177 | int td_io_queue(struct thread_data *td, struct io_u *io_u) |
| 178 | { |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 179 | fio_gettime(&io_u->issue_time, NULL); |
Jens Axboe | 10ba535 | 2006-10-20 11:39:27 +0200 | [diff] [blame] | 180 | |
| 181 | return td->io_ops->queue(td, io_u); |
| 182 | } |
Jens Axboe | 8c16d84 | 2006-10-20 11:48:33 +0200 | [diff] [blame] | 183 | |
| 184 | int td_io_init(struct thread_data *td) |
| 185 | { |
| 186 | if (td->io_ops->init) |
| 187 | return td->io_ops->init(td); |
| 188 | |
| 189 | return 0; |
| 190 | } |