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;
27 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,
66 static int _fio_setup_rbd_data(struct thread_data *td,
67 struct rbd_data **rbd_data_ptr)
69 struct rbd_data *rbd_data;
74 rbd_data = malloc(sizeof(struct rbd_data));
78 memset(rbd_data, 0, sizeof(struct rbd_data));
80 rbd_data->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
81 if (!rbd_data->aio_events)
84 memset(rbd_data->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
86 *rbd_data_ptr = rbd_data;
97 static int _fio_rbd_connect(struct thread_data *td)
99 struct rbd_data *rbd_data = td->io_ops->data;
100 struct rbd_options *o = td->eo;
103 r = rados_create(&(rbd_data->cluster), o->client_name);
105 log_err("rados_create failed.\n");
109 r = rados_conf_read_file(rbd_data->cluster, NULL);
111 log_err("rados_conf_read_file failed.\n");
115 r = rados_connect(rbd_data->cluster);
117 log_err("rados_connect failed.\n");
118 goto failed_shutdown;
121 r = rados_ioctx_create(rbd_data->cluster, o->pool_name,
122 &(rbd_data->io_ctx));
124 log_err("rados_ioctx_create failed.\n");
125 goto failed_shutdown;
128 r = rbd_open(rbd_data->io_ctx, o->rbd_name, &(rbd_data->image),
131 log_err("rbd_open failed.\n");
137 rados_ioctx_destroy(rbd_data->io_ctx);
138 rbd_data->io_ctx = NULL;
140 rados_shutdown(rbd_data->cluster);
141 rbd_data->cluster = NULL;
146 static void _fio_rbd_disconnect(struct rbd_data *rbd_data)
151 /* shutdown everything */
152 if (rbd_data->image) {
153 rbd_close(rbd_data->image);
154 rbd_data->image = NULL;
157 if (rbd_data->io_ctx) {
158 rados_ioctx_destroy(rbd_data->io_ctx);
159 rbd_data->io_ctx = NULL;
162 if (rbd_data->cluster) {
163 rados_shutdown(rbd_data->cluster);
164 rbd_data->cluster = NULL;
168 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
170 struct io_u *io_u = data;
171 struct fio_rbd_iou *fri = io_u->engine_data;
174 fri->io_complete = 1;
177 * Looks like return value is 0 for success, or < 0 for
178 * a specific error. So we have to assume that it can't do
179 * partial completions.
181 ret = rbd_aio_get_return_value(fri->completion);
184 io_u->resid = io_u->xfer_buflen;
189 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
191 struct rbd_data *rbd_data = td->io_ops->data;
193 return rbd_data->aio_events[event];
196 static inline int fri_check_complete(struct rbd_data *rbd_data,
198 unsigned int *events)
200 struct fio_rbd_iou *fri = io_u->engine_data;
202 if (fri->io_complete) {
203 fri->io_complete = 0;
205 rbd_data->aio_events[*events] = io_u;
208 rbd_aio_release(fri->completion);
215 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
216 unsigned int min_evts, int wait)
218 struct rbd_data *rbd_data = td->io_ops->data;
219 unsigned int this_events = 0;
223 io_u_qiter(&td->io_u_all, io_u, i) {
224 struct fio_rbd_iou *fri = io_u->engine_data;
226 if (!(io_u->flags & IO_U_F_FLIGHT))
231 if (fri_check_complete(rbd_data, io_u, events))
234 rbd_aio_wait_for_complete(fri->completion);
236 if (fri_check_complete(rbd_data, io_u, events))
239 if (*events >= min_evts)
246 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
247 unsigned int max, const struct timespec *t)
249 unsigned int this_events, events = 0;
253 this_events = rbd_iter_events(td, &events, min, wait);
266 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
268 struct rbd_data *rbd_data = td->io_ops->data;
269 struct fio_rbd_iou *fri = io_u->engine_data;
272 fio_ro_check(td, io_u);
274 fri->io_complete = 0;
277 if (io_u->ddir == DDIR_WRITE) {
278 r = rbd_aio_create_completion(io_u, _fio_rbd_finish_aiocb,
282 ("rbd_aio_create_completion for DDIR_WRITE failed.\n");
286 r = rbd_aio_write(rbd_data->image, io_u->offset,
287 io_u->xfer_buflen, io_u->xfer_buf,
290 log_err("rbd_aio_write failed.\n");
291 rbd_aio_release(fri->completion);
295 } else if (io_u->ddir == DDIR_READ) {
296 r = rbd_aio_create_completion(io_u, _fio_rbd_finish_aiocb,
300 ("rbd_aio_create_completion for DDIR_READ failed.\n");
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");
310 rbd_aio_release(fri->completion);
314 } else if (io_u->ddir == DDIR_SYNC) {
315 r = rbd_aio_create_completion(io_u, _fio_rbd_finish_aiocb,
319 ("rbd_aio_create_completion for DDIR_SYNC failed.\n");
323 r = rbd_aio_flush(rbd_data->image, fri->completion);
325 log_err("rbd_flush failed.\n");
326 rbd_aio_release(fri->completion);
331 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
333 return FIO_Q_COMPLETED;
340 td_verror(td, io_u->error, "xfer");
341 return FIO_Q_COMPLETED;
344 static int fio_rbd_init(struct thread_data *td)
348 r = _fio_rbd_connect(td);
350 log_err("fio_rbd_connect failed, return code: %d .\n", r);
360 static void fio_rbd_cleanup(struct thread_data *td)
362 struct rbd_data *rbd_data = td->io_ops->data;
365 _fio_rbd_disconnect(rbd_data);
366 free(rbd_data->aio_events);
372 static int fio_rbd_setup(struct thread_data *td)
375 rbd_image_info_t info;
377 struct rbd_data *rbd_data = NULL;
378 int major, minor, extra;
380 /* log version of librbd. No cluster connection required. */
381 rbd_version(&major, &minor, &extra);
382 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
384 /* allocate engine specific structure to deal with librbd. */
385 r = _fio_setup_rbd_data(td, &rbd_data);
387 log_err("fio_setup_rbd_data failed.\n");
390 td->io_ops->data = rbd_data;
392 /* librbd does not allow us to run first in the main thread and later
393 * in a fork child. It needs to be the same process context all the
396 td->o.use_thread = 1;
398 /* connect in the main thread to determine to determine
399 * the size of the given RADOS block device. And disconnect
402 r = _fio_rbd_connect(td);
404 log_err("fio_rbd_connect failed.\n");
408 /* get size of the RADOS block device */
409 r = rbd_stat(rbd_data->image, &info, sizeof(info));
411 log_err("rbd_status failed.\n");
414 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
416 /* taken from "net" engine. Pretend we deal with files,
417 * even if we do not have any ideas about files.
418 * The size of the RBD is set instead of a artificial file.
420 if (!td->files_index) {
421 add_file(td, td->o.filename ? : "rbd", 0, 0);
422 td->o.nr_files = td->o.nr_files ? : 1;
426 f->real_file_size = info.size;
428 /* disconnect, then we were only connected to determine
429 * the size of the RBD.
431 _fio_rbd_disconnect(rbd_data);
435 _fio_rbd_disconnect(rbd_data);
441 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
446 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
451 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
453 struct fio_rbd_iou *fri = io_u->engine_data;
456 io_u->engine_data = NULL;
461 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
463 struct fio_rbd_iou *fri;
465 fri = calloc(1, sizeof(*fri));
467 io_u->engine_data = fri;
471 static struct ioengine_ops ioengine = {
473 .version = FIO_IOOPS_VERSION,
474 .setup = fio_rbd_setup,
475 .init = fio_rbd_init,
476 .queue = fio_rbd_queue,
477 .getevents = fio_rbd_getevents,
478 .event = fio_rbd_event,
479 .cleanup = fio_rbd_cleanup,
480 .open_file = fio_rbd_open,
481 .invalidate = fio_rbd_invalidate,
483 .io_u_init = fio_rbd_io_u_init,
484 .io_u_free = fio_rbd_io_u_free,
485 .option_struct_size = sizeof(struct rbd_options),
488 static void fio_init fio_rbd_register(void)
490 register_ioengine(&ioengine);
493 static void fio_exit fio_rbd_unregister(void)
495 unregister_ioengine(&ioengine);