| 1 | /* |
| 2 | * syslet engine |
| 3 | * |
| 4 | * IO engine that does regular pread(2)/pwrite(2) to transfer data, but |
| 5 | * with syslets to make the execution async. |
| 6 | * |
| 7 | */ |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <unistd.h> |
| 11 | #include <errno.h> |
| 12 | #include <assert.h> |
| 13 | #include <asm/unistd.h> |
| 14 | |
| 15 | #include "../fio.h" |
| 16 | |
| 17 | #ifdef FIO_HAVE_SYSLET |
| 18 | |
| 19 | #ifdef __NR_pread64 |
| 20 | #define __NR_fio_pread __NR_pread64 |
| 21 | #define __NR_fio_pwrite __NR_pwrite64 |
| 22 | #else |
| 23 | #define __NR_fio_pread __NR_pread |
| 24 | #define __NR_fio_pwrite __NR_pwrite |
| 25 | #endif |
| 26 | |
| 27 | #define ATOM_TO_IOU(p) ((struct io_u *) (unsigned long) (p)) |
| 28 | |
| 29 | struct syslet_data { |
| 30 | struct io_u **events; |
| 31 | unsigned int nr_events; |
| 32 | |
| 33 | struct async_head_user ahu; |
| 34 | struct syslet_uatom **ring; |
| 35 | |
| 36 | struct syslet_uatom *head, *tail; |
| 37 | }; |
| 38 | |
| 39 | static void fio_syslet_complete_atom(struct thread_data *td, |
| 40 | struct syslet_uatom *atom) |
| 41 | { |
| 42 | struct syslet_data *sd = td->io_ops->data; |
| 43 | struct syslet_uatom *last; |
| 44 | struct io_u *io_u; |
| 45 | |
| 46 | /* |
| 47 | * complete from the beginning of the sequence up to (and |
| 48 | * including) this atom |
| 49 | */ |
| 50 | last = atom; |
| 51 | io_u = ATOM_TO_IOU(atom); |
| 52 | atom = io_u->req.head; |
| 53 | |
| 54 | /* |
| 55 | * now complete in right order |
| 56 | */ |
| 57 | do { |
| 58 | long ret; |
| 59 | |
| 60 | io_u = ATOM_TO_IOU(atom); |
| 61 | ret = *(long *) (unsigned long) atom->ret_ptr; |
| 62 | if (ret >= 0) |
| 63 | io_u->resid = io_u->xfer_buflen - ret; |
| 64 | else if (ret < 0) |
| 65 | io_u->error = ret; |
| 66 | |
| 67 | assert(sd->nr_events < td->o.iodepth); |
| 68 | sd->events[sd->nr_events++] = io_u; |
| 69 | |
| 70 | if (atom == last) |
| 71 | break; |
| 72 | |
| 73 | atom = (struct syslet_uatom *) (unsigned long) atom->next; |
| 74 | } while (1); |
| 75 | |
| 76 | assert(!last->next); |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Inspect the ring to see if we have completed events |
| 81 | */ |
| 82 | static void fio_syslet_complete(struct thread_data *td) |
| 83 | { |
| 84 | struct syslet_data *sd = td->io_ops->data; |
| 85 | |
| 86 | do { |
| 87 | struct syslet_uatom *atom; |
| 88 | |
| 89 | atom = sd->ring[sd->ahu.user_ring_idx]; |
| 90 | if (!atom) |
| 91 | break; |
| 92 | |
| 93 | sd->ring[sd->ahu.user_ring_idx] = NULL; |
| 94 | if (++sd->ahu.user_ring_idx == td->o.iodepth) |
| 95 | sd->ahu.user_ring_idx = 0; |
| 96 | |
| 97 | fio_syslet_complete_atom(td, atom); |
| 98 | } while (1); |
| 99 | } |
| 100 | |
| 101 | static int fio_syslet_getevents(struct thread_data *td, int min, |
| 102 | int fio_unused max, |
| 103 | struct timespec fio_unused *t) |
| 104 | { |
| 105 | struct syslet_data *sd = td->io_ops->data; |
| 106 | long ret; |
| 107 | |
| 108 | do { |
| 109 | fio_syslet_complete(td); |
| 110 | |
| 111 | /* |
| 112 | * do we have enough immediate completions? |
| 113 | */ |
| 114 | if (sd->nr_events >= (unsigned int) min) |
| 115 | break; |
| 116 | |
| 117 | /* |
| 118 | * OK, we need to wait for some events... |
| 119 | */ |
| 120 | ret = async_wait(1, sd->ahu.user_ring_idx, &sd->ahu); |
| 121 | if (ret < 0) |
| 122 | return -errno; |
| 123 | } while (1); |
| 124 | |
| 125 | ret = sd->nr_events; |
| 126 | sd->nr_events = 0; |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | static struct io_u *fio_syslet_event(struct thread_data *td, int event) |
| 131 | { |
| 132 | struct syslet_data *sd = td->io_ops->data; |
| 133 | |
| 134 | return sd->events[event]; |
| 135 | } |
| 136 | |
| 137 | static void init_atom(struct syslet_uatom *atom, int nr, void *arg0, |
| 138 | void *arg1, void *arg2, void *arg3, void *ret_ptr, |
| 139 | unsigned long flags, void *priv) |
| 140 | { |
| 141 | atom->flags = flags; |
| 142 | atom->nr = nr; |
| 143 | atom->ret_ptr = (uint64_t) (unsigned long) ret_ptr; |
| 144 | atom->next = 0; |
| 145 | atom->arg_ptr[0] = (uint64_t) (unsigned long) arg0; |
| 146 | atom->arg_ptr[1] = (uint64_t) (unsigned long) arg1; |
| 147 | atom->arg_ptr[2] = (uint64_t) (unsigned long) arg2; |
| 148 | atom->arg_ptr[3] = (uint64_t) (unsigned long) arg3; |
| 149 | atom->arg_ptr[4] = 0; |
| 150 | atom->arg_ptr[5] = 0; |
| 151 | atom->private = (uint64_t) (unsigned long) priv; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Use seek atom for sync |
| 156 | */ |
| 157 | static void fio_syslet_prep_sync(struct io_u *io_u, struct fio_file *f) |
| 158 | { |
| 159 | init_atom(&io_u->req.atom, __NR_fsync, &f->fd, NULL, NULL, NULL, |
| 160 | &io_u->req.ret, 0, io_u); |
| 161 | } |
| 162 | |
| 163 | static void fio_syslet_prep_rw(struct io_u *io_u, struct fio_file *f) |
| 164 | { |
| 165 | int nr; |
| 166 | |
| 167 | /* |
| 168 | * prepare rw |
| 169 | */ |
| 170 | if (io_u->ddir == DDIR_READ) |
| 171 | nr = __NR_fio_pread; |
| 172 | else |
| 173 | nr = __NR_fio_pwrite; |
| 174 | |
| 175 | init_atom(&io_u->req.atom, nr, &f->fd, &io_u->xfer_buf, |
| 176 | &io_u->xfer_buflen, &io_u->offset, &io_u->req.ret, 0, io_u); |
| 177 | } |
| 178 | |
| 179 | static int fio_syslet_prep(struct thread_data fio_unused *td, struct io_u *io_u) |
| 180 | { |
| 181 | struct fio_file *f = io_u->file; |
| 182 | |
| 183 | if (io_u->ddir == DDIR_SYNC) |
| 184 | fio_syslet_prep_sync(io_u, f); |
| 185 | else |
| 186 | fio_syslet_prep_rw(io_u, f); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static void cachemiss_thread_start(void) |
| 192 | { |
| 193 | while (1) |
| 194 | async_thread(NULL, NULL); |
| 195 | } |
| 196 | |
| 197 | #define THREAD_STACK_SIZE (16384) |
| 198 | |
| 199 | static unsigned long thread_stack_alloc() |
| 200 | { |
| 201 | return (unsigned long) malloc(THREAD_STACK_SIZE) + THREAD_STACK_SIZE; |
| 202 | } |
| 203 | |
| 204 | static void fio_syslet_queued(struct thread_data *td, struct syslet_data *sd) |
| 205 | { |
| 206 | struct syslet_uatom *atom; |
| 207 | struct timeval now; |
| 208 | |
| 209 | fio_gettime(&now, NULL); |
| 210 | |
| 211 | atom = sd->head; |
| 212 | while (atom) { |
| 213 | struct io_u *io_u = ATOM_TO_IOU(atom); |
| 214 | |
| 215 | memcpy(&io_u->issue_time, &now, sizeof(now)); |
| 216 | io_u_queued(td, io_u); |
| 217 | atom = (struct syslet_uatom *) (unsigned long) atom->next; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | static int fio_syslet_commit(struct thread_data *td) |
| 222 | { |
| 223 | struct syslet_data *sd = td->io_ops->data; |
| 224 | struct syslet_uatom *done; |
| 225 | |
| 226 | if (!sd->head) |
| 227 | return 0; |
| 228 | |
| 229 | assert(!sd->tail->next); |
| 230 | |
| 231 | if (!sd->ahu.new_thread_stack) |
| 232 | sd->ahu.new_thread_stack = thread_stack_alloc(); |
| 233 | |
| 234 | fio_syslet_queued(td, sd); |
| 235 | |
| 236 | /* |
| 237 | * On sync completion, the atom is returned. So on NULL return |
| 238 | * it's queued asynchronously. |
| 239 | */ |
| 240 | done = async_exec(sd->head, &sd->ahu); |
| 241 | |
| 242 | if (done == (void *) -1) { |
| 243 | log_err("fio: syslets don't appear to work\n"); |
| 244 | return -1; |
| 245 | } |
| 246 | |
| 247 | sd->head = sd->tail = NULL; |
| 248 | |
| 249 | if (done) |
| 250 | fio_syslet_complete_atom(td, done); |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static int fio_syslet_queue(struct thread_data *td, struct io_u *io_u) |
| 256 | { |
| 257 | struct syslet_data *sd = td->io_ops->data; |
| 258 | |
| 259 | fio_ro_check(td, io_u); |
| 260 | |
| 261 | if (sd->tail) { |
| 262 | sd->tail->next = (uint64_t) (unsigned long) &io_u->req.atom; |
| 263 | sd->tail = &io_u->req.atom; |
| 264 | } else |
| 265 | sd->head = sd->tail = (struct syslet_uatom *)&io_u->req.atom; |
| 266 | |
| 267 | io_u->req.head = sd->head; |
| 268 | return FIO_Q_QUEUED; |
| 269 | } |
| 270 | |
| 271 | static int async_head_init(struct syslet_data *sd, unsigned int depth) |
| 272 | { |
| 273 | unsigned long ring_size; |
| 274 | |
| 275 | memset(&sd->ahu, 0, sizeof(struct async_head_user)); |
| 276 | |
| 277 | ring_size = sizeof(struct syslet_uatom *) * depth; |
| 278 | sd->ring = malloc(ring_size); |
| 279 | memset(sd->ring, 0, ring_size); |
| 280 | |
| 281 | sd->ahu.user_ring_idx = 0; |
| 282 | sd->ahu.completion_ring_ptr = (uint64_t) (unsigned long) sd->ring; |
| 283 | sd->ahu.ring_size_bytes = ring_size; |
| 284 | sd->ahu.head_stack = thread_stack_alloc(); |
| 285 | sd->ahu.head_ip = (uint64_t) (unsigned long) cachemiss_thread_start; |
| 286 | sd->ahu.new_thread_ip = (uint64_t) (unsigned long) cachemiss_thread_start; |
| 287 | sd->ahu.new_thread_stack = thread_stack_alloc(); |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static void async_head_exit(struct syslet_data *sd) |
| 293 | { |
| 294 | free(sd->ring); |
| 295 | } |
| 296 | |
| 297 | static int check_syslet_support(struct syslet_data *sd) |
| 298 | { |
| 299 | struct syslet_uatom atom; |
| 300 | void *ret; |
| 301 | |
| 302 | init_atom(&atom, __NR_getpid, NULL, NULL, NULL, NULL, NULL, 0, NULL); |
| 303 | ret = async_exec(&atom, &sd->ahu); |
| 304 | if (ret == (void *) -1) |
| 305 | return 1; |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static void fio_syslet_cleanup(struct thread_data *td) |
| 311 | { |
| 312 | struct syslet_data *sd = td->io_ops->data; |
| 313 | |
| 314 | if (sd) { |
| 315 | async_head_exit(sd); |
| 316 | free(sd->events); |
| 317 | free(sd); |
| 318 | td->io_ops->data = NULL; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | static int fio_syslet_init(struct thread_data *td) |
| 323 | { |
| 324 | struct syslet_data *sd; |
| 325 | |
| 326 | sd = malloc(sizeof(*sd)); |
| 327 | memset(sd, 0, sizeof(*sd)); |
| 328 | sd->events = malloc(sizeof(struct io_u *) * td->o.iodepth); |
| 329 | memset(sd->events, 0, sizeof(struct io_u *) * td->o.iodepth); |
| 330 | |
| 331 | /* |
| 332 | * This will handily fail for kernels where syslet isn't available |
| 333 | */ |
| 334 | if (async_head_init(sd, td->o.iodepth)) { |
| 335 | free(sd->events); |
| 336 | free(sd); |
| 337 | return 1; |
| 338 | } |
| 339 | |
| 340 | if (check_syslet_support(sd)) { |
| 341 | log_err("fio: syslets do not appear to work\n"); |
| 342 | free(sd->events); |
| 343 | free(sd); |
| 344 | return 1; |
| 345 | } |
| 346 | |
| 347 | td->io_ops->data = sd; |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static struct ioengine_ops ioengine = { |
| 352 | .name = "syslet-rw", |
| 353 | .version = FIO_IOOPS_VERSION, |
| 354 | .init = fio_syslet_init, |
| 355 | .prep = fio_syslet_prep, |
| 356 | .queue = fio_syslet_queue, |
| 357 | .commit = fio_syslet_commit, |
| 358 | .getevents = fio_syslet_getevents, |
| 359 | .event = fio_syslet_event, |
| 360 | .cleanup = fio_syslet_cleanup, |
| 361 | .open_file = generic_open_file, |
| 362 | .close_file = generic_close_file, |
| 363 | }; |
| 364 | |
| 365 | #else /* FIO_HAVE_SYSLET */ |
| 366 | |
| 367 | /* |
| 368 | * When we have a proper configure system in place, we simply wont build |
| 369 | * and install this io engine. For now install a crippled version that |
| 370 | * just complains and fails to load. |
| 371 | */ |
| 372 | static int fio_syslet_init(struct thread_data fio_unused *td) |
| 373 | { |
| 374 | fprintf(stderr, "fio: syslet not available\n"); |
| 375 | return 1; |
| 376 | } |
| 377 | |
| 378 | static struct ioengine_ops ioengine = { |
| 379 | .name = "syslet-rw", |
| 380 | .version = FIO_IOOPS_VERSION, |
| 381 | .init = fio_syslet_init, |
| 382 | }; |
| 383 | |
| 384 | #endif /* FIO_HAVE_SYSLET */ |
| 385 | |
| 386 | static void fio_init fio_syslet_register(void) |
| 387 | { |
| 388 | register_ioengine(&ioengine); |
| 389 | } |
| 390 | |
| 391 | static void fio_exit fio_syslet_unregister(void) |
| 392 | { |
| 393 | unregister_ioengine(&ioengine); |
| 394 | } |