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 | { | |
286 | struct thread_data *td; | |
287 | unsigned int i; | |
288 | int count = 0; | |
289 | ||
290 | for_each_td(td, i) { | |
291 | const struct fio_blkio_options *options = td->eo; | |
292 | if (strcmp(td->o.ioengine, "libblkio") == 0 && | |
293 | td->o.use_thread && (bool)options->hipri == hipri) | |
294 | ++count; | |
295 | } | |
296 | ||
297 | return count; | |
298 | } | |
299 | ||
300 | static struct { | |
301 | bool set_up; | |
302 | bool direct; | |
303 | struct fio_blkio_options opts; | |
304 | } first_threaded_subjob = { 0 }; | |
305 | ||
306 | static void fio_blkio_log_opt_compat_err(const char *option_name) | |
307 | { | |
308 | log_err("fio: jobs using engine libblkio and sharing a process must agree on the %s option\n", | |
309 | option_name); | |
310 | } | |
311 | ||
312 | /* | |
313 | * If td represents a subjob with option 'thread', check if its options are | |
314 | * compatible with those of other threaded subjobs that were already set up. | |
315 | */ | |
316 | static int fio_blkio_check_opt_compat(struct thread_data *td) | |
317 | { | |
318 | const struct fio_blkio_options *options = td->eo, *prev_options; | |
319 | ||
320 | if (!td->o.use_thread) | |
321 | return 0; /* subjob doesn't use 'thread' */ | |
322 | ||
323 | if (!first_threaded_subjob.set_up) { | |
324 | /* first subjob using 'thread', store options for later */ | |
325 | first_threaded_subjob.set_up = true; | |
326 | first_threaded_subjob.direct = td->o.odirect; | |
327 | first_threaded_subjob.opts = *options; | |
328 | return 0; | |
329 | } | |
330 | ||
331 | /* not first subjob using 'thread', check option compatibility */ | |
332 | prev_options = &first_threaded_subjob.opts; | |
333 | ||
334 | if (td->o.odirect != first_threaded_subjob.direct) { | |
335 | fio_blkio_log_opt_compat_err("direct/buffered"); | |
336 | return 1; | |
337 | } | |
338 | ||
339 | if (strcmp(options->driver, prev_options->driver) != 0) { | |
340 | fio_blkio_log_opt_compat_err("libblkio_driver"); | |
341 | return 1; | |
342 | } | |
343 | ||
344 | if (!possibly_null_strs_equal(options->path, prev_options->path)) { | |
345 | fio_blkio_log_opt_compat_err("libblkio_path"); | |
346 | return 1; | |
347 | } | |
348 | ||
349 | if (!possibly_null_strs_equal(options->pre_connect_props, | |
350 | prev_options->pre_connect_props)) { | |
351 | fio_blkio_log_opt_compat_err("libblkio_pre_connect_props"); | |
352 | return 1; | |
353 | } | |
354 | ||
355 | if (options->num_entries != prev_options->num_entries) { | |
356 | fio_blkio_log_opt_compat_err("libblkio_num_entries"); | |
357 | return 1; | |
358 | } | |
359 | ||
360 | if (options->queue_size != prev_options->queue_size) { | |
361 | fio_blkio_log_opt_compat_err("libblkio_queue_size"); | |
362 | return 1; | |
363 | } | |
364 | ||
365 | if (!possibly_null_strs_equal(options->pre_start_props, | |
366 | prev_options->pre_start_props)) { | |
367 | fio_blkio_log_opt_compat_err("libblkio_pre_start_props"); | |
368 | return 1; | |
369 | } | |
370 | ||
371 | return 0; | |
372 | } | |
373 | ||
a601337a AF |
374 | static int fio_blkio_create_and_connect(struct thread_data *td, |
375 | struct blkio **out_blkio) | |
376 | { | |
377 | const struct fio_blkio_options *options = td->eo; | |
378 | struct blkio *b; | |
379 | int ret; | |
380 | ||
381 | if (!options->driver) { | |
382 | log_err("fio: engine libblkio requires option libblkio_driver to be set\n"); | |
383 | return 1; | |
384 | } | |
385 | ||
386 | if (blkio_create(options->driver, &b) != 0) { | |
387 | fio_blkio_log_err(blkio_create); | |
388 | return 1; | |
389 | } | |
390 | ||
391 | /* don't fail if driver doesn't have a "direct" property */ | |
392 | ret = blkio_set_bool(b, "direct", td->o.odirect); | |
393 | if (ret != 0 && ret != -ENOENT) { | |
394 | fio_blkio_log_err(blkio_set_bool); | |
395 | goto err_blkio_destroy; | |
396 | } | |
397 | ||
398 | if (blkio_set_bool(b, "read-only", read_only) != 0) { | |
399 | fio_blkio_log_err(blkio_set_bool); | |
400 | goto err_blkio_destroy; | |
401 | } | |
402 | ||
13fffdfb AF |
403 | if (options->path) { |
404 | if (blkio_set_str(b, "path", options->path) != 0) { | |
405 | fio_blkio_log_err(blkio_set_str); | |
406 | goto err_blkio_destroy; | |
407 | } | |
408 | } | |
409 | ||
a601337a AF |
410 | if (fio_blkio_set_props_from_str(b, "libblkio_pre_connect_props", |
411 | options->pre_connect_props) != 0) | |
412 | goto err_blkio_destroy; | |
413 | ||
414 | if (blkio_connect(b) != 0) { | |
415 | fio_blkio_log_err(blkio_connect); | |
416 | goto err_blkio_destroy; | |
417 | } | |
418 | ||
13fffdfb AF |
419 | if (options->num_entries != 0) { |
420 | if (blkio_set_int(b, "num-entries", | |
421 | options->num_entries) != 0) { | |
422 | fio_blkio_log_err(blkio_set_int); | |
423 | goto err_blkio_destroy; | |
424 | } | |
425 | } | |
426 | ||
427 | if (options->queue_size != 0) { | |
428 | if (blkio_set_int(b, "queue-size", options->queue_size) != 0) { | |
429 | fio_blkio_log_err(blkio_set_int); | |
430 | goto err_blkio_destroy; | |
431 | } | |
432 | } | |
433 | ||
a601337a AF |
434 | if (fio_blkio_set_props_from_str(b, "libblkio_pre_start_props", |
435 | options->pre_start_props) != 0) | |
436 | goto err_blkio_destroy; | |
437 | ||
438 | *out_blkio = b; | |
439 | return 0; | |
440 | ||
441 | err_blkio_destroy: | |
442 | blkio_destroy(&b); | |
443 | return 1; | |
444 | } | |
445 | ||
3afc2d8a AF |
446 | static bool incompatible_threaded_subjob_options = false; |
447 | ||
a601337a AF |
448 | /* |
449 | * This callback determines the device/file size, so it creates and connects a | |
450 | * blkio instance. But it is invoked from the main thread in the original fio | |
451 | * process, not from the processes in which jobs will actually run. It thus | |
452 | * subsequently destroys the blkio, which is recreated in the init() callback. | |
453 | */ | |
454 | static int fio_blkio_setup(struct thread_data *td) | |
455 | { | |
b158577d | 456 | const struct fio_blkio_options *options = td->eo; |
a601337a AF |
457 | struct blkio *b; |
458 | int ret = 0; | |
459 | uint64_t capacity; | |
460 | ||
461 | assert(td->files_index == 1); | |
462 | ||
3afc2d8a AF |
463 | if (fio_blkio_check_opt_compat(td) != 0) { |
464 | incompatible_threaded_subjob_options = true; | |
465 | return 1; | |
466 | } | |
467 | ||
b158577d AF |
468 | if (options->hipri && |
469 | options->wait_mode == FIO_BLKIO_WAIT_MODE_EVENTFD) { | |
470 | log_err("fio: option hipri is incompatible with option libblkio_wait_mode=eventfd\n"); | |
471 | return 1; | |
472 | } | |
473 | ||
b1bd09b5 AF |
474 | if (options->hipri && options->force_enable_completion_eventfd) { |
475 | log_err("fio: option hipri is incompatible with option libblkio_force_enable_completion_eventfd\n"); | |
476 | return 1; | |
477 | } | |
478 | ||
a601337a AF |
479 | if (fio_blkio_create_and_connect(td, &b) != 0) |
480 | return 1; | |
481 | ||
482 | if (blkio_get_uint64(b, "capacity", &capacity) != 0) { | |
483 | fio_blkio_log_err(blkio_get_uint64); | |
484 | ret = 1; | |
485 | goto out_blkio_destroy; | |
486 | } | |
487 | ||
488 | td->files[0]->real_file_size = capacity; | |
489 | fio_file_set_size_known(td->files[0]); | |
490 | ||
491 | out_blkio_destroy: | |
492 | blkio_destroy(&b); | |
493 | return ret; | |
494 | } | |
495 | ||
496 | static int fio_blkio_init(struct thread_data *td) | |
497 | { | |
a870d6ff | 498 | const struct fio_blkio_options *options = td->eo; |
a601337a | 499 | struct fio_blkio_data *data; |
b158577d | 500 | int flags; |
a601337a | 501 | |
3afc2d8a AF |
502 | if (td->o.use_thread && incompatible_threaded_subjob_options) { |
503 | /* | |
504 | * Different subjobs using option 'thread' specified | |
505 | * incompatible options. We don't know which configuration | |
506 | * should win, so we just fail all such subjobs. | |
507 | */ | |
508 | return 1; | |
509 | } | |
510 | ||
a601337a AF |
511 | /* |
512 | * Request enqueueing is fast, and it's not possible to know exactly | |
513 | * when a request is submitted, so never report submission latencies. | |
514 | */ | |
515 | td->o.disable_slat = 1; | |
516 | ||
517 | data = calloc(1, sizeof(*data)); | |
518 | if (!data) { | |
519 | log_err("fio: calloc() failed\n"); | |
520 | return 1; | |
521 | } | |
522 | ||
6dd4291c | 523 | data->iovecs = calloc(td->o.iodepth, sizeof(data->iovecs[0])); |
a601337a | 524 | data->completions = calloc(td->o.iodepth, sizeof(data->completions[0])); |
6dd4291c | 525 | if (!data->iovecs || !data->completions) { |
a601337a AF |
526 | log_err("fio: calloc() failed\n"); |
527 | goto err_free; | |
528 | } | |
529 | ||
3afc2d8a | 530 | fio_blkio_proc_lock(); |
a601337a | 531 | |
3afc2d8a AF |
532 | if (proc_state.initted_threads == 0) { |
533 | /* initialize per-process blkio */ | |
534 | int num_queues, num_poll_queues; | |
a870d6ff | 535 | |
3afc2d8a AF |
536 | if (td->o.use_thread) { |
537 | num_queues = total_threaded_subjobs(false); | |
538 | num_poll_queues = total_threaded_subjobs(true); | |
539 | } else { | |
540 | num_queues = options->hipri ? 0 : 1; | |
541 | num_poll_queues = options->hipri ? 1 : 0; | |
542 | } | |
a601337a | 543 | |
3afc2d8a AF |
544 | if (fio_blkio_create_and_connect(td, &proc_state.b) != 0) |
545 | goto err_unlock; | |
546 | ||
547 | if (blkio_set_int(proc_state.b, "num-queues", | |
548 | num_queues) != 0) { | |
549 | fio_blkio_log_err(blkio_set_int); | |
550 | goto err_blkio_destroy; | |
551 | } | |
552 | ||
553 | if (blkio_set_int(proc_state.b, "num-poll-queues", | |
554 | num_poll_queues) != 0) { | |
555 | fio_blkio_log_err(blkio_set_int); | |
556 | goto err_blkio_destroy; | |
557 | } | |
558 | ||
559 | if (blkio_start(proc_state.b) != 0) { | |
560 | fio_blkio_log_err(blkio_start); | |
561 | goto err_blkio_destroy; | |
562 | } | |
a601337a AF |
563 | } |
564 | ||
3afc2d8a AF |
565 | if (options->hipri) { |
566 | int i = proc_state.initted_hipri_threads; | |
567 | data->q = blkio_get_poll_queue(proc_state.b, i); | |
568 | } else { | |
569 | int i = proc_state.initted_threads - | |
570 | proc_state.initted_hipri_threads; | |
571 | data->q = blkio_get_queue(proc_state.b, i); | |
572 | } | |
a601337a | 573 | |
b1bd09b5 AF |
574 | if (options->wait_mode == FIO_BLKIO_WAIT_MODE_EVENTFD || |
575 | options->force_enable_completion_eventfd) { | |
b158577d AF |
576 | /* enable completion fd and make it blocking */ |
577 | blkioq_set_completion_fd_enabled(data->q, true); | |
578 | data->completion_fd = blkioq_get_completion_fd(data->q); | |
579 | ||
580 | flags = fcntl(data->completion_fd, F_GETFL); | |
581 | if (flags < 0) { | |
582 | log_err("fio: fcntl(F_GETFL) failed: %s\n", | |
583 | strerror(errno)); | |
584 | goto err_blkio_destroy; | |
585 | } | |
586 | ||
587 | if (fcntl(data->completion_fd, F_SETFL, | |
588 | flags & ~O_NONBLOCK) != 0) { | |
589 | log_err("fio: fcntl(F_SETFL) failed: %s\n", | |
590 | strerror(errno)); | |
591 | goto err_blkio_destroy; | |
592 | } | |
593 | } else { | |
594 | data->completion_fd = -1; | |
595 | } | |
596 | ||
3afc2d8a AF |
597 | ++proc_state.initted_threads; |
598 | if (options->hipri) | |
599 | ++proc_state.initted_hipri_threads; | |
600 | ||
a601337a AF |
601 | /* Set data last so cleanup() does nothing if init() fails. */ |
602 | td->io_ops_data = data; | |
603 | ||
3afc2d8a AF |
604 | fio_blkio_proc_unlock(); |
605 | ||
a601337a AF |
606 | return 0; |
607 | ||
608 | err_blkio_destroy: | |
3afc2d8a AF |
609 | if (proc_state.initted_threads == 0) |
610 | blkio_destroy(&proc_state.b); | |
611 | err_unlock: | |
612 | if (proc_state.initted_threads == 0) | |
613 | proc_state.b = NULL; | |
614 | fio_blkio_proc_unlock(); | |
a601337a AF |
615 | err_free: |
616 | free(data->completions); | |
6dd4291c | 617 | free(data->iovecs); |
a601337a AF |
618 | free(data); |
619 | return 1; | |
620 | } | |
621 | ||
ef9b6f2f AF |
622 | static int fio_blkio_post_init(struct thread_data *td) |
623 | { | |
624 | struct fio_blkio_data *data = td->io_ops_data; | |
625 | ||
626 | if (!data->has_mem_region) { | |
627 | /* | |
628 | * Memory was allocated by the fio core and not iomem_alloc(), | |
629 | * so we need to register it as a memory region here. | |
630 | * | |
631 | * `td->orig_buffer_size` is computed like `len` below, but then | |
632 | * fio can add some padding to it to make sure it is | |
633 | * sufficiently aligned to the page size and the mem_align | |
634 | * option. However, this can make it become unaligned to the | |
635 | * "mem-region-alignment" property in ways that the user can't | |
636 | * control, so we essentially recompute `td->orig_buffer_size` | |
637 | * here but without adding that padding. | |
638 | */ | |
639 | ||
640 | unsigned long long max_block_size; | |
641 | struct blkio_mem_region region; | |
642 | ||
643 | max_block_size = max(td->o.max_bs[DDIR_READ], | |
644 | max(td->o.max_bs[DDIR_WRITE], | |
645 | td->o.max_bs[DDIR_TRIM])); | |
646 | ||
647 | region = (struct blkio_mem_region) { | |
648 | .addr = td->orig_buffer, | |
649 | .len = (size_t)max_block_size * | |
650 | (size_t)td->o.iodepth, | |
651 | .fd = -1, | |
652 | }; | |
653 | ||
3afc2d8a | 654 | if (blkio_map_mem_region(proc_state.b, ®ion) != 0) { |
ef9b6f2f AF |
655 | fio_blkio_log_err(blkio_map_mem_region); |
656 | return 1; | |
657 | } | |
658 | } | |
659 | ||
660 | return 0; | |
661 | } | |
662 | ||
a601337a AF |
663 | static void fio_blkio_cleanup(struct thread_data *td) |
664 | { | |
665 | struct fio_blkio_data *data = td->io_ops_data; | |
666 | ||
3afc2d8a AF |
667 | /* |
668 | * Subjobs from different jobs can be terminated at different times, so | |
669 | * this callback may be invoked for one subjob while another is still | |
670 | * doing I/O. Those subjobs may share the process, so we must wait until | |
671 | * the last subjob in the process wants to clean up to actually destroy | |
672 | * the blkio. | |
673 | */ | |
674 | ||
a601337a | 675 | if (data) { |
a601337a | 676 | free(data->completions); |
6dd4291c | 677 | free(data->iovecs); |
a601337a | 678 | free(data); |
3afc2d8a AF |
679 | |
680 | fio_blkio_proc_lock(); | |
681 | if (--proc_state.initted_threads == 0) { | |
682 | blkio_destroy(&proc_state.b); | |
683 | proc_state.b = NULL; | |
684 | } | |
685 | fio_blkio_proc_unlock(); | |
a601337a AF |
686 | } |
687 | } | |
688 | ||
689 | #define align_up(x, y) ((((x) + (y) - 1) / (y)) * (y)) | |
690 | ||
691 | static int fio_blkio_iomem_alloc(struct thread_data *td, size_t size) | |
692 | { | |
693 | struct fio_blkio_data *data = td->io_ops_data; | |
694 | int ret; | |
695 | uint64_t mem_region_alignment; | |
696 | ||
3afc2d8a | 697 | if (blkio_get_uint64(proc_state.b, "mem-region-alignment", |
a601337a AF |
698 | &mem_region_alignment) != 0) { |
699 | fio_blkio_log_err(blkio_get_uint64); | |
700 | return 1; | |
701 | } | |
702 | ||
703 | /* round up size to satisfy mem-region-alignment */ | |
704 | size = align_up(size, (size_t)mem_region_alignment); | |
705 | ||
3afc2d8a AF |
706 | fio_blkio_proc_lock(); |
707 | ||
708 | if (blkio_alloc_mem_region(proc_state.b, &data->mem_region, | |
709 | size) != 0) { | |
a601337a AF |
710 | fio_blkio_log_err(blkio_alloc_mem_region); |
711 | ret = 1; | |
712 | goto out; | |
713 | } | |
714 | ||
3afc2d8a | 715 | if (blkio_map_mem_region(proc_state.b, &data->mem_region) != 0) { |
a601337a AF |
716 | fio_blkio_log_err(blkio_map_mem_region); |
717 | ret = 1; | |
718 | goto out_free; | |
719 | } | |
720 | ||
721 | td->orig_buffer = data->mem_region.addr; | |
722 | data->has_mem_region = true; | |
723 | ||
724 | ret = 0; | |
725 | goto out; | |
726 | ||
727 | out_free: | |
3afc2d8a | 728 | blkio_free_mem_region(proc_state.b, &data->mem_region); |
a601337a | 729 | out: |
3afc2d8a | 730 | fio_blkio_proc_unlock(); |
a601337a AF |
731 | return ret; |
732 | } | |
733 | ||
734 | static void fio_blkio_iomem_free(struct thread_data *td) | |
735 | { | |
736 | struct fio_blkio_data *data = td->io_ops_data; | |
737 | ||
738 | if (data && data->has_mem_region) { | |
3afc2d8a AF |
739 | fio_blkio_proc_lock(); |
740 | blkio_unmap_mem_region(proc_state.b, &data->mem_region); | |
741 | blkio_free_mem_region(proc_state.b, &data->mem_region); | |
742 | fio_blkio_proc_unlock(); | |
a601337a AF |
743 | |
744 | data->has_mem_region = false; | |
745 | } | |
746 | } | |
747 | ||
748 | static int fio_blkio_open_file(struct thread_data *td, struct fio_file *f) | |
749 | { | |
750 | return 0; | |
751 | } | |
752 | ||
753 | static enum fio_q_status fio_blkio_queue(struct thread_data *td, | |
754 | struct io_u *io_u) | |
755 | { | |
6dd4291c | 756 | const struct fio_blkio_options *options = td->eo; |
a601337a AF |
757 | struct fio_blkio_data *data = td->io_ops_data; |
758 | ||
759 | fio_ro_check(td, io_u); | |
760 | ||
761 | switch (io_u->ddir) { | |
762 | case DDIR_READ: | |
6dd4291c AF |
763 | if (options->vectored) { |
764 | struct iovec *iov = &data->iovecs[io_u->index]; | |
765 | iov->iov_base = io_u->xfer_buf; | |
766 | iov->iov_len = (size_t)io_u->xfer_buflen; | |
767 | ||
768 | blkioq_readv(data->q, io_u->offset, iov, 1, | |
769 | io_u, 0); | |
770 | } else { | |
771 | blkioq_read(data->q, io_u->offset, | |
772 | io_u->xfer_buf, | |
773 | (size_t)io_u->xfer_buflen, io_u, 0); | |
774 | } | |
a601337a AF |
775 | break; |
776 | case DDIR_WRITE: | |
6dd4291c AF |
777 | if (options->vectored) { |
778 | struct iovec *iov = &data->iovecs[io_u->index]; | |
779 | iov->iov_base = io_u->xfer_buf; | |
780 | iov->iov_len = (size_t)io_u->xfer_buflen; | |
781 | ||
782 | blkioq_writev(data->q, io_u->offset, iov, 1, | |
783 | io_u, 0); | |
784 | } else { | |
785 | blkioq_write(data->q, io_u->offset, | |
786 | io_u->xfer_buf, | |
787 | (size_t)io_u->xfer_buflen, io_u, | |
788 | 0); | |
789 | } | |
a601337a AF |
790 | break; |
791 | case DDIR_TRIM: | |
464981ff AF |
792 | if (options->write_zeroes_on_trim) { |
793 | blkioq_write_zeroes(data->q, io_u->offset, | |
794 | io_u->xfer_buflen, io_u, 0); | |
795 | } else { | |
796 | blkioq_discard(data->q, io_u->offset, | |
797 | io_u->xfer_buflen, io_u, 0); | |
798 | } | |
a601337a AF |
799 | break; |
800 | case DDIR_SYNC: | |
801 | case DDIR_DATASYNC: | |
802 | blkioq_flush(data->q, io_u, 0); | |
803 | break; | |
804 | default: | |
805 | io_u->error = ENOTSUP; | |
806 | io_u_log_error(td, io_u); | |
807 | return FIO_Q_COMPLETED; | |
808 | } | |
809 | ||
810 | return FIO_Q_QUEUED; | |
811 | } | |
812 | ||
813 | static int fio_blkio_getevents(struct thread_data *td, unsigned int min, | |
814 | unsigned int max, const struct timespec *t) | |
815 | { | |
b158577d | 816 | const struct fio_blkio_options *options = td->eo; |
a601337a | 817 | struct fio_blkio_data *data = td->io_ops_data; |
b158577d AF |
818 | int ret, n; |
819 | uint64_t event; | |
820 | ||
821 | switch (options->wait_mode) { | |
822 | case FIO_BLKIO_WAIT_MODE_BLOCK: | |
823 | n = blkioq_do_io(data->q, data->completions, (int)min, (int)max, | |
824 | NULL); | |
825 | if (n < 0) { | |
826 | fio_blkio_log_err(blkioq_do_io); | |
827 | return -1; | |
828 | } | |
829 | return n; | |
830 | case FIO_BLKIO_WAIT_MODE_EVENTFD: | |
831 | n = blkioq_do_io(data->q, data->completions, 0, (int)max, NULL); | |
832 | if (n < 0) { | |
833 | fio_blkio_log_err(blkioq_do_io); | |
834 | return -1; | |
835 | } | |
836 | while (n < (int)min) { | |
837 | ret = read(data->completion_fd, &event, sizeof(event)); | |
838 | if (ret != sizeof(event)) { | |
839 | log_err("fio: read() on the completion fd returned %d\n", | |
840 | ret); | |
841 | return -1; | |
842 | } | |
a601337a | 843 | |
b158577d AF |
844 | ret = blkioq_do_io(data->q, data->completions + n, 0, |
845 | (int)max - n, NULL); | |
846 | if (ret < 0) { | |
847 | fio_blkio_log_err(blkioq_do_io); | |
848 | return -1; | |
849 | } | |
850 | ||
851 | n += ret; | |
852 | } | |
853 | return n; | |
854 | case FIO_BLKIO_WAIT_MODE_LOOP: | |
855 | for (n = 0; n < (int)min; ) { | |
856 | ret = blkioq_do_io(data->q, data->completions + n, 0, | |
857 | (int)max - n, NULL); | |
858 | if (ret < 0) { | |
859 | fio_blkio_log_err(blkioq_do_io); | |
860 | return -1; | |
861 | } | |
862 | ||
863 | n += ret; | |
864 | } | |
865 | return n; | |
866 | default: | |
a601337a AF |
867 | return -1; |
868 | } | |
a601337a AF |
869 | } |
870 | ||
871 | static struct io_u *fio_blkio_event(struct thread_data *td, int event) | |
872 | { | |
873 | struct fio_blkio_data *data = td->io_ops_data; | |
874 | struct blkio_completion *completion = &data->completions[event]; | |
875 | struct io_u *io_u = completion->user_data; | |
876 | ||
877 | io_u->error = -completion->ret; | |
878 | ||
879 | return io_u; | |
880 | } | |
881 | ||
882 | FIO_STATIC struct ioengine_ops ioengine = { | |
883 | .name = "libblkio", | |
884 | .version = FIO_IOOPS_VERSION, | |
885 | .flags = FIO_DISKLESSIO | FIO_NOEXTEND | | |
ef9b6f2f | 886 | FIO_NO_OFFLOAD | FIO_SKIPPABLE_IOMEM_ALLOC, |
a601337a AF |
887 | |
888 | .setup = fio_blkio_setup, | |
889 | .init = fio_blkio_init, | |
ef9b6f2f | 890 | .post_init = fio_blkio_post_init, |
a601337a AF |
891 | .cleanup = fio_blkio_cleanup, |
892 | ||
893 | .iomem_alloc = fio_blkio_iomem_alloc, | |
894 | .iomem_free = fio_blkio_iomem_free, | |
895 | ||
896 | .open_file = fio_blkio_open_file, | |
897 | ||
898 | .queue = fio_blkio_queue, | |
899 | .getevents = fio_blkio_getevents, | |
900 | .event = fio_blkio_event, | |
901 | ||
902 | .options = options, | |
903 | .option_struct_size = sizeof(struct fio_blkio_options), | |
904 | }; | |
905 | ||
906 | static void fio_init fio_blkio_register(void) | |
907 | { | |
908 | register_ioengine(&ioengine); | |
909 | } | |
910 | ||
911 | static void fio_exit fio_blkio_unregister(void) | |
912 | { | |
913 | unregister_ioengine(&ioengine); | |
914 | } |