4 * IO engine using Ceph's librbd to test RADOS Block Devices.
8 #include <rbd/librbd.h>
11 #include "../optgroup.h"
12 #ifdef CONFIG_RBD_BLKIN
16 #ifdef CONFIG_RBD_POLL
19 #include <sys/eventfd.h>
24 rbd_completion_t completion;
27 #ifdef CONFIG_RBD_BLKIN
28 struct blkin_trace_info info;
36 struct io_u **aio_events;
37 struct io_u **sort_events;
38 int fd; /* add for poll */
51 static struct fio_option options[] = {
53 .name = "clustername",
54 .lname = "ceph cluster name",
55 .type = FIO_OPT_STR_STORE,
56 .help = "Cluster name for ceph",
57 .off1 = offsetof(struct rbd_options, cluster_name),
58 .category = FIO_OPT_C_ENGINE,
59 .group = FIO_OPT_G_RBD,
63 .lname = "rbd engine rbdname",
64 .type = FIO_OPT_STR_STORE,
65 .help = "RBD name for RBD engine",
66 .off1 = offsetof(struct rbd_options, rbd_name),
67 .category = FIO_OPT_C_ENGINE,
68 .group = FIO_OPT_G_RBD,
72 .lname = "rbd engine pool",
73 .type = FIO_OPT_STR_STORE,
74 .help = "Name of the pool hosting the RBD for the RBD engine",
75 .off1 = offsetof(struct rbd_options, pool_name),
76 .category = FIO_OPT_C_ENGINE,
77 .group = FIO_OPT_G_RBD,
81 .lname = "rbd engine clientname",
82 .type = FIO_OPT_STR_STORE,
83 .help = "Name of the ceph client to access the RBD for the RBD engine",
84 .off1 = offsetof(struct rbd_options, client_name),
85 .category = FIO_OPT_C_ENGINE,
86 .group = FIO_OPT_G_RBD,
92 .help = "Busy poll for completions instead of sleeping",
93 .off1 = offsetof(struct rbd_options, busy_poll),
95 .category = FIO_OPT_C_ENGINE,
96 .group = FIO_OPT_G_RBD,
103 static int _fio_setup_rbd_data(struct thread_data *td,
104 struct rbd_data **rbd_data_ptr)
106 struct rbd_data *rbd;
111 rbd = calloc(1, sizeof(struct rbd_data));
115 rbd->connected = false;
117 /* add for poll, init fd: -1 */
120 rbd->aio_events = calloc(td->o.iodepth, sizeof(struct io_u *));
121 if (!rbd->aio_events)
124 rbd->sort_events = calloc(td->o.iodepth, sizeof(struct io_u *));
125 if (!rbd->sort_events)
134 free(rbd->aio_events);
135 if (rbd->sort_events)
136 free(rbd->sort_events);
143 #ifdef CONFIG_RBD_POLL
144 static bool _fio_rbd_setup_poll(struct rbd_data *rbd)
148 /* add for rbd poll */
149 rbd->fd = eventfd(0, EFD_NONBLOCK);
151 log_err("eventfd failed.\n");
155 r = rbd_set_image_notification(rbd->image, rbd->fd, EVENT_TYPE_EVENTFD);
157 log_err("rbd_set_image_notification failed.\n");
166 static bool _fio_rbd_setup_poll(struct rbd_data *rbd)
172 static int _fio_rbd_connect(struct thread_data *td)
174 struct rbd_data *rbd = td->io_ops_data;
175 struct rbd_options *o = td->eo;
178 if (o->cluster_name) {
179 char *client_name = NULL;
182 * If we specify cluser name, the rados_create2
183 * will not assume 'client.'. name is considered
184 * as a full type.id namestr
186 if (o->client_name) {
187 if (!index(o->client_name, '.')) {
188 client_name = calloc(1, strlen("client.") +
189 strlen(o->client_name) + 1);
190 strcat(client_name, "client.");
191 strcat(client_name, o->client_name);
193 client_name = o->client_name;
197 r = rados_create2(&rbd->cluster, o->cluster_name,
200 if (client_name && !index(o->client_name, '.'))
203 r = rados_create(&rbd->cluster, o->client_name);
206 log_err("rados_create failed.\n");
210 r = rados_conf_read_file(rbd->cluster, NULL);
212 log_err("rados_conf_read_file failed.\n");
216 r = rados_connect(rbd->cluster);
218 log_err("rados_connect failed.\n");
219 goto failed_shutdown;
222 r = rados_ioctx_create(rbd->cluster, o->pool_name, &rbd->io_ctx);
224 log_err("rados_ioctx_create failed.\n");
225 goto failed_shutdown;
228 r = rbd_open(rbd->io_ctx, o->rbd_name, &rbd->image, NULL /*snap */ );
230 log_err("rbd_open failed.\n");
234 if (!_fio_rbd_setup_poll(rbd))
240 rbd_close(rbd->image);
243 rados_ioctx_destroy(rbd->io_ctx);
246 rados_shutdown(rbd->cluster);
252 static void _fio_rbd_disconnect(struct rbd_data *rbd)
263 /* shutdown everything */
265 rbd_close(rbd->image);
270 rados_ioctx_destroy(rbd->io_ctx);
275 rados_shutdown(rbd->cluster);
280 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
282 struct fio_rbd_iou *fri = data;
283 struct io_u *io_u = fri->io_u;
287 * Looks like return value is 0 for success, or < 0 for
288 * a specific error. So we have to assume that it can't do
289 * partial completions.
291 ret = rbd_aio_get_return_value(fri->completion);
294 io_u->resid = io_u->xfer_buflen;
298 fri->io_complete = 1;
301 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
303 struct rbd_data *rbd = td->io_ops_data;
305 return rbd->aio_events[event];
308 static inline int fri_check_complete(struct rbd_data *rbd, struct io_u *io_u,
309 unsigned int *events)
311 struct fio_rbd_iou *fri = io_u->engine_data;
313 if (fri->io_complete) {
315 rbd->aio_events[*events] = io_u;
318 rbd_aio_release(fri->completion);
325 static inline int rbd_io_u_seen(struct io_u *io_u)
327 struct fio_rbd_iou *fri = io_u->engine_data;
332 static void rbd_io_u_wait_complete(struct io_u *io_u)
334 struct fio_rbd_iou *fri = io_u->engine_data;
336 rbd_aio_wait_for_complete(fri->completion);
339 static int rbd_io_u_cmp(const void *p1, const void *p2)
341 const struct io_u **a = (const struct io_u **) p1;
342 const struct io_u **b = (const struct io_u **) p2;
345 at = utime_since_now(&(*a)->start_time);
346 bt = utime_since_now(&(*b)->start_time);
356 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
357 unsigned int min_evts, int wait)
359 struct rbd_data *rbd = td->io_ops_data;
360 unsigned int this_events = 0;
364 #ifdef CONFIG_RBD_POLL
367 struct fio_rbd_iou *fri = NULL;
368 rbd_completion_t comps[min_evts];
374 ret = poll(&pfd, 1, -1);
378 assert(pfd.revents & POLLIN);
380 event_num = rbd_poll_io_events(rbd->image, comps, min_evts);
382 for (i = 0; i < event_num; i++) {
383 fri = rbd_aio_get_arg(comps[i]);
386 io_u_qiter(&td->io_u_all, io_u, i) {
388 if (!(io_u->flags & IO_U_F_FLIGHT))
390 if (rbd_io_u_seen(io_u))
393 if (fri_check_complete(rbd, io_u, events))
396 rbd->sort_events[sidx++] = io_u;
403 * Sort events, oldest issue first, then wait on as many as we
404 * need in order of age. If we have enough events, stop waiting,
405 * and just check if any of the older ones are done.
408 qsort(rbd->sort_events, sidx, sizeof(struct io_u *), rbd_io_u_cmp);
410 for (i = 0; i < sidx; i++) {
411 io_u = rbd->sort_events[i];
413 if (fri_check_complete(rbd, io_u, events)) {
419 * Stop waiting when we have enough, but continue checking
420 * all pending IOs if they are complete.
422 if (*events >= min_evts)
425 rbd_io_u_wait_complete(io_u);
427 if (fri_check_complete(rbd, io_u, events))
434 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
435 unsigned int max, const struct timespec *t)
437 unsigned int this_events, events = 0;
438 struct rbd_options *o = td->eo;
442 this_events = rbd_iter_events(td, &events, min, wait);
458 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
460 struct rbd_data *rbd = td->io_ops_data;
461 struct fio_rbd_iou *fri = io_u->engine_data;
464 fio_ro_check(td, io_u);
467 fri->io_complete = 0;
469 r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
472 log_err("rbd_aio_create_completion failed.\n");
476 if (io_u->ddir == DDIR_WRITE) {
477 #ifdef CONFIG_RBD_BLKIN
478 blkin_init_trace_info(&fri->info);
479 r = rbd_aio_write_traced(rbd->image, io_u->offset, io_u->xfer_buflen,
480 io_u->xfer_buf, fri->completion, &fri->info);
482 r = rbd_aio_write(rbd->image, io_u->offset, io_u->xfer_buflen,
483 io_u->xfer_buf, fri->completion);
486 log_err("rbd_aio_write failed.\n");
490 } else if (io_u->ddir == DDIR_READ) {
491 #ifdef CONFIG_RBD_BLKIN
492 blkin_init_trace_info(&fri->info);
493 r = rbd_aio_read_traced(rbd->image, io_u->offset, io_u->xfer_buflen,
494 io_u->xfer_buf, fri->completion, &fri->info);
496 r = rbd_aio_read(rbd->image, io_u->offset, io_u->xfer_buflen,
497 io_u->xfer_buf, fri->completion);
501 log_err("rbd_aio_read failed.\n");
504 } else if (io_u->ddir == DDIR_TRIM) {
505 r = rbd_aio_discard(rbd->image, io_u->offset,
506 io_u->xfer_buflen, fri->completion);
508 log_err("rbd_aio_discard failed.\n");
511 } else if (io_u->ddir == DDIR_SYNC) {
512 r = rbd_aio_flush(rbd->image, fri->completion);
514 log_err("rbd_flush failed.\n");
518 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
525 rbd_aio_release(fri->completion);
528 td_verror(td, io_u->error, "xfer");
529 return FIO_Q_COMPLETED;
532 static int fio_rbd_init(struct thread_data *td)
535 struct rbd_data *rbd = td->io_ops_data;
540 r = _fio_rbd_connect(td);
542 log_err("fio_rbd_connect failed, return code: %d .\n", r);
552 static void fio_rbd_cleanup(struct thread_data *td)
554 struct rbd_data *rbd = td->io_ops_data;
557 _fio_rbd_disconnect(rbd);
558 free(rbd->aio_events);
559 free(rbd->sort_events);
564 static int fio_rbd_setup(struct thread_data *td)
566 rbd_image_info_t info;
568 struct rbd_data *rbd = NULL;
571 /* allocate engine specific structure to deal with librbd. */
572 r = _fio_setup_rbd_data(td, &rbd);
574 log_err("fio_setup_rbd_data failed.\n");
577 td->io_ops_data = rbd;
579 /* librbd does not allow us to run first in the main thread and later
580 * in a fork child. It needs to be the same process context all the
583 td->o.use_thread = 1;
585 /* connect in the main thread to determine to determine
586 * the size of the given RADOS block device. And disconnect
589 r = _fio_rbd_connect(td);
591 log_err("fio_rbd_connect failed.\n");
594 rbd->connected = true;
596 /* get size of the RADOS block device */
597 r = rbd_stat(rbd->image, &info, sizeof(info));
599 log_err("rbd_status failed.\n");
601 } else if (info.size == 0) {
602 log_err("image size should be larger than zero.\n");
607 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
609 /* taken from "net" engine. Pretend we deal with files,
610 * even if we do not have any ideas about files.
611 * The size of the RBD is set instead of a artificial file.
613 if (!td->files_index) {
614 add_file(td, td->o.filename ? : "rbd", 0, 0);
615 td->o.nr_files = td->o.nr_files ? : 1;
619 f->real_file_size = info.size;
628 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
633 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
635 #if defined(CONFIG_RBD_INVAL)
636 struct rbd_data *rbd = td->io_ops_data;
638 return rbd_invalidate_cache(rbd->image);
644 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
646 struct fio_rbd_iou *fri = io_u->engine_data;
649 io_u->engine_data = NULL;
654 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
656 struct fio_rbd_iou *fri;
658 fri = calloc(1, sizeof(*fri));
660 io_u->engine_data = fri;
664 static struct ioengine_ops ioengine = {
666 .version = FIO_IOOPS_VERSION,
667 .setup = fio_rbd_setup,
668 .init = fio_rbd_init,
669 .queue = fio_rbd_queue,
670 .getevents = fio_rbd_getevents,
671 .event = fio_rbd_event,
672 .cleanup = fio_rbd_cleanup,
673 .open_file = fio_rbd_open,
674 .invalidate = fio_rbd_invalidate,
676 .io_u_init = fio_rbd_io_u_init,
677 .io_u_free = fio_rbd_io_u_free,
678 .option_struct_size = sizeof(struct rbd_options),
681 static void fio_init fio_rbd_register(void)
683 register_ioengine(&ioengine);
686 static void fio_exit fio_rbd_unregister(void)
688 unregister_ioengine(&ioengine);