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);
135 rados_shutdown(rbd_data->cluster);
140 static void _fio_rbd_disconnect(struct rbd_data *rbd_data)
145 /* shutdown everything */
146 if (rbd_data->image) {
147 rbd_close(rbd_data->image);
148 rbd_data->image = NULL;
151 if (rbd_data->io_ctx) {
152 rados_ioctx_destroy(rbd_data->io_ctx);
153 rbd_data->io_ctx = NULL;
156 if (rbd_data->cluster) {
157 rados_shutdown(rbd_data->cluster);
158 rbd_data->cluster = NULL;
162 static void _fio_rbd_finish_write_aiocb(rbd_completion_t comp, void *data)
164 struct io_u *io_u = (struct io_u *)data;
165 struct fio_rbd_iou *fio_rbd_iou =
166 (struct fio_rbd_iou *)io_u->engine_data;
168 fio_rbd_iou->io_complete = 1;
170 /* if write needs to be verified - we should not release comp here
171 without fetching the result */
173 rbd_aio_release(comp);
174 /* TODO handle error */
179 static void _fio_rbd_finish_read_aiocb(rbd_completion_t comp, void *data)
181 struct io_u *io_u = (struct io_u *)data;
182 struct fio_rbd_iou *fio_rbd_iou =
183 (struct fio_rbd_iou *)io_u->engine_data;
185 fio_rbd_iou->io_complete = 1;
187 /* if read needs to be verified - we should not release comp here
188 without fetching the result */
189 rbd_aio_release(comp);
191 /* TODO handle error */
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 int fio_rbd_getevents(struct thread_data *td, unsigned int min,
204 unsigned int max, struct timespec *t)
206 struct rbd_data *rbd_data = td->io_ops->data;
207 unsigned int events = 0;
210 struct fio_rbd_iou *fov;
213 io_u_qiter(&td->io_u_all, io_u, i) {
214 if (!(io_u->flags & IO_U_F_FLIGHT))
217 fov = (struct fio_rbd_iou *)io_u->engine_data;
219 if (fov->io_complete) {
220 fov->io_complete = 0;
221 rbd_data->aio_events[events] = io_u;
236 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
239 struct rbd_data *rbd_data = td->io_ops->data;
240 rbd_completion_t comp;
242 fio_ro_check(td, io_u);
244 if (io_u->ddir == DDIR_WRITE) {
245 r = rbd_aio_create_completion(io_u,
247 _fio_rbd_finish_write_aiocb,
251 ("rbd_aio_create_completion for DDIR_WRITE failed.\n");
255 r = rbd_aio_write(rbd_data->image, io_u->offset,
256 io_u->xfer_buflen, io_u->xfer_buf, comp);
258 log_err("rbd_aio_write failed.\n");
262 } else if (io_u->ddir == DDIR_READ) {
263 r = rbd_aio_create_completion(io_u,
265 _fio_rbd_finish_read_aiocb,
269 ("rbd_aio_create_completion for DDIR_READ failed.\n");
273 r = rbd_aio_read(rbd_data->image, io_u->offset,
274 io_u->xfer_buflen, io_u->xfer_buf, comp);
277 log_err("rbd_aio_read failed.\n");
281 } else if (io_u->ddir == DDIR_SYNC) {
282 r = rbd_flush(rbd_data->image);
284 log_err("rbd_flush failed.\n");
288 return FIO_Q_COMPLETED;
290 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
292 return FIO_Q_COMPLETED;
299 td_verror(td, io_u->error, "xfer");
300 return FIO_Q_COMPLETED;
303 static int fio_rbd_init(struct thread_data *td)
307 r = _fio_rbd_connect(td);
309 log_err("fio_rbd_connect failed, return code: %d .\n", r);
320 static void fio_rbd_cleanup(struct thread_data *td)
322 struct rbd_data *rbd_data = td->io_ops->data;
325 _fio_rbd_disconnect(rbd_data);
326 free(rbd_data->aio_events);
332 static int fio_rbd_setup(struct thread_data *td)
335 rbd_image_info_t info;
337 struct rbd_data *rbd_data = NULL;
338 int major, minor, extra;
340 /* log version of librbd. No cluster connection required. */
341 rbd_version(&major, &minor, &extra);
342 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
344 /* allocate engine specific structure to deal with librbd. */
345 r = _fio_setup_rbd_data(td, &rbd_data);
347 log_err("fio_setup_rbd_data failed.\n");
350 td->io_ops->data = rbd_data;
352 /* librbd does not allow us to run first in the main thread and later in a
353 * fork child. It needs to be the same process context all the time.
355 td->o.use_thread = 1;
357 /* connect in the main thread to determine to determine
358 * the size of the given RADOS block device. And disconnect
361 r = _fio_rbd_connect(td);
363 log_err("fio_rbd_connect failed.\n");
367 /* get size of the RADOS block device */
368 r = rbd_stat(rbd_data->image, &info, sizeof(info));
370 log_err("rbd_status failed.\n");
373 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
375 /* taken from "net" engine. Pretend we deal with files,
376 * even if we do not have any ideas about files.
377 * The size of the RBD is set instead of a artificial file.
379 if (!td->files_index) {
380 add_file(td, td->o.filename ? : "rbd", 0, 0);
381 td->o.nr_files = td->o.nr_files ? : 1;
384 f->real_file_size = info.size;
386 /* disconnect, then we were only connected to determine
387 * the size of the RBD.
389 _fio_rbd_disconnect(rbd_data);
393 _fio_rbd_disconnect(rbd_data);
399 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
404 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
406 struct fio_rbd_iou *o = io_u->engine_data;
409 io_u->engine_data = NULL;
414 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
416 struct fio_rbd_iou *o;
418 o = malloc(sizeof(*o));
421 io_u->engine_data = o;
425 struct ioengine_ops ioengine = {
427 .version = FIO_IOOPS_VERSION,
428 .setup = fio_rbd_setup,
429 .init = fio_rbd_init,
430 .queue = fio_rbd_queue,
431 .getevents = fio_rbd_getevents,
432 .event = fio_rbd_event,
433 .cleanup = fio_rbd_cleanup,
434 .open_file = fio_rbd_open,
436 .io_u_init = fio_rbd_io_u_init,
437 .io_u_free = fio_rbd_io_u_free,
438 .option_struct_size = sizeof(struct rbd_options),
441 static void fio_init fio_rbd_register(void)
443 register_ioengine(&ioengine);
446 static void fio_exit fio_rbd_unregister(void)
448 unregister_ioengine(&ioengine);