16 struct io_completion_data {
19 int error; /* output */
20 uint64_t bytes_done[DDIR_RWDIR_CNT]; /* output */
21 struct timespec time; /* output */
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.
28 static bool random_map_free(struct fio_file *f, const uint64_t block)
30 return !axmap_isset(f->io_axmap, block);
34 * Mark a given offset as used in the map.
36 static uint64_t mark_random_map(struct thread_data *td, struct io_u *io_u,
37 uint64_t offset, uint64_t buflen)
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;
44 block = (offset - f->file_offset) / (uint64_t) min_bs;
45 nr_blocks = (buflen + min_bs - 1) / min_bs;
46 assert(nr_blocks > 0);
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);
53 if ((nr_blocks * min_bs) < buflen)
54 buflen = nr_blocks * min_bs;
59 static uint64_t last_block(struct thread_data *td, struct fio_file *f,
65 assert(ddir_rw(ddir));
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.
71 max_size = f->io_size;
72 if (max_size > f->real_file_size)
73 max_size = f->real_file_size;
75 if (td->o.zone_mode == ZONE_MODE_STRIDED && td->o.zone_range)
76 max_size = td->o.zone_range;
78 if (td->o.min_bs[ddir] > td->o.ba[ddir])
79 max_size -= td->o.min_bs[ddir] - td->o.ba[ddir];
81 max_blocks = max_size / (uint64_t) td->o.ba[ddir];
89 static int __get_next_rand_offset_sprandom(struct thread_data *td, struct fio_file *f,
90 enum fio_ddir ddir, uint64_t *b,
93 assert(ddir == DDIR_WRITE);
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");
104 static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
105 enum fio_ddir ddir, uint64_t *b,
110 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE ||
111 td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64) {
113 r = __rand(&td->random_state);
115 dprint(FD_RANDOM, "off rand %llu\n", (unsigned long long) r);
117 *b = lastb * (r / (rand_max(&td->random_state) + 1.0));
121 assert(fio_file_lfsr(f));
123 if (lfsr_next(&f->lfsr, &off))
130 * if we are not maintaining a random map, we are done.
132 if (!file_randommap(td, f))
136 * calculate map offset and check if it's free
138 if (random_map_free(f, *b))
141 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
142 (unsigned long long) *b);
144 *b = axmap_next_free(f->io_axmap, *b);
145 if (*b == (uint64_t) -1ULL)
151 static int __get_next_rand_offset_zipf(struct thread_data *td,
152 struct fio_file *f, enum fio_ddir ddir,
155 *b = zipf_next(&f->zipf);
159 static int __get_next_rand_offset_pareto(struct thread_data *td,
160 struct fio_file *f, enum fio_ddir ddir,
163 *b = pareto_next(&f->zipf);
167 static int __get_next_rand_offset_gauss(struct thread_data *td,
168 struct fio_file *f, enum fio_ddir ddir,
171 *b = gauss_next(&f->gauss);
175 static int __get_next_rand_offset_zoned_abs(struct thread_data *td,
177 enum fio_ddir ddir, uint64_t *b)
179 struct zone_split_index *zsi;
180 uint64_t lastb, send, stotal;
183 lastb = last_block(td, f, ddir);
187 if (!td->o.zone_split_nr[ddir]) {
189 return __get_next_rand_offset(td, f, ddir, b, lastb);
193 * Generate a value, v, between 1 and 100, both inclusive
195 v = rand_between(&td->zone_state, 1, 100);
198 * Find our generated table. 'send' is the end block of this zone,
199 * 'stotal' is our start offset.
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];
206 * Should never happen
209 if (!fio_did_warn(FIO_WARN_ZONED_BUG))
210 log_err("fio: bug in zoned generation\n");
212 } else if (send > lastb) {
214 * This happens if the user specifies ranges that exceed
215 * the file/device size. We can't handle that gracefully,
218 log_err("fio: zoned_abs sizes exceed file size\n");
223 * Generate index from 0..send-stotal
225 if (__get_next_rand_offset(td, f, ddir, b, send - stotal) == 1)
232 static int __get_next_rand_offset_zoned(struct thread_data *td,
233 struct fio_file *f, enum fio_ddir ddir,
236 unsigned int v, send, stotal;
237 uint64_t offset, lastb;
238 struct zone_split_index *zsi;
240 lastb = last_block(td, f, ddir);
244 if (!td->o.zone_split_nr[ddir]) {
246 return __get_next_rand_offset(td, f, ddir, b, lastb);
250 * Generate a value, v, between 1 and 100, both inclusive
252 v = rand_between(&td->zone_state, 1, 100);
254 zsi = &td->zone_state_index[ddir][v - 1];
255 stotal = zsi->size_perc_prev;
256 send = zsi->size_perc;
259 * Should never happen
262 if (!fio_did_warn(FIO_WARN_ZONED_BUG))
263 log_err("fio: bug in zoned generation\n");
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.
273 offset = stotal * lastb / 100ULL;
277 lastb = lastb * (send - stotal) / 100ULL;
280 * Generate index from 0..send-of-lastb
282 if (__get_next_rand_offset(td, f, ddir, b, lastb) == 1)
286 * Add our start offset, if any
294 static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
295 enum fio_ddir ddir, uint64_t *b)
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) {
302 lastb = last_block(td, f, ddir);
306 return __get_next_rand_offset(td, f, ddir, b, lastb);
307 } else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
308 return __get_next_rand_offset_zipf(td, f, ddir, b);
309 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
310 return __get_next_rand_offset_pareto(td, f, ddir, b);
311 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
312 return __get_next_rand_offset_gauss(td, f, ddir, b);
313 else if (td->o.random_distribution == FIO_RAND_DIST_ZONED)
314 return __get_next_rand_offset_zoned(td, f, ddir, b);
315 else if (td->o.random_distribution == FIO_RAND_DIST_ZONED_ABS)
316 return __get_next_rand_offset_zoned_abs(td, f, ddir, b);
318 log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
322 static bool should_do_random(struct thread_data *td, enum fio_ddir ddir)
326 if (td->o.perc_rand[ddir] == 100)
329 v = rand_between(&td->seq_rand_state[ddir], 1, 100);
331 return v <= td->o.perc_rand[ddir];
334 static void loop_cache_invalidate(struct thread_data *td, struct fio_file *f)
336 struct thread_options *o = &td->o;
338 if (o->invalidate_cache && !o->odirect) {
341 ret = file_invalidate_cache(td, f);
345 static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
346 enum fio_ddir ddir, uint64_t *b)
348 if (!get_next_rand_offset(td, f, ddir, b))
351 if (td->o.time_based ||
352 (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)) {
353 fio_file_reset(td, f);
354 loop_cache_invalidate(td, f);
355 if (!get_next_rand_offset(td, f, ddir, b))
359 dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
360 f->file_name, (unsigned long long) f->last_pos[ddir],
361 (unsigned long long) f->real_file_size);
365 static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
366 enum fio_ddir ddir, uint64_t *offset)
368 struct thread_options *o = &td->o;
370 assert(ddir_rw(ddir));
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.
376 if (f->last_pos[ddir] >= f->io_size + get_start_offset(td, f) &&
377 o->time_based && o->nr_files == 1) {
378 f->last_pos[ddir] = f->file_offset;
379 loop_cache_invalidate(td, f);
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.
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);
393 if (f->last_pos[ddir] < f->real_file_size) {
397 * Only rewind if we already hit the end
399 if (f->last_pos[ddir] == f->file_offset &&
400 f->file_offset && o->ddir_seq_add < 0) {
401 if (f->real_file_size > f->io_size)
402 f->last_pos[ddir] = f->io_size;
404 f->last_pos[ddir] = f->real_file_size;
407 pos = f->last_pos[ddir] - f->file_offset;
408 if (pos && o->ddir_seq_add) {
409 pos += o->ddir_seq_add;
412 * If we reach beyond the end of the file
413 * with holed IO, wrap around to the
414 * beginning again. If we're doing backwards IO,
417 if (pos >= f->real_file_size) {
418 if (o->ddir_seq_add > 0)
419 pos = f->file_offset;
421 if (f->real_file_size > f->io_size)
424 pos = f->real_file_size;
426 pos += o->ddir_seq_add;
438 static int get_next_block(struct thread_data *td, struct io_u *io_u,
439 enum fio_ddir ddir, int rw_seq,
442 struct fio_file *f = io_u->file;
446 assert(ddir_rw(ddir));
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);
453 offset = f->last_start[DDIR_TRIM] - f->file_offset;
458 if (should_do_random(td, ddir)) {
459 ret = get_next_rand_block(td, f, ddir, &b);
463 io_u_set(td, io_u, IO_U_F_BUSY_OK);
464 ret = get_next_seq_offset(td, f, ddir, &offset);
466 ret = get_next_rand_block(td, f, ddir, &b);
470 ret = get_next_seq_offset(td, f, ddir, &offset);
473 io_u_set(td, io_u, IO_U_F_BUSY_OK);
476 if (td->o.rw_seq == RW_SEQ_SEQ) {
477 ret = get_next_seq_offset(td, f, ddir, &offset);
479 ret = get_next_rand_block(td, f, ddir, &b);
482 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
483 if (f->last_start[ddir] != -1ULL)
484 offset = f->last_start[ddir] - f->file_offset;
489 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
496 io_u->offset = offset;
498 io_u->offset = b * td->o.ba[ddir];
500 log_err("fio: bug in offset generation: offset=%llu, b=%llu\n", (unsigned long long) offset, (unsigned long long) b);
503 io_u->verify_offset = io_u->offset;
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.
514 static int get_next_offset(struct thread_data *td, struct io_u *io_u,
517 struct fio_file *f = io_u->file;
518 enum fio_ddir ddir = io_u->ddir;
521 assert(ddir_rw(ddir));
523 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
525 td->ddir_seq_nr = td->o.ddir_seq_nr;
528 if (get_next_block(td, io_u, ddir, rw_seq_hit, is_random))
531 if (io_u->offset >= f->io_size) {
532 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
533 (unsigned long long) io_u->offset,
534 (unsigned long long) f->io_size);
538 io_u->offset += f->file_offset;
539 if (io_u->offset >= f->real_file_size) {
540 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
541 (unsigned long long) io_u->offset,
542 (unsigned long long) f->real_file_size);
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.
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.
560 if (td_randtrimwrite(td) && ddir == DDIR_TRIM &&
561 f->last_start[DDIR_TRIM] == io_u->offset)
562 f->last_start[DDIR_WRITE]--;
564 io_u->verify_offset = io_u->offset;
568 static inline bool io_u_fits(struct thread_data *td, struct io_u *io_u,
569 unsigned long long buflen)
571 struct fio_file *f = io_u->file;
573 return io_u->offset + buflen <= f->io_size + get_start_offset(td, f);
576 static unsigned long long get_next_buflen(struct thread_data *td, struct io_u *io_u,
579 int ddir = io_u->ddir;
580 unsigned long long buflen = 0;
581 unsigned long long minbs, maxbs;
582 uint64_t frand_max, r;
585 assert(ddir_rw(ddir));
587 if (td_randtrimwrite(td) && ddir == DDIR_WRITE) {
588 struct fio_file *f = io_u->file;
590 return f->last_pos[DDIR_TRIM] - f->last_start[DDIR_TRIM];
593 if (td->o.bs_is_seq_rand)
594 ddir = is_random ? DDIR_WRITE : DDIR_READ;
596 minbs = td->o.min_bs[ddir];
597 maxbs = td->o.max_bs[ddir];
603 * If we can't satisfy the min block size from here, then fail
605 if (!io_u_fits(td, io_u, minbs))
608 frand_max = rand_max(&td->bsrange_state[ddir]);
610 r = __rand(&td->bsrange_state[ddir]);
612 if (!td->o.bssplit_nr[ddir]) {
613 buflen = minbs + (unsigned long long) ((double) maxbs *
614 (r / (frand_max + 1.0)));
619 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
620 struct bssplit *bsp = &td->o.bssplit[ddir][i];
626 if ((r / perc <= frand_max / 100ULL) &&
627 io_u_fits(td, io_u, buflen))
632 power_2 = is_power_of_2(minbs);
633 if (!td->o.bs_unaligned && power_2)
634 buflen &= ~(minbs - 1);
635 else if (!td->o.bs_unaligned && !power_2)
636 buflen -= buflen % minbs;
639 } while (!io_u_fits(td, io_u, buflen));
644 static void set_rwmix_bytes(struct thread_data *td)
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.
653 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
654 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
657 static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
661 v = rand_between(&td->rwmix_state, 1, 100);
663 if (v <= td->o.rwmix[DDIR_READ])
669 int io_u_quiesce(struct thread_data *td)
671 int ret = 0, completed = 0, err = 0;
674 * We are going to sleep, ensure that we flush anything pending as
675 * not to skew our latency numbers.
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.
682 if (td->io_u_queued || td->cur_depth)
685 while (td->io_u_in_flight) {
686 ret = io_u_queued_complete(td, 1);
693 if (td->flags & TD_F_REGROW_LOGS)
702 static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
704 enum fio_ddir odir = ddir ^ 1;
708 assert(ddir_rw(ddir));
709 now = utime_since_now(&td->epoch);
712 * if rate_next_io_time is in the past, need to catch up to rate
714 if (td->rate_next_io_time[ddir] <= now)
718 * We are ahead of rate in this direction. See if we
721 if (td_rw(td) && td->o.rwmix[odir]) {
723 * Other direction is behind rate, switch
725 if (td->rate_next_io_time[odir] <= now)
729 * Both directions are ahead of rate. sleep the min,
730 * switch if necessary
732 if (td->rate_next_io_time[ddir] <=
733 td->rate_next_io_time[odir]) {
734 usec = td->rate_next_io_time[ddir] - now;
736 usec = td->rate_next_io_time[odir] - now;
740 usec = td->rate_next_io_time[ddir] - now;
742 if (td->o.io_submit_mode == IO_MODE_INLINE)
745 if (td->o.timeout && ((usec + now) > td->o.timeout)) {
747 * check if the usec is capable of taking negative values
749 if (now > td->o.timeout) {
753 usec = td->o.timeout - now;
755 usec_sleep(td, usec);
757 now = utime_since_now(&td->epoch);
758 if ((td->o.timeout && (now > td->o.timeout)) || td->terminate)
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
769 static enum fio_ddir get_rw_ddir(struct thread_data *td)
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.
777 if (should_fsync(td) && td->last_ddir_issued == DDIR_WRITE) {
778 if (td->o.fsync_blocks && td->io_issues[DDIR_WRITE] &&
779 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks))
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;
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;
793 * Check if it's time to seed a new data direction.
795 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
797 * Put a top limit on how many bytes we do for
798 * one data direction, to avoid overflowing the
801 ddir = get_rand_ddir(td);
803 if (ddir != td->rwmix_ddir)
806 td->rwmix_ddir = ddir;
808 ddir = td->rwmix_ddir;
809 } else if (td_read(td))
811 else if (td_write(td))
813 else if (td_trim(td))
818 if (!should_check_rate(td)) {
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
824 td->rwmix_ddir = ddir;
826 td->rwmix_ddir = rate_ddir(td, ddir);
827 return td->rwmix_ddir;
830 static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
832 enum fio_ddir ddir = get_rw_ddir(td);
834 if (td->o.zone_mode == ZONE_MODE_ZBD)
835 ddir = zbd_adjust_ddir(td, io_u, ddir);
837 if (td_trimwrite(td) && !ddir_sync(ddir)) {
838 struct fio_file *f = io_u->file;
839 if (f->last_start[DDIR_WRITE] == f->last_start[DDIR_TRIM])
845 io_u->ddir = io_u->acct_ddir = ddir;
847 if (io_u->ddir == DDIR_WRITE && td_ioengine_flagged(td, FIO_BARRIER) &&
848 td->o.barrier_blocks &&
849 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
850 td->io_issues[DDIR_WRITE])
851 io_u_set(td, io_u, IO_U_F_BARRIER);
854 void put_file_log(struct thread_data *td, struct fio_file *f)
856 unsigned int ret = put_file(td, f);
859 td_verror(td, ret, "file close");
862 void put_io_u(struct thread_data *td, struct io_u *io_u)
864 const bool needs_lock = td_async_processing(td);
866 zbd_put_io_u(td, io_u);
874 if (io_u->file && !(io_u->flags & IO_U_F_NO_FILE_PUT))
875 put_file_log(td, io_u->file);
878 io_u_set(td, io_u, IO_U_F_FREE);
880 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
882 assert(!(td->flags & TD_F_CHILD));
884 io_u_qpush(&td->io_u_freelist, io_u);
885 td_io_u_free_notify(td);
888 __td_io_u_unlock(td);
891 static inline void io_u_clear_inflight_flags(struct thread_data *td,
894 io_u_clear(td, io_u, IO_U_F_FLIGHT | IO_U_F_BUSY_OK |
895 IO_U_F_PATTERN_DONE);
898 void clear_io_u(struct thread_data *td, struct io_u *io_u)
900 io_u_clear_inflight_flags(td, io_u);
904 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
906 const bool needs_lock = td_async_processing(td);
907 struct io_u *__io_u = *io_u;
908 enum fio_ddir ddir = acct_ddir(__io_u);
910 dprint(FD_IO, "requeue %p\n", __io_u);
918 io_u_set(td, __io_u, IO_U_F_FREE);
919 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(ddir))
920 td->io_issues[ddir]--;
922 io_u_clear(td, __io_u, IO_U_F_FLIGHT);
923 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH) {
925 assert(!(td->flags & TD_F_CHILD));
928 io_u_rpush(&td->io_u_requeues, __io_u);
929 td_io_u_free_notify(td);
932 __td_io_u_unlock(td);
937 static void setup_strided_zone_mode(struct thread_data *td, struct io_u *io_u)
939 struct fio_file *f = io_u->file;
941 assert(td->o.zone_mode == ZONE_MODE_STRIDED);
942 assert(td->o.zone_size);
943 assert(td->o.zone_range);
946 * See if it's time to switch to a new zone
948 if (td->zone_bytes >= td->o.zone_size) {
950 f->file_offset += td->o.zone_range + td->o.zone_skip;
953 * Wrap from the beginning, if we exceed the file size
955 if (f->file_offset >= f->real_file_size)
956 f->file_offset = get_start_offset(td, f);
958 f->last_pos[io_u->ddir] = f->file_offset;
959 td->io_skip_bytes += td->o.zone_skip;
963 * If zone_size > zone_range, then maintain the same zone until
964 * zone_bytes >= zone_size.
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;
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.
976 if ((td->zone_bytes % td->o.zone_range) == 0)
977 fio_file_reset(td, f);
980 static int fill_multi_range_io_u(struct thread_data *td, struct io_u *io_u)
983 uint64_t buflen, i = 0;
984 struct trim_range *range;
985 struct fio_file *f = io_u->file;
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",
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);
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",
1008 (unsigned long long) io_u->offset, io_u->buflen,
1009 (unsigned long long) io_u->file->real_file_size);
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;
1019 buf += sizeof(struct trim_range);
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");
1028 * Set buffer length as overall trim length for this IO, and
1029 * tell the ioengine about the number of ranges to be trimmed.
1031 io_u->buflen = buflen;
1032 io_u->number_trim = i;
1039 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
1043 enum io_u_action ret;
1045 if (td_ioengine_flagged(td, FIO_NOIO))
1048 set_rw_ddir(td, io_u);
1050 if (io_u->ddir == DDIR_INVAL || io_u->ddir == DDIR_TIMEOUT) {
1051 dprint(FD_IO, "invalid direction received ddir = %d", io_u->ddir);
1055 * fsync() or fdatasync() or trim etc, we are done
1057 if (!ddir_rw(io_u->ddir))
1060 if (td->o.zone_mode == ZONE_MODE_STRIDED)
1061 setup_strided_zone_mode(td, io_u);
1062 else if (td->o.zone_mode == ZONE_MODE_ZBD)
1063 setup_zbd_zone_mode(td, io_u);
1065 if (multi_range_trim(td, io_u)) {
1066 if (fill_multi_range_io_u(td, io_u))
1070 * No log, let the seq/rand engine retrieve the next buflen and
1073 if (get_next_offset(td, io_u, &is_random)) {
1074 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
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);
1084 offset = io_u->offset;
1086 if (td->o.zone_mode == ZONE_MODE_ZBD) {
1087 ret = zbd_adjust_block(td, io_u);
1088 if (ret == io_u_eof) {
1089 dprint(FD_IO, "zbd_adjust_block() returned io_u_eof\n");
1094 if (td->o.dp_type != FIO_DP_NONE)
1095 dp_fill_dspec_data(td, io_u);
1097 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
1098 dprint(FD_IO, "io_u %p, off=0x%llx + len=0x%llx exceeds file size=0x%llx\n",
1100 (unsigned long long) io_u->offset, io_u->buflen,
1101 (unsigned long long) io_u->file->real_file_size);
1106 * mark entry before potentially trimming io_u
1108 if (!multi_range_trim(td, io_u) && td_random(td) && file_randommap(td, io_u->file))
1109 io_u->buflen = mark_random_map(td, io_u, offset, io_u->buflen);
1112 if (!multi_range_trim(td, io_u))
1113 dprint_io_u(io_u, "fill");
1114 io_u->verify_offset = io_u->offset;
1115 td->zone_bytes += io_u->buflen;
1119 static void __io_u_mark_map(uint64_t *map, unsigned int nr)
1149 void io_u_mark_submit(struct thread_data *td, unsigned int nr)
1151 __io_u_mark_map(td->ts.io_u_submit, nr);
1152 td->ts.total_submit++;
1155 void io_u_mark_complete(struct thread_data *td, unsigned int nr)
1157 __io_u_mark_map(td->ts.io_u_complete, nr);
1158 td->ts.total_complete++;
1161 void io_u_mark_depth(struct thread_data *td, unsigned int nr)
1165 switch (td->cur_depth) {
1188 td->ts.io_u_map[idx] += nr;
1191 static void io_u_mark_lat_nsec(struct thread_data *td, unsigned long long nsec)
1195 assert(nsec < 1000);
1229 assert(idx < FIO_IO_U_LAT_N_NR);
1230 td->ts.io_u_lat_n[idx]++;
1233 static void io_u_mark_lat_usec(struct thread_data *td, unsigned long long usec)
1237 assert(usec < 1000 && usec >= 1);
1271 assert(idx < FIO_IO_U_LAT_U_NR);
1272 td->ts.io_u_lat_u[idx]++;
1275 static void io_u_mark_lat_msec(struct thread_data *td, unsigned long long msec)
1319 assert(idx < FIO_IO_U_LAT_M_NR);
1320 td->ts.io_u_lat_m[idx]++;
1323 static void io_u_mark_latency(struct thread_data *td, unsigned long long nsec)
1326 io_u_mark_lat_nsec(td, nsec);
1327 else if (nsec < 1000000)
1328 io_u_mark_lat_usec(td, nsec / 1000);
1330 io_u_mark_lat_msec(td, nsec / 1000000);
1333 static unsigned int __get_next_fileno_rand(struct thread_data *td)
1335 unsigned long fileno;
1337 if (td->o.file_service_type == FIO_FSERVICE_RANDOM) {
1338 uint64_t frand_max = rand_max(&td->next_file_state);
1341 r = __rand(&td->next_file_state);
1342 return (unsigned int) ((double) td->o.nr_files
1343 * (r / (frand_max + 1.0)));
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);
1353 log_err("fio: bad file service type: %d\n", td->o.file_service_type);
1358 return fileno >> FIO_FSERVICE_SHIFT;
1362 * Get next file to service by choosing one at random
1364 static struct fio_file *get_next_file_rand(struct thread_data *td,
1365 enum fio_file_flags goodf,
1366 enum fio_file_flags badf)
1374 fno = __get_next_fileno_rand(td);
1377 if (fio_file_done(f))
1380 if (!fio_file_open(f)) {
1383 if (td->nr_open_files >= td->o.open_files)
1384 return ERR_PTR(-EBUSY);
1386 err = td_io_open_file(td, f);
1392 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
1393 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
1397 td_io_close_file(td, f);
1402 * Get next file to service by doing round robin between all available ones
1404 static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
1407 unsigned int old_next_file = td->next_file;
1413 f = td->files[td->next_file];
1416 if (td->next_file >= td->o.nr_files)
1419 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
1420 if (fio_file_done(f)) {
1425 if (!fio_file_open(f)) {
1428 if (td->nr_open_files >= td->o.open_files)
1429 return ERR_PTR(-EBUSY);
1431 err = td_io_open_file(td, f);
1433 dprint(FD_FILE, "error %d on open of %s\n",
1441 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
1443 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
1447 td_io_close_file(td, f);
1450 } while (td->next_file != old_next_file);
1452 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
1456 static struct fio_file *__get_next_file(struct thread_data *td)
1460 assert(td->o.nr_files <= td->files_index);
1462 if (td->nr_done_files >= td->o.nr_files) {
1463 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
1464 " nr_files=%d\n", td->nr_open_files,
1470 f = td->file_service_file;
1471 if (f && fio_file_open(f) && !fio_file_closing(f)) {
1472 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
1474 if (td->file_service_left) {
1475 td->file_service_left--;
1480 if (td->o.file_service_type == FIO_FSERVICE_RR ||
1481 td->o.file_service_type == FIO_FSERVICE_SEQ)
1482 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
1484 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1489 td->file_service_file = f;
1490 td->file_service_left = td->file_service_nr - 1;
1493 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1495 dprint(FD_FILE, "get_next_file: NULL\n");
1499 static struct fio_file *get_next_file(struct thread_data *td)
1501 return __get_next_file(td);
1504 static long set_io_u_file(struct thread_data *td, struct io_u *io_u)
1509 f = get_next_file(td);
1510 if (IS_ERR_OR_NULL(f))
1516 if (!fill_io_u(td, io_u))
1519 zbd_put_io_u(td, io_u);
1521 put_file_log(td, f);
1522 td_io_close_file(td, f);
1525 if (io_u->ddir == DDIR_TIMEOUT)
1528 if (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)
1529 fio_file_reset(td, f);
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,
1534 td->nr_done_files, td->o.nr_files);
1541 static void lat_fatal(struct thread_data *td, struct io_u *io_u, struct io_completion_data *icd,
1542 unsigned long long tnsec, unsigned long long max_nsec)
1545 log_err("fio: latency of %llu nsec exceeds specified max (%llu nsec): %s %s %llu %llu\n",
1547 io_u->file->file_name,
1548 io_ddir_name(io_u->ddir),
1549 io_u->offset, io_u->buflen);
1551 td_verror(td, ETIMEDOUT, "max latency exceeded");
1552 icd->error = ETIMEDOUT;
1555 static void lat_new_cycle(struct thread_data *td)
1557 fio_gettime(&td->latency_ts, NULL);
1558 td->latency_ios = ddir_rw_sum(td->io_blocks);
1559 td->latency_failed = 0;
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.
1566 static bool __lat_target_failed(struct thread_data *td)
1568 if (td->latency_qd == 1)
1571 td->latency_qd_high = td->latency_qd;
1573 if (td->latency_qd == td->latency_qd_low)
1574 td->latency_qd_low--;
1576 td->latency_qd = (td->latency_qd + td->latency_qd_low) / 2;
1577 td->latency_stable_count = 0;
1579 dprint(FD_RATE, "Ramped down: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
1582 * When we ramp QD down, quiesce existing IO to prevent
1583 * a storm of ramp downs due to pending higher depth.
1590 static bool lat_target_failed(struct thread_data *td)
1592 if (td->o.latency_percentile.u.f == 100.0)
1593 return __lat_target_failed(td);
1595 td->latency_failed++;
1599 void lat_target_init(struct thread_data *td)
1601 td->latency_end_run = 0;
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);
1607 td->latency_qd_high = td->o.iodepth;
1608 td->latency_qd_low = 1;
1609 td->latency_ios = ddir_rw_sum(td->io_blocks);
1611 td->latency_qd = td->o.iodepth;
1614 void lat_target_reset(struct thread_data *td)
1616 if (!td->latency_end_run)
1617 lat_target_init(td);
1620 static void lat_target_success(struct thread_data *td)
1622 const unsigned int qd = td->latency_qd;
1623 struct thread_options *o = &td->o;
1625 td->latency_qd_low = td->latency_qd;
1627 if (td->latency_qd + 1 == td->latency_qd_high) {
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.
1636 if (++td->latency_stable_count >= 3) {
1637 td->latency_qd_high++;
1638 td->latency_stable_count = 0;
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.
1647 if (td->latency_qd_high != o->iodepth)
1648 td->latency_qd = (td->latency_qd + td->latency_qd_high) / 2;
1650 td->latency_qd *= 2;
1652 if (td->latency_qd > o->iodepth)
1653 td->latency_qd = o->iodepth;
1655 dprint(FD_RATE, "Ramped up: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
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.
1661 if (!o->latency_run && td->latency_qd == qd) {
1662 if (td->latency_end_run) {
1663 dprint(FD_RATE, "We are done\n");
1666 dprint(FD_RATE, "Quiesce and final run\n");
1668 td->latency_end_run = 1;
1669 reset_all_stats(td);
1678 * Check if we can bump the queue depth
1680 void lat_target_check(struct thread_data *td)
1682 uint64_t usec_window;
1686 usec_window = utime_since_now(&td->latency_ts);
1687 if (usec_window < td->o.latency_window)
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;
1694 dprint(FD_RATE, "Success rate: %.2f%% (target %.2f%%)\n", success_ios, td->o.latency_percentile.u.f);
1696 if (success_ios >= td->o.latency_percentile.u.f)
1697 lat_target_success(td);
1699 __lat_target_failed(td);
1703 * If latency target is enabled, we might be ramping up or down and not
1704 * using the full queue depth available.
1706 bool queue_full(const struct thread_data *td)
1708 const int qempty = io_u_qempty(&td->io_u_freelist);
1712 if (!td->o.latency_target)
1715 return td->cur_depth >= td->latency_qd;
1718 struct io_u *__get_io_u(struct thread_data *td)
1720 const bool needs_lock = td_async_processing(td);
1721 struct io_u *io_u = NULL;
1730 if (td->runstate != TD_FSYNCING && !io_u_rempty(&td->io_u_requeues)) {
1731 io_u = io_u_rpop(&td->io_u_requeues);
1733 } else if (!queue_full(td)) {
1734 io_u = io_u_qpop(&td->io_u_freelist);
1739 io_u->end_io = NULL;
1743 assert(io_u->flags & IO_U_F_FREE);
1744 io_u_clear(td, io_u, IO_U_F_FREE | IO_U_F_NO_FILE_PUT |
1745 IO_U_F_TRIMMED | IO_U_F_BARRIER |
1749 io_u->acct_ddir = -1;
1751 assert(!(td->flags & TD_F_CHILD));
1752 io_u_set(td, io_u, IO_U_F_IN_CUR_DEPTH);
1754 } else if (td_async_processing(td)) {
1757 * We ran out, wait for async verify threads to finish and
1760 assert(!(td->flags & TD_F_CHILD));
1761 ret = pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1762 if (fio_unlikely(ret != 0)) {
1764 } else if (!td->error)
1769 __td_io_u_unlock(td);
1774 static bool check_get_trim(struct thread_data *td, struct io_u *io_u)
1776 if (!(td->flags & TD_F_TRIM_BACKLOG))
1778 if (!td->trim_entries) {
1783 if (td->trim_batch) {
1785 if (get_next_trim(td, io_u))
1789 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
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;
1803 static bool check_get_verify(struct thread_data *td, struct io_u *io_u)
1805 if (!(td->flags & TD_F_VER_BACKLOG))
1808 if (td->io_hist_len) {
1811 if (td->verify_batch)
1813 else if (!(td->io_hist_len % td->o.verify_backlog) &&
1814 td->last_ddir_completed != DDIR_READ) {
1815 td->verify_batch = td->o.verify_batch;
1816 if (!td->verify_batch)
1817 td->verify_batch = td->o.verify_backlog;
1821 if (get_verify && !get_next_verify(td, io_u)) {
1831 * Fill offset and start time into the buffer content, to prevent too
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.
1836 static void small_content_scramble(struct io_u *io_u)
1838 unsigned long long i, nr_blocks = io_u->buflen >> 9;
1839 unsigned int offset;
1840 uint64_t boffset, *iptr;
1847 boffset = io_u->offset;
1849 if (io_u->buf_filled_len)
1850 io_u->buf_filled_len = 0;
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
1858 offset = (io_u->start_time.tv_nsec ^ boffset) & 7;
1860 for (i = 0; i < nr_blocks; i++) {
1862 * Fill offset into start of cacheline, time into end
1865 iptr = (void *) p + (offset << 6);
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;
1878 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1879 * etc. The returned io_u is fully ready to be prepped, populated and submitted.
1881 struct io_u *get_io_u(struct thread_data *td)
1885 int do_scramble = 0;
1888 io_u = __get_io_u(td);
1890 dprint(FD_IO, "__get_io_u failed\n");
1894 if (check_get_verify(td, io_u))
1896 if (check_get_trim(td, io_u))
1900 * from a requeue, io_u already setup
1906 * If using an iolog, grab next piece if any available.
1908 if (td->flags & TD_F_READ_IOLOG) {
1909 if (read_iolog_get(td, io_u))
1911 } else if (set_io_u_file(td, io_u)) {
1913 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1919 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1923 assert(fio_file_open(f));
1925 if (ddir_rw(io_u->ddir) && !multi_range_trim(td, io_u)) {
1926 if (!io_u->buflen && !td_ioengine_flagged(td, FIO_NOIO)) {
1927 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
1931 f->last_start[io_u->ddir] = io_u->offset;
1932 f->last_pos[io_u->ddir] = io_u->offset + io_u->buflen;
1934 if (io_u->ddir == DDIR_WRITE) {
1935 if (td->flags & TD_F_REFILL_BUFFERS) {
1936 io_u_fill_buffer(td, io_u,
1937 td->o.min_bs[DDIR_WRITE],
1939 } else if ((td->flags & TD_F_SCRAMBLE_BUFFERS) &&
1940 !(td->flags & TD_F_COMPRESS) &&
1941 !(td->flags & TD_F_DO_VERIFY)) {
1944 } else if (io_u->ddir == DDIR_READ) {
1946 * Reset the buf_filled parameters so next time if the
1947 * buffer is used for writes it is refilled.
1949 io_u->buf_filled_len = 0;
1954 * Set io data pointers.
1956 io_u->xfer_buf = io_u->buf;
1957 io_u->xfer_buflen = io_u->buflen;
1960 * Remember the issuing context priority. The IO engine may change this.
1962 io_u->ioprio = td->ioprio;
1963 io_u->clat_prio_index = 0;
1966 if (!td_io_prep(td, io_u)) {
1967 if (!td->o.disable_lat)
1968 fio_gettime(&io_u->start_time, NULL);
1971 small_content_scramble(io_u);
1976 dprint(FD_IO, "get_io_u failed\n");
1978 return ERR_PTR(ret);
1981 static void __io_u_log_error(struct thread_data *td, struct io_u *io_u)
1983 enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
1985 if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1988 log_err("fio: io_u error%s%s: %s: %s offset=%llu, buflen=%llu\n",
1989 io_u->file ? " on file " : "",
1990 io_u->file ? io_u->file->file_name : "",
1991 (io_u->flags & IO_U_F_DEVICE_ERROR) ?
1992 "Device-specific error" : strerror(io_u->error),
1993 io_ddir_name(io_u->ddir),
1994 io_u->offset, io_u->xfer_buflen);
1996 zbd_log_err(td, io_u);
1998 if (td->io_ops->errdetails) {
1999 char *err = td->io_ops->errdetails(td, io_u);
2002 log_err("fio: %s\n", err);
2008 td_verror(td, io_u->error, "io_u error");
2011 void io_u_log_error(struct thread_data *td, struct io_u *io_u)
2013 __io_u_log_error(td, io_u);
2015 __io_u_log_error(td->parent, io_u);
2018 static inline bool gtod_reduce(struct thread_data *td)
2020 return (td->o.disable_clat && td->o.disable_slat && td->o.disable_bw)
2021 || td->o.gtod_reduce;
2024 static void trim_block_info(struct thread_data *td, struct io_u *io_u)
2026 uint32_t *info = io_u_block_info(td, io_u);
2028 if (BLOCK_INFO_STATE(*info) >= BLOCK_STATE_TRIM_FAILURE)
2031 *info = BLOCK_INFO(BLOCK_STATE_TRIMMED, BLOCK_INFO_TRIMS(*info) + 1);
2034 static 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)
2038 const int no_reduce = !gtod_reduce(td);
2039 unsigned long long llnsec = 0;
2044 if (!td->o.stats || td_ioengine_flagged(td, FIO_NOSTATS))
2048 llnsec = ntime_since(&io_u->issue_time, &icd->time);
2050 if (!td->o.disable_lat) {
2051 unsigned long long tnsec;
2053 tnsec = ntime_since(&io_u->start_time, &icd->time);
2054 add_lat_sample(td, idx, tnsec, bytes, io_u);
2056 if (td->flags & TD_F_PROFILE_OPS) {
2057 struct prof_io_ops *ops = &td->prof_io_ops;
2060 icd->error = ops->io_u_lat(td, tnsec);
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);
2074 if (!td->o.disable_clat) {
2075 add_clat_sample(td, idx, llnsec, bytes, io_u);
2076 io_u_mark_latency(td, llnsec);
2079 if (!td->o.disable_bw && per_unit_log(td->bw_log))
2080 add_bw_sample(td, io_u, bytes, llnsec);
2082 if (no_reduce && per_unit_log(td->iops_log))
2083 add_iops_sample(td, io_u, bytes);
2084 } else if (ddir_sync(idx) && !td->o.disable_clat)
2085 add_sync_clat_sample(&td->ts, llnsec);
2087 if (td->ts.nr_block_infos && io_u->ddir == DDIR_TRIM)
2088 trim_block_info(td, io_u);
2091 static void file_log_write_comp(const struct thread_data *td, struct fio_file *f,
2092 uint64_t offset, unsigned int bytes)
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;
2103 static bool should_account(struct thread_data *td)
2105 return ramp_time_over(td) && (td->runstate == TD_RUNNING ||
2106 td->runstate == TD_VERIFYING);
2109 static void io_completed(struct thread_data *td, struct io_u **io_u_ptr,
2110 struct io_completion_data *icd)
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;
2116 dprint_io_u(io_u, "complete");
2118 assert(io_u->flags & IO_U_F_FLIGHT);
2119 io_u_clear_inflight_flags(td, io_u);
2120 invalidate_inflight(td, io_u);
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);
2128 * Mark IO ok to verify
2132 * Remove errored entry from the verification list
2135 unlog_io_piece(td, io_u);
2137 atomic_store_release(&io_u->ipo->flags,
2138 io_u->ipo->flags & ~IP_F_IN_FLIGHT);
2142 if (ddir_sync(ddir)) {
2146 f->first_write = -1ULL;
2147 f->last_write = -1ULL;
2149 if (should_account(td))
2150 account_io_completion(td, io_u, icd, ddir, io_u->buflen);
2154 td->last_ddir_completed = ddir;
2156 if (!io_u->error && ddir_rw(ddir)) {
2157 unsigned long long bytes = io_u->xfer_buflen - io_u->resid;
2161 * Make sure we notice short IO from here, and requeue them
2164 if (bytes && io_u->resid) {
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);
2175 td->io_blocks[ddir]++;
2176 td->io_bytes[ddir] += bytes;
2178 if (!(io_u->flags & IO_U_F_VER_LIST)) {
2179 td->this_io_blocks[ddir]++;
2180 td->this_io_bytes[ddir] += bytes;
2183 if (ddir == DDIR_WRITE)
2184 file_log_write_comp(td, f, io_u->offset, bytes);
2186 if (should_account(td))
2187 account_io_completion(td, io_u, icd, ddir, bytes);
2189 icd->bytes_done[ddir] += bytes;
2192 ret = io_u->end_io(td, io_u_ptr);
2194 if (ret && !icd->error)
2197 } else if (io_u->error) {
2199 icd->error = io_u->error;
2200 io_u_log_error(td, io_u);
2203 enum error_type_bit eb = td_error_type(ddir, icd->error);
2205 if (!td_non_fatal_error(td, eb, icd->error))
2209 * If there is a non_fatal error, then add to the error count
2210 * and clear all the errors.
2212 update_error_count(td, icd->error);
2220 static void init_icd(struct thread_data *td, struct io_completion_data *icd,
2225 if (!gtod_reduce(td))
2226 fio_gettime(&icd->time, NULL);
2231 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
2232 icd->bytes_done[ddir] = 0;
2235 static void ios_completed(struct thread_data *td,
2236 struct io_completion_data *icd)
2241 for (i = 0; i < icd->nr; i++) {
2242 io_u = td->io_ops->event(td, i);
2244 io_completed(td, &io_u, icd);
2251 static void io_u_update_bytes_done(struct thread_data *td,
2252 struct io_completion_data *icd)
2256 if (td->runstate == TD_VERIFYING) {
2257 td->bytes_verified += icd->bytes_done[DDIR_READ];
2262 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
2263 td->bytes_done[ddir] += icd->bytes_done[ddir];
2267 * Complete a single io_u for the sync engines.
2269 int io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
2271 struct io_completion_data icd;
2273 init_icd(td, &icd, 1);
2274 io_completed(td, &io_u, &icd);
2280 td_verror(td, icd.error, "io_u_sync_complete");
2284 io_u_update_bytes_done(td, &icd);
2290 * Called to complete min_events number of io for the async engines.
2292 int io_u_queued_complete(struct thread_data *td, int min_evts)
2294 struct io_completion_data icd;
2295 struct timespec *tvp = NULL;
2297 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
2299 dprint(FD_IO, "io_u_queued_complete: min=%d\n", min_evts);
2303 else if (min_evts > td->cur_depth)
2304 min_evts = td->cur_depth;
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);
2310 td_verror(td, -ret, "td_io_getevents");
2315 init_icd(td, &icd, ret);
2316 ios_completed(td, &icd);
2318 td_verror(td, icd.error, "io_u_queued_complete");
2322 io_u_update_bytes_done(td, &icd);
2328 * Call when io_u is really queued, to update the submission latency.
2330 void io_u_queued(struct thread_data *td, struct io_u *io_u)
2332 if (!td->o.disable_slat && ramp_time_over(td) && td->o.stats) {
2335 add_slat_sample(td, io_u);
2340 * See if we should reuse the last seed, if dedupe is enabled
2342 static struct frand_state *get_buf_state(struct thread_data *td)
2345 unsigned long long i;
2347 if (!td->o.dedupe_percentage)
2348 return &td->buf_state;
2349 else if (td->o.dedupe_percentage == 100) {
2350 frand_copy(&td->buf_state_prev, &td->buf_state);
2351 return &td->buf_state;
2354 v = rand_between(&td->dedupe_state, 1, 100);
2356 if (v <= td->o.dedupe_percentage)
2357 switch (td->o.dedupe_mode) {
2358 case DEDUPE_MODE_REPEAT:
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
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;
2372 log_err("unexpected dedupe mode %u\n", td->o.dedupe_mode);
2376 return &td->buf_state;
2379 static void save_buf_state(struct thread_data *td, struct frand_state *rs)
2381 if (td->o.dedupe_percentage == 100)
2382 frand_copy(rs, &td->buf_state_prev);
2383 else if (rs == &td->buf_state)
2384 frand_copy(&td->buf_state_prev, rs);
2387 void fill_io_buffer(struct thread_data *td, void *buf, unsigned long long min_write,
2388 unsigned long long max_bs)
2390 struct thread_options *o = &td->o;
2392 if (o->mem_type == MEM_CUDA_MALLOC)
2395 if (o->compress_percentage || o->dedupe_percentage) {
2396 unsigned int perc = td->o.compress_percentage;
2397 struct frand_state *rs = NULL;
2398 unsigned long long left = max_bs;
2399 unsigned long long this_write;
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
2410 rs = get_buf_state(td);
2412 min_write = min(min_write, left);
2414 this_write = min_not_zero(min_write,
2415 (unsigned long long) td->o.compress_chunk);
2417 fill_random_buf_percentage(rs, buf, perc,
2418 this_write, this_write,
2420 o->buffer_pattern_bytes);
2424 save_buf_state(td, rs);
2426 } else if (o->buffer_pattern_bytes)
2427 fill_buffer_pattern(td, buf, max_bs);
2428 else if (o->zero_buffers)
2429 memset(buf, 0, max_bs);
2431 fill_random_buf(get_buf_state(td), buf, max_bs);
2435 * "randomly" fill the buffer contents
2437 void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
2438 unsigned long long min_write, unsigned long long max_bs)
2440 io_u->buf_filled_len = 0;
2441 fill_io_buffer(td, io_u->buf, min_write, max_bs);
2444 static int do_sync_file_range(const struct thread_data *td,
2447 uint64_t offset, nbytes;
2449 offset = f->first_write;
2450 nbytes = f->last_write - f->first_write;
2455 return sync_file_range(f->fd, offset, nbytes, td->o.sync_file_range);
2458 int do_io_u_sync(const struct thread_data *td, struct io_u *io_u)
2462 if (io_u->ddir == DDIR_SYNC) {
2463 #ifdef CONFIG_FCNTL_SYNC
2464 ret = fcntl(io_u->file->fd, F_FULLFSYNC);
2466 ret = fsync(io_u->file->fd);
2468 } else if (io_u->ddir == DDIR_DATASYNC) {
2469 #ifdef CONFIG_FDATASYNC
2470 ret = fdatasync(io_u->file->fd);
2472 ret = io_u->xfer_buflen;
2473 io_u->error = EINVAL;
2475 } else if (io_u->ddir == DDIR_SYNC_FILE_RANGE)
2476 ret = do_sync_file_range(td, io_u->file);
2478 ret = io_u->xfer_buflen;
2479 io_u->error = EINVAL;
2483 io_u->error = errno;
2488 int do_io_u_trim(struct thread_data *td, struct io_u *io_u)
2490 #ifndef FIO_HAVE_TRIM
2491 io_u->error = EINVAL;
2494 struct fio_file *f = io_u->file;
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;
2505 ret = os_trim(f, io_u->offset, io_u->xfer_buflen);
2507 return io_u->xfer_buflen;