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_creat2
135 * will not assume 'client.'. name is considered
136 * as a full type.id namestr
138 if (!index(o->client_name, '.')) {
139 client_name = calloc(1, strlen("client.") +
140 strlen(o->client_name) + 1);
141 strcat(client_name, "client.");
142 o->client_name = strcat(client_name, o->client_name);
144 r = rados_create2(&rbd->cluster, o->cluster_name,
147 r = rados_create(&rbd->cluster, o->client_name);
150 log_err("rados_create failed.\n");
154 r = rados_conf_read_file(rbd->cluster, NULL);
156 log_err("rados_conf_read_file failed.\n");
160 r = rados_connect(rbd->cluster);
162 log_err("rados_connect failed.\n");
163 goto failed_shutdown;
166 r = rados_ioctx_create(rbd->cluster, o->pool_name, &rbd->io_ctx);
168 log_err("rados_ioctx_create failed.\n");
169 goto failed_shutdown;
172 r = rbd_open(rbd->io_ctx, o->rbd_name, &rbd->image, NULL /*snap */ );
174 log_err("rbd_open failed.\n");
180 rados_ioctx_destroy(rbd->io_ctx);
183 rados_shutdown(rbd->cluster);
189 static void _fio_rbd_disconnect(struct rbd_data *rbd)
194 /* shutdown everything */
196 rbd_close(rbd->image);
201 rados_ioctx_destroy(rbd->io_ctx);
206 rados_shutdown(rbd->cluster);
211 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
213 struct fio_rbd_iou *fri = data;
214 struct io_u *io_u = fri->io_u;
218 * Looks like return value is 0 for success, or < 0 for
219 * a specific error. So we have to assume that it can't do
220 * partial completions.
222 ret = rbd_aio_get_return_value(fri->completion);
225 io_u->resid = io_u->xfer_buflen;
229 fri->io_complete = 1;
232 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
234 struct rbd_data *rbd = td->io_ops_data;
236 return rbd->aio_events[event];
239 static inline int fri_check_complete(struct rbd_data *rbd, struct io_u *io_u,
240 unsigned int *events)
242 struct fio_rbd_iou *fri = io_u->engine_data;
244 if (fri->io_complete) {
246 rbd->aio_events[*events] = io_u;
249 rbd_aio_release(fri->completion);
256 static inline int rbd_io_u_seen(struct io_u *io_u)
258 struct fio_rbd_iou *fri = io_u->engine_data;
263 static void rbd_io_u_wait_complete(struct io_u *io_u)
265 struct fio_rbd_iou *fri = io_u->engine_data;
267 rbd_aio_wait_for_complete(fri->completion);
270 static int rbd_io_u_cmp(const void *p1, const void *p2)
272 const struct io_u **a = (const struct io_u **) p1;
273 const struct io_u **b = (const struct io_u **) p2;
276 at = utime_since_now(&(*a)->start_time);
277 bt = utime_since_now(&(*b)->start_time);
287 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
288 unsigned int min_evts, int wait)
290 struct rbd_data *rbd = td->io_ops_data;
291 unsigned int this_events = 0;
296 io_u_qiter(&td->io_u_all, io_u, i) {
297 if (!(io_u->flags & IO_U_F_FLIGHT))
299 if (rbd_io_u_seen(io_u))
302 if (fri_check_complete(rbd, io_u, events))
305 rbd->sort_events[sidx++] = io_u;
312 * Sort events, oldest issue first, then wait on as many as we
313 * need in order of age. If we have enough events, stop waiting,
314 * and just check if any of the older ones are done.
317 qsort(rbd->sort_events, sidx, sizeof(struct io_u *), rbd_io_u_cmp);
319 for (i = 0; i < sidx; i++) {
320 io_u = rbd->sort_events[i];
322 if (fri_check_complete(rbd, io_u, events)) {
328 * Stop waiting when we have enough, but continue checking
329 * all pending IOs if they are complete.
331 if (*events >= min_evts)
334 rbd_io_u_wait_complete(io_u);
336 if (fri_check_complete(rbd, io_u, events))
343 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
344 unsigned int max, const struct timespec *t)
346 unsigned int this_events, events = 0;
347 struct rbd_options *o = td->eo;
351 this_events = rbd_iter_events(td, &events, min, wait);
367 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
369 struct rbd_data *rbd = td->io_ops_data;
370 struct fio_rbd_iou *fri = io_u->engine_data;
373 fio_ro_check(td, io_u);
376 fri->io_complete = 0;
378 r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
381 log_err("rbd_aio_create_completion failed.\n");
385 if (io_u->ddir == DDIR_WRITE) {
386 r = rbd_aio_write(rbd->image, io_u->offset, io_u->xfer_buflen,
387 io_u->xfer_buf, fri->completion);
389 log_err("rbd_aio_write failed.\n");
393 } else if (io_u->ddir == DDIR_READ) {
394 r = rbd_aio_read(rbd->image, io_u->offset, io_u->xfer_buflen,
395 io_u->xfer_buf, fri->completion);
398 log_err("rbd_aio_read failed.\n");
401 } else if (io_u->ddir == DDIR_TRIM) {
402 r = rbd_aio_discard(rbd->image, io_u->offset,
403 io_u->xfer_buflen, fri->completion);
405 log_err("rbd_aio_discard failed.\n");
408 } else if (io_u->ddir == DDIR_SYNC) {
409 r = rbd_aio_flush(rbd->image, fri->completion);
411 log_err("rbd_flush failed.\n");
415 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
422 rbd_aio_release(fri->completion);
425 td_verror(td, io_u->error, "xfer");
426 return FIO_Q_COMPLETED;
429 static int fio_rbd_init(struct thread_data *td)
433 r = _fio_rbd_connect(td);
435 log_err("fio_rbd_connect failed, return code: %d .\n", r);
445 static void fio_rbd_cleanup(struct thread_data *td)
447 struct rbd_data *rbd = td->io_ops_data;
450 _fio_rbd_disconnect(rbd);
451 free(rbd->aio_events);
452 free(rbd->sort_events);
457 static int fio_rbd_setup(struct thread_data *td)
459 rbd_image_info_t info;
461 struct rbd_data *rbd = NULL;
462 int major, minor, extra;
465 /* log version of librbd. No cluster connection required. */
466 rbd_version(&major, &minor, &extra);
467 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
469 /* allocate engine specific structure to deal with librbd. */
470 r = _fio_setup_rbd_data(td, &rbd);
472 log_err("fio_setup_rbd_data failed.\n");
475 td->io_ops_data = rbd;
477 /* librbd does not allow us to run first in the main thread and later
478 * in a fork child. It needs to be the same process context all the
481 td->o.use_thread = 1;
483 /* connect in the main thread to determine to determine
484 * the size of the given RADOS block device. And disconnect
487 r = _fio_rbd_connect(td);
489 log_err("fio_rbd_connect failed.\n");
493 /* get size of the RADOS block device */
494 r = rbd_stat(rbd->image, &info, sizeof(info));
496 log_err("rbd_status failed.\n");
499 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
501 /* taken from "net" engine. Pretend we deal with files,
502 * even if we do not have any ideas about files.
503 * The size of the RBD is set instead of a artificial file.
505 if (!td->files_index) {
506 add_file(td, td->o.filename ? : "rbd", 0, 0);
507 td->o.nr_files = td->o.nr_files ? : 1;
511 f->real_file_size = info.size;
513 /* disconnect, then we were only connected to determine
514 * the size of the RBD.
516 _fio_rbd_disconnect(rbd);
520 _fio_rbd_disconnect(rbd);
526 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
531 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
533 #if defined(CONFIG_RBD_INVAL)
534 struct rbd_data *rbd = td->io_ops_data;
536 return rbd_invalidate_cache(rbd->image);
542 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
544 struct fio_rbd_iou *fri = io_u->engine_data;
547 io_u->engine_data = NULL;
552 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
554 struct fio_rbd_iou *fri;
556 fri = calloc(1, sizeof(*fri));
558 io_u->engine_data = fri;
562 static struct ioengine_ops ioengine = {
564 .version = FIO_IOOPS_VERSION,
565 .setup = fio_rbd_setup,
566 .init = fio_rbd_init,
567 .queue = fio_rbd_queue,
568 .getevents = fio_rbd_getevents,
569 .event = fio_rbd_event,
570 .cleanup = fio_rbd_cleanup,
571 .open_file = fio_rbd_open,
572 .invalidate = fio_rbd_invalidate,
574 .io_u_init = fio_rbd_io_u_init,
575 .io_u_free = fio_rbd_io_u_free,
576 .option_struct_size = sizeof(struct rbd_options),
579 static void fio_init fio_rbd_register(void)
581 register_ioengine(&ioengine);
584 static void fio_exit fio_rbd_unregister(void)
586 unregister_ioengine(&ioengine);