14 struct io_completion_data {
17 int error; /* output */
18 unsigned long bytes_done[2]; /* 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_block(struct thread_data *td, struct fio_file *f,
238 enum fio_ddir ddir, unsigned long long *b)
240 if (!get_next_rand_offset(td, f, ddir, b))
243 if (td->o.time_based) {
245 if (!get_next_rand_offset(td, f, ddir, b))
249 dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
250 f->file_name, f->last_pos, f->real_file_size);
254 static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
255 enum fio_ddir ddir, unsigned long long *offset)
257 assert(ddir_rw(ddir));
259 if (f->last_pos >= f->io_size && td->o.time_based)
260 f->last_pos = f->last_pos - f->io_size;
262 if (f->last_pos < f->real_file_size) {
263 unsigned long long pos;
265 if (f->last_pos == f->file_offset && td->o.ddir_seq_add < 0)
266 f->last_pos = f->real_file_size;
268 pos = f->last_pos - f->file_offset;
270 pos += td->o.ddir_seq_add;
279 static int get_next_block(struct thread_data *td, struct io_u *io_u,
280 enum fio_ddir ddir, int rw_seq)
282 struct fio_file *f = io_u->file;
283 unsigned long long b, offset;
286 assert(ddir_rw(ddir));
292 ret = get_next_rand_block(td, f, ddir, &b);
294 ret = get_next_seq_offset(td, f, ddir, &offset);
296 io_u->flags |= IO_U_F_BUSY_OK;
298 if (td->o.rw_seq == RW_SEQ_SEQ) {
299 ret = get_next_seq_offset(td, f, ddir, &offset);
301 ret = get_next_rand_block(td, f, ddir, &b);
302 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
303 if (f->last_start != -1ULL)
304 offset = f->last_start - f->file_offset;
309 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
316 io_u->offset = offset;
318 io_u->offset = b * td->o.ba[ddir];
320 log_err("fio: bug in offset generation\n");
329 * For random io, generate a random new block and see if it's used. Repeat
330 * until we find a free one. For sequential io, just return the end of
331 * the last io issued.
333 static int __get_next_offset(struct thread_data *td, struct io_u *io_u)
335 struct fio_file *f = io_u->file;
336 enum fio_ddir ddir = io_u->ddir;
339 assert(ddir_rw(ddir));
341 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
343 td->ddir_seq_nr = td->o.ddir_seq_nr;
346 if (get_next_block(td, io_u, ddir, rw_seq_hit))
349 if (io_u->offset >= f->io_size) {
350 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
351 io_u->offset, f->io_size);
355 io_u->offset += f->file_offset;
356 if (io_u->offset >= f->real_file_size) {
357 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
358 io_u->offset, f->real_file_size);
365 static int get_next_offset(struct thread_data *td, struct io_u *io_u)
367 struct prof_io_ops *ops = &td->prof_io_ops;
369 if (ops->fill_io_u_off)
370 return ops->fill_io_u_off(td, io_u);
372 return __get_next_offset(td, io_u);
375 static inline int io_u_fits(struct thread_data *td, struct io_u *io_u,
378 struct fio_file *f = io_u->file;
380 return io_u->offset + buflen <= f->io_size + td->o.start_offset;
383 static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u)
385 const int ddir = io_u->ddir;
386 unsigned int uninitialized_var(buflen);
387 unsigned int minbs, maxbs;
388 unsigned long r, rand_max;
390 assert(ddir_rw(ddir));
392 minbs = td->o.min_bs[ddir];
393 maxbs = td->o.max_bs[ddir];
399 * If we can't satisfy the min block size from here, then fail
401 if (!io_u_fits(td, io_u, minbs))
404 if (td->o.use_os_rand)
405 rand_max = OS_RAND_MAX;
407 rand_max = FRAND_MAX;
410 if (td->o.use_os_rand)
411 r = os_random_long(&td->bsrange_state);
413 r = __rand(&td->__bsrange_state);
415 if (!td->o.bssplit_nr[ddir]) {
416 buflen = 1 + (unsigned int) ((double) maxbs *
417 (r / (rand_max + 1.0)));
424 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
425 struct bssplit *bsp = &td->o.bssplit[ddir][i];
429 if ((r <= ((rand_max / 100L) * perc)) &&
430 io_u_fits(td, io_u, buflen))
435 if (!td->o.bs_unaligned && is_power_of_2(minbs))
436 buflen = (buflen + minbs - 1) & ~(minbs - 1);
438 } while (!io_u_fits(td, io_u, buflen));
443 static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
445 struct prof_io_ops *ops = &td->prof_io_ops;
447 if (ops->fill_io_u_size)
448 return ops->fill_io_u_size(td, io_u);
450 return __get_next_buflen(td, io_u);
453 static void set_rwmix_bytes(struct thread_data *td)
458 * we do time or byte based switch. this is needed because
459 * buffered writes may issue a lot quicker than they complete,
460 * whereas reads do not.
462 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
463 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
466 static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
471 if (td->o.use_os_rand) {
472 r = os_random_long(&td->rwmix_state);
473 v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
475 r = __rand(&td->__rwmix_state);
476 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
479 if (v <= td->o.rwmix[DDIR_READ])
485 static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
487 enum fio_ddir odir = ddir ^ 1;
491 assert(ddir_rw(ddir));
493 if (td->rate_pending_usleep[ddir] <= 0)
497 * We have too much pending sleep in this direction. See if we
502 * Other direction does not have too much pending, switch
504 if (td->rate_pending_usleep[odir] < 100000)
508 * Both directions have pending sleep. Sleep the minimum time
509 * and deduct from both.
511 if (td->rate_pending_usleep[ddir] <=
512 td->rate_pending_usleep[odir]) {
513 usec = td->rate_pending_usleep[ddir];
515 usec = td->rate_pending_usleep[odir];
519 usec = td->rate_pending_usleep[ddir];
522 * We are going to sleep, ensure that we flush anything pending as
523 * not to skew our latency numbers.
525 * Changed to only monitor 'in flight' requests here instead of the
526 * td->cur_depth, b/c td->cur_depth does not accurately represent
527 * io's that have been actually submitted to an async engine,
528 * and cur_depth is meaningless for sync engines.
530 if (td->io_u_in_flight) {
533 ret = io_u_queued_complete(td, td->io_u_in_flight, NULL);
536 fio_gettime(&t, NULL);
537 usec_sleep(td, usec);
538 usec = utime_since_now(&t);
540 td->rate_pending_usleep[ddir] -= usec;
543 if (td_rw(td) && __should_check_rate(td, odir))
544 td->rate_pending_usleep[odir] -= usec;
550 * Return the data direction for the next io_u. If the job is a
551 * mixed read/write workload, check the rwmix cycle and switch if
554 static enum fio_ddir get_rw_ddir(struct thread_data *td)
559 * see if it's time to fsync
561 if (td->o.fsync_blocks &&
562 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
563 td->io_issues[DDIR_WRITE] && should_fsync(td))
567 * see if it's time to fdatasync
569 if (td->o.fdatasync_blocks &&
570 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
571 td->io_issues[DDIR_WRITE] && should_fsync(td))
572 return DDIR_DATASYNC;
575 * see if it's time to sync_file_range
577 if (td->sync_file_range_nr &&
578 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
579 td->io_issues[DDIR_WRITE] && should_fsync(td))
580 return DDIR_SYNC_FILE_RANGE;
584 * Check if it's time to seed a new data direction.
586 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
588 * Put a top limit on how many bytes we do for
589 * one data direction, to avoid overflowing the
592 ddir = get_rand_ddir(td);
594 if (ddir != td->rwmix_ddir)
597 td->rwmix_ddir = ddir;
599 ddir = td->rwmix_ddir;
600 } else if (td_read(td))
605 td->rwmix_ddir = rate_ddir(td, ddir);
606 return td->rwmix_ddir;
609 static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
611 io_u->ddir = get_rw_ddir(td);
613 if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
614 td->o.barrier_blocks &&
615 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
616 td->io_issues[DDIR_WRITE])
617 io_u->flags |= IO_U_F_BARRIER;
620 void put_file_log(struct thread_data *td, struct fio_file *f)
622 int ret = put_file(td, f);
625 td_verror(td, ret, "file close");
628 void put_io_u(struct thread_data *td, struct io_u *io_u)
632 if (io_u->file && !(io_u->flags & IO_U_F_FREE_DEF))
633 put_file_log(td, io_u->file);
635 io_u->flags &= ~IO_U_F_FREE_DEF;
636 io_u->flags |= IO_U_F_FREE;
638 if (io_u->flags & IO_U_F_IN_CUR_DEPTH)
640 flist_del_init(&io_u->list);
641 flist_add(&io_u->list, &td->io_u_freelist);
643 td_io_u_free_notify(td);
646 void clear_io_u(struct thread_data *td, struct io_u *io_u)
648 io_u->flags &= ~IO_U_F_FLIGHT;
652 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
654 struct io_u *__io_u = *io_u;
656 dprint(FD_IO, "requeue %p\n", __io_u);
660 __io_u->flags |= IO_U_F_FREE;
661 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(__io_u->ddir))
662 td->io_issues[__io_u->ddir]--;
664 __io_u->flags &= ~IO_U_F_FLIGHT;
665 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH)
667 flist_del(&__io_u->list);
668 flist_add_tail(&__io_u->list, &td->io_u_requeues);
673 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
675 if (td->io_ops->flags & FIO_NOIO)
678 set_rw_ddir(td, io_u);
681 * fsync() or fdatasync() or trim etc, we are done
683 if (!ddir_rw(io_u->ddir))
687 * See if it's time to switch to a new zone
689 if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
691 io_u->file->file_offset += td->o.zone_range + td->o.zone_skip;
692 io_u->file->last_pos = io_u->file->file_offset;
693 td->io_skip_bytes += td->o.zone_skip;
697 * No log, let the seq/rand engine retrieve the next buflen and
700 if (get_next_offset(td, io_u)) {
701 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
705 io_u->buflen = get_next_buflen(td, io_u);
707 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
711 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
712 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
713 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
714 io_u->buflen, io_u->file->real_file_size);
719 * mark entry before potentially trimming io_u
721 if (td_random(td) && file_randommap(td, io_u->file))
722 mark_random_map(td, io_u);
725 * If using a write iolog, store this entry.
728 dprint_io_u(io_u, "fill_io_u");
729 td->zone_bytes += io_u->buflen;
734 static void __io_u_mark_map(unsigned int *map, unsigned int nr)
763 void io_u_mark_submit(struct thread_data *td, unsigned int nr)
765 __io_u_mark_map(td->ts.io_u_submit, nr);
766 td->ts.total_submit++;
769 void io_u_mark_complete(struct thread_data *td, unsigned int nr)
771 __io_u_mark_map(td->ts.io_u_complete, nr);
772 td->ts.total_complete++;
775 void io_u_mark_depth(struct thread_data *td, unsigned int nr)
779 switch (td->cur_depth) {
801 td->ts.io_u_map[idx] += nr;
804 static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
841 assert(idx < FIO_IO_U_LAT_U_NR);
842 td->ts.io_u_lat_u[idx]++;
845 static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
886 assert(idx < FIO_IO_U_LAT_M_NR);
887 td->ts.io_u_lat_m[idx]++;
890 static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
893 io_u_mark_lat_usec(td, usec);
895 io_u_mark_lat_msec(td, usec / 1000);
899 * Get next file to service by choosing one at random
901 static struct fio_file *get_next_file_rand(struct thread_data *td,
902 enum fio_file_flags goodf,
903 enum fio_file_flags badf)
912 if (td->o.use_os_rand) {
913 r = os_random_long(&td->next_file_state);
914 fno = (unsigned int) ((double) td->o.nr_files
915 * (r / (OS_RAND_MAX + 1.0)));
917 r = __rand(&td->__next_file_state);
918 fno = (unsigned int) ((double) td->o.nr_files
919 * (r / (FRAND_MAX + 1.0)));
923 if (fio_file_done(f))
926 if (!fio_file_open(f)) {
929 err = td_io_open_file(td, f);
935 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
936 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
940 td_io_close_file(td, f);
945 * Get next file to service by doing round robin between all available ones
947 static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
950 unsigned int old_next_file = td->next_file;
956 f = td->files[td->next_file];
959 if (td->next_file >= td->o.nr_files)
962 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
963 if (fio_file_done(f)) {
968 if (!fio_file_open(f)) {
971 err = td_io_open_file(td, f);
973 dprint(FD_FILE, "error %d on open of %s\n",
981 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
983 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
987 td_io_close_file(td, f);
990 } while (td->next_file != old_next_file);
992 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
996 static struct fio_file *__get_next_file(struct thread_data *td)
1000 assert(td->o.nr_files <= td->files_index);
1002 if (td->nr_done_files >= td->o.nr_files) {
1003 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
1004 " nr_files=%d\n", td->nr_open_files,
1010 f = td->file_service_file;
1011 if (f && fio_file_open(f) && !fio_file_closing(f)) {
1012 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
1014 if (td->file_service_left--)
1018 if (td->o.file_service_type == FIO_FSERVICE_RR ||
1019 td->o.file_service_type == FIO_FSERVICE_SEQ)
1020 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
1022 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1024 td->file_service_file = f;
1025 td->file_service_left = td->file_service_nr - 1;
1027 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1031 static struct fio_file *get_next_file(struct thread_data *td)
1033 struct prof_io_ops *ops = &td->prof_io_ops;
1035 if (ops->get_next_file)
1036 return ops->get_next_file(td);
1038 return __get_next_file(td);
1041 static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
1046 f = get_next_file(td);
1053 if (!fill_io_u(td, io_u))
1056 put_file_log(td, f);
1057 td_io_close_file(td, f);
1059 fio_file_set_done(f);
1060 td->nr_done_files++;
1061 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
1062 td->nr_done_files, td->o.nr_files);
1069 struct io_u *__get_io_u(struct thread_data *td)
1071 struct io_u *io_u = NULL;
1076 if (!flist_empty(&td->io_u_requeues))
1077 io_u = flist_entry(td->io_u_requeues.next, struct io_u, list);
1078 else if (!queue_full(td)) {
1079 io_u = flist_entry(td->io_u_freelist.next, struct io_u, list);
1084 io_u->end_io = NULL;
1088 assert(io_u->flags & IO_U_F_FREE);
1089 io_u->flags &= ~(IO_U_F_FREE | IO_U_F_FREE_DEF);
1090 io_u->flags &= ~(IO_U_F_TRIMMED | IO_U_F_BARRIER);
1091 io_u->flags &= ~IO_U_F_VER_LIST;
1094 flist_del(&io_u->list);
1095 flist_add(&io_u->list, &td->io_u_busylist);
1097 io_u->flags |= IO_U_F_IN_CUR_DEPTH;
1098 } else if (td->o.verify_async) {
1100 * We ran out, wait for async verify threads to finish and
1103 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1111 static int check_get_trim(struct thread_data *td, struct io_u *io_u)
1113 if (td->o.trim_backlog && td->trim_entries) {
1116 if (td->trim_batch) {
1119 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1120 td->last_ddir != DDIR_READ) {
1121 td->trim_batch = td->o.trim_batch;
1122 if (!td->trim_batch)
1123 td->trim_batch = td->o.trim_backlog;
1127 if (get_trim && !get_next_trim(td, io_u))
1134 static int check_get_verify(struct thread_data *td, struct io_u *io_u)
1136 if (td->o.verify_backlog && td->io_hist_len) {
1139 if (td->verify_batch)
1141 else if (!(td->io_hist_len % td->o.verify_backlog) &&
1142 td->last_ddir != DDIR_READ) {
1143 td->verify_batch = td->o.verify_batch;
1144 if (!td->verify_batch)
1145 td->verify_batch = td->o.verify_backlog;
1149 if (get_verify && !get_next_verify(td, io_u)) {
1159 * Fill offset and start time into the buffer content, to prevent too
1160 * easy compressible data for simple de-dupe attempts. Do this for every
1161 * 512b block in the range, since that should be the smallest block size
1162 * we can expect from a device.
1164 static void small_content_scramble(struct io_u *io_u)
1166 unsigned int i, nr_blocks = io_u->buflen / 512;
1167 unsigned long long boffset;
1168 unsigned int offset;
1175 boffset = io_u->offset;
1176 io_u->buf_filled_len = 0;
1178 for (i = 0; i < nr_blocks; i++) {
1180 * Fill the byte offset into a "random" start offset of
1181 * the buffer, given by the product of the usec time
1182 * and the actual offset.
1184 offset = (io_u->start_time.tv_usec ^ boffset) & 511;
1185 offset &= ~(sizeof(unsigned long long) - 1);
1186 if (offset >= 512 - sizeof(unsigned long long))
1187 offset -= sizeof(unsigned long long);
1188 memcpy(p + offset, &boffset, sizeof(boffset));
1190 end = p + 512 - sizeof(io_u->start_time);
1191 memcpy(end, &io_u->start_time, sizeof(io_u->start_time));
1198 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1199 * etc. The returned io_u is fully ready to be prepped and submitted.
1201 struct io_u *get_io_u(struct thread_data *td)
1205 int do_scramble = 0;
1207 io_u = __get_io_u(td);
1209 dprint(FD_IO, "__get_io_u failed\n");
1213 if (check_get_verify(td, io_u))
1215 if (check_get_trim(td, io_u))
1219 * from a requeue, io_u already setup
1225 * If using an iolog, grab next piece if any available.
1227 if (td->o.read_iolog_file) {
1228 if (read_iolog_get(td, io_u))
1230 } else if (set_io_u_file(td, io_u)) {
1231 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1236 assert(fio_file_open(f));
1238 if (ddir_rw(io_u->ddir)) {
1239 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
1240 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
1244 f->last_start = io_u->offset;
1245 f->last_pos = io_u->offset + io_u->buflen;
1247 if (io_u->ddir == DDIR_WRITE) {
1248 if (td->o.refill_buffers) {
1249 io_u_fill_buffer(td, io_u,
1250 io_u->xfer_buflen, io_u->xfer_buflen);
1251 } else if (td->o.scramble_buffers)
1253 if (td->o.verify != VERIFY_NONE) {
1254 populate_verify_io_u(td, io_u);
1257 } else if (io_u->ddir == DDIR_READ) {
1259 * Reset the buf_filled parameters so next time if the
1260 * buffer is used for writes it is refilled.
1262 io_u->buf_filled_len = 0;
1267 * Set io data pointers.
1269 io_u->xfer_buf = io_u->buf;
1270 io_u->xfer_buflen = io_u->buflen;
1274 if (!td_io_prep(td, io_u)) {
1275 if (!td->o.disable_slat)
1276 fio_gettime(&io_u->start_time, NULL);
1278 small_content_scramble(io_u);
1282 dprint(FD_IO, "get_io_u failed\n");
1287 void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1289 const char *msg[] = { "read", "write", "sync", "datasync",
1290 "sync_file_range", "wait", "trim" };
1294 log_err("fio: io_u error");
1297 log_err(" on file %s", io_u->file->file_name);
1299 log_err(": %s\n", strerror(io_u->error));
1301 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
1302 io_u->offset, io_u->xfer_buflen);
1305 td_verror(td, io_u->error, "io_u error");
1308 static void account_io_completion(struct thread_data *td, struct io_u *io_u,
1309 struct io_completion_data *icd,
1310 const enum fio_ddir idx, unsigned int bytes)
1312 unsigned long uninitialized_var(lusec);
1314 if (!td->o.disable_clat || !td->o.disable_bw)
1315 lusec = utime_since(&io_u->issue_time, &icd->time);
1317 if (!td->o.disable_lat) {
1318 unsigned long tusec;
1320 tusec = utime_since(&io_u->start_time, &icd->time);
1321 add_lat_sample(td, idx, tusec, bytes);
1324 if (!td->o.disable_clat) {
1325 add_clat_sample(td, idx, lusec, bytes);
1326 io_u_mark_latency(td, lusec);
1329 if (!td->o.disable_bw)
1330 add_bw_sample(td, idx, bytes, &icd->time);
1332 add_iops_sample(td, idx, &icd->time);
1335 static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
1337 unsigned long long secs, remainder, bps, bytes;
1338 bytes = td->this_io_bytes[ddir];
1339 bps = td->rate_bps[ddir];
1341 remainder = bytes % bps;
1342 return remainder * 1000000 / bps + secs * 1000000;
1345 static void io_completed(struct thread_data *td, struct io_u *io_u,
1346 struct io_completion_data *icd)
1349 * Older gcc's are too dumb to realize that usec is always used
1350 * initialized, silence that warning.
1352 unsigned long uninitialized_var(usec);
1355 dprint_io_u(io_u, "io complete");
1358 assert(io_u->flags & IO_U_F_FLIGHT);
1359 io_u->flags &= ~(IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
1362 if (ddir_sync(io_u->ddir)) {
1363 td->last_was_sync = 1;
1366 f->first_write = -1ULL;
1367 f->last_write = -1ULL;
1372 td->last_was_sync = 0;
1373 td->last_ddir = io_u->ddir;
1375 if (!io_u->error && ddir_rw(io_u->ddir)) {
1376 unsigned int bytes = io_u->buflen - io_u->resid;
1377 const enum fio_ddir idx = io_u->ddir;
1378 const enum fio_ddir odx = io_u->ddir ^ 1;
1381 td->io_blocks[idx]++;
1382 td->this_io_blocks[idx]++;
1383 td->io_bytes[idx] += bytes;
1385 if (!(io_u->flags & IO_U_F_VER_LIST))
1386 td->this_io_bytes[idx] += bytes;
1388 if (idx == DDIR_WRITE) {
1391 if (f->first_write == -1ULL ||
1392 io_u->offset < f->first_write)
1393 f->first_write = io_u->offset;
1394 if (f->last_write == -1ULL ||
1395 ((io_u->offset + bytes) > f->last_write))
1396 f->last_write = io_u->offset + bytes;
1400 if (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
1401 td->runstate == TD_VERIFYING)) {
1402 account_io_completion(td, io_u, icd, idx, bytes);
1404 if (__should_check_rate(td, idx)) {
1405 td->rate_pending_usleep[idx] =
1406 (usec_for_io(td, idx) -
1407 utime_since_now(&td->start));
1409 if (__should_check_rate(td, odx))
1410 td->rate_pending_usleep[odx] =
1411 (usec_for_io(td, odx) -
1412 utime_since_now(&td->start));
1415 if (td_write(td) && idx == DDIR_WRITE &&
1417 td->o.verify != VERIFY_NONE)
1418 log_io_piece(td, io_u);
1420 icd->bytes_done[idx] += bytes;
1423 ret = io_u->end_io(td, io_u);
1424 if (ret && !icd->error)
1427 } else if (io_u->error) {
1428 icd->error = io_u->error;
1429 io_u_log_error(td, io_u);
1431 if (icd->error && td_non_fatal_error(icd->error) &&
1432 (td->o.continue_on_error & td_error_type(io_u->ddir, icd->error))) {
1434 * If there is a non_fatal error, then add to the error count
1435 * and clear all the errors.
1437 update_error_count(td, icd->error);
1444 static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1447 if (!td->o.disable_clat || !td->o.disable_bw)
1448 fio_gettime(&icd->time, NULL);
1453 icd->bytes_done[0] = icd->bytes_done[1] = 0;
1456 static void ios_completed(struct thread_data *td,
1457 struct io_completion_data *icd)
1462 for (i = 0; i < icd->nr; i++) {
1463 io_u = td->io_ops->event(td, i);
1465 io_completed(td, io_u, icd);
1467 if (!(io_u->flags & IO_U_F_FREE_DEF))
1473 * Complete a single io_u for the sync engines.
1475 int io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
1476 unsigned long *bytes)
1478 struct io_completion_data icd;
1480 init_icd(td, &icd, 1);
1481 io_completed(td, io_u, &icd);
1483 if (!(io_u->flags & IO_U_F_FREE_DEF))
1487 td_verror(td, icd.error, "io_u_sync_complete");
1492 bytes[0] += icd.bytes_done[0];
1493 bytes[1] += icd.bytes_done[1];
1500 * Called to complete min_events number of io for the async engines.
1502 int io_u_queued_complete(struct thread_data *td, int min_evts,
1503 unsigned long *bytes)
1505 struct io_completion_data icd;
1506 struct timespec *tvp = NULL;
1508 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
1510 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
1515 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete, tvp);
1517 td_verror(td, -ret, "td_io_getevents");
1522 init_icd(td, &icd, ret);
1523 ios_completed(td, &icd);
1525 td_verror(td, icd.error, "io_u_queued_complete");
1530 bytes[0] += icd.bytes_done[0];
1531 bytes[1] += icd.bytes_done[1];
1538 * Call when io_u is really queued, to update the submission latency.
1540 void io_u_queued(struct thread_data *td, struct io_u *io_u)
1542 if (!td->o.disable_slat) {
1543 unsigned long slat_time;
1545 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
1546 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen);
1551 * "randomly" fill the buffer contents
1553 void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1554 unsigned int min_write, unsigned int max_bs)
1556 io_u->buf_filled_len = 0;
1558 if (!td->o.zero_buffers) {
1559 unsigned int perc = td->o.compress_percentage;
1562 unsigned int seg = min_write;
1564 seg = min(min_write, td->o.compress_chunk);
1565 fill_random_buf_percentage(&td->buf_state, io_u->buf,
1568 fill_random_buf(&td->buf_state, io_u->buf, max_bs);
1570 memset(io_u->buf, 0, max_bs);