| 1 | /* |
| 2 | * read/write() engine that uses syslet to be async |
| 3 | * |
| 4 | */ |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
| 9 | #include <assert.h> |
| 10 | |
| 11 | #include "../fio.h" |
| 12 | #include "../os.h" |
| 13 | |
| 14 | #ifdef FIO_HAVE_SYSLET |
| 15 | |
| 16 | struct syslet_data { |
| 17 | struct io_u **events; |
| 18 | unsigned int nr_events; |
| 19 | |
| 20 | struct syslet_uatom **ring; |
| 21 | unsigned int ring_index; |
| 22 | }; |
| 23 | |
| 24 | /* |
| 25 | * Inspect the ring to see if we have completed events |
| 26 | */ |
| 27 | static void fio_syslet_complete(struct thread_data *td) |
| 28 | { |
| 29 | struct syslet_data *sd = td->io_ops->data; |
| 30 | |
| 31 | do { |
| 32 | struct syslet_uatom *atom; |
| 33 | struct io_u *io_u; |
| 34 | long ret; |
| 35 | |
| 36 | atom = sd->ring[sd->ring_index]; |
| 37 | if (!atom) |
| 38 | break; |
| 39 | |
| 40 | sd->ring[sd->ring_index] = NULL; |
| 41 | if (++sd->ring_index == td->iodepth) |
| 42 | sd->ring_index = 0; |
| 43 | |
| 44 | io_u = atom->private; |
| 45 | ret = *atom->ret_ptr; |
| 46 | if (ret > 0) |
| 47 | io_u->resid = io_u->xfer_buflen - ret; |
| 48 | else if (ret < 0) |
| 49 | io_u->error = ret; |
| 50 | |
| 51 | sd->events[sd->nr_events++] = io_u; |
| 52 | } while (1); |
| 53 | } |
| 54 | |
| 55 | static int fio_syslet_getevents(struct thread_data *td, int min, |
| 56 | int fio_unused max, |
| 57 | struct timespec fio_unused *t) |
| 58 | { |
| 59 | struct syslet_data *sd = td->io_ops->data; |
| 60 | int get_events; |
| 61 | long ret; |
| 62 | |
| 63 | do { |
| 64 | fio_syslet_complete(td); |
| 65 | |
| 66 | /* |
| 67 | * do we have enough immediate completions? |
| 68 | */ |
| 69 | if (sd->nr_events >= (unsigned int) min) |
| 70 | break; |
| 71 | |
| 72 | /* |
| 73 | * OK, we need to wait for some events... |
| 74 | */ |
| 75 | get_events = min - sd->nr_events; |
| 76 | ret = async_wait(get_events); |
| 77 | if (ret < 0) |
| 78 | return errno; |
| 79 | } while (1); |
| 80 | |
| 81 | ret = sd->nr_events; |
| 82 | sd->nr_events = 0; |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | static struct io_u *fio_syslet_event(struct thread_data *td, int event) |
| 87 | { |
| 88 | struct syslet_data *sd = td->io_ops->data; |
| 89 | |
| 90 | return sd->events[event]; |
| 91 | } |
| 92 | |
| 93 | static void init_atom(struct syslet_uatom *atom, int nr, void *arg0, |
| 94 | void *arg1, void *arg2, void *ret_ptr, |
| 95 | unsigned long flags, void *priv,struct syslet_uatom *next) |
| 96 | { |
| 97 | atom->flags = flags; |
| 98 | atom->nr = nr; |
| 99 | atom->ret_ptr = ret_ptr; |
| 100 | atom->next = next; |
| 101 | atom->arg_ptr[0] = arg0; |
| 102 | atom->arg_ptr[1] = arg1; |
| 103 | atom->arg_ptr[2] = arg2; |
| 104 | atom->arg_ptr[3] = atom->arg_ptr[4] = atom->arg_ptr[5] = NULL; |
| 105 | atom->private = priv; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Use seek atom for sync |
| 110 | */ |
| 111 | static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f) |
| 112 | { |
| 113 | init_atom(&io_u->seek_atom.atom, __NR_fsync, &f->fd, NULL, NULL, |
| 114 | &io_u->seek_atom.ret, SYSLET_STOP_ON_NEGATIVE, io_u, NULL); |
| 115 | } |
| 116 | |
| 117 | static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f) |
| 118 | { |
| 119 | int nr; |
| 120 | |
| 121 | /* |
| 122 | * prepare seek |
| 123 | */ |
| 124 | io_u->seek_atom.cmd = SEEK_SET; |
| 125 | init_atom(&io_u->seek_atom.atom, __NR_lseek, &f->fd, &io_u->offset, |
| 126 | &io_u->seek_atom.cmd, &io_u->seek_atom.ret, |
| 127 | SYSLET_STOP_ON_NEGATIVE | SYSLET_NO_COMPLETE | |
| 128 | SYSLET_SKIP_TO_NEXT_ON_STOP, |
| 129 | NULL, &io_u->rw_atom.atom); |
| 130 | |
| 131 | /* |
| 132 | * prepare rw |
| 133 | */ |
| 134 | if (io_u->ddir == DDIR_READ) |
| 135 | nr = __NR_read; |
| 136 | else |
| 137 | nr = __NR_write; |
| 138 | |
| 139 | init_atom(&io_u->rw_atom.atom, nr, &f->fd, &io_u->xfer_buf, |
| 140 | &io_u->xfer_buflen, &io_u->rw_atom.ret, |
| 141 | SYSLET_STOP_ON_NEGATIVE | SYSLET_SKIP_TO_NEXT_ON_STOP, |
| 142 | io_u, NULL); |
| 143 | } |
| 144 | |
| 145 | static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u) |
| 146 | { |
| 147 | struct fio_file *f = io_u->file; |
| 148 | |
| 149 | if (io_u->ddir == DDIR_SYNC) |
| 150 | fio_syslet_prep_sync(io_u, f); |
| 151 | else |
| 152 | fio_syslet_prep_rw(io_u, f); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u) |
| 158 | { |
| 159 | struct syslet_data *sd = td->io_ops->data; |
| 160 | struct syslet_uatom *done; |
| 161 | long ret; |
| 162 | |
| 163 | done = async_exec(&io_u->seek_atom.atom); |
| 164 | if (!done) |
| 165 | return 0; |
| 166 | |
| 167 | /* |
| 168 | * completed sync |
| 169 | */ |
| 170 | ret = io_u->rw_atom.ret; |
| 171 | if (ret != (long) io_u->xfer_buflen) { |
| 172 | if (ret > 0) { |
| 173 | io_u->resid = io_u->xfer_buflen - ret; |
| 174 | io_u->error = 0; |
| 175 | return ret; |
| 176 | } else |
| 177 | io_u->error = errno; |
| 178 | } |
| 179 | |
| 180 | if (!io_u->error) |
| 181 | sd->events[sd->nr_events++] = io_u; |
| 182 | else |
| 183 | td_verror(td, io_u->error); |
| 184 | |
| 185 | return io_u->error; |
| 186 | } |
| 187 | |
| 188 | static void async_head_init(struct syslet_data *sd, unsigned int depth) |
| 189 | { |
| 190 | struct async_head_user ahu; |
| 191 | unsigned long ring_size; |
| 192 | |
| 193 | ring_size = sizeof(struct syslet_uatom *) * depth; |
| 194 | sd->ring = malloc(ring_size); |
| 195 | memset(sd->ring, 0, ring_size); |
| 196 | |
| 197 | memset(&ahu, 0, sizeof(ahu)); |
| 198 | ahu.completion_ring = sd->ring; |
| 199 | ahu.ring_size_bytes = ring_size; |
| 200 | ahu.max_nr_threads = -1; |
| 201 | |
| 202 | if (async_register(&ahu, sizeof(ahu)) < 0) |
| 203 | perror("async_register"); |
| 204 | } |
| 205 | |
| 206 | static void async_head_exit(struct syslet_data *sd, unsigned int depth) |
| 207 | { |
| 208 | struct async_head_user ahu; |
| 209 | |
| 210 | memset(&ahu, 0, sizeof(ahu)); |
| 211 | ahu.completion_ring = sd->ring; |
| 212 | ahu.ring_size_bytes = sizeof(struct syslet_uatom *) * depth; |
| 213 | |
| 214 | if (async_unregister(&ahu, sizeof(ahu)) < 0) |
| 215 | perror("async_register"); |
| 216 | } |
| 217 | |
| 218 | static void fio_syslet_cleanup(struct thread_data *td) |
| 219 | { |
| 220 | struct syslet_data *sd = td->io_ops->data; |
| 221 | |
| 222 | if (sd) { |
| 223 | async_head_exit(sd, td->iodepth); |
| 224 | free(sd->events); |
| 225 | free(sd); |
| 226 | td->io_ops->data = NULL; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | static int fio_syslet_init(struct thread_data *td) |
| 231 | { |
| 232 | struct syslet_data *sd; |
| 233 | |
| 234 | sd = malloc(sizeof(*sd)); |
| 235 | memset(sd, 0, sizeof(*sd)); |
| 236 | sd->events = malloc(sizeof(struct io_u *) * td->iodepth); |
| 237 | memset(sd->events, 0, sizeof(struct io_u *) * td->iodepth); |
| 238 | td->io_ops->data = sd; |
| 239 | async_head_init(sd, td->iodepth); |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static struct ioengine_ops ioengine = { |
| 244 | .name = "syslet-rw", |
| 245 | .version = FIO_IOOPS_VERSION, |
| 246 | .init = fio_syslet_init, |
| 247 | .prep = fio_syslet_prep, |
| 248 | .queue = fio_syslet_queue, |
| 249 | .getevents = fio_syslet_getevents, |
| 250 | .event = fio_syslet_event, |
| 251 | .cleanup = fio_syslet_cleanup, |
| 252 | }; |
| 253 | |
| 254 | #else /* FIO_HAVE_SYSLET */ |
| 255 | |
| 256 | /* |
| 257 | * When we have a proper configure system in place, we simply wont build |
| 258 | * and install this io engine. For now install a crippled version that |
| 259 | * just complains and fails to load. |
| 260 | */ |
| 261 | static int fio_syslet_init(struct thread_data fio_unused *td) |
| 262 | { |
| 263 | fprintf(stderr, "fio: syslet not available\n"); |
| 264 | return 1; |
| 265 | } |
| 266 | |
| 267 | static struct ioengine_ops ioengine = { |
| 268 | .name = "syslet-rw", |
| 269 | .version = FIO_IOOPS_VERSION, |
| 270 | .init = fio_syslet_init, |
| 271 | }; |
| 272 | |
| 273 | #endif /* FIO_HAVE_SYSLET */ |
| 274 | |
| 275 | static void fio_init fio_syslet_register(void) |
| 276 | { |
| 277 | register_ioengine(&ioengine); |
| 278 | } |
| 279 | |
| 280 | static void fio_exit fio_syslet_unregister(void) |
| 281 | { |
| 282 | unregister_ioengine(&ioengine); |
| 283 | } |