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 struct io_u *fio_rbd_event(struct thread_data *td, int event)
200 struct rbd_data *rbd_data = td->io_ops->data;
202 return rbd_data->aio_events[event];
205 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
206 unsigned int max, struct timespec *t)
208 struct rbd_data *rbd_data = td->io_ops->data;
209 unsigned int events = 0;
212 struct fio_rbd_iou *fov;
215 io_u_qiter(&td->io_u_all, io_u, i) {
216 if (!(io_u->flags & IO_U_F_FLIGHT))
219 fov = (struct fio_rbd_iou *)io_u->engine_data;
221 if (fov->io_complete) {
222 fov->io_complete = 0;
223 rbd_data->aio_events[events] = io_u;
238 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
241 struct rbd_data *rbd_data = td->io_ops->data;
242 rbd_completion_t comp;
244 fio_ro_check(td, io_u);
246 if (io_u->ddir == DDIR_WRITE) {
247 r = rbd_aio_create_completion(io_u,
249 _fio_rbd_finish_write_aiocb,
253 ("rbd_aio_create_completion for DDIR_WRITE failed.\n");
257 r = rbd_aio_write(rbd_data->image, io_u->offset,
258 io_u->xfer_buflen, io_u->xfer_buf, comp);
260 log_err("rbd_aio_write failed.\n");
264 } else if (io_u->ddir == DDIR_READ) {
265 r = rbd_aio_create_completion(io_u,
267 _fio_rbd_finish_read_aiocb,
271 ("rbd_aio_create_completion for DDIR_READ failed.\n");
275 r = rbd_aio_read(rbd_data->image, io_u->offset,
276 io_u->xfer_buflen, io_u->xfer_buf, comp);
279 log_err("rbd_aio_read failed.\n");
283 } else if (io_u->ddir == DDIR_SYNC) {
284 r = rbd_flush(rbd_data->image);
286 log_err("rbd_flush failed.\n");
290 return FIO_Q_COMPLETED;
292 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
294 return FIO_Q_COMPLETED;
301 td_verror(td, io_u->error, "xfer");
302 return FIO_Q_COMPLETED;
305 static int fio_rbd_init(struct thread_data *td)
309 r = _fio_rbd_connect(td);
311 log_err("fio_rbd_connect failed, return code: %d .\n", r);
322 static void fio_rbd_cleanup(struct thread_data *td)
324 struct rbd_data *rbd_data = td->io_ops->data;
327 _fio_rbd_disconnect(rbd_data);
328 free(rbd_data->aio_events);
334 static int fio_rbd_setup(struct thread_data *td)
337 rbd_image_info_t info;
339 struct rbd_data *rbd_data = NULL;
340 int major, minor, extra;
342 /* log version of librbd. No cluster connection required. */
343 rbd_version(&major, &minor, &extra);
344 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
346 /* allocate engine specific structure to deal with librbd. */
347 r = _fio_setup_rbd_data(td, &rbd_data);
349 log_err("fio_setup_rbd_data failed.\n");
352 td->io_ops->data = rbd_data;
354 /* librbd does not allow us to run first in the main thread and later in a
355 * fork child. It needs to be the same process context all the time.
357 td->o.use_thread = 1;
359 /* connect in the main thread to determine to determine
360 * the size of the given RADOS block device. And disconnect
363 r = _fio_rbd_connect(td);
365 log_err("fio_rbd_connect failed.\n");
369 /* get size of the RADOS block device */
370 r = rbd_stat(rbd_data->image, &info, sizeof(info));
372 log_err("rbd_status failed.\n");
375 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
377 /* taken from "net" engine. Pretend we deal with files,
378 * even if we do not have any ideas about files.
379 * The size of the RBD is set instead of a artificial file.
381 if (!td->files_index) {
382 add_file(td, td->o.filename ? : "rbd", 0, 0);
383 td->o.nr_files = td->o.nr_files ? : 1;
387 f->real_file_size = info.size;
389 /* disconnect, then we were only connected to determine
390 * the size of the RBD.
392 _fio_rbd_disconnect(rbd_data);
396 _fio_rbd_disconnect(rbd_data);
402 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
407 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
409 struct fio_rbd_iou *o = io_u->engine_data;
412 io_u->engine_data = NULL;
417 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
419 struct fio_rbd_iou *o;
421 o = malloc(sizeof(*o));
424 io_u->engine_data = o;
428 static struct ioengine_ops ioengine = {
430 .version = FIO_IOOPS_VERSION,
431 .setup = fio_rbd_setup,
432 .init = fio_rbd_init,
433 .queue = fio_rbd_queue,
434 .getevents = fio_rbd_getevents,
435 .event = fio_rbd_event,
436 .cleanup = fio_rbd_cleanup,
437 .open_file = fio_rbd_open,
439 .io_u_init = fio_rbd_io_u_init,
440 .io_u_free = fio_rbd_io_u_free,
441 .option_struct_size = sizeof(struct rbd_options),
444 static void fio_init fio_rbd_register(void)
446 register_ioengine(&ioengine);
449 static void fio_exit fio_rbd_unregister(void)
451 unregister_ioengine(&ioengine);