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
18 rbd_completion_t completion;
21 struct blkin_trace_info info;
28 struct io_u **aio_events;
29 struct io_u **sort_events;
41 static struct fio_option options[] = {
43 .name = "clustername",
44 .lname = "ceph cluster name",
45 .type = FIO_OPT_STR_STORE,
46 .help = "Cluster name for ceph",
47 .off1 = offsetof(struct rbd_options, cluster_name),
48 .category = FIO_OPT_C_ENGINE,
49 .group = FIO_OPT_G_RBD,
53 .lname = "rbd engine rbdname",
54 .type = FIO_OPT_STR_STORE,
55 .help = "RBD name for RBD engine",
56 .off1 = offsetof(struct rbd_options, rbd_name),
57 .category = FIO_OPT_C_ENGINE,
58 .group = FIO_OPT_G_RBD,
62 .lname = "rbd engine pool",
63 .type = FIO_OPT_STR_STORE,
64 .help = "Name of the pool hosting the RBD for the RBD engine",
65 .off1 = offsetof(struct rbd_options, pool_name),
66 .category = FIO_OPT_C_ENGINE,
67 .group = FIO_OPT_G_RBD,
71 .lname = "rbd engine clientname",
72 .type = FIO_OPT_STR_STORE,
73 .help = "Name of the ceph client to access the RBD for the RBD engine",
74 .off1 = offsetof(struct rbd_options, client_name),
75 .category = FIO_OPT_C_ENGINE,
76 .group = FIO_OPT_G_RBD,
82 .help = "Busy poll for completions instead of sleeping",
83 .off1 = offsetof(struct rbd_options, busy_poll),
85 .category = FIO_OPT_C_ENGINE,
86 .group = FIO_OPT_G_RBD,
93 static int _fio_setup_rbd_data(struct thread_data *td,
94 struct rbd_data **rbd_data_ptr)
101 rbd = calloc(1, sizeof(struct rbd_data));
105 rbd->aio_events = calloc(td->o.iodepth, sizeof(struct io_u *));
106 if (!rbd->aio_events)
109 rbd->sort_events = calloc(td->o.iodepth, sizeof(struct io_u *));
110 if (!rbd->sort_events)
123 static int _fio_rbd_connect(struct thread_data *td)
125 struct rbd_data *rbd = td->io_ops->data;
126 struct rbd_options *o = td->eo;
129 if (o->cluster_name) {
130 char *client_name = NULL;
133 * If we specify cluser name, the rados_creat2
134 * will not assume 'client.'. name is considered
135 * as a full type.id namestr
137 if (!index(o->client_name, '.')) {
138 client_name = calloc(1, strlen("client.") +
139 strlen(o->client_name) + 1);
140 strcat(client_name, "client.");
141 o->client_name = strcat(client_name, o->client_name);
143 r = rados_create2(&rbd->cluster, o->cluster_name,
146 r = rados_create(&rbd->cluster, o->client_name);
149 log_err("rados_create failed.\n");
153 r = rados_conf_read_file(rbd->cluster, NULL);
155 log_err("rados_conf_read_file failed.\n");
159 r = rados_connect(rbd->cluster);
161 log_err("rados_connect failed.\n");
162 goto failed_shutdown;
165 r = rados_ioctx_create(rbd->cluster, o->pool_name, &rbd->io_ctx);
167 log_err("rados_ioctx_create failed.\n");
168 goto failed_shutdown;
171 r = rbd_open(rbd->io_ctx, o->rbd_name, &rbd->image, NULL /*snap */ );
173 log_err("rbd_open failed.\n");
179 rados_ioctx_destroy(rbd->io_ctx);
182 rados_shutdown(rbd->cluster);
188 static void _fio_rbd_disconnect(struct rbd_data *rbd)
193 /* shutdown everything */
195 rbd_close(rbd->image);
200 rados_ioctx_destroy(rbd->io_ctx);
205 rados_shutdown(rbd->cluster);
210 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
212 struct fio_rbd_iou *fri = data;
213 struct io_u *io_u = fri->io_u;
217 * Looks like return value is 0 for success, or < 0 for
218 * a specific error. So we have to assume that it can't do
219 * partial completions.
221 ret = rbd_aio_get_return_value(fri->completion);
224 io_u->resid = io_u->xfer_buflen;
228 fri->io_complete = 1;
231 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
233 struct rbd_data *rbd = td->io_ops->data;
235 return rbd->aio_events[event];
238 static inline int fri_check_complete(struct rbd_data *rbd, struct io_u *io_u,
239 unsigned int *events)
241 struct fio_rbd_iou *fri = io_u->engine_data;
243 if (fri->io_complete) {
245 rbd->aio_events[*events] = io_u;
248 rbd_aio_release(fri->completion);
255 static inline int rbd_io_u_seen(struct io_u *io_u)
257 struct fio_rbd_iou *fri = io_u->engine_data;
262 static void rbd_io_u_wait_complete(struct io_u *io_u)
264 struct fio_rbd_iou *fri = io_u->engine_data;
266 rbd_aio_wait_for_complete(fri->completion);
269 static int rbd_io_u_cmp(const void *p1, const void *p2)
271 const struct io_u **a = (const struct io_u **) p1;
272 const struct io_u **b = (const struct io_u **) p2;
275 at = utime_since_now(&(*a)->start_time);
276 bt = utime_since_now(&(*b)->start_time);
286 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
287 unsigned int min_evts, int wait)
289 struct rbd_data *rbd = td->io_ops->data;
290 unsigned int this_events = 0;
295 io_u_qiter(&td->io_u_all, io_u, i) {
296 if (!(io_u->flags & IO_U_F_FLIGHT))
298 if (rbd_io_u_seen(io_u))
301 if (fri_check_complete(rbd, io_u, events))
304 rbd->sort_events[sidx++] = io_u;
311 * Sort events, oldest issue first, then wait on as many as we
312 * need in order of age. If we have enough events, stop waiting,
313 * and just check if any of the older ones are done.
316 qsort(rbd->sort_events, sidx, sizeof(struct io_u *), rbd_io_u_cmp);
318 for (i = 0; i < sidx; i++) {
319 io_u = rbd->sort_events[i];
321 if (fri_check_complete(rbd, io_u, events)) {
327 * Stop waiting when we have enough, but continue checking
328 * all pending IOs if they are complete.
330 if (*events >= min_evts)
333 rbd_io_u_wait_complete(io_u);
335 if (fri_check_complete(rbd, io_u, events))
342 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
343 unsigned int max, const struct timespec *t)
345 unsigned int this_events, events = 0;
346 struct rbd_options *o = td->eo;
350 this_events = rbd_iter_events(td, &events, min, wait);
366 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
368 struct rbd_data *rbd = td->io_ops->data;
369 struct fio_rbd_iou *fri = io_u->engine_data;
372 fio_ro_check(td, io_u);
375 fri->io_complete = 0;
377 r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
380 log_err("rbd_aio_create_completion failed.\n");
384 if (io_u->ddir == DDIR_WRITE) {
385 #ifdef CONFIG_RBD_BLKIN
386 blkin_init_trace_info(&fri->info);
387 r = rbd_aio_write_traced(rbd->image, io_u->offset, io_u->xfer_buflen,
388 io_u->xfer_buf, fri->completion, &fri->info);
390 r = rbd_aio_write(rbd->image, io_u->offset, io_u->xfer_buflen,
391 io_u->xfer_buf, fri->completion);
394 log_err("rbd_aio_write failed.\n");
398 } else if (io_u->ddir == DDIR_READ) {
399 #ifdef CONFIG_RBD_BLKIN
400 blkin_init_trace_info(&fri->info);
401 r = rbd_aio_read_traced(rbd->image, io_u->offset, io_u->xfer_buflen,
402 io_u->xfer_buf, fri->completion, &fri->info);
404 r = rbd_aio_read(rbd->image, io_u->offset, io_u->xfer_buflen,
405 io_u->xfer_buf, fri->completion);
409 log_err("rbd_aio_read failed.\n");
412 } else if (io_u->ddir == DDIR_TRIM) {
413 r = rbd_aio_discard(rbd->image, io_u->offset,
414 io_u->xfer_buflen, fri->completion);
416 log_err("rbd_aio_discard failed.\n");
419 } else if (io_u->ddir == DDIR_SYNC) {
420 r = rbd_aio_flush(rbd->image, fri->completion);
422 log_err("rbd_flush failed.\n");
426 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
433 rbd_aio_release(fri->completion);
436 td_verror(td, io_u->error, "xfer");
437 return FIO_Q_COMPLETED;
440 static int fio_rbd_init(struct thread_data *td)
444 r = _fio_rbd_connect(td);
446 log_err("fio_rbd_connect failed, return code: %d .\n", r);
456 static void fio_rbd_cleanup(struct thread_data *td)
458 struct rbd_data *rbd = td->io_ops->data;
461 _fio_rbd_disconnect(rbd);
462 free(rbd->aio_events);
463 free(rbd->sort_events);
468 static int fio_rbd_setup(struct thread_data *td)
470 rbd_image_info_t info;
472 struct rbd_data *rbd = NULL;
473 int major, minor, extra;
476 /* log version of librbd. No cluster connection required. */
477 rbd_version(&major, &minor, &extra);
478 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
480 /* allocate engine specific structure to deal with librbd. */
481 r = _fio_setup_rbd_data(td, &rbd);
483 log_err("fio_setup_rbd_data failed.\n");
486 td->io_ops->data = rbd;
488 /* librbd does not allow us to run first in the main thread and later
489 * in a fork child. It needs to be the same process context all the
492 td->o.use_thread = 1;
494 /* connect in the main thread to determine to determine
495 * the size of the given RADOS block device. And disconnect
498 r = _fio_rbd_connect(td);
500 log_err("fio_rbd_connect failed.\n");
504 /* get size of the RADOS block device */
505 r = rbd_stat(rbd->image, &info, sizeof(info));
507 log_err("rbd_status failed.\n");
510 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
512 /* taken from "net" engine. Pretend we deal with files,
513 * even if we do not have any ideas about files.
514 * The size of the RBD is set instead of a artificial file.
516 if (!td->files_index) {
517 add_file(td, td->o.filename ? : "rbd", 0, 0);
518 td->o.nr_files = td->o.nr_files ? : 1;
522 f->real_file_size = info.size;
524 /* disconnect, then we were only connected to determine
525 * the size of the RBD.
527 _fio_rbd_disconnect(rbd);
531 _fio_rbd_disconnect(rbd);
537 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
542 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
544 #if defined(CONFIG_RBD_INVAL)
545 struct rbd_data *rbd = td->io_ops->data;
547 return rbd_invalidate_cache(rbd->image);
553 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
555 struct fio_rbd_iou *fri = io_u->engine_data;
558 io_u->engine_data = NULL;
563 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
565 struct fio_rbd_iou *fri;
567 fri = calloc(1, sizeof(*fri));
569 io_u->engine_data = fri;
573 static struct ioengine_ops ioengine = {
575 .version = FIO_IOOPS_VERSION,
576 .setup = fio_rbd_setup,
577 .init = fio_rbd_init,
578 .queue = fio_rbd_queue,
579 .getevents = fio_rbd_getevents,
580 .event = fio_rbd_event,
581 .cleanup = fio_rbd_cleanup,
582 .open_file = fio_rbd_open,
583 .invalidate = fio_rbd_invalidate,
585 .io_u_init = fio_rbd_io_u_init,
586 .io_u_free = fio_rbd_io_u_free,
587 .option_struct_size = sizeof(struct rbd_options),
590 static void fio_init fio_rbd_register(void)
592 register_ioengine(&ioengine);
595 static void fio_exit fio_rbd_unregister(void)
597 unregister_ioengine(&ioengine);