4 * IO engine using libblkio to access various block I/O interfaces:
5 * https://gitlab.com/libblkio/libblkio
19 #include "../optgroup.h"
20 #include "../options.h"
23 /* per-thread state */
24 struct fio_blkio_data {
27 int completion_fd; /* -1 if not FIO_BLKIO_WAIT_MODE_EVENTFD */
29 bool has_mem_region; /* whether mem_region is valid */
30 struct blkio_mem_region mem_region; /* only if allocated by libblkio */
32 struct iovec *iovecs; /* for vectored requests */
33 struct blkio_completion *completions;
36 enum fio_blkio_wait_mode {
37 FIO_BLKIO_WAIT_MODE_BLOCK,
38 FIO_BLKIO_WAIT_MODE_EVENTFD,
39 FIO_BLKIO_WAIT_MODE_LOOP,
42 struct fio_blkio_options {
43 void *pad; /* option fields must not have offset 0 */
46 char *pre_connect_props;
47 char *pre_start_props;
50 unsigned int vectored;
51 unsigned int write_zeroes_on_trim;
52 enum fio_blkio_wait_mode wait_mode;
55 static struct fio_option options[] = {
57 .name = "libblkio_driver",
58 .lname = "libblkio driver name",
59 .type = FIO_OPT_STR_STORE,
60 .off1 = offsetof(struct fio_blkio_options, driver),
61 .help = "Name of the driver to be used by libblkio",
62 .category = FIO_OPT_C_ENGINE,
63 .group = FIO_OPT_G_LIBBLKIO,
66 .name = "libblkio_pre_connect_props",
67 .lname = "Properties to be set before blkio_connect()",
68 .type = FIO_OPT_STR_STORE,
69 .off1 = offsetof(struct fio_blkio_options, pre_connect_props),
71 .category = FIO_OPT_C_ENGINE,
72 .group = FIO_OPT_G_LIBBLKIO,
75 .name = "libblkio_pre_start_props",
76 .lname = "Properties to be set before blkio_start()",
77 .type = FIO_OPT_STR_STORE,
78 .off1 = offsetof(struct fio_blkio_options, pre_start_props),
80 .category = FIO_OPT_C_ENGINE,
81 .group = FIO_OPT_G_LIBBLKIO,
85 .lname = "Use poll queues",
86 .type = FIO_OPT_STR_SET,
87 .off1 = offsetof(struct fio_blkio_options, hipri),
88 .help = "Use poll queues",
89 .category = FIO_OPT_C_ENGINE,
90 .group = FIO_OPT_G_LIBBLKIO,
93 .name = "libblkio_vectored",
94 .lname = "Use blkioq_{readv,writev}()",
95 .type = FIO_OPT_STR_SET,
96 .off1 = offsetof(struct fio_blkio_options, vectored),
97 .help = "Use blkioq_{readv,writev}() instead of blkioq_{read,write}()",
98 .category = FIO_OPT_C_ENGINE,
99 .group = FIO_OPT_G_LIBBLKIO,
102 .name = "libblkio_write_zeroes_on_trim",
103 .lname = "Use blkioq_write_zeroes() for TRIM",
104 .type = FIO_OPT_STR_SET,
105 .off1 = offsetof(struct fio_blkio_options,
106 write_zeroes_on_trim),
107 .help = "Use blkioq_write_zeroes() for TRIM instead of blkioq_discard()",
108 .category = FIO_OPT_C_ENGINE,
109 .group = FIO_OPT_G_LIBBLKIO,
112 .name = "libblkio_wait_mode",
113 .lname = "How to wait for completions",
115 .off1 = offsetof(struct fio_blkio_options, wait_mode),
116 .help = "How to wait for completions",
120 .oval = FIO_BLKIO_WAIT_MODE_BLOCK,
121 .help = "Blocking blkioq_do_io()",
124 .oval = FIO_BLKIO_WAIT_MODE_EVENTFD,
125 .help = "Blocking read() on the completion eventfd",
128 .oval = FIO_BLKIO_WAIT_MODE_LOOP,
129 .help = "Busy loop with non-blocking blkioq_do_io()",
132 .category = FIO_OPT_C_ENGINE,
133 .group = FIO_OPT_G_LIBBLKIO,
140 static int fio_blkio_set_props_from_str(struct blkio *b, const char *opt_name,
143 char *new_str, *name, *value;
148 /* iteration can mutate string, so copy it */
149 new_str = strdup(str);
151 log_err("fio: strdup() failed\n");
155 /* iterate over property name-value pairs */
156 while ((name = get_next_str(&new_str))) {
157 /* split into property name and value */
158 value = strchr(name, '=');
160 log_err("fio: missing '=' in option %s\n", opt_name);
168 /* strip whitespace from property name */
169 strip_blank_front(&name);
170 strip_blank_end(name);
172 if (name[0] == '\0') {
173 log_err("fio: empty property name in option %s\n",
179 /* strip whitespace from property value */
180 strip_blank_front(&value);
181 strip_blank_end(value);
184 if (blkio_set_str(b, name, value) != 0) {
185 log_err("fio: error setting property '%s' to '%s': %s\n",
186 name, value, blkio_get_error_msg());
197 * Log the failure of a libblkio function.
199 * `(void)func` is to ensure `func` exists and prevent typos
201 #define fio_blkio_log_err(func) \
204 log_err("fio: %s() failed: %s\n", #func, \
205 blkio_get_error_msg()); \
208 static int fio_blkio_create_and_connect(struct thread_data *td,
209 struct blkio **out_blkio)
211 const struct fio_blkio_options *options = td->eo;
215 if (!options->driver) {
216 log_err("fio: engine libblkio requires option libblkio_driver to be set\n");
220 if (blkio_create(options->driver, &b) != 0) {
221 fio_blkio_log_err(blkio_create);
225 /* don't fail if driver doesn't have a "direct" property */
226 ret = blkio_set_bool(b, "direct", td->o.odirect);
227 if (ret != 0 && ret != -ENOENT) {
228 fio_blkio_log_err(blkio_set_bool);
229 goto err_blkio_destroy;
232 if (blkio_set_bool(b, "read-only", read_only) != 0) {
233 fio_blkio_log_err(blkio_set_bool);
234 goto err_blkio_destroy;
237 if (fio_blkio_set_props_from_str(b, "libblkio_pre_connect_props",
238 options->pre_connect_props) != 0)
239 goto err_blkio_destroy;
241 if (blkio_connect(b) != 0) {
242 fio_blkio_log_err(blkio_connect);
243 goto err_blkio_destroy;
246 if (fio_blkio_set_props_from_str(b, "libblkio_pre_start_props",
247 options->pre_start_props) != 0)
248 goto err_blkio_destroy;
259 * This callback determines the device/file size, so it creates and connects a
260 * blkio instance. But it is invoked from the main thread in the original fio
261 * process, not from the processes in which jobs will actually run. It thus
262 * subsequently destroys the blkio, which is recreated in the init() callback.
264 static int fio_blkio_setup(struct thread_data *td)
266 const struct fio_blkio_options *options = td->eo;
271 assert(td->files_index == 1);
273 if (options->hipri &&
274 options->wait_mode == FIO_BLKIO_WAIT_MODE_EVENTFD) {
275 log_err("fio: option hipri is incompatible with option libblkio_wait_mode=eventfd\n");
279 if (fio_blkio_create_and_connect(td, &b) != 0)
282 if (blkio_get_uint64(b, "capacity", &capacity) != 0) {
283 fio_blkio_log_err(blkio_get_uint64);
285 goto out_blkio_destroy;
288 td->files[0]->real_file_size = capacity;
289 fio_file_set_size_known(td->files[0]);
296 static int fio_blkio_init(struct thread_data *td)
298 const struct fio_blkio_options *options = td->eo;
299 struct fio_blkio_data *data;
303 * Request enqueueing is fast, and it's not possible to know exactly
304 * when a request is submitted, so never report submission latencies.
306 td->o.disable_slat = 1;
308 data = calloc(1, sizeof(*data));
310 log_err("fio: calloc() failed\n");
314 data->iovecs = calloc(td->o.iodepth, sizeof(data->iovecs[0]));
315 data->completions = calloc(td->o.iodepth, sizeof(data->completions[0]));
316 if (!data->iovecs || !data->completions) {
317 log_err("fio: calloc() failed\n");
321 if (fio_blkio_create_and_connect(td, &data->b) != 0)
324 if (blkio_set_int(data->b, "num-queues", options->hipri ? 0 : 1) != 0) {
325 fio_blkio_log_err(blkio_set_int);
326 goto err_blkio_destroy;
329 if (blkio_set_int(data->b, "num-poll-queues",
330 options->hipri ? 1 : 0) != 0) {
331 fio_blkio_log_err(blkio_set_int);
332 goto err_blkio_destroy;
335 if (blkio_start(data->b) != 0) {
336 fio_blkio_log_err(blkio_start);
337 goto err_blkio_destroy;
341 data->q = blkio_get_poll_queue(data->b, 0);
343 data->q = blkio_get_queue(data->b, 0);
345 if (options->wait_mode == FIO_BLKIO_WAIT_MODE_EVENTFD) {
346 /* enable completion fd and make it blocking */
347 blkioq_set_completion_fd_enabled(data->q, true);
348 data->completion_fd = blkioq_get_completion_fd(data->q);
350 flags = fcntl(data->completion_fd, F_GETFL);
352 log_err("fio: fcntl(F_GETFL) failed: %s\n",
354 goto err_blkio_destroy;
357 if (fcntl(data->completion_fd, F_SETFL,
358 flags & ~O_NONBLOCK) != 0) {
359 log_err("fio: fcntl(F_SETFL) failed: %s\n",
361 goto err_blkio_destroy;
364 data->completion_fd = -1;
367 /* Set data last so cleanup() does nothing if init() fails. */
368 td->io_ops_data = data;
373 blkio_destroy(&data->b);
375 free(data->completions);
381 static int fio_blkio_post_init(struct thread_data *td)
383 struct fio_blkio_data *data = td->io_ops_data;
385 if (!data->has_mem_region) {
387 * Memory was allocated by the fio core and not iomem_alloc(),
388 * so we need to register it as a memory region here.
390 * `td->orig_buffer_size` is computed like `len` below, but then
391 * fio can add some padding to it to make sure it is
392 * sufficiently aligned to the page size and the mem_align
393 * option. However, this can make it become unaligned to the
394 * "mem-region-alignment" property in ways that the user can't
395 * control, so we essentially recompute `td->orig_buffer_size`
396 * here but without adding that padding.
399 unsigned long long max_block_size;
400 struct blkio_mem_region region;
402 max_block_size = max(td->o.max_bs[DDIR_READ],
403 max(td->o.max_bs[DDIR_WRITE],
404 td->o.max_bs[DDIR_TRIM]));
406 region = (struct blkio_mem_region) {
407 .addr = td->orig_buffer,
408 .len = (size_t)max_block_size *
409 (size_t)td->o.iodepth,
413 if (blkio_map_mem_region(data->b, ®ion) != 0) {
414 fio_blkio_log_err(blkio_map_mem_region);
422 static void fio_blkio_cleanup(struct thread_data *td)
424 struct fio_blkio_data *data = td->io_ops_data;
427 blkio_destroy(&data->b);
428 free(data->completions);
434 #define align_up(x, y) ((((x) + (y) - 1) / (y)) * (y))
436 static int fio_blkio_iomem_alloc(struct thread_data *td, size_t size)
438 struct fio_blkio_data *data = td->io_ops_data;
440 uint64_t mem_region_alignment;
442 if (blkio_get_uint64(data->b, "mem-region-alignment",
443 &mem_region_alignment) != 0) {
444 fio_blkio_log_err(blkio_get_uint64);
448 /* round up size to satisfy mem-region-alignment */
449 size = align_up(size, (size_t)mem_region_alignment);
451 if (blkio_alloc_mem_region(data->b, &data->mem_region, size) != 0) {
452 fio_blkio_log_err(blkio_alloc_mem_region);
457 if (blkio_map_mem_region(data->b, &data->mem_region) != 0) {
458 fio_blkio_log_err(blkio_map_mem_region);
463 td->orig_buffer = data->mem_region.addr;
464 data->has_mem_region = true;
470 blkio_free_mem_region(data->b, &data->mem_region);
475 static void fio_blkio_iomem_free(struct thread_data *td)
477 struct fio_blkio_data *data = td->io_ops_data;
479 if (data && data->has_mem_region) {
480 blkio_unmap_mem_region(data->b, &data->mem_region);
481 blkio_free_mem_region(data->b, &data->mem_region);
483 data->has_mem_region = false;
487 static int fio_blkio_open_file(struct thread_data *td, struct fio_file *f)
492 static enum fio_q_status fio_blkio_queue(struct thread_data *td,
495 const struct fio_blkio_options *options = td->eo;
496 struct fio_blkio_data *data = td->io_ops_data;
498 fio_ro_check(td, io_u);
500 switch (io_u->ddir) {
502 if (options->vectored) {
503 struct iovec *iov = &data->iovecs[io_u->index];
504 iov->iov_base = io_u->xfer_buf;
505 iov->iov_len = (size_t)io_u->xfer_buflen;
507 blkioq_readv(data->q, io_u->offset, iov, 1,
510 blkioq_read(data->q, io_u->offset,
512 (size_t)io_u->xfer_buflen, io_u, 0);
516 if (options->vectored) {
517 struct iovec *iov = &data->iovecs[io_u->index];
518 iov->iov_base = io_u->xfer_buf;
519 iov->iov_len = (size_t)io_u->xfer_buflen;
521 blkioq_writev(data->q, io_u->offset, iov, 1,
524 blkioq_write(data->q, io_u->offset,
526 (size_t)io_u->xfer_buflen, io_u,
531 if (options->write_zeroes_on_trim) {
532 blkioq_write_zeroes(data->q, io_u->offset,
533 io_u->xfer_buflen, io_u, 0);
535 blkioq_discard(data->q, io_u->offset,
536 io_u->xfer_buflen, io_u, 0);
541 blkioq_flush(data->q, io_u, 0);
544 io_u->error = ENOTSUP;
545 io_u_log_error(td, io_u);
546 return FIO_Q_COMPLETED;
552 static int fio_blkio_getevents(struct thread_data *td, unsigned int min,
553 unsigned int max, const struct timespec *t)
555 const struct fio_blkio_options *options = td->eo;
556 struct fio_blkio_data *data = td->io_ops_data;
560 switch (options->wait_mode) {
561 case FIO_BLKIO_WAIT_MODE_BLOCK:
562 n = blkioq_do_io(data->q, data->completions, (int)min, (int)max,
565 fio_blkio_log_err(blkioq_do_io);
569 case FIO_BLKIO_WAIT_MODE_EVENTFD:
570 n = blkioq_do_io(data->q, data->completions, 0, (int)max, NULL);
572 fio_blkio_log_err(blkioq_do_io);
575 while (n < (int)min) {
576 ret = read(data->completion_fd, &event, sizeof(event));
577 if (ret != sizeof(event)) {
578 log_err("fio: read() on the completion fd returned %d\n",
583 ret = blkioq_do_io(data->q, data->completions + n, 0,
586 fio_blkio_log_err(blkioq_do_io);
593 case FIO_BLKIO_WAIT_MODE_LOOP:
594 for (n = 0; n < (int)min; ) {
595 ret = blkioq_do_io(data->q, data->completions + n, 0,
598 fio_blkio_log_err(blkioq_do_io);
610 static struct io_u *fio_blkio_event(struct thread_data *td, int event)
612 struct fio_blkio_data *data = td->io_ops_data;
613 struct blkio_completion *completion = &data->completions[event];
614 struct io_u *io_u = completion->user_data;
616 io_u->error = -completion->ret;
621 FIO_STATIC struct ioengine_ops ioengine = {
623 .version = FIO_IOOPS_VERSION,
624 .flags = FIO_DISKLESSIO | FIO_NOEXTEND |
625 FIO_NO_OFFLOAD | FIO_SKIPPABLE_IOMEM_ALLOC,
627 .setup = fio_blkio_setup,
628 .init = fio_blkio_init,
629 .post_init = fio_blkio_post_init,
630 .cleanup = fio_blkio_cleanup,
632 .iomem_alloc = fio_blkio_iomem_alloc,
633 .iomem_free = fio_blkio_iomem_free,
635 .open_file = fio_blkio_open_file,
637 .queue = fio_blkio_queue,
638 .getevents = fio_blkio_getevents,
639 .event = fio_blkio_event,
642 .option_struct_size = sizeof(struct fio_blkio_options),
645 static void fio_init fio_blkio_register(void)
647 register_ioengine(&ioengine);
650 static void fio_exit fio_blkio_unregister(void)
652 unregister_ioengine(&ioengine);