13 #include "lib/axmap.h"
15 struct io_completion_data {
18 int error; /* output */
19 unsigned long bytes_done[DDIR_RWDIR_CNT]; /* output */
20 struct timeval time; /* output */
24 * The ->io_axmap contains a map of blocks we have or have not done io
25 * to yet. Used to make sure we cover the entire range in a fair fashion.
27 static int random_map_free(struct fio_file *f, const unsigned long long block)
29 return !axmap_isset(f->io_axmap, block);
33 * Mark a given offset as used in the map.
35 static void mark_random_map(struct thread_data *td, struct io_u *io_u)
37 unsigned int min_bs = td->o.rw_min_bs;
38 struct fio_file *f = io_u->file;
39 unsigned long long block;
40 unsigned int nr_blocks;
42 block = (io_u->offset - f->file_offset) / (unsigned long long) min_bs;
43 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
45 if (!(io_u->flags & IO_U_F_BUSY_OK))
46 nr_blocks = axmap_set_nr(f->io_axmap, block, nr_blocks);
48 if ((nr_blocks * min_bs) < io_u->buflen)
49 io_u->buflen = nr_blocks * min_bs;
52 static unsigned long long last_block(struct thread_data *td, struct fio_file *f,
55 unsigned long long max_blocks;
56 unsigned long long max_size;
58 assert(ddir_rw(ddir));
61 * Hmm, should we make sure that ->io_size <= ->real_file_size?
63 max_size = f->io_size;
64 if (max_size > f->real_file_size)
65 max_size = f->real_file_size;
68 max_size = td->o.zone_range;
70 max_blocks = max_size / (unsigned long long) td->o.ba[ddir];
77 static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
78 enum fio_ddir ddir, unsigned long long *b)
82 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE) {
83 unsigned long long rmax, lastb;
85 lastb = last_block(td, f, ddir);
89 rmax = td->o.use_os_rand ? OS_RAND_MAX : FRAND_MAX;
91 if (td->o.use_os_rand) {
93 r = os_random_long(&td->random_state);
96 r = __rand(&td->__random_state);
99 dprint(FD_RANDOM, "off rand %llu\n", r);
101 *b = (lastb - 1) * (r / ((unsigned long long) rmax + 1.0));
105 if (lfsr_next(&f->lfsr, &off))
112 * if we are not maintaining a random map, we are done.
114 if (!file_randommap(td, f))
118 * calculate map offset and check if it's free
120 if (random_map_free(f, *b))
123 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n", *b);
125 *b = axmap_next_free(f->io_axmap, *b);
126 if (*b == (uint64_t) -1ULL)
132 static int __get_next_rand_offset_zipf(struct thread_data *td,
133 struct fio_file *f, enum fio_ddir ddir,
134 unsigned long long *b)
136 *b = zipf_next(&f->zipf);
140 static int __get_next_rand_offset_pareto(struct thread_data *td,
141 struct fio_file *f, enum fio_ddir ddir,
142 unsigned long long *b)
144 *b = pareto_next(&f->zipf);
148 static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
149 enum fio_ddir ddir, unsigned long long *b)
151 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
152 return __get_next_rand_offset(td, f, ddir, b);
153 else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
154 return __get_next_rand_offset_zipf(td, f, ddir, b);
155 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
156 return __get_next_rand_offset_pareto(td, f, ddir, b);
158 log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
162 static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
163 enum fio_ddir ddir, unsigned long long *b)
165 if (!get_next_rand_offset(td, f, ddir, b))
168 if (td->o.time_based) {
170 if (!get_next_rand_offset(td, f, ddir, b))
174 dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
175 f->file_name, f->last_pos, f->real_file_size);
179 static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
180 enum fio_ddir ddir, unsigned long long *offset)
182 assert(ddir_rw(ddir));
184 if (f->last_pos >= f->io_size + get_start_offset(td) && td->o.time_based)
185 f->last_pos = f->last_pos - f->io_size;
187 if (f->last_pos < f->real_file_size) {
188 unsigned long long pos;
190 if (f->last_pos == f->file_offset && td->o.ddir_seq_add < 0)
191 f->last_pos = f->real_file_size;
193 pos = f->last_pos - f->file_offset;
195 pos += td->o.ddir_seq_add;
204 static int get_next_block(struct thread_data *td, struct io_u *io_u,
205 enum fio_ddir ddir, int rw_seq)
207 struct fio_file *f = io_u->file;
208 unsigned long long b, offset;
211 assert(ddir_rw(ddir));
217 ret = get_next_rand_block(td, f, ddir, &b);
219 ret = get_next_seq_offset(td, f, ddir, &offset);
221 io_u->flags |= IO_U_F_BUSY_OK;
223 if (td->o.rw_seq == RW_SEQ_SEQ) {
224 ret = get_next_seq_offset(td, f, ddir, &offset);
226 ret = get_next_rand_block(td, f, ddir, &b);
227 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
228 if (f->last_start != -1ULL)
229 offset = f->last_start - f->file_offset;
234 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
241 io_u->offset = offset;
243 io_u->offset = b * td->o.ba[ddir];
245 log_err("fio: bug in offset generation: offset=%llu, b=%llu\n",
255 * For random io, generate a random new block and see if it's used. Repeat
256 * until we find a free one. For sequential io, just return the end of
257 * the last io issued.
259 static int __get_next_offset(struct thread_data *td, struct io_u *io_u)
261 struct fio_file *f = io_u->file;
262 enum fio_ddir ddir = io_u->ddir;
265 assert(ddir_rw(ddir));
267 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
269 td->ddir_seq_nr = td->o.ddir_seq_nr;
272 if (get_next_block(td, io_u, ddir, rw_seq_hit))
275 if (io_u->offset >= f->io_size) {
276 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
277 io_u->offset, f->io_size);
281 io_u->offset += f->file_offset;
282 if (io_u->offset >= f->real_file_size) {
283 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
284 io_u->offset, f->real_file_size);
291 static int get_next_offset(struct thread_data *td, struct io_u *io_u)
293 struct prof_io_ops *ops = &td->prof_io_ops;
295 if (ops->fill_io_u_off)
296 return ops->fill_io_u_off(td, io_u);
298 return __get_next_offset(td, io_u);
301 static inline int io_u_fits(struct thread_data *td, struct io_u *io_u,
304 struct fio_file *f = io_u->file;
306 return io_u->offset + buflen <= f->io_size + get_start_offset(td);
309 static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u)
311 const int ddir = io_u->ddir;
312 unsigned int buflen = 0;
313 unsigned int minbs, maxbs;
314 unsigned long r, rand_max;
316 assert(ddir_rw(ddir));
318 minbs = td->o.min_bs[ddir];
319 maxbs = td->o.max_bs[ddir];
325 * If we can't satisfy the min block size from here, then fail
327 if (!io_u_fits(td, io_u, minbs))
330 if (td->o.use_os_rand)
331 rand_max = OS_RAND_MAX;
333 rand_max = FRAND_MAX;
336 if (td->o.use_os_rand)
337 r = os_random_long(&td->bsrange_state);
339 r = __rand(&td->__bsrange_state);
341 if (!td->o.bssplit_nr[ddir]) {
342 buflen = 1 + (unsigned int) ((double) maxbs *
343 (r / (rand_max + 1.0)));
350 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
351 struct bssplit *bsp = &td->o.bssplit[ddir][i];
355 if ((r <= ((rand_max / 100L) * perc)) &&
356 io_u_fits(td, io_u, buflen))
361 if (!td->o.bs_unaligned && is_power_of_2(minbs))
362 buflen = (buflen + minbs - 1) & ~(minbs - 1);
364 } while (!io_u_fits(td, io_u, buflen));
369 static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
371 struct prof_io_ops *ops = &td->prof_io_ops;
373 if (ops->fill_io_u_size)
374 return ops->fill_io_u_size(td, io_u);
376 return __get_next_buflen(td, io_u);
379 static void set_rwmix_bytes(struct thread_data *td)
384 * we do time or byte based switch. this is needed because
385 * buffered writes may issue a lot quicker than they complete,
386 * whereas reads do not.
388 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
389 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
392 static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
397 if (td->o.use_os_rand) {
398 r = os_random_long(&td->rwmix_state);
399 v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
401 r = __rand(&td->__rwmix_state);
402 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
405 if (v <= td->o.rwmix[DDIR_READ])
411 static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
413 enum fio_ddir odir = ddir ^ 1;
417 assert(ddir_rw(ddir));
419 if (td->rate_pending_usleep[ddir] <= 0)
423 * We have too much pending sleep in this direction. See if we
428 * Other direction does not have too much pending, switch
430 if (td->rate_pending_usleep[odir] < 100000)
434 * Both directions have pending sleep. Sleep the minimum time
435 * and deduct from both.
437 if (td->rate_pending_usleep[ddir] <=
438 td->rate_pending_usleep[odir]) {
439 usec = td->rate_pending_usleep[ddir];
441 usec = td->rate_pending_usleep[odir];
445 usec = td->rate_pending_usleep[ddir];
448 * We are going to sleep, ensure that we flush anything pending as
449 * not to skew our latency numbers.
451 * Changed to only monitor 'in flight' requests here instead of the
452 * td->cur_depth, b/c td->cur_depth does not accurately represent
453 * io's that have been actually submitted to an async engine,
454 * and cur_depth is meaningless for sync engines.
456 if (td->io_u_in_flight) {
459 ret = io_u_queued_complete(td, td->io_u_in_flight, NULL);
462 fio_gettime(&t, NULL);
463 usec_sleep(td, usec);
464 usec = utime_since_now(&t);
466 td->rate_pending_usleep[ddir] -= usec;
469 if (td_rw(td) && __should_check_rate(td, odir))
470 td->rate_pending_usleep[odir] -= usec;
478 * Return the data direction for the next io_u. If the job is a
479 * mixed read/write workload, check the rwmix cycle and switch if
482 static enum fio_ddir get_rw_ddir(struct thread_data *td)
487 * see if it's time to fsync
489 if (td->o.fsync_blocks &&
490 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
491 td->io_issues[DDIR_WRITE] && should_fsync(td))
495 * see if it's time to fdatasync
497 if (td->o.fdatasync_blocks &&
498 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
499 td->io_issues[DDIR_WRITE] && should_fsync(td))
500 return DDIR_DATASYNC;
503 * see if it's time to sync_file_range
505 if (td->sync_file_range_nr &&
506 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
507 td->io_issues[DDIR_WRITE] && should_fsync(td))
508 return DDIR_SYNC_FILE_RANGE;
512 * Check if it's time to seed a new data direction.
514 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
516 * Put a top limit on how many bytes we do for
517 * one data direction, to avoid overflowing the
520 ddir = get_rand_ddir(td);
522 if (ddir != td->rwmix_ddir)
525 td->rwmix_ddir = ddir;
527 ddir = td->rwmix_ddir;
528 } else if (td_read(td))
530 else if (td_write(td))
535 td->rwmix_ddir = rate_ddir(td, ddir);
536 return td->rwmix_ddir;
539 static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
541 io_u->ddir = get_rw_ddir(td);
543 if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
544 td->o.barrier_blocks &&
545 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
546 td->io_issues[DDIR_WRITE])
547 io_u->flags |= IO_U_F_BARRIER;
550 void put_file_log(struct thread_data *td, struct fio_file *f)
552 int ret = put_file(td, f);
555 td_verror(td, ret, "file close");
558 void put_io_u(struct thread_data *td, struct io_u *io_u)
562 if (io_u->file && !(io_u->flags & IO_U_F_FREE_DEF))
563 put_file_log(td, io_u->file);
565 io_u->flags &= ~IO_U_F_FREE_DEF;
566 io_u->flags |= IO_U_F_FREE;
568 if (io_u->flags & IO_U_F_IN_CUR_DEPTH)
570 flist_del_init(&io_u->list);
571 flist_add(&io_u->list, &td->io_u_freelist);
573 td_io_u_free_notify(td);
576 void clear_io_u(struct thread_data *td, struct io_u *io_u)
578 io_u->flags &= ~IO_U_F_FLIGHT;
582 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
584 struct io_u *__io_u = *io_u;
586 dprint(FD_IO, "requeue %p\n", __io_u);
590 __io_u->flags |= IO_U_F_FREE;
591 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(__io_u->ddir))
592 td->io_issues[__io_u->ddir]--;
594 __io_u->flags &= ~IO_U_F_FLIGHT;
595 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH)
597 flist_del(&__io_u->list);
598 flist_add_tail(&__io_u->list, &td->io_u_requeues);
603 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
605 if (td->io_ops->flags & FIO_NOIO)
608 set_rw_ddir(td, io_u);
611 * fsync() or fdatasync() or trim etc, we are done
613 if (!ddir_rw(io_u->ddir))
617 * See if it's time to switch to a new zone
619 if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
621 io_u->file->file_offset += td->o.zone_range + td->o.zone_skip;
622 io_u->file->last_pos = io_u->file->file_offset;
623 td->io_skip_bytes += td->o.zone_skip;
627 * No log, let the seq/rand engine retrieve the next buflen and
630 if (get_next_offset(td, io_u)) {
631 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
635 io_u->buflen = get_next_buflen(td, io_u);
637 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
641 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
642 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
643 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
644 io_u->buflen, io_u->file->real_file_size);
649 * mark entry before potentially trimming io_u
651 if (td_random(td) && file_randommap(td, io_u->file))
652 mark_random_map(td, io_u);
655 * If using a write iolog, store this entry.
658 dprint_io_u(io_u, "fill_io_u");
659 td->zone_bytes += io_u->buflen;
664 static void __io_u_mark_map(unsigned int *map, unsigned int nr)
693 void io_u_mark_submit(struct thread_data *td, unsigned int nr)
695 __io_u_mark_map(td->ts.io_u_submit, nr);
696 td->ts.total_submit++;
699 void io_u_mark_complete(struct thread_data *td, unsigned int nr)
701 __io_u_mark_map(td->ts.io_u_complete, nr);
702 td->ts.total_complete++;
705 void io_u_mark_depth(struct thread_data *td, unsigned int nr)
709 switch (td->cur_depth) {
731 td->ts.io_u_map[idx] += nr;
734 static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
771 assert(idx < FIO_IO_U_LAT_U_NR);
772 td->ts.io_u_lat_u[idx]++;
775 static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
816 assert(idx < FIO_IO_U_LAT_M_NR);
817 td->ts.io_u_lat_m[idx]++;
820 static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
823 io_u_mark_lat_usec(td, usec);
825 io_u_mark_lat_msec(td, usec / 1000);
829 * Get next file to service by choosing one at random
831 static struct fio_file *get_next_file_rand(struct thread_data *td,
832 enum fio_file_flags goodf,
833 enum fio_file_flags badf)
842 if (td->o.use_os_rand) {
843 r = os_random_long(&td->next_file_state);
844 fno = (unsigned int) ((double) td->o.nr_files
845 * (r / (OS_RAND_MAX + 1.0)));
847 r = __rand(&td->__next_file_state);
848 fno = (unsigned int) ((double) td->o.nr_files
849 * (r / (FRAND_MAX + 1.0)));
853 if (fio_file_done(f))
856 if (!fio_file_open(f)) {
859 err = td_io_open_file(td, f);
865 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
866 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
870 td_io_close_file(td, f);
875 * Get next file to service by doing round robin between all available ones
877 static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
880 unsigned int old_next_file = td->next_file;
886 f = td->files[td->next_file];
889 if (td->next_file >= td->o.nr_files)
892 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
893 if (fio_file_done(f)) {
898 if (!fio_file_open(f)) {
901 err = td_io_open_file(td, f);
903 dprint(FD_FILE, "error %d on open of %s\n",
911 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
913 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
917 td_io_close_file(td, f);
920 } while (td->next_file != old_next_file);
922 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
926 static struct fio_file *__get_next_file(struct thread_data *td)
930 assert(td->o.nr_files <= td->files_index);
932 if (td->nr_done_files >= td->o.nr_files) {
933 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
934 " nr_files=%d\n", td->nr_open_files,
940 f = td->file_service_file;
941 if (f && fio_file_open(f) && !fio_file_closing(f)) {
942 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
944 if (td->file_service_left--)
948 if (td->o.file_service_type == FIO_FSERVICE_RR ||
949 td->o.file_service_type == FIO_FSERVICE_SEQ)
950 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
952 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
954 td->file_service_file = f;
955 td->file_service_left = td->file_service_nr - 1;
957 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
961 static struct fio_file *get_next_file(struct thread_data *td)
963 struct prof_io_ops *ops = &td->prof_io_ops;
965 if (ops->get_next_file)
966 return ops->get_next_file(td);
968 return __get_next_file(td);
971 static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
976 f = get_next_file(td);
983 if (!fill_io_u(td, io_u))
987 td_io_close_file(td, f);
989 fio_file_set_done(f);
991 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
992 td->nr_done_files, td->o.nr_files);
999 struct io_u *__get_io_u(struct thread_data *td)
1001 struct io_u *io_u = NULL;
1006 if (!flist_empty(&td->io_u_requeues))
1007 io_u = flist_entry(td->io_u_requeues.next, struct io_u, list);
1008 else if (!queue_full(td)) {
1009 io_u = flist_entry(td->io_u_freelist.next, struct io_u, list);
1014 io_u->end_io = NULL;
1018 assert(io_u->flags & IO_U_F_FREE);
1019 io_u->flags &= ~(IO_U_F_FREE | IO_U_F_FREE_DEF);
1020 io_u->flags &= ~(IO_U_F_TRIMMED | IO_U_F_BARRIER);
1021 io_u->flags &= ~IO_U_F_VER_LIST;
1024 flist_del(&io_u->list);
1025 flist_add_tail(&io_u->list, &td->io_u_busylist);
1027 io_u->flags |= IO_U_F_IN_CUR_DEPTH;
1028 } else if (td->o.verify_async) {
1030 * We ran out, wait for async verify threads to finish and
1033 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1041 static int check_get_trim(struct thread_data *td, struct io_u *io_u)
1043 if (td->o.trim_backlog && td->trim_entries) {
1046 if (td->trim_batch) {
1049 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1050 td->last_ddir != DDIR_READ) {
1051 td->trim_batch = td->o.trim_batch;
1052 if (!td->trim_batch)
1053 td->trim_batch = td->o.trim_backlog;
1057 if (get_trim && !get_next_trim(td, io_u))
1064 static int check_get_verify(struct thread_data *td, struct io_u *io_u)
1066 if (td->o.verify_backlog && td->io_hist_len) {
1069 if (td->verify_batch)
1071 else if (!(td->io_hist_len % td->o.verify_backlog) &&
1072 td->last_ddir != DDIR_READ) {
1073 td->verify_batch = td->o.verify_batch;
1074 if (!td->verify_batch)
1075 td->verify_batch = td->o.verify_backlog;
1079 if (get_verify && !get_next_verify(td, io_u)) {
1089 * Fill offset and start time into the buffer content, to prevent too
1090 * easy compressible data for simple de-dupe attempts. Do this for every
1091 * 512b block in the range, since that should be the smallest block size
1092 * we can expect from a device.
1094 static void small_content_scramble(struct io_u *io_u)
1096 unsigned int i, nr_blocks = io_u->buflen / 512;
1097 unsigned long long boffset;
1098 unsigned int offset;
1105 boffset = io_u->offset;
1106 io_u->buf_filled_len = 0;
1108 for (i = 0; i < nr_blocks; i++) {
1110 * Fill the byte offset into a "random" start offset of
1111 * the buffer, given by the product of the usec time
1112 * and the actual offset.
1114 offset = (io_u->start_time.tv_usec ^ boffset) & 511;
1115 offset &= ~(sizeof(unsigned long long) - 1);
1116 if (offset >= 512 - sizeof(unsigned long long))
1117 offset -= sizeof(unsigned long long);
1118 memcpy(p + offset, &boffset, sizeof(boffset));
1120 end = p + 512 - sizeof(io_u->start_time);
1121 memcpy(end, &io_u->start_time, sizeof(io_u->start_time));
1128 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1129 * etc. The returned io_u is fully ready to be prepped and submitted.
1131 struct io_u *get_io_u(struct thread_data *td)
1135 int do_scramble = 0;
1137 io_u = __get_io_u(td);
1139 dprint(FD_IO, "__get_io_u failed\n");
1143 if (check_get_verify(td, io_u))
1145 if (check_get_trim(td, io_u))
1149 * from a requeue, io_u already setup
1155 * If using an iolog, grab next piece if any available.
1157 if (td->o.read_iolog_file) {
1158 if (read_iolog_get(td, io_u))
1160 } else if (set_io_u_file(td, io_u)) {
1161 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1166 assert(fio_file_open(f));
1168 if (ddir_rw(io_u->ddir)) {
1169 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
1170 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
1174 f->last_start = io_u->offset;
1175 f->last_pos = io_u->offset + io_u->buflen;
1177 if (io_u->ddir == DDIR_WRITE) {
1178 if (td->o.refill_buffers) {
1179 io_u_fill_buffer(td, io_u,
1180 io_u->xfer_buflen, io_u->xfer_buflen);
1181 } else if (td->o.scramble_buffers)
1183 if (td->o.verify != VERIFY_NONE) {
1184 populate_verify_io_u(td, io_u);
1187 } else if (io_u->ddir == DDIR_READ) {
1189 * Reset the buf_filled parameters so next time if the
1190 * buffer is used for writes it is refilled.
1192 io_u->buf_filled_len = 0;
1197 * Set io data pointers.
1199 io_u->xfer_buf = io_u->buf;
1200 io_u->xfer_buflen = io_u->buflen;
1204 if (!td_io_prep(td, io_u)) {
1205 if (!td->o.disable_slat)
1206 fio_gettime(&io_u->start_time, NULL);
1208 small_content_scramble(io_u);
1212 dprint(FD_IO, "get_io_u failed\n");
1217 void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1219 enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
1220 const char *msg[] = { "read", "write", "sync", "datasync",
1221 "sync_file_range", "wait", "trim" };
1223 if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1226 log_err("fio: io_u error");
1229 log_err(" on file %s", io_u->file->file_name);
1231 log_err(": %s\n", strerror(io_u->error));
1233 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
1234 io_u->offset, io_u->xfer_buflen);
1237 td_verror(td, io_u->error, "io_u error");
1240 static void account_io_completion(struct thread_data *td, struct io_u *io_u,
1241 struct io_completion_data *icd,
1242 const enum fio_ddir idx, unsigned int bytes)
1244 unsigned long lusec = 0;
1246 if (!td->o.disable_clat || !td->o.disable_bw)
1247 lusec = utime_since(&io_u->issue_time, &icd->time);
1249 if (!td->o.disable_lat) {
1250 unsigned long tusec;
1252 tusec = utime_since(&io_u->start_time, &icd->time);
1253 add_lat_sample(td, idx, tusec, bytes);
1255 if (td->o.max_latency && tusec > td->o.max_latency) {
1257 log_err("fio: latency of %lu usec exceeds specified max (%u usec)\n", tusec, td->o.max_latency);
1258 td_verror(td, ETIMEDOUT, "max latency exceeded");
1259 icd->error = ETIMEDOUT;
1263 if (!td->o.disable_clat) {
1264 add_clat_sample(td, idx, lusec, bytes);
1265 io_u_mark_latency(td, lusec);
1268 if (!td->o.disable_bw)
1269 add_bw_sample(td, idx, bytes, &icd->time);
1271 add_iops_sample(td, idx, &icd->time);
1274 static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
1276 unsigned long long secs, remainder, bps, bytes;
1277 bytes = td->this_io_bytes[ddir];
1278 bps = td->rate_bps[ddir];
1280 remainder = bytes % bps;
1281 return remainder * 1000000 / bps + secs * 1000000;
1284 static void io_completed(struct thread_data *td, struct io_u *io_u,
1285 struct io_completion_data *icd)
1289 dprint_io_u(io_u, "io complete");
1292 assert(io_u->flags & IO_U_F_FLIGHT);
1293 io_u->flags &= ~(IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
1296 if (ddir_sync(io_u->ddir)) {
1297 td->last_was_sync = 1;
1300 f->first_write = -1ULL;
1301 f->last_write = -1ULL;
1306 td->last_was_sync = 0;
1307 td->last_ddir = io_u->ddir;
1309 if (!io_u->error && ddir_rw(io_u->ddir)) {
1310 unsigned int bytes = io_u->buflen - io_u->resid;
1311 const enum fio_ddir idx = io_u->ddir;
1312 const enum fio_ddir odx = io_u->ddir ^ 1;
1315 td->io_blocks[idx]++;
1316 td->this_io_blocks[idx]++;
1317 td->io_bytes[idx] += bytes;
1319 if (!(io_u->flags & IO_U_F_VER_LIST))
1320 td->this_io_bytes[idx] += bytes;
1322 if (idx == DDIR_WRITE) {
1325 if (f->first_write == -1ULL ||
1326 io_u->offset < f->first_write)
1327 f->first_write = io_u->offset;
1328 if (f->last_write == -1ULL ||
1329 ((io_u->offset + bytes) > f->last_write))
1330 f->last_write = io_u->offset + bytes;
1334 if (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
1335 td->runstate == TD_VERIFYING)) {
1336 account_io_completion(td, io_u, icd, idx, bytes);
1338 if (__should_check_rate(td, idx)) {
1339 td->rate_pending_usleep[idx] =
1340 (usec_for_io(td, idx) -
1341 utime_since_now(&td->start));
1343 if (idx != DDIR_TRIM && __should_check_rate(td, odx))
1344 td->rate_pending_usleep[odx] =
1345 (usec_for_io(td, odx) -
1346 utime_since_now(&td->start));
1349 if (td_write(td) && idx == DDIR_WRITE &&
1351 td->o.verify != VERIFY_NONE)
1352 log_io_piece(td, io_u);
1354 icd->bytes_done[idx] += bytes;
1357 ret = io_u->end_io(td, io_u);
1358 if (ret && !icd->error)
1361 } else if (io_u->error) {
1362 icd->error = io_u->error;
1363 io_u_log_error(td, io_u);
1366 enum error_type_bit eb = td_error_type(io_u->ddir, icd->error);
1367 if (!td_non_fatal_error(td, eb, icd->error))
1370 * If there is a non_fatal error, then add to the error count
1371 * and clear all the errors.
1373 update_error_count(td, icd->error);
1380 static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1384 if (!td->o.disable_clat || !td->o.disable_bw)
1385 fio_gettime(&icd->time, NULL);
1390 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1391 icd->bytes_done[ddir] = 0;
1394 static void ios_completed(struct thread_data *td,
1395 struct io_completion_data *icd)
1400 for (i = 0; i < icd->nr; i++) {
1401 io_u = td->io_ops->event(td, i);
1403 io_completed(td, io_u, icd);
1405 if (!(io_u->flags & IO_U_F_FREE_DEF))
1411 * Complete a single io_u for the sync engines.
1413 int io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
1414 unsigned long *bytes)
1416 struct io_completion_data icd;
1418 init_icd(td, &icd, 1);
1419 io_completed(td, io_u, &icd);
1421 if (!(io_u->flags & IO_U_F_FREE_DEF))
1425 td_verror(td, icd.error, "io_u_sync_complete");
1432 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1433 bytes[ddir] += icd.bytes_done[ddir];
1440 * Called to complete min_events number of io for the async engines.
1442 int io_u_queued_complete(struct thread_data *td, int min_evts,
1443 unsigned long *bytes)
1445 struct io_completion_data icd;
1446 struct timespec *tvp = NULL;
1448 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
1450 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
1455 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete, tvp);
1457 td_verror(td, -ret, "td_io_getevents");
1462 init_icd(td, &icd, ret);
1463 ios_completed(td, &icd);
1465 td_verror(td, icd.error, "io_u_queued_complete");
1472 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1473 bytes[ddir] += icd.bytes_done[ddir];
1480 * Call when io_u is really queued, to update the submission latency.
1482 void io_u_queued(struct thread_data *td, struct io_u *io_u)
1484 if (!td->o.disable_slat) {
1485 unsigned long slat_time;
1487 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
1488 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen);
1493 * "randomly" fill the buffer contents
1495 void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1496 unsigned int min_write, unsigned int max_bs)
1498 io_u->buf_filled_len = 0;
1500 if (!td->o.zero_buffers) {
1501 unsigned int perc = td->o.compress_percentage;
1504 unsigned int seg = min_write;
1506 seg = min(min_write, td->o.compress_chunk);
1507 fill_random_buf_percentage(&td->buf_state, io_u->buf,
1510 fill_random_buf(&td->buf_state, io_u->buf, max_bs);
1512 memset(io_u->buf, 0, max_bs);