14 struct io_completion_data {
17 int error; /* output */
18 unsigned long bytes_done[DDIR_RWDIR_CNT]; /* output */
19 struct timeval time; /* output */
23 * The ->file_map[] contains a map of blocks we have or have not done io
24 * to yet. Used to make sure we cover the entire range in a fair fashion.
26 static int random_map_free(struct fio_file *f, const unsigned long long block)
28 unsigned int idx = RAND_MAP_IDX(f, block);
29 unsigned int bit = RAND_MAP_BIT(f, block);
31 dprint(FD_RANDOM, "free: b=%llu, idx=%u, bit=%u\n", block, idx, bit);
33 return (f->file_map[idx] & (1UL << bit)) == 0;
37 * Mark a given offset as used in the map.
39 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
41 unsigned int min_bs = td->o.rw_min_bs;
42 struct fio_file *f = io_u->file;
43 unsigned long long block;
44 unsigned int blocks, nr_blocks;
47 block = (io_u->offset - f->file_offset) / (unsigned long long) min_bs;
48 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
50 busy_check = !(io_u->flags & IO_U_F_BUSY_OK);
53 unsigned int idx, bit;
54 unsigned long mask, this_blocks;
57 * If we have a mixed random workload, we may
58 * encounter blocks we already did IO to.
64 if ((td->o.ddir_seq_nr == 1) && !random_map_free(f, block))
67 idx = RAND_MAP_IDX(f, block);
68 bit = RAND_MAP_BIT(f, block);
70 fio_assert(td, idx < f->num_maps);
72 this_blocks = nr_blocks;
73 if (this_blocks + bit > BLOCKS_PER_MAP)
74 this_blocks = BLOCKS_PER_MAP - bit;
77 if (this_blocks == BLOCKS_PER_MAP)
80 mask = ((1UL << this_blocks) - 1) << bit;
82 if (!(f->file_map[idx] & mask))
86 } while (this_blocks);
91 f->file_map[idx] |= mask;
92 nr_blocks -= this_blocks;
93 blocks += this_blocks;
97 if ((blocks * min_bs) < io_u->buflen)
98 io_u->buflen = blocks * min_bs;
101 static unsigned long long last_block(struct thread_data *td, struct fio_file *f,
104 unsigned long long max_blocks;
105 unsigned long long max_size;
107 assert(ddir_rw(ddir));
110 * Hmm, should we make sure that ->io_size <= ->real_file_size?
112 max_size = f->io_size;
113 if (max_size > f->real_file_size)
114 max_size = f->real_file_size;
116 if (td->o.zone_range)
117 max_size = td->o.zone_range;
119 max_blocks = max_size / (unsigned long long) td->o.ba[ddir];
127 * Return the next free block in the map.
129 static int get_next_free_block(struct thread_data *td, struct fio_file *f,
130 enum fio_ddir ddir, unsigned long long *b)
132 unsigned long long block, min_bs = td->o.rw_min_bs, lastb;
135 lastb = last_block(td, f, ddir);
139 i = f->last_free_lookup;
140 block = i * BLOCKS_PER_MAP;
141 while (block * min_bs < f->real_file_size &&
142 block * min_bs < f->io_size) {
143 if (f->file_map[i] != -1UL) {
144 block += ffz(f->file_map[i]);
147 f->last_free_lookup = i;
152 block += BLOCKS_PER_MAP;
156 dprint(FD_IO, "failed finding a free block\n");
160 static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
161 enum fio_ddir ddir, unsigned long long *b)
163 unsigned long long rmax, r, lastb;
166 lastb = last_block(td, f, ddir);
170 if (f->failed_rands >= 200)
173 rmax = td->o.use_os_rand ? OS_RAND_MAX : FRAND_MAX;
175 if (td->o.use_os_rand)
176 r = os_random_long(&td->random_state);
178 r = __rand(&td->__random_state);
180 *b = (lastb - 1) * (r / ((unsigned long long) rmax + 1.0));
182 dprint(FD_RANDOM, "off rand %llu\n", r);
186 * if we are not maintaining a random map, we are done.
188 if (!file_randommap(td, f))
192 * calculate map offset and check if it's free
194 if (random_map_free(f, *b))
197 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
201 if (!f->failed_rands++)
202 f->last_free_lookup = 0;
205 * we get here, if we didn't suceed in looking up a block. generate
206 * a random start offset into the filemap, and find the first free
211 f->last_free_lookup = (f->num_maps - 1) *
212 (r / ((unsigned long long) rmax + 1.0));
213 if (!get_next_free_block(td, f, ddir, b))
216 if (td->o.use_os_rand)
217 r = os_random_long(&td->random_state);
219 r = __rand(&td->__random_state);
223 * that didn't work either, try exhaustive search from the start
225 f->last_free_lookup = 0;
227 if (!get_next_free_block(td, f, ddir, b))
229 f->last_free_lookup = 0;
230 return get_next_free_block(td, f, ddir, b);
237 static int __get_next_rand_offset_zipf(struct thread_data *td,
238 struct fio_file *f, enum fio_ddir ddir,
239 unsigned long long *b)
241 *b = zipf_next(&f->zipf);
245 static int __get_next_rand_offset_pareto(struct thread_data *td,
246 struct fio_file *f, enum fio_ddir ddir,
247 unsigned long long *b)
249 *b = pareto_next(&f->zipf);
253 static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
254 enum fio_ddir ddir, unsigned long long *b)
256 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
257 return __get_next_rand_offset(td, f, ddir, b);
258 else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
259 return __get_next_rand_offset_zipf(td, f, ddir, b);
260 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
261 return __get_next_rand_offset_pareto(td, f, ddir, b);
263 log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
267 static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
268 enum fio_ddir ddir, unsigned long long *b)
270 if (!get_next_rand_offset(td, f, ddir, b))
273 if (td->o.time_based) {
275 if (!get_next_rand_offset(td, f, ddir, b))
279 dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
280 f->file_name, f->last_pos, f->real_file_size);
284 static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
285 enum fio_ddir ddir, unsigned long long *offset)
287 assert(ddir_rw(ddir));
289 if (f->last_pos >= f->io_size + get_start_offset(td) && td->o.time_based)
290 f->last_pos = f->last_pos - f->io_size;
292 if (f->last_pos < f->real_file_size) {
293 unsigned long long pos;
295 if (f->last_pos == f->file_offset && td->o.ddir_seq_add < 0)
296 f->last_pos = f->real_file_size;
298 pos = f->last_pos - f->file_offset;
300 pos += td->o.ddir_seq_add;
309 static int get_next_block(struct thread_data *td, struct io_u *io_u,
310 enum fio_ddir ddir, int rw_seq)
312 struct fio_file *f = io_u->file;
313 unsigned long long b, offset;
316 assert(ddir_rw(ddir));
322 ret = get_next_rand_block(td, f, ddir, &b);
324 ret = get_next_seq_offset(td, f, ddir, &offset);
326 io_u->flags |= IO_U_F_BUSY_OK;
328 if (td->o.rw_seq == RW_SEQ_SEQ) {
329 ret = get_next_seq_offset(td, f, ddir, &offset);
331 ret = get_next_rand_block(td, f, ddir, &b);
332 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
333 if (f->last_start != -1ULL)
334 offset = f->last_start - f->file_offset;
339 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
346 io_u->offset = offset;
348 io_u->offset = b * td->o.ba[ddir];
350 log_err("fio: bug in offset generation\n");
359 * For random io, generate a random new block and see if it's used. Repeat
360 * until we find a free one. For sequential io, just return the end of
361 * the last io issued.
363 static int __get_next_offset(struct thread_data *td, struct io_u *io_u)
365 struct fio_file *f = io_u->file;
366 enum fio_ddir ddir = io_u->ddir;
369 assert(ddir_rw(ddir));
371 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
373 td->ddir_seq_nr = td->o.ddir_seq_nr;
376 if (get_next_block(td, io_u, ddir, rw_seq_hit))
379 if (io_u->offset >= f->io_size) {
380 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
381 io_u->offset, f->io_size);
385 io_u->offset += f->file_offset;
386 if (io_u->offset >= f->real_file_size) {
387 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
388 io_u->offset, f->real_file_size);
395 static int get_next_offset(struct thread_data *td, struct io_u *io_u)
397 struct prof_io_ops *ops = &td->prof_io_ops;
399 if (ops->fill_io_u_off)
400 return ops->fill_io_u_off(td, io_u);
402 return __get_next_offset(td, io_u);
405 static inline int io_u_fits(struct thread_data *td, struct io_u *io_u,
408 struct fio_file *f = io_u->file;
410 return io_u->offset + buflen <= f->io_size + get_start_offset(td);
413 static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u)
415 const int ddir = io_u->ddir;
416 unsigned int uninitialized_var(buflen);
417 unsigned int minbs, maxbs;
418 unsigned long r, rand_max;
420 assert(ddir_rw(ddir));
422 minbs = td->o.min_bs[ddir];
423 maxbs = td->o.max_bs[ddir];
429 * If we can't satisfy the min block size from here, then fail
431 if (!io_u_fits(td, io_u, minbs))
434 if (td->o.use_os_rand)
435 rand_max = OS_RAND_MAX;
437 rand_max = FRAND_MAX;
440 if (td->o.use_os_rand)
441 r = os_random_long(&td->bsrange_state);
443 r = __rand(&td->__bsrange_state);
445 if (!td->o.bssplit_nr[ddir]) {
446 buflen = 1 + (unsigned int) ((double) maxbs *
447 (r / (rand_max + 1.0)));
454 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
455 struct bssplit *bsp = &td->o.bssplit[ddir][i];
459 if ((r <= ((rand_max / 100L) * perc)) &&
460 io_u_fits(td, io_u, buflen))
465 if (!td->o.bs_unaligned && is_power_of_2(minbs))
466 buflen = (buflen + minbs - 1) & ~(minbs - 1);
468 } while (!io_u_fits(td, io_u, buflen));
473 static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
475 struct prof_io_ops *ops = &td->prof_io_ops;
477 if (ops->fill_io_u_size)
478 return ops->fill_io_u_size(td, io_u);
480 return __get_next_buflen(td, io_u);
483 static void set_rwmix_bytes(struct thread_data *td)
488 * we do time or byte based switch. this is needed because
489 * buffered writes may issue a lot quicker than they complete,
490 * whereas reads do not.
492 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
493 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
496 static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
501 if (td->o.use_os_rand) {
502 r = os_random_long(&td->rwmix_state);
503 v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
505 r = __rand(&td->__rwmix_state);
506 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
509 if (v <= td->o.rwmix[DDIR_READ])
515 static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
517 enum fio_ddir odir = ddir ^ 1;
521 assert(ddir_rw(ddir));
523 if (td->rate_pending_usleep[ddir] <= 0)
527 * We have too much pending sleep in this direction. See if we
532 * Other direction does not have too much pending, switch
534 if (td->rate_pending_usleep[odir] < 100000)
538 * Both directions have pending sleep. Sleep the minimum time
539 * and deduct from both.
541 if (td->rate_pending_usleep[ddir] <=
542 td->rate_pending_usleep[odir]) {
543 usec = td->rate_pending_usleep[ddir];
545 usec = td->rate_pending_usleep[odir];
549 usec = td->rate_pending_usleep[ddir];
552 * We are going to sleep, ensure that we flush anything pending as
553 * not to skew our latency numbers.
555 * Changed to only monitor 'in flight' requests here instead of the
556 * td->cur_depth, b/c td->cur_depth does not accurately represent
557 * io's that have been actually submitted to an async engine,
558 * and cur_depth is meaningless for sync engines.
560 if (td->io_u_in_flight) {
563 ret = io_u_queued_complete(td, td->io_u_in_flight, NULL);
566 fio_gettime(&t, NULL);
567 usec_sleep(td, usec);
568 usec = utime_since_now(&t);
570 td->rate_pending_usleep[ddir] -= usec;
573 if (td_rw(td) && __should_check_rate(td, odir))
574 td->rate_pending_usleep[odir] -= usec;
582 * Return the data direction for the next io_u. If the job is a
583 * mixed read/write workload, check the rwmix cycle and switch if
586 static enum fio_ddir get_rw_ddir(struct thread_data *td)
591 * see if it's time to fsync
593 if (td->o.fsync_blocks &&
594 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
595 td->io_issues[DDIR_WRITE] && should_fsync(td))
599 * see if it's time to fdatasync
601 if (td->o.fdatasync_blocks &&
602 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
603 td->io_issues[DDIR_WRITE] && should_fsync(td))
604 return DDIR_DATASYNC;
607 * see if it's time to sync_file_range
609 if (td->sync_file_range_nr &&
610 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
611 td->io_issues[DDIR_WRITE] && should_fsync(td))
612 return DDIR_SYNC_FILE_RANGE;
616 * Check if it's time to seed a new data direction.
618 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
620 * Put a top limit on how many bytes we do for
621 * one data direction, to avoid overflowing the
624 ddir = get_rand_ddir(td);
626 if (ddir != td->rwmix_ddir)
629 td->rwmix_ddir = ddir;
631 ddir = td->rwmix_ddir;
632 } else if (td_read(td))
634 else if (td_write(td))
639 td->rwmix_ddir = rate_ddir(td, ddir);
640 return td->rwmix_ddir;
643 static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
645 io_u->ddir = get_rw_ddir(td);
647 if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
648 td->o.barrier_blocks &&
649 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
650 td->io_issues[DDIR_WRITE])
651 io_u->flags |= IO_U_F_BARRIER;
654 void put_file_log(struct thread_data *td, struct fio_file *f)
656 int ret = put_file(td, f);
659 td_verror(td, ret, "file close");
662 void put_io_u(struct thread_data *td, struct io_u *io_u)
666 if (io_u->file && !(io_u->flags & IO_U_F_FREE_DEF))
667 put_file_log(td, io_u->file);
669 io_u->flags &= ~IO_U_F_FREE_DEF;
670 io_u->flags |= IO_U_F_FREE;
672 if (io_u->flags & IO_U_F_IN_CUR_DEPTH)
674 flist_del_init(&io_u->list);
675 flist_add(&io_u->list, &td->io_u_freelist);
677 td_io_u_free_notify(td);
680 void clear_io_u(struct thread_data *td, struct io_u *io_u)
682 io_u->flags &= ~IO_U_F_FLIGHT;
686 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
688 struct io_u *__io_u = *io_u;
690 dprint(FD_IO, "requeue %p\n", __io_u);
694 __io_u->flags |= IO_U_F_FREE;
695 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(__io_u->ddir))
696 td->io_issues[__io_u->ddir]--;
698 __io_u->flags &= ~IO_U_F_FLIGHT;
699 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH)
701 flist_del(&__io_u->list);
702 flist_add_tail(&__io_u->list, &td->io_u_requeues);
707 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
709 if (td->io_ops->flags & FIO_NOIO)
712 set_rw_ddir(td, io_u);
715 * fsync() or fdatasync() or trim etc, we are done
717 if (!ddir_rw(io_u->ddir))
721 * See if it's time to switch to a new zone
723 if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
725 io_u->file->file_offset += td->o.zone_range + td->o.zone_skip;
726 io_u->file->last_pos = io_u->file->file_offset;
727 td->io_skip_bytes += td->o.zone_skip;
731 * No log, let the seq/rand engine retrieve the next buflen and
734 if (get_next_offset(td, io_u)) {
735 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
739 io_u->buflen = get_next_buflen(td, io_u);
741 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
745 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
746 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
747 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
748 io_u->buflen, io_u->file->real_file_size);
753 * mark entry before potentially trimming io_u
755 if (td_random(td) && file_randommap(td, io_u->file))
756 mark_random_map(td, io_u);
759 * If using a write iolog, store this entry.
762 dprint_io_u(io_u, "fill_io_u");
763 td->zone_bytes += io_u->buflen;
768 static void __io_u_mark_map(unsigned int *map, unsigned int nr)
797 void io_u_mark_submit(struct thread_data *td, unsigned int nr)
799 __io_u_mark_map(td->ts.io_u_submit, nr);
800 td->ts.total_submit++;
803 void io_u_mark_complete(struct thread_data *td, unsigned int nr)
805 __io_u_mark_map(td->ts.io_u_complete, nr);
806 td->ts.total_complete++;
809 void io_u_mark_depth(struct thread_data *td, unsigned int nr)
813 switch (td->cur_depth) {
835 td->ts.io_u_map[idx] += nr;
838 static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
875 assert(idx < FIO_IO_U_LAT_U_NR);
876 td->ts.io_u_lat_u[idx]++;
879 static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
920 assert(idx < FIO_IO_U_LAT_M_NR);
921 td->ts.io_u_lat_m[idx]++;
924 static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
927 io_u_mark_lat_usec(td, usec);
929 io_u_mark_lat_msec(td, usec / 1000);
933 * Get next file to service by choosing one at random
935 static struct fio_file *get_next_file_rand(struct thread_data *td,
936 enum fio_file_flags goodf,
937 enum fio_file_flags badf)
946 if (td->o.use_os_rand) {
947 r = os_random_long(&td->next_file_state);
948 fno = (unsigned int) ((double) td->o.nr_files
949 * (r / (OS_RAND_MAX + 1.0)));
951 r = __rand(&td->__next_file_state);
952 fno = (unsigned int) ((double) td->o.nr_files
953 * (r / (FRAND_MAX + 1.0)));
957 if (fio_file_done(f))
960 if (!fio_file_open(f)) {
963 err = td_io_open_file(td, f);
969 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
970 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
974 td_io_close_file(td, f);
979 * Get next file to service by doing round robin between all available ones
981 static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
984 unsigned int old_next_file = td->next_file;
990 f = td->files[td->next_file];
993 if (td->next_file >= td->o.nr_files)
996 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
997 if (fio_file_done(f)) {
1002 if (!fio_file_open(f)) {
1005 err = td_io_open_file(td, f);
1007 dprint(FD_FILE, "error %d on open of %s\n",
1015 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
1017 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
1021 td_io_close_file(td, f);
1024 } while (td->next_file != old_next_file);
1026 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
1030 static struct fio_file *__get_next_file(struct thread_data *td)
1034 assert(td->o.nr_files <= td->files_index);
1036 if (td->nr_done_files >= td->o.nr_files) {
1037 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
1038 " nr_files=%d\n", td->nr_open_files,
1044 f = td->file_service_file;
1045 if (f && fio_file_open(f) && !fio_file_closing(f)) {
1046 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
1048 if (td->file_service_left--)
1052 if (td->o.file_service_type == FIO_FSERVICE_RR ||
1053 td->o.file_service_type == FIO_FSERVICE_SEQ)
1054 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
1056 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1058 td->file_service_file = f;
1059 td->file_service_left = td->file_service_nr - 1;
1061 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1065 static struct fio_file *get_next_file(struct thread_data *td)
1067 struct prof_io_ops *ops = &td->prof_io_ops;
1069 if (ops->get_next_file)
1070 return ops->get_next_file(td);
1072 return __get_next_file(td);
1075 static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
1080 f = get_next_file(td);
1087 if (!fill_io_u(td, io_u))
1090 put_file_log(td, f);
1091 td_io_close_file(td, f);
1093 fio_file_set_done(f);
1094 td->nr_done_files++;
1095 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
1096 td->nr_done_files, td->o.nr_files);
1103 struct io_u *__get_io_u(struct thread_data *td)
1105 struct io_u *io_u = NULL;
1110 if (!flist_empty(&td->io_u_requeues))
1111 io_u = flist_entry(td->io_u_requeues.next, struct io_u, list);
1112 else if (!queue_full(td)) {
1113 io_u = flist_entry(td->io_u_freelist.next, struct io_u, list);
1118 io_u->end_io = NULL;
1122 assert(io_u->flags & IO_U_F_FREE);
1123 io_u->flags &= ~(IO_U_F_FREE | IO_U_F_FREE_DEF);
1124 io_u->flags &= ~(IO_U_F_TRIMMED | IO_U_F_BARRIER);
1125 io_u->flags &= ~IO_U_F_VER_LIST;
1128 flist_del(&io_u->list);
1129 flist_add_tail(&io_u->list, &td->io_u_busylist);
1131 io_u->flags |= IO_U_F_IN_CUR_DEPTH;
1132 } else if (td->o.verify_async) {
1134 * We ran out, wait for async verify threads to finish and
1137 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1145 static int check_get_trim(struct thread_data *td, struct io_u *io_u)
1147 if (td->o.trim_backlog && td->trim_entries) {
1150 if (td->trim_batch) {
1153 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1154 td->last_ddir != DDIR_READ) {
1155 td->trim_batch = td->o.trim_batch;
1156 if (!td->trim_batch)
1157 td->trim_batch = td->o.trim_backlog;
1161 if (get_trim && !get_next_trim(td, io_u))
1168 static int check_get_verify(struct thread_data *td, struct io_u *io_u)
1170 if (td->o.verify_backlog && td->io_hist_len) {
1173 if (td->verify_batch)
1175 else if (!(td->io_hist_len % td->o.verify_backlog) &&
1176 td->last_ddir != DDIR_READ) {
1177 td->verify_batch = td->o.verify_batch;
1178 if (!td->verify_batch)
1179 td->verify_batch = td->o.verify_backlog;
1183 if (get_verify && !get_next_verify(td, io_u)) {
1193 * Fill offset and start time into the buffer content, to prevent too
1194 * easy compressible data for simple de-dupe attempts. Do this for every
1195 * 512b block in the range, since that should be the smallest block size
1196 * we can expect from a device.
1198 static void small_content_scramble(struct io_u *io_u)
1200 unsigned int i, nr_blocks = io_u->buflen / 512;
1201 unsigned long long boffset;
1202 unsigned int offset;
1209 boffset = io_u->offset;
1210 io_u->buf_filled_len = 0;
1212 for (i = 0; i < nr_blocks; i++) {
1214 * Fill the byte offset into a "random" start offset of
1215 * the buffer, given by the product of the usec time
1216 * and the actual offset.
1218 offset = (io_u->start_time.tv_usec ^ boffset) & 511;
1219 offset &= ~(sizeof(unsigned long long) - 1);
1220 if (offset >= 512 - sizeof(unsigned long long))
1221 offset -= sizeof(unsigned long long);
1222 memcpy(p + offset, &boffset, sizeof(boffset));
1224 end = p + 512 - sizeof(io_u->start_time);
1225 memcpy(end, &io_u->start_time, sizeof(io_u->start_time));
1232 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1233 * etc. The returned io_u is fully ready to be prepped and submitted.
1235 struct io_u *get_io_u(struct thread_data *td)
1239 int do_scramble = 0;
1241 io_u = __get_io_u(td);
1243 dprint(FD_IO, "__get_io_u failed\n");
1247 if (check_get_verify(td, io_u))
1249 if (check_get_trim(td, io_u))
1253 * from a requeue, io_u already setup
1259 * If using an iolog, grab next piece if any available.
1261 if (td->o.read_iolog_file) {
1262 if (read_iolog_get(td, io_u))
1264 } else if (set_io_u_file(td, io_u)) {
1265 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1270 assert(fio_file_open(f));
1272 if (ddir_rw(io_u->ddir)) {
1273 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
1274 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
1278 f->last_start = io_u->offset;
1279 f->last_pos = io_u->offset + io_u->buflen;
1281 if (io_u->ddir == DDIR_WRITE) {
1282 if (td->o.refill_buffers) {
1283 io_u_fill_buffer(td, io_u,
1284 io_u->xfer_buflen, io_u->xfer_buflen);
1285 } else if (td->o.scramble_buffers)
1287 if (td->o.verify != VERIFY_NONE) {
1288 populate_verify_io_u(td, io_u);
1291 } else if (io_u->ddir == DDIR_READ) {
1293 * Reset the buf_filled parameters so next time if the
1294 * buffer is used for writes it is refilled.
1296 io_u->buf_filled_len = 0;
1301 * Set io data pointers.
1303 io_u->xfer_buf = io_u->buf;
1304 io_u->xfer_buflen = io_u->buflen;
1308 if (!td_io_prep(td, io_u)) {
1309 if (!td->o.disable_slat)
1310 fio_gettime(&io_u->start_time, NULL);
1312 small_content_scramble(io_u);
1316 dprint(FD_IO, "get_io_u failed\n");
1321 void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1323 enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
1324 const char *msg[] = { "read", "write", "sync", "datasync",
1325 "sync_file_range", "wait", "trim" };
1327 if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1330 log_err("fio: io_u error");
1333 log_err(" on file %s", io_u->file->file_name);
1335 log_err(": %s\n", strerror(io_u->error));
1337 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
1338 io_u->offset, io_u->xfer_buflen);
1341 td_verror(td, io_u->error, "io_u error");
1344 static void account_io_completion(struct thread_data *td, struct io_u *io_u,
1345 struct io_completion_data *icd,
1346 const enum fio_ddir idx, unsigned int bytes)
1348 unsigned long uninitialized_var(lusec);
1350 if (!td->o.disable_clat || !td->o.disable_bw)
1351 lusec = utime_since(&io_u->issue_time, &icd->time);
1353 if (!td->o.disable_lat) {
1354 unsigned long tusec;
1356 tusec = utime_since(&io_u->start_time, &icd->time);
1357 add_lat_sample(td, idx, tusec, bytes);
1359 if (td->o.max_latency && tusec > td->o.max_latency) {
1361 log_err("fio: latency of %lu usec exceeds specified max (%u usec)\n", tusec, td->o.max_latency);
1362 td_verror(td, ETIME, "max latency exceeded");
1367 if (!td->o.disable_clat) {
1368 add_clat_sample(td, idx, lusec, bytes);
1369 io_u_mark_latency(td, lusec);
1372 if (!td->o.disable_bw)
1373 add_bw_sample(td, idx, bytes, &icd->time);
1375 add_iops_sample(td, idx, &icd->time);
1378 static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
1380 unsigned long long secs, remainder, bps, bytes;
1381 bytes = td->this_io_bytes[ddir];
1382 bps = td->rate_bps[ddir];
1384 remainder = bytes % bps;
1385 return remainder * 1000000 / bps + secs * 1000000;
1388 static void io_completed(struct thread_data *td, struct io_u *io_u,
1389 struct io_completion_data *icd)
1392 * Older gcc's are too dumb to realize that usec is always used
1393 * initialized, silence that warning.
1395 unsigned long uninitialized_var(usec);
1398 dprint_io_u(io_u, "io complete");
1401 assert(io_u->flags & IO_U_F_FLIGHT);
1402 io_u->flags &= ~(IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
1405 if (ddir_sync(io_u->ddir)) {
1406 td->last_was_sync = 1;
1409 f->first_write = -1ULL;
1410 f->last_write = -1ULL;
1415 td->last_was_sync = 0;
1416 td->last_ddir = io_u->ddir;
1418 if (!io_u->error && ddir_rw(io_u->ddir)) {
1419 unsigned int bytes = io_u->buflen - io_u->resid;
1420 const enum fio_ddir idx = io_u->ddir;
1421 const enum fio_ddir odx = io_u->ddir ^ 1;
1424 td->io_blocks[idx]++;
1425 td->this_io_blocks[idx]++;
1426 td->io_bytes[idx] += bytes;
1428 if (!(io_u->flags & IO_U_F_VER_LIST))
1429 td->this_io_bytes[idx] += bytes;
1431 if (idx == DDIR_WRITE) {
1434 if (f->first_write == -1ULL ||
1435 io_u->offset < f->first_write)
1436 f->first_write = io_u->offset;
1437 if (f->last_write == -1ULL ||
1438 ((io_u->offset + bytes) > f->last_write))
1439 f->last_write = io_u->offset + bytes;
1443 if (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
1444 td->runstate == TD_VERIFYING)) {
1445 account_io_completion(td, io_u, icd, idx, bytes);
1447 if (__should_check_rate(td, idx)) {
1448 td->rate_pending_usleep[idx] =
1449 (usec_for_io(td, idx) -
1450 utime_since_now(&td->start));
1452 if (idx != DDIR_TRIM && __should_check_rate(td, odx))
1453 td->rate_pending_usleep[odx] =
1454 (usec_for_io(td, odx) -
1455 utime_since_now(&td->start));
1458 if (td_write(td) && idx == DDIR_WRITE &&
1460 td->o.verify != VERIFY_NONE)
1461 log_io_piece(td, io_u);
1463 icd->bytes_done[idx] += bytes;
1466 ret = io_u->end_io(td, io_u);
1467 if (ret && !icd->error)
1470 } else if (io_u->error) {
1471 icd->error = io_u->error;
1472 io_u_log_error(td, io_u);
1475 enum error_type_bit eb = td_error_type(io_u->ddir, icd->error);
1476 if (!td_non_fatal_error(td, eb, icd->error))
1479 * If there is a non_fatal error, then add to the error count
1480 * and clear all the errors.
1482 update_error_count(td, icd->error);
1489 static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1493 if (!td->o.disable_clat || !td->o.disable_bw)
1494 fio_gettime(&icd->time, NULL);
1499 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1500 icd->bytes_done[ddir] = 0;
1503 static void ios_completed(struct thread_data *td,
1504 struct io_completion_data *icd)
1509 for (i = 0; i < icd->nr; i++) {
1510 io_u = td->io_ops->event(td, i);
1512 io_completed(td, io_u, icd);
1514 if (!(io_u->flags & IO_U_F_FREE_DEF))
1520 * Complete a single io_u for the sync engines.
1522 int io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
1523 unsigned long *bytes)
1525 struct io_completion_data icd;
1527 init_icd(td, &icd, 1);
1528 io_completed(td, io_u, &icd);
1530 if (!(io_u->flags & IO_U_F_FREE_DEF))
1534 td_verror(td, icd.error, "io_u_sync_complete");
1541 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1542 bytes[ddir] += icd.bytes_done[ddir];
1549 * Called to complete min_events number of io for the async engines.
1551 int io_u_queued_complete(struct thread_data *td, int min_evts,
1552 unsigned long *bytes)
1554 struct io_completion_data icd;
1555 struct timespec *tvp = NULL;
1557 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
1559 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
1564 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete, tvp);
1566 td_verror(td, -ret, "td_io_getevents");
1571 init_icd(td, &icd, ret);
1572 ios_completed(td, &icd);
1574 td_verror(td, icd.error, "io_u_queued_complete");
1581 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1582 bytes[ddir] += icd.bytes_done[ddir];
1589 * Call when io_u is really queued, to update the submission latency.
1591 void io_u_queued(struct thread_data *td, struct io_u *io_u)
1593 if (!td->o.disable_slat) {
1594 unsigned long slat_time;
1596 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
1597 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen);
1602 * "randomly" fill the buffer contents
1604 void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1605 unsigned int min_write, unsigned int max_bs)
1607 io_u->buf_filled_len = 0;
1609 if (!td->o.zero_buffers) {
1610 unsigned int perc = td->o.compress_percentage;
1613 unsigned int seg = min_write;
1615 seg = min(min_write, td->o.compress_chunk);
1616 fill_random_buf_percentage(&td->buf_state, io_u->buf,
1619 fill_random_buf(&td->buf_state, io_u->buf, max_bs);
1621 memset(io_u->buf, 0, max_bs);