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 #ifdef CONFIG_RBD_BLKIN
22 struct blkin_trace_info info;
30 struct io_u **aio_events;
31 struct io_u **sort_events;
43 static struct fio_option options[] = {
45 .name = "clustername",
46 .lname = "ceph cluster name",
47 .type = FIO_OPT_STR_STORE,
48 .help = "Cluster name for ceph",
49 .off1 = offsetof(struct rbd_options, cluster_name),
50 .category = FIO_OPT_C_ENGINE,
51 .group = FIO_OPT_G_RBD,
55 .lname = "rbd engine rbdname",
56 .type = FIO_OPT_STR_STORE,
57 .help = "RBD name for RBD engine",
58 .off1 = offsetof(struct rbd_options, rbd_name),
59 .category = FIO_OPT_C_ENGINE,
60 .group = FIO_OPT_G_RBD,
64 .lname = "rbd engine pool",
65 .type = FIO_OPT_STR_STORE,
66 .help = "Name of the pool hosting the RBD for the RBD engine",
67 .off1 = offsetof(struct rbd_options, pool_name),
68 .category = FIO_OPT_C_ENGINE,
69 .group = FIO_OPT_G_RBD,
73 .lname = "rbd engine clientname",
74 .type = FIO_OPT_STR_STORE,
75 .help = "Name of the ceph client to access the RBD for the RBD engine",
76 .off1 = offsetof(struct rbd_options, client_name),
77 .category = FIO_OPT_C_ENGINE,
78 .group = FIO_OPT_G_RBD,
84 .help = "Busy poll for completions instead of sleeping",
85 .off1 = offsetof(struct rbd_options, busy_poll),
87 .category = FIO_OPT_C_ENGINE,
88 .group = FIO_OPT_G_RBD,
95 static int _fio_setup_rbd_data(struct thread_data *td,
96 struct rbd_data **rbd_data_ptr)
103 rbd = calloc(1, sizeof(struct rbd_data));
107 rbd->aio_events = calloc(td->o.iodepth, sizeof(struct io_u *));
108 if (!rbd->aio_events)
111 rbd->sort_events = calloc(td->o.iodepth, sizeof(struct io_u *));
112 if (!rbd->sort_events)
121 free(rbd->aio_events);
122 if (rbd->sort_events)
123 free(rbd->sort_events);
130 static int _fio_rbd_connect(struct thread_data *td)
132 struct rbd_data *rbd = td->io_ops_data;
133 struct rbd_options *o = td->eo;
136 if (o->cluster_name) {
137 char *client_name = NULL;
140 * If we specify cluser name, the rados_create2
141 * will not assume 'client.'. name is considered
142 * as a full type.id namestr
144 if (o->client_name) {
145 if (!index(o->client_name, '.')) {
146 client_name = calloc(1, strlen("client.") +
147 strlen(o->client_name) + 1);
148 strcat(client_name, "client.");
149 strcat(client_name, o->client_name);
151 client_name = o->client_name;
155 r = rados_create2(&rbd->cluster, o->cluster_name,
158 if (client_name && !index(o->client_name, '.'))
161 r = rados_create(&rbd->cluster, o->client_name);
164 log_err("rados_create failed.\n");
168 r = rados_conf_read_file(rbd->cluster, NULL);
170 log_err("rados_conf_read_file failed.\n");
174 r = rados_connect(rbd->cluster);
176 log_err("rados_connect failed.\n");
177 goto failed_shutdown;
180 r = rados_ioctx_create(rbd->cluster, o->pool_name, &rbd->io_ctx);
182 log_err("rados_ioctx_create failed.\n");
183 goto failed_shutdown;
186 r = rbd_open(rbd->io_ctx, o->rbd_name, &rbd->image, NULL /*snap */ );
188 log_err("rbd_open failed.\n");
194 rados_ioctx_destroy(rbd->io_ctx);
197 rados_shutdown(rbd->cluster);
203 static void _fio_rbd_disconnect(struct rbd_data *rbd)
208 /* shutdown everything */
210 rbd_close(rbd->image);
215 rados_ioctx_destroy(rbd->io_ctx);
220 rados_shutdown(rbd->cluster);
225 static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data)
227 struct fio_rbd_iou *fri = data;
228 struct io_u *io_u = fri->io_u;
232 * Looks like return value is 0 for success, or < 0 for
233 * a specific error. So we have to assume that it can't do
234 * partial completions.
236 ret = rbd_aio_get_return_value(fri->completion);
239 io_u->resid = io_u->xfer_buflen;
243 fri->io_complete = 1;
246 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
248 struct rbd_data *rbd = td->io_ops_data;
250 return rbd->aio_events[event];
253 static inline int fri_check_complete(struct rbd_data *rbd, struct io_u *io_u,
254 unsigned int *events)
256 struct fio_rbd_iou *fri = io_u->engine_data;
258 if (fri->io_complete) {
260 rbd->aio_events[*events] = io_u;
263 rbd_aio_release(fri->completion);
270 static inline int rbd_io_u_seen(struct io_u *io_u)
272 struct fio_rbd_iou *fri = io_u->engine_data;
277 static void rbd_io_u_wait_complete(struct io_u *io_u)
279 struct fio_rbd_iou *fri = io_u->engine_data;
281 rbd_aio_wait_for_complete(fri->completion);
284 static int rbd_io_u_cmp(const void *p1, const void *p2)
286 const struct io_u **a = (const struct io_u **) p1;
287 const struct io_u **b = (const struct io_u **) p2;
290 at = utime_since_now(&(*a)->start_time);
291 bt = utime_since_now(&(*b)->start_time);
301 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
302 unsigned int min_evts, int wait)
304 struct rbd_data *rbd = td->io_ops_data;
305 unsigned int this_events = 0;
310 io_u_qiter(&td->io_u_all, io_u, i) {
311 if (!(io_u->flags & IO_U_F_FLIGHT))
313 if (rbd_io_u_seen(io_u))
316 if (fri_check_complete(rbd, io_u, events))
319 rbd->sort_events[sidx++] = io_u;
326 * Sort events, oldest issue first, then wait on as many as we
327 * need in order of age. If we have enough events, stop waiting,
328 * and just check if any of the older ones are done.
331 qsort(rbd->sort_events, sidx, sizeof(struct io_u *), rbd_io_u_cmp);
333 for (i = 0; i < sidx; i++) {
334 io_u = rbd->sort_events[i];
336 if (fri_check_complete(rbd, io_u, events)) {
342 * Stop waiting when we have enough, but continue checking
343 * all pending IOs if they are complete.
345 if (*events >= min_evts)
348 rbd_io_u_wait_complete(io_u);
350 if (fri_check_complete(rbd, io_u, events))
357 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
358 unsigned int max, const struct timespec *t)
360 unsigned int this_events, events = 0;
361 struct rbd_options *o = td->eo;
365 this_events = rbd_iter_events(td, &events, min, wait);
381 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
383 struct rbd_data *rbd = td->io_ops_data;
384 struct fio_rbd_iou *fri = io_u->engine_data;
387 fio_ro_check(td, io_u);
390 fri->io_complete = 0;
392 r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
395 log_err("rbd_aio_create_completion failed.\n");
399 if (io_u->ddir == DDIR_WRITE) {
400 #ifdef CONFIG_RBD_BLKIN
401 blkin_init_trace_info(&fri->info);
402 r = rbd_aio_write_traced(rbd->image, io_u->offset, io_u->xfer_buflen,
403 io_u->xfer_buf, fri->completion, &fri->info);
405 r = rbd_aio_write(rbd->image, io_u->offset, io_u->xfer_buflen,
406 io_u->xfer_buf, fri->completion);
409 log_err("rbd_aio_write failed.\n");
413 } else if (io_u->ddir == DDIR_READ) {
414 #ifdef CONFIG_RBD_BLKIN
415 blkin_init_trace_info(&fri->info);
416 r = rbd_aio_read_traced(rbd->image, io_u->offset, io_u->xfer_buflen,
417 io_u->xfer_buf, fri->completion, &fri->info);
419 r = rbd_aio_read(rbd->image, io_u->offset, io_u->xfer_buflen,
420 io_u->xfer_buf, fri->completion);
424 log_err("rbd_aio_read failed.\n");
427 } else if (io_u->ddir == DDIR_TRIM) {
428 r = rbd_aio_discard(rbd->image, io_u->offset,
429 io_u->xfer_buflen, fri->completion);
431 log_err("rbd_aio_discard failed.\n");
434 } else if (io_u->ddir == DDIR_SYNC) {
435 r = rbd_aio_flush(rbd->image, fri->completion);
437 log_err("rbd_flush failed.\n");
441 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
448 rbd_aio_release(fri->completion);
451 td_verror(td, io_u->error, "xfer");
452 return FIO_Q_COMPLETED;
455 static int fio_rbd_init(struct thread_data *td)
459 r = _fio_rbd_connect(td);
461 log_err("fio_rbd_connect failed, return code: %d .\n", r);
471 static void fio_rbd_cleanup(struct thread_data *td)
473 struct rbd_data *rbd = td->io_ops_data;
476 _fio_rbd_disconnect(rbd);
477 free(rbd->aio_events);
478 free(rbd->sort_events);
483 static int fio_rbd_setup(struct thread_data *td)
485 rbd_image_info_t info;
487 struct rbd_data *rbd = NULL;
488 int major, minor, extra;
491 /* log version of librbd. No cluster connection required. */
492 rbd_version(&major, &minor, &extra);
493 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
495 /* allocate engine specific structure to deal with librbd. */
496 r = _fio_setup_rbd_data(td, &rbd);
498 log_err("fio_setup_rbd_data failed.\n");
501 td->io_ops_data = rbd;
503 /* librbd does not allow us to run first in the main thread and later
504 * in a fork child. It needs to be the same process context all the
507 td->o.use_thread = 1;
509 /* connect in the main thread to determine to determine
510 * the size of the given RADOS block device. And disconnect
513 r = _fio_rbd_connect(td);
515 log_err("fio_rbd_connect failed.\n");
519 /* get size of the RADOS block device */
520 r = rbd_stat(rbd->image, &info, sizeof(info));
522 log_err("rbd_status failed.\n");
525 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
527 /* taken from "net" engine. Pretend we deal with files,
528 * even if we do not have any ideas about files.
529 * The size of the RBD is set instead of a artificial file.
531 if (!td->files_index) {
532 add_file(td, td->o.filename ? : "rbd", 0, 0);
533 td->o.nr_files = td->o.nr_files ? : 1;
537 f->real_file_size = info.size;
539 /* disconnect, then we were only connected to determine
540 * the size of the RBD.
542 _fio_rbd_disconnect(rbd);
546 _fio_rbd_disconnect(rbd);
552 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
557 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
559 #if defined(CONFIG_RBD_INVAL)
560 struct rbd_data *rbd = td->io_ops_data;
562 return rbd_invalidate_cache(rbd->image);
568 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
570 struct fio_rbd_iou *fri = io_u->engine_data;
573 io_u->engine_data = NULL;
578 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
580 struct fio_rbd_iou *fri;
582 fri = calloc(1, sizeof(*fri));
584 io_u->engine_data = fri;
588 static struct ioengine_ops ioengine = {
590 .version = FIO_IOOPS_VERSION,
591 .setup = fio_rbd_setup,
592 .init = fio_rbd_init,
593 .queue = fio_rbd_queue,
594 .getevents = fio_rbd_getevents,
595 .event = fio_rbd_event,
596 .cleanup = fio_rbd_cleanup,
597 .open_file = fio_rbd_open,
598 .invalidate = fio_rbd_invalidate,
600 .io_u_init = fio_rbd_io_u_init,
601 .io_u_free = fio_rbd_io_u_free,
602 .option_struct_size = sizeof(struct rbd_options),
605 static void fio_init fio_rbd_register(void)
607 register_ioengine(&ioengine);
610 static void fio_exit fio_rbd_unregister(void)
612 unregister_ioengine(&ioengine);