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;
32 static struct fio_option options[] = {
35 .lname = "rbd engine rbdname",
36 .type = FIO_OPT_STR_STORE,
37 .help = "RBD name for RBD engine",
38 .off1 = offsetof(struct rbd_options, rbd_name),
39 .category = FIO_OPT_C_ENGINE,
40 .group = FIO_OPT_G_RBD,
44 .lname = "rbd engine pool",
45 .type = FIO_OPT_STR_STORE,
46 .help = "Name of the pool hosting the RBD for the RBD engine",
47 .off1 = offsetof(struct rbd_options, pool_name),
48 .category = FIO_OPT_C_ENGINE,
49 .group = FIO_OPT_G_RBD,
53 .lname = "rbd engine clientname",
54 .type = FIO_OPT_STR_STORE,
55 .help = "Name of the ceph client to access the RBD for the RBD engine",
56 .off1 = offsetof(struct rbd_options, client_name),
57 .category = FIO_OPT_C_ENGINE,
58 .group = FIO_OPT_G_RBD,
64 .help = "Busy poll for completions instead of sleeping",
65 .off1 = offsetof(struct rbd_options, client_name),
67 .category = FIO_OPT_C_ENGINE,
68 .group = FIO_OPT_G_RBD,
75 static int _fio_setup_rbd_data(struct thread_data *td,
76 struct rbd_data **rbd_data_ptr)
78 struct rbd_data *rbd_data;
83 rbd_data = malloc(sizeof(struct rbd_data));
87 memset(rbd_data, 0, sizeof(struct rbd_data));
89 rbd_data->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
90 if (!rbd_data->aio_events)
93 memset(rbd_data->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
95 *rbd_data_ptr = rbd_data;
106 static int _fio_rbd_connect(struct thread_data *td)
108 struct rbd_data *rbd_data = td->io_ops->data;
109 struct rbd_options *o = td->eo;
112 r = rados_create(&rbd_data->cluster, o->client_name);
114 log_err("rados_create failed.\n");
118 r = rados_conf_read_file(rbd_data->cluster, NULL);
120 log_err("rados_conf_read_file failed.\n");
124 r = rados_connect(rbd_data->cluster);
126 log_err("rados_connect failed.\n");
127 goto failed_shutdown;
130 r = rados_ioctx_create(rbd_data->cluster, o->pool_name,
133 log_err("rados_ioctx_create failed.\n");
134 goto failed_shutdown;
137 r = rbd_open(rbd_data->io_ctx, o->rbd_name, &rbd_data->image,
140 log_err("rbd_open failed.\n");
146 rados_ioctx_destroy(rbd_data->io_ctx);
147 rbd_data->io_ctx = NULL;
149 rados_shutdown(rbd_data->cluster);
150 rbd_data->cluster = NULL;
155 static void _fio_rbd_disconnect(struct rbd_data *rbd_data)
160 /* shutdown everything */
161 if (rbd_data->image) {
162 rbd_close(rbd_data->image);
163 rbd_data->image = NULL;
166 if (rbd_data->io_ctx) {
167 rados_ioctx_destroy(rbd_data->io_ctx);
168 rbd_data->io_ctx = NULL;
171 if (rbd_data->cluster) {
172 rados_shutdown(rbd_data->cluster);
173 rbd_data->cluster = NULL;
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 ret = rbd_aio_get_return_value(fri->completion);
191 io_u->resid = io_u->xfer_buflen;
196 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
198 struct rbd_data *rbd_data = td->io_ops->data;
200 return rbd_data->aio_events[event];
203 static inline int fri_check_complete(struct rbd_data *rbd_data,
205 unsigned int *events)
207 struct fio_rbd_iou *fri = io_u->engine_data;
209 if (rbd_aio_is_complete(fri->completion)) {
211 rbd_data->aio_events[*events] = io_u;
214 rbd_aio_release(fri->completion);
221 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
222 unsigned int min_evts, int wait)
224 struct rbd_data *rbd_data = td->io_ops->data;
225 unsigned int this_events = 0;
229 io_u_qiter(&td->io_u_all, io_u, i) {
230 struct fio_rbd_iou *fri = io_u->engine_data;
232 if (!(io_u->flags & IO_U_F_FLIGHT))
237 if (fri_check_complete(rbd_data, io_u, events))
240 rbd_aio_wait_for_complete(fri->completion);
242 if (fri_check_complete(rbd_data, io_u, events))
245 if (*events >= min_evts)
252 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
253 unsigned int max, const struct timespec *t)
255 unsigned int this_events, events = 0;
256 struct rbd_options *o = td->eo;
260 this_events = rbd_iter_events(td, &events, min, wait);
276 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
278 struct rbd_data *rbd_data = td->io_ops->data;
279 struct fio_rbd_iou *fri = io_u->engine_data;
282 fio_ro_check(td, io_u);
286 r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
289 log_err("rbd_aio_create_completion failed.\n");
293 if (io_u->ddir == DDIR_WRITE) {
294 r = rbd_aio_write(rbd_data->image, io_u->offset,
295 io_u->xfer_buflen, io_u->xfer_buf,
298 log_err("rbd_aio_write failed.\n");
302 } else if (io_u->ddir == DDIR_READ) {
303 r = rbd_aio_read(rbd_data->image, io_u->offset,
304 io_u->xfer_buflen, io_u->xfer_buf,
308 log_err("rbd_aio_read failed.\n");
311 } else if (io_u->ddir == DDIR_TRIM) {
312 r = rbd_aio_discard(rbd_data->image, io_u->offset,
313 io_u->xfer_buflen, fri->completion);
315 log_err("rbd_aio_discard failed.\n");
318 } else if (io_u->ddir == DDIR_SYNC) {
319 r = rbd_aio_flush(rbd_data->image, fri->completion);
321 log_err("rbd_flush failed.\n");
325 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
332 rbd_aio_release(fri->completion);
335 td_verror(td, io_u->error, "xfer");
336 return FIO_Q_COMPLETED;
339 static int fio_rbd_init(struct thread_data *td)
343 r = _fio_rbd_connect(td);
345 log_err("fio_rbd_connect failed, return code: %d .\n", r);
355 static void fio_rbd_cleanup(struct thread_data *td)
357 struct rbd_data *rbd_data = td->io_ops->data;
360 _fio_rbd_disconnect(rbd_data);
361 free(rbd_data->aio_events);
367 static int fio_rbd_setup(struct thread_data *td)
370 rbd_image_info_t info;
372 struct rbd_data *rbd_data = NULL;
373 int major, minor, extra;
375 /* log version of librbd. No cluster connection required. */
376 rbd_version(&major, &minor, &extra);
377 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
379 /* allocate engine specific structure to deal with librbd. */
380 r = _fio_setup_rbd_data(td, &rbd_data);
382 log_err("fio_setup_rbd_data failed.\n");
385 td->io_ops->data = rbd_data;
387 /* librbd does not allow us to run first in the main thread and later
388 * in a fork child. It needs to be the same process context all the
391 td->o.use_thread = 1;
393 /* connect in the main thread to determine to determine
394 * the size of the given RADOS block device. And disconnect
397 r = _fio_rbd_connect(td);
399 log_err("fio_rbd_connect failed.\n");
403 /* get size of the RADOS block device */
404 r = rbd_stat(rbd_data->image, &info, sizeof(info));
406 log_err("rbd_status failed.\n");
409 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
411 /* taken from "net" engine. Pretend we deal with files,
412 * even if we do not have any ideas about files.
413 * The size of the RBD is set instead of a artificial file.
415 if (!td->files_index) {
416 add_file(td, td->o.filename ? : "rbd", 0, 0);
417 td->o.nr_files = td->o.nr_files ? : 1;
421 f->real_file_size = info.size;
423 /* disconnect, then we were only connected to determine
424 * the size of the RBD.
426 _fio_rbd_disconnect(rbd_data);
430 _fio_rbd_disconnect(rbd_data);
436 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
441 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
443 #if defined(CONFIG_RBD_INVAL)
444 struct rbd_data *rbd_data = td->io_ops->data;
446 return rbd_invalidate_cache(rbd_data->image);
452 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
454 struct fio_rbd_iou *fri = io_u->engine_data;
457 io_u->engine_data = NULL;
462 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
464 struct fio_rbd_iou *fri;
466 fri = calloc(1, sizeof(*fri));
468 io_u->engine_data = fri;
472 static struct ioengine_ops ioengine = {
474 .version = FIO_IOOPS_VERSION,
475 .setup = fio_rbd_setup,
476 .init = fio_rbd_init,
477 .queue = fio_rbd_queue,
478 .getevents = fio_rbd_getevents,
479 .event = fio_rbd_event,
480 .cleanup = fio_rbd_cleanup,
481 .open_file = fio_rbd_open,
482 .invalidate = fio_rbd_invalidate,
484 .io_u_init = fio_rbd_io_u_init,
485 .io_u_free = fio_rbd_io_u_free,
486 .option_struct_size = sizeof(struct rbd_options),
489 static void fio_init fio_rbd_register(void)
491 register_ioengine(&ioengine);
494 static void fio_exit fio_rbd_unregister(void)
496 unregister_ioengine(&ioengine);