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 {
28 bool has_mem_region; /* whether mem_region is valid */
29 struct blkio_mem_region mem_region;
31 struct blkio_completion *completions;
34 struct fio_blkio_options {
35 void *pad; /* option fields must not have offset 0 */
38 char *pre_connect_props;
39 char *pre_start_props;
42 static struct fio_option options[] = {
44 .name = "libblkio_driver",
45 .lname = "libblkio driver name",
46 .type = FIO_OPT_STR_STORE,
47 .off1 = offsetof(struct fio_blkio_options, driver),
48 .help = "Name of the driver to be used by libblkio",
49 .category = FIO_OPT_C_ENGINE,
50 .group = FIO_OPT_G_LIBBLKIO,
53 .name = "libblkio_pre_connect_props",
54 .lname = "Properties to be set before blkio_connect()",
55 .type = FIO_OPT_STR_STORE,
56 .off1 = offsetof(struct fio_blkio_options, pre_connect_props),
58 .category = FIO_OPT_C_ENGINE,
59 .group = FIO_OPT_G_LIBBLKIO,
62 .name = "libblkio_pre_start_props",
63 .lname = "Properties to be set before blkio_start()",
64 .type = FIO_OPT_STR_STORE,
65 .off1 = offsetof(struct fio_blkio_options, pre_start_props),
67 .category = FIO_OPT_C_ENGINE,
68 .group = FIO_OPT_G_LIBBLKIO,
75 static int fio_blkio_set_props_from_str(struct blkio *b, const char *opt_name,
78 char *new_str, *name, *value;
83 /* iteration can mutate string, so copy it */
84 new_str = strdup(str);
86 log_err("fio: strdup() failed\n");
90 /* iterate over property name-value pairs */
91 while ((name = get_next_str(&new_str))) {
92 /* split into property name and value */
93 value = strchr(name, '=');
95 log_err("fio: missing '=' in option %s\n", opt_name);
103 /* strip whitespace from property name */
104 strip_blank_front(&name);
105 strip_blank_end(name);
107 if (name[0] == '\0') {
108 log_err("fio: empty property name in option %s\n",
114 /* strip whitespace from property value */
115 strip_blank_front(&value);
116 strip_blank_end(value);
119 if (blkio_set_str(b, name, value) != 0) {
120 log_err("fio: error setting property '%s' to '%s': %s\n",
121 name, value, blkio_get_error_msg());
132 * Log the failure of a libblkio function.
134 * `(void)func` is to ensure `func` exists and prevent typos
136 #define fio_blkio_log_err(func) \
139 log_err("fio: %s() failed: %s\n", #func, \
140 blkio_get_error_msg()); \
143 static int fio_blkio_create_and_connect(struct thread_data *td,
144 struct blkio **out_blkio)
146 const struct fio_blkio_options *options = td->eo;
150 if (!options->driver) {
151 log_err("fio: engine libblkio requires option libblkio_driver to be set\n");
155 if (blkio_create(options->driver, &b) != 0) {
156 fio_blkio_log_err(blkio_create);
160 /* don't fail if driver doesn't have a "direct" property */
161 ret = blkio_set_bool(b, "direct", td->o.odirect);
162 if (ret != 0 && ret != -ENOENT) {
163 fio_blkio_log_err(blkio_set_bool);
164 goto err_blkio_destroy;
167 if (blkio_set_bool(b, "read-only", read_only) != 0) {
168 fio_blkio_log_err(blkio_set_bool);
169 goto err_blkio_destroy;
172 if (fio_blkio_set_props_from_str(b, "libblkio_pre_connect_props",
173 options->pre_connect_props) != 0)
174 goto err_blkio_destroy;
176 if (blkio_connect(b) != 0) {
177 fio_blkio_log_err(blkio_connect);
178 goto err_blkio_destroy;
181 if (fio_blkio_set_props_from_str(b, "libblkio_pre_start_props",
182 options->pre_start_props) != 0)
183 goto err_blkio_destroy;
194 * This callback determines the device/file size, so it creates and connects a
195 * blkio instance. But it is invoked from the main thread in the original fio
196 * process, not from the processes in which jobs will actually run. It thus
197 * subsequently destroys the blkio, which is recreated in the init() callback.
199 static int fio_blkio_setup(struct thread_data *td)
205 assert(td->files_index == 1);
207 if (fio_blkio_create_and_connect(td, &b) != 0)
210 if (blkio_get_uint64(b, "capacity", &capacity) != 0) {
211 fio_blkio_log_err(blkio_get_uint64);
213 goto out_blkio_destroy;
216 td->files[0]->real_file_size = capacity;
217 fio_file_set_size_known(td->files[0]);
224 static int fio_blkio_init(struct thread_data *td)
226 struct fio_blkio_data *data;
229 * Request enqueueing is fast, and it's not possible to know exactly
230 * when a request is submitted, so never report submission latencies.
232 td->o.disable_slat = 1;
234 data = calloc(1, sizeof(*data));
236 log_err("fio: calloc() failed\n");
240 data->completions = calloc(td->o.iodepth, sizeof(data->completions[0]));
241 if (!data->completions) {
242 log_err("fio: calloc() failed\n");
246 if (fio_blkio_create_and_connect(td, &data->b) != 0)
249 if (blkio_set_int(data->b, "num-queues", 1) != 0) {
250 fio_blkio_log_err(blkio_set_int);
251 goto err_blkio_destroy;
254 if (blkio_start(data->b) != 0) {
255 fio_blkio_log_err(blkio_start);
256 goto err_blkio_destroy;
259 data->q = blkio_get_queue(data->b, 0);
261 /* Set data last so cleanup() does nothing if init() fails. */
262 td->io_ops_data = data;
267 blkio_destroy(&data->b);
269 free(data->completions);
274 static void fio_blkio_cleanup(struct thread_data *td)
276 struct fio_blkio_data *data = td->io_ops_data;
279 blkio_destroy(&data->b);
280 free(data->completions);
285 #define align_up(x, y) ((((x) + (y) - 1) / (y)) * (y))
287 static int fio_blkio_iomem_alloc(struct thread_data *td, size_t size)
289 struct fio_blkio_data *data = td->io_ops_data;
291 uint64_t mem_region_alignment;
293 if (blkio_get_uint64(data->b, "mem-region-alignment",
294 &mem_region_alignment) != 0) {
295 fio_blkio_log_err(blkio_get_uint64);
299 /* round up size to satisfy mem-region-alignment */
300 size = align_up(size, (size_t)mem_region_alignment);
302 if (blkio_alloc_mem_region(data->b, &data->mem_region, size) != 0) {
303 fio_blkio_log_err(blkio_alloc_mem_region);
308 if (blkio_map_mem_region(data->b, &data->mem_region) != 0) {
309 fio_blkio_log_err(blkio_map_mem_region);
314 td->orig_buffer = data->mem_region.addr;
315 data->has_mem_region = true;
321 blkio_free_mem_region(data->b, &data->mem_region);
326 static void fio_blkio_iomem_free(struct thread_data *td)
328 struct fio_blkio_data *data = td->io_ops_data;
330 if (data && data->has_mem_region) {
331 blkio_unmap_mem_region(data->b, &data->mem_region);
332 blkio_free_mem_region(data->b, &data->mem_region);
334 data->has_mem_region = false;
338 static int fio_blkio_open_file(struct thread_data *td, struct fio_file *f)
343 static enum fio_q_status fio_blkio_queue(struct thread_data *td,
346 struct fio_blkio_data *data = td->io_ops_data;
348 fio_ro_check(td, io_u);
350 switch (io_u->ddir) {
352 blkioq_read(data->q, io_u->offset, io_u->xfer_buf,
353 (size_t)io_u->xfer_buflen, io_u, 0);
356 blkioq_write(data->q, io_u->offset, io_u->xfer_buf,
357 (size_t)io_u->xfer_buflen, io_u, 0);
360 blkioq_discard(data->q, io_u->offset, io_u->xfer_buflen,
365 blkioq_flush(data->q, io_u, 0);
368 io_u->error = ENOTSUP;
369 io_u_log_error(td, io_u);
370 return FIO_Q_COMPLETED;
376 static int fio_blkio_getevents(struct thread_data *td, unsigned int min,
377 unsigned int max, const struct timespec *t)
379 struct fio_blkio_data *data = td->io_ops_data;
382 n = blkioq_do_io(data->q, data->completions, (int)min, (int)max, NULL);
384 fio_blkio_log_err(blkioq_do_io);
391 static struct io_u *fio_blkio_event(struct thread_data *td, int event)
393 struct fio_blkio_data *data = td->io_ops_data;
394 struct blkio_completion *completion = &data->completions[event];
395 struct io_u *io_u = completion->user_data;
397 io_u->error = -completion->ret;
402 FIO_STATIC struct ioengine_ops ioengine = {
404 .version = FIO_IOOPS_VERSION,
405 .flags = FIO_DISKLESSIO | FIO_NOEXTEND |
408 .setup = fio_blkio_setup,
409 .init = fio_blkio_init,
410 .cleanup = fio_blkio_cleanup,
412 .iomem_alloc = fio_blkio_iomem_alloc,
413 .iomem_free = fio_blkio_iomem_free,
415 .open_file = fio_blkio_open_file,
417 .queue = fio_blkio_queue,
418 .getevents = fio_blkio_getevents,
419 .event = fio_blkio_event,
422 .option_struct_size = sizeof(struct fio_blkio_options),
425 static void fio_init fio_blkio_register(void)
427 register_ioengine(&ioengine);
430 static void fio_exit fio_blkio_unregister(void)
432 unregister_ioengine(&ioengine);