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, struct fio_file *f,
238 enum fio_ddir ddir, unsigned long long *b)
240 *b = zipf_next(&td->zipf);
244 static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
245 enum fio_ddir ddir, unsigned long long *b)
247 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
248 return __get_next_rand_offset(td, f, ddir, b);
249 else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
250 return __get_next_rand_offset_zipf(td, f, ddir, b);
252 log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
256 static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
257 enum fio_ddir ddir, unsigned long long *b)
259 if (!get_next_rand_offset(td, f, ddir, b))
262 if (td->o.time_based) {
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, f->last_pos, f->real_file_size);
273 static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
274 enum fio_ddir ddir, unsigned long long *offset)
276 assert(ddir_rw(ddir));
278 if (f->last_pos >= f->io_size + get_start_offset(td) && td->o.time_based)
279 f->last_pos = f->last_pos - f->io_size;
281 if (f->last_pos < f->real_file_size) {
282 unsigned long long pos;
284 if (f->last_pos == f->file_offset && td->o.ddir_seq_add < 0)
285 f->last_pos = f->real_file_size;
287 pos = f->last_pos - f->file_offset;
289 pos += td->o.ddir_seq_add;
298 static int get_next_block(struct thread_data *td, struct io_u *io_u,
299 enum fio_ddir ddir, int rw_seq)
301 struct fio_file *f = io_u->file;
302 unsigned long long b, offset;
305 assert(ddir_rw(ddir));
311 ret = get_next_rand_block(td, f, ddir, &b);
313 ret = get_next_seq_offset(td, f, ddir, &offset);
315 io_u->flags |= IO_U_F_BUSY_OK;
317 if (td->o.rw_seq == RW_SEQ_SEQ) {
318 ret = get_next_seq_offset(td, f, ddir, &offset);
320 ret = get_next_rand_block(td, f, ddir, &b);
321 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
322 if (f->last_start != -1ULL)
323 offset = f->last_start - f->file_offset;
328 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
335 io_u->offset = offset;
337 io_u->offset = b * td->o.ba[ddir];
339 log_err("fio: bug in offset generation\n");
348 * For random io, generate a random new block and see if it's used. Repeat
349 * until we find a free one. For sequential io, just return the end of
350 * the last io issued.
352 static int __get_next_offset(struct thread_data *td, struct io_u *io_u)
354 struct fio_file *f = io_u->file;
355 enum fio_ddir ddir = io_u->ddir;
358 assert(ddir_rw(ddir));
360 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
362 td->ddir_seq_nr = td->o.ddir_seq_nr;
365 if (get_next_block(td, io_u, ddir, rw_seq_hit))
368 if (io_u->offset >= f->io_size) {
369 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
370 io_u->offset, f->io_size);
374 io_u->offset += f->file_offset;
375 if (io_u->offset >= f->real_file_size) {
376 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
377 io_u->offset, f->real_file_size);
384 static int get_next_offset(struct thread_data *td, struct io_u *io_u)
386 struct prof_io_ops *ops = &td->prof_io_ops;
388 if (ops->fill_io_u_off)
389 return ops->fill_io_u_off(td, io_u);
391 return __get_next_offset(td, io_u);
394 static inline int io_u_fits(struct thread_data *td, struct io_u *io_u,
397 struct fio_file *f = io_u->file;
399 return io_u->offset + buflen <= f->io_size + get_start_offset(td);
402 static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u)
404 const int ddir = io_u->ddir;
405 unsigned int uninitialized_var(buflen);
406 unsigned int minbs, maxbs;
407 unsigned long r, rand_max;
409 assert(ddir_rw(ddir));
411 minbs = td->o.min_bs[ddir];
412 maxbs = td->o.max_bs[ddir];
418 * If we can't satisfy the min block size from here, then fail
420 if (!io_u_fits(td, io_u, minbs))
423 if (td->o.use_os_rand)
424 rand_max = OS_RAND_MAX;
426 rand_max = FRAND_MAX;
429 if (td->o.use_os_rand)
430 r = os_random_long(&td->bsrange_state);
432 r = __rand(&td->__bsrange_state);
434 if (!td->o.bssplit_nr[ddir]) {
435 buflen = 1 + (unsigned int) ((double) maxbs *
436 (r / (rand_max + 1.0)));
443 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
444 struct bssplit *bsp = &td->o.bssplit[ddir][i];
448 if ((r <= ((rand_max / 100L) * perc)) &&
449 io_u_fits(td, io_u, buflen))
454 if (!td->o.bs_unaligned && is_power_of_2(minbs))
455 buflen = (buflen + minbs - 1) & ~(minbs - 1);
457 } while (!io_u_fits(td, io_u, buflen));
462 static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
464 struct prof_io_ops *ops = &td->prof_io_ops;
466 if (ops->fill_io_u_size)
467 return ops->fill_io_u_size(td, io_u);
469 return __get_next_buflen(td, io_u);
472 static void set_rwmix_bytes(struct thread_data *td)
477 * we do time or byte based switch. this is needed because
478 * buffered writes may issue a lot quicker than they complete,
479 * whereas reads do not.
481 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
482 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
485 static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
490 if (td->o.use_os_rand) {
491 r = os_random_long(&td->rwmix_state);
492 v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
494 r = __rand(&td->__rwmix_state);
495 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
498 if (v <= td->o.rwmix[DDIR_READ])
504 static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
506 enum fio_ddir odir = ddir ^ 1;
510 assert(ddir_rw(ddir));
512 if (td->rate_pending_usleep[ddir] <= 0)
516 * We have too much pending sleep in this direction. See if we
521 * Other direction does not have too much pending, switch
523 if (td->rate_pending_usleep[odir] < 100000)
527 * Both directions have pending sleep. Sleep the minimum time
528 * and deduct from both.
530 if (td->rate_pending_usleep[ddir] <=
531 td->rate_pending_usleep[odir]) {
532 usec = td->rate_pending_usleep[ddir];
534 usec = td->rate_pending_usleep[odir];
538 usec = td->rate_pending_usleep[ddir];
541 * We are going to sleep, ensure that we flush anything pending as
542 * not to skew our latency numbers.
544 * Changed to only monitor 'in flight' requests here instead of the
545 * td->cur_depth, b/c td->cur_depth does not accurately represent
546 * io's that have been actually submitted to an async engine,
547 * and cur_depth is meaningless for sync engines.
549 if (td->io_u_in_flight) {
552 ret = io_u_queued_complete(td, td->io_u_in_flight, NULL);
555 fio_gettime(&t, NULL);
556 usec_sleep(td, usec);
557 usec = utime_since_now(&t);
559 td->rate_pending_usleep[ddir] -= usec;
562 if (td_rw(td) && __should_check_rate(td, odir))
563 td->rate_pending_usleep[odir] -= usec;
571 * Return the data direction for the next io_u. If the job is a
572 * mixed read/write workload, check the rwmix cycle and switch if
575 static enum fio_ddir get_rw_ddir(struct thread_data *td)
580 * see if it's time to fsync
582 if (td->o.fsync_blocks &&
583 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
584 td->io_issues[DDIR_WRITE] && should_fsync(td))
588 * see if it's time to fdatasync
590 if (td->o.fdatasync_blocks &&
591 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
592 td->io_issues[DDIR_WRITE] && should_fsync(td))
593 return DDIR_DATASYNC;
596 * see if it's time to sync_file_range
598 if (td->sync_file_range_nr &&
599 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
600 td->io_issues[DDIR_WRITE] && should_fsync(td))
601 return DDIR_SYNC_FILE_RANGE;
605 * Check if it's time to seed a new data direction.
607 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
609 * Put a top limit on how many bytes we do for
610 * one data direction, to avoid overflowing the
613 ddir = get_rand_ddir(td);
615 if (ddir != td->rwmix_ddir)
618 td->rwmix_ddir = ddir;
620 ddir = td->rwmix_ddir;
621 } else if (td_read(td))
623 else if (td_write(td))
628 td->rwmix_ddir = rate_ddir(td, ddir);
629 return td->rwmix_ddir;
632 static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
634 io_u->ddir = get_rw_ddir(td);
636 if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
637 td->o.barrier_blocks &&
638 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
639 td->io_issues[DDIR_WRITE])
640 io_u->flags |= IO_U_F_BARRIER;
643 void put_file_log(struct thread_data *td, struct fio_file *f)
645 int ret = put_file(td, f);
648 td_verror(td, ret, "file close");
651 void put_io_u(struct thread_data *td, struct io_u *io_u)
655 if (io_u->file && !(io_u->flags & IO_U_F_FREE_DEF))
656 put_file_log(td, io_u->file);
658 io_u->flags &= ~IO_U_F_FREE_DEF;
659 io_u->flags |= IO_U_F_FREE;
661 if (io_u->flags & IO_U_F_IN_CUR_DEPTH)
663 flist_del_init(&io_u->list);
664 flist_add(&io_u->list, &td->io_u_freelist);
666 td_io_u_free_notify(td);
669 void clear_io_u(struct thread_data *td, struct io_u *io_u)
671 io_u->flags &= ~IO_U_F_FLIGHT;
675 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
677 struct io_u *__io_u = *io_u;
679 dprint(FD_IO, "requeue %p\n", __io_u);
683 __io_u->flags |= IO_U_F_FREE;
684 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(__io_u->ddir))
685 td->io_issues[__io_u->ddir]--;
687 __io_u->flags &= ~IO_U_F_FLIGHT;
688 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH)
690 flist_del(&__io_u->list);
691 flist_add_tail(&__io_u->list, &td->io_u_requeues);
696 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
698 if (td->io_ops->flags & FIO_NOIO)
701 set_rw_ddir(td, io_u);
704 * fsync() or fdatasync() or trim etc, we are done
706 if (!ddir_rw(io_u->ddir))
710 * See if it's time to switch to a new zone
712 if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
714 io_u->file->file_offset += td->o.zone_range + td->o.zone_skip;
715 io_u->file->last_pos = io_u->file->file_offset;
716 td->io_skip_bytes += td->o.zone_skip;
720 * No log, let the seq/rand engine retrieve the next buflen and
723 if (get_next_offset(td, io_u)) {
724 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
728 io_u->buflen = get_next_buflen(td, io_u);
730 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
734 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
735 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
736 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
737 io_u->buflen, io_u->file->real_file_size);
742 * mark entry before potentially trimming io_u
744 if (td_random(td) && file_randommap(td, io_u->file))
745 mark_random_map(td, io_u);
748 * If using a write iolog, store this entry.
751 dprint_io_u(io_u, "fill_io_u");
752 td->zone_bytes += io_u->buflen;
757 static void __io_u_mark_map(unsigned int *map, unsigned int nr)
786 void io_u_mark_submit(struct thread_data *td, unsigned int nr)
788 __io_u_mark_map(td->ts.io_u_submit, nr);
789 td->ts.total_submit++;
792 void io_u_mark_complete(struct thread_data *td, unsigned int nr)
794 __io_u_mark_map(td->ts.io_u_complete, nr);
795 td->ts.total_complete++;
798 void io_u_mark_depth(struct thread_data *td, unsigned int nr)
802 switch (td->cur_depth) {
824 td->ts.io_u_map[idx] += nr;
827 static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
864 assert(idx < FIO_IO_U_LAT_U_NR);
865 td->ts.io_u_lat_u[idx]++;
868 static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
909 assert(idx < FIO_IO_U_LAT_M_NR);
910 td->ts.io_u_lat_m[idx]++;
913 static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
916 io_u_mark_lat_usec(td, usec);
918 io_u_mark_lat_msec(td, usec / 1000);
922 * Get next file to service by choosing one at random
924 static struct fio_file *get_next_file_rand(struct thread_data *td,
925 enum fio_file_flags goodf,
926 enum fio_file_flags badf)
935 if (td->o.use_os_rand) {
936 r = os_random_long(&td->next_file_state);
937 fno = (unsigned int) ((double) td->o.nr_files
938 * (r / (OS_RAND_MAX + 1.0)));
940 r = __rand(&td->__next_file_state);
941 fno = (unsigned int) ((double) td->o.nr_files
942 * (r / (FRAND_MAX + 1.0)));
946 if (fio_file_done(f))
949 if (!fio_file_open(f)) {
952 err = td_io_open_file(td, f);
958 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
959 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
963 td_io_close_file(td, f);
968 * Get next file to service by doing round robin between all available ones
970 static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
973 unsigned int old_next_file = td->next_file;
979 f = td->files[td->next_file];
982 if (td->next_file >= td->o.nr_files)
985 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
986 if (fio_file_done(f)) {
991 if (!fio_file_open(f)) {
994 err = td_io_open_file(td, f);
996 dprint(FD_FILE, "error %d on open of %s\n",
1004 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
1006 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
1010 td_io_close_file(td, f);
1013 } while (td->next_file != old_next_file);
1015 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
1019 static struct fio_file *__get_next_file(struct thread_data *td)
1023 assert(td->o.nr_files <= td->files_index);
1025 if (td->nr_done_files >= td->o.nr_files) {
1026 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
1027 " nr_files=%d\n", td->nr_open_files,
1033 f = td->file_service_file;
1034 if (f && fio_file_open(f) && !fio_file_closing(f)) {
1035 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
1037 if (td->file_service_left--)
1041 if (td->o.file_service_type == FIO_FSERVICE_RR ||
1042 td->o.file_service_type == FIO_FSERVICE_SEQ)
1043 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
1045 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1047 td->file_service_file = f;
1048 td->file_service_left = td->file_service_nr - 1;
1050 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1054 static struct fio_file *get_next_file(struct thread_data *td)
1056 struct prof_io_ops *ops = &td->prof_io_ops;
1058 if (ops->get_next_file)
1059 return ops->get_next_file(td);
1061 return __get_next_file(td);
1064 static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
1069 f = get_next_file(td);
1076 if (!fill_io_u(td, io_u))
1079 put_file_log(td, f);
1080 td_io_close_file(td, f);
1082 fio_file_set_done(f);
1083 td->nr_done_files++;
1084 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
1085 td->nr_done_files, td->o.nr_files);
1092 struct io_u *__get_io_u(struct thread_data *td)
1094 struct io_u *io_u = NULL;
1099 if (!flist_empty(&td->io_u_requeues))
1100 io_u = flist_entry(td->io_u_requeues.next, struct io_u, list);
1101 else if (!queue_full(td)) {
1102 io_u = flist_entry(td->io_u_freelist.next, struct io_u, list);
1107 io_u->end_io = NULL;
1111 assert(io_u->flags & IO_U_F_FREE);
1112 io_u->flags &= ~(IO_U_F_FREE | IO_U_F_FREE_DEF);
1113 io_u->flags &= ~(IO_U_F_TRIMMED | IO_U_F_BARRIER);
1114 io_u->flags &= ~IO_U_F_VER_LIST;
1117 flist_del(&io_u->list);
1118 flist_add_tail(&io_u->list, &td->io_u_busylist);
1120 io_u->flags |= IO_U_F_IN_CUR_DEPTH;
1121 } else if (td->o.verify_async) {
1123 * We ran out, wait for async verify threads to finish and
1126 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1134 static int check_get_trim(struct thread_data *td, struct io_u *io_u)
1136 if (td->o.trim_backlog && td->trim_entries) {
1139 if (td->trim_batch) {
1142 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1143 td->last_ddir != DDIR_READ) {
1144 td->trim_batch = td->o.trim_batch;
1145 if (!td->trim_batch)
1146 td->trim_batch = td->o.trim_backlog;
1150 if (get_trim && !get_next_trim(td, io_u))
1157 static int check_get_verify(struct thread_data *td, struct io_u *io_u)
1159 if (td->o.verify_backlog && td->io_hist_len) {
1162 if (td->verify_batch)
1164 else if (!(td->io_hist_len % td->o.verify_backlog) &&
1165 td->last_ddir != DDIR_READ) {
1166 td->verify_batch = td->o.verify_batch;
1167 if (!td->verify_batch)
1168 td->verify_batch = td->o.verify_backlog;
1172 if (get_verify && !get_next_verify(td, io_u)) {
1182 * Fill offset and start time into the buffer content, to prevent too
1183 * easy compressible data for simple de-dupe attempts. Do this for every
1184 * 512b block in the range, since that should be the smallest block size
1185 * we can expect from a device.
1187 static void small_content_scramble(struct io_u *io_u)
1189 unsigned int i, nr_blocks = io_u->buflen / 512;
1190 unsigned long long boffset;
1191 unsigned int offset;
1198 boffset = io_u->offset;
1199 io_u->buf_filled_len = 0;
1201 for (i = 0; i < nr_blocks; i++) {
1203 * Fill the byte offset into a "random" start offset of
1204 * the buffer, given by the product of the usec time
1205 * and the actual offset.
1207 offset = (io_u->start_time.tv_usec ^ boffset) & 511;
1208 offset &= ~(sizeof(unsigned long long) - 1);
1209 if (offset >= 512 - sizeof(unsigned long long))
1210 offset -= sizeof(unsigned long long);
1211 memcpy(p + offset, &boffset, sizeof(boffset));
1213 end = p + 512 - sizeof(io_u->start_time);
1214 memcpy(end, &io_u->start_time, sizeof(io_u->start_time));
1221 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1222 * etc. The returned io_u is fully ready to be prepped and submitted.
1224 struct io_u *get_io_u(struct thread_data *td)
1228 int do_scramble = 0;
1230 io_u = __get_io_u(td);
1232 dprint(FD_IO, "__get_io_u failed\n");
1236 if (check_get_verify(td, io_u))
1238 if (check_get_trim(td, io_u))
1242 * from a requeue, io_u already setup
1248 * If using an iolog, grab next piece if any available.
1250 if (td->o.read_iolog_file) {
1251 if (read_iolog_get(td, io_u))
1253 } else if (set_io_u_file(td, io_u)) {
1254 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1259 assert(fio_file_open(f));
1261 if (ddir_rw(io_u->ddir)) {
1262 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
1263 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
1267 f->last_start = io_u->offset;
1268 f->last_pos = io_u->offset + io_u->buflen;
1270 if (io_u->ddir == DDIR_WRITE) {
1271 if (td->o.refill_buffers) {
1272 io_u_fill_buffer(td, io_u,
1273 io_u->xfer_buflen, io_u->xfer_buflen);
1274 } else if (td->o.scramble_buffers)
1276 if (td->o.verify != VERIFY_NONE) {
1277 populate_verify_io_u(td, io_u);
1280 } else if (io_u->ddir == DDIR_READ) {
1282 * Reset the buf_filled parameters so next time if the
1283 * buffer is used for writes it is refilled.
1285 io_u->buf_filled_len = 0;
1290 * Set io data pointers.
1292 io_u->xfer_buf = io_u->buf;
1293 io_u->xfer_buflen = io_u->buflen;
1297 if (!td_io_prep(td, io_u)) {
1298 if (!td->o.disable_slat)
1299 fio_gettime(&io_u->start_time, NULL);
1301 small_content_scramble(io_u);
1305 dprint(FD_IO, "get_io_u failed\n");
1310 void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1312 enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
1313 const char *msg[] = { "read", "write", "sync", "datasync",
1314 "sync_file_range", "wait", "trim" };
1316 if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1319 log_err("fio: io_u error");
1322 log_err(" on file %s", io_u->file->file_name);
1324 log_err(": %s\n", strerror(io_u->error));
1326 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
1327 io_u->offset, io_u->xfer_buflen);
1330 td_verror(td, io_u->error, "io_u error");
1333 static void account_io_completion(struct thread_data *td, struct io_u *io_u,
1334 struct io_completion_data *icd,
1335 const enum fio_ddir idx, unsigned int bytes)
1337 unsigned long uninitialized_var(lusec);
1339 if (!td->o.disable_clat || !td->o.disable_bw)
1340 lusec = utime_since(&io_u->issue_time, &icd->time);
1342 if (!td->o.disable_lat) {
1343 unsigned long tusec;
1345 tusec = utime_since(&io_u->start_time, &icd->time);
1346 add_lat_sample(td, idx, tusec, bytes);
1348 if (td->o.max_latency && tusec > td->o.max_latency) {
1350 log_err("fio: latency of %lu usec exceeds specified max (%u usec)\n", tusec, td->o.max_latency);
1351 td_verror(td, ETIME, "max latency exceeded");
1356 if (!td->o.disable_clat) {
1357 add_clat_sample(td, idx, lusec, bytes);
1358 io_u_mark_latency(td, lusec);
1361 if (!td->o.disable_bw)
1362 add_bw_sample(td, idx, bytes, &icd->time);
1364 add_iops_sample(td, idx, &icd->time);
1367 static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
1369 unsigned long long secs, remainder, bps, bytes;
1370 bytes = td->this_io_bytes[ddir];
1371 bps = td->rate_bps[ddir];
1373 remainder = bytes % bps;
1374 return remainder * 1000000 / bps + secs * 1000000;
1377 static void io_completed(struct thread_data *td, struct io_u *io_u,
1378 struct io_completion_data *icd)
1381 * Older gcc's are too dumb to realize that usec is always used
1382 * initialized, silence that warning.
1384 unsigned long uninitialized_var(usec);
1387 dprint_io_u(io_u, "io complete");
1390 assert(io_u->flags & IO_U_F_FLIGHT);
1391 io_u->flags &= ~(IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
1394 if (ddir_sync(io_u->ddir)) {
1395 td->last_was_sync = 1;
1398 f->first_write = -1ULL;
1399 f->last_write = -1ULL;
1404 td->last_was_sync = 0;
1405 td->last_ddir = io_u->ddir;
1407 if (!io_u->error && ddir_rw(io_u->ddir)) {
1408 unsigned int bytes = io_u->buflen - io_u->resid;
1409 const enum fio_ddir idx = io_u->ddir;
1410 const enum fio_ddir odx = io_u->ddir ^ 1;
1413 td->io_blocks[idx]++;
1414 td->this_io_blocks[idx]++;
1415 td->io_bytes[idx] += bytes;
1417 if (!(io_u->flags & IO_U_F_VER_LIST))
1418 td->this_io_bytes[idx] += bytes;
1420 if (idx == DDIR_WRITE) {
1423 if (f->first_write == -1ULL ||
1424 io_u->offset < f->first_write)
1425 f->first_write = io_u->offset;
1426 if (f->last_write == -1ULL ||
1427 ((io_u->offset + bytes) > f->last_write))
1428 f->last_write = io_u->offset + bytes;
1432 if (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
1433 td->runstate == TD_VERIFYING)) {
1434 account_io_completion(td, io_u, icd, idx, bytes);
1436 if (__should_check_rate(td, idx)) {
1437 td->rate_pending_usleep[idx] =
1438 (usec_for_io(td, idx) -
1439 utime_since_now(&td->start));
1441 if (idx != DDIR_TRIM && __should_check_rate(td, odx))
1442 td->rate_pending_usleep[odx] =
1443 (usec_for_io(td, odx) -
1444 utime_since_now(&td->start));
1447 if (td_write(td) && idx == DDIR_WRITE &&
1449 td->o.verify != VERIFY_NONE)
1450 log_io_piece(td, io_u);
1452 icd->bytes_done[idx] += bytes;
1455 ret = io_u->end_io(td, io_u);
1456 if (ret && !icd->error)
1459 } else if (io_u->error) {
1460 icd->error = io_u->error;
1461 io_u_log_error(td, io_u);
1464 enum error_type_bit eb = td_error_type(io_u->ddir, icd->error);
1465 if (!td_non_fatal_error(td, eb, icd->error))
1468 * If there is a non_fatal error, then add to the error count
1469 * and clear all the errors.
1471 update_error_count(td, icd->error);
1478 static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1482 if (!td->o.disable_clat || !td->o.disable_bw)
1483 fio_gettime(&icd->time, NULL);
1488 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1489 icd->bytes_done[ddir] = 0;
1492 static void ios_completed(struct thread_data *td,
1493 struct io_completion_data *icd)
1498 for (i = 0; i < icd->nr; i++) {
1499 io_u = td->io_ops->event(td, i);
1501 io_completed(td, io_u, icd);
1503 if (!(io_u->flags & IO_U_F_FREE_DEF))
1509 * Complete a single io_u for the sync engines.
1511 int io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
1512 unsigned long *bytes)
1514 struct io_completion_data icd;
1516 init_icd(td, &icd, 1);
1517 io_completed(td, io_u, &icd);
1519 if (!(io_u->flags & IO_U_F_FREE_DEF))
1523 td_verror(td, icd.error, "io_u_sync_complete");
1530 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1531 bytes[ddir] += icd.bytes_done[ddir];
1538 * Called to complete min_events number of io for the async engines.
1540 int io_u_queued_complete(struct thread_data *td, int min_evts,
1541 unsigned long *bytes)
1543 struct io_completion_data icd;
1544 struct timespec *tvp = NULL;
1546 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
1548 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
1553 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete, tvp);
1555 td_verror(td, -ret, "td_io_getevents");
1560 init_icd(td, &icd, ret);
1561 ios_completed(td, &icd);
1563 td_verror(td, icd.error, "io_u_queued_complete");
1570 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1571 bytes[ddir] += icd.bytes_done[ddir];
1578 * Call when io_u is really queued, to update the submission latency.
1580 void io_u_queued(struct thread_data *td, struct io_u *io_u)
1582 if (!td->o.disable_slat) {
1583 unsigned long slat_time;
1585 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
1586 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen);
1591 * "randomly" fill the buffer contents
1593 void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1594 unsigned int min_write, unsigned int max_bs)
1596 io_u->buf_filled_len = 0;
1598 if (!td->o.zero_buffers) {
1599 unsigned int perc = td->o.compress_percentage;
1602 unsigned int seg = min_write;
1604 seg = min(min_write, td->o.compress_chunk);
1605 fill_random_buf_percentage(&td->buf_state, io_u->buf,
1608 fill_random_buf(&td->buf_state, io_u->buf, max_bs);
1610 memset(io_u->buf, 0, max_bs);