target: fixes
[fio.git] / io_u.c
... / ...
CommitLineData
1#include <unistd.h>
2#include <string.h>
3#include <assert.h>
4
5#include "fio.h"
6#include "verify.h"
7#include "trim.h"
8#include "lib/rand.h"
9#include "lib/axmap.h"
10#include "err.h"
11#include "lib/pow2.h"
12#include "minmax.h"
13#include "zbd.h"
14#include "target.h"
15
16struct io_completion_data {
17 int nr; /* input */
18
19 int error; /* output */
20 uint64_t bytes_done[DDIR_RWDIR_CNT]; /* output */
21 struct timespec time; /* output */
22};
23
24/*
25 * The ->io_axmap contains a map of blocks we have or have not done io
26 * to yet. Used to make sure we cover the entire range in a fair fashion.
27 */
28static bool random_map_free(struct fio_file *f, const uint64_t block)
29{
30 return !axmap_isset(f->io_axmap, block);
31}
32
33/*
34 * Mark a given offset as used in the map.
35 */
36static uint64_t mark_random_map(struct thread_data *td, struct io_u *io_u,
37 uint64_t offset, uint64_t buflen)
38{
39 unsigned long long min_bs = td->o.min_bs[io_u->ddir];
40 struct fio_file *f = io_u->file;
41 unsigned long long nr_blocks;
42 uint64_t block;
43
44 block = (offset - f->file_offset) / (uint64_t) min_bs;
45 nr_blocks = (buflen + min_bs - 1) / min_bs;
46 assert(nr_blocks > 0);
47
48 if (!(io_u->flags & IO_U_F_BUSY_OK)) {
49 nr_blocks = axmap_set_nr(f->io_axmap, block, nr_blocks);
50 assert(nr_blocks > 0);
51 }
52
53 if ((nr_blocks * min_bs) < buflen)
54 buflen = nr_blocks * min_bs;
55
56 return buflen;
57}
58
59static uint64_t last_block(struct thread_data *td, struct fio_file *f,
60 enum fio_ddir ddir)
61{
62 uint64_t max_blocks;
63 uint64_t max_size;
64
65 assert(ddir_rw(ddir));
66
67 /*
68 * Hmm, should we make sure that ->io_size <= ->real_file_size?
69 * -> not for now since there is code assuming it could go either.
70 */
71 max_size = f->io_size;
72 if (max_size > f->real_file_size)
73 max_size = f->real_file_size;
74
75 if (td->o.zone_mode == ZONE_MODE_STRIDED && td->o.zone_range)
76 max_size = td->o.zone_range;
77
78 if (td->o.min_bs[ddir] > td->o.ba[ddir])
79 max_size -= td->o.min_bs[ddir] - td->o.ba[ddir];
80
81 max_blocks = max_size / (uint64_t) td->o.ba[ddir];
82 if (!max_blocks)
83 return 0;
84
85 return max_blocks;
86}
87
88static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
89 enum fio_ddir ddir, uint64_t *b,
90 uint64_t lastb)
91{
92 uint64_t r;
93
94 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE ||
95 td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64) {
96
97 r = __rand(&td->random_state);
98
99 dprint(FD_RANDOM, "off rand %llu\n", (unsigned long long) r);
100
101 *b = lastb * (r / (rand_max(&td->random_state) + 1.0));
102 } else {
103 uint64_t off = 0;
104
105 assert(fio_file_lfsr(f));
106
107 if (lfsr_next(&f->lfsr, &off))
108 return 1;
109
110 *b = off;
111 }
112
113 /*
114 * if we are not maintaining a random map, we are done.
115 */
116 if (!file_randommap(td, f))
117 goto ret;
118
119 /*
120 * calculate map offset and check if it's free
121 */
122 if (random_map_free(f, *b))
123 goto ret;
124
125 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
126 (unsigned long long) *b);
127
128 *b = axmap_next_free(f->io_axmap, *b);
129 if (*b == (uint64_t) -1ULL)
130 return 1;
131ret:
132 return 0;
133}
134
135static int __get_next_rand_offset_zipf(struct thread_data *td,
136 struct fio_file *f, enum fio_ddir ddir,
137 uint64_t *b)
138{
139 *b = zipf_next(&f->zipf);
140 return 0;
141}
142
143static int __get_next_rand_offset_pareto(struct thread_data *td,
144 struct fio_file *f, enum fio_ddir ddir,
145 uint64_t *b)
146{
147 *b = pareto_next(&f->zipf);
148 return 0;
149}
150
151static int __get_next_rand_offset_gauss(struct thread_data *td,
152 struct fio_file *f, enum fio_ddir ddir,
153 uint64_t *b)
154{
155 *b = gauss_next(&f->gauss);
156 return 0;
157}
158
159static int __get_next_rand_offset_zoned_abs(struct thread_data *td,
160 struct fio_file *f,
161 enum fio_ddir ddir, uint64_t *b)
162{
163 struct zone_split_index *zsi;
164 uint64_t lastb, send, stotal;
165 unsigned int v;
166
167 lastb = last_block(td, f, ddir);
168 if (!lastb)
169 return 1;
170
171 if (!td->o.zone_split_nr[ddir]) {
172bail:
173 return __get_next_rand_offset(td, f, ddir, b, lastb);
174 }
175
176 /*
177 * Generate a value, v, between 1 and 100, both inclusive
178 */
179 v = rand_between(&td->zone_state, 1, 100);
180
181 /*
182 * Find our generated table. 'send' is the end block of this zone,
183 * 'stotal' is our start offset.
184 */
185 zsi = &td->zone_state_index[ddir][v - 1];
186 stotal = zsi->size_prev / td->o.ba[ddir];
187 send = zsi->size / td->o.ba[ddir];
188
189 /*
190 * Should never happen
191 */
192 if (send == -1U) {
193 if (!fio_did_warn(FIO_WARN_ZONED_BUG))
194 log_err("fio: bug in zoned generation\n");
195 goto bail;
196 } else if (send > lastb) {
197 /*
198 * This happens if the user specifies ranges that exceed
199 * the file/device size. We can't handle that gracefully,
200 * so error and exit.
201 */
202 log_err("fio: zoned_abs sizes exceed file size\n");
203 return 1;
204 }
205
206 /*
207 * Generate index from 0..send-stotal
208 */
209 if (__get_next_rand_offset(td, f, ddir, b, send - stotal) == 1)
210 return 1;
211
212 *b += stotal;
213 return 0;
214}
215
216static int __get_next_rand_offset_zoned(struct thread_data *td,
217 struct fio_file *f, enum fio_ddir ddir,
218 uint64_t *b)
219{
220 unsigned int v, send, stotal;
221 uint64_t offset, lastb;
222 struct zone_split_index *zsi;
223
224 lastb = last_block(td, f, ddir);
225 if (!lastb)
226 return 1;
227
228 if (!td->o.zone_split_nr[ddir]) {
229bail:
230 return __get_next_rand_offset(td, f, ddir, b, lastb);
231 }
232
233 /*
234 * Generate a value, v, between 1 and 100, both inclusive
235 */
236 v = rand_between(&td->zone_state, 1, 100);
237
238 zsi = &td->zone_state_index[ddir][v - 1];
239 stotal = zsi->size_perc_prev;
240 send = zsi->size_perc;
241
242 /*
243 * Should never happen
244 */
245 if (send == -1U) {
246 if (!fio_did_warn(FIO_WARN_ZONED_BUG))
247 log_err("fio: bug in zoned generation\n");
248 goto bail;
249 }
250
251 /*
252 * 'send' is some percentage below or equal to 100 that
253 * marks the end of the current IO range. 'stotal' marks
254 * the start, in percent.
255 */
256 if (stotal)
257 offset = stotal * lastb / 100ULL;
258 else
259 offset = 0;
260
261 lastb = lastb * (send - stotal) / 100ULL;
262
263 /*
264 * Generate index from 0..send-of-lastb
265 */
266 if (__get_next_rand_offset(td, f, ddir, b, lastb) == 1)
267 return 1;
268
269 /*
270 * Add our start offset, if any
271 */
272 if (offset)
273 *b += offset;
274
275 return 0;
276}
277
278static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
279 enum fio_ddir ddir, uint64_t *b)
280{
281 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM) {
282 uint64_t lastb;
283
284 lastb = last_block(td, f, ddir);
285 if (!lastb)
286 return 1;
287
288 return __get_next_rand_offset(td, f, ddir, b, lastb);
289 } else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
290 return __get_next_rand_offset_zipf(td, f, ddir, b);
291 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
292 return __get_next_rand_offset_pareto(td, f, ddir, b);
293 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
294 return __get_next_rand_offset_gauss(td, f, ddir, b);
295 else if (td->o.random_distribution == FIO_RAND_DIST_ZONED)
296 return __get_next_rand_offset_zoned(td, f, ddir, b);
297 else if (td->o.random_distribution == FIO_RAND_DIST_ZONED_ABS)
298 return __get_next_rand_offset_zoned_abs(td, f, ddir, b);
299
300 log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
301 return 1;
302}
303
304static bool should_do_random(struct thread_data *td, enum fio_ddir ddir)
305{
306 unsigned int v;
307
308 if (td->o.perc_rand[ddir] == 100)
309 return true;
310
311 v = rand_between(&td->seq_rand_state[ddir], 1, 100);
312
313 return v <= td->o.perc_rand[ddir];
314}
315
316static void loop_cache_invalidate(struct thread_data *td, struct fio_file *f)
317{
318 struct thread_options *o = &td->o;
319
320 if (o->invalidate_cache && !o->odirect) {
321 int fio_unused ret;
322
323 ret = file_invalidate_cache(td, f);
324 }
325}
326
327static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
328 enum fio_ddir ddir, uint64_t *b)
329{
330 if (!get_next_rand_offset(td, f, ddir, b))
331 return 0;
332
333 if (td->o.time_based ||
334 (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)) {
335 fio_file_reset(td, f);
336 loop_cache_invalidate(td, f);
337 if (!get_next_rand_offset(td, f, ddir, b))
338 return 0;
339 }
340
341 dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
342 f->file_name, (unsigned long long) f->last_pos[ddir],
343 (unsigned long long) f->real_file_size);
344 return 1;
345}
346
347static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
348 enum fio_ddir ddir, uint64_t *offset)
349{
350 struct thread_options *o = &td->o;
351
352 assert(ddir_rw(ddir));
353
354 /*
355 * If we reach the end for a time based run, reset us back to 0
356 * and invalidate the cache, if we need to.
357 */
358 if (f->last_pos[ddir] >= f->io_size + get_start_offset(td, f) &&
359 o->time_based) {
360 f->last_pos[ddir] = f->file_offset;
361 loop_cache_invalidate(td, f);
362 }
363
364 if (f->last_pos[ddir] < f->real_file_size) {
365 uint64_t pos;
366
367 /*
368 * Only rewind if we already hit the end
369 */
370 if (f->last_pos[ddir] == f->file_offset &&
371 f->file_offset && o->ddir_seq_add < 0) {
372 if (f->real_file_size > f->io_size)
373 f->last_pos[ddir] = f->io_size;
374 else
375 f->last_pos[ddir] = f->real_file_size;
376 }
377
378 pos = f->last_pos[ddir] - f->file_offset;
379 if (pos && o->ddir_seq_add) {
380 pos += o->ddir_seq_add;
381
382 /*
383 * If we reach beyond the end of the file
384 * with holed IO, wrap around to the
385 * beginning again. If we're doing backwards IO,
386 * wrap to the end.
387 */
388 if (pos >= f->real_file_size) {
389 if (o->ddir_seq_add > 0)
390 pos = f->file_offset;
391 else {
392 if (f->real_file_size > f->io_size)
393 pos = f->io_size;
394 else
395 pos = f->real_file_size;
396
397 pos += o->ddir_seq_add;
398 }
399 }
400 }
401
402 *offset = pos;
403 return 0;
404 }
405
406 return 1;
407}
408
409static int get_next_block(struct thread_data *td, struct io_u *io_u,
410 enum fio_ddir ddir, int rw_seq,
411 bool *is_random)
412{
413 struct fio_file *f = io_u->file;
414 uint64_t b, offset;
415 int ret;
416
417 assert(ddir_rw(ddir));
418
419 b = offset = -1ULL;
420
421 if (rw_seq) {
422 if (td_random(td)) {
423 if (should_do_random(td, ddir)) {
424 ret = get_next_rand_block(td, f, ddir, &b);
425 *is_random = true;
426 } else {
427 *is_random = false;
428 io_u_set(td, io_u, IO_U_F_BUSY_OK);
429 ret = get_next_seq_offset(td, f, ddir, &offset);
430 if (ret)
431 ret = get_next_rand_block(td, f, ddir, &b);
432 }
433 } else {
434 *is_random = false;
435 ret = get_next_seq_offset(td, f, ddir, &offset);
436 }
437 } else {
438 io_u_set(td, io_u, IO_U_F_BUSY_OK);
439 *is_random = false;
440
441 if (td->o.rw_seq == RW_SEQ_SEQ) {
442 ret = get_next_seq_offset(td, f, ddir, &offset);
443 if (ret) {
444 ret = get_next_rand_block(td, f, ddir, &b);
445 *is_random = false;
446 }
447 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
448 if (f->last_start[ddir] != -1ULL)
449 offset = f->last_start[ddir] - f->file_offset;
450 else
451 offset = 0;
452 ret = 0;
453 } else {
454 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
455 ret = 1;
456 }
457 }
458
459 if (!ret) {
460 if (offset != -1ULL)
461 io_u->offset = offset;
462 else if (b != -1ULL)
463 io_u->offset = b * td->o.ba[ddir];
464 else {
465 log_err("fio: bug in offset generation: offset=%llu, b=%llu\n", (unsigned long long) offset, (unsigned long long) b);
466 ret = 1;
467 }
468 }
469
470 return ret;
471}
472
473/*
474 * For random io, generate a random new block and see if it's used. Repeat
475 * until we find a free one. For sequential io, just return the end of
476 * the last io issued.
477 */
478static int get_next_offset(struct thread_data *td, struct io_u *io_u,
479 bool *is_random)
480{
481 struct fio_file *f = io_u->file;
482 enum fio_ddir ddir = io_u->ddir;
483 int rw_seq_hit = 0;
484
485 assert(ddir_rw(ddir));
486
487 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
488 rw_seq_hit = 1;
489 td->ddir_seq_nr = td->o.ddir_seq_nr;
490 }
491
492 if (get_next_block(td, io_u, ddir, rw_seq_hit, is_random))
493 return 1;
494
495 if (io_u->offset >= f->io_size) {
496 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
497 (unsigned long long) io_u->offset,
498 (unsigned long long) f->io_size);
499 return 1;
500 }
501
502 io_u->offset += f->file_offset;
503 if (io_u->offset >= f->real_file_size) {
504 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
505 (unsigned long long) io_u->offset,
506 (unsigned long long) f->real_file_size);
507 return 1;
508 }
509
510 return 0;
511}
512
513static inline bool io_u_fits(struct thread_data *td, struct io_u *io_u,
514 unsigned long long buflen)
515{
516 struct fio_file *f = io_u->file;
517
518 return io_u->offset + buflen <= f->io_size + get_start_offset(td, f);
519}
520
521static unsigned long long get_next_buflen(struct thread_data *td, struct io_u *io_u,
522 bool is_random)
523{
524 int ddir = io_u->ddir;
525 unsigned long long buflen = 0;
526 unsigned long long minbs, maxbs;
527 uint64_t frand_max, r;
528 bool power_2;
529
530 assert(ddir_rw(ddir));
531
532 if (td->o.bs_is_seq_rand)
533 ddir = is_random ? DDIR_WRITE : DDIR_READ;
534
535 minbs = td->o.min_bs[ddir];
536 maxbs = td->o.max_bs[ddir];
537
538 if (minbs == maxbs)
539 return minbs;
540
541 /*
542 * If we can't satisfy the min block size from here, then fail
543 */
544 if (!io_u_fits(td, io_u, minbs))
545 return 0;
546
547 frand_max = rand_max(&td->bsrange_state[ddir]);
548 do {
549 r = __rand(&td->bsrange_state[ddir]);
550
551 if (!td->o.bssplit_nr[ddir]) {
552 buflen = minbs + (unsigned long long) ((double) maxbs *
553 (r / (frand_max + 1.0)));
554 } else {
555 long long perc = 0;
556 unsigned int i;
557
558 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
559 struct bssplit *bsp = &td->o.bssplit[ddir][i];
560
561 buflen = bsp->bs;
562 perc += bsp->perc;
563 if (!perc)
564 break;
565 if ((r / perc <= frand_max / 100ULL) &&
566 io_u_fits(td, io_u, buflen))
567 break;
568 }
569 }
570
571 power_2 = is_power_of_2(minbs);
572 if (!td->o.bs_unaligned && power_2)
573 buflen &= ~(minbs - 1);
574 else if (!td->o.bs_unaligned && !power_2)
575 buflen -= buflen % minbs;
576 } while (!io_u_fits(td, io_u, buflen));
577
578 return buflen;
579}
580
581static void set_rwmix_bytes(struct thread_data *td)
582{
583 unsigned int diff;
584
585 /*
586 * we do time or byte based switch. this is needed because
587 * buffered writes may issue a lot quicker than they complete,
588 * whereas reads do not.
589 */
590 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
591 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
592}
593
594static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
595{
596 unsigned int v;
597
598 v = rand_between(&td->rwmix_state, 1, 100);
599
600 if (v <= td->o.rwmix[DDIR_READ])
601 return DDIR_READ;
602
603 return DDIR_WRITE;
604}
605
606int io_u_quiesce(struct thread_data *td)
607{
608 int completed = 0;
609
610 /*
611 * We are going to sleep, ensure that we flush anything pending as
612 * not to skew our latency numbers.
613 *
614 * Changed to only monitor 'in flight' requests here instead of the
615 * td->cur_depth, b/c td->cur_depth does not accurately represent
616 * io's that have been actually submitted to an async engine,
617 * and cur_depth is meaningless for sync engines.
618 */
619 if (td->io_u_queued || td->cur_depth)
620 td_io_commit(td);
621
622 while (td->io_u_in_flight) {
623 int ret;
624
625 ret = io_u_queued_complete(td, 1);
626 if (ret > 0)
627 completed += ret;
628 }
629
630 if (td->flags & TD_F_REGROW_LOGS)
631 regrow_logs(td);
632
633 return completed;
634}
635
636static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
637{
638 enum fio_ddir odir = ddir ^ 1;
639 uint64_t usec;
640 uint64_t now;
641
642 assert(ddir_rw(ddir));
643 now = utime_since_now(&td->start);
644
645 /*
646 * if rate_next_io_time is in the past, need to catch up to rate
647 */
648 if (td->rate_next_io_time[ddir] <= now)
649 return ddir;
650
651 /*
652 * We are ahead of rate in this direction. See if we
653 * should switch.
654 */
655 if (td_rw(td) && td->o.rwmix[odir]) {
656 /*
657 * Other direction is behind rate, switch
658 */
659 if (td->rate_next_io_time[odir] <= now)
660 return odir;
661
662 /*
663 * Both directions are ahead of rate. sleep the min,
664 * switch if necessary
665 */
666 if (td->rate_next_io_time[ddir] <=
667 td->rate_next_io_time[odir]) {
668 usec = td->rate_next_io_time[ddir] - now;
669 } else {
670 usec = td->rate_next_io_time[odir] - now;
671 ddir = odir;
672 }
673 } else
674 usec = td->rate_next_io_time[ddir] - now;
675
676 if (td->o.io_submit_mode == IO_MODE_INLINE)
677 io_u_quiesce(td);
678
679 usec_sleep(td, usec);
680 return ddir;
681}
682
683/*
684 * Return the data direction for the next io_u. If the job is a
685 * mixed read/write workload, check the rwmix cycle and switch if
686 * necessary.
687 */
688static enum fio_ddir get_rw_ddir(struct thread_data *td)
689{
690 enum fio_ddir ddir;
691
692 /*
693 * See if it's time to fsync/fdatasync/sync_file_range first,
694 * and if not then move on to check regular I/Os.
695 */
696 if (should_fsync(td)) {
697 if (td->o.fsync_blocks && td->io_issues[DDIR_WRITE] &&
698 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks))
699 return DDIR_SYNC;
700
701 if (td->o.fdatasync_blocks && td->io_issues[DDIR_WRITE] &&
702 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks))
703 return DDIR_DATASYNC;
704
705 if (td->sync_file_range_nr && td->io_issues[DDIR_WRITE] &&
706 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr))
707 return DDIR_SYNC_FILE_RANGE;
708 }
709
710 if (td_rw(td)) {
711 /*
712 * Check if it's time to seed a new data direction.
713 */
714 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
715 /*
716 * Put a top limit on how many bytes we do for
717 * one data direction, to avoid overflowing the
718 * ranges too much
719 */
720 ddir = get_rand_ddir(td);
721
722 if (ddir != td->rwmix_ddir)
723 set_rwmix_bytes(td);
724
725 td->rwmix_ddir = ddir;
726 }
727 ddir = td->rwmix_ddir;
728 } else if (td_read(td))
729 ddir = DDIR_READ;
730 else if (td_write(td))
731 ddir = DDIR_WRITE;
732 else if (td_trim(td))
733 ddir = DDIR_TRIM;
734 else
735 ddir = DDIR_INVAL;
736
737 td->rwmix_ddir = rate_ddir(td, ddir);
738 return td->rwmix_ddir;
739}
740
741static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
742{
743 enum fio_ddir ddir = get_rw_ddir(td);
744
745 if (td_trimwrite(td)) {
746 struct fio_file *f = io_u->file;
747 if (f->last_pos[DDIR_WRITE] == f->last_pos[DDIR_TRIM])
748 ddir = DDIR_TRIM;
749 else
750 ddir = DDIR_WRITE;
751 }
752
753 io_u->ddir = io_u->acct_ddir = ddir;
754
755 if (io_u->ddir == DDIR_WRITE && td_ioengine_flagged(td, FIO_BARRIER) &&
756 td->o.barrier_blocks &&
757 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
758 td->io_issues[DDIR_WRITE])
759 io_u_set(td, io_u, IO_U_F_BARRIER);
760}
761
762void put_file_log(struct thread_data *td, struct fio_file *f)
763{
764 unsigned int ret = put_file(td, f);
765
766 if (ret)
767 td_verror(td, ret, "file close");
768}
769
770void put_io_u(struct thread_data *td, struct io_u *io_u)
771{
772 const bool needs_lock = td_async_processing(td);
773
774 if (io_u->post_submit) {
775 io_u->post_submit(io_u, io_u->error == 0);
776 io_u->post_submit = NULL;
777 }
778
779 if (td->parent)
780 td = td->parent;
781
782 if (needs_lock)
783 __td_io_u_lock(td);
784
785 if (io_u->file && !(io_u->flags & IO_U_F_NO_FILE_PUT))
786 put_file_log(td, io_u->file);
787
788 io_u->file = NULL;
789 io_u_set(td, io_u, IO_U_F_FREE);
790
791 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
792 td->cur_depth--;
793 assert(!(td->flags & TD_F_CHILD));
794 }
795 io_u_qpush(&td->io_u_freelist, io_u);
796 td_io_u_free_notify(td);
797
798 if (needs_lock)
799 __td_io_u_unlock(td);
800}
801
802void clear_io_u(struct thread_data *td, struct io_u *io_u)
803{
804 io_u_clear(td, io_u, IO_U_F_FLIGHT);
805 put_io_u(td, io_u);
806}
807
808void requeue_io_u(struct thread_data *td, struct io_u **io_u)
809{
810 const bool needs_lock = td_async_processing(td);
811 struct io_u *__io_u = *io_u;
812 enum fio_ddir ddir = acct_ddir(__io_u);
813
814 dprint(FD_IO, "requeue %p\n", __io_u);
815
816 if (td->parent)
817 td = td->parent;
818
819 if (needs_lock)
820 __td_io_u_lock(td);
821
822 io_u_set(td, __io_u, IO_U_F_FREE);
823 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(ddir))
824 td->io_issues[ddir]--;
825
826 io_u_clear(td, __io_u, IO_U_F_FLIGHT);
827 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH) {
828 td->cur_depth--;
829 assert(!(td->flags & TD_F_CHILD));
830 }
831
832 io_u_rpush(&td->io_u_requeues, __io_u);
833 td_io_u_free_notify(td);
834
835 if (needs_lock)
836 __td_io_u_unlock(td);
837
838 *io_u = NULL;
839}
840
841static void setup_strided_zone_mode(struct thread_data *td, struct io_u *io_u)
842{
843 struct fio_file *f = io_u->file;
844
845 assert(td->o.zone_mode == ZONE_MODE_STRIDED);
846 assert(td->o.zone_size);
847 assert(td->o.zone_range);
848
849 /*
850 * See if it's time to switch to a new zone
851 */
852 if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
853 td->zone_bytes = 0;
854 f->file_offset += td->o.zone_range + td->o.zone_skip;
855
856 /*
857 * Wrap from the beginning, if we exceed the file size
858 */
859 if (f->file_offset >= f->real_file_size)
860 f->file_offset = get_start_offset(td, f);
861
862 f->last_pos[io_u->ddir] = f->file_offset;
863 td->io_skip_bytes += td->o.zone_skip;
864 }
865
866 /*
867 * If zone_size > zone_range, then maintain the same zone until
868 * zone_bytes >= zone_size.
869 */
870 if (f->last_pos[io_u->ddir] >= (f->file_offset + td->o.zone_range)) {
871 dprint(FD_IO, "io_u maintain zone offset=%" PRIu64 "/last_pos=%" PRIu64 "\n",
872 f->file_offset, f->last_pos[io_u->ddir]);
873 f->last_pos[io_u->ddir] = f->file_offset;
874 }
875
876 /*
877 * For random: if 'norandommap' is not set and zone_size > zone_range,
878 * map needs to be reset as it's done with zone_range everytime.
879 */
880 if ((td->zone_bytes % td->o.zone_range) == 0)
881 fio_file_reset(td, f);
882}
883
884static int fill_io_u(struct thread_data *td, struct io_u *io_u)
885{
886 bool is_random;
887 uint64_t offset;
888 enum io_u_action ret;
889
890 if (td_ioengine_flagged(td, FIO_NOIO))
891 goto out;
892
893 set_rw_ddir(td, io_u);
894
895 /*
896 * fsync() or fdatasync() or trim etc, we are done
897 */
898 if (!ddir_rw(io_u->ddir))
899 goto out;
900
901 if (td->o.zone_mode == ZONE_MODE_STRIDED)
902 setup_strided_zone_mode(td, io_u);
903
904 /*
905 * No log, let the seq/rand engine retrieve the next buflen and
906 * position.
907 */
908 if (get_next_offset(td, io_u, &is_random)) {
909 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
910 return 1;
911 }
912
913 io_u->buflen = get_next_buflen(td, io_u, is_random);
914 if (!io_u->buflen) {
915 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
916 return 1;
917 }
918
919 offset = io_u->offset;
920 if (td->o.zone_mode == ZONE_MODE_ZBD) {
921 ret = zbd_adjust_block(td, io_u);
922 if (ret == io_u_eof)
923 return 1;
924 }
925
926 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
927 dprint(FD_IO, "io_u %p, off=0x%llx + len=0x%llx exceeds file size=0x%llx\n",
928 io_u,
929 (unsigned long long) io_u->offset, io_u->buflen,
930 (unsigned long long) io_u->file->real_file_size);
931 return 1;
932 }
933
934 /*
935 * mark entry before potentially trimming io_u
936 */
937 if (td_random(td) && file_randommap(td, io_u->file))
938 io_u->buflen = mark_random_map(td, io_u, offset, io_u->buflen);
939
940out:
941 dprint_io_u(io_u, "fill");
942 td->zone_bytes += io_u->buflen;
943 return 0;
944}
945
946static void __io_u_mark_map(uint64_t *map, unsigned int nr)
947{
948 int idx = 0;
949
950 switch (nr) {
951 default:
952 idx = 6;
953 break;
954 case 33 ... 64:
955 idx = 5;
956 break;
957 case 17 ... 32:
958 idx = 4;
959 break;
960 case 9 ... 16:
961 idx = 3;
962 break;
963 case 5 ... 8:
964 idx = 2;
965 break;
966 case 1 ... 4:
967 idx = 1;
968 case 0:
969 break;
970 }
971
972 map[idx]++;
973}
974
975void io_u_mark_submit(struct thread_data *td, unsigned int nr)
976{
977 __io_u_mark_map(td->ts.io_u_submit, nr);
978 td->ts.total_submit++;
979}
980
981void io_u_mark_complete(struct thread_data *td, unsigned int nr)
982{
983 __io_u_mark_map(td->ts.io_u_complete, nr);
984 td->ts.total_complete++;
985}
986
987void io_u_mark_depth(struct thread_data *td, unsigned int nr)
988{
989 int idx = 0;
990
991 switch (td->cur_depth) {
992 default:
993 idx = 6;
994 break;
995 case 32 ... 63:
996 idx = 5;
997 break;
998 case 16 ... 31:
999 idx = 4;
1000 break;
1001 case 8 ... 15:
1002 idx = 3;
1003 break;
1004 case 4 ... 7:
1005 idx = 2;
1006 break;
1007 case 2 ... 3:
1008 idx = 1;
1009 case 1:
1010 break;
1011 }
1012
1013 td->ts.io_u_map[idx] += nr;
1014}
1015
1016static void io_u_mark_lat_nsec(struct thread_data *td, unsigned long long nsec)
1017{
1018 int idx = 0;
1019
1020 assert(nsec < 1000);
1021
1022 switch (nsec) {
1023 case 750 ... 999:
1024 idx = 9;
1025 break;
1026 case 500 ... 749:
1027 idx = 8;
1028 break;
1029 case 250 ... 499:
1030 idx = 7;
1031 break;
1032 case 100 ... 249:
1033 idx = 6;
1034 break;
1035 case 50 ... 99:
1036 idx = 5;
1037 break;
1038 case 20 ... 49:
1039 idx = 4;
1040 break;
1041 case 10 ... 19:
1042 idx = 3;
1043 break;
1044 case 4 ... 9:
1045 idx = 2;
1046 break;
1047 case 2 ... 3:
1048 idx = 1;
1049 case 0 ... 1:
1050 break;
1051 }
1052
1053 assert(idx < FIO_IO_U_LAT_N_NR);
1054 td->ts.io_u_lat_n[idx]++;
1055}
1056
1057static void io_u_mark_lat_usec(struct thread_data *td, unsigned long long usec)
1058{
1059 int idx = 0;
1060
1061 assert(usec < 1000 && usec >= 1);
1062
1063 switch (usec) {
1064 case 750 ... 999:
1065 idx = 9;
1066 break;
1067 case 500 ... 749:
1068 idx = 8;
1069 break;
1070 case 250 ... 499:
1071 idx = 7;
1072 break;
1073 case 100 ... 249:
1074 idx = 6;
1075 break;
1076 case 50 ... 99:
1077 idx = 5;
1078 break;
1079 case 20 ... 49:
1080 idx = 4;
1081 break;
1082 case 10 ... 19:
1083 idx = 3;
1084 break;
1085 case 4 ... 9:
1086 idx = 2;
1087 break;
1088 case 2 ... 3:
1089 idx = 1;
1090 case 0 ... 1:
1091 break;
1092 }
1093
1094 assert(idx < FIO_IO_U_LAT_U_NR);
1095 td->ts.io_u_lat_u[idx]++;
1096}
1097
1098static void io_u_mark_lat_msec(struct thread_data *td, unsigned long long msec)
1099{
1100 int idx = 0;
1101
1102 assert(msec >= 1);
1103
1104 switch (msec) {
1105 default:
1106 idx = 11;
1107 break;
1108 case 1000 ... 1999:
1109 idx = 10;
1110 break;
1111 case 750 ... 999:
1112 idx = 9;
1113 break;
1114 case 500 ... 749:
1115 idx = 8;
1116 break;
1117 case 250 ... 499:
1118 idx = 7;
1119 break;
1120 case 100 ... 249:
1121 idx = 6;
1122 break;
1123 case 50 ... 99:
1124 idx = 5;
1125 break;
1126 case 20 ... 49:
1127 idx = 4;
1128 break;
1129 case 10 ... 19:
1130 idx = 3;
1131 break;
1132 case 4 ... 9:
1133 idx = 2;
1134 break;
1135 case 2 ... 3:
1136 idx = 1;
1137 case 0 ... 1:
1138 break;
1139 }
1140
1141 assert(idx < FIO_IO_U_LAT_M_NR);
1142 td->ts.io_u_lat_m[idx]++;
1143}
1144
1145static void io_u_mark_latency(struct thread_data *td, unsigned long long nsec)
1146{
1147 if (nsec < 1000)
1148 io_u_mark_lat_nsec(td, nsec);
1149 else if (nsec < 1000000)
1150 io_u_mark_lat_usec(td, nsec / 1000);
1151 else
1152 io_u_mark_lat_msec(td, nsec / 1000000);
1153}
1154
1155static unsigned int __get_next_fileno_rand(struct thread_data *td)
1156{
1157 unsigned long fileno;
1158
1159 if (td->o.file_service_type == FIO_FSERVICE_RANDOM) {
1160 uint64_t frand_max = rand_max(&td->next_file_state);
1161 unsigned long r;
1162
1163 r = __rand(&td->next_file_state);
1164 return (unsigned int) ((double) td->o.nr_files
1165 * (r / (frand_max + 1.0)));
1166 }
1167
1168 if (td->o.file_service_type == FIO_FSERVICE_ZIPF)
1169 fileno = zipf_next(&td->next_file_zipf);
1170 else if (td->o.file_service_type == FIO_FSERVICE_PARETO)
1171 fileno = pareto_next(&td->next_file_zipf);
1172 else if (td->o.file_service_type == FIO_FSERVICE_GAUSS)
1173 fileno = gauss_next(&td->next_file_gauss);
1174 else {
1175 log_err("fio: bad file service type: %d\n", td->o.file_service_type);
1176 assert(0);
1177 return 0;
1178 }
1179
1180 return fileno >> FIO_FSERVICE_SHIFT;
1181}
1182
1183/*
1184 * Get next file to service by choosing one at random
1185 */
1186static struct fio_file *get_next_file_rand(struct thread_data *td,
1187 enum fio_file_flags goodf,
1188 enum fio_file_flags badf)
1189{
1190 struct fio_file *f;
1191 int fno;
1192
1193 do {
1194 int opened = 0;
1195
1196 fno = __get_next_fileno_rand(td);
1197
1198 f = td->files[fno];
1199 if (fio_file_done(f))
1200 continue;
1201
1202 if (!fio_file_open(f)) {
1203 int err;
1204
1205 if (td->nr_open_files >= td->o.open_files)
1206 return ERR_PTR(-EBUSY);
1207
1208 err = td_io_open_file(td, f);
1209 if (err)
1210 continue;
1211 opened = 1;
1212 }
1213
1214 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
1215 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
1216 return f;
1217 }
1218 if (opened)
1219 td_io_close_file(td, f);
1220 } while (1);
1221}
1222
1223/*
1224 * Get next file to service by doing round robin between all available ones
1225 */
1226static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
1227 int badf)
1228{
1229 unsigned int old_next_file = td->next_file;
1230 struct fio_file *f;
1231
1232 do {
1233 int opened = 0;
1234
1235 f = td->files[td->next_file];
1236
1237 td->next_file++;
1238 if (td->next_file >= td->o.nr_files)
1239 td->next_file = 0;
1240
1241 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
1242 if (fio_file_done(f)) {
1243 f = NULL;
1244 continue;
1245 }
1246
1247 if (!fio_file_open(f)) {
1248 int err;
1249
1250 if (td->nr_open_files >= td->o.open_files)
1251 return ERR_PTR(-EBUSY);
1252
1253 err = td_io_open_file(td, f);
1254 if (err) {
1255 dprint(FD_FILE, "error %d on open of %s\n",
1256 err, f->file_name);
1257 f = NULL;
1258 continue;
1259 }
1260 opened = 1;
1261 }
1262
1263 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
1264 f->flags);
1265 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
1266 break;
1267
1268 if (opened)
1269 td_io_close_file(td, f);
1270
1271 f = NULL;
1272 } while (td->next_file != old_next_file);
1273
1274 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
1275 return f;
1276}
1277
1278static struct fio_file *__get_next_file(struct thread_data *td)
1279{
1280 struct fio_file *f;
1281
1282 assert(td->o.nr_files <= td->files_index);
1283
1284 if (td->nr_done_files >= td->o.nr_files) {
1285 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
1286 " nr_files=%d\n", td->nr_open_files,
1287 td->nr_done_files,
1288 td->o.nr_files);
1289 return NULL;
1290 }
1291
1292 f = td->file_service_file;
1293 if (f && fio_file_open(f) && !fio_file_closing(f)) {
1294 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
1295 goto out;
1296 if (td->file_service_left--)
1297 goto out;
1298 }
1299
1300 if (td->o.file_service_type == FIO_FSERVICE_RR ||
1301 td->o.file_service_type == FIO_FSERVICE_SEQ)
1302 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
1303 else
1304 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1305
1306 if (IS_ERR(f))
1307 return f;
1308
1309 td->file_service_file = f;
1310 td->file_service_left = td->file_service_nr - 1;
1311out:
1312 if (f)
1313 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1314 else
1315 dprint(FD_FILE, "get_next_file: NULL\n");
1316 return f;
1317}
1318
1319static struct fio_file *get_next_file(struct thread_data *td)
1320{
1321 return __get_next_file(td);
1322}
1323
1324static long set_io_u_file(struct thread_data *td, struct io_u *io_u)
1325{
1326 struct fio_file *f;
1327
1328 do {
1329 f = get_next_file(td);
1330 if (IS_ERR_OR_NULL(f))
1331 return PTR_ERR(f);
1332
1333 io_u->file = f;
1334 get_file(f);
1335
1336 if (!fill_io_u(td, io_u))
1337 break;
1338
1339 if (io_u->post_submit) {
1340 io_u->post_submit(io_u, false);
1341 io_u->post_submit = NULL;
1342 }
1343
1344 put_file_log(td, f);
1345 td_io_close_file(td, f);
1346 io_u->file = NULL;
1347 if (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)
1348 fio_file_reset(td, f);
1349 else {
1350 fio_file_set_done(f);
1351 td->nr_done_files++;
1352 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
1353 td->nr_done_files, td->o.nr_files);
1354 }
1355 } while (1);
1356
1357 return 0;
1358}
1359
1360/*
1361 * If latency target is enabled, we might be ramping up or down and not
1362 * using the full queue depth available.
1363 */
1364bool queue_full(const struct thread_data *td)
1365{
1366 const int qempty = io_u_qempty(&td->io_u_freelist);
1367
1368 if (qempty)
1369 return true;
1370 if (!td->o.latency_target || td->o.iodepth_mode != IOD_STEPPED)
1371 return false;
1372
1373 return td->cur_depth >= td->latency_qd;
1374}
1375
1376struct io_u *__get_io_u(struct thread_data *td)
1377{
1378 const bool needs_lock = td_async_processing(td);
1379 struct io_u *io_u = NULL;
1380 int ret;
1381
1382 if (td->stop_io)
1383 return NULL;
1384
1385 if (needs_lock)
1386 __td_io_u_lock(td);
1387
1388again:
1389 if (!io_u_rempty(&td->io_u_requeues))
1390 io_u = io_u_rpop(&td->io_u_requeues);
1391 else if (!queue_full(td)) {
1392 io_u = io_u_qpop(&td->io_u_freelist);
1393
1394 io_u->file = NULL;
1395 io_u->buflen = 0;
1396 io_u->resid = 0;
1397 io_u->end_io = NULL;
1398 }
1399
1400 if (io_u) {
1401 assert(io_u->flags & IO_U_F_FREE);
1402 io_u_clear(td, io_u, IO_U_F_FREE | IO_U_F_NO_FILE_PUT |
1403 IO_U_F_TRIMMED | IO_U_F_BARRIER |
1404 IO_U_F_VER_LIST);
1405
1406 io_u->error = 0;
1407 io_u->acct_ddir = -1;
1408 td->cur_depth++;
1409 assert(!(td->flags & TD_F_CHILD));
1410 io_u_set(td, io_u, IO_U_F_IN_CUR_DEPTH);
1411 io_u->ipo = NULL;
1412 } else if (td_async_processing(td)) {
1413 /*
1414 * We ran out, wait for async verify threads to finish and
1415 * return one
1416 */
1417 assert(!(td->flags & TD_F_CHILD));
1418 ret = pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1419 assert(ret == 0);
1420 goto again;
1421 }
1422
1423 if (needs_lock)
1424 __td_io_u_unlock(td);
1425
1426 return io_u;
1427}
1428
1429static bool check_get_trim(struct thread_data *td, struct io_u *io_u)
1430{
1431 if (!(td->flags & TD_F_TRIM_BACKLOG))
1432 return false;
1433 if (!td->trim_entries)
1434 return false;
1435
1436 if (td->trim_batch) {
1437 td->trim_batch--;
1438 if (get_next_trim(td, io_u))
1439 return true;
1440 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1441 td->last_ddir != DDIR_READ) {
1442 td->trim_batch = td->o.trim_batch;
1443 if (!td->trim_batch)
1444 td->trim_batch = td->o.trim_backlog;
1445 if (get_next_trim(td, io_u))
1446 return true;
1447 }
1448
1449 return false;
1450}
1451
1452static bool check_get_verify(struct thread_data *td, struct io_u *io_u)
1453{
1454 if (!(td->flags & TD_F_VER_BACKLOG))
1455 return false;
1456
1457 if (td->io_hist_len) {
1458 int get_verify = 0;
1459
1460 if (td->verify_batch)
1461 get_verify = 1;
1462 else if (!(td->io_hist_len % td->o.verify_backlog) &&
1463 td->last_ddir != DDIR_READ) {
1464 td->verify_batch = td->o.verify_batch;
1465 if (!td->verify_batch)
1466 td->verify_batch = td->o.verify_backlog;
1467 get_verify = 1;
1468 }
1469
1470 if (get_verify && !get_next_verify(td, io_u)) {
1471 td->verify_batch--;
1472 return true;
1473 }
1474 }
1475
1476 return false;
1477}
1478
1479/*
1480 * Fill offset and start time into the buffer content, to prevent too
1481 * easy compressible data for simple de-dupe attempts. Do this for every
1482 * 512b block in the range, since that should be the smallest block size
1483 * we can expect from a device.
1484 */
1485static void small_content_scramble(struct io_u *io_u)
1486{
1487 unsigned long long i, nr_blocks = io_u->buflen >> 9;
1488 unsigned int offset;
1489 uint64_t boffset, *iptr;
1490 char *p;
1491
1492 if (!nr_blocks)
1493 return;
1494
1495 p = io_u->xfer_buf;
1496 boffset = io_u->offset;
1497
1498 if (io_u->buf_filled_len)
1499 io_u->buf_filled_len = 0;
1500
1501 /*
1502 * Generate random index between 0..7. We do chunks of 512b, if
1503 * we assume a cacheline is 64 bytes, then we have 8 of those.
1504 * Scramble content within the blocks in the same cacheline to
1505 * speed things up.
1506 */
1507 offset = (io_u->start_time.tv_nsec ^ boffset) & 7;
1508
1509 for (i = 0; i < nr_blocks; i++) {
1510 /*
1511 * Fill offset into start of cacheline, time into end
1512 * of cacheline
1513 */
1514 iptr = (void *) p + (offset << 6);
1515 *iptr = boffset;
1516
1517 iptr = (void *) p + 64 - 2 * sizeof(uint64_t);
1518 iptr[0] = io_u->start_time.tv_sec;
1519 iptr[1] = io_u->start_time.tv_nsec;
1520
1521 p += 512;
1522 boffset += 512;
1523 }
1524}
1525
1526/*
1527 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1528 * etc. The returned io_u is fully ready to be prepped, populated and submitted.
1529 */
1530struct io_u *get_io_u(struct thread_data *td)
1531{
1532 struct fio_file *f;
1533 struct io_u *io_u;
1534 int do_scramble = 0;
1535 long ret = 0;
1536
1537 io_u = __get_io_u(td);
1538 if (!io_u) {
1539 dprint(FD_IO, "__get_io_u failed\n");
1540 return NULL;
1541 }
1542
1543 if (check_get_verify(td, io_u))
1544 goto out;
1545 if (check_get_trim(td, io_u))
1546 goto out;
1547
1548 /*
1549 * from a requeue, io_u already setup
1550 */
1551 if (io_u->file)
1552 goto out;
1553
1554 /*
1555 * If using an iolog, grab next piece if any available.
1556 */
1557 if (td->flags & TD_F_READ_IOLOG) {
1558 if (read_iolog_get(td, io_u))
1559 goto err_put;
1560 } else if (set_io_u_file(td, io_u)) {
1561 ret = -EBUSY;
1562 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1563 goto err_put;
1564 }
1565
1566 f = io_u->file;
1567 if (!f) {
1568 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1569 goto err_put;
1570 }
1571
1572 assert(fio_file_open(f));
1573
1574 if (ddir_rw(io_u->ddir)) {
1575 if (!io_u->buflen && !td_ioengine_flagged(td, FIO_NOIO)) {
1576 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
1577 goto err_put;
1578 }
1579
1580 f->last_start[io_u->ddir] = io_u->offset;
1581 f->last_pos[io_u->ddir] = io_u->offset + io_u->buflen;
1582
1583 if (io_u->ddir == DDIR_WRITE) {
1584 if (td->flags & TD_F_REFILL_BUFFERS) {
1585 io_u_fill_buffer(td, io_u,
1586 td->o.min_bs[DDIR_WRITE],
1587 io_u->buflen);
1588 } else if ((td->flags & TD_F_SCRAMBLE_BUFFERS) &&
1589 !(td->flags & TD_F_COMPRESS) &&
1590 !(td->flags & TD_F_DO_VERIFY))
1591 do_scramble = 1;
1592 } else if (io_u->ddir == DDIR_READ) {
1593 /*
1594 * Reset the buf_filled parameters so next time if the
1595 * buffer is used for writes it is refilled.
1596 */
1597 io_u->buf_filled_len = 0;
1598 }
1599 }
1600
1601 /*
1602 * Set io data pointers.
1603 */
1604 io_u->xfer_buf = io_u->buf;
1605 io_u->xfer_buflen = io_u->buflen;
1606
1607out:
1608 assert(io_u->file);
1609 if (!td_io_prep(td, io_u)) {
1610 if (!td->o.disable_lat)
1611 fio_gettime(&io_u->start_time, NULL);
1612
1613 if (do_scramble)
1614 small_content_scramble(io_u);
1615
1616 return io_u;
1617 }
1618err_put:
1619 dprint(FD_IO, "get_io_u failed\n");
1620 put_io_u(td, io_u);
1621 return ERR_PTR(ret);
1622}
1623
1624static void __io_u_log_error(struct thread_data *td, struct io_u *io_u)
1625{
1626 enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
1627
1628 if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1629 return;
1630
1631 log_err("fio: io_u error%s%s: %s: %s offset=%llu, buflen=%llu\n",
1632 io_u->file ? " on file " : "",
1633 io_u->file ? io_u->file->file_name : "",
1634 strerror(io_u->error),
1635 io_ddir_name(io_u->ddir),
1636 io_u->offset, io_u->xfer_buflen);
1637
1638 if (td->io_ops->errdetails) {
1639 char *err = td->io_ops->errdetails(io_u);
1640
1641 log_err("fio: %s\n", err);
1642 free(err);
1643 }
1644
1645 if (!td->error)
1646 td_verror(td, io_u->error, "io_u error");
1647}
1648
1649void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1650{
1651 __io_u_log_error(td, io_u);
1652 if (td->parent)
1653 __io_u_log_error(td->parent, io_u);
1654}
1655
1656static inline bool gtod_reduce(struct thread_data *td)
1657{
1658 return (td->o.disable_clat && td->o.disable_slat && td->o.disable_bw)
1659 || td->o.gtod_reduce;
1660}
1661
1662static void trim_block_info(struct thread_data *td, struct io_u *io_u)
1663{
1664 uint32_t *info = io_u_block_info(td, io_u);
1665
1666 if (BLOCK_INFO_STATE(*info) >= BLOCK_STATE_TRIM_FAILURE)
1667 return;
1668
1669 *info = BLOCK_INFO(BLOCK_STATE_TRIMMED, BLOCK_INFO_TRIMS(*info) + 1);
1670}
1671
1672static void account_io_completion(struct thread_data *td, struct io_u *io_u,
1673 struct io_completion_data *icd,
1674 const enum fio_ddir idx, unsigned int bytes)
1675{
1676 const int no_reduce = !gtod_reduce(td);
1677 unsigned long long llnsec = 0;
1678
1679 if (td->parent)
1680 td = td->parent;
1681
1682 if (!td->o.stats || td_ioengine_flagged(td, FIO_NOSTATS))
1683 return;
1684
1685 if (no_reduce)
1686 llnsec = ntime_since(&io_u->issue_time, &icd->time);
1687
1688 if (!td->o.disable_lat) {
1689 unsigned long long tnsec;
1690
1691 tnsec = ntime_since(&io_u->start_time, &icd->time);
1692 add_lat_sample(td, idx, tnsec, bytes, io_u->offset);
1693
1694 if (td->flags & TD_F_PROFILE_OPS) {
1695 struct prof_io_ops *ops = &td->prof_io_ops;
1696
1697 if (ops->io_u_lat)
1698 icd->error = ops->io_u_lat(td, tnsec);
1699 }
1700
1701 if (td->o.max_latency && tnsec > td->o.max_latency) {
1702 icd->error = ETIMEDOUT;
1703 lat_fatal(td, tnsec, td->o.max_latency);
1704 }
1705 if (td->o.latency_target && tnsec > td->o.latency_target) {
1706 if (lat_target_failed(td)) {
1707 icd->error = ETIMEDOUT;
1708 lat_fatal(td, tnsec, td->o.latency_target);
1709 }
1710 }
1711 }
1712
1713 if (ddir_rw(idx)) {
1714 if (!td->o.disable_clat) {
1715 add_clat_sample(td, idx, llnsec, bytes, io_u->offset);
1716 io_u_mark_latency(td, llnsec);
1717 }
1718
1719 if (!td->o.disable_bw && per_unit_log(td->bw_log))
1720 add_bw_sample(td, io_u, bytes, llnsec);
1721
1722 if (no_reduce && per_unit_log(td->iops_log))
1723 add_iops_sample(td, io_u, bytes);
1724 } else if (ddir_sync(idx) && !td->o.disable_clat)
1725 add_sync_clat_sample(&td->ts, llnsec);
1726
1727 if (td->ts.nr_block_infos && io_u->ddir == DDIR_TRIM)
1728 trim_block_info(td, io_u);
1729}
1730
1731static void file_log_write_comp(const struct thread_data *td, struct fio_file *f,
1732 uint64_t offset, unsigned int bytes)
1733{
1734 int idx;
1735
1736 if (!f)
1737 return;
1738
1739 if (f->first_write == -1ULL || offset < f->first_write)
1740 f->first_write = offset;
1741 if (f->last_write == -1ULL || ((offset + bytes) > f->last_write))
1742 f->last_write = offset + bytes;
1743
1744 if (!f->last_write_comp)
1745 return;
1746
1747 idx = f->last_write_idx++;
1748 f->last_write_comp[idx] = offset;
1749 if (f->last_write_idx == td->o.iodepth)
1750 f->last_write_idx = 0;
1751}
1752
1753static bool should_account(struct thread_data *td)
1754{
1755 if (td->parent)
1756 td = td->parent;
1757
1758 return lat_step_account(td) && ramp_time_over(td) &&
1759 (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING);
1760}
1761
1762static void io_completed(struct thread_data *td, struct io_u **io_u_ptr,
1763 struct io_completion_data *icd)
1764{
1765 struct io_u *io_u = *io_u_ptr;
1766 enum fio_ddir ddir = io_u->ddir;
1767 struct fio_file *f = io_u->file;
1768
1769 dprint_io_u(io_u, "complete");
1770
1771 assert(io_u->flags & IO_U_F_FLIGHT);
1772 io_u_clear(td, io_u, IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
1773
1774 /*
1775 * Mark IO ok to verify
1776 */
1777 if (io_u->ipo) {
1778 /*
1779 * Remove errored entry from the verification list
1780 */
1781 if (io_u->error)
1782 unlog_io_piece(td, io_u);
1783 else {
1784 io_u->ipo->flags &= ~IP_F_IN_FLIGHT;
1785 write_barrier();
1786 }
1787 }
1788
1789 if (ddir_sync(ddir)) {
1790 td->last_was_sync = true;
1791 if (f) {
1792 f->first_write = -1ULL;
1793 f->last_write = -1ULL;
1794 }
1795 if (should_account(td))
1796 account_io_completion(td, io_u, icd, ddir, io_u->buflen);
1797 return;
1798 }
1799
1800 td->last_was_sync = false;
1801 td->last_ddir = ddir;
1802
1803 if (!io_u->error && ddir_rw(ddir)) {
1804 unsigned long long bytes = io_u->buflen - io_u->resid;
1805 int ret;
1806
1807 td->io_blocks[ddir]++;
1808 td->io_bytes[ddir] += bytes;
1809
1810 if (!(io_u->flags & IO_U_F_VER_LIST)) {
1811 td->this_io_blocks[ddir]++;
1812 td->this_io_bytes[ddir] += bytes;
1813 }
1814
1815 if (ddir == DDIR_WRITE)
1816 file_log_write_comp(td, f, io_u->offset, bytes);
1817
1818 if (should_account(td))
1819 account_io_completion(td, io_u, icd, ddir, bytes);
1820
1821 icd->bytes_done[ddir] += bytes;
1822
1823 if (io_u->end_io) {
1824 ret = io_u->end_io(td, io_u_ptr);
1825 io_u = *io_u_ptr;
1826 if (ret && !icd->error)
1827 icd->error = ret;
1828 }
1829 } else if (io_u->error) {
1830 icd->error = io_u->error;
1831 io_u_log_error(td, io_u);
1832 }
1833 if (icd->error) {
1834 enum error_type_bit eb = td_error_type(ddir, icd->error);
1835
1836 if (!td_non_fatal_error(td, eb, icd->error))
1837 return;
1838
1839 /*
1840 * If there is a non_fatal error, then add to the error count
1841 * and clear all the errors.
1842 */
1843 update_error_count(td, icd->error);
1844 td_clear_error(td);
1845 icd->error = 0;
1846 if (io_u)
1847 io_u->error = 0;
1848 }
1849}
1850
1851static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1852 int nr)
1853{
1854 int ddir;
1855
1856 if (!gtod_reduce(td))
1857 fio_gettime(&icd->time, NULL);
1858
1859 icd->nr = nr;
1860
1861 icd->error = 0;
1862 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
1863 icd->bytes_done[ddir] = 0;
1864}
1865
1866static void ios_completed(struct thread_data *td,
1867 struct io_completion_data *icd)
1868{
1869 struct io_u *io_u;
1870 int i;
1871
1872 for (i = 0; i < icd->nr; i++) {
1873 io_u = td->io_ops->event(td, i);
1874
1875 io_completed(td, &io_u, icd);
1876
1877 if (io_u)
1878 put_io_u(td, io_u);
1879 }
1880}
1881
1882/*
1883 * Complete a single io_u for the sync engines.
1884 */
1885int io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
1886{
1887 struct io_completion_data icd;
1888 int ddir;
1889
1890 init_icd(td, &icd, 1);
1891 io_completed(td, &io_u, &icd);
1892
1893 if (io_u)
1894 put_io_u(td, io_u);
1895
1896 if (icd.error) {
1897 td_verror(td, icd.error, "io_u_sync_complete");
1898 return -1;
1899 }
1900
1901 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
1902 td->bytes_done[ddir] += icd.bytes_done[ddir];
1903
1904 return 0;
1905}
1906
1907/*
1908 * Called to complete min_events number of io for the async engines.
1909 */
1910int io_u_queued_complete(struct thread_data *td, int min_evts)
1911{
1912 struct io_completion_data icd;
1913 struct timespec *tvp = NULL;
1914 int ret, ddir;
1915 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
1916
1917 dprint(FD_IO, "io_u_queued_complete: min=%d\n", min_evts);
1918
1919 if (!min_evts)
1920 tvp = &ts;
1921 else if (min_evts > td->cur_depth)
1922 min_evts = td->cur_depth;
1923
1924 /* No worries, td_io_getevents fixes min and max if they are
1925 * set incorrectly */
1926 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete_max, tvp);
1927 if (ret < 0) {
1928 td_verror(td, -ret, "td_io_getevents");
1929 return ret;
1930 } else if (!ret)
1931 return ret;
1932
1933 init_icd(td, &icd, ret);
1934 ios_completed(td, &icd);
1935 if (icd.error) {
1936 td_verror(td, icd.error, "io_u_queued_complete");
1937 return -1;
1938 }
1939
1940 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
1941 td->bytes_done[ddir] += icd.bytes_done[ddir];
1942
1943 return ret;
1944}
1945
1946/*
1947 * Call when io_u is really queued, to update the submission latency.
1948 */
1949void io_u_queued(struct thread_data *td, struct io_u *io_u)
1950{
1951 if (!td->o.disable_slat && ramp_time_over(td) && td->o.stats) {
1952 unsigned long slat_time;
1953
1954 slat_time = ntime_since(&io_u->start_time, &io_u->issue_time);
1955
1956 if (td->parent)
1957 td = td->parent;
1958
1959 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen,
1960 io_u->offset);
1961 }
1962}
1963
1964/*
1965 * See if we should reuse the last seed, if dedupe is enabled
1966 */
1967static struct frand_state *get_buf_state(struct thread_data *td)
1968{
1969 unsigned int v;
1970
1971 if (!td->o.dedupe_percentage)
1972 return &td->buf_state;
1973 else if (td->o.dedupe_percentage == 100) {
1974 frand_copy(&td->buf_state_prev, &td->buf_state);
1975 return &td->buf_state;
1976 }
1977
1978 v = rand_between(&td->dedupe_state, 1, 100);
1979
1980 if (v <= td->o.dedupe_percentage)
1981 return &td->buf_state_prev;
1982
1983 return &td->buf_state;
1984}
1985
1986static void save_buf_state(struct thread_data *td, struct frand_state *rs)
1987{
1988 if (td->o.dedupe_percentage == 100)
1989 frand_copy(rs, &td->buf_state_prev);
1990 else if (rs == &td->buf_state)
1991 frand_copy(&td->buf_state_prev, rs);
1992}
1993
1994void fill_io_buffer(struct thread_data *td, void *buf, unsigned long long min_write,
1995 unsigned long long max_bs)
1996{
1997 struct thread_options *o = &td->o;
1998
1999 if (o->mem_type == MEM_CUDA_MALLOC)
2000 return;
2001
2002 if (o->compress_percentage || o->dedupe_percentage) {
2003 unsigned int perc = td->o.compress_percentage;
2004 struct frand_state *rs;
2005 unsigned long long left = max_bs;
2006 unsigned long long this_write;
2007
2008 do {
2009 rs = get_buf_state(td);
2010
2011 min_write = min(min_write, left);
2012
2013 if (perc) {
2014 this_write = min_not_zero(min_write,
2015 (unsigned long long) td->o.compress_chunk);
2016
2017 fill_random_buf_percentage(rs, buf, perc,
2018 this_write, this_write,
2019 o->buffer_pattern,
2020 o->buffer_pattern_bytes);
2021 } else {
2022 fill_random_buf(rs, buf, min_write);
2023 this_write = min_write;
2024 }
2025
2026 buf += this_write;
2027 left -= this_write;
2028 save_buf_state(td, rs);
2029 } while (left);
2030 } else if (o->buffer_pattern_bytes)
2031 fill_buffer_pattern(td, buf, max_bs);
2032 else if (o->zero_buffers)
2033 memset(buf, 0, max_bs);
2034 else
2035 fill_random_buf(get_buf_state(td), buf, max_bs);
2036}
2037
2038/*
2039 * "randomly" fill the buffer contents
2040 */
2041void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
2042 unsigned long long min_write, unsigned long long max_bs)
2043{
2044 io_u->buf_filled_len = 0;
2045 fill_io_buffer(td, io_u->buf, min_write, max_bs);
2046}
2047
2048static int do_sync_file_range(const struct thread_data *td,
2049 struct fio_file *f)
2050{
2051 off64_t offset, nbytes;
2052
2053 offset = f->first_write;
2054 nbytes = f->last_write - f->first_write;
2055
2056 if (!nbytes)
2057 return 0;
2058
2059 return sync_file_range(f->fd, offset, nbytes, td->o.sync_file_range);
2060}
2061
2062int do_io_u_sync(const struct thread_data *td, struct io_u *io_u)
2063{
2064 int ret;
2065
2066 if (io_u->ddir == DDIR_SYNC) {
2067 ret = fsync(io_u->file->fd);
2068 } else if (io_u->ddir == DDIR_DATASYNC) {
2069#ifdef CONFIG_FDATASYNC
2070 ret = fdatasync(io_u->file->fd);
2071#else
2072 ret = io_u->xfer_buflen;
2073 io_u->error = EINVAL;
2074#endif
2075 } else if (io_u->ddir == DDIR_SYNC_FILE_RANGE)
2076 ret = do_sync_file_range(td, io_u->file);
2077 else {
2078 ret = io_u->xfer_buflen;
2079 io_u->error = EINVAL;
2080 }
2081
2082 if (ret < 0)
2083 io_u->error = errno;
2084
2085 return ret;
2086}
2087
2088int do_io_u_trim(const struct thread_data *td, struct io_u *io_u)
2089{
2090#ifndef FIO_HAVE_TRIM
2091 io_u->error = EINVAL;
2092 return 0;
2093#else
2094 struct fio_file *f = io_u->file;
2095 int ret;
2096
2097 ret = os_trim(f, io_u->offset, io_u->xfer_buflen);
2098 if (!ret)
2099 return io_u->xfer_buflen;
2100
2101 io_u->error = ret;
2102 return 0;
2103#endif
2104}