Merge branch 'sprandom-log-fix' of https://github.com/tomas-winkler-sndk/fio
[fio.git] / io_u.c
CommitLineData
10ba535a 1#include <unistd.h>
10ba535a 2#include <string.h>
0c6e7517 3#include <assert.h>
10ba535a
JA
4
5#include "fio.h"
4f5af7b2 6#include "verify.h"
0d29de83 7#include "trim.h"
1fbbf72e 8#include "lib/rand.h"
7ebd796f 9#include "lib/axmap.h"
002fe734 10#include "err.h"
0f38bbef 11#include "lib/pow2.h"
4b157ac6 12#include "minmax.h"
bfbdd35b 13#include "zbd.h"
40c482b5 14#include "sprandom.h"
10ba535a 15
97601024
JA
16struct io_completion_data {
17 int nr; /* input */
97601024
JA
18
19 int error; /* output */
100f49f1 20 uint64_t bytes_done[DDIR_RWDIR_CNT]; /* output */
8b6a404c 21 struct timespec time; /* output */
97601024
JA
22};
23
10ba535a 24/*
7ebd796f 25 * The ->io_axmap contains a map of blocks we have or have not done io
10ba535a
JA
26 * to yet. Used to make sure we cover the entire range in a fair fashion.
27 */
e39c0676 28static bool random_map_free(struct fio_file *f, const uint64_t block)
10ba535a 29{
7ebd796f 30 return !axmap_isset(f->io_axmap, block);
10ba535a
JA
31}
32
df415585
JA
33/*
34 * Mark a given offset as used in the map.
35 */
6cc1a3d1
BVA
36static uint64_t mark_random_map(struct thread_data *td, struct io_u *io_u,
37 uint64_t offset, uint64_t buflen)
df415585 38{
5fff9543 39 unsigned long long min_bs = td->o.min_bs[io_u->ddir];
9bf2061e 40 struct fio_file *f = io_u->file;
5fff9543 41 unsigned long long nr_blocks;
1ae83d45 42 uint64_t block;
df415585 43
6cc1a3d1
BVA
44 block = (offset - f->file_offset) / (uint64_t) min_bs;
45 nr_blocks = (buflen + min_bs - 1) / min_bs;
bd6b959a 46 assert(nr_blocks > 0);
df415585 47
bd6b959a 48 if (!(io_u->flags & IO_U_F_BUSY_OK)) {
7ebd796f 49 nr_blocks = axmap_set_nr(f->io_axmap, block, nr_blocks);
bd6b959a
BVA
50 assert(nr_blocks > 0);
51 }
df415585 52
6cc1a3d1
BVA
53 if ((nr_blocks * min_bs) < buflen)
54 buflen = nr_blocks * min_bs;
55
56 return buflen;
df415585
JA
57}
58
74776733
JA
59static uint64_t last_block(struct thread_data *td, struct fio_file *f,
60 enum fio_ddir ddir)
2ba1c290 61{
74776733
JA
62 uint64_t max_blocks;
63 uint64_t max_size;
2ba1c290 64
ff58fced
JA
65 assert(ddir_rw(ddir));
66
d9dd70f7
JA
67 /*
68 * Hmm, should we make sure that ->io_size <= ->real_file_size?
79591fa9 69 * -> not for now since there is code assuming it could go either.
d9dd70f7
JA
70 */
71 max_size = f->io_size;
72 if (max_size > f->real_file_size)
73 max_size = f->real_file_size;
74
7b865a2f 75 if (td->o.zone_mode == ZONE_MODE_STRIDED && td->o.zone_range)
ed335855
SN
76 max_size = td->o.zone_range;
77
0412d12e
JE
78 if (td->o.min_bs[ddir] > td->o.ba[ddir])
79 max_size -= td->o.min_bs[ddir] - td->o.ba[ddir];
80
1ae83d45 81 max_blocks = max_size / (uint64_t) td->o.ba[ddir];
2ba1c290
JA
82 if (!max_blocks)
83 return 0;
84
67778e88 85 return max_blocks;
2ba1c290
JA
86}
87
40c482b5
TW
88
89static int __get_next_rand_offset_sprandom(struct thread_data *td, struct fio_file *f,
90 enum fio_ddir ddir, uint64_t *b,
91 uint64_t lastb)
92{
93 assert(ddir == DDIR_WRITE);
94
95 /* SP RANDOM writes all addresses once */
96 if (sprandom_get_next_offset(f->spr_info, f, b)) {
97 dprint(FD_SPRANDOM, "sprandom is done\n");
98 td->done = 1;
99 return 1;
100 }
101 return 0;
102}
103
e25839d4 104static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
e0a04ac1
JA
105 enum fio_ddir ddir, uint64_t *b,
106 uint64_t lastb)
ec4015da 107{
6f49f8bc 108 uint64_t r;
5e0baa7f 109
c3546b53
JA
110 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE ||
111 td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64) {
6f49f8bc 112
d6b72507 113 r = __rand(&td->random_state);
8055e41d 114
4b91ee8f 115 dprint(FD_RANDOM, "off rand %llu\n", (unsigned long long) r);
8055e41d 116
e0a04ac1 117 *b = lastb * (r / (rand_max(&td->random_state) + 1.0));
51ede0b1 118 } else {
8055e41d 119 uint64_t off = 0;
43c63a78 120
967d1b63
JA
121 assert(fio_file_lfsr(f));
122
6f49f8bc 123 if (lfsr_next(&f->lfsr, &off))
8055e41d 124 return 1;
ec4015da 125
8055e41d
JA
126 *b = off;
127 }
0ce8b119 128
ec4015da 129 /*
51ede0b1 130 * if we are not maintaining a random map, we are done.
ec4015da 131 */
51ede0b1
JA
132 if (!file_randommap(td, f))
133 goto ret;
43c63a78
JA
134
135 /*
51ede0b1 136 * calculate map offset and check if it's free
43c63a78 137 */
51ede0b1
JA
138 if (random_map_free(f, *b))
139 goto ret;
140
4b91ee8f
JA
141 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
142 (unsigned long long) *b);
51ede0b1 143
7ebd796f 144 *b = axmap_next_free(f->io_axmap, *b);
51ede0b1
JA
145 if (*b == (uint64_t) -1ULL)
146 return 1;
0ce8b119
JA
147ret:
148 return 0;
ec4015da
JA
149}
150
925fee33
JA
151static int __get_next_rand_offset_zipf(struct thread_data *td,
152 struct fio_file *f, enum fio_ddir ddir,
1ae83d45 153 uint64_t *b)
e25839d4 154{
9c6f6316 155 *b = zipf_next(&f->zipf);
e25839d4
JA
156 return 0;
157}
158
925fee33
JA
159static int __get_next_rand_offset_pareto(struct thread_data *td,
160 struct fio_file *f, enum fio_ddir ddir,
1ae83d45 161 uint64_t *b)
925fee33 162{
9c6f6316 163 *b = pareto_next(&f->zipf);
925fee33
JA
164 return 0;
165}
166
56d9fa4b
JA
167static int __get_next_rand_offset_gauss(struct thread_data *td,
168 struct fio_file *f, enum fio_ddir ddir,
169 uint64_t *b)
170{
171 *b = gauss_next(&f->gauss);
172 return 0;
173}
174
59466396
JA
175static int __get_next_rand_offset_zoned_abs(struct thread_data *td,
176 struct fio_file *f,
177 enum fio_ddir ddir, uint64_t *b)
178{
179 struct zone_split_index *zsi;
e3c8c108 180 uint64_t lastb, send, stotal;
59466396
JA
181 unsigned int v;
182
183 lastb = last_block(td, f, ddir);
184 if (!lastb)
185 return 1;
186
187 if (!td->o.zone_split_nr[ddir]) {
188bail:
189 return __get_next_rand_offset(td, f, ddir, b, lastb);
190 }
191
192 /*
193 * Generate a value, v, between 1 and 100, both inclusive
194 */
1bd5d213 195 v = rand_between(&td->zone_state, 1, 100);
59466396 196
e3c8c108
JA
197 /*
198 * Find our generated table. 'send' is the end block of this zone,
199 * 'stotal' is our start offset.
200 */
59466396
JA
201 zsi = &td->zone_state_index[ddir][v - 1];
202 stotal = zsi->size_prev / td->o.ba[ddir];
203 send = zsi->size / td->o.ba[ddir];
204
205 /*
206 * Should never happen
207 */
208 if (send == -1U) {
264e3d30 209 if (!fio_did_warn(FIO_WARN_ZONED_BUG))
59466396 210 log_err("fio: bug in zoned generation\n");
59466396
JA
211 goto bail;
212 } else if (send > lastb) {
213 /*
214 * This happens if the user specifies ranges that exceed
215 * the file/device size. We can't handle that gracefully,
216 * so error and exit.
217 */
218 log_err("fio: zoned_abs sizes exceed file size\n");
219 return 1;
220 }
221
222 /*
e3c8c108 223 * Generate index from 0..send-stotal
59466396 224 */
e3c8c108 225 if (__get_next_rand_offset(td, f, ddir, b, send - stotal) == 1)
59466396
JA
226 return 1;
227
e3c8c108 228 *b += stotal;
59466396
JA
229 return 0;
230}
231
e0a04ac1
JA
232static int __get_next_rand_offset_zoned(struct thread_data *td,
233 struct fio_file *f, enum fio_ddir ddir,
234 uint64_t *b)
235{
236 unsigned int v, send, stotal;
237 uint64_t offset, lastb;
e0a04ac1
JA
238 struct zone_split_index *zsi;
239
240 lastb = last_block(td, f, ddir);
241 if (!lastb)
242 return 1;
243
244 if (!td->o.zone_split_nr[ddir]) {
245bail:
246 return __get_next_rand_offset(td, f, ddir, b, lastb);
247 }
248
249 /*
250 * Generate a value, v, between 1 and 100, both inclusive
251 */
1bd5d213 252 v = rand_between(&td->zone_state, 1, 100);
e0a04ac1
JA
253
254 zsi = &td->zone_state_index[ddir][v - 1];
255 stotal = zsi->size_perc_prev;
256 send = zsi->size_perc;
257
258 /*
259 * Should never happen
260 */
261 if (send == -1U) {
264e3d30 262 if (!fio_did_warn(FIO_WARN_ZONED_BUG))
e0a04ac1 263 log_err("fio: bug in zoned generation\n");
e0a04ac1
JA
264 goto bail;
265 }
266
267 /*
268 * 'send' is some percentage below or equal to 100 that
269 * marks the end of the current IO range. 'stotal' marks
270 * the start, in percent.
271 */
272 if (stotal)
273 offset = stotal * lastb / 100ULL;
274 else
275 offset = 0;
276
277 lastb = lastb * (send - stotal) / 100ULL;
278
279 /*
280 * Generate index from 0..send-of-lastb
281 */
282 if (__get_next_rand_offset(td, f, ddir, b, lastb) == 1)
283 return 1;
284
285 /*
286 * Add our start offset, if any
287 */
288 if (offset)
289 *b += offset;
290
291 return 0;
292}
56d9fa4b 293
f31feaa2
JA
294static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
295 enum fio_ddir ddir, uint64_t *b)
e25839d4 296{
40c482b5
TW
297 if (td->o.sprandom && ddir == DDIR_WRITE) {
298 return __get_next_rand_offset_sprandom(td, f, ddir, b, 0);
299 } else if (td->o.random_distribution == FIO_RAND_DIST_RANDOM) {
e0a04ac1
JA
300 uint64_t lastb;
301
302 lastb = last_block(td, f, ddir);
303 if (!lastb)
304 return 1;
305
306 return __get_next_rand_offset(td, f, ddir, b, lastb);
307 } else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
e25839d4 308 return __get_next_rand_offset_zipf(td, f, ddir, b);
925fee33
JA
309 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
310 return __get_next_rand_offset_pareto(td, f, ddir, b);
56d9fa4b
JA
311 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
312 return __get_next_rand_offset_gauss(td, f, ddir, b);
e0a04ac1
JA
313 else if (td->o.random_distribution == FIO_RAND_DIST_ZONED)
314 return __get_next_rand_offset_zoned(td, f, ddir, b);
59466396
JA
315 else if (td->o.random_distribution == FIO_RAND_DIST_ZONED_ABS)
316 return __get_next_rand_offset_zoned_abs(td, f, ddir, b);
e25839d4
JA
317
318 log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
319 return 1;
320}
321
e39c0676 322static bool should_do_random(struct thread_data *td, enum fio_ddir ddir)
211c9b89
JA
323{
324 unsigned int v;
211c9b89 325
d9472271 326 if (td->o.perc_rand[ddir] == 100)
e39c0676 327 return true;
211c9b89 328
1bd5d213 329 v = rand_between(&td->seq_rand_state[ddir], 1, 100);
211c9b89 330
d9472271 331 return v <= td->o.perc_rand[ddir];
211c9b89
JA
332}
333
0bcf41cd
JA
334static void loop_cache_invalidate(struct thread_data *td, struct fio_file *f)
335{
336 struct thread_options *o = &td->o;
337
338 if (o->invalidate_cache && !o->odirect) {
339 int fio_unused ret;
340
341 ret = file_invalidate_cache(td, f);
342 }
343}
344
38dad62d 345static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
1ae83d45 346 enum fio_ddir ddir, uint64_t *b)
38dad62d 347{
c04e4661
DE
348 if (!get_next_rand_offset(td, f, ddir, b))
349 return 0;
350
8c07860d
JA
351 if (td->o.time_based ||
352 (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)) {
33c48814 353 fio_file_reset(td, f);
80f02150 354 loop_cache_invalidate(td, f);
c04e4661
DE
355 if (!get_next_rand_offset(td, f, ddir, b))
356 return 0;
38dad62d
JA
357 }
358
c04e4661 359 dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
f1dfb668 360 f->file_name, (unsigned long long) f->last_pos[ddir],
4b91ee8f 361 (unsigned long long) f->real_file_size);
c04e4661 362 return 1;
38dad62d
JA
363}
364
37cf9e3c 365static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
1ae83d45 366 enum fio_ddir ddir, uint64_t *offset)
38dad62d 367{
ac002339
JA
368 struct thread_options *o = &td->o;
369
ff58fced
JA
370 assert(ddir_rw(ddir));
371
17373ce2
JA
372 /*
373 * If we reach the end for a time based run, reset us back to 0
374 * and invalidate the cache, if we need to.
375 */
f1dfb668 376 if (f->last_pos[ddir] >= f->io_size + get_start_offset(td, f) &&
46ec8270 377 o->time_based && o->nr_files == 1) {
c89daa4a 378 f->last_pos[ddir] = f->file_offset;
0bcf41cd 379 loop_cache_invalidate(td, f);
19ddc35b 380 }
c04e4661 381
d782b76f
CZ
382 /*
383 * If we reach the end for a rw-io-size based run, reset us back to 0
384 * and invalidate the cache, if we need to.
385 */
386 if (td_rw(td) && o->io_size > o->size) {
387 if (f->last_pos[ddir] >= f->io_size + get_start_offset(td, f)) {
388 f->last_pos[ddir] = f->file_offset;
389 loop_cache_invalidate(td, f);
390 }
391 }
392
f1dfb668 393 if (f->last_pos[ddir] < f->real_file_size) {
1ae83d45 394 uint64_t pos;
059b0802 395
69b98f11
JA
396 /*
397 * Only rewind if we already hit the end
398 */
399 if (f->last_pos[ddir] == f->file_offset &&
400 f->file_offset && o->ddir_seq_add < 0) {
c22825bb
JA
401 if (f->real_file_size > f->io_size)
402 f->last_pos[ddir] = f->io_size;
403 else
404 f->last_pos[ddir] = f->real_file_size;
405 }
a66da7a2 406
f1dfb668 407 pos = f->last_pos[ddir] - f->file_offset;
ac002339
JA
408 if (pos && o->ddir_seq_add) {
409 pos += o->ddir_seq_add;
410
411 /*
412 * If we reach beyond the end of the file
413 * with holed IO, wrap around to the
b0a84f48
JA
414 * beginning again. If we're doing backwards IO,
415 * wrap to the end.
ac002339 416 */
b0a84f48
JA
417 if (pos >= f->real_file_size) {
418 if (o->ddir_seq_add > 0)
419 pos = f->file_offset;
c22825bb
JA
420 else {
421 if (f->real_file_size > f->io_size)
422 pos = f->io_size;
423 else
424 pos = f->real_file_size;
425
426 pos += o->ddir_seq_add;
427 }
b0a84f48 428 }
ac002339 429 }
059b0802 430
37cf9e3c 431 *offset = pos;
38dad62d
JA
432 return 0;
433 }
434
435 return 1;
436}
437
438static int get_next_block(struct thread_data *td, struct io_u *io_u,
6aca9b3d 439 enum fio_ddir ddir, int rw_seq,
ec370c22 440 bool *is_random)
38dad62d
JA
441{
442 struct fio_file *f = io_u->file;
1ae83d45 443 uint64_t b, offset;
38dad62d
JA
444 int ret;
445
ff58fced
JA
446 assert(ddir_rw(ddir));
447
37cf9e3c
JA
448 b = offset = -1ULL;
449
f9f1e137
VF
450 if (td_randtrimwrite(td) && ddir == DDIR_WRITE) {
451 /* don't mark randommap for these writes */
452 io_u_set(td, io_u, IO_U_F_BUSY_OK);
544be3b1 453 offset = f->last_start[DDIR_TRIM] - f->file_offset;
f9f1e137
VF
454 *is_random = true;
455 ret = 0;
456 } else if (rw_seq) {
211c9b89 457 if (td_random(td)) {
6aca9b3d 458 if (should_do_random(td, ddir)) {
211c9b89 459 ret = get_next_rand_block(td, f, ddir, &b);
ec370c22 460 *is_random = true;
6aca9b3d 461 } else {
ec370c22 462 *is_random = false;
1651e431 463 io_u_set(td, io_u, IO_U_F_BUSY_OK);
211c9b89
JA
464 ret = get_next_seq_offset(td, f, ddir, &offset);
465 if (ret)
466 ret = get_next_rand_block(td, f, ddir, &b);
467 }
6aca9b3d 468 } else {
ec370c22 469 *is_random = false;
37cf9e3c 470 ret = get_next_seq_offset(td, f, ddir, &offset);
6aca9b3d 471 }
38dad62d 472 } else {
1651e431 473 io_u_set(td, io_u, IO_U_F_BUSY_OK);
ec370c22 474 *is_random = false;
38dad62d
JA
475
476 if (td->o.rw_seq == RW_SEQ_SEQ) {
37cf9e3c 477 ret = get_next_seq_offset(td, f, ddir, &offset);
6aca9b3d 478 if (ret) {
37cf9e3c 479 ret = get_next_rand_block(td, f, ddir, &b);
ec370c22 480 *is_random = false;
6aca9b3d 481 }
38dad62d 482 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
f1dfb668
JA
483 if (f->last_start[ddir] != -1ULL)
484 offset = f->last_start[ddir] - f->file_offset;
38dad62d 485 else
37cf9e3c 486 offset = 0;
38dad62d
JA
487 ret = 0;
488 } else {
489 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
490 ret = 1;
491 }
492 }
6d68b997 493
37cf9e3c
JA
494 if (!ret) {
495 if (offset != -1ULL)
496 io_u->offset = offset;
497 else if (b != -1ULL)
498 io_u->offset = b * td->o.ba[ddir];
499 else {
4e0a8fa2 500 log_err("fio: bug in offset generation: offset=%llu, b=%llu\n", (unsigned long long) offset, (unsigned long long) b);
37cf9e3c
JA
501 ret = 1;
502 }
4fff54cc 503 io_u->verify_offset = io_u->offset;
37cf9e3c
JA
504 }
505
38dad62d
JA
506 return ret;
507}
508
10ba535a
JA
509/*
510 * For random io, generate a random new block and see if it's used. Repeat
511 * until we find a free one. For sequential io, just return the end of
512 * the last io issued.
513 */
e8fb335e 514static int get_next_offset(struct thread_data *td, struct io_u *io_u,
ec370c22 515 bool *is_random)
10ba535a 516{
9bf2061e 517 struct fio_file *f = io_u->file;
4ba66134 518 enum fio_ddir ddir = io_u->ddir;
38dad62d 519 int rw_seq_hit = 0;
10ba535a 520
ff58fced
JA
521 assert(ddir_rw(ddir));
522
38dad62d
JA
523 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
524 rw_seq_hit = 1;
5736c10d 525 td->ddir_seq_nr = td->o.ddir_seq_nr;
38dad62d 526 }
211097b2 527
6aca9b3d 528 if (get_next_block(td, io_u, ddir, rw_seq_hit, is_random))
38dad62d 529 return 1;
10ba535a 530
009bd847
JA
531 if (io_u->offset >= f->io_size) {
532 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
4b91ee8f
JA
533 (unsigned long long) io_u->offset,
534 (unsigned long long) f->io_size);
009bd847
JA
535 return 1;
536 }
537
538 io_u->offset += f->file_offset;
2ba1c290
JA
539 if (io_u->offset >= f->real_file_size) {
540 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
4b91ee8f
JA
541 (unsigned long long) io_u->offset,
542 (unsigned long long) f->real_file_size);
10ba535a 543 return 1;
2ba1c290 544 }
10ba535a 545
5e0863da
VF
546 /*
547 * For randtrimwrite, we decide whether to issue a trim or a write
548 * based on whether the offsets for the most recent trim and write
549 * operations match. If they don't match that means we just issued a
550 * new trim and the next operation should be a write. If they *do*
551 * match that means we just completed a trim+write pair and the next
552 * command should be a trim.
553 *
554 * This works fine for sequential workloads but for random workloads
555 * it's possible to complete a trim+write pair and then have the next
556 * randomly generated offset match the previous offset. If that happens
557 * we need to alter the offset for the last write operation in order
558 * to ensure that we issue a write operation the next time through.
559 */
560 if (td_randtrimwrite(td) && ddir == DDIR_TRIM &&
561 f->last_start[DDIR_TRIM] == io_u->offset)
793b8686 562 f->last_start[DDIR_WRITE]--;
5e0863da 563
9bf46532 564 io_u->verify_offset = io_u->offset;
10ba535a
JA
565 return 0;
566}
567
e39c0676 568static inline bool io_u_fits(struct thread_data *td, struct io_u *io_u,
5fff9543 569 unsigned long long buflen)
79944128
JA
570{
571 struct fio_file *f = io_u->file;
572
bedc9dc2 573 return io_u->offset + buflen <= f->io_size + get_start_offset(td, f);
79944128
JA
574}
575
5fff9543 576static unsigned long long get_next_buflen(struct thread_data *td, struct io_u *io_u,
ec370c22 577 bool is_random)
10ba535a 578{
6aca9b3d 579 int ddir = io_u->ddir;
5fff9543
JF
580 unsigned long long buflen = 0;
581 unsigned long long minbs, maxbs;
3dd29f7c 582 uint64_t frand_max, r;
7c961359 583 bool power_2;
10ba535a 584
9ee1c647 585 assert(ddir_rw(ddir));
6aca9b3d 586
f9f1e137
VF
587 if (td_randtrimwrite(td) && ddir == DDIR_WRITE) {
588 struct fio_file *f = io_u->file;
589
590 return f->last_pos[DDIR_TRIM] - f->last_start[DDIR_TRIM];
591 }
592
6aca9b3d 593 if (td->o.bs_is_seq_rand)
ec370c22 594 ddir = is_random ? DDIR_WRITE : DDIR_READ;
ff58fced 595
f3059de1
JA
596 minbs = td->o.min_bs[ddir];
597 maxbs = td->o.max_bs[ddir];
598
79944128
JA
599 if (minbs == maxbs)
600 return minbs;
601
52c58027
JA
602 /*
603 * If we can't satisfy the min block size from here, then fail
604 */
605 if (!io_u_fits(td, io_u, minbs))
606 return 0;
607
2f282cec 608 frand_max = rand_max(&td->bsrange_state[ddir]);
79944128 609 do {
2f282cec 610 r = __rand(&td->bsrange_state[ddir]);
4c07ad86 611
720e84ad 612 if (!td->o.bssplit_nr[ddir]) {
5fff9543 613 buflen = minbs + (unsigned long long) ((double) maxbs *
c3546b53 614 (r / (frand_max + 1.0)));
5ec10eaa 615 } else {
3dd29f7c 616 long long perc = 0;
564ca972
JA
617 unsigned int i;
618
720e84ad
JA
619 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
620 struct bssplit *bsp = &td->o.bssplit[ddir][i];
564ca972 621
cc5e4cb0
DLM
622 if (!bsp->perc)
623 continue;
564ca972
JA
624 buflen = bsp->bs;
625 perc += bsp->perc;
3dd29f7c 626 if ((r / perc <= frand_max / 100ULL) &&
79944128 627 io_u_fits(td, io_u, buflen))
564ca972
JA
628 break;
629 }
630 }
79944128 631
17a6b702
PL
632 power_2 = is_power_of_2(minbs);
633 if (!td->o.bs_unaligned && power_2)
7c306bb1 634 buflen &= ~(minbs - 1);
8135fe4d
JA
635 else if (!td->o.bs_unaligned && !power_2)
636 buflen -= buflen % minbs;
637 if (buflen > maxbs)
638 buflen = maxbs;
79944128 639 } while (!io_u_fits(td, io_u, buflen));
6a5e6884 640
10ba535a
JA
641 return buflen;
642}
643
afe24a5a
JA
644static void set_rwmix_bytes(struct thread_data *td)
645{
afe24a5a
JA
646 unsigned int diff;
647
648 /*
649 * we do time or byte based switch. this is needed because
650 * buffered writes may issue a lot quicker than they complete,
651 * whereas reads do not.
652 */
e47f799f 653 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
04c540d9 654 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
e47f799f
JA
655}
656
657static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
658{
659 unsigned int v;
e47f799f 660
1bd5d213 661 v = rand_between(&td->rwmix_state, 1, 100);
4c07ad86 662
04c540d9 663 if (v <= td->o.rwmix[DDIR_READ])
e47f799f
JA
664 return DDIR_READ;
665
666 return DDIR_WRITE;
afe24a5a
JA
667}
668
0d593542 669int io_u_quiesce(struct thread_data *td)
002e7183 670{
2dcfdeb9 671 int ret = 0, completed = 0, err = 0;
0d593542 672
002e7183
JA
673 /*
674 * We are going to sleep, ensure that we flush anything pending as
675 * not to skew our latency numbers.
676 *
677 * Changed to only monitor 'in flight' requests here instead of the
678 * td->cur_depth, b/c td->cur_depth does not accurately represent
679 * io's that have been actually submitted to an async engine,
680 * and cur_depth is meaningless for sync engines.
681 */
a80cb54b
BVA
682 if (td->io_u_queued || td->cur_depth)
683 td_io_commit(td);
9cc80b6d 684
002e7183 685 while (td->io_u_in_flight) {
55312f9f 686 ret = io_u_queued_complete(td, 1);
0d593542
JA
687 if (ret > 0)
688 completed += ret;
d28174f0 689 else if (ret < 0)
2dcfdeb9 690 err = ret;
002e7183 691 }
0d593542 692
6be06c46
JA
693 if (td->flags & TD_F_REGROW_LOGS)
694 regrow_logs(td);
695
d28174f0
JA
696 if (completed)
697 return completed;
698
2dcfdeb9 699 return err;
002e7183
JA
700}
701
581e7141
JA
702static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
703{
704 enum fio_ddir odir = ddir ^ 1;
b5407f8b 705 uint64_t usec;
90eff1c9 706 uint64_t now;
581e7141 707
ff58fced 708 assert(ddir_rw(ddir));
970d9000 709 now = utime_since_now(&td->epoch);
ff58fced 710
50a8ce86
D
711 /*
712 * if rate_next_io_time is in the past, need to catch up to rate
713 */
714 if (td->rate_next_io_time[ddir] <= now)
581e7141
JA
715 return ddir;
716
717 /*
50a8ce86 718 * We are ahead of rate in this direction. See if we
581e7141
JA
719 * should switch.
720 */
315fcfec 721 if (td_rw(td) && td->o.rwmix[odir]) {
581e7141 722 /*
50a8ce86 723 * Other direction is behind rate, switch
581e7141 724 */
50a8ce86 725 if (td->rate_next_io_time[odir] <= now)
581e7141
JA
726 return odir;
727
728 /*
395feabb
JA
729 * Both directions are ahead of rate. sleep the min,
730 * switch if necessary
581e7141 731 */
50a8ce86 732 if (td->rate_next_io_time[ddir] <=
395feabb 733 td->rate_next_io_time[odir]) {
50a8ce86 734 usec = td->rate_next_io_time[ddir] - now;
581e7141 735 } else {
50a8ce86 736 usec = td->rate_next_io_time[odir] - now;
581e7141
JA
737 ddir = odir;
738 }
739 } else
50a8ce86 740 usec = td->rate_next_io_time[ddir] - now;
581e7141 741
a9da8ab2
JA
742 if (td->o.io_submit_mode == IO_MODE_INLINE)
743 io_u_quiesce(td);
78c1eda5 744
df06a036 745 if (td->o.timeout && ((usec + now) > td->o.timeout)) {
746 /*
747 * check if the usec is capable of taking negative values
748 */
749 if (now > td->o.timeout) {
e8a0b539 750 ddir = DDIR_TIMEOUT;
df06a036 751 return ddir;
752 }
753 usec = td->o.timeout - now;
754 }
1a9bf814 755 usec_sleep(td, usec);
df06a036 756
757 now = utime_since_now(&td->epoch);
758 if ((td->o.timeout && (now > td->o.timeout)) || td->terminate)
e8a0b539 759 ddir = DDIR_TIMEOUT;
df06a036 760
581e7141
JA
761 return ddir;
762}
763
10ba535a
JA
764/*
765 * Return the data direction for the next io_u. If the job is a
766 * mixed read/write workload, check the rwmix cycle and switch if
767 * necessary.
768 */
1e97cce9 769static enum fio_ddir get_rw_ddir(struct thread_data *td)
10ba535a 770{
581e7141
JA
771 enum fio_ddir ddir;
772
5f9099ea 773 /*
f6860972
TK
774 * See if it's time to fsync/fdatasync/sync_file_range first,
775 * and if not then move on to check regular I/Os.
5f9099ea 776 */
704a8cfe 777 if (should_fsync(td) && td->last_ddir_issued == DDIR_WRITE) {
f6860972
TK
778 if (td->o.fsync_blocks && td->io_issues[DDIR_WRITE] &&
779 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks))
780 return DDIR_SYNC;
781
782 if (td->o.fdatasync_blocks && td->io_issues[DDIR_WRITE] &&
783 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks))
784 return DDIR_DATASYNC;
785
786 if (td->sync_file_range_nr && td->io_issues[DDIR_WRITE] &&
787 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr))
788 return DDIR_SYNC_FILE_RANGE;
789 }
44f29692 790
10ba535a 791 if (td_rw(td)) {
10ba535a
JA
792 /*
793 * Check if it's time to seed a new data direction.
794 */
e4928662 795 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
e47f799f
JA
796 /*
797 * Put a top limit on how many bytes we do for
798 * one data direction, to avoid overflowing the
799 * ranges too much
800 */
801 ddir = get_rand_ddir(td);
e47f799f
JA
802
803 if (ddir != td->rwmix_ddir)
804 set_rwmix_bytes(td);
805
806 td->rwmix_ddir = ddir;
10ba535a 807 }
581e7141 808 ddir = td->rwmix_ddir;
10ba535a 809 } else if (td_read(td))
581e7141 810 ddir = DDIR_READ;
6eaf09d6 811 else if (td_write(td))
581e7141 812 ddir = DDIR_WRITE;
5c8e84ca 813 else if (td_trim(td))
6eaf09d6 814 ddir = DDIR_TRIM;
5c8e84ca
TK
815 else
816 ddir = DDIR_INVAL;
581e7141 817
2f5102a5
H
818 if (!should_check_rate(td)) {
819 /*
820 * avoid time-consuming call to utime_since_now() if rate checking
821 * isn't being used. this imrpoves IOPs 50%. See:
822 * https://github.com/axboe/fio/issues/1501#issuecomment-1418327049
823 */
824 td->rwmix_ddir = ddir;
825 } else
826 td->rwmix_ddir = rate_ddir(td, ddir);
581e7141 827 return td->rwmix_ddir;
10ba535a
JA
828}
829
1ef2b6be
JA
830static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
831{
0e4dd95c
DE
832 enum fio_ddir ddir = get_rw_ddir(td);
833
731461cc
DLM
834 if (td->o.zone_mode == ZONE_MODE_ZBD)
835 ddir = zbd_adjust_ddir(td, io_u, ddir);
c65057f9 836
cf168c5b 837 if (td_trimwrite(td) && !ddir_sync(ddir)) {
0e4dd95c 838 struct fio_file *f = io_u->file;
793b8686 839 if (f->last_start[DDIR_WRITE] == f->last_start[DDIR_TRIM])
0e4dd95c
DE
840 ddir = DDIR_TRIM;
841 else
842 ddir = DDIR_WRITE;
843 }
844
845 io_u->ddir = io_u->acct_ddir = ddir;
1ef2b6be 846
9b87f09b 847 if (io_u->ddir == DDIR_WRITE && td_ioengine_flagged(td, FIO_BARRIER) &&
1ef2b6be
JA
848 td->o.barrier_blocks &&
849 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
850 td->io_issues[DDIR_WRITE])
1651e431 851 io_u_set(td, io_u, IO_U_F_BARRIER);
1ef2b6be
JA
852}
853
e8462bd8 854void put_file_log(struct thread_data *td, struct fio_file *f)
60f2c658 855{
71b84caa 856 unsigned int ret = put_file(td, f);
60f2c658
JA
857
858 if (ret)
859 td_verror(td, ret, "file close");
860}
861
10ba535a
JA
862void put_io_u(struct thread_data *td, struct io_u *io_u)
863{
26b3a188
JA
864 const bool needs_lock = td_async_processing(td);
865
b2da58c4 866 zbd_put_io_u(td, io_u);
99952ca7 867
a9da8ab2
JA
868 if (td->parent)
869 td = td->parent;
870
26b3a188
JA
871 if (needs_lock)
872 __td_io_u_lock(td);
e8462bd8 873
f8b0bd10 874 if (io_u->file && !(io_u->flags & IO_U_F_NO_FILE_PUT))
60f2c658 875 put_file_log(td, io_u->file);
f8b0bd10 876
10ba535a 877 io_u->file = NULL;
1651e431 878 io_u_set(td, io_u, IO_U_F_FREE);
d7ee2a7d 879
a9da8ab2 880 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
0c41214f 881 td->cur_depth--;
a9da8ab2
JA
882 assert(!(td->flags & TD_F_CHILD));
883 }
2ae0b204 884 io_u_qpush(&td->io_u_freelist, io_u);
e8462bd8 885 td_io_u_free_notify(td);
26b3a188
JA
886
887 if (needs_lock)
888 __td_io_u_unlock(td);
10ba535a
JA
889}
890
1e6ca399
SK
891static inline void io_u_clear_inflight_flags(struct thread_data *td,
892 struct io_u *io_u)
893{
894 io_u_clear(td, io_u, IO_U_F_FLIGHT | IO_U_F_BUSY_OK |
895 IO_U_F_PATTERN_DONE);
896}
897
f2bba182
RR
898void clear_io_u(struct thread_data *td, struct io_u *io_u)
899{
1e6ca399 900 io_u_clear_inflight_flags(td, io_u);
f2bba182
RR
901 put_io_u(td, io_u);
902}
903
755200a3
JA
904void requeue_io_u(struct thread_data *td, struct io_u **io_u)
905{
26b3a188 906 const bool needs_lock = td_async_processing(td);
755200a3 907 struct io_u *__io_u = *io_u;
bcd5abfa 908 enum fio_ddir ddir = acct_ddir(__io_u);
755200a3 909
465221b0
JA
910 dprint(FD_IO, "requeue %p\n", __io_u);
911
a9da8ab2
JA
912 if (td->parent)
913 td = td->parent;
914
26b3a188
JA
915 if (needs_lock)
916 __td_io_u_lock(td);
e8462bd8 917
1651e431 918 io_u_set(td, __io_u, IO_U_F_FREE);
bcd5abfa
JA
919 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(ddir))
920 td->io_issues[ddir]--;
5ec10eaa 921
1651e431 922 io_u_clear(td, __io_u, IO_U_F_FLIGHT);
a9da8ab2 923 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH) {
0c41214f 924 td->cur_depth--;
a9da8ab2
JA
925 assert(!(td->flags & TD_F_CHILD));
926 }
2ae0b204
JA
927
928 io_u_rpush(&td->io_u_requeues, __io_u);
a9da8ab2 929 td_io_u_free_notify(td);
26b3a188
JA
930
931 if (needs_lock)
932 __td_io_u_unlock(td);
933
755200a3
JA
934 *io_u = NULL;
935}
936
7b865a2f 937static void setup_strided_zone_mode(struct thread_data *td, struct io_u *io_u)
224b3093 938{
939 struct fio_file *f = io_u->file;
940
7b865a2f
BVA
941 assert(td->o.zone_mode == ZONE_MODE_STRIDED);
942 assert(td->o.zone_size);
943 assert(td->o.zone_range);
944
224b3093 945 /*
946 * See if it's time to switch to a new zone
947 */
fa9fd914 948 if (td->zone_bytes >= td->o.zone_size) {
224b3093 949 td->zone_bytes = 0;
950 f->file_offset += td->o.zone_range + td->o.zone_skip;
951
952 /*
953 * Wrap from the beginning, if we exceed the file size
954 */
955 if (f->file_offset >= f->real_file_size)
04bc85a1
JA
956 f->file_offset = get_start_offset(td, f);
957
224b3093 958 f->last_pos[io_u->ddir] = f->file_offset;
959 td->io_skip_bytes += td->o.zone_skip;
960 }
961
962 /*
04bc85a1
JA
963 * If zone_size > zone_range, then maintain the same zone until
964 * zone_bytes >= zone_size.
965 */
224b3093 966 if (f->last_pos[io_u->ddir] >= (f->file_offset + td->o.zone_range)) {
967 dprint(FD_IO, "io_u maintain zone offset=%" PRIu64 "/last_pos=%" PRIu64 "\n",
968 f->file_offset, f->last_pos[io_u->ddir]);
969 f->last_pos[io_u->ddir] = f->file_offset;
970 }
971
972 /*
973 * For random: if 'norandommap' is not set and zone_size > zone_range,
974 * map needs to be reset as it's done with zone_range everytime.
975 */
04bc85a1 976 if ((td->zone_bytes % td->o.zone_range) == 0)
224b3093 977 fio_file_reset(td, f);
224b3093 978}
979
b3251e31
AK
980static int fill_multi_range_io_u(struct thread_data *td, struct io_u *io_u)
981{
982 bool is_random;
983 uint64_t buflen, i = 0;
984 struct trim_range *range;
985 struct fio_file *f = io_u->file;
986 uint8_t *buf;
987
988 buf = io_u->buf;
989 buflen = 0;
990
991 while (i < td->o.num_range) {
992 range = (struct trim_range *)buf;
993 if (get_next_offset(td, io_u, &is_random)) {
994 dprint(FD_IO, "io_u %p, failed getting offset\n",
995 io_u);
996 break;
997 }
998
999 io_u->buflen = get_next_buflen(td, io_u, is_random);
1000 if (!io_u->buflen) {
1001 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
1002 break;
1003 }
1004
1005 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
1006 dprint(FD_IO, "io_u %p, off=0x%llx + len=0x%llx exceeds file size=0x%llx\n",
1007 io_u,
1008 (unsigned long long) io_u->offset, io_u->buflen,
1009 (unsigned long long) io_u->file->real_file_size);
1010 break;
1011 }
1012
1013 range->start = io_u->offset;
1014 range->len = io_u->buflen;
1015 buflen += io_u->buflen;
1016 f->last_start[io_u->ddir] = io_u->offset;
1017 f->last_pos[io_u->ddir] = io_u->offset + range->len;
1018
1019 buf += sizeof(struct trim_range);
1020 i++;
1021
1022 if (td_random(td) && file_randommap(td, io_u->file))
1023 mark_random_map(td, io_u, io_u->offset, io_u->buflen);
1024 dprint_io_u(io_u, "fill");
1025 }
1026 if (buflen) {
1027 /*
1028 * Set buffer length as overall trim length for this IO, and
1029 * tell the ioengine about the number of ranges to be trimmed.
1030 */
1031 io_u->buflen = buflen;
1032 io_u->number_trim = i;
1033 return 0;
1034 }
1035
1036 return 1;
1037}
1038
9bf2061e 1039static int fill_io_u(struct thread_data *td, struct io_u *io_u)
10ba535a 1040{
ec370c22 1041 bool is_random;
bfbdd35b
BVA
1042 uint64_t offset;
1043 enum io_u_action ret;
6aca9b3d 1044
9b87f09b 1045 if (td_ioengine_flagged(td, FIO_NOIO))
b4c5e1ac
JA
1046 goto out;
1047
1ef2b6be 1048 set_rw_ddir(td, io_u);
5f9099ea 1049
e8a0b539 1050 if (io_u->ddir == DDIR_INVAL || io_u->ddir == DDIR_TIMEOUT) {
df06a036 1051 dprint(FD_IO, "invalid direction received ddir = %d", io_u->ddir);
1052 return 1;
1053 }
87dc1ab1 1054 /*
ff58fced 1055 * fsync() or fdatasync() or trim etc, we are done
87dc1ab1 1056 */
ff58fced 1057 if (!ddir_rw(io_u->ddir))
c38e9468 1058 goto out;
a00735e6 1059
7b865a2f
BVA
1060 if (td->o.zone_mode == ZONE_MODE_STRIDED)
1061 setup_strided_zone_mode(td, io_u);
4d37720a
DLM
1062 else if (td->o.zone_mode == ZONE_MODE_ZBD)
1063 setup_zbd_zone_mode(td, io_u);
48f5abd3 1064
b3251e31
AK
1065 if (multi_range_trim(td, io_u)) {
1066 if (fill_multi_range_io_u(td, io_u))
1067 return 1;
1068 } else {
1069 /*
1070 * No log, let the seq/rand engine retrieve the next buflen and
1071 * position.
1072 */
1073 if (get_next_offset(td, io_u, &is_random)) {
1074 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
1075 return 1;
1076 }
10ba535a 1077
b3251e31
AK
1078 io_u->buflen = get_next_buflen(td, io_u, is_random);
1079 if (!io_u->buflen) {
1080 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
1081 return 1;
1082 }
2ba1c290 1083 }
bfbdd35b 1084 offset = io_u->offset;
b3251e31 1085
bfbdd35b
BVA
1086 if (td->o.zone_mode == ZONE_MODE_ZBD) {
1087 ret = zbd_adjust_block(td, io_u);
629eda50
BVA
1088 if (ret == io_u_eof) {
1089 dprint(FD_IO, "zbd_adjust_block() returned io_u_eof\n");
bfbdd35b 1090 return 1;
629eda50 1091 }
bfbdd35b
BVA
1092 }
1093
1a3a21b7 1094 if (td->o.dp_type != FIO_DP_NONE)
c60d54ae 1095 dp_fill_dspec_data(td, io_u);
a7e8aae0 1096
2ba1c290 1097 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
5fff9543 1098 dprint(FD_IO, "io_u %p, off=0x%llx + len=0x%llx exceeds file size=0x%llx\n",
e5f9a813 1099 io_u,
4b91ee8f
JA
1100 (unsigned long long) io_u->offset, io_u->buflen,
1101 (unsigned long long) io_u->file->real_file_size);
6a5e6884 1102 return 1;
2ba1c290 1103 }
6a5e6884 1104
bca4ed4d
JA
1105 /*
1106 * mark entry before potentially trimming io_u
1107 */
b3251e31 1108 if (!multi_range_trim(td, io_u) && td_random(td) && file_randommap(td, io_u->file))
bfbdd35b 1109 io_u->buflen = mark_random_map(td, io_u, offset, io_u->buflen);
bca4ed4d 1110
c38e9468 1111out:
b3251e31
AK
1112 if (!multi_range_trim(td, io_u))
1113 dprint_io_u(io_u, "fill");
cb7d7abb 1114 io_u->verify_offset = io_u->offset;
d9d91e39 1115 td->zone_bytes += io_u->buflen;
bca4ed4d 1116 return 0;
10ba535a
JA
1117}
1118
6cc0e5aa 1119static void __io_u_mark_map(uint64_t *map, unsigned int nr)
838bc709 1120{
2b13e716 1121 int idx = 0;
838bc709
JA
1122
1123 switch (nr) {
1124 default:
2b13e716 1125 idx = 6;
838bc709
JA
1126 break;
1127 case 33 ... 64:
2b13e716 1128 idx = 5;
838bc709
JA
1129 break;
1130 case 17 ... 32:
2b13e716 1131 idx = 4;
838bc709
JA
1132 break;
1133 case 9 ... 16:
2b13e716 1134 idx = 3;
838bc709
JA
1135 break;
1136 case 5 ... 8:
2b13e716 1137 idx = 2;
838bc709
JA
1138 break;
1139 case 1 ... 4:
2b13e716 1140 idx = 1;
87933e32 1141 fio_fallthrough;
838bc709
JA
1142 case 0:
1143 break;
1144 }
1145
2b13e716 1146 map[idx]++;
838bc709
JA
1147}
1148
1149void io_u_mark_submit(struct thread_data *td, unsigned int nr)
1150{
1151 __io_u_mark_map(td->ts.io_u_submit, nr);
1152 td->ts.total_submit++;
1153}
1154
1155void io_u_mark_complete(struct thread_data *td, unsigned int nr)
1156{
1157 __io_u_mark_map(td->ts.io_u_complete, nr);
1158 td->ts.total_complete++;
1159}
1160
d8005759 1161void io_u_mark_depth(struct thread_data *td, unsigned int nr)
71619dc2 1162{
2b13e716 1163 int idx = 0;
71619dc2
JA
1164
1165 switch (td->cur_depth) {
1166 default:
2b13e716 1167 idx = 6;
a783e61a 1168 break;
71619dc2 1169 case 32 ... 63:
2b13e716 1170 idx = 5;
a783e61a 1171 break;
71619dc2 1172 case 16 ... 31:
2b13e716 1173 idx = 4;
a783e61a 1174 break;
71619dc2 1175 case 8 ... 15:
2b13e716 1176 idx = 3;
a783e61a 1177 break;
71619dc2 1178 case 4 ... 7:
2b13e716 1179 idx = 2;
a783e61a 1180 break;
71619dc2 1181 case 2 ... 3:
2b13e716 1182 idx = 1;
87933e32 1183 fio_fallthrough;
71619dc2
JA
1184 case 1:
1185 break;
1186 }
1187
2b13e716 1188 td->ts.io_u_map[idx] += nr;
71619dc2
JA
1189}
1190
d6bb626e 1191static void io_u_mark_lat_nsec(struct thread_data *td, unsigned long long nsec)
04a0feae 1192{
2b13e716 1193 int idx = 0;
04a0feae 1194
d6bb626e
VF
1195 assert(nsec < 1000);
1196
1197 switch (nsec) {
1198 case 750 ... 999:
1199 idx = 9;
1200 break;
1201 case 500 ... 749:
1202 idx = 8;
1203 break;
1204 case 250 ... 499:
1205 idx = 7;
1206 break;
1207 case 100 ... 249:
1208 idx = 6;
1209 break;
1210 case 50 ... 99:
1211 idx = 5;
1212 break;
1213 case 20 ... 49:
1214 idx = 4;
1215 break;
1216 case 10 ... 19:
1217 idx = 3;
1218 break;
1219 case 4 ... 9:
1220 idx = 2;
1221 break;
1222 case 2 ... 3:
1223 idx = 1;
87933e32 1224 fio_fallthrough;
d6bb626e
VF
1225 case 0 ... 1:
1226 break;
1227 }
1228
1229 assert(idx < FIO_IO_U_LAT_N_NR);
1230 td->ts.io_u_lat_n[idx]++;
1231}
1232
1233static void io_u_mark_lat_usec(struct thread_data *td, unsigned long long usec)
1234{
1235 int idx = 0;
1236
1237 assert(usec < 1000 && usec >= 1);
04a0feae
JA
1238
1239 switch (usec) {
1240 case 750 ... 999:
2b13e716 1241 idx = 9;
04a0feae
JA
1242 break;
1243 case 500 ... 749:
2b13e716 1244 idx = 8;
04a0feae
JA
1245 break;
1246 case 250 ... 499:
2b13e716 1247 idx = 7;
04a0feae
JA
1248 break;
1249 case 100 ... 249:
2b13e716 1250 idx = 6;
04a0feae
JA
1251 break;
1252 case 50 ... 99:
2b13e716 1253 idx = 5;
04a0feae
JA
1254 break;
1255 case 20 ... 49:
2b13e716 1256 idx = 4;
04a0feae
JA
1257 break;
1258 case 10 ... 19:
2b13e716 1259 idx = 3;
04a0feae
JA
1260 break;
1261 case 4 ... 9:
2b13e716 1262 idx = 2;
04a0feae
JA
1263 break;
1264 case 2 ... 3:
2b13e716 1265 idx = 1;
87933e32 1266 fio_fallthrough;
04a0feae
JA
1267 case 0 ... 1:
1268 break;
1269 }
1270
2b13e716
JA
1271 assert(idx < FIO_IO_U_LAT_U_NR);
1272 td->ts.io_u_lat_u[idx]++;
04a0feae
JA
1273}
1274
d6bb626e 1275static void io_u_mark_lat_msec(struct thread_data *td, unsigned long long msec)
ec118304 1276{
2b13e716 1277 int idx = 0;
ec118304 1278
d6bb626e
VF
1279 assert(msec >= 1);
1280
ec118304
JA
1281 switch (msec) {
1282 default:
2b13e716 1283 idx = 11;
04a0feae 1284 break;
8abdce66 1285 case 1000 ... 1999:
2b13e716 1286 idx = 10;
04a0feae 1287 break;
8abdce66 1288 case 750 ... 999:
2b13e716 1289 idx = 9;
04a0feae 1290 break;
8abdce66 1291 case 500 ... 749:
2b13e716 1292 idx = 8;
04a0feae 1293 break;
8abdce66 1294 case 250 ... 499:
2b13e716 1295 idx = 7;
04a0feae 1296 break;
8abdce66 1297 case 100 ... 249:
2b13e716 1298 idx = 6;
04a0feae 1299 break;
8abdce66 1300 case 50 ... 99:
2b13e716 1301 idx = 5;
04a0feae 1302 break;
8abdce66 1303 case 20 ... 49:
2b13e716 1304 idx = 4;
04a0feae 1305 break;
8abdce66 1306 case 10 ... 19:
2b13e716 1307 idx = 3;
04a0feae 1308 break;
8abdce66 1309 case 4 ... 9:
2b13e716 1310 idx = 2;
04a0feae 1311 break;
ec118304 1312 case 2 ... 3:
2b13e716 1313 idx = 1;
87933e32 1314 fio_fallthrough;
ec118304
JA
1315 case 0 ... 1:
1316 break;
1317 }
1318
2b13e716
JA
1319 assert(idx < FIO_IO_U_LAT_M_NR);
1320 td->ts.io_u_lat_m[idx]++;
04a0feae
JA
1321}
1322
d6bb626e 1323static void io_u_mark_latency(struct thread_data *td, unsigned long long nsec)
04a0feae 1324{
d6bb626e
VF
1325 if (nsec < 1000)
1326 io_u_mark_lat_nsec(td, nsec);
1327 else if (nsec < 1000000)
1328 io_u_mark_lat_usec(td, nsec / 1000);
04a0feae 1329 else
d6bb626e 1330 io_u_mark_lat_msec(td, nsec / 1000000);
ec118304
JA
1331}
1332
8c07860d
JA
1333static unsigned int __get_next_fileno_rand(struct thread_data *td)
1334{
1335 unsigned long fileno;
1336
1337 if (td->o.file_service_type == FIO_FSERVICE_RANDOM) {
1338 uint64_t frand_max = rand_max(&td->next_file_state);
1339 unsigned long r;
1340
1341 r = __rand(&td->next_file_state);
1342 return (unsigned int) ((double) td->o.nr_files
1343 * (r / (frand_max + 1.0)));
1344 }
1345
1346 if (td->o.file_service_type == FIO_FSERVICE_ZIPF)
1347 fileno = zipf_next(&td->next_file_zipf);
1348 else if (td->o.file_service_type == FIO_FSERVICE_PARETO)
1349 fileno = pareto_next(&td->next_file_zipf);
1350 else if (td->o.file_service_type == FIO_FSERVICE_GAUSS)
1351 fileno = gauss_next(&td->next_file_gauss);
1352 else {
1353 log_err("fio: bad file service type: %d\n", td->o.file_service_type);
1354 assert(0);
1355 return 0;
1356 }
1357
1358 return fileno >> FIO_FSERVICE_SHIFT;
1359}
1360
0aabe160
JA
1361/*
1362 * Get next file to service by choosing one at random
1363 */
2cc52930
JA
1364static struct fio_file *get_next_file_rand(struct thread_data *td,
1365 enum fio_file_flags goodf,
d6aed795 1366 enum fio_file_flags badf)
0aabe160 1367{
0aabe160 1368 struct fio_file *f;
1c178180 1369 int fno;
0aabe160
JA
1370
1371 do {
87b10676 1372 int opened = 0;
4c07ad86 1373
8c07860d 1374 fno = __get_next_fileno_rand(td);
7c83c089 1375
126d65c6 1376 f = td->files[fno];
d6aed795 1377 if (fio_file_done(f))
059e63c0 1378 continue;
1c178180 1379
d6aed795 1380 if (!fio_file_open(f)) {
87b10676
JA
1381 int err;
1382
002fe734
JA
1383 if (td->nr_open_files >= td->o.open_files)
1384 return ERR_PTR(-EBUSY);
1385
87b10676
JA
1386 err = td_io_open_file(td, f);
1387 if (err)
1388 continue;
1389 opened = 1;
1390 }
1391
2ba1c290
JA
1392 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
1393 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
0aabe160 1394 return f;
2ba1c290 1395 }
87b10676
JA
1396 if (opened)
1397 td_io_close_file(td, f);
0aabe160
JA
1398 } while (1);
1399}
1400
1401/*
1402 * Get next file to service by doing round robin between all available ones
1403 */
1c178180
JA
1404static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
1405 int badf)
3d7c391d
JA
1406{
1407 unsigned int old_next_file = td->next_file;
1408 struct fio_file *f;
1409
1410 do {
87b10676
JA
1411 int opened = 0;
1412
126d65c6 1413 f = td->files[td->next_file];
3d7c391d
JA
1414
1415 td->next_file++;
2dc1bbeb 1416 if (td->next_file >= td->o.nr_files)
3d7c391d
JA
1417 td->next_file = 0;
1418
87b10676 1419 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
d6aed795 1420 if (fio_file_done(f)) {
d5ed68ea 1421 f = NULL;
059e63c0 1422 continue;
d5ed68ea 1423 }
059e63c0 1424
d6aed795 1425 if (!fio_file_open(f)) {
87b10676
JA
1426 int err;
1427
002fe734
JA
1428 if (td->nr_open_files >= td->o.open_files)
1429 return ERR_PTR(-EBUSY);
1430
87b10676 1431 err = td_io_open_file(td, f);
b5696bfc
JA
1432 if (err) {
1433 dprint(FD_FILE, "error %d on open of %s\n",
1434 err, f->file_name);
87c27b45 1435 f = NULL;
87b10676 1436 continue;
b5696bfc 1437 }
87b10676
JA
1438 opened = 1;
1439 }
1440
0b9d69ec
JA
1441 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
1442 f->flags);
1c178180 1443 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
3d7c391d
JA
1444 break;
1445
87b10676
JA
1446 if (opened)
1447 td_io_close_file(td, f);
1448
3d7c391d
JA
1449 f = NULL;
1450 } while (td->next_file != old_next_file);
1451
2ba1c290 1452 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
3d7c391d
JA
1453 return f;
1454}
1455
7eb36574 1456static struct fio_file *__get_next_file(struct thread_data *td)
bdb4e2e9 1457{
1907dbc6
JA
1458 struct fio_file *f;
1459
2dc1bbeb 1460 assert(td->o.nr_files <= td->files_index);
1c178180 1461
b5696bfc 1462 if (td->nr_done_files >= td->o.nr_files) {
5ec10eaa
JA
1463 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
1464 " nr_files=%d\n", td->nr_open_files,
1465 td->nr_done_files,
1466 td->o.nr_files);
bdb4e2e9 1467 return NULL;
2ba1c290 1468 }
bdb4e2e9 1469
1907dbc6 1470 f = td->file_service_file;
d6aed795 1471 if (f && fio_file_open(f) && !fio_file_closing(f)) {
a086c257
JA
1472 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
1473 goto out;
4ef1562a 1474 if (td->file_service_left) {
2d8c2709
JA
1475 td->file_service_left--;
1476 goto out;
4ef1562a 1477 }
a086c257 1478 }
1907dbc6 1479
a086c257
JA
1480 if (td->o.file_service_type == FIO_FSERVICE_RR ||
1481 td->o.file_service_type == FIO_FSERVICE_SEQ)
d6aed795 1482 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
bdb4e2e9 1483 else
d6aed795 1484 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1907dbc6 1485
002fe734
JA
1486 if (IS_ERR(f))
1487 return f;
1488
1907dbc6
JA
1489 td->file_service_file = f;
1490 td->file_service_left = td->file_service_nr - 1;
2ba1c290 1491out:
0dac421f
JA
1492 if (f)
1493 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1494 else
1495 dprint(FD_FILE, "get_next_file: NULL\n");
1907dbc6 1496 return f;
bdb4e2e9
JA
1497}
1498
7eb36574
JA
1499static struct fio_file *get_next_file(struct thread_data *td)
1500{
7eb36574
JA
1501 return __get_next_file(td);
1502}
1503
002fe734 1504static long set_io_u_file(struct thread_data *td, struct io_u *io_u)
429f6675
JA
1505{
1506 struct fio_file *f;
1507
1508 do {
1509 f = get_next_file(td);
002fe734
JA
1510 if (IS_ERR_OR_NULL(f))
1511 return PTR_ERR(f);
429f6675 1512
429f6675
JA
1513 io_u->file = f;
1514 get_file(f);
1515
1516 if (!fill_io_u(td, io_u))
1517 break;
1518
b2da58c4 1519 zbd_put_io_u(td, io_u);
99952ca7 1520
b5696bfc 1521 put_file_log(td, f);
429f6675 1522 td_io_close_file(td, f);
b5696bfc 1523 io_u->file = NULL;
e8a0b539 1524
1525 if (io_u->ddir == DDIR_TIMEOUT)
1526 return 1;
1527
8c07860d
JA
1528 if (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)
1529 fio_file_reset(td, f);
1530 else {
1531 fio_file_set_done(f);
1532 td->nr_done_files++;
1533 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
0b9d69ec 1534 td->nr_done_files, td->o.nr_files);
8c07860d 1535 }
429f6675
JA
1536 } while (1);
1537
1538 return 0;
1539}
1540
f7cf63bf 1541static void lat_fatal(struct thread_data *td, struct io_u *io_u, struct io_completion_data *icd,
c3a32714 1542 unsigned long long tnsec, unsigned long long max_nsec)
3e260a46 1543{
f7cf63bf
VR
1544 if (!td->error) {
1545 log_err("fio: latency of %llu nsec exceeds specified max (%llu nsec): %s %s %llu %llu\n",
1546 tnsec, max_nsec,
1547 io_u->file->file_name,
1548 io_ddir_name(io_u->ddir),
1549 io_u->offset, io_u->buflen);
1550 }
3e260a46
JA
1551 td_verror(td, ETIMEDOUT, "max latency exceeded");
1552 icd->error = ETIMEDOUT;
1553}
1554
1555static void lat_new_cycle(struct thread_data *td)
1556{
1557 fio_gettime(&td->latency_ts, NULL);
1558 td->latency_ios = ddir_rw_sum(td->io_blocks);
1559 td->latency_failed = 0;
1560}
1561
1562/*
1563 * We had an IO outside the latency target. Reduce the queue depth. If we
1564 * are at QD=1, then it's time to give up.
1565 */
e39c0676 1566static bool __lat_target_failed(struct thread_data *td)
3e260a46
JA
1567{
1568 if (td->latency_qd == 1)
e39c0676 1569 return true;
3e260a46
JA
1570
1571 td->latency_qd_high = td->latency_qd;
6bb58215
JA
1572
1573 if (td->latency_qd == td->latency_qd_low)
1574 td->latency_qd_low--;
1575
3e260a46 1576 td->latency_qd = (td->latency_qd + td->latency_qd_low) / 2;
e1bcd541 1577 td->latency_stable_count = 0;
3e260a46
JA
1578
1579 dprint(FD_RATE, "Ramped down: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
1580
1581 /*
1582 * When we ramp QD down, quiesce existing IO to prevent
1583 * a storm of ramp downs due to pending higher depth.
1584 */
1585 io_u_quiesce(td);
1586 lat_new_cycle(td);
e39c0676 1587 return false;
3e260a46
JA
1588}
1589
e39c0676 1590static bool lat_target_failed(struct thread_data *td)
3e260a46
JA
1591{
1592 if (td->o.latency_percentile.u.f == 100.0)
1593 return __lat_target_failed(td);
1594
1595 td->latency_failed++;
e39c0676 1596 return false;
3e260a46
JA
1597}
1598
1599void lat_target_init(struct thread_data *td)
1600{
6bb58215
JA
1601 td->latency_end_run = 0;
1602
3e260a46
JA
1603 if (td->o.latency_target) {
1604 dprint(FD_RATE, "Latency target=%llu\n", td->o.latency_target);
1605 fio_gettime(&td->latency_ts, NULL);
1606 td->latency_qd = 1;
1607 td->latency_qd_high = td->o.iodepth;
1608 td->latency_qd_low = 1;
1609 td->latency_ios = ddir_rw_sum(td->io_blocks);
1610 } else
1611 td->latency_qd = td->o.iodepth;
1612}
1613
6bb58215
JA
1614void lat_target_reset(struct thread_data *td)
1615{
1616 if (!td->latency_end_run)
1617 lat_target_init(td);
1618}
1619
3e260a46
JA
1620static void lat_target_success(struct thread_data *td)
1621{
1622 const unsigned int qd = td->latency_qd;
6bb58215 1623 struct thread_options *o = &td->o;
3e260a46
JA
1624
1625 td->latency_qd_low = td->latency_qd;
1626
e1bcd541
SL
1627 if (td->latency_qd + 1 == td->latency_qd_high) {
1628 /*
1629 * latency_qd will not incease on lat_target_success(), so
1630 * called stable. If we stick with this queue depth, the
1631 * final latency is likely lower than latency_target. Fix
1632 * this by increasing latency_qd_high slowly. Use a naive
1633 * heuristic here. If we get lat_target_success() 3 times
1634 * in a row, increase latency_qd_high by 1.
1635 */
1636 if (++td->latency_stable_count >= 3) {
1637 td->latency_qd_high++;
1638 td->latency_stable_count = 0;
1639 }
1640 }
1641
3e260a46
JA
1642 /*
1643 * If we haven't failed yet, we double up to a failing value instead
1644 * of bisecting from highest possible queue depth. If we have set
1645 * a limit other than td->o.iodepth, bisect between that.
1646 */
6bb58215 1647 if (td->latency_qd_high != o->iodepth)
3e260a46
JA
1648 td->latency_qd = (td->latency_qd + td->latency_qd_high) / 2;
1649 else
1650 td->latency_qd *= 2;
1651
6bb58215
JA
1652 if (td->latency_qd > o->iodepth)
1653 td->latency_qd = o->iodepth;
3e260a46
JA
1654
1655 dprint(FD_RATE, "Ramped up: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
6bb58215 1656
3e260a46 1657 /*
6bb58215
JA
1658 * Same as last one, we are done. Let it run a latency cycle, so
1659 * we get only the results from the targeted depth.
3e260a46 1660 */
e1bcd541 1661 if (!o->latency_run && td->latency_qd == qd) {
6bb58215
JA
1662 if (td->latency_end_run) {
1663 dprint(FD_RATE, "We are done\n");
1664 td->done = 1;
1665 } else {
1666 dprint(FD_RATE, "Quiesce and final run\n");
1667 io_u_quiesce(td);
1668 td->latency_end_run = 1;
1669 reset_all_stats(td);
1670 reset_io_stats(td);
1671 }
1672 }
3e260a46
JA
1673
1674 lat_new_cycle(td);
1675}
1676
1677/*
1678 * Check if we can bump the queue depth
1679 */
1680void lat_target_check(struct thread_data *td)
1681{
1682 uint64_t usec_window;
1683 uint64_t ios;
1684 double success_ios;
1685
1686 usec_window = utime_since_now(&td->latency_ts);
1687 if (usec_window < td->o.latency_window)
1688 return;
1689
1690 ios = ddir_rw_sum(td->io_blocks) - td->latency_ios;
1691 success_ios = (double) (ios - td->latency_failed) / (double) ios;
1692 success_ios *= 100.0;
1693
1694 dprint(FD_RATE, "Success rate: %.2f%% (target %.2f%%)\n", success_ios, td->o.latency_percentile.u.f);
1695
1696 if (success_ios >= td->o.latency_percentile.u.f)
1697 lat_target_success(td);
1698 else
1699 __lat_target_failed(td);
1700}
1701
1702/*
1703 * If latency target is enabled, we might be ramping up or down and not
1704 * using the full queue depth available.
1705 */
e39c0676 1706bool queue_full(const struct thread_data *td)
3e260a46
JA
1707{
1708 const int qempty = io_u_qempty(&td->io_u_freelist);
1709
1710 if (qempty)
e39c0676 1711 return true;
3e260a46 1712 if (!td->o.latency_target)
e39c0676 1713 return false;
3e260a46
JA
1714
1715 return td->cur_depth >= td->latency_qd;
1716}
429f6675 1717
10ba535a
JA
1718struct io_u *__get_io_u(struct thread_data *td)
1719{
26b3a188 1720 const bool needs_lock = td_async_processing(td);
0cae66f6 1721 struct io_u *io_u = NULL;
10ba535a 1722
ca09be4b
JA
1723 if (td->stop_io)
1724 return NULL;
1725
26b3a188
JA
1726 if (needs_lock)
1727 __td_io_u_lock(td);
e8462bd8
JA
1728
1729again:
1c83ed15 1730 if (td->runstate != TD_FSYNCING && !io_u_rempty(&td->io_u_requeues)) {
2ae0b204 1731 io_u = io_u_rpop(&td->io_u_requeues);
bba6b14f
JA
1732 io_u->resid = 0;
1733 } else if (!queue_full(td)) {
2ae0b204 1734 io_u = io_u_qpop(&td->io_u_freelist);
10ba535a 1735
225ba9e3 1736 io_u->file = NULL;
6040dabc 1737 io_u->buflen = 0;
10ba535a 1738 io_u->resid = 0;
d7762cf8 1739 io_u->end_io = NULL;
755200a3
JA
1740 }
1741
1742 if (io_u) {
0c6e7517 1743 assert(io_u->flags & IO_U_F_FREE);
1651e431 1744 io_u_clear(td, io_u, IO_U_F_FREE | IO_U_F_NO_FILE_PUT |
f8b0bd10 1745 IO_U_F_TRIMMED | IO_U_F_BARRIER |
692dec0c 1746 IO_U_F_VER_LIST);
0c6e7517 1747
755200a3 1748 io_u->error = 0;
bcd5abfa 1749 io_u->acct_ddir = -1;
10ba535a 1750 td->cur_depth++;
a9da8ab2 1751 assert(!(td->flags & TD_F_CHILD));
1651e431 1752 io_u_set(td, io_u, IO_U_F_IN_CUR_DEPTH);
f9401285 1753 io_u->ipo = NULL;
a9da8ab2 1754 } else if (td_async_processing(td)) {
83276370 1755 int ret;
1dec3e07
JA
1756 /*
1757 * We ran out, wait for async verify threads to finish and
1758 * return one
1759 */
a9da8ab2 1760 assert(!(td->flags & TD_F_CHILD));
93b45bb2 1761 ret = pthread_cond_wait(&td->free_cond, &td->io_u_lock);
83276370
DP
1762 if (fio_unlikely(ret != 0)) {
1763 td->error = errno;
1764 } else if (!td->error)
ee5e08ad 1765 goto again;
10ba535a
JA
1766 }
1767
26b3a188
JA
1768 if (needs_lock)
1769 __td_io_u_unlock(td);
1770
10ba535a
JA
1771 return io_u;
1772}
1773
e39c0676 1774static bool check_get_trim(struct thread_data *td, struct io_u *io_u)
10ba535a 1775{
d72be545 1776 if (!(td->flags & TD_F_TRIM_BACKLOG))
e39c0676 1777 return false;
67379ba4
VF
1778 if (!td->trim_entries) {
1779 td->trim_batch = 0;
c9a73054 1780 return false;
67379ba4 1781 }
d72be545 1782
c9a73054
JA
1783 if (td->trim_batch) {
1784 td->trim_batch--;
1785 if (get_next_trim(td, io_u))
1786 return true;
67379ba4
VF
1787 else
1788 td->trim_batch = 0;
c9a73054 1789 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
46f22856
VF
1790 td->last_ddir_completed != DDIR_TRIM) {
1791 if (get_next_trim(td, io_u)) {
1792 td->trim_batch = td->o.trim_batch;
1793 if (!td->trim_batch)
1794 td->trim_batch = td->o.trim_backlog;
1795 td->trim_batch--;
e39c0676 1796 return true;
46f22856 1797 }
2ba1c290 1798 }
10ba535a 1799
e39c0676 1800 return false;
0d29de83
JA
1801}
1802
e39c0676 1803static bool check_get_verify(struct thread_data *td, struct io_u *io_u)
0d29de83 1804{
d72be545 1805 if (!(td->flags & TD_F_VER_BACKLOG))
e39c0676 1806 return false;
d72be545
JA
1807
1808 if (td->io_hist_len) {
9e144189
JA
1809 int get_verify = 0;
1810
d1ece0c7 1811 if (td->verify_batch)
9e144189 1812 get_verify = 1;
d1ece0c7 1813 else if (!(td->io_hist_len % td->o.verify_backlog) &&
87b10027 1814 td->last_ddir_completed != DDIR_READ) {
9e144189 1815 td->verify_batch = td->o.verify_batch;
f8a75c99
JA
1816 if (!td->verify_batch)
1817 td->verify_batch = td->o.verify_backlog;
9e144189
JA
1818 get_verify = 1;
1819 }
1820
d1ece0c7
JA
1821 if (get_verify && !get_next_verify(td, io_u)) {
1822 td->verify_batch--;
e39c0676 1823 return true;
d1ece0c7 1824 }
9e144189
JA
1825 }
1826
e39c0676 1827 return false;
0d29de83
JA
1828}
1829
de789769
JA
1830/*
1831 * Fill offset and start time into the buffer content, to prevent too
23f394d5
JA
1832 * easy compressible data for simple de-dupe attempts. Do this for every
1833 * 512b block in the range, since that should be the smallest block size
1834 * we can expect from a device.
de789769
JA
1835 */
1836static void small_content_scramble(struct io_u *io_u)
1837{
5fff9543 1838 unsigned long long i, nr_blocks = io_u->buflen >> 9;
23f394d5 1839 unsigned int offset;
319b073f
JA
1840 uint64_t boffset, *iptr;
1841 char *p;
de789769 1842
23f394d5
JA
1843 if (!nr_blocks)
1844 return;
1845
1846 p = io_u->xfer_buf;
fba76ee8 1847 boffset = io_u->offset;
319b073f
JA
1848
1849 if (io_u->buf_filled_len)
1850 io_u->buf_filled_len = 0;
1851
1852 /*
1853 * Generate random index between 0..7. We do chunks of 512b, if
1854 * we assume a cacheline is 64 bytes, then we have 8 of those.
1855 * Scramble content within the blocks in the same cacheline to
1856 * speed things up.
1857 */
1858 offset = (io_u->start_time.tv_nsec ^ boffset) & 7;
fad82f76 1859
23f394d5
JA
1860 for (i = 0; i < nr_blocks; i++) {
1861 /*
319b073f
JA
1862 * Fill offset into start of cacheline, time into end
1863 * of cacheline
23f394d5 1864 */
319b073f
JA
1865 iptr = (void *) p + (offset << 6);
1866 *iptr = boffset;
1867
1868 iptr = (void *) p + 64 - 2 * sizeof(uint64_t);
1869 iptr[0] = io_u->start_time.tv_sec;
1870 iptr[1] = io_u->start_time.tv_nsec;
23f394d5 1871
23f394d5 1872 p += 512;
fad82f76 1873 boffset += 512;
23f394d5 1874 }
de789769
JA
1875}
1876
0d29de83
JA
1877/*
1878 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
5c5c33c1 1879 * etc. The returned io_u is fully ready to be prepped, populated and submitted.
0d29de83
JA
1880 */
1881struct io_u *get_io_u(struct thread_data *td)
1882{
1883 struct fio_file *f;
1884 struct io_u *io_u;
de789769 1885 int do_scramble = 0;
002fe734 1886 long ret = 0;
0d29de83
JA
1887
1888 io_u = __get_io_u(td);
1889 if (!io_u) {
1890 dprint(FD_IO, "__get_io_u failed\n");
1891 return NULL;
1892 }
1893
1894 if (check_get_verify(td, io_u))
1895 goto out;
1896 if (check_get_trim(td, io_u))
1897 goto out;
1898
755200a3
JA
1899 /*
1900 * from a requeue, io_u already setup
1901 */
1902 if (io_u->file)
77f392bf 1903 goto out;
755200a3 1904
429f6675
JA
1905 /*
1906 * If using an iolog, grab next piece if any available.
1907 */
d72be545 1908 if (td->flags & TD_F_READ_IOLOG) {
429f6675
JA
1909 if (read_iolog_get(td, io_u))
1910 goto err_put;
2ba1c290 1911 } else if (set_io_u_file(td, io_u)) {
002fe734 1912 ret = -EBUSY;
2ba1c290 1913 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
429f6675 1914 goto err_put;
2ba1c290 1915 }
5ec10eaa 1916
429f6675 1917 f = io_u->file;
002fe734
JA
1918 if (!f) {
1919 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1920 goto err_put;
1921 }
1922
d6aed795 1923 assert(fio_file_open(f));
97af62ce 1924
b3251e31 1925 if (ddir_rw(io_u->ddir) && !multi_range_trim(td, io_u)) {
9b87f09b 1926 if (!io_u->buflen && !td_ioengine_flagged(td, FIO_NOIO)) {
2ba1c290 1927 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
429f6675 1928 goto err_put;
2ba1c290 1929 }
10ba535a 1930
f1dfb668
JA
1931 f->last_start[io_u->ddir] = io_u->offset;
1932 f->last_pos[io_u->ddir] = io_u->offset + io_u->buflen;
10ba535a 1933
fd68418e 1934 if (io_u->ddir == DDIR_WRITE) {
d72be545 1935 if (td->flags & TD_F_REFILL_BUFFERS) {
9c42684e 1936 io_u_fill_buffer(td, io_u,
1066358a 1937 td->o.min_bs[DDIR_WRITE],
9e129577 1938 io_u->buflen);
ff441ae8 1939 } else if ((td->flags & TD_F_SCRAMBLE_BUFFERS) &&
5c5c33c1 1940 !(td->flags & TD_F_COMPRESS) &&
5b7565b0 1941 !(td->flags & TD_F_DO_VERIFY)) {
fd68418e 1942 do_scramble = 1;
5b7565b0 1943 }
fd68418e 1944 } else if (io_u->ddir == DDIR_READ) {
cbe8d756
RR
1945 /*
1946 * Reset the buf_filled parameters so next time if the
1947 * buffer is used for writes it is refilled.
1948 */
cbe8d756
RR
1949 io_u->buf_filled_len = 0;
1950 }
87dc1ab1 1951 }
10ba535a 1952
165faf16
JA
1953 /*
1954 * Set io data pointers.
1955 */
cec6b55d
JA
1956 io_u->xfer_buf = io_u->buf;
1957 io_u->xfer_buflen = io_u->buflen;
5973cafb 1958
03ec570f
DLM
1959 /*
1960 * Remember the issuing context priority. The IO engine may change this.
1961 */
1962 io_u->ioprio = td->ioprio;
f0547200 1963 io_u->clat_prio_index = 0;
6ac7a331 1964out:
0d29de83 1965 assert(io_u->file);
429f6675 1966 if (!td_io_prep(td, io_u)) {
3ecc8c67 1967 if (!td->o.disable_lat)
993bf48b 1968 fio_gettime(&io_u->start_time, NULL);
03553853 1969
de789769
JA
1970 if (do_scramble)
1971 small_content_scramble(io_u);
03553853 1972
429f6675 1973 return io_u;
36167d82 1974 }
429f6675 1975err_put:
2ba1c290 1976 dprint(FD_IO, "get_io_u failed\n");
429f6675 1977 put_io_u(td, io_u);
002fe734 1978 return ERR_PTR(ret);
10ba535a
JA
1979}
1980
a9da8ab2 1981static void __io_u_log_error(struct thread_data *td, struct io_u *io_u)
5451792e 1982{
8b28bd41 1983 enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
825f818e 1984
8b28bd41
DM
1985 if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1986 return;
5451792e 1987
5fff9543 1988 log_err("fio: io_u error%s%s: %s: %s offset=%llu, buflen=%llu\n",
709c8313
RE
1989 io_u->file ? " on file " : "",
1990 io_u->file ? io_u->file->file_name : "",
ebe67b66
MI
1991 (io_u->flags & IO_U_F_DEVICE_ERROR) ?
1992 "Device-specific error" : strerror(io_u->error),
709c8313
RE
1993 io_ddir_name(io_u->ddir),
1994 io_u->offset, io_u->xfer_buflen);
5451792e 1995
8b403508
SK
1996 zbd_log_err(td, io_u);
1997
5ad7be56 1998 if (td->io_ops->errdetails) {
0c89abc1 1999 char *err = td->io_ops->errdetails(td, io_u);
5ad7be56 2000
ebe67b66
MI
2001 if (err) {
2002 log_err("fio: %s\n", err);
2003 free(err);
2004 }
5ad7be56
KD
2005 }
2006
5451792e
JA
2007 if (!td->error)
2008 td_verror(td, io_u->error, "io_u error");
2009}
2010
a9da8ab2
JA
2011void io_u_log_error(struct thread_data *td, struct io_u *io_u)
2012{
2013 __io_u_log_error(td, io_u);
2014 if (td->parent)
094e66cb 2015 __io_u_log_error(td->parent, io_u);
a9da8ab2
JA
2016}
2017
e39c0676 2018static inline bool gtod_reduce(struct thread_data *td)
aba6c951 2019{
3ecc8c67
JA
2020 return (td->o.disable_clat && td->o.disable_slat && td->o.disable_bw)
2021 || td->o.gtod_reduce;
aba6c951
JA
2022}
2023
d7e92306
JA
2024static void trim_block_info(struct thread_data *td, struct io_u *io_u)
2025{
2026 uint32_t *info = io_u_block_info(td, io_u);
2027
2028 if (BLOCK_INFO_STATE(*info) >= BLOCK_STATE_TRIM_FAILURE)
2029 return;
2030
2031 *info = BLOCK_INFO(BLOCK_STATE_TRIMMED, BLOCK_INFO_TRIMS(*info) + 1);
2032}
2033
c8eeb9df
JA
2034static void account_io_completion(struct thread_data *td, struct io_u *io_u,
2035 struct io_completion_data *icd,
2036 const enum fio_ddir idx, unsigned int bytes)
2037{
a9da8ab2 2038 const int no_reduce = !gtod_reduce(td);
d6bb626e 2039 unsigned long long llnsec = 0;
c8eeb9df 2040
75dc383e
JA
2041 if (td->parent)
2042 td = td->parent;
2043
132b1ee4 2044 if (!td->o.stats || td_ioengine_flagged(td, FIO_NOSTATS))
8243be59
JA
2045 return;
2046
a9da8ab2 2047 if (no_reduce)
d6bb626e 2048 llnsec = ntime_since(&io_u->issue_time, &icd->time);
c8eeb9df
JA
2049
2050 if (!td->o.disable_lat) {
c3a32714 2051 unsigned long long tnsec;
c8eeb9df 2052
d6bb626e 2053 tnsec = ntime_since(&io_u->start_time, &icd->time);
ac18dd16 2054 add_lat_sample(td, idx, tnsec, bytes, io_u);
15501535 2055
d4afedfd
JA
2056 if (td->flags & TD_F_PROFILE_OPS) {
2057 struct prof_io_ops *ops = &td->prof_io_ops;
2058
2059 if (ops->io_u_lat)
c3a32714 2060 icd->error = ops->io_u_lat(td, tnsec);
d4afedfd
JA
2061 }
2062
f7cf63bf
VR
2063 if (ddir_rw(idx)) {
2064 if (td->o.max_latency[idx] && tnsec > td->o.max_latency[idx])
2065 lat_fatal(td, io_u, icd, tnsec, td->o.max_latency[idx]);
2066 if (td->o.latency_target && tnsec > td->o.latency_target) {
2067 if (lat_target_failed(td))
2068 lat_fatal(td, io_u, icd, tnsec, td->o.latency_target);
2069 }
15501535 2070 }
c8eeb9df
JA
2071 }
2072
a47591e4
JA
2073 if (ddir_rw(idx)) {
2074 if (!td->o.disable_clat) {
ac18dd16 2075 add_clat_sample(td, idx, llnsec, bytes, io_u);
d6bb626e 2076 io_u_mark_latency(td, llnsec);
a47591e4 2077 }
c8eeb9df 2078
a47591e4 2079 if (!td->o.disable_bw && per_unit_log(td->bw_log))
d6bb626e 2080 add_bw_sample(td, io_u, bytes, llnsec);
c8eeb9df 2081
a47591e4
JA
2082 if (no_reduce && per_unit_log(td->iops_log))
2083 add_iops_sample(td, io_u, bytes);
b2b3eefe
JA
2084 } else if (ddir_sync(idx) && !td->o.disable_clat)
2085 add_sync_clat_sample(&td->ts, llnsec);
66347cfa 2086
d7e92306
JA
2087 if (td->ts.nr_block_infos && io_u->ddir == DDIR_TRIM)
2088 trim_block_info(td, io_u);
c8eeb9df
JA
2089}
2090
94a6e1bb
JA
2091static void file_log_write_comp(const struct thread_data *td, struct fio_file *f,
2092 uint64_t offset, unsigned int bytes)
2093{
639ad1ea
JA
2094 if (!f)
2095 return;
2096
94a6e1bb
JA
2097 if (f->first_write == -1ULL || offset < f->first_write)
2098 f->first_write = offset;
2099 if (f->last_write == -1ULL || ((offset + bytes) > f->last_write))
2100 f->last_write = offset + bytes;
94a6e1bb
JA
2101}
2102
b2b3eefe
JA
2103static bool should_account(struct thread_data *td)
2104{
2105 return ramp_time_over(td) && (td->runstate == TD_RUNNING ||
2106 td->runstate == TD_VERIFYING);
2107}
2108
f8b0bd10 2109static void io_completed(struct thread_data *td, struct io_u **io_u_ptr,
97601024 2110 struct io_completion_data *icd)
10ba535a 2111{
f8b0bd10
JA
2112 struct io_u *io_u = *io_u_ptr;
2113 enum fio_ddir ddir = io_u->ddir;
2114 struct fio_file *f = io_u->file;
10ba535a 2115
e5f9a813 2116 dprint_io_u(io_u, "complete");
2ba1c290 2117
0c6e7517 2118 assert(io_u->flags & IO_U_F_FLIGHT);
1e6ca399 2119 io_u_clear_inflight_flags(td, io_u);
a9ba7cef 2120 invalidate_inflight(td, io_u);
f9401285 2121
650c4ad3
SK
2122 if (td->o.zone_mode == ZONE_MODE_ZBD && td->o.recover_zbd_write_error &&
2123 io_u->error && io_u->ddir == DDIR_WRITE &&
2124 !td_ioengine_flagged(td, FIO_SYNCIO))
2125 zbd_recover_write_error(td, io_u);
2126
f9401285
JA
2127 /*
2128 * Mark IO ok to verify
2129 */
2130 if (io_u->ipo) {
890b6656
JA
2131 /*
2132 * Remove errored entry from the verification list
2133 */
2134 if (io_u->error)
2135 unlog_io_piece(td, io_u);
2136 else {
0337173e
BVA
2137 atomic_store_release(&io_u->ipo->flags,
2138 io_u->ipo->flags & ~IP_F_IN_FLIGHT);
890b6656 2139 }
f9401285
JA
2140 }
2141
f8b0bd10 2142 if (ddir_sync(ddir)) {
870ea002
JA
2143 if (io_u->error)
2144 goto error;
44f29692
JA
2145 if (f) {
2146 f->first_write = -1ULL;
2147 f->last_write = -1ULL;
2148 }
b2b3eefe
JA
2149 if (should_account(td))
2150 account_io_completion(td, io_u, icd, ddir, io_u->buflen);
87dc1ab1
JA
2151 return;
2152 }
2153
87b10027 2154 td->last_ddir_completed = ddir;
87dc1ab1 2155
f8b0bd10 2156 if (!io_u->error && ddir_rw(ddir)) {
84abeef7 2157 unsigned long long bytes = io_u->xfer_buflen - io_u->resid;
b29ee5b3 2158 int ret;
10ba535a 2159
7d33649c
JA
2160 /*
2161 * Make sure we notice short IO from here, and requeue them
2162 * appropriately!
2163 */
c4cb947e 2164 if (bytes && io_u->resid) {
7d33649c
JA
2165 io_u->xfer_buflen = io_u->resid;
2166 io_u->xfer_buf += bytes;
2167 io_u->offset += bytes;
2168 td->ts.short_io_u[io_u->ddir]++;
2169 if (io_u->offset < io_u->file->real_file_size) {
2170 requeue_io_u(td, io_u_ptr);
2171 return;
2172 }
2173 }
2174
f8b0bd10 2175 td->io_blocks[ddir]++;
f8b0bd10 2176 td->io_bytes[ddir] += bytes;
ae2fafc8 2177
e1c325d2
JA
2178 if (!(io_u->flags & IO_U_F_VER_LIST)) {
2179 td->this_io_blocks[ddir]++;
f8b0bd10 2180 td->this_io_bytes[ddir] += bytes;
e1c325d2 2181 }
f8b0bd10 2182
639ad1ea 2183 if (ddir == DDIR_WRITE)
94a6e1bb 2184 file_log_write_comp(td, f, io_u->offset, bytes);
44f29692 2185
b2b3eefe 2186 if (should_account(td))
f8b0bd10 2187 account_io_completion(td, io_u, icd, ddir, bytes);
40e1a6f0 2188
f8b0bd10 2189 icd->bytes_done[ddir] += bytes;
3af6ef39 2190
d7762cf8 2191 if (io_u->end_io) {
f8b0bd10
JA
2192 ret = io_u->end_io(td, io_u_ptr);
2193 io_u = *io_u_ptr;
3af6ef39
JA
2194 if (ret && !icd->error)
2195 icd->error = ret;
2196 }
ff58fced 2197 } else if (io_u->error) {
870ea002 2198error:
10ba535a 2199 icd->error = io_u->error;
5451792e
JA
2200 io_u_log_error(td, io_u);
2201 }
8b28bd41 2202 if (icd->error) {
f8b0bd10
JA
2203 enum error_type_bit eb = td_error_type(ddir, icd->error);
2204
8b28bd41
DM
2205 if (!td_non_fatal_error(td, eb, icd->error))
2206 return;
f8b0bd10 2207
f2bba182
RR
2208 /*
2209 * If there is a non_fatal error, then add to the error count
2210 * and clear all the errors.
2211 */
2212 update_error_count(td, icd->error);
2213 td_clear_error(td);
2214 icd->error = 0;
f8b0bd10
JA
2215 if (io_u)
2216 io_u->error = 0;
f2bba182 2217 }
10ba535a
JA
2218}
2219
9520ebb9
JA
2220static void init_icd(struct thread_data *td, struct io_completion_data *icd,
2221 int nr)
10ba535a 2222{
6eaf09d6 2223 int ddir;
aba6c951
JA
2224
2225 if (!gtod_reduce(td))
9520ebb9 2226 fio_gettime(&icd->time, NULL);
02bcaa8c 2227
3af6ef39
JA
2228 icd->nr = nr;
2229
10ba535a 2230 icd->error = 0;
c1f50f76 2231 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
6eaf09d6 2232 icd->bytes_done[ddir] = 0;
36167d82
JA
2233}
2234
97601024
JA
2235static void ios_completed(struct thread_data *td,
2236 struct io_completion_data *icd)
36167d82
JA
2237{
2238 struct io_u *io_u;
2239 int i;
2240
10ba535a
JA
2241 for (i = 0; i < icd->nr; i++) {
2242 io_u = td->io_ops->event(td, i);
2243
f8b0bd10 2244 io_completed(td, &io_u, icd);
e8462bd8 2245
f8b0bd10 2246 if (io_u)
e8462bd8 2247 put_io_u(td, io_u);
10ba535a
JA
2248 }
2249}
97601024 2250
191d6634
SK
2251static void io_u_update_bytes_done(struct thread_data *td,
2252 struct io_completion_data *icd)
2253{
2254 int ddir;
2255
2256 if (td->runstate == TD_VERIFYING) {
2257 td->bytes_verified += icd->bytes_done[DDIR_READ];
96a7e64c
SK
2258 if (td_write(td))
2259 return;
191d6634
SK
2260 }
2261
2262 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
2263 td->bytes_done[ddir] += icd->bytes_done[ddir];
2264}
2265
e7e6cfb4
JA
2266/*
2267 * Complete a single io_u for the sync engines.
2268 */
55312f9f 2269int io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
97601024
JA
2270{
2271 struct io_completion_data icd;
2272
9520ebb9 2273 init_icd(td, &icd, 1);
f8b0bd10 2274 io_completed(td, &io_u, &icd);
e8462bd8 2275
f8b0bd10 2276 if (io_u)
e8462bd8 2277 put_io_u(td, io_u);
97601024 2278
581e7141
JA
2279 if (icd.error) {
2280 td_verror(td, icd.error, "io_u_sync_complete");
2281 return -1;
2282 }
97601024 2283
191d6634 2284 io_u_update_bytes_done(td, &icd);
581e7141
JA
2285
2286 return 0;
97601024
JA
2287}
2288
e7e6cfb4
JA
2289/*
2290 * Called to complete min_events number of io for the async engines.
2291 */
55312f9f 2292int io_u_queued_complete(struct thread_data *td, int min_evts)
97601024 2293{
97601024 2294 struct io_completion_data icd;
00de55ef 2295 struct timespec *tvp = NULL;
191d6634 2296 int ret;
4d06a338 2297 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
97601024 2298
12bb8569 2299 dprint(FD_IO, "io_u_queued_complete: min=%d\n", min_evts);
b271fe62 2300
4950421a 2301 if (!min_evts)
00de55ef 2302 tvp = &ts;
5fb4b366
RE
2303 else if (min_evts > td->cur_depth)
2304 min_evts = td->cur_depth;
97601024 2305
82407585
RP
2306 /* No worries, td_io_getevents fixes min and max if they are
2307 * set incorrectly */
2308 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete_max, tvp);
97601024 2309 if (ret < 0) {
e1161c32 2310 td_verror(td, -ret, "td_io_getevents");
97601024
JA
2311 return ret;
2312 } else if (!ret)
2313 return ret;
2314
9520ebb9 2315 init_icd(td, &icd, ret);
97601024 2316 ios_completed(td, &icd);
581e7141
JA
2317 if (icd.error) {
2318 td_verror(td, icd.error, "io_u_queued_complete");
2319 return -1;
2320 }
97601024 2321
191d6634 2322 io_u_update_bytes_done(td, &icd);
581e7141 2323
0d593542 2324 return ret;
97601024 2325}
7e77dd02
JA
2326
2327/*
2328 * Call when io_u is really queued, to update the submission latency.
2329 */
2330void io_u_queued(struct thread_data *td, struct io_u *io_u)
2331{
8243be59 2332 if (!td->o.disable_slat && ramp_time_over(td) && td->o.stats) {
75dc383e
JA
2333 if (td->parent)
2334 td = td->parent;
ac18dd16 2335 add_slat_sample(td, io_u);
9520ebb9 2336 }
7e77dd02 2337}
433afcb4 2338
5c94b008
JA
2339/*
2340 * See if we should reuse the last seed, if dedupe is enabled
2341 */
9451b93e 2342static struct frand_state *get_buf_state(struct thread_data *td)
5c94b008
JA
2343{
2344 unsigned int v;
0d71aa98 2345 unsigned long long i;
5c94b008
JA
2346
2347 if (!td->o.dedupe_percentage)
2348 return &td->buf_state;
732eedd0 2349 else if (td->o.dedupe_percentage == 100) {
9451b93e
JA
2350 frand_copy(&td->buf_state_prev, &td->buf_state);
2351 return &td->buf_state;
732eedd0 2352 }
5c94b008 2353
1bd5d213 2354 v = rand_between(&td->dedupe_state, 1, 100);
5c94b008 2355
0d71aa98
BD
2356 if (v <= td->o.dedupe_percentage)
2357 switch (td->o.dedupe_mode) {
2358 case DEDUPE_MODE_REPEAT:
2359 /*
2360 * The caller advances the returned frand_state.
2361 * A copy of prev should be returned instead since
2362 * a subsequent intention to generate a deduped buffer
2363 * might result in generating a unique one
2364 */
2365 frand_copy(&td->buf_state_ret, &td->buf_state_prev);
2366 return &td->buf_state_ret;
2367 case DEDUPE_MODE_WORKING_SET:
2368 i = rand_between(&td->dedupe_working_set_index_state, 0, td->num_unique_pages - 1);
2369 frand_copy(&td->buf_state_ret, &td->dedupe_working_set_states[i]);
2370 return &td->buf_state_ret;
2371 default:
2372 log_err("unexpected dedupe mode %u\n", td->o.dedupe_mode);
2373 assert(0);
2374 }
5c94b008
JA
2375
2376 return &td->buf_state;
2377}
2378
9451b93e 2379static void save_buf_state(struct thread_data *td, struct frand_state *rs)
5c94b008 2380{
9451b93e
JA
2381 if (td->o.dedupe_percentage == 100)
2382 frand_copy(rs, &td->buf_state_prev);
2383 else if (rs == &td->buf_state)
5c94b008
JA
2384 frand_copy(&td->buf_state_prev, rs);
2385}
2386
5fff9543
JF
2387void fill_io_buffer(struct thread_data *td, void *buf, unsigned long long min_write,
2388 unsigned long long max_bs)
5973cafb 2389{
d1af2894
JA
2390 struct thread_options *o = &td->o;
2391
15600335
JA
2392 if (o->mem_type == MEM_CUDA_MALLOC)
2393 return;
03553853 2394
4eff3e57 2395 if (o->compress_percentage || o->dedupe_percentage) {
9c42684e 2396 unsigned int perc = td->o.compress_percentage;
eb57e710 2397 struct frand_state *rs = NULL;
5fff9543
JF
2398 unsigned long long left = max_bs;
2399 unsigned long long this_write;
5c94b008 2400
1066358a 2401 do {
eb57e710
BD
2402 /*
2403 * Buffers are either entirely dedupe-able or not.
2404 * If we choose to dedup, the buffer should undergo
2405 * the same manipulation as the original write. Which
2406 * means we should retrack the steps we took for compression
2407 * as well.
2408 */
2409 if (!rs)
2410 rs = get_buf_state(td);
9c42684e 2411
1066358a 2412 min_write = min(min_write, left);
f97a43a1 2413
eb57e710
BD
2414 this_write = min_not_zero(min_write,
2415 (unsigned long long) td->o.compress_chunk);
1e7f82e2 2416
eb57e710
BD
2417 fill_random_buf_percentage(rs, buf, perc,
2418 this_write, this_write,
2419 o->buffer_pattern,
2420 o->buffer_pattern_bytes);
1066358a 2421
1e7f82e2
JA
2422 buf += this_write;
2423 left -= this_write;
9451b93e 2424 save_buf_state(td, rs);
1066358a 2425 } while (left);
d1af2894
JA
2426 } else if (o->buffer_pattern_bytes)
2427 fill_buffer_pattern(td, buf, max_bs);
999d245e 2428 else if (o->zero_buffers)
cc86c395 2429 memset(buf, 0, max_bs);
999d245e 2430 else
9451b93e 2431 fill_random_buf(get_buf_state(td), buf, max_bs);
cc86c395
JA
2432}
2433
2434/*
2435 * "randomly" fill the buffer contents
2436 */
2437void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
5fff9543 2438 unsigned long long min_write, unsigned long long max_bs)
cc86c395
JA
2439{
2440 io_u->buf_filled_len = 0;
2441 fill_io_buffer(td, io_u->buf, min_write, max_bs);
5973cafb 2442}
e2c75fc4
TK
2443
2444static int do_sync_file_range(const struct thread_data *td,
2445 struct fio_file *f)
2446{
7e2a317e 2447 uint64_t offset, nbytes;
e2c75fc4
TK
2448
2449 offset = f->first_write;
2450 nbytes = f->last_write - f->first_write;
2451
2452 if (!nbytes)
2453 return 0;
2454
2455 return sync_file_range(f->fd, offset, nbytes, td->o.sync_file_range);
2456}
2457
2458int do_io_u_sync(const struct thread_data *td, struct io_u *io_u)
2459{
2460 int ret;
2461
2462 if (io_u->ddir == DDIR_SYNC) {
a04e0665 2463#ifdef CONFIG_FCNTL_SYNC
c99c81ad 2464 ret = fcntl(io_u->file->fd, F_FULLFSYNC);
a04e0665 2465#else
e2c75fc4 2466 ret = fsync(io_u->file->fd);
a04e0665 2467#endif
e2c75fc4
TK
2468 } else if (io_u->ddir == DDIR_DATASYNC) {
2469#ifdef CONFIG_FDATASYNC
2470 ret = fdatasync(io_u->file->fd);
2471#else
2472 ret = io_u->xfer_buflen;
2473 io_u->error = EINVAL;
2474#endif
2475 } else if (io_u->ddir == DDIR_SYNC_FILE_RANGE)
2476 ret = do_sync_file_range(td, io_u->file);
2477 else {
2478 ret = io_u->xfer_buflen;
2479 io_u->error = EINVAL;
2480 }
2481
2482 if (ret < 0)
2483 io_u->error = errno;
2484
2485 return ret;
2486}
2487
67282020 2488int do_io_u_trim(struct thread_data *td, struct io_u *io_u)
e2c75fc4
TK
2489{
2490#ifndef FIO_HAVE_TRIM
2491 io_u->error = EINVAL;
2492 return 0;
2493#else
2494 struct fio_file *f = io_u->file;
2495 int ret;
2496
e3be810b
SK
2497 if (td->o.zone_mode == ZONE_MODE_ZBD) {
2498 ret = zbd_do_io_u_trim(td, io_u);
2499 if (ret == io_u_completed)
2500 return io_u->xfer_buflen;
2501 if (ret)
2502 goto err;
2503 }
2504
496b1f9e 2505 ret = os_trim(f, io_u->offset, io_u->xfer_buflen);
e2c75fc4
TK
2506 if (!ret)
2507 return io_u->xfer_buflen;
2508
e3be810b 2509err:
e2c75fc4
TK
2510 io_u->error = ret;
2511 return 0;
2512#endif
2513}