4 * IO engine using Ceph's librbd to test RADOS Block Devices.
8 #include <rbd/librbd.h>
14 rbd_completion_t completion;
22 struct io_u **aio_events;
26 struct thread_data *td;
33 static struct fio_option options[] = {
36 .lname = "rbd engine rbdname",
37 .type = FIO_OPT_STR_STORE,
38 .help = "RBD name for RBD engine",
39 .off1 = offsetof(struct rbd_options, rbd_name),
40 .category = FIO_OPT_C_ENGINE,
41 .group = FIO_OPT_G_RBD,
45 .lname = "rbd engine pool",
46 .type = FIO_OPT_STR_STORE,
47 .help = "Name of the pool hosting the RBD for the RBD engine",
48 .off1 = offsetof(struct rbd_options, pool_name),
49 .category = FIO_OPT_C_ENGINE,
50 .group = FIO_OPT_G_RBD,
54 .lname = "rbd engine clientname",
55 .type = FIO_OPT_STR_STORE,
56 .help = "Name of the ceph client to access the RBD for the RBD engine",
57 .off1 = offsetof(struct rbd_options, client_name),
58 .category = FIO_OPT_C_ENGINE,
59 .group = FIO_OPT_G_RBD,
65 .help = "Busy poll for completions instead of sleeping",
66 .off1 = offsetof(struct rbd_options, busy_poll),
68 .category = FIO_OPT_C_ENGINE,
69 .group = FIO_OPT_G_RBD,
76 static int _fio_setup_rbd_data(struct thread_data *td,
77 struct rbd_data **rbd_data_ptr)
79 struct rbd_data *rbd_data;
84 rbd_data = malloc(sizeof(struct rbd_data));
88 memset(rbd_data, 0, sizeof(struct rbd_data));
90 rbd_data->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
91 if (!rbd_data->aio_events)
94 memset(rbd_data->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
96 *rbd_data_ptr = rbd_data;
107 static int _fio_rbd_connect(struct thread_data *td)
109 struct rbd_data *rbd_data = td->io_ops->data;
110 struct rbd_options *o = td->eo;
113 r = rados_create(&rbd_data->cluster, o->client_name);
115 log_err("rados_create failed.\n");
119 r = rados_conf_read_file(rbd_data->cluster, NULL);
121 log_err("rados_conf_read_file failed.\n");
125 r = rados_connect(rbd_data->cluster);
127 log_err("rados_connect failed.\n");
128 goto failed_shutdown;
131 r = rados_ioctx_create(rbd_data->cluster, o->pool_name,
134 log_err("rados_ioctx_create failed.\n");
135 goto failed_shutdown;
138 r = rbd_open(rbd_data->io_ctx, o->rbd_name, &rbd_data->image,
141 log_err("rbd_open failed.\n");
147 rados_ioctx_destroy(rbd_data->io_ctx);
148 rbd_data->io_ctx = NULL;
150 rados_shutdown(rbd_data->cluster);
151 rbd_data->cluster = NULL;
156 static void _fio_rbd_disconnect(struct rbd_data *rbd_data)
161 /* shutdown everything */
162 if (rbd_data->image) {
163 rbd_close(rbd_data->image);
164 rbd_data->image = NULL;
167 if (rbd_data->io_ctx) {
168 rados_ioctx_destroy(rbd_data->io_ctx);
169 rbd_data->io_ctx = NULL;
172 if (rbd_data->cluster) {
173 rados_shutdown(rbd_data->cluster);
174 rbd_data->cluster = NULL;
178 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
180 struct fio_rbd_iou *fri = data;
181 struct io_u *io_u = fri->io_u;
185 * Looks like return value is 0 for success, or < 0 for
186 * a specific error. So we have to assume that it can't do
187 * partial completions.
189 ret = rbd_aio_get_return_value(fri->completion);
192 io_u->resid = io_u->xfer_buflen;
197 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
199 struct rbd_data *rbd_data = td->io_ops->data;
201 return rbd_data->aio_events[event];
204 static inline int fri_check_complete(struct rbd_data *rbd_data,
206 unsigned int *events)
208 struct fio_rbd_iou *fri = io_u->engine_data;
210 if (rbd_aio_is_complete(fri->completion)) {
212 rbd_data->aio_events[*events] = io_u;
215 rbd_aio_release(fri->completion);
222 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
223 unsigned int min_evts, int wait)
225 struct rbd_data *rbd_data = td->io_ops->data;
226 unsigned int this_events = 0;
230 io_u_qiter(&td->io_u_all, io_u, i) {
231 struct fio_rbd_iou *fri = io_u->engine_data;
233 if (!(io_u->flags & IO_U_F_FLIGHT))
238 if (fri_check_complete(rbd_data, io_u, events))
241 rbd_aio_wait_for_complete(fri->completion);
243 if (fri_check_complete(rbd_data, io_u, events))
246 if (*events >= min_evts)
253 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
254 unsigned int max, const struct timespec *t)
256 unsigned int this_events, events = 0;
257 struct rbd_options *o = td->eo;
261 this_events = rbd_iter_events(td, &events, min, wait);
277 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
279 struct rbd_data *rbd_data = td->io_ops->data;
280 struct fio_rbd_iou *fri = io_u->engine_data;
283 fio_ro_check(td, io_u);
287 r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
290 log_err("rbd_aio_create_completion failed.\n");
294 if (io_u->ddir == DDIR_WRITE) {
295 r = rbd_aio_write(rbd_data->image, io_u->offset,
296 io_u->xfer_buflen, io_u->xfer_buf,
299 log_err("rbd_aio_write failed.\n");
303 } else if (io_u->ddir == DDIR_READ) {
304 r = rbd_aio_read(rbd_data->image, io_u->offset,
305 io_u->xfer_buflen, io_u->xfer_buf,
309 log_err("rbd_aio_read failed.\n");
312 } else if (io_u->ddir == DDIR_TRIM) {
313 r = rbd_aio_discard(rbd_data->image, io_u->offset,
314 io_u->xfer_buflen, fri->completion);
316 log_err("rbd_aio_discard failed.\n");
319 } else if (io_u->ddir == DDIR_SYNC) {
320 r = rbd_aio_flush(rbd_data->image, fri->completion);
322 log_err("rbd_flush failed.\n");
326 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
333 rbd_aio_release(fri->completion);
336 td_verror(td, io_u->error, "xfer");
337 return FIO_Q_COMPLETED;
340 static int fio_rbd_init(struct thread_data *td)
344 r = _fio_rbd_connect(td);
346 log_err("fio_rbd_connect failed, return code: %d .\n", r);
356 static void fio_rbd_cleanup(struct thread_data *td)
358 struct rbd_data *rbd_data = td->io_ops->data;
361 _fio_rbd_disconnect(rbd_data);
362 free(rbd_data->aio_events);
368 static int fio_rbd_setup(struct thread_data *td)
371 rbd_image_info_t info;
373 struct rbd_data *rbd_data = NULL;
374 int major, minor, extra;
376 /* log version of librbd. No cluster connection required. */
377 rbd_version(&major, &minor, &extra);
378 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
380 /* allocate engine specific structure to deal with librbd. */
381 r = _fio_setup_rbd_data(td, &rbd_data);
383 log_err("fio_setup_rbd_data failed.\n");
386 td->io_ops->data = rbd_data;
388 /* librbd does not allow us to run first in the main thread and later
389 * in a fork child. It needs to be the same process context all the
392 td->o.use_thread = 1;
394 /* connect in the main thread to determine to determine
395 * the size of the given RADOS block device. And disconnect
398 r = _fio_rbd_connect(td);
400 log_err("fio_rbd_connect failed.\n");
404 /* get size of the RADOS block device */
405 r = rbd_stat(rbd_data->image, &info, sizeof(info));
407 log_err("rbd_status failed.\n");
410 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
412 /* taken from "net" engine. Pretend we deal with files,
413 * even if we do not have any ideas about files.
414 * The size of the RBD is set instead of a artificial file.
416 if (!td->files_index) {
417 add_file(td, td->o.filename ? : "rbd", 0, 0);
418 td->o.nr_files = td->o.nr_files ? : 1;
422 f->real_file_size = info.size;
424 /* disconnect, then we were only connected to determine
425 * the size of the RBD.
427 _fio_rbd_disconnect(rbd_data);
431 _fio_rbd_disconnect(rbd_data);
437 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
442 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
444 #if defined(CONFIG_RBD_INVAL)
445 struct rbd_data *rbd_data = td->io_ops->data;
447 return rbd_invalidate_cache(rbd_data->image);
453 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
455 struct fio_rbd_iou *fri = io_u->engine_data;
458 io_u->engine_data = NULL;
463 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
465 struct fio_rbd_iou *fri;
467 fri = calloc(1, sizeof(*fri));
469 io_u->engine_data = fri;
473 static struct ioengine_ops ioengine = {
475 .version = FIO_IOOPS_VERSION,
476 .setup = fio_rbd_setup,
477 .init = fio_rbd_init,
478 .queue = fio_rbd_queue,
479 .getevents = fio_rbd_getevents,
480 .event = fio_rbd_event,
481 .cleanup = fio_rbd_cleanup,
482 .open_file = fio_rbd_open,
483 .invalidate = fio_rbd_invalidate,
485 .io_u_init = fio_rbd_io_u_init,
486 .io_u_free = fio_rbd_io_u_free,
487 .option_struct_size = sizeof(struct rbd_options),
490 static void fio_init fio_rbd_register(void)
492 register_ioengine(&ioengine);
495 static void fio_exit fio_rbd_unregister(void)
497 unregister_ioengine(&ioengine);