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)
119 static int _fio_rbd_connect(struct thread_data *td)
121 struct rbd_data *rbd = td->io_ops->data;
122 struct rbd_options *o = td->eo;
125 if (o->cluster_name) {
126 char *client_name = NULL;
129 * If we specify cluser name, the rados_creat2
130 * will not assume 'client.'. name is considered
131 * as a full type.id namestr
133 if (!index(o->client_name, '.')) {
134 client_name = calloc(1, strlen("client.") +
135 strlen(o->client_name) + 1);
136 strcat(client_name, "client.");
137 o->client_name = strcat(client_name, o->client_name);
139 r = rados_create2(&rbd->cluster, o->cluster_name,
142 r = rados_create(&rbd->cluster, o->client_name);
145 log_err("rados_create failed.\n");
149 r = rados_conf_read_file(rbd->cluster, NULL);
151 log_err("rados_conf_read_file failed.\n");
155 r = rados_connect(rbd->cluster);
157 log_err("rados_connect failed.\n");
158 goto failed_shutdown;
161 r = rados_ioctx_create(rbd->cluster, o->pool_name, &rbd->io_ctx);
163 log_err("rados_ioctx_create failed.\n");
164 goto failed_shutdown;
167 r = rbd_open(rbd->io_ctx, o->rbd_name, &rbd->image, NULL /*snap */ );
169 log_err("rbd_open failed.\n");
175 rados_ioctx_destroy(rbd->io_ctx);
178 rados_shutdown(rbd->cluster);
184 static void _fio_rbd_disconnect(struct rbd_data *rbd)
189 /* shutdown everything */
191 rbd_close(rbd->image);
196 rados_ioctx_destroy(rbd->io_ctx);
201 rados_shutdown(rbd->cluster);
206 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
208 struct fio_rbd_iou *fri = data;
209 struct io_u *io_u = fri->io_u;
213 * Looks like return value is 0 for success, or < 0 for
214 * a specific error. So we have to assume that it can't do
215 * partial completions.
217 ret = rbd_aio_get_return_value(fri->completion);
220 io_u->resid = io_u->xfer_buflen;
224 fri->io_complete = 1;
227 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
229 struct rbd_data *rbd = td->io_ops->data;
231 return rbd->aio_events[event];
234 static inline int fri_check_complete(struct rbd_data *rbd, struct io_u *io_u,
235 unsigned int *events)
237 struct fio_rbd_iou *fri = io_u->engine_data;
239 if (fri->io_complete) {
241 rbd->aio_events[*events] = io_u;
244 rbd_aio_release(fri->completion);
251 static inline int rbd_io_u_seen(struct io_u *io_u)
253 struct fio_rbd_iou *fri = io_u->engine_data;
258 static void rbd_io_u_wait_complete(struct io_u *io_u)
260 struct fio_rbd_iou *fri = io_u->engine_data;
262 rbd_aio_wait_for_complete(fri->completion);
265 static int rbd_io_u_cmp(const void *p1, const void *p2)
267 const struct io_u **a = (const struct io_u **) p1;
268 const struct io_u **b = (const struct io_u **) p2;
271 at = utime_since_now(&(*a)->start_time);
272 bt = utime_since_now(&(*b)->start_time);
282 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
283 unsigned int min_evts, int wait)
285 struct rbd_data *rbd = td->io_ops->data;
286 unsigned int this_events = 0;
291 io_u_qiter(&td->io_u_all, io_u, i) {
292 if (!(io_u->flags & IO_U_F_FLIGHT))
294 if (rbd_io_u_seen(io_u))
297 if (fri_check_complete(rbd, io_u, events))
300 rbd->sort_events[sidx++] = io_u;
307 * Sort events, oldest issue first, then wait on as many as we
308 * need in order of age. If we have enough events, stop waiting,
309 * and just check if any of the older ones are done.
312 qsort(rbd->sort_events, sidx, sizeof(struct io_u *), rbd_io_u_cmp);
314 for (i = 0; i < sidx; i++) {
315 io_u = rbd->sort_events[i];
317 if (fri_check_complete(rbd, io_u, events)) {
323 * Stop waiting when we have enough, but continue checking
324 * all pending IOs if they are complete.
326 if (*events >= min_evts)
329 rbd_io_u_wait_complete(io_u);
331 if (fri_check_complete(rbd, io_u, events))
338 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
339 unsigned int max, const struct timespec *t)
341 unsigned int this_events, events = 0;
342 struct rbd_options *o = td->eo;
346 this_events = rbd_iter_events(td, &events, min, wait);
362 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
364 struct rbd_data *rbd = td->io_ops->data;
365 struct fio_rbd_iou *fri = io_u->engine_data;
368 fio_ro_check(td, io_u);
371 fri->io_complete = 0;
373 r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
376 log_err("rbd_aio_create_completion failed.\n");
380 if (io_u->ddir == DDIR_WRITE) {
381 r = rbd_aio_write(rbd->image, io_u->offset, io_u->xfer_buflen,
382 io_u->xfer_buf, fri->completion);
384 log_err("rbd_aio_write failed.\n");
388 } else if (io_u->ddir == DDIR_READ) {
389 r = rbd_aio_read(rbd->image, io_u->offset, io_u->xfer_buflen,
390 io_u->xfer_buf, fri->completion);
393 log_err("rbd_aio_read failed.\n");
396 } else if (io_u->ddir == DDIR_TRIM) {
397 r = rbd_aio_discard(rbd->image, io_u->offset,
398 io_u->xfer_buflen, fri->completion);
400 log_err("rbd_aio_discard failed.\n");
403 } else if (io_u->ddir == DDIR_SYNC) {
404 r = rbd_aio_flush(rbd->image, fri->completion);
406 log_err("rbd_flush failed.\n");
410 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
417 rbd_aio_release(fri->completion);
420 td_verror(td, io_u->error, "xfer");
421 return FIO_Q_COMPLETED;
424 static int fio_rbd_init(struct thread_data *td)
428 r = _fio_rbd_connect(td);
430 log_err("fio_rbd_connect failed, return code: %d .\n", r);
440 static void fio_rbd_cleanup(struct thread_data *td)
442 struct rbd_data *rbd = td->io_ops->data;
445 _fio_rbd_disconnect(rbd);
446 free(rbd->aio_events);
447 free(rbd->sort_events);
452 static int fio_rbd_setup(struct thread_data *td)
454 rbd_image_info_t info;
456 struct rbd_data *rbd = NULL;
457 int major, minor, extra;
460 /* log version of librbd. No cluster connection required. */
461 rbd_version(&major, &minor, &extra);
462 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
464 /* allocate engine specific structure to deal with librbd. */
465 r = _fio_setup_rbd_data(td, &rbd);
467 log_err("fio_setup_rbd_data failed.\n");
470 td->io_ops->data = rbd;
472 /* librbd does not allow us to run first in the main thread and later
473 * in a fork child. It needs to be the same process context all the
476 td->o.use_thread = 1;
478 /* connect in the main thread to determine to determine
479 * the size of the given RADOS block device. And disconnect
482 r = _fio_rbd_connect(td);
484 log_err("fio_rbd_connect failed.\n");
488 /* get size of the RADOS block device */
489 r = rbd_stat(rbd->image, &info, sizeof(info));
491 log_err("rbd_status failed.\n");
494 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
496 /* taken from "net" engine. Pretend we deal with files,
497 * even if we do not have any ideas about files.
498 * The size of the RBD is set instead of a artificial file.
500 if (!td->files_index) {
501 add_file(td, td->o.filename ? : "rbd", 0, 0);
502 td->o.nr_files = td->o.nr_files ? : 1;
506 f->real_file_size = info.size;
508 /* disconnect, then we were only connected to determine
509 * the size of the RBD.
511 _fio_rbd_disconnect(rbd);
515 _fio_rbd_disconnect(rbd);
521 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
526 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
528 #if defined(CONFIG_RBD_INVAL)
529 struct rbd_data *rbd = td->io_ops->data;
531 return rbd_invalidate_cache(rbd->image);
537 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
539 struct fio_rbd_iou *fri = io_u->engine_data;
542 io_u->engine_data = NULL;
547 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
549 struct fio_rbd_iou *fri;
551 fri = calloc(1, sizeof(*fri));
553 io_u->engine_data = fri;
557 static struct ioengine_ops ioengine = {
559 .version = FIO_IOOPS_VERSION,
560 .setup = fio_rbd_setup,
561 .init = fio_rbd_init,
562 .queue = fio_rbd_queue,
563 .getevents = fio_rbd_getevents,
564 .event = fio_rbd_event,
565 .cleanup = fio_rbd_cleanup,
566 .open_file = fio_rbd_open,
567 .invalidate = fio_rbd_invalidate,
569 .io_u_init = fio_rbd_io_u_init,
570 .io_u_free = fio_rbd_io_u_free,
571 .option_struct_size = sizeof(struct rbd_options),
574 static void fio_init fio_rbd_register(void)
576 register_ioengine(&ioengine);
579 static void fio_exit fio_rbd_unregister(void)
581 unregister_ioengine(&ioengine);