engines/libblkio: Add option libblkio_vectored
[fio.git] / engines / libblkio.c
CommitLineData
a601337a
AF
1/*
2 * libblkio engine
3 *
4 * IO engine using libblkio to access various block I/O interfaces:
5 * https://gitlab.com/libblkio/libblkio
6 */
7
8#include <assert.h>
9#include <errno.h>
10#include <stdbool.h>
11#include <stddef.h>
12#include <stdint.h>
13#include <stdlib.h>
14#include <string.h>
15
16#include <blkio.h>
17
18#include "../fio.h"
19#include "../optgroup.h"
20#include "../options.h"
21#include "../parse.h"
22
23/* per-thread state */
24struct fio_blkio_data {
25 struct blkio *b;
26 struct blkioq *q;
27
28 bool has_mem_region; /* whether mem_region is valid */
ef9b6f2f 29 struct blkio_mem_region mem_region; /* only if allocated by libblkio */
a601337a 30
6dd4291c 31 struct iovec *iovecs; /* for vectored requests */
a601337a
AF
32 struct blkio_completion *completions;
33};
34
35struct fio_blkio_options {
36 void *pad; /* option fields must not have offset 0 */
37
38 char *driver;
39 char *pre_connect_props;
40 char *pre_start_props;
a870d6ff
AF
41
42 unsigned int hipri;
6dd4291c 43 unsigned int vectored;
a601337a
AF
44};
45
46static struct fio_option options[] = {
47 {
48 .name = "libblkio_driver",
49 .lname = "libblkio driver name",
50 .type = FIO_OPT_STR_STORE,
51 .off1 = offsetof(struct fio_blkio_options, driver),
52 .help = "Name of the driver to be used by libblkio",
53 .category = FIO_OPT_C_ENGINE,
54 .group = FIO_OPT_G_LIBBLKIO,
55 },
56 {
57 .name = "libblkio_pre_connect_props",
58 .lname = "Properties to be set before blkio_connect()",
59 .type = FIO_OPT_STR_STORE,
60 .off1 = offsetof(struct fio_blkio_options, pre_connect_props),
61 .help = "",
62 .category = FIO_OPT_C_ENGINE,
63 .group = FIO_OPT_G_LIBBLKIO,
64 },
65 {
66 .name = "libblkio_pre_start_props",
67 .lname = "Properties to be set before blkio_start()",
68 .type = FIO_OPT_STR_STORE,
69 .off1 = offsetof(struct fio_blkio_options, pre_start_props),
70 .help = "",
71 .category = FIO_OPT_C_ENGINE,
72 .group = FIO_OPT_G_LIBBLKIO,
73 },
a870d6ff
AF
74 {
75 .name = "hipri",
76 .lname = "Use poll queues",
77 .type = FIO_OPT_STR_SET,
78 .off1 = offsetof(struct fio_blkio_options, hipri),
79 .help = "Use poll queues",
80 .category = FIO_OPT_C_ENGINE,
81 .group = FIO_OPT_G_LIBBLKIO,
82 },
6dd4291c
AF
83 {
84 .name = "libblkio_vectored",
85 .lname = "Use blkioq_{readv,writev}()",
86 .type = FIO_OPT_STR_SET,
87 .off1 = offsetof(struct fio_blkio_options, vectored),
88 .help = "Use blkioq_{readv,writev}() instead of blkioq_{read,write}()",
89 .category = FIO_OPT_C_ENGINE,
90 .group = FIO_OPT_G_LIBBLKIO,
91 },
a601337a
AF
92 {
93 .name = NULL,
94 },
95};
96
97static int fio_blkio_set_props_from_str(struct blkio *b, const char *opt_name,
98 const char *str) {
99 int ret = 0;
100 char *new_str, *name, *value;
101
102 if (!str)
103 return 0;
104
105 /* iteration can mutate string, so copy it */
106 new_str = strdup(str);
107 if (!new_str) {
108 log_err("fio: strdup() failed\n");
109 return 1;
110 }
111
112 /* iterate over property name-value pairs */
113 while ((name = get_next_str(&new_str))) {
114 /* split into property name and value */
115 value = strchr(name, '=');
116 if (!value) {
117 log_err("fio: missing '=' in option %s\n", opt_name);
118 ret = 1;
119 break;
120 }
121
122 *value = '\0';
123 ++value;
124
125 /* strip whitespace from property name */
126 strip_blank_front(&name);
127 strip_blank_end(name);
128
129 if (name[0] == '\0') {
130 log_err("fio: empty property name in option %s\n",
131 opt_name);
132 ret = 1;
133 break;
134 }
135
136 /* strip whitespace from property value */
137 strip_blank_front(&value);
138 strip_blank_end(value);
139
140 /* set property */
141 if (blkio_set_str(b, name, value) != 0) {
142 log_err("fio: error setting property '%s' to '%s': %s\n",
143 name, value, blkio_get_error_msg());
144 ret = 1;
145 break;
146 }
147 }
148
149 free(new_str);
150 return ret;
151}
152
153/*
154 * Log the failure of a libblkio function.
155 *
156 * `(void)func` is to ensure `func` exists and prevent typos
157 */
158#define fio_blkio_log_err(func) \
159 ({ \
160 (void)func; \
161 log_err("fio: %s() failed: %s\n", #func, \
162 blkio_get_error_msg()); \
163 })
164
165static int fio_blkio_create_and_connect(struct thread_data *td,
166 struct blkio **out_blkio)
167{
168 const struct fio_blkio_options *options = td->eo;
169 struct blkio *b;
170 int ret;
171
172 if (!options->driver) {
173 log_err("fio: engine libblkio requires option libblkio_driver to be set\n");
174 return 1;
175 }
176
177 if (blkio_create(options->driver, &b) != 0) {
178 fio_blkio_log_err(blkio_create);
179 return 1;
180 }
181
182 /* don't fail if driver doesn't have a "direct" property */
183 ret = blkio_set_bool(b, "direct", td->o.odirect);
184 if (ret != 0 && ret != -ENOENT) {
185 fio_blkio_log_err(blkio_set_bool);
186 goto err_blkio_destroy;
187 }
188
189 if (blkio_set_bool(b, "read-only", read_only) != 0) {
190 fio_blkio_log_err(blkio_set_bool);
191 goto err_blkio_destroy;
192 }
193
194 if (fio_blkio_set_props_from_str(b, "libblkio_pre_connect_props",
195 options->pre_connect_props) != 0)
196 goto err_blkio_destroy;
197
198 if (blkio_connect(b) != 0) {
199 fio_blkio_log_err(blkio_connect);
200 goto err_blkio_destroy;
201 }
202
203 if (fio_blkio_set_props_from_str(b, "libblkio_pre_start_props",
204 options->pre_start_props) != 0)
205 goto err_blkio_destroy;
206
207 *out_blkio = b;
208 return 0;
209
210err_blkio_destroy:
211 blkio_destroy(&b);
212 return 1;
213}
214
215/*
216 * This callback determines the device/file size, so it creates and connects a
217 * blkio instance. But it is invoked from the main thread in the original fio
218 * process, not from the processes in which jobs will actually run. It thus
219 * subsequently destroys the blkio, which is recreated in the init() callback.
220 */
221static int fio_blkio_setup(struct thread_data *td)
222{
223 struct blkio *b;
224 int ret = 0;
225 uint64_t capacity;
226
227 assert(td->files_index == 1);
228
229 if (fio_blkio_create_and_connect(td, &b) != 0)
230 return 1;
231
232 if (blkio_get_uint64(b, "capacity", &capacity) != 0) {
233 fio_blkio_log_err(blkio_get_uint64);
234 ret = 1;
235 goto out_blkio_destroy;
236 }
237
238 td->files[0]->real_file_size = capacity;
239 fio_file_set_size_known(td->files[0]);
240
241out_blkio_destroy:
242 blkio_destroy(&b);
243 return ret;
244}
245
246static int fio_blkio_init(struct thread_data *td)
247{
a870d6ff 248 const struct fio_blkio_options *options = td->eo;
a601337a
AF
249 struct fio_blkio_data *data;
250
251 /*
252 * Request enqueueing is fast, and it's not possible to know exactly
253 * when a request is submitted, so never report submission latencies.
254 */
255 td->o.disable_slat = 1;
256
257 data = calloc(1, sizeof(*data));
258 if (!data) {
259 log_err("fio: calloc() failed\n");
260 return 1;
261 }
262
6dd4291c 263 data->iovecs = calloc(td->o.iodepth, sizeof(data->iovecs[0]));
a601337a 264 data->completions = calloc(td->o.iodepth, sizeof(data->completions[0]));
6dd4291c 265 if (!data->iovecs || !data->completions) {
a601337a
AF
266 log_err("fio: calloc() failed\n");
267 goto err_free;
268 }
269
270 if (fio_blkio_create_and_connect(td, &data->b) != 0)
271 goto err_free;
272
a870d6ff
AF
273 if (blkio_set_int(data->b, "num-queues", options->hipri ? 0 : 1) != 0) {
274 fio_blkio_log_err(blkio_set_int);
275 goto err_blkio_destroy;
276 }
277
278 if (blkio_set_int(data->b, "num-poll-queues",
279 options->hipri ? 1 : 0) != 0) {
a601337a
AF
280 fio_blkio_log_err(blkio_set_int);
281 goto err_blkio_destroy;
282 }
283
284 if (blkio_start(data->b) != 0) {
285 fio_blkio_log_err(blkio_start);
286 goto err_blkio_destroy;
287 }
288
a870d6ff
AF
289 if (options->hipri)
290 data->q = blkio_get_poll_queue(data->b, 0);
291 else
292 data->q = blkio_get_queue(data->b, 0);
a601337a
AF
293
294 /* Set data last so cleanup() does nothing if init() fails. */
295 td->io_ops_data = data;
296
297 return 0;
298
299err_blkio_destroy:
300 blkio_destroy(&data->b);
301err_free:
302 free(data->completions);
6dd4291c 303 free(data->iovecs);
a601337a
AF
304 free(data);
305 return 1;
306}
307
ef9b6f2f
AF
308static int fio_blkio_post_init(struct thread_data *td)
309{
310 struct fio_blkio_data *data = td->io_ops_data;
311
312 if (!data->has_mem_region) {
313 /*
314 * Memory was allocated by the fio core and not iomem_alloc(),
315 * so we need to register it as a memory region here.
316 *
317 * `td->orig_buffer_size` is computed like `len` below, but then
318 * fio can add some padding to it to make sure it is
319 * sufficiently aligned to the page size and the mem_align
320 * option. However, this can make it become unaligned to the
321 * "mem-region-alignment" property in ways that the user can't
322 * control, so we essentially recompute `td->orig_buffer_size`
323 * here but without adding that padding.
324 */
325
326 unsigned long long max_block_size;
327 struct blkio_mem_region region;
328
329 max_block_size = max(td->o.max_bs[DDIR_READ],
330 max(td->o.max_bs[DDIR_WRITE],
331 td->o.max_bs[DDIR_TRIM]));
332
333 region = (struct blkio_mem_region) {
334 .addr = td->orig_buffer,
335 .len = (size_t)max_block_size *
336 (size_t)td->o.iodepth,
337 .fd = -1,
338 };
339
340 if (blkio_map_mem_region(data->b, &region) != 0) {
341 fio_blkio_log_err(blkio_map_mem_region);
342 return 1;
343 }
344 }
345
346 return 0;
347}
348
a601337a
AF
349static void fio_blkio_cleanup(struct thread_data *td)
350{
351 struct fio_blkio_data *data = td->io_ops_data;
352
353 if (data) {
354 blkio_destroy(&data->b);
355 free(data->completions);
6dd4291c 356 free(data->iovecs);
a601337a
AF
357 free(data);
358 }
359}
360
361#define align_up(x, y) ((((x) + (y) - 1) / (y)) * (y))
362
363static int fio_blkio_iomem_alloc(struct thread_data *td, size_t size)
364{
365 struct fio_blkio_data *data = td->io_ops_data;
366 int ret;
367 uint64_t mem_region_alignment;
368
369 if (blkio_get_uint64(data->b, "mem-region-alignment",
370 &mem_region_alignment) != 0) {
371 fio_blkio_log_err(blkio_get_uint64);
372 return 1;
373 }
374
375 /* round up size to satisfy mem-region-alignment */
376 size = align_up(size, (size_t)mem_region_alignment);
377
378 if (blkio_alloc_mem_region(data->b, &data->mem_region, size) != 0) {
379 fio_blkio_log_err(blkio_alloc_mem_region);
380 ret = 1;
381 goto out;
382 }
383
384 if (blkio_map_mem_region(data->b, &data->mem_region) != 0) {
385 fio_blkio_log_err(blkio_map_mem_region);
386 ret = 1;
387 goto out_free;
388 }
389
390 td->orig_buffer = data->mem_region.addr;
391 data->has_mem_region = true;
392
393 ret = 0;
394 goto out;
395
396out_free:
397 blkio_free_mem_region(data->b, &data->mem_region);
398out:
399 return ret;
400}
401
402static void fio_blkio_iomem_free(struct thread_data *td)
403{
404 struct fio_blkio_data *data = td->io_ops_data;
405
406 if (data && data->has_mem_region) {
407 blkio_unmap_mem_region(data->b, &data->mem_region);
408 blkio_free_mem_region(data->b, &data->mem_region);
409
410 data->has_mem_region = false;
411 }
412}
413
414static int fio_blkio_open_file(struct thread_data *td, struct fio_file *f)
415{
416 return 0;
417}
418
419static enum fio_q_status fio_blkio_queue(struct thread_data *td,
420 struct io_u *io_u)
421{
6dd4291c 422 const struct fio_blkio_options *options = td->eo;
a601337a
AF
423 struct fio_blkio_data *data = td->io_ops_data;
424
425 fio_ro_check(td, io_u);
426
427 switch (io_u->ddir) {
428 case DDIR_READ:
6dd4291c
AF
429 if (options->vectored) {
430 struct iovec *iov = &data->iovecs[io_u->index];
431 iov->iov_base = io_u->xfer_buf;
432 iov->iov_len = (size_t)io_u->xfer_buflen;
433
434 blkioq_readv(data->q, io_u->offset, iov, 1,
435 io_u, 0);
436 } else {
437 blkioq_read(data->q, io_u->offset,
438 io_u->xfer_buf,
439 (size_t)io_u->xfer_buflen, io_u, 0);
440 }
a601337a
AF
441 break;
442 case DDIR_WRITE:
6dd4291c
AF
443 if (options->vectored) {
444 struct iovec *iov = &data->iovecs[io_u->index];
445 iov->iov_base = io_u->xfer_buf;
446 iov->iov_len = (size_t)io_u->xfer_buflen;
447
448 blkioq_writev(data->q, io_u->offset, iov, 1,
449 io_u, 0);
450 } else {
451 blkioq_write(data->q, io_u->offset,
452 io_u->xfer_buf,
453 (size_t)io_u->xfer_buflen, io_u,
454 0);
455 }
a601337a
AF
456 break;
457 case DDIR_TRIM:
458 blkioq_discard(data->q, io_u->offset, io_u->xfer_buflen,
459 io_u, 0);
460 break;
461 case DDIR_SYNC:
462 case DDIR_DATASYNC:
463 blkioq_flush(data->q, io_u, 0);
464 break;
465 default:
466 io_u->error = ENOTSUP;
467 io_u_log_error(td, io_u);
468 return FIO_Q_COMPLETED;
469 }
470
471 return FIO_Q_QUEUED;
472}
473
474static int fio_blkio_getevents(struct thread_data *td, unsigned int min,
475 unsigned int max, const struct timespec *t)
476{
477 struct fio_blkio_data *data = td->io_ops_data;
478 int n;
479
480 n = blkioq_do_io(data->q, data->completions, (int)min, (int)max, NULL);
481 if (n < 0) {
482 fio_blkio_log_err(blkioq_do_io);
483 return -1;
484 }
485
486 return n;
487}
488
489static struct io_u *fio_blkio_event(struct thread_data *td, int event)
490{
491 struct fio_blkio_data *data = td->io_ops_data;
492 struct blkio_completion *completion = &data->completions[event];
493 struct io_u *io_u = completion->user_data;
494
495 io_u->error = -completion->ret;
496
497 return io_u;
498}
499
500FIO_STATIC struct ioengine_ops ioengine = {
501 .name = "libblkio",
502 .version = FIO_IOOPS_VERSION,
503 .flags = FIO_DISKLESSIO | FIO_NOEXTEND |
ef9b6f2f 504 FIO_NO_OFFLOAD | FIO_SKIPPABLE_IOMEM_ALLOC,
a601337a
AF
505
506 .setup = fio_blkio_setup,
507 .init = fio_blkio_init,
ef9b6f2f 508 .post_init = fio_blkio_post_init,
a601337a
AF
509 .cleanup = fio_blkio_cleanup,
510
511 .iomem_alloc = fio_blkio_iomem_alloc,
512 .iomem_free = fio_blkio_iomem_free,
513
514 .open_file = fio_blkio_open_file,
515
516 .queue = fio_blkio_queue,
517 .getevents = fio_blkio_getevents,
518 .event = fio_blkio_event,
519
520 .options = options,
521 .option_struct_size = sizeof(struct fio_blkio_options),
522};
523
524static void fio_init fio_blkio_register(void)
525{
526 register_ioengine(&ioengine);
527}
528
529static void fio_exit fio_blkio_unregister(void)
530{
531 unregister_ioengine(&ioengine);
532}