13 #include "lib/axmap.h"
17 struct io_completion_data {
20 int error; /* output */
21 uint64_t bytes_done[DDIR_RWDIR_CNT]; /* output */
22 struct timeval time; /* output */
26 * The ->io_axmap contains a map of blocks we have or have not done io
27 * to yet. Used to make sure we cover the entire range in a fair fashion.
29 static int random_map_free(struct fio_file *f, const uint64_t block)
31 return !axmap_isset(f->io_axmap, block);
35 * Mark a given offset as used in the map.
37 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
39 unsigned int min_bs = td->o.rw_min_bs;
40 struct fio_file *f = io_u->file;
41 unsigned int nr_blocks;
44 block = (io_u->offset - f->file_offset) / (uint64_t) min_bs;
45 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
47 if (!(io_u->flags & IO_U_F_BUSY_OK))
48 nr_blocks = axmap_set_nr(f->io_axmap, block, nr_blocks);
50 if ((nr_blocks * min_bs) < io_u->buflen)
51 io_u->buflen = nr_blocks * min_bs;
54 static uint64_t last_block(struct thread_data *td, struct fio_file *f,
60 assert(ddir_rw(ddir));
63 * Hmm, should we make sure that ->io_size <= ->real_file_size?
65 max_size = f->io_size;
66 if (max_size > f->real_file_size)
67 max_size = f->real_file_size;
70 max_size = td->o.zone_range;
72 if (td->o.min_bs[ddir] > td->o.ba[ddir])
73 max_size -= td->o.min_bs[ddir] - td->o.ba[ddir];
75 max_blocks = max_size / (uint64_t) td->o.ba[ddir];
83 struct flist_head list;
87 static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
88 enum fio_ddir ddir, uint64_t *b)
92 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE ||
93 td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64) {
94 uint64_t frand_max, lastb;
96 lastb = last_block(td, f, ddir);
100 frand_max = rand_max(&td->random_state);
101 r = __rand(&td->random_state);
103 dprint(FD_RANDOM, "off rand %llu\n", (unsigned long long) r);
105 *b = lastb * (r / ((uint64_t) frand_max + 1.0));
109 assert(fio_file_lfsr(f));
111 if (lfsr_next(&f->lfsr, &off))
118 * if we are not maintaining a random map, we are done.
120 if (!file_randommap(td, f))
124 * calculate map offset and check if it's free
126 if (random_map_free(f, *b))
129 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
130 (unsigned long long) *b);
132 *b = axmap_next_free(f->io_axmap, *b);
133 if (*b == (uint64_t) -1ULL)
139 static int __get_next_rand_offset_zipf(struct thread_data *td,
140 struct fio_file *f, enum fio_ddir ddir,
143 *b = zipf_next(&f->zipf);
147 static int __get_next_rand_offset_pareto(struct thread_data *td,
148 struct fio_file *f, enum fio_ddir ddir,
151 *b = pareto_next(&f->zipf);
155 static int __get_next_rand_offset_gauss(struct thread_data *td,
156 struct fio_file *f, enum fio_ddir ddir,
159 *b = gauss_next(&f->gauss);
164 static int flist_cmp(void *data, struct flist_head *a, struct flist_head *b)
166 struct rand_off *r1 = flist_entry(a, struct rand_off, list);
167 struct rand_off *r2 = flist_entry(b, struct rand_off, list);
169 return r1->off - r2->off;
172 static int get_off_from_method(struct thread_data *td, struct fio_file *f,
173 enum fio_ddir ddir, uint64_t *b)
175 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
176 return __get_next_rand_offset(td, f, ddir, b);
177 else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
178 return __get_next_rand_offset_zipf(td, f, ddir, b);
179 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
180 return __get_next_rand_offset_pareto(td, f, ddir, b);
181 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
182 return __get_next_rand_offset_gauss(td, f, ddir, b);
184 log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
189 * Sort the reads for a verify phase in batches of verifysort_nr, if
192 static inline int should_sort_io(struct thread_data *td)
194 if (!td->o.verifysort_nr || !td->o.do_verify)
198 if (td->runstate != TD_VERIFYING)
200 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE ||
201 td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
207 static int should_do_random(struct thread_data *td, enum fio_ddir ddir)
213 if (td->o.perc_rand[ddir] == 100)
216 frand_max = rand_max(&td->seq_rand_state[ddir]);
217 r = __rand(&td->seq_rand_state[ddir]);
218 v = 1 + (int) (100.0 * (r / (frand_max + 1.0)));
220 return v <= td->o.perc_rand[ddir];
223 static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
224 enum fio_ddir ddir, uint64_t *b)
229 if (!should_sort_io(td))
230 return get_off_from_method(td, f, ddir, b);
232 if (!flist_empty(&td->next_rand_list)) {
234 r = flist_first_entry(&td->next_rand_list, struct rand_off, list);
241 for (i = 0; i < td->o.verifysort_nr; i++) {
242 r = malloc(sizeof(*r));
244 ret = get_off_from_method(td, f, ddir, &r->off);
250 flist_add(&r->list, &td->next_rand_list);
256 assert(!flist_empty(&td->next_rand_list));
257 flist_sort(NULL, &td->next_rand_list, flist_cmp);
261 static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
262 enum fio_ddir ddir, uint64_t *b)
264 if (!get_next_rand_offset(td, f, ddir, b))
267 if (td->o.time_based) {
268 fio_file_reset(td, f);
269 if (!get_next_rand_offset(td, f, ddir, b))
273 dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
274 f->file_name, (unsigned long long) f->last_pos[ddir],
275 (unsigned long long) f->real_file_size);
279 static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
280 enum fio_ddir ddir, uint64_t *offset)
282 struct thread_options *o = &td->o;
284 assert(ddir_rw(ddir));
286 if (f->last_pos[ddir] >= f->io_size + get_start_offset(td, f) &&
288 f->last_pos[ddir] = f->last_pos[ddir] - f->io_size;
290 if (f->last_pos[ddir] < f->real_file_size) {
293 if (f->last_pos[ddir] == f->file_offset && o->ddir_seq_add < 0)
294 f->last_pos[ddir] = f->real_file_size;
296 pos = f->last_pos[ddir] - f->file_offset;
297 if (pos && o->ddir_seq_add) {
298 pos += o->ddir_seq_add;
301 * If we reach beyond the end of the file
302 * with holed IO, wrap around to the
305 if (pos >= f->real_file_size)
306 pos = f->file_offset;
316 static int get_next_block(struct thread_data *td, struct io_u *io_u,
317 enum fio_ddir ddir, int rw_seq,
318 unsigned int *is_random)
320 struct fio_file *f = io_u->file;
324 assert(ddir_rw(ddir));
330 if (should_do_random(td, ddir)) {
331 ret = get_next_rand_block(td, f, ddir, &b);
335 io_u_set(io_u, IO_U_F_BUSY_OK);
336 ret = get_next_seq_offset(td, f, ddir, &offset);
338 ret = get_next_rand_block(td, f, ddir, &b);
342 ret = get_next_seq_offset(td, f, ddir, &offset);
345 io_u_set(io_u, IO_U_F_BUSY_OK);
348 if (td->o.rw_seq == RW_SEQ_SEQ) {
349 ret = get_next_seq_offset(td, f, ddir, &offset);
351 ret = get_next_rand_block(td, f, ddir, &b);
354 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
355 if (f->last_start[ddir] != -1ULL)
356 offset = f->last_start[ddir] - f->file_offset;
361 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
368 io_u->offset = offset;
370 io_u->offset = b * td->o.ba[ddir];
372 log_err("fio: bug in offset generation: offset=%llu, b=%llu\n", (unsigned long long) offset, (unsigned long long) b);
381 * For random io, generate a random new block and see if it's used. Repeat
382 * until we find a free one. For sequential io, just return the end of
383 * the last io issued.
385 static int __get_next_offset(struct thread_data *td, struct io_u *io_u,
386 unsigned int *is_random)
388 struct fio_file *f = io_u->file;
389 enum fio_ddir ddir = io_u->ddir;
392 assert(ddir_rw(ddir));
394 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
396 td->ddir_seq_nr = td->o.ddir_seq_nr;
399 if (get_next_block(td, io_u, ddir, rw_seq_hit, is_random))
402 if (io_u->offset >= f->io_size) {
403 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
404 (unsigned long long) io_u->offset,
405 (unsigned long long) f->io_size);
409 io_u->offset += f->file_offset;
410 if (io_u->offset >= f->real_file_size) {
411 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
412 (unsigned long long) io_u->offset,
413 (unsigned long long) f->real_file_size);
420 static int get_next_offset(struct thread_data *td, struct io_u *io_u,
421 unsigned int *is_random)
423 if (td->flags & TD_F_PROFILE_OPS) {
424 struct prof_io_ops *ops = &td->prof_io_ops;
426 if (ops->fill_io_u_off)
427 return ops->fill_io_u_off(td, io_u, is_random);
430 return __get_next_offset(td, io_u, is_random);
433 static inline int io_u_fits(struct thread_data *td, struct io_u *io_u,
436 struct fio_file *f = io_u->file;
438 return io_u->offset + buflen <= f->io_size + get_start_offset(td, f);
441 static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u,
442 unsigned int is_random)
444 int ddir = io_u->ddir;
445 unsigned int buflen = 0;
446 unsigned int minbs, maxbs;
450 assert(ddir_rw(ddir));
452 if (td->o.bs_is_seq_rand)
453 ddir = is_random ? DDIR_WRITE: DDIR_READ;
455 minbs = td->o.min_bs[ddir];
456 maxbs = td->o.max_bs[ddir];
462 * If we can't satisfy the min block size from here, then fail
464 if (!io_u_fits(td, io_u, minbs))
467 frand_max = rand_max(&td->bsrange_state);
469 r = __rand(&td->bsrange_state);
471 if (!td->o.bssplit_nr[ddir]) {
472 buflen = 1 + (unsigned int) ((double) maxbs *
473 (r / (frand_max + 1.0)));
480 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
481 struct bssplit *bsp = &td->o.bssplit[ddir][i];
485 if ((r <= ((frand_max / 100L) * perc)) &&
486 io_u_fits(td, io_u, buflen))
491 if (td->o.do_verify && td->o.verify != VERIFY_NONE)
492 buflen = (buflen + td->o.verify_interval - 1) &
493 ~(td->o.verify_interval - 1);
495 if (!td->o.bs_unaligned && is_power_of_2(minbs))
496 buflen &= ~(minbs - 1);
498 } while (!io_u_fits(td, io_u, buflen));
503 static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u,
504 unsigned int is_random)
506 if (td->flags & TD_F_PROFILE_OPS) {
507 struct prof_io_ops *ops = &td->prof_io_ops;
509 if (ops->fill_io_u_size)
510 return ops->fill_io_u_size(td, io_u, is_random);
513 return __get_next_buflen(td, io_u, is_random);
516 static void set_rwmix_bytes(struct thread_data *td)
521 * we do time or byte based switch. this is needed because
522 * buffered writes may issue a lot quicker than they complete,
523 * whereas reads do not.
525 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
526 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
529 static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
531 uint64_t frand_max = rand_max(&td->rwmix_state);
535 r = __rand(&td->rwmix_state);
536 v = 1 + (int) (100.0 * (r / (frand_max + 1.0)));
538 if (v <= td->o.rwmix[DDIR_READ])
544 void io_u_quiesce(struct thread_data *td)
547 * We are going to sleep, ensure that we flush anything pending as
548 * not to skew our latency numbers.
550 * Changed to only monitor 'in flight' requests here instead of the
551 * td->cur_depth, b/c td->cur_depth does not accurately represent
552 * io's that have been actually submitted to an async engine,
553 * and cur_depth is meaningless for sync engines.
555 if (td->io_u_queued || td->cur_depth) {
558 ret = td_io_commit(td);
561 while (td->io_u_in_flight) {
564 ret = io_u_queued_complete(td, 1);
568 static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
570 enum fio_ddir odir = ddir ^ 1;
573 assert(ddir_rw(ddir));
574 now = utime_since_now(&td->start);
577 * if rate_next_io_time is in the past, need to catch up to rate
579 if (td->rate_next_io_time[ddir] <= now)
583 * We are ahead of rate in this direction. See if we
586 if (td_rw(td) && td->o.rwmix[odir]) {
588 * Other direction is behind rate, switch
590 if (td->rate_next_io_time[odir] <= now)
594 * Both directions are ahead of rate. sleep the min
595 * switch if necissary
597 if (td->rate_next_io_time[ddir] <=
598 td->rate_next_io_time[odir]) {
599 usec = td->rate_next_io_time[ddir] - now;
601 usec = td->rate_next_io_time[odir] - now;
605 usec = td->rate_next_io_time[ddir] - now;
607 if (td->o.io_submit_mode == IO_MODE_INLINE)
610 usec = usec_sleep(td, usec);
616 * Return the data direction for the next io_u. If the job is a
617 * mixed read/write workload, check the rwmix cycle and switch if
620 static enum fio_ddir get_rw_ddir(struct thread_data *td)
625 * see if it's time to fsync
627 if (td->o.fsync_blocks &&
628 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
629 td->io_issues[DDIR_WRITE] && should_fsync(td))
633 * see if it's time to fdatasync
635 if (td->o.fdatasync_blocks &&
636 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
637 td->io_issues[DDIR_WRITE] && should_fsync(td))
638 return DDIR_DATASYNC;
641 * see if it's time to sync_file_range
643 if (td->sync_file_range_nr &&
644 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
645 td->io_issues[DDIR_WRITE] && should_fsync(td))
646 return DDIR_SYNC_FILE_RANGE;
650 * Check if it's time to seed a new data direction.
652 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
654 * Put a top limit on how many bytes we do for
655 * one data direction, to avoid overflowing the
658 ddir = get_rand_ddir(td);
660 if (ddir != td->rwmix_ddir)
663 td->rwmix_ddir = ddir;
665 ddir = td->rwmix_ddir;
666 } else if (td_read(td))
668 else if (td_write(td))
673 td->rwmix_ddir = rate_ddir(td, ddir);
674 return td->rwmix_ddir;
677 static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
679 enum fio_ddir ddir = get_rw_ddir(td);
681 if (td_trimwrite(td)) {
682 struct fio_file *f = io_u->file;
683 if (f->last_pos[DDIR_WRITE] == f->last_pos[DDIR_TRIM])
689 io_u->ddir = io_u->acct_ddir = ddir;
691 if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
692 td->o.barrier_blocks &&
693 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
694 td->io_issues[DDIR_WRITE])
695 io_u_set(io_u, IO_U_F_BARRIER);
698 void put_file_log(struct thread_data *td, struct fio_file *f)
700 unsigned int ret = put_file(td, f);
703 td_verror(td, ret, "file close");
706 void put_io_u(struct thread_data *td, struct io_u *io_u)
713 if (io_u->file && !(io_u->flags & IO_U_F_NO_FILE_PUT))
714 put_file_log(td, io_u->file);
717 io_u_set(io_u, IO_U_F_FREE);
719 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
721 assert(!(td->flags & TD_F_CHILD));
723 io_u_qpush(&td->io_u_freelist, io_u);
725 td_io_u_free_notify(td);
728 void clear_io_u(struct thread_data *td, struct io_u *io_u)
730 io_u_clear(io_u, IO_U_F_FLIGHT);
734 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
736 struct io_u *__io_u = *io_u;
737 enum fio_ddir ddir = acct_ddir(__io_u);
739 dprint(FD_IO, "requeue %p\n", __io_u);
746 io_u_set(__io_u, IO_U_F_FREE);
747 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(ddir))
748 td->io_issues[ddir]--;
750 io_u_clear(__io_u, IO_U_F_FLIGHT);
751 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH) {
753 assert(!(td->flags & TD_F_CHILD));
756 io_u_rpush(&td->io_u_requeues, __io_u);
758 td_io_u_free_notify(td);
762 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
764 unsigned int is_random;
766 if (td->io_ops->flags & FIO_NOIO)
769 set_rw_ddir(td, io_u);
772 * fsync() or fdatasync() or trim etc, we are done
774 if (!ddir_rw(io_u->ddir))
778 * See if it's time to switch to a new zone
780 if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
781 struct fio_file *f = io_u->file;
784 f->file_offset += td->o.zone_range + td->o.zone_skip;
787 * Wrap from the beginning, if we exceed the file size
789 if (f->file_offset >= f->real_file_size)
790 f->file_offset = f->real_file_size - f->file_offset;
791 f->last_pos[io_u->ddir] = f->file_offset;
792 td->io_skip_bytes += td->o.zone_skip;
796 * No log, let the seq/rand engine retrieve the next buflen and
799 if (get_next_offset(td, io_u, &is_random)) {
800 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
804 io_u->buflen = get_next_buflen(td, io_u, is_random);
806 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
810 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
811 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
812 dprint(FD_IO, " off=%llu/%lu > %llu\n",
813 (unsigned long long) io_u->offset, io_u->buflen,
814 (unsigned long long) io_u->file->real_file_size);
819 * mark entry before potentially trimming io_u
821 if (td_random(td) && file_randommap(td, io_u->file))
822 mark_random_map(td, io_u);
825 dprint_io_u(io_u, "fill_io_u");
826 td->zone_bytes += io_u->buflen;
830 static void __io_u_mark_map(unsigned int *map, unsigned int nr)
859 void io_u_mark_submit(struct thread_data *td, unsigned int nr)
861 __io_u_mark_map(td->ts.io_u_submit, nr);
862 td->ts.total_submit++;
865 void io_u_mark_complete(struct thread_data *td, unsigned int nr)
867 __io_u_mark_map(td->ts.io_u_complete, nr);
868 td->ts.total_complete++;
871 void io_u_mark_depth(struct thread_data *td, unsigned int nr)
875 switch (td->cur_depth) {
897 td->ts.io_u_map[idx] += nr;
900 static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
937 assert(idx < FIO_IO_U_LAT_U_NR);
938 td->ts.io_u_lat_u[idx]++;
941 static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
982 assert(idx < FIO_IO_U_LAT_M_NR);
983 td->ts.io_u_lat_m[idx]++;
986 static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
989 io_u_mark_lat_usec(td, usec);
991 io_u_mark_lat_msec(td, usec / 1000);
995 * Get next file to service by choosing one at random
997 static struct fio_file *get_next_file_rand(struct thread_data *td,
998 enum fio_file_flags goodf,
999 enum fio_file_flags badf)
1001 uint64_t frand_max = rand_max(&td->next_file_state);
1009 r = __rand(&td->next_file_state);
1010 fno = (unsigned int) ((double) td->o.nr_files
1011 * (r / (frand_max + 1.0)));
1014 if (fio_file_done(f))
1017 if (!fio_file_open(f)) {
1020 if (td->nr_open_files >= td->o.open_files)
1021 return ERR_PTR(-EBUSY);
1023 err = td_io_open_file(td, f);
1029 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
1030 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
1034 td_io_close_file(td, f);
1039 * Get next file to service by doing round robin between all available ones
1041 static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
1044 unsigned int old_next_file = td->next_file;
1050 f = td->files[td->next_file];
1053 if (td->next_file >= td->o.nr_files)
1056 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
1057 if (fio_file_done(f)) {
1062 if (!fio_file_open(f)) {
1065 if (td->nr_open_files >= td->o.open_files)
1066 return ERR_PTR(-EBUSY);
1068 err = td_io_open_file(td, f);
1070 dprint(FD_FILE, "error %d on open of %s\n",
1078 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
1080 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
1084 td_io_close_file(td, f);
1087 } while (td->next_file != old_next_file);
1089 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
1093 static struct fio_file *__get_next_file(struct thread_data *td)
1097 assert(td->o.nr_files <= td->files_index);
1099 if (td->nr_done_files >= td->o.nr_files) {
1100 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
1101 " nr_files=%d\n", td->nr_open_files,
1107 f = td->file_service_file;
1108 if (f && fio_file_open(f) && !fio_file_closing(f)) {
1109 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
1111 if (td->file_service_left--)
1115 if (td->o.file_service_type == FIO_FSERVICE_RR ||
1116 td->o.file_service_type == FIO_FSERVICE_SEQ)
1117 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
1119 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1124 td->file_service_file = f;
1125 td->file_service_left = td->file_service_nr - 1;
1128 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1130 dprint(FD_FILE, "get_next_file: NULL\n");
1134 static struct fio_file *get_next_file(struct thread_data *td)
1136 if (td->flags & TD_F_PROFILE_OPS) {
1137 struct prof_io_ops *ops = &td->prof_io_ops;
1139 if (ops->get_next_file)
1140 return ops->get_next_file(td);
1143 return __get_next_file(td);
1146 static long set_io_u_file(struct thread_data *td, struct io_u *io_u)
1151 f = get_next_file(td);
1152 if (IS_ERR_OR_NULL(f))
1158 if (!fill_io_u(td, io_u))
1161 put_file_log(td, f);
1162 td_io_close_file(td, f);
1164 fio_file_set_done(f);
1165 td->nr_done_files++;
1166 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
1167 td->nr_done_files, td->o.nr_files);
1173 static void lat_fatal(struct thread_data *td, struct io_completion_data *icd,
1174 unsigned long tusec, unsigned long max_usec)
1177 log_err("fio: latency of %lu usec exceeds specified max (%lu usec)\n", tusec, max_usec);
1178 td_verror(td, ETIMEDOUT, "max latency exceeded");
1179 icd->error = ETIMEDOUT;
1182 static void lat_new_cycle(struct thread_data *td)
1184 fio_gettime(&td->latency_ts, NULL);
1185 td->latency_ios = ddir_rw_sum(td->io_blocks);
1186 td->latency_failed = 0;
1190 * We had an IO outside the latency target. Reduce the queue depth. If we
1191 * are at QD=1, then it's time to give up.
1193 static int __lat_target_failed(struct thread_data *td)
1195 if (td->latency_qd == 1)
1198 td->latency_qd_high = td->latency_qd;
1200 if (td->latency_qd == td->latency_qd_low)
1201 td->latency_qd_low--;
1203 td->latency_qd = (td->latency_qd + td->latency_qd_low) / 2;
1205 dprint(FD_RATE, "Ramped down: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
1208 * When we ramp QD down, quiesce existing IO to prevent
1209 * a storm of ramp downs due to pending higher depth.
1216 static int lat_target_failed(struct thread_data *td)
1218 if (td->o.latency_percentile.u.f == 100.0)
1219 return __lat_target_failed(td);
1221 td->latency_failed++;
1225 void lat_target_init(struct thread_data *td)
1227 td->latency_end_run = 0;
1229 if (td->o.latency_target) {
1230 dprint(FD_RATE, "Latency target=%llu\n", td->o.latency_target);
1231 fio_gettime(&td->latency_ts, NULL);
1233 td->latency_qd_high = td->o.iodepth;
1234 td->latency_qd_low = 1;
1235 td->latency_ios = ddir_rw_sum(td->io_blocks);
1237 td->latency_qd = td->o.iodepth;
1240 void lat_target_reset(struct thread_data *td)
1242 if (!td->latency_end_run)
1243 lat_target_init(td);
1246 static void lat_target_success(struct thread_data *td)
1248 const unsigned int qd = td->latency_qd;
1249 struct thread_options *o = &td->o;
1251 td->latency_qd_low = td->latency_qd;
1254 * If we haven't failed yet, we double up to a failing value instead
1255 * of bisecting from highest possible queue depth. If we have set
1256 * a limit other than td->o.iodepth, bisect between that.
1258 if (td->latency_qd_high != o->iodepth)
1259 td->latency_qd = (td->latency_qd + td->latency_qd_high) / 2;
1261 td->latency_qd *= 2;
1263 if (td->latency_qd > o->iodepth)
1264 td->latency_qd = o->iodepth;
1266 dprint(FD_RATE, "Ramped up: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
1269 * Same as last one, we are done. Let it run a latency cycle, so
1270 * we get only the results from the targeted depth.
1272 if (td->latency_qd == qd) {
1273 if (td->latency_end_run) {
1274 dprint(FD_RATE, "We are done\n");
1277 dprint(FD_RATE, "Quiesce and final run\n");
1279 td->latency_end_run = 1;
1280 reset_all_stats(td);
1289 * Check if we can bump the queue depth
1291 void lat_target_check(struct thread_data *td)
1293 uint64_t usec_window;
1297 usec_window = utime_since_now(&td->latency_ts);
1298 if (usec_window < td->o.latency_window)
1301 ios = ddir_rw_sum(td->io_blocks) - td->latency_ios;
1302 success_ios = (double) (ios - td->latency_failed) / (double) ios;
1303 success_ios *= 100.0;
1305 dprint(FD_RATE, "Success rate: %.2f%% (target %.2f%%)\n", success_ios, td->o.latency_percentile.u.f);
1307 if (success_ios >= td->o.latency_percentile.u.f)
1308 lat_target_success(td);
1310 __lat_target_failed(td);
1314 * If latency target is enabled, we might be ramping up or down and not
1315 * using the full queue depth available.
1317 int queue_full(const struct thread_data *td)
1319 const int qempty = io_u_qempty(&td->io_u_freelist);
1323 if (!td->o.latency_target)
1326 return td->cur_depth >= td->latency_qd;
1329 struct io_u *__get_io_u(struct thread_data *td)
1331 struct io_u *io_u = NULL;
1339 if (!io_u_rempty(&td->io_u_requeues))
1340 io_u = io_u_rpop(&td->io_u_requeues);
1341 else if (!queue_full(td)) {
1342 io_u = io_u_qpop(&td->io_u_freelist);
1347 io_u->end_io = NULL;
1351 assert(io_u->flags & IO_U_F_FREE);
1352 io_u_clear(io_u, IO_U_F_FREE | IO_U_F_NO_FILE_PUT |
1353 IO_U_F_TRIMMED | IO_U_F_BARRIER |
1357 io_u->acct_ddir = -1;
1359 assert(!(td->flags & TD_F_CHILD));
1360 io_u_set(io_u, IO_U_F_IN_CUR_DEPTH);
1362 } else if (td_async_processing(td)) {
1364 * We ran out, wait for async verify threads to finish and
1367 assert(!(td->flags & TD_F_CHILD));
1368 assert(!pthread_cond_wait(&td->free_cond, &td->io_u_lock));
1376 static int check_get_trim(struct thread_data *td, struct io_u *io_u)
1378 if (!(td->flags & TD_F_TRIM_BACKLOG))
1381 if (td->trim_entries) {
1384 if (td->trim_batch) {
1387 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1388 td->last_ddir != DDIR_READ) {
1389 td->trim_batch = td->o.trim_batch;
1390 if (!td->trim_batch)
1391 td->trim_batch = td->o.trim_backlog;
1395 if (get_trim && !get_next_trim(td, io_u))
1402 static int check_get_verify(struct thread_data *td, struct io_u *io_u)
1404 if (!(td->flags & TD_F_VER_BACKLOG))
1407 if (td->io_hist_len) {
1410 if (td->verify_batch)
1412 else if (!(td->io_hist_len % td->o.verify_backlog) &&
1413 td->last_ddir != DDIR_READ) {
1414 td->verify_batch = td->o.verify_batch;
1415 if (!td->verify_batch)
1416 td->verify_batch = td->o.verify_backlog;
1420 if (get_verify && !get_next_verify(td, io_u)) {
1430 * Fill offset and start time into the buffer content, to prevent too
1431 * easy compressible data for simple de-dupe attempts. Do this for every
1432 * 512b block in the range, since that should be the smallest block size
1433 * we can expect from a device.
1435 static void small_content_scramble(struct io_u *io_u)
1437 unsigned int i, nr_blocks = io_u->buflen / 512;
1439 unsigned int offset;
1446 boffset = io_u->offset;
1447 io_u->buf_filled_len = 0;
1449 for (i = 0; i < nr_blocks; i++) {
1451 * Fill the byte offset into a "random" start offset of
1452 * the buffer, given by the product of the usec time
1453 * and the actual offset.
1455 offset = (io_u->start_time.tv_usec ^ boffset) & 511;
1456 offset &= ~(sizeof(uint64_t) - 1);
1457 if (offset >= 512 - sizeof(uint64_t))
1458 offset -= sizeof(uint64_t);
1459 memcpy(p + offset, &boffset, sizeof(boffset));
1461 end = p + 512 - sizeof(io_u->start_time);
1462 memcpy(end, &io_u->start_time, sizeof(io_u->start_time));
1469 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1470 * etc. The returned io_u is fully ready to be prepped and submitted.
1472 struct io_u *get_io_u(struct thread_data *td)
1476 int do_scramble = 0;
1479 io_u = __get_io_u(td);
1481 dprint(FD_IO, "__get_io_u failed\n");
1485 if (check_get_verify(td, io_u))
1487 if (check_get_trim(td, io_u))
1491 * from a requeue, io_u already setup
1497 * If using an iolog, grab next piece if any available.
1499 if (td->flags & TD_F_READ_IOLOG) {
1500 if (read_iolog_get(td, io_u))
1502 } else if (set_io_u_file(td, io_u)) {
1504 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1510 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1514 assert(fio_file_open(f));
1516 if (ddir_rw(io_u->ddir)) {
1517 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
1518 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
1522 f->last_start[io_u->ddir] = io_u->offset;
1523 f->last_pos[io_u->ddir] = io_u->offset + io_u->buflen;
1525 if (io_u->ddir == DDIR_WRITE) {
1526 if (td->flags & TD_F_REFILL_BUFFERS) {
1527 io_u_fill_buffer(td, io_u,
1528 td->o.min_bs[DDIR_WRITE],
1530 } else if ((td->flags & TD_F_SCRAMBLE_BUFFERS) &&
1531 !(td->flags & TD_F_COMPRESS))
1533 if (td->flags & TD_F_VER_NONE) {
1534 populate_verify_io_u(td, io_u);
1537 } else if (io_u->ddir == DDIR_READ) {
1539 * Reset the buf_filled parameters so next time if the
1540 * buffer is used for writes it is refilled.
1542 io_u->buf_filled_len = 0;
1547 * Set io data pointers.
1549 io_u->xfer_buf = io_u->buf;
1550 io_u->xfer_buflen = io_u->buflen;
1554 if (!td_io_prep(td, io_u)) {
1555 if (!td->o.disable_slat)
1556 fio_gettime(&io_u->start_time, NULL);
1558 small_content_scramble(io_u);
1562 dprint(FD_IO, "get_io_u failed\n");
1564 return ERR_PTR(ret);
1567 static void __io_u_log_error(struct thread_data *td, struct io_u *io_u)
1569 enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
1571 if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1574 log_err("fio: io_u error%s%s: %s: %s offset=%llu, buflen=%lu\n",
1575 io_u->file ? " on file " : "",
1576 io_u->file ? io_u->file->file_name : "",
1577 strerror(io_u->error),
1578 io_ddir_name(io_u->ddir),
1579 io_u->offset, io_u->xfer_buflen);
1581 if (td->io_ops->errdetails) {
1582 char *err = td->io_ops->errdetails(io_u);
1584 log_err("fio: %s\n", err);
1589 td_verror(td, io_u->error, "io_u error");
1592 void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1594 __io_u_log_error(td, io_u);
1596 __io_u_log_error(td, io_u);
1599 static inline int gtod_reduce(struct thread_data *td)
1601 return td->o.disable_clat && td->o.disable_lat && td->o.disable_slat
1602 && td->o.disable_bw;
1605 static void account_io_completion(struct thread_data *td, struct io_u *io_u,
1606 struct io_completion_data *icd,
1607 const enum fio_ddir idx, unsigned int bytes)
1609 const int no_reduce = !gtod_reduce(td);
1610 unsigned long lusec = 0;
1616 lusec = utime_since(&io_u->issue_time, &icd->time);
1618 if (!td->o.disable_lat) {
1619 unsigned long tusec;
1621 tusec = utime_since(&io_u->start_time, &icd->time);
1622 add_lat_sample(td, idx, tusec, bytes, io_u->offset);
1624 if (td->flags & TD_F_PROFILE_OPS) {
1625 struct prof_io_ops *ops = &td->prof_io_ops;
1628 icd->error = ops->io_u_lat(td, tusec);
1631 if (td->o.max_latency && tusec > td->o.max_latency)
1632 lat_fatal(td, icd, tusec, td->o.max_latency);
1633 if (td->o.latency_target && tusec > td->o.latency_target) {
1634 if (lat_target_failed(td))
1635 lat_fatal(td, icd, tusec, td->o.latency_target);
1639 if (!td->o.disable_clat) {
1640 add_clat_sample(td, idx, lusec, bytes, io_u->offset);
1641 io_u_mark_latency(td, lusec);
1644 if (!td->o.disable_bw)
1645 add_bw_sample(td, idx, bytes, &icd->time);
1648 add_iops_sample(td, idx, bytes, &icd->time);
1650 if (td->ts.nr_block_infos && io_u->ddir == DDIR_TRIM) {
1651 uint32_t *info = io_u_block_info(td, io_u);
1652 if (BLOCK_INFO_STATE(*info) < BLOCK_STATE_TRIM_FAILURE) {
1653 if (io_u->ddir == DDIR_TRIM) {
1654 *info = BLOCK_INFO(BLOCK_STATE_TRIMMED,
1655 BLOCK_INFO_TRIMS(*info) + 1);
1656 } else if (io_u->ddir == DDIR_WRITE) {
1657 *info = BLOCK_INFO_SET_STATE(BLOCK_STATE_WRITTEN,
1664 static void io_completed(struct thread_data *td, struct io_u **io_u_ptr,
1665 struct io_completion_data *icd)
1667 struct io_u *io_u = *io_u_ptr;
1668 enum fio_ddir ddir = io_u->ddir;
1669 struct fio_file *f = io_u->file;
1671 dprint_io_u(io_u, "io complete");
1673 assert(io_u->flags & IO_U_F_FLIGHT);
1674 io_u_clear(io_u, IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
1677 * Mark IO ok to verify
1681 * Remove errored entry from the verification list
1684 unlog_io_piece(td, io_u);
1686 io_u->ipo->flags &= ~IP_F_IN_FLIGHT;
1691 if (ddir_sync(ddir)) {
1692 td->last_was_sync = 1;
1694 f->first_write = -1ULL;
1695 f->last_write = -1ULL;
1700 td->last_was_sync = 0;
1701 td->last_ddir = ddir;
1703 if (!io_u->error && ddir_rw(ddir)) {
1704 unsigned int bytes = io_u->buflen - io_u->resid;
1707 td->io_blocks[ddir]++;
1708 td->this_io_blocks[ddir]++;
1709 td->io_bytes[ddir] += bytes;
1711 if (!(io_u->flags & IO_U_F_VER_LIST))
1712 td->this_io_bytes[ddir] += bytes;
1714 if (ddir == DDIR_WRITE) {
1716 if (f->first_write == -1ULL ||
1717 io_u->offset < f->first_write)
1718 f->first_write = io_u->offset;
1719 if (f->last_write == -1ULL ||
1720 ((io_u->offset + bytes) > f->last_write))
1721 f->last_write = io_u->offset + bytes;
1723 if (td->last_write_comp) {
1724 int idx = td->last_write_idx++;
1726 td->last_write_comp[idx] = io_u->offset;
1727 if (td->last_write_idx == td->o.iodepth)
1728 td->last_write_idx = 0;
1732 if (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
1733 td->runstate == TD_VERIFYING))
1734 account_io_completion(td, io_u, icd, ddir, bytes);
1736 icd->bytes_done[ddir] += bytes;
1739 ret = io_u->end_io(td, io_u_ptr);
1741 if (ret && !icd->error)
1744 } else if (io_u->error) {
1745 icd->error = io_u->error;
1746 io_u_log_error(td, io_u);
1749 enum error_type_bit eb = td_error_type(ddir, icd->error);
1751 if (!td_non_fatal_error(td, eb, icd->error))
1755 * If there is a non_fatal error, then add to the error count
1756 * and clear all the errors.
1758 update_error_count(td, icd->error);
1766 static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1771 if (!gtod_reduce(td))
1772 fio_gettime(&icd->time, NULL);
1777 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1778 icd->bytes_done[ddir] = 0;
1781 static void ios_completed(struct thread_data *td,
1782 struct io_completion_data *icd)
1787 for (i = 0; i < icd->nr; i++) {
1788 io_u = td->io_ops->event(td, i);
1790 io_completed(td, &io_u, icd);
1798 * Complete a single io_u for the sync engines.
1800 int io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
1802 struct io_completion_data icd;
1805 init_icd(td, &icd, 1);
1806 io_completed(td, &io_u, &icd);
1812 td_verror(td, icd.error, "io_u_sync_complete");
1816 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1817 td->bytes_done[ddir] += icd.bytes_done[ddir];
1823 * Called to complete min_events number of io for the async engines.
1825 int io_u_queued_complete(struct thread_data *td, int min_evts)
1827 struct io_completion_data icd;
1828 struct timespec *tvp = NULL;
1830 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
1832 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
1836 else if (min_evts > td->cur_depth)
1837 min_evts = td->cur_depth;
1839 /* No worries, td_io_getevents fixes min and max if they are
1840 * set incorrectly */
1841 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete_max, tvp);
1843 td_verror(td, -ret, "td_io_getevents");
1848 init_icd(td, &icd, ret);
1849 ios_completed(td, &icd);
1851 td_verror(td, icd.error, "io_u_queued_complete");
1855 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1856 td->bytes_done[ddir] += icd.bytes_done[ddir];
1862 * Call when io_u is really queued, to update the submission latency.
1864 void io_u_queued(struct thread_data *td, struct io_u *io_u)
1866 if (!td->o.disable_slat) {
1867 unsigned long slat_time;
1869 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
1874 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen,
1880 * See if we should reuse the last seed, if dedupe is enabled
1882 static struct frand_state *get_buf_state(struct thread_data *td)
1888 if (!td->o.dedupe_percentage)
1889 return &td->buf_state;
1890 else if (td->o.dedupe_percentage == 100) {
1891 frand_copy(&td->buf_state_prev, &td->buf_state);
1892 return &td->buf_state;
1895 frand_max = rand_max(&td->dedupe_state);
1896 r = __rand(&td->dedupe_state);
1897 v = 1 + (int) (100.0 * (r / (frand_max + 1.0)));
1899 if (v <= td->o.dedupe_percentage)
1900 return &td->buf_state_prev;
1902 return &td->buf_state;
1905 static void save_buf_state(struct thread_data *td, struct frand_state *rs)
1907 if (td->o.dedupe_percentage == 100)
1908 frand_copy(rs, &td->buf_state_prev);
1909 else if (rs == &td->buf_state)
1910 frand_copy(&td->buf_state_prev, rs);
1913 void fill_io_buffer(struct thread_data *td, void *buf, unsigned int min_write,
1914 unsigned int max_bs)
1916 struct thread_options *o = &td->o;
1918 if (o->compress_percentage || o->dedupe_percentage) {
1919 unsigned int perc = td->o.compress_percentage;
1920 struct frand_state *rs;
1921 unsigned int left = max_bs;
1924 rs = get_buf_state(td);
1926 min_write = min(min_write, left);
1929 unsigned int seg = min_write;
1931 seg = min(min_write, td->o.compress_chunk);
1935 fill_random_buf_percentage(rs, buf, perc, seg,
1936 min_write, o->buffer_pattern,
1937 o->buffer_pattern_bytes);
1939 fill_random_buf(rs, buf, min_write);
1943 save_buf_state(td, rs);
1945 } else if (o->buffer_pattern_bytes)
1946 fill_buffer_pattern(td, buf, max_bs);
1947 else if (o->zero_buffers)
1948 memset(buf, 0, max_bs);
1950 fill_random_buf(get_buf_state(td), buf, max_bs);
1954 * "randomly" fill the buffer contents
1956 void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1957 unsigned int min_write, unsigned int max_bs)
1959 io_u->buf_filled_len = 0;
1960 fill_io_buffer(td, io_u->buf, min_write, max_bs);