4 * IO engine using Ceph's librbd to test RADOS Block Devices.
8 #include <rbd/librbd.h>
14 rbd_completion_t completion;
23 struct io_u **aio_events;
24 struct io_u **sort_events;
35 static struct fio_option options[] = {
38 .lname = "rbd engine rbdname",
39 .type = FIO_OPT_STR_STORE,
40 .help = "RBD name for RBD engine",
41 .off1 = offsetof(struct rbd_options, rbd_name),
42 .category = FIO_OPT_C_ENGINE,
43 .group = FIO_OPT_G_RBD,
47 .lname = "rbd engine pool",
48 .type = FIO_OPT_STR_STORE,
49 .help = "Name of the pool hosting the RBD for the RBD engine",
50 .off1 = offsetof(struct rbd_options, pool_name),
51 .category = FIO_OPT_C_ENGINE,
52 .group = FIO_OPT_G_RBD,
56 .lname = "rbd engine clientname",
57 .type = FIO_OPT_STR_STORE,
58 .help = "Name of the ceph client to access the RBD for the RBD engine",
59 .off1 = offsetof(struct rbd_options, client_name),
60 .category = FIO_OPT_C_ENGINE,
61 .group = FIO_OPT_G_RBD,
67 .help = "Busy poll for completions instead of sleeping",
68 .off1 = offsetof(struct rbd_options, busy_poll),
70 .category = FIO_OPT_C_ENGINE,
71 .group = FIO_OPT_G_RBD,
78 static int _fio_setup_rbd_data(struct thread_data *td,
79 struct rbd_data **rbd_data_ptr)
86 rbd = calloc(1, sizeof(struct rbd_data));
90 rbd->aio_events = calloc(td->o.iodepth, sizeof(struct io_u *));
94 rbd->sort_events = calloc(td->o.iodepth, sizeof(struct io_u *));
95 if (!rbd->sort_events)
108 static int _fio_rbd_connect(struct thread_data *td)
110 struct rbd_data *rbd = td->io_ops->data;
111 struct rbd_options *o = td->eo;
114 r = rados_create(&rbd->cluster, o->client_name);
116 log_err("rados_create failed.\n");
120 r = rados_conf_read_file(rbd->cluster, NULL);
122 log_err("rados_conf_read_file failed.\n");
126 r = rados_connect(rbd->cluster);
128 log_err("rados_connect failed.\n");
129 goto failed_shutdown;
132 r = rados_ioctx_create(rbd->cluster, o->pool_name, &rbd->io_ctx);
134 log_err("rados_ioctx_create failed.\n");
135 goto failed_shutdown;
138 r = rbd_open(rbd->io_ctx, o->rbd_name, &rbd->image, NULL /*snap */ );
140 log_err("rbd_open failed.\n");
146 rados_ioctx_destroy(rbd->io_ctx);
149 rados_shutdown(rbd->cluster);
155 static void _fio_rbd_disconnect(struct rbd_data *rbd)
160 /* shutdown everything */
162 rbd_close(rbd->image);
167 rados_ioctx_destroy(rbd->io_ctx);
172 rados_shutdown(rbd->cluster);
177 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
179 struct fio_rbd_iou *fri = data;
180 struct io_u *io_u = fri->io_u;
184 * Looks like return value is 0 for success, or < 0 for
185 * a specific error. So we have to assume that it can't do
186 * partial completions.
188 fri->io_complete = 1;
190 ret = rbd_aio_get_return_value(fri->completion);
193 io_u->resid = io_u->xfer_buflen;
198 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
200 struct rbd_data *rbd = td->io_ops->data;
202 return rbd->aio_events[event];
205 static inline int fri_check_complete(struct rbd_data *rbd, struct io_u *io_u,
206 unsigned int *events)
208 struct fio_rbd_iou *fri = io_u->engine_data;
210 if (fri->io_complete) {
212 rbd->aio_events[*events] = io_u;
215 rbd_aio_release(fri->completion);
222 static inline int rbd_io_u_seen(struct io_u *io_u)
224 struct fio_rbd_iou *fri = io_u->engine_data;
229 static void rbd_io_u_wait_complete(struct io_u *io_u)
231 struct fio_rbd_iou *fri = io_u->engine_data;
233 rbd_aio_wait_for_complete(fri->completion);
236 static int rbd_io_u_cmp(const void *p1, const void *p2)
238 const struct io_u **a = (const struct io_u **) p1;
239 const struct io_u **b = (const struct io_u **) p2;
242 at = utime_since_now(&(*a)->start_time);
243 bt = utime_since_now(&(*b)->start_time);
253 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
254 unsigned int min_evts, int wait)
256 struct rbd_data *rbd = td->io_ops->data;
257 unsigned int this_events = 0;
262 io_u_qiter(&td->io_u_all, io_u, i) {
263 if (!(io_u->flags & IO_U_F_FLIGHT))
265 if (rbd_io_u_seen(io_u))
268 if (fri_check_complete(rbd, io_u, events))
271 rbd->sort_events[sidx++] = io_u;
278 * Sort events, oldest issue first, then wait on as many as we
279 * need in order of age. If we have enough events, stop waiting,
280 * and just check if any of the older ones are done.
283 qsort(rbd->sort_events, sidx, sizeof(struct io_u *), rbd_io_u_cmp);
285 for (i = 0; i < sidx; i++) {
286 io_u = rbd->sort_events[i];
288 if (fri_check_complete(rbd, io_u, events)) {
294 * Stop waiting when we have enough, but continue checking
295 * all pending IOs if they are complete.
297 if (*events >= min_evts)
300 rbd_io_u_wait_complete(io_u);
302 if (fri_check_complete(rbd, io_u, events))
309 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
310 unsigned int max, const struct timespec *t)
312 unsigned int this_events, events = 0;
313 struct rbd_options *o = td->eo;
317 this_events = rbd_iter_events(td, &events, min, wait);
333 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
335 struct rbd_data *rbd = td->io_ops->data;
336 struct fio_rbd_iou *fri = io_u->engine_data;
339 fio_ro_check(td, io_u);
342 fri->io_complete = 0;
344 r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
347 log_err("rbd_aio_create_completion failed.\n");
351 if (io_u->ddir == DDIR_WRITE) {
352 r = rbd_aio_write(rbd->image, io_u->offset, io_u->xfer_buflen,
353 io_u->xfer_buf, fri->completion);
355 log_err("rbd_aio_write failed.\n");
359 } else if (io_u->ddir == DDIR_READ) {
360 r = rbd_aio_read(rbd->image, io_u->offset, io_u->xfer_buflen,
361 io_u->xfer_buf, fri->completion);
364 log_err("rbd_aio_read failed.\n");
367 } else if (io_u->ddir == DDIR_TRIM) {
368 r = rbd_aio_discard(rbd->image, io_u->offset,
369 io_u->xfer_buflen, fri->completion);
371 log_err("rbd_aio_discard failed.\n");
374 } else if (io_u->ddir == DDIR_SYNC) {
375 r = rbd_aio_flush(rbd->image, fri->completion);
377 log_err("rbd_flush failed.\n");
381 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
388 rbd_aio_release(fri->completion);
391 td_verror(td, io_u->error, "xfer");
392 return FIO_Q_COMPLETED;
395 static int fio_rbd_init(struct thread_data *td)
399 r = _fio_rbd_connect(td);
401 log_err("fio_rbd_connect failed, return code: %d .\n", r);
411 static void fio_rbd_cleanup(struct thread_data *td)
413 struct rbd_data *rbd = td->io_ops->data;
416 _fio_rbd_disconnect(rbd);
417 free(rbd->aio_events);
418 free(rbd->sort_events);
423 static int fio_rbd_setup(struct thread_data *td)
425 rbd_image_info_t info;
427 struct rbd_data *rbd = NULL;
428 int major, minor, extra;
431 /* log version of librbd. No cluster connection required. */
432 rbd_version(&major, &minor, &extra);
433 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
435 /* allocate engine specific structure to deal with librbd. */
436 r = _fio_setup_rbd_data(td, &rbd);
438 log_err("fio_setup_rbd_data failed.\n");
441 td->io_ops->data = rbd;
443 /* librbd does not allow us to run first in the main thread and later
444 * in a fork child. It needs to be the same process context all the
447 td->o.use_thread = 1;
449 /* connect in the main thread to determine to determine
450 * the size of the given RADOS block device. And disconnect
453 r = _fio_rbd_connect(td);
455 log_err("fio_rbd_connect failed.\n");
459 /* get size of the RADOS block device */
460 r = rbd_stat(rbd->image, &info, sizeof(info));
462 log_err("rbd_status failed.\n");
465 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
467 /* taken from "net" engine. Pretend we deal with files,
468 * even if we do not have any ideas about files.
469 * The size of the RBD is set instead of a artificial file.
471 if (!td->files_index) {
472 add_file(td, td->o.filename ? : "rbd", 0, 0);
473 td->o.nr_files = td->o.nr_files ? : 1;
477 f->real_file_size = info.size;
479 /* disconnect, then we were only connected to determine
480 * the size of the RBD.
482 _fio_rbd_disconnect(rbd);
486 _fio_rbd_disconnect(rbd);
492 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
497 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
499 #if defined(CONFIG_RBD_INVAL)
500 struct rbd_data *rbd = td->io_ops->data;
502 return rbd_invalidate_cache(rbd->image);
508 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
510 struct fio_rbd_iou *fri = io_u->engine_data;
513 io_u->engine_data = NULL;
518 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
520 struct fio_rbd_iou *fri;
522 fri = calloc(1, sizeof(*fri));
524 io_u->engine_data = fri;
528 static struct ioengine_ops ioengine = {
530 .version = FIO_IOOPS_VERSION,
531 .setup = fio_rbd_setup,
532 .init = fio_rbd_init,
533 .queue = fio_rbd_queue,
534 .getevents = fio_rbd_getevents,
535 .event = fio_rbd_event,
536 .cleanup = fio_rbd_cleanup,
537 .open_file = fio_rbd_open,
538 .invalidate = fio_rbd_invalidate,
540 .io_u_init = fio_rbd_io_u_init,
541 .io_u_free = fio_rbd_io_u_free,
542 .option_struct_size = sizeof(struct rbd_options),
545 static void fio_init fio_rbd_register(void)
547 register_ioengine(&ioengine);
550 static void fio_exit fio_rbd_unregister(void)
552 unregister_ioengine(&ioengine);