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;
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,
65 static int _fio_setup_rbd_data(struct thread_data *td,
66 struct rbd_data **rbd_data_ptr)
68 struct rbd_data *rbd_data;
73 rbd_data = malloc(sizeof(struct rbd_data));
77 memset(rbd_data, 0, sizeof(struct rbd_data));
79 rbd_data->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
80 if (!rbd_data->aio_events)
83 memset(rbd_data->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
85 *rbd_data_ptr = rbd_data;
96 static int _fio_rbd_connect(struct thread_data *td)
98 struct rbd_data *rbd_data = td->io_ops->data;
99 struct rbd_options *o = td->eo;
102 r = rados_create(&rbd_data->cluster, o->client_name);
104 log_err("rados_create failed.\n");
108 r = rados_conf_read_file(rbd_data->cluster, NULL);
110 log_err("rados_conf_read_file failed.\n");
114 r = rados_connect(rbd_data->cluster);
116 log_err("rados_connect failed.\n");
117 goto failed_shutdown;
120 r = rados_ioctx_create(rbd_data->cluster, o->pool_name,
123 log_err("rados_ioctx_create failed.\n");
124 goto failed_shutdown;
127 r = rbd_open(rbd_data->io_ctx, o->rbd_name, &rbd_data->image,
130 log_err("rbd_open failed.\n");
136 rados_ioctx_destroy(rbd_data->io_ctx);
137 rbd_data->io_ctx = NULL;
139 rados_shutdown(rbd_data->cluster);
140 rbd_data->cluster = NULL;
145 static void _fio_rbd_disconnect(struct rbd_data *rbd_data)
150 /* shutdown everything */
151 if (rbd_data->image) {
152 rbd_close(rbd_data->image);
153 rbd_data->image = NULL;
156 if (rbd_data->io_ctx) {
157 rados_ioctx_destroy(rbd_data->io_ctx);
158 rbd_data->io_ctx = NULL;
161 if (rbd_data->cluster) {
162 rados_shutdown(rbd_data->cluster);
163 rbd_data->cluster = NULL;
167 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
169 struct fio_rbd_iou *fri = data;
170 struct io_u *io_u = fri->io_u;
174 * Looks like return value is 0 for success, or < 0 for
175 * a specific error. So we have to assume that it can't do
176 * partial completions.
178 ret = rbd_aio_get_return_value(fri->completion);
181 io_u->resid = io_u->xfer_buflen;
186 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
188 struct rbd_data *rbd_data = td->io_ops->data;
190 return rbd_data->aio_events[event];
193 static inline int fri_check_complete(struct rbd_data *rbd_data,
195 unsigned int *events)
197 struct fio_rbd_iou *fri = io_u->engine_data;
199 if (rbd_aio_is_complete(fri->completion)) {
201 rbd_data->aio_events[*events] = io_u;
204 rbd_aio_release(fri->completion);
211 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
212 unsigned int min_evts, int wait)
214 struct rbd_data *rbd_data = td->io_ops->data;
215 unsigned int this_events = 0;
219 io_u_qiter(&td->io_u_all, io_u, i) {
220 struct fio_rbd_iou *fri = io_u->engine_data;
222 if (!(io_u->flags & IO_U_F_FLIGHT))
227 if (fri_check_complete(rbd_data, io_u, events))
230 rbd_aio_wait_for_complete(fri->completion);
232 if (fri_check_complete(rbd_data, io_u, events))
235 if (*events >= min_evts)
242 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
243 unsigned int max, const struct timespec *t)
245 unsigned int this_events, events = 0;
249 this_events = rbd_iter_events(td, &events, min, wait);
262 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
264 struct rbd_data *rbd_data = td->io_ops->data;
265 struct fio_rbd_iou *fri = io_u->engine_data;
268 fio_ro_check(td, io_u);
272 r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
275 log_err("rbd_aio_create_completion failed.\n");
279 if (io_u->ddir == DDIR_WRITE) {
280 r = rbd_aio_write(rbd_data->image, io_u->offset,
281 io_u->xfer_buflen, io_u->xfer_buf,
284 log_err("rbd_aio_write failed.\n");
288 } else if (io_u->ddir == DDIR_READ) {
289 r = rbd_aio_read(rbd_data->image, io_u->offset,
290 io_u->xfer_buflen, io_u->xfer_buf,
294 log_err("rbd_aio_read failed.\n");
297 } else if (io_u->ddir == DDIR_TRIM) {
298 r = rbd_aio_discard(rbd_data->image, io_u->offset,
299 io_u->xfer_buflen, fri->completion);
301 log_err("rbd_aio_discard failed.\n");
304 } else if (io_u->ddir == DDIR_SYNC) {
305 r = rbd_aio_flush(rbd_data->image, fri->completion);
307 log_err("rbd_flush failed.\n");
311 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
318 rbd_aio_release(fri->completion);
321 td_verror(td, io_u->error, "xfer");
322 return FIO_Q_COMPLETED;
325 static int fio_rbd_init(struct thread_data *td)
329 r = _fio_rbd_connect(td);
331 log_err("fio_rbd_connect failed, return code: %d .\n", r);
341 static void fio_rbd_cleanup(struct thread_data *td)
343 struct rbd_data *rbd_data = td->io_ops->data;
346 _fio_rbd_disconnect(rbd_data);
347 free(rbd_data->aio_events);
353 static int fio_rbd_setup(struct thread_data *td)
356 rbd_image_info_t info;
358 struct rbd_data *rbd_data = NULL;
359 int major, minor, extra;
361 /* log version of librbd. No cluster connection required. */
362 rbd_version(&major, &minor, &extra);
363 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
365 /* allocate engine specific structure to deal with librbd. */
366 r = _fio_setup_rbd_data(td, &rbd_data);
368 log_err("fio_setup_rbd_data failed.\n");
371 td->io_ops->data = rbd_data;
373 /* librbd does not allow us to run first in the main thread and later
374 * in a fork child. It needs to be the same process context all the
377 td->o.use_thread = 1;
379 /* connect in the main thread to determine to determine
380 * the size of the given RADOS block device. And disconnect
383 r = _fio_rbd_connect(td);
385 log_err("fio_rbd_connect failed.\n");
389 /* get size of the RADOS block device */
390 r = rbd_stat(rbd_data->image, &info, sizeof(info));
392 log_err("rbd_status failed.\n");
395 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
397 /* taken from "net" engine. Pretend we deal with files,
398 * even if we do not have any ideas about files.
399 * The size of the RBD is set instead of a artificial file.
401 if (!td->files_index) {
402 add_file(td, td->o.filename ? : "rbd", 0, 0);
403 td->o.nr_files = td->o.nr_files ? : 1;
407 f->real_file_size = info.size;
409 /* disconnect, then we were only connected to determine
410 * the size of the RBD.
412 _fio_rbd_disconnect(rbd_data);
416 _fio_rbd_disconnect(rbd_data);
422 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
427 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
429 #if defined(CONFIG_RBD_INVAL)
430 struct rbd_data *rbd_data = td->io_ops->data;
432 return rbd_invalidate_cache(rbd_data->image);
438 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
440 struct fio_rbd_iou *fri = io_u->engine_data;
443 io_u->engine_data = NULL;
448 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
450 struct fio_rbd_iou *fri;
452 fri = calloc(1, sizeof(*fri));
454 io_u->engine_data = fri;
458 static struct ioengine_ops ioengine = {
460 .version = FIO_IOOPS_VERSION,
461 .setup = fio_rbd_setup,
462 .init = fio_rbd_init,
463 .queue = fio_rbd_queue,
464 .getevents = fio_rbd_getevents,
465 .event = fio_rbd_event,
466 .cleanup = fio_rbd_cleanup,
467 .open_file = fio_rbd_open,
468 .invalidate = fio_rbd_invalidate,
470 .io_u_init = fio_rbd_io_u_init,
471 .io_u_free = fio_rbd_io_u_free,
472 .option_struct_size = sizeof(struct rbd_options),
475 static void fio_init fio_rbd_register(void)
477 register_ioengine(&ioengine);
480 static void fio_exit fio_rbd_unregister(void)
482 unregister_ioengine(&ioengine);