engines/libblkio: Add option libblkio_force_enable_completion_eventfd
[fio.git] / engines / libblkio.c
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 */
24 struct fio_blkio_data {
25         struct blkio *b;
26         struct blkioq *q;
27         int completion_fd; /* may be -1 if not FIO_BLKIO_WAIT_MODE_EVENTFD */
28
29         bool has_mem_region; /* whether mem_region is valid */
30         struct blkio_mem_region mem_region; /* only if allocated by libblkio */
31
32         struct iovec *iovecs; /* for vectored requests */
33         struct blkio_completion *completions;
34 };
35
36 enum fio_blkio_wait_mode {
37         FIO_BLKIO_WAIT_MODE_BLOCK,
38         FIO_BLKIO_WAIT_MODE_EVENTFD,
39         FIO_BLKIO_WAIT_MODE_LOOP,
40 };
41
42 struct fio_blkio_options {
43         void *pad; /* option fields must not have offset 0 */
44
45         char *driver;
46         char *pre_connect_props;
47         char *pre_start_props;
48
49         unsigned int hipri;
50         unsigned int vectored;
51         unsigned int write_zeroes_on_trim;
52         enum fio_blkio_wait_mode wait_mode;
53         unsigned int force_enable_completion_eventfd;
54 };
55
56 static struct fio_option options[] = {
57         {
58                 .name   = "libblkio_driver",
59                 .lname  = "libblkio driver name",
60                 .type   = FIO_OPT_STR_STORE,
61                 .off1   = offsetof(struct fio_blkio_options, driver),
62                 .help   = "Name of the driver to be used by libblkio",
63                 .category = FIO_OPT_C_ENGINE,
64                 .group  = FIO_OPT_G_LIBBLKIO,
65         },
66         {
67                 .name   = "libblkio_pre_connect_props",
68                 .lname  = "Properties to be set before blkio_connect()",
69                 .type   = FIO_OPT_STR_STORE,
70                 .off1   = offsetof(struct fio_blkio_options, pre_connect_props),
71                 .help   = "",
72                 .category = FIO_OPT_C_ENGINE,
73                 .group  = FIO_OPT_G_LIBBLKIO,
74         },
75         {
76                 .name   = "libblkio_pre_start_props",
77                 .lname  = "Properties to be set before blkio_start()",
78                 .type   = FIO_OPT_STR_STORE,
79                 .off1   = offsetof(struct fio_blkio_options, pre_start_props),
80                 .help   = "",
81                 .category = FIO_OPT_C_ENGINE,
82                 .group  = FIO_OPT_G_LIBBLKIO,
83         },
84         {
85                 .name   = "hipri",
86                 .lname  = "Use poll queues",
87                 .type   = FIO_OPT_STR_SET,
88                 .off1   = offsetof(struct fio_blkio_options, hipri),
89                 .help   = "Use poll queues",
90                 .category = FIO_OPT_C_ENGINE,
91                 .group  = FIO_OPT_G_LIBBLKIO,
92         },
93         {
94                 .name   = "libblkio_vectored",
95                 .lname  = "Use blkioq_{readv,writev}()",
96                 .type   = FIO_OPT_STR_SET,
97                 .off1   = offsetof(struct fio_blkio_options, vectored),
98                 .help   = "Use blkioq_{readv,writev}() instead of blkioq_{read,write}()",
99                 .category = FIO_OPT_C_ENGINE,
100                 .group  = FIO_OPT_G_LIBBLKIO,
101         },
102         {
103                 .name   = "libblkio_write_zeroes_on_trim",
104                 .lname  = "Use blkioq_write_zeroes() for TRIM",
105                 .type   = FIO_OPT_STR_SET,
106                 .off1   = offsetof(struct fio_blkio_options,
107                                    write_zeroes_on_trim),
108                 .help   = "Use blkioq_write_zeroes() for TRIM instead of blkioq_discard()",
109                 .category = FIO_OPT_C_ENGINE,
110                 .group  = FIO_OPT_G_LIBBLKIO,
111         },
112         {
113                 .name   = "libblkio_wait_mode",
114                 .lname  = "How to wait for completions",
115                 .type   = FIO_OPT_STR,
116                 .off1   = offsetof(struct fio_blkio_options, wait_mode),
117                 .help   = "How to wait for completions",
118                 .def    = "block",
119                 .posval = {
120                           { .ival = "block",
121                             .oval = FIO_BLKIO_WAIT_MODE_BLOCK,
122                             .help = "Blocking blkioq_do_io()",
123                           },
124                           { .ival = "eventfd",
125                             .oval = FIO_BLKIO_WAIT_MODE_EVENTFD,
126                             .help = "Blocking read() on the completion eventfd",
127                           },
128                           { .ival = "loop",
129                             .oval = FIO_BLKIO_WAIT_MODE_LOOP,
130                             .help = "Busy loop with non-blocking blkioq_do_io()",
131                           },
132                 },
133                 .category = FIO_OPT_C_ENGINE,
134                 .group  = FIO_OPT_G_LIBBLKIO,
135         },
136         {
137                 .name   = "libblkio_force_enable_completion_eventfd",
138                 .lname  = "Force enable the completion eventfd, even if unused",
139                 .type   = FIO_OPT_STR_SET,
140                 .off1   = offsetof(struct fio_blkio_options,
141                                    force_enable_completion_eventfd),
142                 .help   = "This can impact performance",
143                 .category = FIO_OPT_C_ENGINE,
144                 .group  = FIO_OPT_G_LIBBLKIO,
145         },
146         {
147                 .name = NULL,
148         },
149 };
150
151 static int fio_blkio_set_props_from_str(struct blkio *b, const char *opt_name,
152                                         const char *str) {
153         int ret = 0;
154         char *new_str, *name, *value;
155
156         if (!str)
157                 return 0;
158
159         /* iteration can mutate string, so copy it */
160         new_str = strdup(str);
161         if (!new_str) {
162                 log_err("fio: strdup() failed\n");
163                 return 1;
164         }
165
166         /* iterate over property name-value pairs */
167         while ((name = get_next_str(&new_str))) {
168                 /* split into property name and value */
169                 value = strchr(name, '=');
170                 if (!value) {
171                         log_err("fio: missing '=' in option %s\n", opt_name);
172                         ret = 1;
173                         break;
174                 }
175
176                 *value = '\0';
177                 ++value;
178
179                 /* strip whitespace from property name */
180                 strip_blank_front(&name);
181                 strip_blank_end(name);
182
183                 if (name[0] == '\0') {
184                         log_err("fio: empty property name in option %s\n",
185                                 opt_name);
186                         ret = 1;
187                         break;
188                 }
189
190                 /* strip whitespace from property value */
191                 strip_blank_front(&value);
192                 strip_blank_end(value);
193
194                 /* set property */
195                 if (blkio_set_str(b, name, value) != 0) {
196                         log_err("fio: error setting property '%s' to '%s': %s\n",
197                                 name, value, blkio_get_error_msg());
198                         ret = 1;
199                         break;
200                 }
201         }
202
203         free(new_str);
204         return ret;
205 }
206
207 /*
208  * Log the failure of a libblkio function.
209  *
210  * `(void)func` is to ensure `func` exists and prevent typos
211  */
212 #define fio_blkio_log_err(func) \
213         ({ \
214                 (void)func; \
215                 log_err("fio: %s() failed: %s\n", #func, \
216                         blkio_get_error_msg()); \
217         })
218
219 static int fio_blkio_create_and_connect(struct thread_data *td,
220                                         struct blkio **out_blkio)
221 {
222         const struct fio_blkio_options *options = td->eo;
223         struct blkio *b;
224         int ret;
225
226         if (!options->driver) {
227                 log_err("fio: engine libblkio requires option libblkio_driver to be set\n");
228                 return 1;
229         }
230
231         if (blkio_create(options->driver, &b) != 0) {
232                 fio_blkio_log_err(blkio_create);
233                 return 1;
234         }
235
236         /* don't fail if driver doesn't have a "direct" property */
237         ret = blkio_set_bool(b, "direct", td->o.odirect);
238         if (ret != 0 && ret != -ENOENT) {
239                 fio_blkio_log_err(blkio_set_bool);
240                 goto err_blkio_destroy;
241         }
242
243         if (blkio_set_bool(b, "read-only", read_only) != 0) {
244                 fio_blkio_log_err(blkio_set_bool);
245                 goto err_blkio_destroy;
246         }
247
248         if (fio_blkio_set_props_from_str(b, "libblkio_pre_connect_props",
249                                          options->pre_connect_props) != 0)
250                 goto err_blkio_destroy;
251
252         if (blkio_connect(b) != 0) {
253                 fio_blkio_log_err(blkio_connect);
254                 goto err_blkio_destroy;
255         }
256
257         if (fio_blkio_set_props_from_str(b, "libblkio_pre_start_props",
258                                          options->pre_start_props) != 0)
259                 goto err_blkio_destroy;
260
261         *out_blkio = b;
262         return 0;
263
264 err_blkio_destroy:
265         blkio_destroy(&b);
266         return 1;
267 }
268
269 /*
270  * This callback determines the device/file size, so it creates and connects a
271  * blkio instance. But it is invoked from the main thread in the original fio
272  * process, not from the processes in which jobs will actually run. It thus
273  * subsequently destroys the blkio, which is recreated in the init() callback.
274  */
275 static int fio_blkio_setup(struct thread_data *td)
276 {
277         const struct fio_blkio_options *options = td->eo;
278         struct blkio *b;
279         int ret = 0;
280         uint64_t capacity;
281
282         assert(td->files_index == 1);
283
284         if (options->hipri &&
285                 options->wait_mode == FIO_BLKIO_WAIT_MODE_EVENTFD) {
286                 log_err("fio: option hipri is incompatible with option libblkio_wait_mode=eventfd\n");
287                 return 1;
288         }
289
290         if (options->hipri && options->force_enable_completion_eventfd) {
291                 log_err("fio: option hipri is incompatible with option libblkio_force_enable_completion_eventfd\n");
292                 return 1;
293         }
294
295         if (fio_blkio_create_and_connect(td, &b) != 0)
296                 return 1;
297
298         if (blkio_get_uint64(b, "capacity", &capacity) != 0) {
299                 fio_blkio_log_err(blkio_get_uint64);
300                 ret = 1;
301                 goto out_blkio_destroy;
302         }
303
304         td->files[0]->real_file_size = capacity;
305         fio_file_set_size_known(td->files[0]);
306
307 out_blkio_destroy:
308         blkio_destroy(&b);
309         return ret;
310 }
311
312 static int fio_blkio_init(struct thread_data *td)
313 {
314         const struct fio_blkio_options *options = td->eo;
315         struct fio_blkio_data *data;
316         int flags;
317
318         /*
319          * Request enqueueing is fast, and it's not possible to know exactly
320          * when a request is submitted, so never report submission latencies.
321          */
322         td->o.disable_slat = 1;
323
324         data = calloc(1, sizeof(*data));
325         if (!data) {
326                 log_err("fio: calloc() failed\n");
327                 return 1;
328         }
329
330         data->iovecs = calloc(td->o.iodepth, sizeof(data->iovecs[0]));
331         data->completions = calloc(td->o.iodepth, sizeof(data->completions[0]));
332         if (!data->iovecs || !data->completions) {
333                 log_err("fio: calloc() failed\n");
334                 goto err_free;
335         }
336
337         if (fio_blkio_create_and_connect(td, &data->b) != 0)
338                 goto err_free;
339
340         if (blkio_set_int(data->b, "num-queues", options->hipri ? 0 : 1) != 0) {
341                 fio_blkio_log_err(blkio_set_int);
342                 goto err_blkio_destroy;
343         }
344
345         if (blkio_set_int(data->b, "num-poll-queues",
346                           options->hipri ? 1 : 0) != 0) {
347                 fio_blkio_log_err(blkio_set_int);
348                 goto err_blkio_destroy;
349         }
350
351         if (blkio_start(data->b) != 0) {
352                 fio_blkio_log_err(blkio_start);
353                 goto err_blkio_destroy;
354         }
355
356         if (options->hipri)
357                 data->q = blkio_get_poll_queue(data->b, 0);
358         else
359                 data->q = blkio_get_queue(data->b, 0);
360
361         if (options->wait_mode == FIO_BLKIO_WAIT_MODE_EVENTFD ||
362                 options->force_enable_completion_eventfd) {
363                 /* enable completion fd and make it blocking */
364                 blkioq_set_completion_fd_enabled(data->q, true);
365                 data->completion_fd = blkioq_get_completion_fd(data->q);
366
367                 flags = fcntl(data->completion_fd, F_GETFL);
368                 if (flags < 0) {
369                         log_err("fio: fcntl(F_GETFL) failed: %s\n",
370                                 strerror(errno));
371                         goto err_blkio_destroy;
372                 }
373
374                 if (fcntl(data->completion_fd, F_SETFL,
375                           flags & ~O_NONBLOCK) != 0) {
376                         log_err("fio: fcntl(F_SETFL) failed: %s\n",
377                                 strerror(errno));
378                         goto err_blkio_destroy;
379                 }
380         } else {
381                 data->completion_fd = -1;
382         }
383
384         /* Set data last so cleanup() does nothing if init() fails. */
385         td->io_ops_data = data;
386
387         return 0;
388
389 err_blkio_destroy:
390         blkio_destroy(&data->b);
391 err_free:
392         free(data->completions);
393         free(data->iovecs);
394         free(data);
395         return 1;
396 }
397
398 static int fio_blkio_post_init(struct thread_data *td)
399 {
400         struct fio_blkio_data *data = td->io_ops_data;
401
402         if (!data->has_mem_region) {
403                 /*
404                  * Memory was allocated by the fio core and not iomem_alloc(),
405                  * so we need to register it as a memory region here.
406                  *
407                  * `td->orig_buffer_size` is computed like `len` below, but then
408                  * fio can add some padding to it to make sure it is
409                  * sufficiently aligned to the page size and the mem_align
410                  * option. However, this can make it become unaligned to the
411                  * "mem-region-alignment" property in ways that the user can't
412                  * control, so we essentially recompute `td->orig_buffer_size`
413                  * here but without adding that padding.
414                  */
415
416                 unsigned long long max_block_size;
417                 struct blkio_mem_region region;
418
419                 max_block_size = max(td->o.max_bs[DDIR_READ],
420                                      max(td->o.max_bs[DDIR_WRITE],
421                                          td->o.max_bs[DDIR_TRIM]));
422
423                 region = (struct blkio_mem_region) {
424                         .addr   = td->orig_buffer,
425                         .len    = (size_t)max_block_size *
426                                         (size_t)td->o.iodepth,
427                         .fd     = -1,
428                 };
429
430                 if (blkio_map_mem_region(data->b, &region) != 0) {
431                         fio_blkio_log_err(blkio_map_mem_region);
432                         return 1;
433                 }
434         }
435
436         return 0;
437 }
438
439 static void fio_blkio_cleanup(struct thread_data *td)
440 {
441         struct fio_blkio_data *data = td->io_ops_data;
442
443         if (data) {
444                 blkio_destroy(&data->b);
445                 free(data->completions);
446                 free(data->iovecs);
447                 free(data);
448         }
449 }
450
451 #define align_up(x, y) ((((x) + (y) - 1) / (y)) * (y))
452
453 static int fio_blkio_iomem_alloc(struct thread_data *td, size_t size)
454 {
455         struct fio_blkio_data *data = td->io_ops_data;
456         int ret;
457         uint64_t mem_region_alignment;
458
459         if (blkio_get_uint64(data->b, "mem-region-alignment",
460                              &mem_region_alignment) != 0) {
461                 fio_blkio_log_err(blkio_get_uint64);
462                 return 1;
463         }
464
465         /* round up size to satisfy mem-region-alignment */
466         size = align_up(size, (size_t)mem_region_alignment);
467
468         if (blkio_alloc_mem_region(data->b, &data->mem_region, size) != 0) {
469                 fio_blkio_log_err(blkio_alloc_mem_region);
470                 ret = 1;
471                 goto out;
472         }
473
474         if (blkio_map_mem_region(data->b, &data->mem_region) != 0) {
475                 fio_blkio_log_err(blkio_map_mem_region);
476                 ret = 1;
477                 goto out_free;
478         }
479
480         td->orig_buffer = data->mem_region.addr;
481         data->has_mem_region = true;
482
483         ret = 0;
484         goto out;
485
486 out_free:
487         blkio_free_mem_region(data->b, &data->mem_region);
488 out:
489         return ret;
490 }
491
492 static void fio_blkio_iomem_free(struct thread_data *td)
493 {
494         struct fio_blkio_data *data = td->io_ops_data;
495
496         if (data && data->has_mem_region) {
497                 blkio_unmap_mem_region(data->b, &data->mem_region);
498                 blkio_free_mem_region(data->b, &data->mem_region);
499
500                 data->has_mem_region = false;
501         }
502 }
503
504 static int fio_blkio_open_file(struct thread_data *td, struct fio_file *f)
505 {
506         return 0;
507 }
508
509 static enum fio_q_status fio_blkio_queue(struct thread_data *td,
510                                          struct io_u *io_u)
511 {
512         const struct fio_blkio_options *options = td->eo;
513         struct fio_blkio_data *data = td->io_ops_data;
514
515         fio_ro_check(td, io_u);
516
517         switch (io_u->ddir) {
518                 case DDIR_READ:
519                         if (options->vectored) {
520                                 struct iovec *iov = &data->iovecs[io_u->index];
521                                 iov->iov_base = io_u->xfer_buf;
522                                 iov->iov_len = (size_t)io_u->xfer_buflen;
523
524                                 blkioq_readv(data->q, io_u->offset, iov, 1,
525                                              io_u, 0);
526                         } else {
527                                 blkioq_read(data->q, io_u->offset,
528                                             io_u->xfer_buf,
529                                             (size_t)io_u->xfer_buflen, io_u, 0);
530                         }
531                         break;
532                 case DDIR_WRITE:
533                         if (options->vectored) {
534                                 struct iovec *iov = &data->iovecs[io_u->index];
535                                 iov->iov_base = io_u->xfer_buf;
536                                 iov->iov_len = (size_t)io_u->xfer_buflen;
537
538                                 blkioq_writev(data->q, io_u->offset, iov, 1,
539                                               io_u, 0);
540                         } else {
541                                 blkioq_write(data->q, io_u->offset,
542                                              io_u->xfer_buf,
543                                              (size_t)io_u->xfer_buflen, io_u,
544                                              0);
545                         }
546                         break;
547                 case DDIR_TRIM:
548                         if (options->write_zeroes_on_trim) {
549                                 blkioq_write_zeroes(data->q, io_u->offset,
550                                                     io_u->xfer_buflen, io_u, 0);
551                         } else {
552                                 blkioq_discard(data->q, io_u->offset,
553                                                io_u->xfer_buflen, io_u, 0);
554                         }
555                         break;
556                 case DDIR_SYNC:
557                 case DDIR_DATASYNC:
558                         blkioq_flush(data->q, io_u, 0);
559                         break;
560                 default:
561                         io_u->error = ENOTSUP;
562                         io_u_log_error(td, io_u);
563                         return FIO_Q_COMPLETED;
564         }
565
566         return FIO_Q_QUEUED;
567 }
568
569 static int fio_blkio_getevents(struct thread_data *td, unsigned int min,
570                                unsigned int max, const struct timespec *t)
571 {
572         const struct fio_blkio_options *options = td->eo;
573         struct fio_blkio_data *data = td->io_ops_data;
574         int ret, n;
575         uint64_t event;
576
577         switch (options->wait_mode) {
578         case FIO_BLKIO_WAIT_MODE_BLOCK:
579                 n = blkioq_do_io(data->q, data->completions, (int)min, (int)max,
580                                  NULL);
581                 if (n < 0) {
582                         fio_blkio_log_err(blkioq_do_io);
583                         return -1;
584                 }
585                 return n;
586         case FIO_BLKIO_WAIT_MODE_EVENTFD:
587                 n = blkioq_do_io(data->q, data->completions, 0, (int)max, NULL);
588                 if (n < 0) {
589                         fio_blkio_log_err(blkioq_do_io);
590                         return -1;
591                 }
592                 while (n < (int)min) {
593                         ret = read(data->completion_fd, &event, sizeof(event));
594                         if (ret != sizeof(event)) {
595                                 log_err("fio: read() on the completion fd returned %d\n",
596                                         ret);
597                                 return -1;
598                         }
599
600                         ret = blkioq_do_io(data->q, data->completions + n, 0,
601                                            (int)max - n, NULL);
602                         if (ret < 0) {
603                                 fio_blkio_log_err(blkioq_do_io);
604                                 return -1;
605                         }
606
607                         n += ret;
608                 }
609                 return n;
610         case FIO_BLKIO_WAIT_MODE_LOOP:
611                 for (n = 0; n < (int)min; ) {
612                         ret = blkioq_do_io(data->q, data->completions + n, 0,
613                                            (int)max - n, NULL);
614                         if (ret < 0) {
615                                 fio_blkio_log_err(blkioq_do_io);
616                                 return -1;
617                         }
618
619                         n += ret;
620                 }
621                 return n;
622         default:
623                 return -1;
624         }
625 }
626
627 static struct io_u *fio_blkio_event(struct thread_data *td, int event)
628 {
629         struct fio_blkio_data *data = td->io_ops_data;
630         struct blkio_completion *completion = &data->completions[event];
631         struct io_u *io_u = completion->user_data;
632
633         io_u->error = -completion->ret;
634
635         return io_u;
636 }
637
638 FIO_STATIC struct ioengine_ops ioengine = {
639         .name                   = "libblkio",
640         .version                = FIO_IOOPS_VERSION,
641         .flags                  = FIO_DISKLESSIO | FIO_NOEXTEND |
642                                   FIO_NO_OFFLOAD | FIO_SKIPPABLE_IOMEM_ALLOC,
643
644         .setup                  = fio_blkio_setup,
645         .init                   = fio_blkio_init,
646         .post_init              = fio_blkio_post_init,
647         .cleanup                = fio_blkio_cleanup,
648
649         .iomem_alloc            = fio_blkio_iomem_alloc,
650         .iomem_free             = fio_blkio_iomem_free,
651
652         .open_file              = fio_blkio_open_file,
653
654         .queue                  = fio_blkio_queue,
655         .getevents              = fio_blkio_getevents,
656         .event                  = fio_blkio_event,
657
658         .options                = options,
659         .option_struct_size     = sizeof(struct fio_blkio_options),
660 };
661
662 static void fio_init fio_blkio_register(void)
663 {
664         register_ioengine(&ioengine);
665 }
666
667 static void fio_exit fio_blkio_unregister(void)
668 {
669         unregister_ioengine(&ioengine);
670 }