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;
37 static struct fio_option options[] = {
39 .name = "clustername",
40 .lname = "ceph cluster name",
41 .type = FIO_OPT_STR_STORE,
42 .help = "Cluster name for ceph",
43 .off1 = offsetof(struct rbd_options, cluster_name),
44 .category = FIO_OPT_C_ENGINE,
45 .group = FIO_OPT_G_RBD,
49 .lname = "rbd engine rbdname",
50 .type = FIO_OPT_STR_STORE,
51 .help = "RBD name for RBD engine",
52 .off1 = offsetof(struct rbd_options, rbd_name),
53 .category = FIO_OPT_C_ENGINE,
54 .group = FIO_OPT_G_RBD,
58 .lname = "rbd engine pool",
59 .type = FIO_OPT_STR_STORE,
60 .help = "Name of the pool hosting the RBD for the RBD engine",
61 .off1 = offsetof(struct rbd_options, pool_name),
62 .category = FIO_OPT_C_ENGINE,
63 .group = FIO_OPT_G_RBD,
67 .lname = "rbd engine clientname",
68 .type = FIO_OPT_STR_STORE,
69 .help = "Name of the ceph client to access the RBD for the RBD engine",
70 .off1 = offsetof(struct rbd_options, client_name),
71 .category = FIO_OPT_C_ENGINE,
72 .group = FIO_OPT_G_RBD,
78 .help = "Busy poll for completions instead of sleeping",
79 .off1 = offsetof(struct rbd_options, busy_poll),
81 .category = FIO_OPT_C_ENGINE,
82 .group = FIO_OPT_G_RBD,
89 static int _fio_setup_rbd_data(struct thread_data *td,
90 struct rbd_data **rbd_data_ptr)
97 rbd = calloc(1, sizeof(struct rbd_data));
101 rbd->aio_events = calloc(td->o.iodepth, sizeof(struct io_u *));
102 if (!rbd->aio_events)
105 rbd->sort_events = calloc(td->o.iodepth, sizeof(struct io_u *));
106 if (!rbd->sort_events)
115 free(rbd->aio_events);
116 if (rbd->sort_events)
117 free(rbd->sort_events);
124 static int _fio_rbd_connect(struct thread_data *td)
126 struct rbd_data *rbd = td->io_ops_data;
127 struct rbd_options *o = td->eo;
130 if (o->cluster_name) {
131 char *client_name = NULL;
134 * If we specify cluser name, the rados_create2
135 * will not assume 'client.'. name is considered
136 * as a full type.id namestr
138 if (o->client_name) {
139 if (!index(o->client_name, '.')) {
140 client_name = calloc(1, strlen("client.") +
141 strlen(o->client_name) + 1);
142 strcat(client_name, "client.");
143 o->client_name = strcat(client_name,
147 r = rados_create2(&rbd->cluster, o->cluster_name,
150 r = rados_create(&rbd->cluster, o->client_name);
153 log_err("rados_create failed.\n");
157 r = rados_conf_read_file(rbd->cluster, NULL);
159 log_err("rados_conf_read_file failed.\n");
163 r = rados_connect(rbd->cluster);
165 log_err("rados_connect failed.\n");
166 goto failed_shutdown;
169 r = rados_ioctx_create(rbd->cluster, o->pool_name, &rbd->io_ctx);
171 log_err("rados_ioctx_create failed.\n");
172 goto failed_shutdown;
175 r = rbd_open(rbd->io_ctx, o->rbd_name, &rbd->image, NULL /*snap */ );
177 log_err("rbd_open failed.\n");
183 rados_ioctx_destroy(rbd->io_ctx);
186 rados_shutdown(rbd->cluster);
192 static void _fio_rbd_disconnect(struct rbd_data *rbd)
197 /* shutdown everything */
199 rbd_close(rbd->image);
204 rados_ioctx_destroy(rbd->io_ctx);
209 rados_shutdown(rbd->cluster);
214 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
216 struct fio_rbd_iou *fri = data;
217 struct io_u *io_u = fri->io_u;
221 * Looks like return value is 0 for success, or < 0 for
222 * a specific error. So we have to assume that it can't do
223 * partial completions.
225 ret = rbd_aio_get_return_value(fri->completion);
228 io_u->resid = io_u->xfer_buflen;
232 fri->io_complete = 1;
235 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
237 struct rbd_data *rbd = td->io_ops_data;
239 return rbd->aio_events[event];
242 static inline int fri_check_complete(struct rbd_data *rbd, struct io_u *io_u,
243 unsigned int *events)
245 struct fio_rbd_iou *fri = io_u->engine_data;
247 if (fri->io_complete) {
249 rbd->aio_events[*events] = io_u;
252 rbd_aio_release(fri->completion);
259 static inline int rbd_io_u_seen(struct io_u *io_u)
261 struct fio_rbd_iou *fri = io_u->engine_data;
266 static void rbd_io_u_wait_complete(struct io_u *io_u)
268 struct fio_rbd_iou *fri = io_u->engine_data;
270 rbd_aio_wait_for_complete(fri->completion);
273 static int rbd_io_u_cmp(const void *p1, const void *p2)
275 const struct io_u **a = (const struct io_u **) p1;
276 const struct io_u **b = (const struct io_u **) p2;
279 at = utime_since_now(&(*a)->start_time);
280 bt = utime_since_now(&(*b)->start_time);
290 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
291 unsigned int min_evts, int wait)
293 struct rbd_data *rbd = td->io_ops_data;
294 unsigned int this_events = 0;
299 io_u_qiter(&td->io_u_all, io_u, i) {
300 if (!(io_u->flags & IO_U_F_FLIGHT))
302 if (rbd_io_u_seen(io_u))
305 if (fri_check_complete(rbd, io_u, events))
308 rbd->sort_events[sidx++] = io_u;
315 * Sort events, oldest issue first, then wait on as many as we
316 * need in order of age. If we have enough events, stop waiting,
317 * and just check if any of the older ones are done.
320 qsort(rbd->sort_events, sidx, sizeof(struct io_u *), rbd_io_u_cmp);
322 for (i = 0; i < sidx; i++) {
323 io_u = rbd->sort_events[i];
325 if (fri_check_complete(rbd, io_u, events)) {
331 * Stop waiting when we have enough, but continue checking
332 * all pending IOs if they are complete.
334 if (*events >= min_evts)
337 rbd_io_u_wait_complete(io_u);
339 if (fri_check_complete(rbd, io_u, events))
346 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
347 unsigned int max, const struct timespec *t)
349 unsigned int this_events, events = 0;
350 struct rbd_options *o = td->eo;
354 this_events = rbd_iter_events(td, &events, min, wait);
370 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
372 struct rbd_data *rbd = td->io_ops_data;
373 struct fio_rbd_iou *fri = io_u->engine_data;
376 fio_ro_check(td, io_u);
379 fri->io_complete = 0;
381 r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
384 log_err("rbd_aio_create_completion failed.\n");
388 if (io_u->ddir == DDIR_WRITE) {
389 r = rbd_aio_write(rbd->image, io_u->offset, io_u->xfer_buflen,
390 io_u->xfer_buf, fri->completion);
392 log_err("rbd_aio_write failed.\n");
396 } else if (io_u->ddir == DDIR_READ) {
397 r = rbd_aio_read(rbd->image, io_u->offset, io_u->xfer_buflen,
398 io_u->xfer_buf, fri->completion);
401 log_err("rbd_aio_read failed.\n");
404 } else if (io_u->ddir == DDIR_TRIM) {
405 r = rbd_aio_discard(rbd->image, io_u->offset,
406 io_u->xfer_buflen, fri->completion);
408 log_err("rbd_aio_discard failed.\n");
411 } else if (io_u->ddir == DDIR_SYNC) {
412 r = rbd_aio_flush(rbd->image, fri->completion);
414 log_err("rbd_flush failed.\n");
418 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
425 rbd_aio_release(fri->completion);
428 td_verror(td, io_u->error, "xfer");
429 return FIO_Q_COMPLETED;
432 static int fio_rbd_init(struct thread_data *td)
436 r = _fio_rbd_connect(td);
438 log_err("fio_rbd_connect failed, return code: %d .\n", r);
448 static void fio_rbd_cleanup(struct thread_data *td)
450 struct rbd_data *rbd = td->io_ops_data;
453 _fio_rbd_disconnect(rbd);
454 free(rbd->aio_events);
455 free(rbd->sort_events);
460 static int fio_rbd_setup(struct thread_data *td)
462 rbd_image_info_t info;
464 struct rbd_data *rbd = NULL;
465 int major, minor, extra;
468 /* log version of librbd. No cluster connection required. */
469 rbd_version(&major, &minor, &extra);
470 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
472 /* allocate engine specific structure to deal with librbd. */
473 r = _fio_setup_rbd_data(td, &rbd);
475 log_err("fio_setup_rbd_data failed.\n");
478 td->io_ops_data = rbd;
480 /* librbd does not allow us to run first in the main thread and later
481 * in a fork child. It needs to be the same process context all the
484 td->o.use_thread = 1;
486 /* connect in the main thread to determine to determine
487 * the size of the given RADOS block device. And disconnect
490 r = _fio_rbd_connect(td);
492 log_err("fio_rbd_connect failed.\n");
496 /* get size of the RADOS block device */
497 r = rbd_stat(rbd->image, &info, sizeof(info));
499 log_err("rbd_status failed.\n");
502 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
504 /* taken from "net" engine. Pretend we deal with files,
505 * even if we do not have any ideas about files.
506 * The size of the RBD is set instead of a artificial file.
508 if (!td->files_index) {
509 add_file(td, td->o.filename ? : "rbd", 0, 0);
510 td->o.nr_files = td->o.nr_files ? : 1;
514 f->real_file_size = info.size;
516 /* disconnect, then we were only connected to determine
517 * the size of the RBD.
519 _fio_rbd_disconnect(rbd);
523 _fio_rbd_disconnect(rbd);
529 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
534 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
536 #if defined(CONFIG_RBD_INVAL)
537 struct rbd_data *rbd = td->io_ops_data;
539 return rbd_invalidate_cache(rbd->image);
545 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
547 struct fio_rbd_iou *fri = io_u->engine_data;
550 io_u->engine_data = NULL;
555 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
557 struct fio_rbd_iou *fri;
559 fri = calloc(1, sizeof(*fri));
561 io_u->engine_data = fri;
565 static struct ioengine_ops ioengine = {
567 .version = FIO_IOOPS_VERSION,
568 .setup = fio_rbd_setup,
569 .init = fio_rbd_init,
570 .queue = fio_rbd_queue,
571 .getevents = fio_rbd_getevents,
572 .event = fio_rbd_event,
573 .cleanup = fio_rbd_cleanup,
574 .open_file = fio_rbd_open,
575 .invalidate = fio_rbd_invalidate,
577 .io_u_init = fio_rbd_io_u_init,
578 .io_u_free = fio_rbd_io_u_free,
579 .option_struct_size = sizeof(struct rbd_options),
582 static void fio_init fio_rbd_register(void)
584 register_ioengine(&ioengine);
587 static void fio_exit fio_rbd_unregister(void)
589 unregister_ioengine(&ioengine);