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-process state */
25 pthread_mutex_t mutex;
27 int initted_hipri_threads;
29 } proc_state = { PTHREAD_MUTEX_INITIALIZER, 0, 0, NULL };
31 static void fio_blkio_proc_lock(void) {
33 ret = pthread_mutex_lock(&proc_state.mutex);
37 static void fio_blkio_proc_unlock(void) {
39 ret = pthread_mutex_unlock(&proc_state.mutex);
43 /* per-thread state */
44 struct fio_blkio_data {
46 int completion_fd; /* may be -1 if not FIO_BLKIO_WAIT_MODE_EVENTFD */
48 bool has_mem_region; /* whether mem_region is valid */
49 struct blkio_mem_region mem_region; /* only if allocated by libblkio */
51 struct iovec *iovecs; /* for vectored requests */
52 struct blkio_completion *completions;
55 enum fio_blkio_wait_mode {
56 FIO_BLKIO_WAIT_MODE_BLOCK,
57 FIO_BLKIO_WAIT_MODE_EVENTFD,
58 FIO_BLKIO_WAIT_MODE_LOOP,
61 struct fio_blkio_options {
62 void *pad; /* option fields must not have offset 0 */
67 char *pre_connect_props;
71 char *pre_start_props;
74 unsigned int vectored;
75 unsigned int write_zeroes_on_trim;
76 enum fio_blkio_wait_mode wait_mode;
77 unsigned int force_enable_completion_eventfd;
80 static struct fio_option options[] = {
82 .name = "libblkio_driver",
83 .lname = "libblkio driver name",
84 .type = FIO_OPT_STR_STORE,
85 .off1 = offsetof(struct fio_blkio_options, driver),
86 .help = "Name of the driver to be used by libblkio",
87 .category = FIO_OPT_C_ENGINE,
88 .group = FIO_OPT_G_LIBBLKIO,
91 .name = "libblkio_path",
92 .lname = "libblkio \"path\" property",
93 .type = FIO_OPT_STR_STORE,
94 .off1 = offsetof(struct fio_blkio_options, path),
95 .help = "Value to set the \"path\" property to",
96 .category = FIO_OPT_C_ENGINE,
97 .group = FIO_OPT_G_LIBBLKIO,
100 .name = "libblkio_pre_connect_props",
101 .lname = "Additional properties to be set before blkio_connect()",
102 .type = FIO_OPT_STR_STORE,
103 .off1 = offsetof(struct fio_blkio_options, pre_connect_props),
105 .category = FIO_OPT_C_ENGINE,
106 .group = FIO_OPT_G_LIBBLKIO,
109 .name = "libblkio_num_entries",
110 .lname = "libblkio \"num-entries\" property",
112 .off1 = offsetof(struct fio_blkio_options, num_entries),
113 .help = "Value to set the \"num-entries\" property to",
116 .category = FIO_OPT_C_ENGINE,
117 .group = FIO_OPT_G_LIBBLKIO,
120 .name = "libblkio_queue_size",
121 .lname = "libblkio \"queue-size\" property",
123 .off1 = offsetof(struct fio_blkio_options, queue_size),
124 .help = "Value to set the \"queue-size\" property to",
127 .category = FIO_OPT_C_ENGINE,
128 .group = FIO_OPT_G_LIBBLKIO,
131 .name = "libblkio_pre_start_props",
132 .lname = "Additional properties to be set before blkio_start()",
133 .type = FIO_OPT_STR_STORE,
134 .off1 = offsetof(struct fio_blkio_options, pre_start_props),
136 .category = FIO_OPT_C_ENGINE,
137 .group = FIO_OPT_G_LIBBLKIO,
141 .lname = "Use poll queues",
142 .type = FIO_OPT_STR_SET,
143 .off1 = offsetof(struct fio_blkio_options, hipri),
144 .help = "Use poll queues",
145 .category = FIO_OPT_C_ENGINE,
146 .group = FIO_OPT_G_LIBBLKIO,
149 .name = "libblkio_vectored",
150 .lname = "Use blkioq_{readv,writev}()",
151 .type = FIO_OPT_STR_SET,
152 .off1 = offsetof(struct fio_blkio_options, vectored),
153 .help = "Use blkioq_{readv,writev}() instead of blkioq_{read,write}()",
154 .category = FIO_OPT_C_ENGINE,
155 .group = FIO_OPT_G_LIBBLKIO,
158 .name = "libblkio_write_zeroes_on_trim",
159 .lname = "Use blkioq_write_zeroes() for TRIM",
160 .type = FIO_OPT_STR_SET,
161 .off1 = offsetof(struct fio_blkio_options,
162 write_zeroes_on_trim),
163 .help = "Use blkioq_write_zeroes() for TRIM instead of blkioq_discard()",
164 .category = FIO_OPT_C_ENGINE,
165 .group = FIO_OPT_G_LIBBLKIO,
168 .name = "libblkio_wait_mode",
169 .lname = "How to wait for completions",
171 .off1 = offsetof(struct fio_blkio_options, wait_mode),
172 .help = "How to wait for completions",
176 .oval = FIO_BLKIO_WAIT_MODE_BLOCK,
177 .help = "Blocking blkioq_do_io()",
180 .oval = FIO_BLKIO_WAIT_MODE_EVENTFD,
181 .help = "Blocking read() on the completion eventfd",
184 .oval = FIO_BLKIO_WAIT_MODE_LOOP,
185 .help = "Busy loop with non-blocking blkioq_do_io()",
188 .category = FIO_OPT_C_ENGINE,
189 .group = FIO_OPT_G_LIBBLKIO,
192 .name = "libblkio_force_enable_completion_eventfd",
193 .lname = "Force enable the completion eventfd, even if unused",
194 .type = FIO_OPT_STR_SET,
195 .off1 = offsetof(struct fio_blkio_options,
196 force_enable_completion_eventfd),
197 .help = "This can impact performance",
198 .category = FIO_OPT_C_ENGINE,
199 .group = FIO_OPT_G_LIBBLKIO,
206 static int fio_blkio_set_props_from_str(struct blkio *b, const char *opt_name,
209 char *new_str, *name, *value;
214 /* iteration can mutate string, so copy it */
215 new_str = strdup(str);
217 log_err("fio: strdup() failed\n");
221 /* iterate over property name-value pairs */
222 while ((name = get_next_str(&new_str))) {
223 /* split into property name and value */
224 value = strchr(name, '=');
226 log_err("fio: missing '=' in option %s\n", opt_name);
234 /* strip whitespace from property name */
235 strip_blank_front(&name);
236 strip_blank_end(name);
238 if (name[0] == '\0') {
239 log_err("fio: empty property name in option %s\n",
245 /* strip whitespace from property value */
246 strip_blank_front(&value);
247 strip_blank_end(value);
250 if (blkio_set_str(b, name, value) != 0) {
251 log_err("fio: error setting property '%s' to '%s': %s\n",
252 name, value, blkio_get_error_msg());
263 * Log the failure of a libblkio function.
265 * `(void)func` is to ensure `func` exists and prevent typos
267 #define fio_blkio_log_err(func) \
270 log_err("fio: %s() failed: %s\n", #func, \
271 blkio_get_error_msg()); \
274 static bool possibly_null_strs_equal(const char *a, const char *b)
276 return (!a && !b) || (a && b && strcmp(a, b) == 0);
280 * Returns the total number of subjobs using the 'libblkio' ioengine and setting
281 * the 'thread' option in the entire workload that have the given value for the
284 static int total_threaded_subjobs(bool hipri)
289 const struct fio_blkio_options *options = td->eo;
290 if (strcmp(td->o.ioengine, "libblkio") == 0 &&
291 td->o.use_thread && (bool)options->hipri == hipri)
301 struct fio_blkio_options opts;
302 } first_threaded_subjob = { 0 };
304 static void fio_blkio_log_opt_compat_err(const char *option_name)
306 log_err("fio: jobs using engine libblkio and sharing a process must agree on the %s option\n",
311 * If td represents a subjob with option 'thread', check if its options are
312 * compatible with those of other threaded subjobs that were already set up.
314 static int fio_blkio_check_opt_compat(struct thread_data *td)
316 const struct fio_blkio_options *options = td->eo, *prev_options;
318 if (!td->o.use_thread)
319 return 0; /* subjob doesn't use 'thread' */
321 if (!first_threaded_subjob.set_up) {
322 /* first subjob using 'thread', store options for later */
323 first_threaded_subjob.set_up = true;
324 first_threaded_subjob.direct = td->o.odirect;
325 first_threaded_subjob.opts = *options;
329 /* not first subjob using 'thread', check option compatibility */
330 prev_options = &first_threaded_subjob.opts;
332 if (td->o.odirect != first_threaded_subjob.direct) {
333 fio_blkio_log_opt_compat_err("direct/buffered");
337 if (strcmp(options->driver, prev_options->driver) != 0) {
338 fio_blkio_log_opt_compat_err("libblkio_driver");
342 if (!possibly_null_strs_equal(options->path, prev_options->path)) {
343 fio_blkio_log_opt_compat_err("libblkio_path");
347 if (!possibly_null_strs_equal(options->pre_connect_props,
348 prev_options->pre_connect_props)) {
349 fio_blkio_log_opt_compat_err("libblkio_pre_connect_props");
353 if (options->num_entries != prev_options->num_entries) {
354 fio_blkio_log_opt_compat_err("libblkio_num_entries");
358 if (options->queue_size != prev_options->queue_size) {
359 fio_blkio_log_opt_compat_err("libblkio_queue_size");
363 if (!possibly_null_strs_equal(options->pre_start_props,
364 prev_options->pre_start_props)) {
365 fio_blkio_log_opt_compat_err("libblkio_pre_start_props");
372 static int fio_blkio_create_and_connect(struct thread_data *td,
373 struct blkio **out_blkio)
375 const struct fio_blkio_options *options = td->eo;
379 if (!options->driver) {
380 log_err("fio: engine libblkio requires option libblkio_driver to be set\n");
384 if (blkio_create(options->driver, &b) != 0) {
385 fio_blkio_log_err(blkio_create);
389 /* don't fail if driver doesn't have a "direct" property */
390 ret = blkio_set_bool(b, "direct", td->o.odirect);
391 if (ret != 0 && ret != -ENOENT) {
392 fio_blkio_log_err(blkio_set_bool);
393 goto err_blkio_destroy;
396 if (blkio_set_bool(b, "read-only", read_only) != 0) {
397 fio_blkio_log_err(blkio_set_bool);
398 goto err_blkio_destroy;
402 if (blkio_set_str(b, "path", options->path) != 0) {
403 fio_blkio_log_err(blkio_set_str);
404 goto err_blkio_destroy;
408 if (fio_blkio_set_props_from_str(b, "libblkio_pre_connect_props",
409 options->pre_connect_props) != 0)
410 goto err_blkio_destroy;
412 if (blkio_connect(b) != 0) {
413 fio_blkio_log_err(blkio_connect);
414 goto err_blkio_destroy;
417 if (options->num_entries != 0) {
418 if (blkio_set_int(b, "num-entries",
419 options->num_entries) != 0) {
420 fio_blkio_log_err(blkio_set_int);
421 goto err_blkio_destroy;
425 if (options->queue_size != 0) {
426 if (blkio_set_int(b, "queue-size", options->queue_size) != 0) {
427 fio_blkio_log_err(blkio_set_int);
428 goto err_blkio_destroy;
432 if (fio_blkio_set_props_from_str(b, "libblkio_pre_start_props",
433 options->pre_start_props) != 0)
434 goto err_blkio_destroy;
444 static bool incompatible_threaded_subjob_options = false;
447 * This callback determines the device/file size, so it creates and connects a
448 * blkio instance. But it is invoked from the main thread in the original fio
449 * process, not from the processes in which jobs will actually run. It thus
450 * subsequently destroys the blkio, which is recreated in the init() callback.
452 static int fio_blkio_setup(struct thread_data *td)
454 const struct fio_blkio_options *options = td->eo;
459 assert(td->files_index == 1);
461 if (fio_blkio_check_opt_compat(td) != 0) {
462 incompatible_threaded_subjob_options = true;
466 if (options->hipri &&
467 options->wait_mode == FIO_BLKIO_WAIT_MODE_EVENTFD) {
468 log_err("fio: option hipri is incompatible with option libblkio_wait_mode=eventfd\n");
472 if (options->hipri && options->force_enable_completion_eventfd) {
473 log_err("fio: option hipri is incompatible with option libblkio_force_enable_completion_eventfd\n");
477 if (fio_blkio_create_and_connect(td, &b) != 0)
480 if (blkio_get_uint64(b, "capacity", &capacity) != 0) {
481 fio_blkio_log_err(blkio_get_uint64);
483 goto out_blkio_destroy;
486 td->files[0]->real_file_size = capacity;
487 fio_file_set_size_known(td->files[0]);
494 static int fio_blkio_init(struct thread_data *td)
496 const struct fio_blkio_options *options = td->eo;
497 struct fio_blkio_data *data;
500 if (td->o.use_thread && incompatible_threaded_subjob_options) {
502 * Different subjobs using option 'thread' specified
503 * incompatible options. We don't know which configuration
504 * should win, so we just fail all such subjobs.
510 * Request enqueueing is fast, and it's not possible to know exactly
511 * when a request is submitted, so never report submission latencies.
513 td->o.disable_slat = 1;
515 data = calloc(1, sizeof(*data));
517 log_err("fio: calloc() failed\n");
521 data->iovecs = calloc(td->o.iodepth, sizeof(data->iovecs[0]));
522 data->completions = calloc(td->o.iodepth, sizeof(data->completions[0]));
523 if (!data->iovecs || !data->completions) {
524 log_err("fio: calloc() failed\n");
528 fio_blkio_proc_lock();
530 if (proc_state.initted_threads == 0) {
531 /* initialize per-process blkio */
532 int num_queues, num_poll_queues;
534 if (td->o.use_thread) {
535 num_queues = total_threaded_subjobs(false);
536 num_poll_queues = total_threaded_subjobs(true);
538 num_queues = options->hipri ? 0 : 1;
539 num_poll_queues = options->hipri ? 1 : 0;
542 if (fio_blkio_create_and_connect(td, &proc_state.b) != 0)
545 if (blkio_set_int(proc_state.b, "num-queues",
547 fio_blkio_log_err(blkio_set_int);
548 goto err_blkio_destroy;
551 if (blkio_set_int(proc_state.b, "num-poll-queues",
552 num_poll_queues) != 0) {
553 fio_blkio_log_err(blkio_set_int);
554 goto err_blkio_destroy;
557 if (blkio_start(proc_state.b) != 0) {
558 fio_blkio_log_err(blkio_start);
559 goto err_blkio_destroy;
563 if (options->hipri) {
564 int i = proc_state.initted_hipri_threads;
565 data->q = blkio_get_poll_queue(proc_state.b, i);
567 int i = proc_state.initted_threads -
568 proc_state.initted_hipri_threads;
569 data->q = blkio_get_queue(proc_state.b, i);
572 if (options->wait_mode == FIO_BLKIO_WAIT_MODE_EVENTFD ||
573 options->force_enable_completion_eventfd) {
574 /* enable completion fd and make it blocking */
575 blkioq_set_completion_fd_enabled(data->q, true);
576 data->completion_fd = blkioq_get_completion_fd(data->q);
578 flags = fcntl(data->completion_fd, F_GETFL);
580 log_err("fio: fcntl(F_GETFL) failed: %s\n",
582 goto err_blkio_destroy;
585 if (fcntl(data->completion_fd, F_SETFL,
586 flags & ~O_NONBLOCK) != 0) {
587 log_err("fio: fcntl(F_SETFL) failed: %s\n",
589 goto err_blkio_destroy;
592 data->completion_fd = -1;
595 ++proc_state.initted_threads;
597 ++proc_state.initted_hipri_threads;
599 /* Set data last so cleanup() does nothing if init() fails. */
600 td->io_ops_data = data;
602 fio_blkio_proc_unlock();
607 if (proc_state.initted_threads == 0)
608 blkio_destroy(&proc_state.b);
610 if (proc_state.initted_threads == 0)
612 fio_blkio_proc_unlock();
614 free(data->completions);
620 static int fio_blkio_post_init(struct thread_data *td)
622 struct fio_blkio_data *data = td->io_ops_data;
624 if (!data->has_mem_region) {
626 * Memory was allocated by the fio core and not iomem_alloc(),
627 * so we need to register it as a memory region here.
629 * `td->orig_buffer_size` is computed like `len` below, but then
630 * fio can add some padding to it to make sure it is
631 * sufficiently aligned to the page size and the mem_align
632 * option. However, this can make it become unaligned to the
633 * "mem-region-alignment" property in ways that the user can't
634 * control, so we essentially recompute `td->orig_buffer_size`
635 * here but without adding that padding.
638 unsigned long long max_block_size;
639 struct blkio_mem_region region;
641 max_block_size = max(td->o.max_bs[DDIR_READ],
642 max(td->o.max_bs[DDIR_WRITE],
643 td->o.max_bs[DDIR_TRIM]));
645 region = (struct blkio_mem_region) {
646 .addr = td->orig_buffer,
647 .len = (size_t)max_block_size *
648 (size_t)td->o.iodepth,
652 if (blkio_map_mem_region(proc_state.b, ®ion) != 0) {
653 fio_blkio_log_err(blkio_map_mem_region);
661 static void fio_blkio_cleanup(struct thread_data *td)
663 struct fio_blkio_data *data = td->io_ops_data;
666 * Subjobs from different jobs can be terminated at different times, so
667 * this callback may be invoked for one subjob while another is still
668 * doing I/O. Those subjobs may share the process, so we must wait until
669 * the last subjob in the process wants to clean up to actually destroy
674 free(data->completions);
678 fio_blkio_proc_lock();
679 if (--proc_state.initted_threads == 0) {
680 blkio_destroy(&proc_state.b);
683 fio_blkio_proc_unlock();
687 #define align_up(x, y) ((((x) + (y) - 1) / (y)) * (y))
689 static int fio_blkio_iomem_alloc(struct thread_data *td, size_t size)
691 struct fio_blkio_data *data = td->io_ops_data;
693 uint64_t mem_region_alignment;
695 if (blkio_get_uint64(proc_state.b, "mem-region-alignment",
696 &mem_region_alignment) != 0) {
697 fio_blkio_log_err(blkio_get_uint64);
701 /* round up size to satisfy mem-region-alignment */
702 size = align_up(size, (size_t)mem_region_alignment);
704 fio_blkio_proc_lock();
706 if (blkio_alloc_mem_region(proc_state.b, &data->mem_region,
708 fio_blkio_log_err(blkio_alloc_mem_region);
713 if (blkio_map_mem_region(proc_state.b, &data->mem_region) != 0) {
714 fio_blkio_log_err(blkio_map_mem_region);
719 td->orig_buffer = data->mem_region.addr;
720 data->has_mem_region = true;
726 blkio_free_mem_region(proc_state.b, &data->mem_region);
728 fio_blkio_proc_unlock();
732 static void fio_blkio_iomem_free(struct thread_data *td)
734 struct fio_blkio_data *data = td->io_ops_data;
736 if (data && data->has_mem_region) {
737 fio_blkio_proc_lock();
738 blkio_unmap_mem_region(proc_state.b, &data->mem_region);
739 blkio_free_mem_region(proc_state.b, &data->mem_region);
740 fio_blkio_proc_unlock();
742 data->has_mem_region = false;
746 static int fio_blkio_open_file(struct thread_data *td, struct fio_file *f)
751 static enum fio_q_status fio_blkio_queue(struct thread_data *td,
754 const struct fio_blkio_options *options = td->eo;
755 struct fio_blkio_data *data = td->io_ops_data;
757 fio_ro_check(td, io_u);
759 switch (io_u->ddir) {
761 if (options->vectored) {
762 struct iovec *iov = &data->iovecs[io_u->index];
763 iov->iov_base = io_u->xfer_buf;
764 iov->iov_len = (size_t)io_u->xfer_buflen;
766 blkioq_readv(data->q, io_u->offset, iov, 1,
769 blkioq_read(data->q, io_u->offset,
771 (size_t)io_u->xfer_buflen, io_u, 0);
775 if (options->vectored) {
776 struct iovec *iov = &data->iovecs[io_u->index];
777 iov->iov_base = io_u->xfer_buf;
778 iov->iov_len = (size_t)io_u->xfer_buflen;
780 blkioq_writev(data->q, io_u->offset, iov, 1,
783 blkioq_write(data->q, io_u->offset,
785 (size_t)io_u->xfer_buflen, io_u,
790 if (options->write_zeroes_on_trim) {
791 blkioq_write_zeroes(data->q, io_u->offset,
792 io_u->xfer_buflen, io_u, 0);
794 blkioq_discard(data->q, io_u->offset,
795 io_u->xfer_buflen, io_u, 0);
800 blkioq_flush(data->q, io_u, 0);
803 io_u->error = ENOTSUP;
804 io_u_log_error(td, io_u);
805 return FIO_Q_COMPLETED;
811 static int fio_blkio_getevents(struct thread_data *td, unsigned int min,
812 unsigned int max, const struct timespec *t)
814 const struct fio_blkio_options *options = td->eo;
815 struct fio_blkio_data *data = td->io_ops_data;
819 switch (options->wait_mode) {
820 case FIO_BLKIO_WAIT_MODE_BLOCK:
821 n = blkioq_do_io(data->q, data->completions, (int)min, (int)max,
824 fio_blkio_log_err(blkioq_do_io);
828 case FIO_BLKIO_WAIT_MODE_EVENTFD:
829 n = blkioq_do_io(data->q, data->completions, 0, (int)max, NULL);
831 fio_blkio_log_err(blkioq_do_io);
834 while (n < (int)min) {
835 ret = read(data->completion_fd, &event, sizeof(event));
836 if (ret != sizeof(event)) {
837 log_err("fio: read() on the completion fd returned %d\n",
842 ret = blkioq_do_io(data->q, data->completions + n, 0,
845 fio_blkio_log_err(blkioq_do_io);
852 case FIO_BLKIO_WAIT_MODE_LOOP:
853 for (n = 0; n < (int)min; ) {
854 ret = blkioq_do_io(data->q, data->completions + n, 0,
857 fio_blkio_log_err(blkioq_do_io);
869 static struct io_u *fio_blkio_event(struct thread_data *td, int event)
871 struct fio_blkio_data *data = td->io_ops_data;
872 struct blkio_completion *completion = &data->completions[event];
873 struct io_u *io_u = completion->user_data;
875 io_u->error = -completion->ret;
880 FIO_STATIC struct ioengine_ops ioengine = {
882 .version = FIO_IOOPS_VERSION,
883 .flags = FIO_DISKLESSIO | FIO_NOEXTEND |
884 FIO_NO_OFFLOAD | FIO_SKIPPABLE_IOMEM_ALLOC,
886 .setup = fio_blkio_setup,
887 .init = fio_blkio_init,
888 .post_init = fio_blkio_post_init,
889 .cleanup = fio_blkio_cleanup,
891 .iomem_alloc = fio_blkio_iomem_alloc,
892 .iomem_free = fio_blkio_iomem_free,
894 .open_file = fio_blkio_open_file,
896 .queue = fio_blkio_queue,
897 .getevents = fio_blkio_getevents,
898 .event = fio_blkio_event,
901 .option_struct_size = sizeof(struct fio_blkio_options),
904 static void fio_init fio_blkio_register(void)
906 register_ioengine(&ioengine);
909 static void fio_exit fio_blkio_unregister(void)
911 unregister_ioengine(&ioengine);