13 #include "lib/axmap.h"
18 struct io_completion_data {
21 int error; /* output */
22 uint64_t bytes_done[DDIR_RWDIR_CNT]; /* output */
23 struct timeval time; /* output */
27 * The ->io_axmap contains a map of blocks we have or have not done io
28 * to yet. Used to make sure we cover the entire range in a fair fashion.
30 static bool random_map_free(struct fio_file *f, const uint64_t block)
32 return !axmap_isset(f->io_axmap, block);
36 * Mark a given offset as used in the map.
38 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
40 unsigned int min_bs = td->o.rw_min_bs;
41 struct fio_file *f = io_u->file;
42 unsigned int nr_blocks;
45 block = (io_u->offset - f->file_offset) / (uint64_t) min_bs;
46 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
48 if (!(io_u->flags & IO_U_F_BUSY_OK))
49 nr_blocks = axmap_set_nr(f->io_axmap, block, nr_blocks);
51 if ((nr_blocks * min_bs) < io_u->buflen)
52 io_u->buflen = nr_blocks * min_bs;
55 static uint64_t last_block(struct thread_data *td, struct fio_file *f,
61 assert(ddir_rw(ddir));
64 * Hmm, should we make sure that ->io_size <= ->real_file_size?
66 max_size = f->io_size;
67 if (max_size > f->real_file_size)
68 max_size = f->real_file_size;
71 max_size = td->o.zone_range;
73 if (td->o.min_bs[ddir] > td->o.ba[ddir])
74 max_size -= td->o.min_bs[ddir] - td->o.ba[ddir];
76 max_blocks = max_size / (uint64_t) td->o.ba[ddir];
84 struct flist_head list;
88 static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
89 enum fio_ddir ddir, uint64_t *b)
93 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE ||
94 td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64) {
95 uint64_t frand_max, lastb;
97 lastb = last_block(td, f, ddir);
101 frand_max = rand_max(&td->random_state);
102 r = __rand(&td->random_state);
104 dprint(FD_RANDOM, "off rand %llu\n", (unsigned long long) r);
106 *b = lastb * (r / ((uint64_t) frand_max + 1.0));
110 assert(fio_file_lfsr(f));
112 if (lfsr_next(&f->lfsr, &off))
119 * if we are not maintaining a random map, we are done.
121 if (!file_randommap(td, f))
125 * calculate map offset and check if it's free
127 if (random_map_free(f, *b))
130 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
131 (unsigned long long) *b);
133 *b = axmap_next_free(f->io_axmap, *b);
134 if (*b == (uint64_t) -1ULL)
140 static int __get_next_rand_offset_zipf(struct thread_data *td,
141 struct fio_file *f, enum fio_ddir ddir,
144 *b = zipf_next(&f->zipf);
148 static int __get_next_rand_offset_pareto(struct thread_data *td,
149 struct fio_file *f, enum fio_ddir ddir,
152 *b = pareto_next(&f->zipf);
156 static int __get_next_rand_offset_gauss(struct thread_data *td,
157 struct fio_file *f, enum fio_ddir ddir,
160 *b = gauss_next(&f->gauss);
165 static int flist_cmp(void *data, struct flist_head *a, struct flist_head *b)
167 struct rand_off *r1 = flist_entry(a, struct rand_off, list);
168 struct rand_off *r2 = flist_entry(b, struct rand_off, list);
170 return r1->off - r2->off;
173 static int get_off_from_method(struct thread_data *td, struct fio_file *f,
174 enum fio_ddir ddir, uint64_t *b)
176 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
177 return __get_next_rand_offset(td, f, ddir, b);
178 else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
179 return __get_next_rand_offset_zipf(td, f, ddir, b);
180 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
181 return __get_next_rand_offset_pareto(td, f, ddir, b);
182 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
183 return __get_next_rand_offset_gauss(td, f, ddir, b);
185 log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
190 * Sort the reads for a verify phase in batches of verifysort_nr, if
193 static inline bool should_sort_io(struct thread_data *td)
195 if (!td->o.verifysort_nr || !td->o.do_verify)
199 if (td->runstate != TD_VERIFYING)
201 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE ||
202 td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
208 static bool should_do_random(struct thread_data *td, enum fio_ddir ddir)
214 if (td->o.perc_rand[ddir] == 100)
217 frand_max = rand_max(&td->seq_rand_state[ddir]);
218 r = __rand(&td->seq_rand_state[ddir]);
219 v = 1 + (int) (100.0 * (r / (frand_max + 1.0)));
221 return v <= td->o.perc_rand[ddir];
224 static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
225 enum fio_ddir ddir, uint64_t *b)
230 if (!should_sort_io(td))
231 return get_off_from_method(td, f, ddir, b);
233 if (!flist_empty(&td->next_rand_list)) {
235 r = flist_first_entry(&td->next_rand_list, struct rand_off, list);
242 for (i = 0; i < td->o.verifysort_nr; i++) {
243 r = malloc(sizeof(*r));
245 ret = get_off_from_method(td, f, ddir, &r->off);
251 flist_add(&r->list, &td->next_rand_list);
257 assert(!flist_empty(&td->next_rand_list));
258 flist_sort(NULL, &td->next_rand_list, flist_cmp);
262 static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
263 enum fio_ddir ddir, uint64_t *b)
265 if (!get_next_rand_offset(td, f, ddir, b))
268 if (td->o.time_based) {
269 fio_file_reset(td, f);
270 if (!get_next_rand_offset(td, f, ddir, b))
274 dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
275 f->file_name, (unsigned long long) f->last_pos[ddir],
276 (unsigned long long) f->real_file_size);
280 static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
281 enum fio_ddir ddir, uint64_t *offset)
283 struct thread_options *o = &td->o;
285 assert(ddir_rw(ddir));
287 if (f->last_pos[ddir] >= f->io_size + get_start_offset(td, f) &&
289 struct thread_options *o = &td->o;
290 uint64_t io_size = f->io_size + (f->io_size % o->min_bs[ddir]);
292 if (io_size > f->last_pos[ddir])
293 f->last_pos[ddir] = 0;
295 f->last_pos[ddir] = f->last_pos[ddir] - io_size;
298 if (f->last_pos[ddir] < f->real_file_size) {
301 if (f->last_pos[ddir] == f->file_offset && o->ddir_seq_add < 0)
302 f->last_pos[ddir] = f->real_file_size;
304 pos = f->last_pos[ddir] - f->file_offset;
305 if (pos && o->ddir_seq_add) {
306 pos += o->ddir_seq_add;
309 * If we reach beyond the end of the file
310 * with holed IO, wrap around to the
313 if (pos >= f->real_file_size)
314 pos = f->file_offset;
324 static int get_next_block(struct thread_data *td, struct io_u *io_u,
325 enum fio_ddir ddir, int rw_seq,
326 unsigned int *is_random)
328 struct fio_file *f = io_u->file;
332 assert(ddir_rw(ddir));
338 if (should_do_random(td, ddir)) {
339 ret = get_next_rand_block(td, f, ddir, &b);
343 io_u_set(io_u, IO_U_F_BUSY_OK);
344 ret = get_next_seq_offset(td, f, ddir, &offset);
346 ret = get_next_rand_block(td, f, ddir, &b);
350 ret = get_next_seq_offset(td, f, ddir, &offset);
353 io_u_set(io_u, IO_U_F_BUSY_OK);
356 if (td->o.rw_seq == RW_SEQ_SEQ) {
357 ret = get_next_seq_offset(td, f, ddir, &offset);
359 ret = get_next_rand_block(td, f, ddir, &b);
362 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
363 if (f->last_start[ddir] != -1ULL)
364 offset = f->last_start[ddir] - f->file_offset;
369 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
376 io_u->offset = offset;
378 io_u->offset = b * td->o.ba[ddir];
380 log_err("fio: bug in offset generation: offset=%llu, b=%llu\n", (unsigned long long) offset, (unsigned long long) b);
389 * For random io, generate a random new block and see if it's used. Repeat
390 * until we find a free one. For sequential io, just return the end of
391 * the last io issued.
393 static int __get_next_offset(struct thread_data *td, struct io_u *io_u,
394 unsigned int *is_random)
396 struct fio_file *f = io_u->file;
397 enum fio_ddir ddir = io_u->ddir;
400 assert(ddir_rw(ddir));
402 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
404 td->ddir_seq_nr = td->o.ddir_seq_nr;
407 if (get_next_block(td, io_u, ddir, rw_seq_hit, is_random))
410 if (io_u->offset >= f->io_size) {
411 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
412 (unsigned long long) io_u->offset,
413 (unsigned long long) f->io_size);
417 io_u->offset += f->file_offset;
418 if (io_u->offset >= f->real_file_size) {
419 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
420 (unsigned long long) io_u->offset,
421 (unsigned long long) f->real_file_size);
428 static int get_next_offset(struct thread_data *td, struct io_u *io_u,
429 unsigned int *is_random)
431 if (td->flags & TD_F_PROFILE_OPS) {
432 struct prof_io_ops *ops = &td->prof_io_ops;
434 if (ops->fill_io_u_off)
435 return ops->fill_io_u_off(td, io_u, is_random);
438 return __get_next_offset(td, io_u, is_random);
441 static inline bool io_u_fits(struct thread_data *td, struct io_u *io_u,
444 struct fio_file *f = io_u->file;
446 return io_u->offset + buflen <= f->io_size + get_start_offset(td, f);
449 static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u,
450 unsigned int is_random)
452 int ddir = io_u->ddir;
453 unsigned int buflen = 0;
454 unsigned int minbs, maxbs;
458 assert(ddir_rw(ddir));
460 if (td->o.bs_is_seq_rand)
461 ddir = is_random ? DDIR_WRITE: DDIR_READ;
463 minbs = td->o.min_bs[ddir];
464 maxbs = td->o.max_bs[ddir];
470 * If we can't satisfy the min block size from here, then fail
472 if (!io_u_fits(td, io_u, minbs))
475 frand_max = rand_max(&td->bsrange_state);
477 r = __rand(&td->bsrange_state);
479 if (!td->o.bssplit_nr[ddir]) {
480 buflen = 1 + (unsigned int) ((double) maxbs *
481 (r / (frand_max + 1.0)));
488 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
489 struct bssplit *bsp = &td->o.bssplit[ddir][i];
493 if ((r <= ((frand_max / 100L) * perc)) &&
494 io_u_fits(td, io_u, buflen))
499 if (td->o.verify != VERIFY_NONE)
500 buflen = (buflen + td->o.verify_interval - 1) &
501 ~(td->o.verify_interval - 1);
503 if (!td->o.bs_unaligned && is_power_of_2(minbs))
504 buflen &= ~(minbs - 1);
506 } while (!io_u_fits(td, io_u, buflen));
511 static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u,
512 unsigned int is_random)
514 if (td->flags & TD_F_PROFILE_OPS) {
515 struct prof_io_ops *ops = &td->prof_io_ops;
517 if (ops->fill_io_u_size)
518 return ops->fill_io_u_size(td, io_u, is_random);
521 return __get_next_buflen(td, io_u, is_random);
524 static void set_rwmix_bytes(struct thread_data *td)
529 * we do time or byte based switch. this is needed because
530 * buffered writes may issue a lot quicker than they complete,
531 * whereas reads do not.
533 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
534 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
537 static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
539 uint64_t frand_max = rand_max(&td->rwmix_state);
543 r = __rand(&td->rwmix_state);
544 v = 1 + (int) (100.0 * (r / (frand_max + 1.0)));
546 if (v <= td->o.rwmix[DDIR_READ])
552 int io_u_quiesce(struct thread_data *td)
557 * We are going to sleep, ensure that we flush anything pending as
558 * not to skew our latency numbers.
560 * Changed to only monitor 'in flight' requests here instead of the
561 * td->cur_depth, b/c td->cur_depth does not accurately represent
562 * io's that have been actually submitted to an async engine,
563 * and cur_depth is meaningless for sync engines.
565 if (td->io_u_queued || td->cur_depth) {
568 ret = td_io_commit(td);
571 while (td->io_u_in_flight) {
574 ret = io_u_queued_complete(td, 1);
582 static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
584 enum fio_ddir odir = ddir ^ 1;
587 assert(ddir_rw(ddir));
588 now = utime_since_now(&td->start);
591 * if rate_next_io_time is in the past, need to catch up to rate
593 if (td->rate_next_io_time[ddir] <= now)
597 * We are ahead of rate in this direction. See if we
600 if (td_rw(td) && td->o.rwmix[odir]) {
602 * Other direction is behind rate, switch
604 if (td->rate_next_io_time[odir] <= now)
608 * Both directions are ahead of rate. sleep the min
609 * switch if necissary
611 if (td->rate_next_io_time[ddir] <=
612 td->rate_next_io_time[odir]) {
613 usec = td->rate_next_io_time[ddir] - now;
615 usec = td->rate_next_io_time[odir] - now;
619 usec = td->rate_next_io_time[ddir] - now;
621 if (td->o.io_submit_mode == IO_MODE_INLINE)
624 usec = usec_sleep(td, usec);
630 * Return the data direction for the next io_u. If the job is a
631 * mixed read/write workload, check the rwmix cycle and switch if
634 static enum fio_ddir get_rw_ddir(struct thread_data *td)
639 * see if it's time to fsync
641 if (td->o.fsync_blocks &&
642 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
643 td->io_issues[DDIR_WRITE] && should_fsync(td))
647 * see if it's time to fdatasync
649 if (td->o.fdatasync_blocks &&
650 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
651 td->io_issues[DDIR_WRITE] && should_fsync(td))
652 return DDIR_DATASYNC;
655 * see if it's time to sync_file_range
657 if (td->sync_file_range_nr &&
658 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
659 td->io_issues[DDIR_WRITE] && should_fsync(td))
660 return DDIR_SYNC_FILE_RANGE;
664 * Check if it's time to seed a new data direction.
666 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
668 * Put a top limit on how many bytes we do for
669 * one data direction, to avoid overflowing the
672 ddir = get_rand_ddir(td);
674 if (ddir != td->rwmix_ddir)
677 td->rwmix_ddir = ddir;
679 ddir = td->rwmix_ddir;
680 } else if (td_read(td))
682 else if (td_write(td))
687 td->rwmix_ddir = rate_ddir(td, ddir);
688 return td->rwmix_ddir;
691 static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
693 enum fio_ddir ddir = get_rw_ddir(td);
695 if (td_trimwrite(td)) {
696 struct fio_file *f = io_u->file;
697 if (f->last_pos[DDIR_WRITE] == f->last_pos[DDIR_TRIM])
703 io_u->ddir = io_u->acct_ddir = ddir;
705 if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
706 td->o.barrier_blocks &&
707 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
708 td->io_issues[DDIR_WRITE])
709 io_u_set(io_u, IO_U_F_BARRIER);
712 void put_file_log(struct thread_data *td, struct fio_file *f)
714 unsigned int ret = put_file(td, f);
717 td_verror(td, ret, "file close");
720 void put_io_u(struct thread_data *td, struct io_u *io_u)
727 if (io_u->file && !(io_u->flags & IO_U_F_NO_FILE_PUT))
728 put_file_log(td, io_u->file);
731 io_u_set(io_u, IO_U_F_FREE);
733 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
735 assert(!(td->flags & TD_F_CHILD));
737 io_u_qpush(&td->io_u_freelist, io_u);
739 td_io_u_free_notify(td);
742 void clear_io_u(struct thread_data *td, struct io_u *io_u)
744 io_u_clear(io_u, IO_U_F_FLIGHT);
748 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
750 struct io_u *__io_u = *io_u;
751 enum fio_ddir ddir = acct_ddir(__io_u);
753 dprint(FD_IO, "requeue %p\n", __io_u);
760 io_u_set(__io_u, IO_U_F_FREE);
761 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(ddir))
762 td->io_issues[ddir]--;
764 io_u_clear(__io_u, IO_U_F_FLIGHT);
765 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH) {
767 assert(!(td->flags & TD_F_CHILD));
770 io_u_rpush(&td->io_u_requeues, __io_u);
772 td_io_u_free_notify(td);
776 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
778 unsigned int is_random;
780 if (td->io_ops->flags & FIO_NOIO)
783 set_rw_ddir(td, io_u);
786 * fsync() or fdatasync() or trim etc, we are done
788 if (!ddir_rw(io_u->ddir))
792 * See if it's time to switch to a new zone
794 if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
795 struct fio_file *f = io_u->file;
798 f->file_offset += td->o.zone_range + td->o.zone_skip;
801 * Wrap from the beginning, if we exceed the file size
803 if (f->file_offset >= f->real_file_size)
804 f->file_offset = f->real_file_size - f->file_offset;
805 f->last_pos[io_u->ddir] = f->file_offset;
806 td->io_skip_bytes += td->o.zone_skip;
810 * No log, let the seq/rand engine retrieve the next buflen and
813 if (get_next_offset(td, io_u, &is_random)) {
814 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
818 io_u->buflen = get_next_buflen(td, io_u, is_random);
820 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
824 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
825 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
826 dprint(FD_IO, " off=%llu/%lu > %llu\n",
827 (unsigned long long) io_u->offset, io_u->buflen,
828 (unsigned long long) io_u->file->real_file_size);
833 * mark entry before potentially trimming io_u
835 if (td_random(td) && file_randommap(td, io_u->file))
836 mark_random_map(td, io_u);
839 dprint_io_u(io_u, "fill_io_u");
840 td->zone_bytes += io_u->buflen;
844 static void __io_u_mark_map(unsigned int *map, unsigned int nr)
873 void io_u_mark_submit(struct thread_data *td, unsigned int nr)
875 __io_u_mark_map(td->ts.io_u_submit, nr);
876 td->ts.total_submit++;
879 void io_u_mark_complete(struct thread_data *td, unsigned int nr)
881 __io_u_mark_map(td->ts.io_u_complete, nr);
882 td->ts.total_complete++;
885 void io_u_mark_depth(struct thread_data *td, unsigned int nr)
889 switch (td->cur_depth) {
911 td->ts.io_u_map[idx] += nr;
914 static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
951 assert(idx < FIO_IO_U_LAT_U_NR);
952 td->ts.io_u_lat_u[idx]++;
955 static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
996 assert(idx < FIO_IO_U_LAT_M_NR);
997 td->ts.io_u_lat_m[idx]++;
1000 static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
1003 io_u_mark_lat_usec(td, usec);
1005 io_u_mark_lat_msec(td, usec / 1000);
1009 * Get next file to service by choosing one at random
1011 static struct fio_file *get_next_file_rand(struct thread_data *td,
1012 enum fio_file_flags goodf,
1013 enum fio_file_flags badf)
1015 uint64_t frand_max = rand_max(&td->next_file_state);
1023 r = __rand(&td->next_file_state);
1024 fno = (unsigned int) ((double) td->o.nr_files
1025 * (r / (frand_max + 1.0)));
1028 if (fio_file_done(f))
1031 if (!fio_file_open(f)) {
1034 if (td->nr_open_files >= td->o.open_files)
1035 return ERR_PTR(-EBUSY);
1037 err = td_io_open_file(td, f);
1043 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
1044 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
1048 td_io_close_file(td, f);
1053 * Get next file to service by doing round robin between all available ones
1055 static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
1058 unsigned int old_next_file = td->next_file;
1064 f = td->files[td->next_file];
1067 if (td->next_file >= td->o.nr_files)
1070 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
1071 if (fio_file_done(f)) {
1076 if (!fio_file_open(f)) {
1079 if (td->nr_open_files >= td->o.open_files)
1080 return ERR_PTR(-EBUSY);
1082 err = td_io_open_file(td, f);
1084 dprint(FD_FILE, "error %d on open of %s\n",
1092 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
1094 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
1098 td_io_close_file(td, f);
1101 } while (td->next_file != old_next_file);
1103 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
1107 static struct fio_file *__get_next_file(struct thread_data *td)
1111 assert(td->o.nr_files <= td->files_index);
1113 if (td->nr_done_files >= td->o.nr_files) {
1114 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
1115 " nr_files=%d\n", td->nr_open_files,
1121 f = td->file_service_file;
1122 if (f && fio_file_open(f) && !fio_file_closing(f)) {
1123 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
1125 if (td->file_service_left--)
1129 if (td->o.file_service_type == FIO_FSERVICE_RR ||
1130 td->o.file_service_type == FIO_FSERVICE_SEQ)
1131 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
1133 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1138 td->file_service_file = f;
1139 td->file_service_left = td->file_service_nr - 1;
1142 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1144 dprint(FD_FILE, "get_next_file: NULL\n");
1148 static struct fio_file *get_next_file(struct thread_data *td)
1150 if (td->flags & TD_F_PROFILE_OPS) {
1151 struct prof_io_ops *ops = &td->prof_io_ops;
1153 if (ops->get_next_file)
1154 return ops->get_next_file(td);
1157 return __get_next_file(td);
1160 static long set_io_u_file(struct thread_data *td, struct io_u *io_u)
1165 f = get_next_file(td);
1166 if (IS_ERR_OR_NULL(f))
1172 if (!fill_io_u(td, io_u))
1175 put_file_log(td, f);
1176 td_io_close_file(td, f);
1178 fio_file_set_done(f);
1179 td->nr_done_files++;
1180 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
1181 td->nr_done_files, td->o.nr_files);
1187 static void lat_fatal(struct thread_data *td, struct io_completion_data *icd,
1188 unsigned long tusec, unsigned long max_usec)
1191 log_err("fio: latency of %lu usec exceeds specified max (%lu usec)\n", tusec, max_usec);
1192 td_verror(td, ETIMEDOUT, "max latency exceeded");
1193 icd->error = ETIMEDOUT;
1196 static void lat_new_cycle(struct thread_data *td)
1198 fio_gettime(&td->latency_ts, NULL);
1199 td->latency_ios = ddir_rw_sum(td->io_blocks);
1200 td->latency_failed = 0;
1204 * We had an IO outside the latency target. Reduce the queue depth. If we
1205 * are at QD=1, then it's time to give up.
1207 static bool __lat_target_failed(struct thread_data *td)
1209 if (td->latency_qd == 1)
1212 td->latency_qd_high = td->latency_qd;
1214 if (td->latency_qd == td->latency_qd_low)
1215 td->latency_qd_low--;
1217 td->latency_qd = (td->latency_qd + td->latency_qd_low) / 2;
1219 dprint(FD_RATE, "Ramped down: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
1222 * When we ramp QD down, quiesce existing IO to prevent
1223 * a storm of ramp downs due to pending higher depth.
1230 static bool lat_target_failed(struct thread_data *td)
1232 if (td->o.latency_percentile.u.f == 100.0)
1233 return __lat_target_failed(td);
1235 td->latency_failed++;
1239 void lat_target_init(struct thread_data *td)
1241 td->latency_end_run = 0;
1243 if (td->o.latency_target) {
1244 dprint(FD_RATE, "Latency target=%llu\n", td->o.latency_target);
1245 fio_gettime(&td->latency_ts, NULL);
1247 td->latency_qd_high = td->o.iodepth;
1248 td->latency_qd_low = 1;
1249 td->latency_ios = ddir_rw_sum(td->io_blocks);
1251 td->latency_qd = td->o.iodepth;
1254 void lat_target_reset(struct thread_data *td)
1256 if (!td->latency_end_run)
1257 lat_target_init(td);
1260 static void lat_target_success(struct thread_data *td)
1262 const unsigned int qd = td->latency_qd;
1263 struct thread_options *o = &td->o;
1265 td->latency_qd_low = td->latency_qd;
1268 * If we haven't failed yet, we double up to a failing value instead
1269 * of bisecting from highest possible queue depth. If we have set
1270 * a limit other than td->o.iodepth, bisect between that.
1272 if (td->latency_qd_high != o->iodepth)
1273 td->latency_qd = (td->latency_qd + td->latency_qd_high) / 2;
1275 td->latency_qd *= 2;
1277 if (td->latency_qd > o->iodepth)
1278 td->latency_qd = o->iodepth;
1280 dprint(FD_RATE, "Ramped up: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
1283 * Same as last one, we are done. Let it run a latency cycle, so
1284 * we get only the results from the targeted depth.
1286 if (td->latency_qd == qd) {
1287 if (td->latency_end_run) {
1288 dprint(FD_RATE, "We are done\n");
1291 dprint(FD_RATE, "Quiesce and final run\n");
1293 td->latency_end_run = 1;
1294 reset_all_stats(td);
1303 * Check if we can bump the queue depth
1305 void lat_target_check(struct thread_data *td)
1307 uint64_t usec_window;
1311 usec_window = utime_since_now(&td->latency_ts);
1312 if (usec_window < td->o.latency_window)
1315 ios = ddir_rw_sum(td->io_blocks) - td->latency_ios;
1316 success_ios = (double) (ios - td->latency_failed) / (double) ios;
1317 success_ios *= 100.0;
1319 dprint(FD_RATE, "Success rate: %.2f%% (target %.2f%%)\n", success_ios, td->o.latency_percentile.u.f);
1321 if (success_ios >= td->o.latency_percentile.u.f)
1322 lat_target_success(td);
1324 __lat_target_failed(td);
1328 * If latency target is enabled, we might be ramping up or down and not
1329 * using the full queue depth available.
1331 bool queue_full(const struct thread_data *td)
1333 const int qempty = io_u_qempty(&td->io_u_freelist);
1337 if (!td->o.latency_target)
1340 return td->cur_depth >= td->latency_qd;
1343 struct io_u *__get_io_u(struct thread_data *td)
1345 struct io_u *io_u = NULL;
1353 if (!io_u_rempty(&td->io_u_requeues))
1354 io_u = io_u_rpop(&td->io_u_requeues);
1355 else if (!queue_full(td)) {
1356 io_u = io_u_qpop(&td->io_u_freelist);
1361 io_u->end_io = NULL;
1365 assert(io_u->flags & IO_U_F_FREE);
1366 io_u_clear(io_u, IO_U_F_FREE | IO_U_F_NO_FILE_PUT |
1367 IO_U_F_TRIMMED | IO_U_F_BARRIER |
1371 io_u->acct_ddir = -1;
1373 assert(!(td->flags & TD_F_CHILD));
1374 io_u_set(io_u, IO_U_F_IN_CUR_DEPTH);
1376 } else if (td_async_processing(td)) {
1378 * We ran out, wait for async verify threads to finish and
1381 assert(!(td->flags & TD_F_CHILD));
1382 assert(!pthread_cond_wait(&td->free_cond, &td->io_u_lock));
1390 static bool check_get_trim(struct thread_data *td, struct io_u *io_u)
1392 if (!(td->flags & TD_F_TRIM_BACKLOG))
1395 if (td->trim_entries) {
1398 if (td->trim_batch) {
1401 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1402 td->last_ddir != DDIR_READ) {
1403 td->trim_batch = td->o.trim_batch;
1404 if (!td->trim_batch)
1405 td->trim_batch = td->o.trim_backlog;
1409 if (get_trim && !get_next_trim(td, io_u))
1416 static bool check_get_verify(struct thread_data *td, struct io_u *io_u)
1418 if (!(td->flags & TD_F_VER_BACKLOG))
1421 if (td->io_hist_len) {
1424 if (td->verify_batch)
1426 else if (!(td->io_hist_len % td->o.verify_backlog) &&
1427 td->last_ddir != DDIR_READ) {
1428 td->verify_batch = td->o.verify_batch;
1429 if (!td->verify_batch)
1430 td->verify_batch = td->o.verify_backlog;
1434 if (get_verify && !get_next_verify(td, io_u)) {
1444 * Fill offset and start time into the buffer content, to prevent too
1445 * easy compressible data for simple de-dupe attempts. Do this for every
1446 * 512b block in the range, since that should be the smallest block size
1447 * we can expect from a device.
1449 static void small_content_scramble(struct io_u *io_u)
1451 unsigned int i, nr_blocks = io_u->buflen / 512;
1453 unsigned int offset;
1460 boffset = io_u->offset;
1461 io_u->buf_filled_len = 0;
1463 for (i = 0; i < nr_blocks; i++) {
1465 * Fill the byte offset into a "random" start offset of
1466 * the buffer, given by the product of the usec time
1467 * and the actual offset.
1469 offset = (io_u->start_time.tv_usec ^ boffset) & 511;
1470 offset &= ~(sizeof(uint64_t) - 1);
1471 if (offset >= 512 - sizeof(uint64_t))
1472 offset -= sizeof(uint64_t);
1473 memcpy(p + offset, &boffset, sizeof(boffset));
1475 end = p + 512 - sizeof(io_u->start_time);
1476 memcpy(end, &io_u->start_time, sizeof(io_u->start_time));
1483 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1484 * etc. The returned io_u is fully ready to be prepped and submitted.
1486 struct io_u *get_io_u(struct thread_data *td)
1490 int do_scramble = 0;
1493 io_u = __get_io_u(td);
1495 dprint(FD_IO, "__get_io_u failed\n");
1499 if (check_get_verify(td, io_u))
1501 if (check_get_trim(td, io_u))
1505 * from a requeue, io_u already setup
1511 * If using an iolog, grab next piece if any available.
1513 if (td->flags & TD_F_READ_IOLOG) {
1514 if (read_iolog_get(td, io_u))
1516 } else if (set_io_u_file(td, io_u)) {
1518 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1524 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1528 assert(fio_file_open(f));
1530 if (ddir_rw(io_u->ddir)) {
1531 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
1532 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
1536 f->last_start[io_u->ddir] = io_u->offset;
1537 f->last_pos[io_u->ddir] = io_u->offset + io_u->buflen;
1539 if (io_u->ddir == DDIR_WRITE) {
1540 if (td->flags & TD_F_REFILL_BUFFERS) {
1541 io_u_fill_buffer(td, io_u,
1542 td->o.min_bs[DDIR_WRITE],
1544 } else if ((td->flags & TD_F_SCRAMBLE_BUFFERS) &&
1545 !(td->flags & TD_F_COMPRESS))
1547 if (td->flags & TD_F_VER_NONE) {
1548 populate_verify_io_u(td, io_u);
1551 } else if (io_u->ddir == DDIR_READ) {
1553 * Reset the buf_filled parameters so next time if the
1554 * buffer is used for writes it is refilled.
1556 io_u->buf_filled_len = 0;
1561 * Set io data pointers.
1563 io_u->xfer_buf = io_u->buf;
1564 io_u->xfer_buflen = io_u->buflen;
1568 if (!td_io_prep(td, io_u)) {
1569 if (!td->o.disable_lat)
1570 fio_gettime(&io_u->start_time, NULL);
1572 small_content_scramble(io_u);
1576 dprint(FD_IO, "get_io_u failed\n");
1578 return ERR_PTR(ret);
1581 static void __io_u_log_error(struct thread_data *td, struct io_u *io_u)
1583 enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
1585 if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1588 log_err("fio: io_u error%s%s: %s: %s offset=%llu, buflen=%lu\n",
1589 io_u->file ? " on file " : "",
1590 io_u->file ? io_u->file->file_name : "",
1591 strerror(io_u->error),
1592 io_ddir_name(io_u->ddir),
1593 io_u->offset, io_u->xfer_buflen);
1595 if (td->io_ops->errdetails) {
1596 char *err = td->io_ops->errdetails(io_u);
1598 log_err("fio: %s\n", err);
1603 td_verror(td, io_u->error, "io_u error");
1606 void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1608 __io_u_log_error(td, io_u);
1610 __io_u_log_error(td, io_u);
1613 static inline bool gtod_reduce(struct thread_data *td)
1615 return (td->o.disable_clat && td->o.disable_slat && td->o.disable_bw)
1616 || td->o.gtod_reduce;
1619 static void account_io_completion(struct thread_data *td, struct io_u *io_u,
1620 struct io_completion_data *icd,
1621 const enum fio_ddir idx, unsigned int bytes)
1623 const int no_reduce = !gtod_reduce(td);
1624 unsigned long lusec = 0;
1630 lusec = utime_since(&io_u->issue_time, &icd->time);
1632 if (!td->o.disable_lat) {
1633 unsigned long tusec;
1635 tusec = utime_since(&io_u->start_time, &icd->time);
1636 add_lat_sample(td, idx, tusec, bytes, io_u->offset);
1638 if (td->flags & TD_F_PROFILE_OPS) {
1639 struct prof_io_ops *ops = &td->prof_io_ops;
1642 icd->error = ops->io_u_lat(td, tusec);
1645 if (td->o.max_latency && tusec > td->o.max_latency)
1646 lat_fatal(td, icd, tusec, td->o.max_latency);
1647 if (td->o.latency_target && tusec > td->o.latency_target) {
1648 if (lat_target_failed(td))
1649 lat_fatal(td, icd, tusec, td->o.latency_target);
1653 if (!td->o.disable_clat) {
1654 add_clat_sample(td, idx, lusec, bytes, io_u->offset);
1655 io_u_mark_latency(td, lusec);
1658 if (!td->o.disable_bw)
1659 add_bw_sample(td, idx, bytes, &icd->time);
1662 add_iops_sample(td, idx, bytes, &icd->time);
1664 if (td->ts.nr_block_infos && io_u->ddir == DDIR_TRIM) {
1665 uint32_t *info = io_u_block_info(td, io_u);
1666 if (BLOCK_INFO_STATE(*info) < BLOCK_STATE_TRIM_FAILURE) {
1667 if (io_u->ddir == DDIR_TRIM) {
1668 *info = BLOCK_INFO(BLOCK_STATE_TRIMMED,
1669 BLOCK_INFO_TRIMS(*info) + 1);
1670 } else if (io_u->ddir == DDIR_WRITE) {
1671 *info = BLOCK_INFO_SET_STATE(BLOCK_STATE_WRITTEN,
1678 static void io_completed(struct thread_data *td, struct io_u **io_u_ptr,
1679 struct io_completion_data *icd)
1681 struct io_u *io_u = *io_u_ptr;
1682 enum fio_ddir ddir = io_u->ddir;
1683 struct fio_file *f = io_u->file;
1685 dprint_io_u(io_u, "io complete");
1687 assert(io_u->flags & IO_U_F_FLIGHT);
1688 io_u_clear(io_u, IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
1691 * Mark IO ok to verify
1695 * Remove errored entry from the verification list
1698 unlog_io_piece(td, io_u);
1700 io_u->ipo->flags &= ~IP_F_IN_FLIGHT;
1705 if (ddir_sync(ddir)) {
1706 td->last_was_sync = 1;
1708 f->first_write = -1ULL;
1709 f->last_write = -1ULL;
1714 td->last_was_sync = 0;
1715 td->last_ddir = ddir;
1717 if (!io_u->error && ddir_rw(ddir)) {
1718 unsigned int bytes = io_u->buflen - io_u->resid;
1721 td->io_blocks[ddir]++;
1722 td->this_io_blocks[ddir]++;
1723 td->io_bytes[ddir] += bytes;
1725 if (!(io_u->flags & IO_U_F_VER_LIST))
1726 td->this_io_bytes[ddir] += bytes;
1728 if (ddir == DDIR_WRITE) {
1730 if (f->first_write == -1ULL ||
1731 io_u->offset < f->first_write)
1732 f->first_write = io_u->offset;
1733 if (f->last_write == -1ULL ||
1734 ((io_u->offset + bytes) > f->last_write))
1735 f->last_write = io_u->offset + bytes;
1737 if (td->last_write_comp) {
1738 int idx = td->last_write_idx++;
1740 td->last_write_comp[idx] = io_u->offset;
1741 if (td->last_write_idx == td->o.iodepth)
1742 td->last_write_idx = 0;
1746 if (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
1747 td->runstate == TD_VERIFYING))
1748 account_io_completion(td, io_u, icd, ddir, bytes);
1750 icd->bytes_done[ddir] += bytes;
1753 ret = io_u->end_io(td, io_u_ptr);
1755 if (ret && !icd->error)
1758 } else if (io_u->error) {
1759 icd->error = io_u->error;
1760 io_u_log_error(td, io_u);
1763 enum error_type_bit eb = td_error_type(ddir, icd->error);
1765 if (!td_non_fatal_error(td, eb, icd->error))
1769 * If there is a non_fatal error, then add to the error count
1770 * and clear all the errors.
1772 update_error_count(td, icd->error);
1780 static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1785 if (!gtod_reduce(td))
1786 fio_gettime(&icd->time, NULL);
1791 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1792 icd->bytes_done[ddir] = 0;
1795 static void ios_completed(struct thread_data *td,
1796 struct io_completion_data *icd)
1801 for (i = 0; i < icd->nr; i++) {
1802 io_u = td->io_ops->event(td, i);
1804 io_completed(td, &io_u, icd);
1812 * Complete a single io_u for the sync engines.
1814 int io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
1816 struct io_completion_data icd;
1819 init_icd(td, &icd, 1);
1820 io_completed(td, &io_u, &icd);
1826 td_verror(td, icd.error, "io_u_sync_complete");
1830 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1831 td->bytes_done[ddir] += icd.bytes_done[ddir];
1837 * Called to complete min_events number of io for the async engines.
1839 int io_u_queued_complete(struct thread_data *td, int min_evts)
1841 struct io_completion_data icd;
1842 struct timespec *tvp = NULL;
1844 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
1846 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
1850 else if (min_evts > td->cur_depth)
1851 min_evts = td->cur_depth;
1853 /* No worries, td_io_getevents fixes min and max if they are
1854 * set incorrectly */
1855 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete_max, tvp);
1857 td_verror(td, -ret, "td_io_getevents");
1862 init_icd(td, &icd, ret);
1863 ios_completed(td, &icd);
1865 td_verror(td, icd.error, "io_u_queued_complete");
1869 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1870 td->bytes_done[ddir] += icd.bytes_done[ddir];
1876 * Call when io_u is really queued, to update the submission latency.
1878 void io_u_queued(struct thread_data *td, struct io_u *io_u)
1880 if (!td->o.disable_slat) {
1881 unsigned long slat_time;
1883 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
1888 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen,
1894 * See if we should reuse the last seed, if dedupe is enabled
1896 static struct frand_state *get_buf_state(struct thread_data *td)
1902 if (!td->o.dedupe_percentage)
1903 return &td->buf_state;
1904 else if (td->o.dedupe_percentage == 100) {
1905 frand_copy(&td->buf_state_prev, &td->buf_state);
1906 return &td->buf_state;
1909 frand_max = rand_max(&td->dedupe_state);
1910 r = __rand(&td->dedupe_state);
1911 v = 1 + (int) (100.0 * (r / (frand_max + 1.0)));
1913 if (v <= td->o.dedupe_percentage)
1914 return &td->buf_state_prev;
1916 return &td->buf_state;
1919 static void save_buf_state(struct thread_data *td, struct frand_state *rs)
1921 if (td->o.dedupe_percentage == 100)
1922 frand_copy(rs, &td->buf_state_prev);
1923 else if (rs == &td->buf_state)
1924 frand_copy(&td->buf_state_prev, rs);
1927 void fill_io_buffer(struct thread_data *td, void *buf, unsigned int min_write,
1928 unsigned int max_bs)
1930 struct thread_options *o = &td->o;
1932 if (o->compress_percentage || o->dedupe_percentage) {
1933 unsigned int perc = td->o.compress_percentage;
1934 struct frand_state *rs;
1935 unsigned int left = max_bs;
1936 unsigned int this_write;
1939 rs = get_buf_state(td);
1941 min_write = min(min_write, left);
1944 this_write = min_not_zero(min_write,
1945 td->o.compress_chunk);
1947 fill_random_buf_percentage(rs, buf, perc,
1948 this_write, this_write,
1950 o->buffer_pattern_bytes);
1952 fill_random_buf(rs, buf, min_write);
1953 this_write = min_write;
1958 save_buf_state(td, rs);
1960 } else if (o->buffer_pattern_bytes)
1961 fill_buffer_pattern(td, buf, max_bs);
1962 else if (o->zero_buffers)
1963 memset(buf, 0, max_bs);
1965 fill_random_buf(get_buf_state(td), buf, max_bs);
1969 * "randomly" fill the buffer contents
1971 void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1972 unsigned int min_write, unsigned int max_bs)
1974 io_u->buf_filled_len = 0;
1975 fill_io_buffer(td, io_u->buf, min_write, max_bs);