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) {
95 lastb = last_block(td, f, ddir);
99 r = __rand(&td->random_state);
101 dprint(FD_RANDOM, "off rand %llu\n", (unsigned long long) r);
103 *b = lastb * (r / ((uint64_t) FRAND_MAX + 1.0));
107 assert(fio_file_lfsr(f));
109 if (lfsr_next(&f->lfsr, &off))
116 * if we are not maintaining a random map, we are done.
118 if (!file_randommap(td, f))
122 * calculate map offset and check if it's free
124 if (random_map_free(f, *b))
127 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
128 (unsigned long long) *b);
130 *b = axmap_next_free(f->io_axmap, *b);
131 if (*b == (uint64_t) -1ULL)
137 static int __get_next_rand_offset_zipf(struct thread_data *td,
138 struct fio_file *f, enum fio_ddir ddir,
141 *b = zipf_next(&f->zipf);
145 static int __get_next_rand_offset_pareto(struct thread_data *td,
146 struct fio_file *f, enum fio_ddir ddir,
149 *b = pareto_next(&f->zipf);
153 static int __get_next_rand_offset_gauss(struct thread_data *td,
154 struct fio_file *f, enum fio_ddir ddir,
157 *b = gauss_next(&f->gauss);
162 static int flist_cmp(void *data, struct flist_head *a, struct flist_head *b)
164 struct rand_off *r1 = flist_entry(a, struct rand_off, list);
165 struct rand_off *r2 = flist_entry(b, struct rand_off, list);
167 return r1->off - r2->off;
170 static int get_off_from_method(struct thread_data *td, struct fio_file *f,
171 enum fio_ddir ddir, uint64_t *b)
173 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
174 return __get_next_rand_offset(td, f, ddir, b);
175 else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
176 return __get_next_rand_offset_zipf(td, f, ddir, b);
177 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
178 return __get_next_rand_offset_pareto(td, f, ddir, b);
179 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
180 return __get_next_rand_offset_gauss(td, f, ddir, b);
182 log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
187 * Sort the reads for a verify phase in batches of verifysort_nr, if
190 static inline int should_sort_io(struct thread_data *td)
192 if (!td->o.verifysort_nr || !td->o.do_verify)
196 if (td->runstate != TD_VERIFYING)
198 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE)
204 static int should_do_random(struct thread_data *td, enum fio_ddir ddir)
209 if (td->o.perc_rand[ddir] == 100)
212 r = __rand(&td->seq_rand_state[ddir]);
213 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
215 return v <= td->o.perc_rand[ddir];
218 static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
219 enum fio_ddir ddir, uint64_t *b)
224 if (!should_sort_io(td))
225 return get_off_from_method(td, f, ddir, b);
227 if (!flist_empty(&td->next_rand_list)) {
229 r = flist_first_entry(&td->next_rand_list, struct rand_off, list);
236 for (i = 0; i < td->o.verifysort_nr; i++) {
237 r = malloc(sizeof(*r));
239 ret = get_off_from_method(td, f, ddir, &r->off);
245 flist_add(&r->list, &td->next_rand_list);
251 assert(!flist_empty(&td->next_rand_list));
252 flist_sort(NULL, &td->next_rand_list, flist_cmp);
256 static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
257 enum fio_ddir ddir, uint64_t *b)
259 if (!get_next_rand_offset(td, f, ddir, b))
262 if (td->o.time_based) {
263 fio_file_reset(td, f);
264 if (!get_next_rand_offset(td, f, ddir, b))
268 dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
269 f->file_name, (unsigned long long) f->last_pos[ddir],
270 (unsigned long long) f->real_file_size);
274 static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
275 enum fio_ddir ddir, uint64_t *offset)
277 struct thread_options *o = &td->o;
279 assert(ddir_rw(ddir));
281 if (f->last_pos[ddir] >= f->io_size + get_start_offset(td, f) &&
283 f->last_pos[ddir] = f->last_pos[ddir] - f->io_size;
285 if (f->last_pos[ddir] < f->real_file_size) {
288 if (f->last_pos[ddir] == f->file_offset && o->ddir_seq_add < 0)
289 f->last_pos[ddir] = f->real_file_size;
291 pos = f->last_pos[ddir] - f->file_offset;
292 if (pos && o->ddir_seq_add) {
293 pos += o->ddir_seq_add;
296 * If we reach beyond the end of the file
297 * with holed IO, wrap around to the
300 if (pos >= f->real_file_size)
301 pos = f->file_offset;
311 static int get_next_block(struct thread_data *td, struct io_u *io_u,
312 enum fio_ddir ddir, int rw_seq,
313 unsigned int *is_random)
315 struct fio_file *f = io_u->file;
319 assert(ddir_rw(ddir));
325 if (should_do_random(td, ddir)) {
326 ret = get_next_rand_block(td, f, ddir, &b);
330 io_u_set(io_u, IO_U_F_BUSY_OK);
331 ret = get_next_seq_offset(td, f, ddir, &offset);
333 ret = get_next_rand_block(td, f, ddir, &b);
337 ret = get_next_seq_offset(td, f, ddir, &offset);
340 io_u_set(io_u, IO_U_F_BUSY_OK);
343 if (td->o.rw_seq == RW_SEQ_SEQ) {
344 ret = get_next_seq_offset(td, f, ddir, &offset);
346 ret = get_next_rand_block(td, f, ddir, &b);
349 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
350 if (f->last_start[ddir] != -1ULL)
351 offset = f->last_start[ddir] - f->file_offset;
356 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
363 io_u->offset = offset;
365 io_u->offset = b * td->o.ba[ddir];
367 log_err("fio: bug in offset generation: offset=%llu, b=%llu\n", (unsigned long long) offset, (unsigned long long) b);
376 * For random io, generate a random new block and see if it's used. Repeat
377 * until we find a free one. For sequential io, just return the end of
378 * the last io issued.
380 static int __get_next_offset(struct thread_data *td, struct io_u *io_u,
381 unsigned int *is_random)
383 struct fio_file *f = io_u->file;
384 enum fio_ddir ddir = io_u->ddir;
387 assert(ddir_rw(ddir));
389 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
391 td->ddir_seq_nr = td->o.ddir_seq_nr;
394 if (get_next_block(td, io_u, ddir, rw_seq_hit, is_random))
397 if (io_u->offset >= f->io_size) {
398 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
399 (unsigned long long) io_u->offset,
400 (unsigned long long) f->io_size);
404 io_u->offset += f->file_offset;
405 if (io_u->offset >= f->real_file_size) {
406 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
407 (unsigned long long) io_u->offset,
408 (unsigned long long) f->real_file_size);
415 static int get_next_offset(struct thread_data *td, struct io_u *io_u,
416 unsigned int *is_random)
418 if (td->flags & TD_F_PROFILE_OPS) {
419 struct prof_io_ops *ops = &td->prof_io_ops;
421 if (ops->fill_io_u_off)
422 return ops->fill_io_u_off(td, io_u, is_random);
425 return __get_next_offset(td, io_u, is_random);
428 static inline int io_u_fits(struct thread_data *td, struct io_u *io_u,
431 struct fio_file *f = io_u->file;
433 return io_u->offset + buflen <= f->io_size + get_start_offset(td, f);
436 static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u,
437 unsigned int is_random)
439 int ddir = io_u->ddir;
440 unsigned int buflen = 0;
441 unsigned int minbs, maxbs;
444 assert(ddir_rw(ddir));
446 if (td->o.bs_is_seq_rand)
447 ddir = is_random ? DDIR_WRITE: DDIR_READ;
449 minbs = td->o.min_bs[ddir];
450 maxbs = td->o.max_bs[ddir];
456 * If we can't satisfy the min block size from here, then fail
458 if (!io_u_fits(td, io_u, minbs))
462 r = __rand(&td->bsrange_state);
464 if (!td->o.bssplit_nr[ddir]) {
465 buflen = 1 + (unsigned int) ((double) maxbs *
466 (r / (FRAND_MAX + 1.0)));
473 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
474 struct bssplit *bsp = &td->o.bssplit[ddir][i];
478 if ((r <= ((FRAND_MAX / 100L) * perc)) &&
479 io_u_fits(td, io_u, buflen))
484 if (td->o.do_verify && td->o.verify != VERIFY_NONE)
485 buflen = (buflen + td->o.verify_interval - 1) &
486 ~(td->o.verify_interval - 1);
488 if (!td->o.bs_unaligned && is_power_of_2(minbs))
489 buflen &= ~(minbs - 1);
491 } while (!io_u_fits(td, io_u, buflen));
496 static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u,
497 unsigned int is_random)
499 if (td->flags & TD_F_PROFILE_OPS) {
500 struct prof_io_ops *ops = &td->prof_io_ops;
502 if (ops->fill_io_u_size)
503 return ops->fill_io_u_size(td, io_u, is_random);
506 return __get_next_buflen(td, io_u, is_random);
509 static void set_rwmix_bytes(struct thread_data *td)
514 * we do time or byte based switch. this is needed because
515 * buffered writes may issue a lot quicker than they complete,
516 * whereas reads do not.
518 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
519 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
522 static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
527 r = __rand(&td->rwmix_state);
528 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
530 if (v <= td->o.rwmix[DDIR_READ])
536 void io_u_quiesce(struct thread_data *td)
539 * We are going to sleep, ensure that we flush anything pending as
540 * not to skew our latency numbers.
542 * Changed to only monitor 'in flight' requests here instead of the
543 * td->cur_depth, b/c td->cur_depth does not accurately represent
544 * io's that have been actually submitted to an async engine,
545 * and cur_depth is meaningless for sync engines.
547 if (td->io_u_queued || td->cur_depth) {
550 ret = td_io_commit(td);
553 while (td->io_u_in_flight) {
556 ret = io_u_queued_complete(td, 1);
560 static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
562 enum fio_ddir odir = ddir ^ 1;
565 assert(ddir_rw(ddir));
567 if (td->rate_pending_usleep[ddir] <= 0)
571 * We have too much pending sleep in this direction. See if we
574 if (td_rw(td) && td->o.rwmix[odir]) {
576 * Other direction does not have too much pending, switch
578 if (td->rate_pending_usleep[odir] < 100000)
582 * Both directions have pending sleep. Sleep the minimum time
583 * and deduct from both.
585 if (td->rate_pending_usleep[ddir] <=
586 td->rate_pending_usleep[odir]) {
587 usec = td->rate_pending_usleep[ddir];
589 usec = td->rate_pending_usleep[odir];
593 usec = td->rate_pending_usleep[ddir];
595 if (td->o.io_submit_mode == IO_MODE_INLINE)
598 usec = usec_sleep(td, usec);
600 td->rate_pending_usleep[ddir] -= usec;
603 if (td_rw(td) && __should_check_rate(td, odir))
604 td->rate_pending_usleep[odir] -= usec;
610 * Return the data direction for the next io_u. If the job is a
611 * mixed read/write workload, check the rwmix cycle and switch if
614 static enum fio_ddir get_rw_ddir(struct thread_data *td)
619 * see if it's time to fsync
621 if (td->o.fsync_blocks &&
622 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
623 td->io_issues[DDIR_WRITE] && should_fsync(td))
627 * see if it's time to fdatasync
629 if (td->o.fdatasync_blocks &&
630 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
631 td->io_issues[DDIR_WRITE] && should_fsync(td))
632 return DDIR_DATASYNC;
635 * see if it's time to sync_file_range
637 if (td->sync_file_range_nr &&
638 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
639 td->io_issues[DDIR_WRITE] && should_fsync(td))
640 return DDIR_SYNC_FILE_RANGE;
644 * Check if it's time to seed a new data direction.
646 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
648 * Put a top limit on how many bytes we do for
649 * one data direction, to avoid overflowing the
652 ddir = get_rand_ddir(td);
654 if (ddir != td->rwmix_ddir)
657 td->rwmix_ddir = ddir;
659 ddir = td->rwmix_ddir;
660 } else if (td_read(td))
662 else if (td_write(td))
667 td->rwmix_ddir = rate_ddir(td, ddir);
668 return td->rwmix_ddir;
671 static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
673 enum fio_ddir ddir = get_rw_ddir(td);
675 if (td_trimwrite(td)) {
676 struct fio_file *f = io_u->file;
677 if (f->last_pos[DDIR_WRITE] == f->last_pos[DDIR_TRIM])
683 io_u->ddir = io_u->acct_ddir = ddir;
685 if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
686 td->o.barrier_blocks &&
687 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
688 td->io_issues[DDIR_WRITE])
689 io_u_set(io_u, IO_U_F_BARRIER);
692 void put_file_log(struct thread_data *td, struct fio_file *f)
694 unsigned int ret = put_file(td, f);
697 td_verror(td, ret, "file close");
700 void put_io_u(struct thread_data *td, struct io_u *io_u)
707 if (io_u->file && !(io_u->flags & IO_U_F_NO_FILE_PUT))
708 put_file_log(td, io_u->file);
711 io_u_set(io_u, IO_U_F_FREE);
713 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
715 assert(!(td->flags & TD_F_CHILD));
717 io_u_qpush(&td->io_u_freelist, io_u);
719 td_io_u_free_notify(td);
722 void clear_io_u(struct thread_data *td, struct io_u *io_u)
724 io_u_clear(io_u, IO_U_F_FLIGHT);
728 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
730 struct io_u *__io_u = *io_u;
731 enum fio_ddir ddir = acct_ddir(__io_u);
733 dprint(FD_IO, "requeue %p\n", __io_u);
740 io_u_set(__io_u, IO_U_F_FREE);
741 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(ddir))
742 td->io_issues[ddir]--;
744 io_u_clear(__io_u, IO_U_F_FLIGHT);
745 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH) {
747 assert(!(td->flags & TD_F_CHILD));
750 io_u_rpush(&td->io_u_requeues, __io_u);
752 td_io_u_free_notify(td);
756 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
758 unsigned int is_random;
760 if (td->io_ops->flags & FIO_NOIO)
763 set_rw_ddir(td, io_u);
766 * fsync() or fdatasync() or trim etc, we are done
768 if (!ddir_rw(io_u->ddir))
772 * See if it's time to switch to a new zone
774 if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
775 struct fio_file *f = io_u->file;
778 f->file_offset += td->o.zone_range + td->o.zone_skip;
781 * Wrap from the beginning, if we exceed the file size
783 if (f->file_offset >= f->real_file_size)
784 f->file_offset = f->real_file_size - f->file_offset;
785 f->last_pos[io_u->ddir] = f->file_offset;
786 td->io_skip_bytes += td->o.zone_skip;
790 * No log, let the seq/rand engine retrieve the next buflen and
793 if (get_next_offset(td, io_u, &is_random)) {
794 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
798 io_u->buflen = get_next_buflen(td, io_u, is_random);
800 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
804 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
805 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
806 dprint(FD_IO, " off=%llu/%lu > %llu\n",
807 (unsigned long long) io_u->offset, io_u->buflen,
808 (unsigned long long) io_u->file->real_file_size);
813 * mark entry before potentially trimming io_u
815 if (td_random(td) && file_randommap(td, io_u->file))
816 mark_random_map(td, io_u);
819 dprint_io_u(io_u, "fill_io_u");
820 td->zone_bytes += io_u->buflen;
824 static void __io_u_mark_map(unsigned int *map, unsigned int nr)
853 void io_u_mark_submit(struct thread_data *td, unsigned int nr)
855 __io_u_mark_map(td->ts.io_u_submit, nr);
856 td->ts.total_submit++;
859 void io_u_mark_complete(struct thread_data *td, unsigned int nr)
861 __io_u_mark_map(td->ts.io_u_complete, nr);
862 td->ts.total_complete++;
865 void io_u_mark_depth(struct thread_data *td, unsigned int nr)
869 switch (td->cur_depth) {
891 td->ts.io_u_map[idx] += nr;
894 static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
931 assert(idx < FIO_IO_U_LAT_U_NR);
932 td->ts.io_u_lat_u[idx]++;
935 static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
976 assert(idx < FIO_IO_U_LAT_M_NR);
977 td->ts.io_u_lat_m[idx]++;
980 static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
983 io_u_mark_lat_usec(td, usec);
985 io_u_mark_lat_msec(td, usec / 1000);
989 * Get next file to service by choosing one at random
991 static struct fio_file *get_next_file_rand(struct thread_data *td,
992 enum fio_file_flags goodf,
993 enum fio_file_flags badf)
1002 r = __rand(&td->next_file_state);
1003 fno = (unsigned int) ((double) td->o.nr_files
1004 * (r / (FRAND_MAX + 1.0)));
1007 if (fio_file_done(f))
1010 if (!fio_file_open(f)) {
1013 if (td->nr_open_files >= td->o.open_files)
1014 return ERR_PTR(-EBUSY);
1016 err = td_io_open_file(td, f);
1022 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
1023 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
1027 td_io_close_file(td, f);
1032 * Get next file to service by doing round robin between all available ones
1034 static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
1037 unsigned int old_next_file = td->next_file;
1043 f = td->files[td->next_file];
1046 if (td->next_file >= td->o.nr_files)
1049 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
1050 if (fio_file_done(f)) {
1055 if (!fio_file_open(f)) {
1058 if (td->nr_open_files >= td->o.open_files)
1059 return ERR_PTR(-EBUSY);
1061 err = td_io_open_file(td, f);
1063 dprint(FD_FILE, "error %d on open of %s\n",
1071 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
1073 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
1077 td_io_close_file(td, f);
1080 } while (td->next_file != old_next_file);
1082 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
1086 static struct fio_file *__get_next_file(struct thread_data *td)
1090 assert(td->o.nr_files <= td->files_index);
1092 if (td->nr_done_files >= td->o.nr_files) {
1093 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
1094 " nr_files=%d\n", td->nr_open_files,
1100 f = td->file_service_file;
1101 if (f && fio_file_open(f) && !fio_file_closing(f)) {
1102 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
1104 if (td->file_service_left--)
1108 if (td->o.file_service_type == FIO_FSERVICE_RR ||
1109 td->o.file_service_type == FIO_FSERVICE_SEQ)
1110 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
1112 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1117 td->file_service_file = f;
1118 td->file_service_left = td->file_service_nr - 1;
1121 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1123 dprint(FD_FILE, "get_next_file: NULL\n");
1127 static struct fio_file *get_next_file(struct thread_data *td)
1129 if (td->flags & TD_F_PROFILE_OPS) {
1130 struct prof_io_ops *ops = &td->prof_io_ops;
1132 if (ops->get_next_file)
1133 return ops->get_next_file(td);
1136 return __get_next_file(td);
1139 static long set_io_u_file(struct thread_data *td, struct io_u *io_u)
1144 f = get_next_file(td);
1145 if (IS_ERR_OR_NULL(f))
1151 if (!fill_io_u(td, io_u))
1154 put_file_log(td, f);
1155 td_io_close_file(td, f);
1157 fio_file_set_done(f);
1158 td->nr_done_files++;
1159 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
1160 td->nr_done_files, td->o.nr_files);
1166 static void lat_fatal(struct thread_data *td, struct io_completion_data *icd,
1167 unsigned long tusec, unsigned long max_usec)
1170 log_err("fio: latency of %lu usec exceeds specified max (%lu usec)\n", tusec, max_usec);
1171 td_verror(td, ETIMEDOUT, "max latency exceeded");
1172 icd->error = ETIMEDOUT;
1175 static void lat_new_cycle(struct thread_data *td)
1177 fio_gettime(&td->latency_ts, NULL);
1178 td->latency_ios = ddir_rw_sum(td->io_blocks);
1179 td->latency_failed = 0;
1183 * We had an IO outside the latency target. Reduce the queue depth. If we
1184 * are at QD=1, then it's time to give up.
1186 static int __lat_target_failed(struct thread_data *td)
1188 if (td->latency_qd == 1)
1191 td->latency_qd_high = td->latency_qd;
1193 if (td->latency_qd == td->latency_qd_low)
1194 td->latency_qd_low--;
1196 td->latency_qd = (td->latency_qd + td->latency_qd_low) / 2;
1198 dprint(FD_RATE, "Ramped down: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
1201 * When we ramp QD down, quiesce existing IO to prevent
1202 * a storm of ramp downs due to pending higher depth.
1209 static int lat_target_failed(struct thread_data *td)
1211 if (td->o.latency_percentile.u.f == 100.0)
1212 return __lat_target_failed(td);
1214 td->latency_failed++;
1218 void lat_target_init(struct thread_data *td)
1220 td->latency_end_run = 0;
1222 if (td->o.latency_target) {
1223 dprint(FD_RATE, "Latency target=%llu\n", td->o.latency_target);
1224 fio_gettime(&td->latency_ts, NULL);
1226 td->latency_qd_high = td->o.iodepth;
1227 td->latency_qd_low = 1;
1228 td->latency_ios = ddir_rw_sum(td->io_blocks);
1230 td->latency_qd = td->o.iodepth;
1233 void lat_target_reset(struct thread_data *td)
1235 if (!td->latency_end_run)
1236 lat_target_init(td);
1239 static void lat_target_success(struct thread_data *td)
1241 const unsigned int qd = td->latency_qd;
1242 struct thread_options *o = &td->o;
1244 td->latency_qd_low = td->latency_qd;
1247 * If we haven't failed yet, we double up to a failing value instead
1248 * of bisecting from highest possible queue depth. If we have set
1249 * a limit other than td->o.iodepth, bisect between that.
1251 if (td->latency_qd_high != o->iodepth)
1252 td->latency_qd = (td->latency_qd + td->latency_qd_high) / 2;
1254 td->latency_qd *= 2;
1256 if (td->latency_qd > o->iodepth)
1257 td->latency_qd = o->iodepth;
1259 dprint(FD_RATE, "Ramped up: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
1262 * Same as last one, we are done. Let it run a latency cycle, so
1263 * we get only the results from the targeted depth.
1265 if (td->latency_qd == qd) {
1266 if (td->latency_end_run) {
1267 dprint(FD_RATE, "We are done\n");
1270 dprint(FD_RATE, "Quiesce and final run\n");
1272 td->latency_end_run = 1;
1273 reset_all_stats(td);
1282 * Check if we can bump the queue depth
1284 void lat_target_check(struct thread_data *td)
1286 uint64_t usec_window;
1290 usec_window = utime_since_now(&td->latency_ts);
1291 if (usec_window < td->o.latency_window)
1294 ios = ddir_rw_sum(td->io_blocks) - td->latency_ios;
1295 success_ios = (double) (ios - td->latency_failed) / (double) ios;
1296 success_ios *= 100.0;
1298 dprint(FD_RATE, "Success rate: %.2f%% (target %.2f%%)\n", success_ios, td->o.latency_percentile.u.f);
1300 if (success_ios >= td->o.latency_percentile.u.f)
1301 lat_target_success(td);
1303 __lat_target_failed(td);
1307 * If latency target is enabled, we might be ramping up or down and not
1308 * using the full queue depth available.
1310 int queue_full(const struct thread_data *td)
1312 const int qempty = io_u_qempty(&td->io_u_freelist);
1316 if (!td->o.latency_target)
1319 return td->cur_depth >= td->latency_qd;
1322 struct io_u *__get_io_u(struct thread_data *td)
1324 struct io_u *io_u = NULL;
1332 if (!io_u_rempty(&td->io_u_requeues))
1333 io_u = io_u_rpop(&td->io_u_requeues);
1334 else if (!queue_full(td)) {
1335 io_u = io_u_qpop(&td->io_u_freelist);
1340 io_u->end_io = NULL;
1344 assert(io_u->flags & IO_U_F_FREE);
1345 io_u_clear(io_u, IO_U_F_FREE | IO_U_F_NO_FILE_PUT |
1346 IO_U_F_TRIMMED | IO_U_F_BARRIER |
1350 io_u->acct_ddir = -1;
1352 assert(!(td->flags & TD_F_CHILD));
1353 io_u_set(io_u, IO_U_F_IN_CUR_DEPTH);
1355 } else if (td_async_processing(td)) {
1357 * We ran out, wait for async verify threads to finish and
1360 assert(!(td->flags & TD_F_CHILD));
1361 assert(!pthread_cond_wait(&td->free_cond, &td->io_u_lock));
1369 static int check_get_trim(struct thread_data *td, struct io_u *io_u)
1371 if (!(td->flags & TD_F_TRIM_BACKLOG))
1374 if (td->trim_entries) {
1377 if (td->trim_batch) {
1380 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1381 td->last_ddir != DDIR_READ) {
1382 td->trim_batch = td->o.trim_batch;
1383 if (!td->trim_batch)
1384 td->trim_batch = td->o.trim_backlog;
1388 if (get_trim && !get_next_trim(td, io_u))
1395 static int check_get_verify(struct thread_data *td, struct io_u *io_u)
1397 if (!(td->flags & TD_F_VER_BACKLOG))
1400 if (td->io_hist_len) {
1403 if (td->verify_batch)
1405 else if (!(td->io_hist_len % td->o.verify_backlog) &&
1406 td->last_ddir != DDIR_READ) {
1407 td->verify_batch = td->o.verify_batch;
1408 if (!td->verify_batch)
1409 td->verify_batch = td->o.verify_backlog;
1413 if (get_verify && !get_next_verify(td, io_u)) {
1423 * Fill offset and start time into the buffer content, to prevent too
1424 * easy compressible data for simple de-dupe attempts. Do this for every
1425 * 512b block in the range, since that should be the smallest block size
1426 * we can expect from a device.
1428 static void small_content_scramble(struct io_u *io_u)
1430 unsigned int i, nr_blocks = io_u->buflen / 512;
1432 unsigned int offset;
1439 boffset = io_u->offset;
1440 io_u->buf_filled_len = 0;
1442 for (i = 0; i < nr_blocks; i++) {
1444 * Fill the byte offset into a "random" start offset of
1445 * the buffer, given by the product of the usec time
1446 * and the actual offset.
1448 offset = (io_u->start_time.tv_usec ^ boffset) & 511;
1449 offset &= ~(sizeof(uint64_t) - 1);
1450 if (offset >= 512 - sizeof(uint64_t))
1451 offset -= sizeof(uint64_t);
1452 memcpy(p + offset, &boffset, sizeof(boffset));
1454 end = p + 512 - sizeof(io_u->start_time);
1455 memcpy(end, &io_u->start_time, sizeof(io_u->start_time));
1462 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1463 * etc. The returned io_u is fully ready to be prepped and submitted.
1465 struct io_u *get_io_u(struct thread_data *td)
1469 int do_scramble = 0;
1472 io_u = __get_io_u(td);
1474 dprint(FD_IO, "__get_io_u failed\n");
1478 if (check_get_verify(td, io_u))
1480 if (check_get_trim(td, io_u))
1484 * from a requeue, io_u already setup
1490 * If using an iolog, grab next piece if any available.
1492 if (td->flags & TD_F_READ_IOLOG) {
1493 if (read_iolog_get(td, io_u))
1495 } else if (set_io_u_file(td, io_u)) {
1497 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1503 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1507 assert(fio_file_open(f));
1509 if (ddir_rw(io_u->ddir)) {
1510 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
1511 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
1515 f->last_start[io_u->ddir] = io_u->offset;
1516 f->last_pos[io_u->ddir] = io_u->offset + io_u->buflen;
1518 if (io_u->ddir == DDIR_WRITE) {
1519 if (td->flags & TD_F_REFILL_BUFFERS) {
1520 io_u_fill_buffer(td, io_u,
1521 td->o.min_bs[DDIR_WRITE],
1523 } else if ((td->flags & TD_F_SCRAMBLE_BUFFERS) &&
1524 !(td->flags & TD_F_COMPRESS))
1526 if (td->flags & TD_F_VER_NONE) {
1527 populate_verify_io_u(td, io_u);
1530 } else if (io_u->ddir == DDIR_READ) {
1532 * Reset the buf_filled parameters so next time if the
1533 * buffer is used for writes it is refilled.
1535 io_u->buf_filled_len = 0;
1540 * Set io data pointers.
1542 io_u->xfer_buf = io_u->buf;
1543 io_u->xfer_buflen = io_u->buflen;
1547 if (!td_io_prep(td, io_u)) {
1548 if (!td->o.disable_slat)
1549 fio_gettime(&io_u->start_time, NULL);
1551 small_content_scramble(io_u);
1555 dprint(FD_IO, "get_io_u failed\n");
1557 return ERR_PTR(ret);
1560 static void __io_u_log_error(struct thread_data *td, struct io_u *io_u)
1562 enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
1564 if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1567 log_err("fio: io_u error%s%s: %s: %s offset=%llu, buflen=%lu\n",
1568 io_u->file ? " on file " : "",
1569 io_u->file ? io_u->file->file_name : "",
1570 strerror(io_u->error),
1571 io_ddir_name(io_u->ddir),
1572 io_u->offset, io_u->xfer_buflen);
1575 td_verror(td, io_u->error, "io_u error");
1578 void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1580 __io_u_log_error(td, io_u);
1582 __io_u_log_error(td, io_u);
1585 static inline int gtod_reduce(struct thread_data *td)
1587 return td->o.disable_clat && td->o.disable_lat && td->o.disable_slat
1588 && td->o.disable_bw;
1591 static void account_io_completion(struct thread_data *td, struct io_u *io_u,
1592 struct io_completion_data *icd,
1593 const enum fio_ddir idx, unsigned int bytes)
1595 const int no_reduce = !gtod_reduce(td);
1596 unsigned long lusec = 0;
1599 lusec = utime_since(&io_u->issue_time, &icd->time);
1601 if (!td->o.disable_lat) {
1602 unsigned long tusec;
1604 tusec = utime_since(&io_u->start_time, &icd->time);
1605 add_lat_sample(td, idx, tusec, bytes, io_u->offset);
1607 if (td->flags & TD_F_PROFILE_OPS) {
1608 struct prof_io_ops *ops = &td->prof_io_ops;
1611 icd->error = ops->io_u_lat(td, tusec);
1614 if (td->o.max_latency && tusec > td->o.max_latency)
1615 lat_fatal(td, icd, tusec, td->o.max_latency);
1616 if (td->o.latency_target && tusec > td->o.latency_target) {
1617 if (lat_target_failed(td))
1618 lat_fatal(td, icd, tusec, td->o.latency_target);
1622 if (!td->o.disable_clat) {
1623 add_clat_sample(td, idx, lusec, bytes, io_u->offset);
1624 io_u_mark_latency(td, lusec);
1630 if (!td->o.disable_bw)
1631 add_bw_sample(td, idx, bytes, &icd->time);
1634 add_iops_sample(td, idx, bytes, &icd->time);
1636 if (td->ts.nr_block_infos && io_u->ddir == DDIR_TRIM) {
1637 uint32_t *info = io_u_block_info(td, io_u);
1638 if (BLOCK_INFO_STATE(*info) < BLOCK_STATE_TRIM_FAILURE) {
1639 if (io_u->ddir == DDIR_TRIM) {
1640 *info = BLOCK_INFO(BLOCK_STATE_TRIMMED,
1641 BLOCK_INFO_TRIMS(*info) + 1);
1642 } else if (io_u->ddir == DDIR_WRITE) {
1643 *info = BLOCK_INFO_SET_STATE(BLOCK_STATE_WRITTEN,
1650 static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
1652 uint64_t secs, remainder, bps, bytes;
1654 assert(!(td->flags & TD_F_CHILD));
1655 bytes = td->this_io_bytes[ddir];
1656 bps = td->rate_bps[ddir];
1658 remainder = bytes % bps;
1659 return remainder * 1000000 / bps + secs * 1000000;
1662 static void io_completed(struct thread_data *td, struct io_u **io_u_ptr,
1663 struct io_completion_data *icd)
1665 struct io_u *io_u = *io_u_ptr;
1666 enum fio_ddir ddir = io_u->ddir;
1667 struct fio_file *f = io_u->file;
1669 dprint_io_u(io_u, "io complete");
1671 assert(io_u->flags & IO_U_F_FLIGHT);
1672 io_u_clear(io_u, IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
1675 * Mark IO ok to verify
1679 * Remove errored entry from the verification list
1682 unlog_io_piece(td, io_u);
1684 io_u->ipo->flags &= ~IP_F_IN_FLIGHT;
1689 if (ddir_sync(ddir)) {
1690 td->last_was_sync = 1;
1692 f->first_write = -1ULL;
1693 f->last_write = -1ULL;
1698 td->last_was_sync = 0;
1699 td->last_ddir = ddir;
1701 if (!io_u->error && ddir_rw(ddir)) {
1702 unsigned int bytes = io_u->buflen - io_u->resid;
1703 const enum fio_ddir oddir = ddir ^ 1;
1706 td->io_blocks[ddir]++;
1707 td->this_io_blocks[ddir]++;
1708 td->io_bytes[ddir] += bytes;
1710 if (!(io_u->flags & IO_U_F_VER_LIST))
1711 td->this_io_bytes[ddir] += bytes;
1713 if (ddir == DDIR_WRITE) {
1715 if (f->first_write == -1ULL ||
1716 io_u->offset < f->first_write)
1717 f->first_write = io_u->offset;
1718 if (f->last_write == -1ULL ||
1719 ((io_u->offset + bytes) > f->last_write))
1720 f->last_write = io_u->offset + bytes;
1722 if (td->last_write_comp) {
1723 int idx = td->last_write_idx++;
1725 td->last_write_comp[idx] = io_u->offset;
1726 if (td->last_write_idx == td->o.iodepth)
1727 td->last_write_idx = 0;
1731 if (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
1732 td->runstate == TD_VERIFYING)) {
1733 struct thread_data *__td = td;
1735 account_io_completion(td, io_u, icd, ddir, bytes);
1740 if (__should_check_rate(__td, ddir)) {
1741 __td->rate_pending_usleep[ddir] =
1742 (usec_for_io(__td, ddir) -
1743 utime_since_now(&__td->start));
1745 if (ddir != DDIR_TRIM &&
1746 __should_check_rate(__td, oddir)) {
1747 __td->rate_pending_usleep[oddir] =
1748 (usec_for_io(__td, oddir) -
1749 utime_since_now(&__td->start));
1753 icd->bytes_done[ddir] += bytes;
1756 ret = io_u->end_io(td, io_u_ptr);
1758 if (ret && !icd->error)
1761 } else if (io_u->error) {
1762 icd->error = io_u->error;
1763 io_u_log_error(td, io_u);
1766 enum error_type_bit eb = td_error_type(ddir, icd->error);
1768 if (!td_non_fatal_error(td, eb, icd->error))
1772 * If there is a non_fatal error, then add to the error count
1773 * and clear all the errors.
1775 update_error_count(td, icd->error);
1783 static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1788 if (!gtod_reduce(td))
1789 fio_gettime(&icd->time, NULL);
1794 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1795 icd->bytes_done[ddir] = 0;
1798 static void ios_completed(struct thread_data *td,
1799 struct io_completion_data *icd)
1804 for (i = 0; i < icd->nr; i++) {
1805 io_u = td->io_ops->event(td, i);
1807 io_completed(td, &io_u, icd);
1815 * Complete a single io_u for the sync engines.
1817 int io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
1819 struct io_completion_data icd;
1822 init_icd(td, &icd, 1);
1823 io_completed(td, &io_u, &icd);
1829 td_verror(td, icd.error, "io_u_sync_complete");
1833 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1834 td->bytes_done[ddir] += icd.bytes_done[ddir];
1840 * Called to complete min_events number of io for the async engines.
1842 int io_u_queued_complete(struct thread_data *td, int min_evts)
1844 struct io_completion_data icd;
1845 struct timespec *tvp = NULL;
1847 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
1849 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
1853 else if (min_evts > td->cur_depth)
1854 min_evts = td->cur_depth;
1856 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete, tvp);
1858 td_verror(td, -ret, "td_io_getevents");
1863 init_icd(td, &icd, ret);
1864 ios_completed(td, &icd);
1866 td_verror(td, icd.error, "io_u_queued_complete");
1870 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1871 td->bytes_done[ddir] += icd.bytes_done[ddir];
1877 * Call when io_u is really queued, to update the submission latency.
1879 void io_u_queued(struct thread_data *td, struct io_u *io_u)
1881 if (!td->o.disable_slat) {
1882 unsigned long slat_time;
1884 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
1885 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen,
1891 * See if we should reuse the last seed, if dedupe is enabled
1893 static struct frand_state *get_buf_state(struct thread_data *td)
1898 if (!td->o.dedupe_percentage)
1899 return &td->buf_state;
1900 else if (td->o.dedupe_percentage == 100) {
1901 frand_copy(&td->buf_state_prev, &td->buf_state);
1902 return &td->buf_state;
1905 r = __rand(&td->dedupe_state);
1906 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
1908 if (v <= td->o.dedupe_percentage)
1909 return &td->buf_state_prev;
1911 return &td->buf_state;
1914 static void save_buf_state(struct thread_data *td, struct frand_state *rs)
1916 if (td->o.dedupe_percentage == 100)
1917 frand_copy(rs, &td->buf_state_prev);
1918 else if (rs == &td->buf_state)
1919 frand_copy(&td->buf_state_prev, rs);
1922 void fill_io_buffer(struct thread_data *td, void *buf, unsigned int min_write,
1923 unsigned int max_bs)
1925 struct thread_options *o = &td->o;
1927 if (o->compress_percentage || o->dedupe_percentage) {
1928 unsigned int perc = td->o.compress_percentage;
1929 struct frand_state *rs;
1930 unsigned int left = max_bs;
1933 rs = get_buf_state(td);
1935 min_write = min(min_write, left);
1938 unsigned int seg = min_write;
1940 seg = min(min_write, td->o.compress_chunk);
1944 fill_random_buf_percentage(rs, buf, perc, seg,
1945 min_write, o->buffer_pattern,
1946 o->buffer_pattern_bytes);
1948 fill_random_buf(rs, buf, min_write);
1952 save_buf_state(td, rs);
1954 } else if (o->buffer_pattern_bytes)
1955 fill_buffer_pattern(td, buf, max_bs);
1956 else if (o->zero_buffers)
1957 memset(buf, 0, max_bs);
1959 fill_random_buf(get_buf_state(td), buf, max_bs);
1963 * "randomly" fill the buffer contents
1965 void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1966 unsigned int min_write, unsigned int max_bs)
1968 io_u->buf_filled_len = 0;
1969 fill_io_buffer(td, io_u->buf, min_write, max_bs);