Commit | Line | Data |
---|---|---|
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 | ||
3afc2d8a AF |
23 | /* per-process state */ |
24 | static struct { | |
25 | pthread_mutex_t mutex; | |
26 | int initted_threads; | |
27 | int initted_hipri_threads; | |
28 | struct blkio *b; | |
29 | } proc_state = { PTHREAD_MUTEX_INITIALIZER, 0, 0, NULL }; | |
30 | ||
31 | static void fio_blkio_proc_lock(void) { | |
32 | int ret; | |
33 | ret = pthread_mutex_lock(&proc_state.mutex); | |
34 | assert(ret == 0); | |
35 | } | |
36 | ||
37 | static void fio_blkio_proc_unlock(void) { | |
38 | int ret; | |
39 | ret = pthread_mutex_unlock(&proc_state.mutex); | |
40 | assert(ret == 0); | |
41 | } | |
42 | ||
a601337a AF |
43 | /* per-thread state */ |
44 | struct fio_blkio_data { | |
a601337a | 45 | struct blkioq *q; |
b1bd09b5 | 46 | int completion_fd; /* may be -1 if not FIO_BLKIO_WAIT_MODE_EVENTFD */ |
a601337a AF |
47 | |
48 | bool has_mem_region; /* whether mem_region is valid */ | |
ef9b6f2f | 49 | struct blkio_mem_region mem_region; /* only if allocated by libblkio */ |
a601337a | 50 | |
6dd4291c | 51 | struct iovec *iovecs; /* for vectored requests */ |
a601337a AF |
52 | struct blkio_completion *completions; |
53 | }; | |
54 | ||
b158577d AF |
55 | enum fio_blkio_wait_mode { |
56 | FIO_BLKIO_WAIT_MODE_BLOCK, | |
57 | FIO_BLKIO_WAIT_MODE_EVENTFD, | |
58 | FIO_BLKIO_WAIT_MODE_LOOP, | |
59 | }; | |
60 | ||
a601337a AF |
61 | struct fio_blkio_options { |
62 | void *pad; /* option fields must not have offset 0 */ | |
63 | ||
64 | char *driver; | |
13fffdfb AF |
65 | |
66 | char *path; | |
a601337a | 67 | char *pre_connect_props; |
13fffdfb AF |
68 | |
69 | int num_entries; | |
70 | int queue_size; | |
a601337a | 71 | char *pre_start_props; |
a870d6ff AF |
72 | |
73 | unsigned int hipri; | |
6dd4291c | 74 | unsigned int vectored; |
464981ff | 75 | unsigned int write_zeroes_on_trim; |
b158577d | 76 | enum fio_blkio_wait_mode wait_mode; |
b1bd09b5 | 77 | unsigned int force_enable_completion_eventfd; |
a601337a AF |
78 | }; |
79 | ||
80 | static struct fio_option options[] = { | |
81 | { | |
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, | |
89 | }, | |
13fffdfb AF |
90 | { |
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, | |
98 | }, | |
a601337a AF |
99 | { |
100 | .name = "libblkio_pre_connect_props", | |
13fffdfb | 101 | .lname = "Additional properties to be set before blkio_connect()", |
a601337a AF |
102 | .type = FIO_OPT_STR_STORE, |
103 | .off1 = offsetof(struct fio_blkio_options, pre_connect_props), | |
104 | .help = "", | |
105 | .category = FIO_OPT_C_ENGINE, | |
106 | .group = FIO_OPT_G_LIBBLKIO, | |
107 | }, | |
13fffdfb AF |
108 | { |
109 | .name = "libblkio_num_entries", | |
110 | .lname = "libblkio \"num-entries\" property", | |
111 | .type = FIO_OPT_INT, | |
112 | .off1 = offsetof(struct fio_blkio_options, num_entries), | |
113 | .help = "Value to set the \"num-entries\" property to", | |
114 | .minval = 1, | |
115 | .interval = 1, | |
116 | .category = FIO_OPT_C_ENGINE, | |
117 | .group = FIO_OPT_G_LIBBLKIO, | |
118 | }, | |
119 | { | |
120 | .name = "libblkio_queue_size", | |
121 | .lname = "libblkio \"queue-size\" property", | |
122 | .type = FIO_OPT_INT, | |
123 | .off1 = offsetof(struct fio_blkio_options, queue_size), | |
124 | .help = "Value to set the \"queue-size\" property to", | |
125 | .minval = 1, | |
126 | .interval = 1, | |
127 | .category = FIO_OPT_C_ENGINE, | |
128 | .group = FIO_OPT_G_LIBBLKIO, | |
129 | }, | |
a601337a AF |
130 | { |
131 | .name = "libblkio_pre_start_props", | |
13fffdfb | 132 | .lname = "Additional properties to be set before blkio_start()", |
a601337a AF |
133 | .type = FIO_OPT_STR_STORE, |
134 | .off1 = offsetof(struct fio_blkio_options, pre_start_props), | |
135 | .help = "", | |
136 | .category = FIO_OPT_C_ENGINE, | |
137 | .group = FIO_OPT_G_LIBBLKIO, | |
138 | }, | |
a870d6ff AF |
139 | { |
140 | .name = "hipri", | |
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, | |
147 | }, | |
6dd4291c AF |
148 | { |
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, | |
156 | }, | |
464981ff AF |
157 | { |
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, | |
166 | }, | |
b158577d AF |
167 | { |
168 | .name = "libblkio_wait_mode", | |
169 | .lname = "How to wait for completions", | |
170 | .type = FIO_OPT_STR, | |
171 | .off1 = offsetof(struct fio_blkio_options, wait_mode), | |
172 | .help = "How to wait for completions", | |
173 | .def = "block", | |
174 | .posval = { | |
175 | { .ival = "block", | |
176 | .oval = FIO_BLKIO_WAIT_MODE_BLOCK, | |
177 | .help = "Blocking blkioq_do_io()", | |
178 | }, | |
179 | { .ival = "eventfd", | |
180 | .oval = FIO_BLKIO_WAIT_MODE_EVENTFD, | |
181 | .help = "Blocking read() on the completion eventfd", | |
182 | }, | |
183 | { .ival = "loop", | |
184 | .oval = FIO_BLKIO_WAIT_MODE_LOOP, | |
185 | .help = "Busy loop with non-blocking blkioq_do_io()", | |
186 | }, | |
187 | }, | |
188 | .category = FIO_OPT_C_ENGINE, | |
189 | .group = FIO_OPT_G_LIBBLKIO, | |
190 | }, | |
b1bd09b5 AF |
191 | { |
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, | |
200 | }, | |
a601337a AF |
201 | { |
202 | .name = NULL, | |
203 | }, | |
204 | }; | |
205 | ||
206 | static int fio_blkio_set_props_from_str(struct blkio *b, const char *opt_name, | |
207 | const char *str) { | |
208 | int ret = 0; | |
209 | char *new_str, *name, *value; | |
210 | ||
211 | if (!str) | |
212 | return 0; | |
213 | ||
214 | /* iteration can mutate string, so copy it */ | |
215 | new_str = strdup(str); | |
216 | if (!new_str) { | |
217 | log_err("fio: strdup() failed\n"); | |
218 | return 1; | |
219 | } | |
220 | ||
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, '='); | |
225 | if (!value) { | |
226 | log_err("fio: missing '=' in option %s\n", opt_name); | |
227 | ret = 1; | |
228 | break; | |
229 | } | |
230 | ||
231 | *value = '\0'; | |
232 | ++value; | |
233 | ||
234 | /* strip whitespace from property name */ | |
235 | strip_blank_front(&name); | |
236 | strip_blank_end(name); | |
237 | ||
238 | if (name[0] == '\0') { | |
239 | log_err("fio: empty property name in option %s\n", | |
240 | opt_name); | |
241 | ret = 1; | |
242 | break; | |
243 | } | |
244 | ||
245 | /* strip whitespace from property value */ | |
246 | strip_blank_front(&value); | |
247 | strip_blank_end(value); | |
248 | ||
249 | /* set property */ | |
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()); | |
253 | ret = 1; | |
254 | break; | |
255 | } | |
256 | } | |
257 | ||
258 | free(new_str); | |
259 | return ret; | |
260 | } | |
261 | ||
262 | /* | |
263 | * Log the failure of a libblkio function. | |
264 | * | |
265 | * `(void)func` is to ensure `func` exists and prevent typos | |
266 | */ | |
267 | #define fio_blkio_log_err(func) \ | |
268 | ({ \ | |
269 | (void)func; \ | |
270 | log_err("fio: %s() failed: %s\n", #func, \ | |
271 | blkio_get_error_msg()); \ | |
272 | }) | |
273 | ||
3afc2d8a AF |
274 | static bool possibly_null_strs_equal(const char *a, const char *b) |
275 | { | |
276 | return (!a && !b) || (a && b && strcmp(a, b) == 0); | |
277 | } | |
278 | ||
279 | /* | |
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 | |
282 | * 'hipri' option. | |
283 | */ | |
284 | static int total_threaded_subjobs(bool hipri) | |
285 | { | |
3afc2d8a AF |
286 | int count = 0; |
287 | ||
da8f124f | 288 | for_each_td(td) { |
3afc2d8a AF |
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) | |
292 | ++count; | |
da8f124f | 293 | } end_for_each(); |
3afc2d8a AF |
294 | |
295 | return count; | |
296 | } | |
297 | ||
298 | static struct { | |
299 | bool set_up; | |
300 | bool direct; | |
301 | struct fio_blkio_options opts; | |
302 | } first_threaded_subjob = { 0 }; | |
303 | ||
304 | static void fio_blkio_log_opt_compat_err(const char *option_name) | |
305 | { | |
306 | log_err("fio: jobs using engine libblkio and sharing a process must agree on the %s option\n", | |
307 | option_name); | |
308 | } | |
309 | ||
310 | /* | |
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. | |
313 | */ | |
314 | static int fio_blkio_check_opt_compat(struct thread_data *td) | |
315 | { | |
316 | const struct fio_blkio_options *options = td->eo, *prev_options; | |
317 | ||
318 | if (!td->o.use_thread) | |
319 | return 0; /* subjob doesn't use 'thread' */ | |
320 | ||
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; | |
326 | return 0; | |
327 | } | |
328 | ||
329 | /* not first subjob using 'thread', check option compatibility */ | |
330 | prev_options = &first_threaded_subjob.opts; | |
331 | ||
332 | if (td->o.odirect != first_threaded_subjob.direct) { | |
333 | fio_blkio_log_opt_compat_err("direct/buffered"); | |
334 | return 1; | |
335 | } | |
336 | ||
337 | if (strcmp(options->driver, prev_options->driver) != 0) { | |
338 | fio_blkio_log_opt_compat_err("libblkio_driver"); | |
339 | return 1; | |
340 | } | |
341 | ||
342 | if (!possibly_null_strs_equal(options->path, prev_options->path)) { | |
343 | fio_blkio_log_opt_compat_err("libblkio_path"); | |
344 | return 1; | |
345 | } | |
346 | ||
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"); | |
350 | return 1; | |
351 | } | |
352 | ||
353 | if (options->num_entries != prev_options->num_entries) { | |
354 | fio_blkio_log_opt_compat_err("libblkio_num_entries"); | |
355 | return 1; | |
356 | } | |
357 | ||
358 | if (options->queue_size != prev_options->queue_size) { | |
359 | fio_blkio_log_opt_compat_err("libblkio_queue_size"); | |
360 | return 1; | |
361 | } | |
362 | ||
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"); | |
366 | return 1; | |
367 | } | |
368 | ||
369 | return 0; | |
370 | } | |
371 | ||
a601337a AF |
372 | static int fio_blkio_create_and_connect(struct thread_data *td, |
373 | struct blkio **out_blkio) | |
374 | { | |
375 | const struct fio_blkio_options *options = td->eo; | |
376 | struct blkio *b; | |
377 | int ret; | |
378 | ||
379 | if (!options->driver) { | |
380 | log_err("fio: engine libblkio requires option libblkio_driver to be set\n"); | |
381 | return 1; | |
382 | } | |
383 | ||
384 | if (blkio_create(options->driver, &b) != 0) { | |
385 | fio_blkio_log_err(blkio_create); | |
386 | return 1; | |
387 | } | |
388 | ||
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; | |
394 | } | |
395 | ||
396 | if (blkio_set_bool(b, "read-only", read_only) != 0) { | |
397 | fio_blkio_log_err(blkio_set_bool); | |
398 | goto err_blkio_destroy; | |
399 | } | |
400 | ||
13fffdfb AF |
401 | if (options->path) { |
402 | if (blkio_set_str(b, "path", options->path) != 0) { | |
403 | fio_blkio_log_err(blkio_set_str); | |
404 | goto err_blkio_destroy; | |
405 | } | |
406 | } | |
407 | ||
a601337a AF |
408 | if (fio_blkio_set_props_from_str(b, "libblkio_pre_connect_props", |
409 | options->pre_connect_props) != 0) | |
410 | goto err_blkio_destroy; | |
411 | ||
412 | if (blkio_connect(b) != 0) { | |
413 | fio_blkio_log_err(blkio_connect); | |
414 | goto err_blkio_destroy; | |
415 | } | |
416 | ||
13fffdfb AF |
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; | |
422 | } | |
423 | } | |
424 | ||
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; | |
429 | } | |
430 | } | |
431 | ||
a601337a AF |
432 | if (fio_blkio_set_props_from_str(b, "libblkio_pre_start_props", |
433 | options->pre_start_props) != 0) | |
434 | goto err_blkio_destroy; | |
435 | ||
436 | *out_blkio = b; | |
437 | return 0; | |
438 | ||
439 | err_blkio_destroy: | |
440 | blkio_destroy(&b); | |
441 | return 1; | |
442 | } | |
443 | ||
3afc2d8a AF |
444 | static bool incompatible_threaded_subjob_options = false; |
445 | ||
a601337a AF |
446 | /* |
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. | |
451 | */ | |
452 | static int fio_blkio_setup(struct thread_data *td) | |
453 | { | |
b158577d | 454 | const struct fio_blkio_options *options = td->eo; |
a601337a AF |
455 | struct blkio *b; |
456 | int ret = 0; | |
457 | uint64_t capacity; | |
458 | ||
459 | assert(td->files_index == 1); | |
460 | ||
3afc2d8a AF |
461 | if (fio_blkio_check_opt_compat(td) != 0) { |
462 | incompatible_threaded_subjob_options = true; | |
463 | return 1; | |
464 | } | |
465 | ||
b158577d AF |
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"); | |
469 | return 1; | |
470 | } | |
471 | ||
b1bd09b5 AF |
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"); | |
474 | return 1; | |
475 | } | |
476 | ||
a601337a AF |
477 | if (fio_blkio_create_and_connect(td, &b) != 0) |
478 | return 1; | |
479 | ||
480 | if (blkio_get_uint64(b, "capacity", &capacity) != 0) { | |
481 | fio_blkio_log_err(blkio_get_uint64); | |
482 | ret = 1; | |
483 | goto out_blkio_destroy; | |
484 | } | |
485 | ||
486 | td->files[0]->real_file_size = capacity; | |
487 | fio_file_set_size_known(td->files[0]); | |
488 | ||
489 | out_blkio_destroy: | |
490 | blkio_destroy(&b); | |
491 | return ret; | |
492 | } | |
493 | ||
494 | static int fio_blkio_init(struct thread_data *td) | |
495 | { | |
a870d6ff | 496 | const struct fio_blkio_options *options = td->eo; |
a601337a | 497 | struct fio_blkio_data *data; |
b158577d | 498 | int flags; |
a601337a | 499 | |
3afc2d8a AF |
500 | if (td->o.use_thread && incompatible_threaded_subjob_options) { |
501 | /* | |
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. | |
505 | */ | |
506 | return 1; | |
507 | } | |
508 | ||
a601337a AF |
509 | /* |
510 | * Request enqueueing is fast, and it's not possible to know exactly | |
511 | * when a request is submitted, so never report submission latencies. | |
512 | */ | |
513 | td->o.disable_slat = 1; | |
514 | ||
515 | data = calloc(1, sizeof(*data)); | |
516 | if (!data) { | |
517 | log_err("fio: calloc() failed\n"); | |
518 | return 1; | |
519 | } | |
520 | ||
6dd4291c | 521 | data->iovecs = calloc(td->o.iodepth, sizeof(data->iovecs[0])); |
a601337a | 522 | data->completions = calloc(td->o.iodepth, sizeof(data->completions[0])); |
6dd4291c | 523 | if (!data->iovecs || !data->completions) { |
a601337a AF |
524 | log_err("fio: calloc() failed\n"); |
525 | goto err_free; | |
526 | } | |
527 | ||
3afc2d8a | 528 | fio_blkio_proc_lock(); |
a601337a | 529 | |
3afc2d8a AF |
530 | if (proc_state.initted_threads == 0) { |
531 | /* initialize per-process blkio */ | |
532 | int num_queues, num_poll_queues; | |
a870d6ff | 533 | |
3afc2d8a AF |
534 | if (td->o.use_thread) { |
535 | num_queues = total_threaded_subjobs(false); | |
536 | num_poll_queues = total_threaded_subjobs(true); | |
537 | } else { | |
538 | num_queues = options->hipri ? 0 : 1; | |
539 | num_poll_queues = options->hipri ? 1 : 0; | |
540 | } | |
a601337a | 541 | |
3afc2d8a AF |
542 | if (fio_blkio_create_and_connect(td, &proc_state.b) != 0) |
543 | goto err_unlock; | |
544 | ||
545 | if (blkio_set_int(proc_state.b, "num-queues", | |
546 | num_queues) != 0) { | |
547 | fio_blkio_log_err(blkio_set_int); | |
548 | goto err_blkio_destroy; | |
549 | } | |
550 | ||
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; | |
555 | } | |
556 | ||
557 | if (blkio_start(proc_state.b) != 0) { | |
558 | fio_blkio_log_err(blkio_start); | |
559 | goto err_blkio_destroy; | |
560 | } | |
a601337a AF |
561 | } |
562 | ||
3afc2d8a AF |
563 | if (options->hipri) { |
564 | int i = proc_state.initted_hipri_threads; | |
565 | data->q = blkio_get_poll_queue(proc_state.b, i); | |
566 | } else { | |
567 | int i = proc_state.initted_threads - | |
568 | proc_state.initted_hipri_threads; | |
569 | data->q = blkio_get_queue(proc_state.b, i); | |
570 | } | |
a601337a | 571 | |
b1bd09b5 AF |
572 | if (options->wait_mode == FIO_BLKIO_WAIT_MODE_EVENTFD || |
573 | options->force_enable_completion_eventfd) { | |
b158577d AF |
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); | |
577 | ||
578 | flags = fcntl(data->completion_fd, F_GETFL); | |
579 | if (flags < 0) { | |
580 | log_err("fio: fcntl(F_GETFL) failed: %s\n", | |
581 | strerror(errno)); | |
582 | goto err_blkio_destroy; | |
583 | } | |
584 | ||
585 | if (fcntl(data->completion_fd, F_SETFL, | |
586 | flags & ~O_NONBLOCK) != 0) { | |
587 | log_err("fio: fcntl(F_SETFL) failed: %s\n", | |
588 | strerror(errno)); | |
589 | goto err_blkio_destroy; | |
590 | } | |
591 | } else { | |
592 | data->completion_fd = -1; | |
593 | } | |
594 | ||
3afc2d8a AF |
595 | ++proc_state.initted_threads; |
596 | if (options->hipri) | |
597 | ++proc_state.initted_hipri_threads; | |
598 | ||
a601337a AF |
599 | /* Set data last so cleanup() does nothing if init() fails. */ |
600 | td->io_ops_data = data; | |
601 | ||
3afc2d8a AF |
602 | fio_blkio_proc_unlock(); |
603 | ||
a601337a AF |
604 | return 0; |
605 | ||
606 | err_blkio_destroy: | |
3afc2d8a AF |
607 | if (proc_state.initted_threads == 0) |
608 | blkio_destroy(&proc_state.b); | |
609 | err_unlock: | |
610 | if (proc_state.initted_threads == 0) | |
611 | proc_state.b = NULL; | |
612 | fio_blkio_proc_unlock(); | |
a601337a AF |
613 | err_free: |
614 | free(data->completions); | |
6dd4291c | 615 | free(data->iovecs); |
a601337a AF |
616 | free(data); |
617 | return 1; | |
618 | } | |
619 | ||
ef9b6f2f AF |
620 | static int fio_blkio_post_init(struct thread_data *td) |
621 | { | |
622 | struct fio_blkio_data *data = td->io_ops_data; | |
623 | ||
624 | if (!data->has_mem_region) { | |
625 | /* | |
626 | * Memory was allocated by the fio core and not iomem_alloc(), | |
627 | * so we need to register it as a memory region here. | |
628 | * | |
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. | |
636 | */ | |
637 | ||
638 | unsigned long long max_block_size; | |
639 | struct blkio_mem_region region; | |
640 | ||
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])); | |
644 | ||
645 | region = (struct blkio_mem_region) { | |
646 | .addr = td->orig_buffer, | |
647 | .len = (size_t)max_block_size * | |
648 | (size_t)td->o.iodepth, | |
649 | .fd = -1, | |
650 | }; | |
651 | ||
3afc2d8a | 652 | if (blkio_map_mem_region(proc_state.b, ®ion) != 0) { |
ef9b6f2f AF |
653 | fio_blkio_log_err(blkio_map_mem_region); |
654 | return 1; | |
655 | } | |
656 | } | |
657 | ||
658 | return 0; | |
659 | } | |
660 | ||
a601337a AF |
661 | static void fio_blkio_cleanup(struct thread_data *td) |
662 | { | |
663 | struct fio_blkio_data *data = td->io_ops_data; | |
664 | ||
3afc2d8a AF |
665 | /* |
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 | |
670 | * the blkio. | |
671 | */ | |
672 | ||
a601337a | 673 | if (data) { |
a601337a | 674 | free(data->completions); |
6dd4291c | 675 | free(data->iovecs); |
a601337a | 676 | free(data); |
3afc2d8a AF |
677 | |
678 | fio_blkio_proc_lock(); | |
679 | if (--proc_state.initted_threads == 0) { | |
680 | blkio_destroy(&proc_state.b); | |
681 | proc_state.b = NULL; | |
682 | } | |
683 | fio_blkio_proc_unlock(); | |
a601337a AF |
684 | } |
685 | } | |
686 | ||
687 | #define align_up(x, y) ((((x) + (y) - 1) / (y)) * (y)) | |
688 | ||
689 | static int fio_blkio_iomem_alloc(struct thread_data *td, size_t size) | |
690 | { | |
691 | struct fio_blkio_data *data = td->io_ops_data; | |
692 | int ret; | |
693 | uint64_t mem_region_alignment; | |
694 | ||
3afc2d8a | 695 | if (blkio_get_uint64(proc_state.b, "mem-region-alignment", |
a601337a AF |
696 | &mem_region_alignment) != 0) { |
697 | fio_blkio_log_err(blkio_get_uint64); | |
698 | return 1; | |
699 | } | |
700 | ||
701 | /* round up size to satisfy mem-region-alignment */ | |
702 | size = align_up(size, (size_t)mem_region_alignment); | |
703 | ||
3afc2d8a AF |
704 | fio_blkio_proc_lock(); |
705 | ||
706 | if (blkio_alloc_mem_region(proc_state.b, &data->mem_region, | |
707 | size) != 0) { | |
a601337a AF |
708 | fio_blkio_log_err(blkio_alloc_mem_region); |
709 | ret = 1; | |
710 | goto out; | |
711 | } | |
712 | ||
3afc2d8a | 713 | if (blkio_map_mem_region(proc_state.b, &data->mem_region) != 0) { |
a601337a AF |
714 | fio_blkio_log_err(blkio_map_mem_region); |
715 | ret = 1; | |
716 | goto out_free; | |
717 | } | |
718 | ||
719 | td->orig_buffer = data->mem_region.addr; | |
720 | data->has_mem_region = true; | |
721 | ||
722 | ret = 0; | |
723 | goto out; | |
724 | ||
725 | out_free: | |
3afc2d8a | 726 | blkio_free_mem_region(proc_state.b, &data->mem_region); |
a601337a | 727 | out: |
3afc2d8a | 728 | fio_blkio_proc_unlock(); |
a601337a AF |
729 | return ret; |
730 | } | |
731 | ||
732 | static void fio_blkio_iomem_free(struct thread_data *td) | |
733 | { | |
734 | struct fio_blkio_data *data = td->io_ops_data; | |
735 | ||
736 | if (data && data->has_mem_region) { | |
3afc2d8a AF |
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(); | |
a601337a AF |
741 | |
742 | data->has_mem_region = false; | |
743 | } | |
744 | } | |
745 | ||
746 | static int fio_blkio_open_file(struct thread_data *td, struct fio_file *f) | |
747 | { | |
748 | return 0; | |
749 | } | |
750 | ||
751 | static enum fio_q_status fio_blkio_queue(struct thread_data *td, | |
752 | struct io_u *io_u) | |
753 | { | |
6dd4291c | 754 | const struct fio_blkio_options *options = td->eo; |
a601337a AF |
755 | struct fio_blkio_data *data = td->io_ops_data; |
756 | ||
757 | fio_ro_check(td, io_u); | |
758 | ||
759 | switch (io_u->ddir) { | |
760 | case DDIR_READ: | |
6dd4291c AF |
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; | |
765 | ||
766 | blkioq_readv(data->q, io_u->offset, iov, 1, | |
767 | io_u, 0); | |
768 | } else { | |
769 | blkioq_read(data->q, io_u->offset, | |
770 | io_u->xfer_buf, | |
771 | (size_t)io_u->xfer_buflen, io_u, 0); | |
772 | } | |
a601337a AF |
773 | break; |
774 | case DDIR_WRITE: | |
6dd4291c AF |
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; | |
779 | ||
780 | blkioq_writev(data->q, io_u->offset, iov, 1, | |
781 | io_u, 0); | |
782 | } else { | |
783 | blkioq_write(data->q, io_u->offset, | |
784 | io_u->xfer_buf, | |
785 | (size_t)io_u->xfer_buflen, io_u, | |
786 | 0); | |
787 | } | |
a601337a AF |
788 | break; |
789 | case DDIR_TRIM: | |
464981ff AF |
790 | if (options->write_zeroes_on_trim) { |
791 | blkioq_write_zeroes(data->q, io_u->offset, | |
792 | io_u->xfer_buflen, io_u, 0); | |
793 | } else { | |
794 | blkioq_discard(data->q, io_u->offset, | |
795 | io_u->xfer_buflen, io_u, 0); | |
796 | } | |
a601337a AF |
797 | break; |
798 | case DDIR_SYNC: | |
799 | case DDIR_DATASYNC: | |
800 | blkioq_flush(data->q, io_u, 0); | |
801 | break; | |
802 | default: | |
803 | io_u->error = ENOTSUP; | |
804 | io_u_log_error(td, io_u); | |
805 | return FIO_Q_COMPLETED; | |
806 | } | |
807 | ||
808 | return FIO_Q_QUEUED; | |
809 | } | |
810 | ||
811 | static int fio_blkio_getevents(struct thread_data *td, unsigned int min, | |
812 | unsigned int max, const struct timespec *t) | |
813 | { | |
b158577d | 814 | const struct fio_blkio_options *options = td->eo; |
a601337a | 815 | struct fio_blkio_data *data = td->io_ops_data; |
b158577d AF |
816 | int ret, n; |
817 | uint64_t event; | |
818 | ||
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, | |
822 | NULL); | |
823 | if (n < 0) { | |
824 | fio_blkio_log_err(blkioq_do_io); | |
825 | return -1; | |
826 | } | |
827 | return n; | |
828 | case FIO_BLKIO_WAIT_MODE_EVENTFD: | |
829 | n = blkioq_do_io(data->q, data->completions, 0, (int)max, NULL); | |
830 | if (n < 0) { | |
831 | fio_blkio_log_err(blkioq_do_io); | |
832 | return -1; | |
833 | } | |
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", | |
838 | ret); | |
839 | return -1; | |
840 | } | |
a601337a | 841 | |
b158577d AF |
842 | ret = blkioq_do_io(data->q, data->completions + n, 0, |
843 | (int)max - n, NULL); | |
844 | if (ret < 0) { | |
845 | fio_blkio_log_err(blkioq_do_io); | |
846 | return -1; | |
847 | } | |
848 | ||
849 | n += ret; | |
850 | } | |
851 | return n; | |
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, | |
855 | (int)max - n, NULL); | |
856 | if (ret < 0) { | |
857 | fio_blkio_log_err(blkioq_do_io); | |
858 | return -1; | |
859 | } | |
860 | ||
861 | n += ret; | |
862 | } | |
863 | return n; | |
864 | default: | |
a601337a AF |
865 | return -1; |
866 | } | |
a601337a AF |
867 | } |
868 | ||
869 | static struct io_u *fio_blkio_event(struct thread_data *td, int event) | |
870 | { | |
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; | |
874 | ||
875 | io_u->error = -completion->ret; | |
876 | ||
877 | return io_u; | |
878 | } | |
879 | ||
880 | FIO_STATIC struct ioengine_ops ioengine = { | |
881 | .name = "libblkio", | |
882 | .version = FIO_IOOPS_VERSION, | |
883 | .flags = FIO_DISKLESSIO | FIO_NOEXTEND | | |
ef9b6f2f | 884 | FIO_NO_OFFLOAD | FIO_SKIPPABLE_IOMEM_ALLOC, |
a601337a AF |
885 | |
886 | .setup = fio_blkio_setup, | |
887 | .init = fio_blkio_init, | |
ef9b6f2f | 888 | .post_init = fio_blkio_post_init, |
a601337a AF |
889 | .cleanup = fio_blkio_cleanup, |
890 | ||
891 | .iomem_alloc = fio_blkio_iomem_alloc, | |
892 | .iomem_free = fio_blkio_iomem_free, | |
893 | ||
894 | .open_file = fio_blkio_open_file, | |
895 | ||
896 | .queue = fio_blkio_queue, | |
897 | .getevents = fio_blkio_getevents, | |
898 | .event = fio_blkio_event, | |
899 | ||
900 | .options = options, | |
901 | .option_struct_size = sizeof(struct fio_blkio_options), | |
902 | }; | |
903 | ||
904 | static void fio_init fio_blkio_register(void) | |
905 | { | |
906 | register_ioengine(&ioengine); | |
907 | } | |
908 | ||
909 | static void fio_exit fio_blkio_unregister(void) | |
910 | { | |
911 | unregister_ioengine(&ioengine); | |
912 | } |