4 * IO engine using Ceph's librbd to test RADOS Block Devices.
8 #include <rbd/librbd.h>
21 struct io_u **aio_events;
25 struct thread_data *td;
31 static struct fio_option options[] = {
34 .lname = "rbd engine rbdname",
35 .type = FIO_OPT_STR_STORE,
36 .help = "RBD name for RBD engine",
37 .off1 = offsetof(struct rbd_options, rbd_name),
38 .category = FIO_OPT_C_ENGINE,
39 .group = FIO_OPT_G_RBD,
43 .lname = "rbd engine pool",
44 .type = FIO_OPT_STR_STORE,
45 .help = "Name of the pool hosting the RBD for the RBD engine",
46 .off1 = offsetof(struct rbd_options, pool_name),
47 .category = FIO_OPT_C_ENGINE,
48 .group = FIO_OPT_G_RBD,
52 .lname = "rbd engine clientname",
53 .type = FIO_OPT_STR_STORE,
54 .help = "Name of the ceph client to access the RBD for the RBD engine",
55 .off1 = offsetof(struct rbd_options, client_name),
56 .category = FIO_OPT_C_ENGINE,
57 .group = FIO_OPT_G_RBD,
64 static int _fio_setup_rbd_data(struct thread_data *td,
65 struct rbd_data **rbd_data_ptr)
67 struct rbd_data *rbd_data;
72 rbd_data = malloc(sizeof(struct rbd_data));
76 memset(rbd_data, 0, sizeof(struct rbd_data));
78 rbd_data->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
79 if (!rbd_data->aio_events)
82 memset(rbd_data->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
84 *rbd_data_ptr = rbd_data;
93 static int _fio_rbd_connect(struct thread_data *td)
95 struct rbd_data *rbd_data = td->io_ops->data;
96 struct rbd_options *o = td->eo;
99 r = rados_create(&(rbd_data->cluster), o->client_name);
101 log_err("rados_create failed.\n");
105 r = rados_conf_read_file(rbd_data->cluster, NULL);
107 log_err("rados_conf_read_file failed.\n");
111 r = rados_connect(rbd_data->cluster);
113 log_err("rados_connect failed.\n");
114 goto failed_shutdown;
117 r = rados_ioctx_create(rbd_data->cluster, o->pool_name,
118 &(rbd_data->io_ctx));
120 log_err("rados_ioctx_create failed.\n");
121 goto failed_shutdown;
124 r = rbd_open(rbd_data->io_ctx, o->rbd_name, &(rbd_data->image),
127 log_err("rbd_open failed.\n");
133 rados_ioctx_destroy(rbd_data->io_ctx);
134 rbd_data->io_ctx = NULL;
136 rados_shutdown(rbd_data->cluster);
137 rbd_data->cluster = NULL;
142 static void _fio_rbd_disconnect(struct rbd_data *rbd_data)
147 /* shutdown everything */
148 if (rbd_data->image) {
149 rbd_close(rbd_data->image);
150 rbd_data->image = NULL;
153 if (rbd_data->io_ctx) {
154 rados_ioctx_destroy(rbd_data->io_ctx);
155 rbd_data->io_ctx = NULL;
158 if (rbd_data->cluster) {
159 rados_shutdown(rbd_data->cluster);
160 rbd_data->cluster = NULL;
164 static void _fio_rbd_finish_write_aiocb(rbd_completion_t comp, void *data)
166 struct io_u *io_u = (struct io_u *)data;
167 struct fio_rbd_iou *fio_rbd_iou =
168 (struct fio_rbd_iou *)io_u->engine_data;
170 fio_rbd_iou->io_complete = 1;
172 /* if write needs to be verified - we should not release comp here
173 without fetching the result */
175 rbd_aio_release(comp);
176 /* TODO handle error */
181 static void _fio_rbd_finish_read_aiocb(rbd_completion_t comp, void *data)
183 struct io_u *io_u = (struct io_u *)data;
184 struct fio_rbd_iou *fio_rbd_iou =
185 (struct fio_rbd_iou *)io_u->engine_data;
187 fio_rbd_iou->io_complete = 1;
189 /* if read needs to be verified - we should not release comp here
190 without fetching the result */
191 rbd_aio_release(comp);
193 /* TODO handle error */
198 static void _fio_rbd_finish_sync_aiocb(rbd_completion_t comp, void *data)
200 struct io_u *io_u = (struct io_u *)data;
201 struct fio_rbd_iou *fio_rbd_iou =
202 (struct fio_rbd_iou *)io_u->engine_data;
204 fio_rbd_iou->io_complete = 1;
206 /* if sync needs to be verified - we should not release comp here
207 without fetching the result */
208 rbd_aio_release(comp);
210 /* TODO handle error */
215 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
217 struct rbd_data *rbd_data = td->io_ops->data;
219 return rbd_data->aio_events[event];
222 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
223 unsigned int max, struct timespec *t)
225 struct rbd_data *rbd_data = td->io_ops->data;
226 unsigned int events = 0;
229 struct fio_rbd_iou *fov;
232 io_u_qiter(&td->io_u_all, io_u, i) {
233 if (!(io_u->flags & IO_U_F_FLIGHT))
236 fov = (struct fio_rbd_iou *)io_u->engine_data;
238 if (fov->io_complete) {
239 fov->io_complete = 0;
240 rbd_data->aio_events[events] = io_u;
255 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
258 struct rbd_data *rbd_data = td->io_ops->data;
259 rbd_completion_t comp;
261 fio_ro_check(td, io_u);
263 if (io_u->ddir == DDIR_WRITE) {
264 r = rbd_aio_create_completion(io_u,
266 _fio_rbd_finish_write_aiocb,
270 ("rbd_aio_create_completion for DDIR_WRITE failed.\n");
274 r = rbd_aio_write(rbd_data->image, io_u->offset,
275 io_u->xfer_buflen, io_u->xfer_buf, comp);
277 log_err("rbd_aio_write failed.\n");
281 } else if (io_u->ddir == DDIR_READ) {
282 r = rbd_aio_create_completion(io_u,
284 _fio_rbd_finish_read_aiocb,
288 ("rbd_aio_create_completion for DDIR_READ failed.\n");
292 r = rbd_aio_read(rbd_data->image, io_u->offset,
293 io_u->xfer_buflen, io_u->xfer_buf, comp);
296 log_err("rbd_aio_read failed.\n");
300 } else if (io_u->ddir == DDIR_SYNC) {
301 r = rbd_aio_create_completion(io_u,
303 _fio_rbd_finish_sync_aiocb,
307 ("rbd_aio_create_completion for DDIR_SYNC failed.\n");
311 r = rbd_aio_flush(rbd_data->image, comp);
313 log_err("rbd_flush failed.\n");
318 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
320 return FIO_Q_COMPLETED;
327 td_verror(td, io_u->error, "xfer");
328 return FIO_Q_COMPLETED;
331 static int fio_rbd_init(struct thread_data *td)
335 r = _fio_rbd_connect(td);
337 log_err("fio_rbd_connect failed, return code: %d .\n", r);
348 static void fio_rbd_cleanup(struct thread_data *td)
350 struct rbd_data *rbd_data = td->io_ops->data;
353 _fio_rbd_disconnect(rbd_data);
354 free(rbd_data->aio_events);
360 static int fio_rbd_setup(struct thread_data *td)
363 rbd_image_info_t info;
365 struct rbd_data *rbd_data = NULL;
366 int major, minor, extra;
368 /* log version of librbd. No cluster connection required. */
369 rbd_version(&major, &minor, &extra);
370 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
372 /* allocate engine specific structure to deal with librbd. */
373 r = _fio_setup_rbd_data(td, &rbd_data);
375 log_err("fio_setup_rbd_data failed.\n");
378 td->io_ops->data = rbd_data;
380 /* librbd does not allow us to run first in the main thread and later in a
381 * fork child. It needs to be the same process context all the time.
383 td->o.use_thread = 1;
385 /* connect in the main thread to determine to determine
386 * the size of the given RADOS block device. And disconnect
389 r = _fio_rbd_connect(td);
391 log_err("fio_rbd_connect failed.\n");
395 /* get size of the RADOS block device */
396 r = rbd_stat(rbd_data->image, &info, sizeof(info));
398 log_err("rbd_status failed.\n");
401 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
403 /* taken from "net" engine. Pretend we deal with files,
404 * even if we do not have any ideas about files.
405 * The size of the RBD is set instead of a artificial file.
407 if (!td->files_index) {
408 add_file(td, td->o.filename ? : "rbd", 0, 0);
409 td->o.nr_files = td->o.nr_files ? : 1;
413 f->real_file_size = info.size;
415 /* disconnect, then we were only connected to determine
416 * the size of the RBD.
418 _fio_rbd_disconnect(rbd_data);
422 _fio_rbd_disconnect(rbd_data);
428 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
433 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
438 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
440 struct fio_rbd_iou *o = 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 *o;
452 o = malloc(sizeof(*o));
455 io_u->engine_data = o;
459 static struct ioengine_ops ioengine = {
461 .version = FIO_IOOPS_VERSION,
462 .setup = fio_rbd_setup,
463 .init = fio_rbd_init,
464 .queue = fio_rbd_queue,
465 .getevents = fio_rbd_getevents,
466 .event = fio_rbd_event,
467 .cleanup = fio_rbd_cleanup,
468 .open_file = fio_rbd_open,
469 .invalidate = fio_rbd_invalidate,
471 .io_u_init = fio_rbd_io_u_init,
472 .io_u_free = fio_rbd_io_u_free,
473 .option_struct_size = sizeof(struct rbd_options),
476 static void fio_init fio_rbd_register(void)
478 register_ioengine(&ioengine);
481 static void fio_exit fio_rbd_unregister(void)
483 unregister_ioengine(&ioengine);