4 * IO engine using Ceph's librbd to test RADOS Block Devices.
8 #include <rbd/librbd.h>
11 #include "../optgroup.h"
12 #ifdef CONFIG_RBD_BLKIN
18 rbd_completion_t completion;
21 struct blkin_trace_info info;
28 struct io_u **aio_events;
29 struct io_u **sort_events;
41 static struct fio_option options[] = {
43 .name = "clustername",
44 .lname = "ceph cluster name",
45 .type = FIO_OPT_STR_STORE,
46 .help = "Cluster name for ceph",
47 .off1 = offsetof(struct rbd_options, cluster_name),
48 .category = FIO_OPT_C_ENGINE,
49 .group = FIO_OPT_G_RBD,
53 .lname = "rbd engine rbdname",
54 .type = FIO_OPT_STR_STORE,
55 .help = "RBD name for RBD engine",
56 .off1 = offsetof(struct rbd_options, rbd_name),
57 .category = FIO_OPT_C_ENGINE,
58 .group = FIO_OPT_G_RBD,
62 .lname = "rbd engine pool",
63 .type = FIO_OPT_STR_STORE,
64 .help = "Name of the pool hosting the RBD for the RBD engine",
65 .off1 = offsetof(struct rbd_options, pool_name),
66 .category = FIO_OPT_C_ENGINE,
67 .group = FIO_OPT_G_RBD,
71 .lname = "rbd engine clientname",
72 .type = FIO_OPT_STR_STORE,
73 .help = "Name of the ceph client to access the RBD for the RBD engine",
74 .off1 = offsetof(struct rbd_options, client_name),
75 .category = FIO_OPT_C_ENGINE,
76 .group = FIO_OPT_G_RBD,
82 .help = "Busy poll for completions instead of sleeping",
83 .off1 = offsetof(struct rbd_options, busy_poll),
85 .category = FIO_OPT_C_ENGINE,
86 .group = FIO_OPT_G_RBD,
93 static int _fio_setup_rbd_data(struct thread_data *td,
94 struct rbd_data **rbd_data_ptr)
101 rbd = calloc(1, sizeof(struct rbd_data));
105 rbd->aio_events = calloc(td->o.iodepth, sizeof(struct io_u *));
106 if (!rbd->aio_events)
109 rbd->sort_events = calloc(td->o.iodepth, sizeof(struct io_u *));
110 if (!rbd->sort_events)
119 free(rbd->aio_events);
120 if (rbd->sort_events)
121 free(rbd->sort_events);
128 static int _fio_rbd_connect(struct thread_data *td)
130 struct rbd_data *rbd = td->io_ops_data;
131 struct rbd_options *o = td->eo;
134 if (o->cluster_name) {
135 char *client_name = NULL;
138 * If we specify cluser name, the rados_create2
139 * will not assume 'client.'. name is considered
140 * as a full type.id namestr
142 if (o->client_name) {
143 if (!index(o->client_name, '.')) {
144 client_name = calloc(1, strlen("client.") +
145 strlen(o->client_name) + 1);
146 strcat(client_name, "client.");
147 strcat(client_name, o->client_name);
149 client_name = o->client_name;
153 r = rados_create2(&rbd->cluster, o->cluster_name,
156 if (client_name && !index(o->client_name, '.'))
159 r = rados_create(&rbd->cluster, o->client_name);
162 log_err("rados_create failed.\n");
166 r = rados_conf_read_file(rbd->cluster, NULL);
168 log_err("rados_conf_read_file failed.\n");
172 r = rados_connect(rbd->cluster);
174 log_err("rados_connect failed.\n");
175 goto failed_shutdown;
178 r = rados_ioctx_create(rbd->cluster, o->pool_name, &rbd->io_ctx);
180 log_err("rados_ioctx_create failed.\n");
181 goto failed_shutdown;
184 r = rbd_open(rbd->io_ctx, o->rbd_name, &rbd->image, NULL /*snap */ );
186 log_err("rbd_open failed.\n");
192 rados_ioctx_destroy(rbd->io_ctx);
195 rados_shutdown(rbd->cluster);
201 static void _fio_rbd_disconnect(struct rbd_data *rbd)
206 /* shutdown everything */
208 rbd_close(rbd->image);
213 rados_ioctx_destroy(rbd->io_ctx);
218 rados_shutdown(rbd->cluster);
223 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
225 struct fio_rbd_iou *fri = data;
226 struct io_u *io_u = fri->io_u;
230 * Looks like return value is 0 for success, or < 0 for
231 * a specific error. So we have to assume that it can't do
232 * partial completions.
234 ret = rbd_aio_get_return_value(fri->completion);
237 io_u->resid = io_u->xfer_buflen;
241 fri->io_complete = 1;
244 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
246 struct rbd_data *rbd = td->io_ops_data;
248 return rbd->aio_events[event];
251 static inline int fri_check_complete(struct rbd_data *rbd, struct io_u *io_u,
252 unsigned int *events)
254 struct fio_rbd_iou *fri = io_u->engine_data;
256 if (fri->io_complete) {
258 rbd->aio_events[*events] = io_u;
261 rbd_aio_release(fri->completion);
268 static inline int rbd_io_u_seen(struct io_u *io_u)
270 struct fio_rbd_iou *fri = io_u->engine_data;
275 static void rbd_io_u_wait_complete(struct io_u *io_u)
277 struct fio_rbd_iou *fri = io_u->engine_data;
279 rbd_aio_wait_for_complete(fri->completion);
282 static int rbd_io_u_cmp(const void *p1, const void *p2)
284 const struct io_u **a = (const struct io_u **) p1;
285 const struct io_u **b = (const struct io_u **) p2;
288 at = utime_since_now(&(*a)->start_time);
289 bt = utime_since_now(&(*b)->start_time);
299 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
300 unsigned int min_evts, int wait)
302 struct rbd_data *rbd = td->io_ops_data;
303 unsigned int this_events = 0;
308 io_u_qiter(&td->io_u_all, io_u, i) {
309 if (!(io_u->flags & IO_U_F_FLIGHT))
311 if (rbd_io_u_seen(io_u))
314 if (fri_check_complete(rbd, io_u, events))
317 rbd->sort_events[sidx++] = io_u;
324 * Sort events, oldest issue first, then wait on as many as we
325 * need in order of age. If we have enough events, stop waiting,
326 * and just check if any of the older ones are done.
329 qsort(rbd->sort_events, sidx, sizeof(struct io_u *), rbd_io_u_cmp);
331 for (i = 0; i < sidx; i++) {
332 io_u = rbd->sort_events[i];
334 if (fri_check_complete(rbd, io_u, events)) {
340 * Stop waiting when we have enough, but continue checking
341 * all pending IOs if they are complete.
343 if (*events >= min_evts)
346 rbd_io_u_wait_complete(io_u);
348 if (fri_check_complete(rbd, io_u, events))
355 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
356 unsigned int max, const struct timespec *t)
358 unsigned int this_events, events = 0;
359 struct rbd_options *o = td->eo;
363 this_events = rbd_iter_events(td, &events, min, wait);
379 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
381 struct rbd_data *rbd = td->io_ops_data;
382 struct fio_rbd_iou *fri = io_u->engine_data;
385 fio_ro_check(td, io_u);
388 fri->io_complete = 0;
390 r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
393 log_err("rbd_aio_create_completion failed.\n");
397 if (io_u->ddir == DDIR_WRITE) {
398 #ifdef CONFIG_RBD_BLKIN
399 blkin_init_trace_info(&fri->info);
400 r = rbd_aio_write_traced(rbd->image, io_u->offset, io_u->xfer_buflen,
401 io_u->xfer_buf, fri->completion, &fri->info);
403 r = rbd_aio_write(rbd->image, io_u->offset, io_u->xfer_buflen,
404 io_u->xfer_buf, fri->completion);
407 log_err("rbd_aio_write failed.\n");
411 } else if (io_u->ddir == DDIR_READ) {
412 #ifdef CONFIG_RBD_BLKIN
413 blkin_init_trace_info(&fri->info);
414 r = rbd_aio_read_traced(rbd->image, io_u->offset, io_u->xfer_buflen,
415 io_u->xfer_buf, fri->completion, &fri->info);
417 r = rbd_aio_read(rbd->image, io_u->offset, io_u->xfer_buflen,
418 io_u->xfer_buf, fri->completion);
422 log_err("rbd_aio_read failed.\n");
425 } else if (io_u->ddir == DDIR_TRIM) {
426 r = rbd_aio_discard(rbd->image, io_u->offset,
427 io_u->xfer_buflen, fri->completion);
429 log_err("rbd_aio_discard failed.\n");
432 } else if (io_u->ddir == DDIR_SYNC) {
433 r = rbd_aio_flush(rbd->image, fri->completion);
435 log_err("rbd_flush failed.\n");
439 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
446 rbd_aio_release(fri->completion);
449 td_verror(td, io_u->error, "xfer");
450 return FIO_Q_COMPLETED;
453 static int fio_rbd_init(struct thread_data *td)
457 r = _fio_rbd_connect(td);
459 log_err("fio_rbd_connect failed, return code: %d .\n", r);
469 static void fio_rbd_cleanup(struct thread_data *td)
471 struct rbd_data *rbd = td->io_ops_data;
474 _fio_rbd_disconnect(rbd);
475 free(rbd->aio_events);
476 free(rbd->sort_events);
481 static int fio_rbd_setup(struct thread_data *td)
483 rbd_image_info_t info;
485 struct rbd_data *rbd = NULL;
486 int major, minor, extra;
489 /* log version of librbd. No cluster connection required. */
490 rbd_version(&major, &minor, &extra);
491 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
493 /* allocate engine specific structure to deal with librbd. */
494 r = _fio_setup_rbd_data(td, &rbd);
496 log_err("fio_setup_rbd_data failed.\n");
499 td->io_ops_data = rbd;
501 /* librbd does not allow us to run first in the main thread and later
502 * in a fork child. It needs to be the same process context all the
505 td->o.use_thread = 1;
507 /* connect in the main thread to determine to determine
508 * the size of the given RADOS block device. And disconnect
511 r = _fio_rbd_connect(td);
513 log_err("fio_rbd_connect failed.\n");
517 /* get size of the RADOS block device */
518 r = rbd_stat(rbd->image, &info, sizeof(info));
520 log_err("rbd_status failed.\n");
523 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
525 /* taken from "net" engine. Pretend we deal with files,
526 * even if we do not have any ideas about files.
527 * The size of the RBD is set instead of a artificial file.
529 if (!td->files_index) {
530 add_file(td, td->o.filename ? : "rbd", 0, 0);
531 td->o.nr_files = td->o.nr_files ? : 1;
535 f->real_file_size = info.size;
537 /* disconnect, then we were only connected to determine
538 * the size of the RBD.
540 _fio_rbd_disconnect(rbd);
544 _fio_rbd_disconnect(rbd);
550 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
555 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
557 #if defined(CONFIG_RBD_INVAL)
558 struct rbd_data *rbd = td->io_ops_data;
560 return rbd_invalidate_cache(rbd->image);
566 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
568 struct fio_rbd_iou *fri = io_u->engine_data;
571 io_u->engine_data = NULL;
576 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
578 struct fio_rbd_iou *fri;
580 fri = calloc(1, sizeof(*fri));
582 io_u->engine_data = fri;
586 static struct ioengine_ops ioengine = {
588 .version = FIO_IOOPS_VERSION,
589 .setup = fio_rbd_setup,
590 .init = fio_rbd_init,
591 .queue = fio_rbd_queue,
592 .getevents = fio_rbd_getevents,
593 .event = fio_rbd_event,
594 .cleanup = fio_rbd_cleanup,
595 .open_file = fio_rbd_open,
596 .invalidate = fio_rbd_invalidate,
598 .io_u_init = fio_rbd_io_u_init,
599 .io_u_free = fio_rbd_io_u_free,
600 .option_struct_size = sizeof(struct rbd_options),
603 static void fio_init fio_rbd_register(void)
605 register_ioengine(&ioengine);
608 static void fio_exit fio_rbd_unregister(void)
610 unregister_ioengine(&ioengine);