4 * IO engine using Ceph's librbd to test RADOS Block Devices.
8 #include <rbd/librbd.h>
11 #include "../optgroup.h"
15 rbd_completion_t completion;
24 struct io_u **aio_events;
25 struct io_u **sort_events;
36 static struct fio_option options[] = {
39 .lname = "rbd engine rbdname",
40 .type = FIO_OPT_STR_STORE,
41 .help = "RBD name for RBD engine",
42 .off1 = offsetof(struct rbd_options, rbd_name),
43 .category = FIO_OPT_C_ENGINE,
44 .group = FIO_OPT_G_RBD,
48 .lname = "rbd engine pool",
49 .type = FIO_OPT_STR_STORE,
50 .help = "Name of the pool hosting the RBD for the RBD engine",
51 .off1 = offsetof(struct rbd_options, pool_name),
52 .category = FIO_OPT_C_ENGINE,
53 .group = FIO_OPT_G_RBD,
57 .lname = "rbd engine clientname",
58 .type = FIO_OPT_STR_STORE,
59 .help = "Name of the ceph client to access the RBD for the RBD engine",
60 .off1 = offsetof(struct rbd_options, client_name),
61 .category = FIO_OPT_C_ENGINE,
62 .group = FIO_OPT_G_RBD,
68 .help = "Busy poll for completions instead of sleeping",
69 .off1 = offsetof(struct rbd_options, busy_poll),
71 .category = FIO_OPT_C_ENGINE,
72 .group = FIO_OPT_G_RBD,
79 static int _fio_setup_rbd_data(struct thread_data *td,
80 struct rbd_data **rbd_data_ptr)
87 rbd = calloc(1, sizeof(struct rbd_data));
91 rbd->aio_events = calloc(td->o.iodepth, sizeof(struct io_u *));
95 rbd->sort_events = calloc(td->o.iodepth, sizeof(struct io_u *));
96 if (!rbd->sort_events)
109 static int _fio_rbd_connect(struct thread_data *td)
111 struct rbd_data *rbd = td->io_ops->data;
112 struct rbd_options *o = td->eo;
115 r = rados_create(&rbd->cluster, o->client_name);
117 log_err("rados_create failed.\n");
121 r = rados_conf_read_file(rbd->cluster, NULL);
123 log_err("rados_conf_read_file failed.\n");
127 r = rados_connect(rbd->cluster);
129 log_err("rados_connect failed.\n");
130 goto failed_shutdown;
133 r = rados_ioctx_create(rbd->cluster, o->pool_name, &rbd->io_ctx);
135 log_err("rados_ioctx_create failed.\n");
136 goto failed_shutdown;
139 r = rbd_open(rbd->io_ctx, o->rbd_name, &rbd->image, NULL /*snap */ );
141 log_err("rbd_open failed.\n");
147 rados_ioctx_destroy(rbd->io_ctx);
150 rados_shutdown(rbd->cluster);
156 static void _fio_rbd_disconnect(struct rbd_data *rbd)
161 /* shutdown everything */
163 rbd_close(rbd->image);
168 rados_ioctx_destroy(rbd->io_ctx);
173 rados_shutdown(rbd->cluster);
178 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
180 struct fio_rbd_iou *fri = data;
181 struct io_u *io_u = fri->io_u;
185 * Looks like return value is 0 for success, or < 0 for
186 * a specific error. So we have to assume that it can't do
187 * partial completions.
189 ret = rbd_aio_get_return_value(fri->completion);
192 io_u->resid = io_u->xfer_buflen;
196 fri->io_complete = 1;
199 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
201 struct rbd_data *rbd = td->io_ops->data;
203 return rbd->aio_events[event];
206 static inline int fri_check_complete(struct rbd_data *rbd, struct io_u *io_u,
207 unsigned int *events)
209 struct fio_rbd_iou *fri = io_u->engine_data;
211 if (fri->io_complete) {
213 rbd->aio_events[*events] = io_u;
216 rbd_aio_release(fri->completion);
223 static inline int rbd_io_u_seen(struct io_u *io_u)
225 struct fio_rbd_iou *fri = io_u->engine_data;
230 static void rbd_io_u_wait_complete(struct io_u *io_u)
232 struct fio_rbd_iou *fri = io_u->engine_data;
234 rbd_aio_wait_for_complete(fri->completion);
237 static int rbd_io_u_cmp(const void *p1, const void *p2)
239 const struct io_u **a = (const struct io_u **) p1;
240 const struct io_u **b = (const struct io_u **) p2;
243 at = utime_since_now(&(*a)->start_time);
244 bt = utime_since_now(&(*b)->start_time);
254 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
255 unsigned int min_evts, int wait)
257 struct rbd_data *rbd = td->io_ops->data;
258 unsigned int this_events = 0;
263 io_u_qiter(&td->io_u_all, io_u, i) {
264 if (!(io_u->flags & IO_U_F_FLIGHT))
266 if (rbd_io_u_seen(io_u))
269 if (fri_check_complete(rbd, io_u, events))
272 rbd->sort_events[sidx++] = io_u;
279 * Sort events, oldest issue first, then wait on as many as we
280 * need in order of age. If we have enough events, stop waiting,
281 * and just check if any of the older ones are done.
284 qsort(rbd->sort_events, sidx, sizeof(struct io_u *), rbd_io_u_cmp);
286 for (i = 0; i < sidx; i++) {
287 io_u = rbd->sort_events[i];
289 if (fri_check_complete(rbd, io_u, events)) {
295 * Stop waiting when we have enough, but continue checking
296 * all pending IOs if they are complete.
298 if (*events >= min_evts)
301 rbd_io_u_wait_complete(io_u);
303 if (fri_check_complete(rbd, io_u, events))
310 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
311 unsigned int max, const struct timespec *t)
313 unsigned int this_events, events = 0;
314 struct rbd_options *o = td->eo;
318 this_events = rbd_iter_events(td, &events, min, wait);
334 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
336 struct rbd_data *rbd = td->io_ops->data;
337 struct fio_rbd_iou *fri = io_u->engine_data;
340 fio_ro_check(td, io_u);
343 fri->io_complete = 0;
345 r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
348 log_err("rbd_aio_create_completion failed.\n");
352 if (io_u->ddir == DDIR_WRITE) {
353 r = rbd_aio_write(rbd->image, io_u->offset, io_u->xfer_buflen,
354 io_u->xfer_buf, fri->completion);
356 log_err("rbd_aio_write failed.\n");
360 } else if (io_u->ddir == DDIR_READ) {
361 r = rbd_aio_read(rbd->image, io_u->offset, io_u->xfer_buflen,
362 io_u->xfer_buf, fri->completion);
365 log_err("rbd_aio_read failed.\n");
368 } else if (io_u->ddir == DDIR_TRIM) {
369 r = rbd_aio_discard(rbd->image, io_u->offset,
370 io_u->xfer_buflen, fri->completion);
372 log_err("rbd_aio_discard failed.\n");
375 } else if (io_u->ddir == DDIR_SYNC) {
376 r = rbd_aio_flush(rbd->image, fri->completion);
378 log_err("rbd_flush failed.\n");
382 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
389 rbd_aio_release(fri->completion);
392 td_verror(td, io_u->error, "xfer");
393 return FIO_Q_COMPLETED;
396 static int fio_rbd_init(struct thread_data *td)
400 r = _fio_rbd_connect(td);
402 log_err("fio_rbd_connect failed, return code: %d .\n", r);
412 static void fio_rbd_cleanup(struct thread_data *td)
414 struct rbd_data *rbd = td->io_ops->data;
417 _fio_rbd_disconnect(rbd);
418 free(rbd->aio_events);
419 free(rbd->sort_events);
424 static int fio_rbd_setup(struct thread_data *td)
426 rbd_image_info_t info;
428 struct rbd_data *rbd = NULL;
429 int major, minor, extra;
432 /* log version of librbd. No cluster connection required. */
433 rbd_version(&major, &minor, &extra);
434 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
436 /* allocate engine specific structure to deal with librbd. */
437 r = _fio_setup_rbd_data(td, &rbd);
439 log_err("fio_setup_rbd_data failed.\n");
442 td->io_ops->data = rbd;
444 /* librbd does not allow us to run first in the main thread and later
445 * in a fork child. It needs to be the same process context all the
448 td->o.use_thread = 1;
450 /* connect in the main thread to determine to determine
451 * the size of the given RADOS block device. And disconnect
454 r = _fio_rbd_connect(td);
456 log_err("fio_rbd_connect failed.\n");
460 /* get size of the RADOS block device */
461 r = rbd_stat(rbd->image, &info, sizeof(info));
463 log_err("rbd_status failed.\n");
466 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
468 /* taken from "net" engine. Pretend we deal with files,
469 * even if we do not have any ideas about files.
470 * The size of the RBD is set instead of a artificial file.
472 if (!td->files_index) {
473 add_file(td, td->o.filename ? : "rbd", 0, 0);
474 td->o.nr_files = td->o.nr_files ? : 1;
478 f->real_file_size = info.size;
480 /* disconnect, then we were only connected to determine
481 * the size of the RBD.
483 _fio_rbd_disconnect(rbd);
487 _fio_rbd_disconnect(rbd);
493 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
498 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
500 #if defined(CONFIG_RBD_INVAL)
501 struct rbd_data *rbd = td->io_ops->data;
503 return rbd_invalidate_cache(rbd->image);
509 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
511 struct fio_rbd_iou *fri = io_u->engine_data;
514 io_u->engine_data = NULL;
519 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
521 struct fio_rbd_iou *fri;
523 fri = calloc(1, sizeof(*fri));
525 io_u->engine_data = fri;
529 static struct ioengine_ops ioengine = {
531 .version = FIO_IOOPS_VERSION,
532 .setup = fio_rbd_setup,
533 .init = fio_rbd_init,
534 .queue = fio_rbd_queue,
535 .getevents = fio_rbd_getevents,
536 .event = fio_rbd_event,
537 .cleanup = fio_rbd_cleanup,
538 .open_file = fio_rbd_open,
539 .invalidate = fio_rbd_invalidate,
541 .io_u_init = fio_rbd_io_u_init,
542 .io_u_free = fio_rbd_io_u_free,
543 .option_struct_size = sizeof(struct rbd_options),
546 static void fio_init fio_rbd_register(void)
548 register_ioengine(&ioengine);
551 static void fio_exit fio_rbd_unregister(void)
553 unregister_ioengine(&ioengine);