13 #include "lib/bitmap.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_bitmap 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 !bitmap_isset(f->io_bitmap, 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 = bitmap_set_nr(f->io_bitmap, 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)
80 unsigned long long rmax, r, lastb;
82 lastb = last_block(td, f, ddir);
86 rmax = td->o.use_os_rand ? OS_RAND_MAX : FRAND_MAX;
88 if (td->o.use_os_rand) {
90 r = os_random_long(&td->random_state);
93 r = __rand(&td->__random_state);
96 *b = (lastb - 1) * (r / ((unsigned long long) rmax + 1.0));
98 dprint(FD_RANDOM, "off rand %llu\n", r);
101 * if we are not maintaining a random map, we are done.
103 if (!file_randommap(td, f))
107 * calculate map offset and check if it's free
109 if (random_map_free(f, *b))
112 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n", *b);
114 *b = bitmap_next_free(f->io_bitmap, *b);
115 if (*b == (uint64_t) -1ULL)
121 static int __get_next_rand_offset_zipf(struct thread_data *td,
122 struct fio_file *f, enum fio_ddir ddir,
123 unsigned long long *b)
125 *b = zipf_next(&f->zipf);
129 static int __get_next_rand_offset_pareto(struct thread_data *td,
130 struct fio_file *f, enum fio_ddir ddir,
131 unsigned long long *b)
133 *b = pareto_next(&f->zipf);
137 static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
138 enum fio_ddir ddir, unsigned long long *b)
140 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
141 return __get_next_rand_offset(td, f, ddir, b);
142 else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
143 return __get_next_rand_offset_zipf(td, f, ddir, b);
144 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
145 return __get_next_rand_offset_pareto(td, f, ddir, b);
147 log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
151 static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
152 enum fio_ddir ddir, unsigned long long *b)
154 if (!get_next_rand_offset(td, f, ddir, b))
157 if (td->o.time_based) {
159 if (!get_next_rand_offset(td, f, ddir, b))
163 dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
164 f->file_name, f->last_pos, f->real_file_size);
168 static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
169 enum fio_ddir ddir, unsigned long long *offset)
171 assert(ddir_rw(ddir));
173 if (f->last_pos >= f->io_size + get_start_offset(td) && td->o.time_based)
174 f->last_pos = f->last_pos - f->io_size;
176 if (f->last_pos < f->real_file_size) {
177 unsigned long long pos;
179 if (f->last_pos == f->file_offset && td->o.ddir_seq_add < 0)
180 f->last_pos = f->real_file_size;
182 pos = f->last_pos - f->file_offset;
184 pos += td->o.ddir_seq_add;
193 static int get_next_block(struct thread_data *td, struct io_u *io_u,
194 enum fio_ddir ddir, int rw_seq)
196 struct fio_file *f = io_u->file;
197 unsigned long long b, offset;
200 assert(ddir_rw(ddir));
206 ret = get_next_rand_block(td, f, ddir, &b);
208 ret = get_next_seq_offset(td, f, ddir, &offset);
210 io_u->flags |= IO_U_F_BUSY_OK;
212 if (td->o.rw_seq == RW_SEQ_SEQ) {
213 ret = get_next_seq_offset(td, f, ddir, &offset);
215 ret = get_next_rand_block(td, f, ddir, &b);
216 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
217 if (f->last_start != -1ULL)
218 offset = f->last_start - f->file_offset;
223 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
230 io_u->offset = offset;
232 io_u->offset = b * td->o.ba[ddir];
234 log_err("fio: bug in offset generation: offset=%llu, b=%llu\n",
244 * For random io, generate a random new block and see if it's used. Repeat
245 * until we find a free one. For sequential io, just return the end of
246 * the last io issued.
248 static int __get_next_offset(struct thread_data *td, struct io_u *io_u)
250 struct fio_file *f = io_u->file;
251 enum fio_ddir ddir = io_u->ddir;
254 assert(ddir_rw(ddir));
256 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
258 td->ddir_seq_nr = td->o.ddir_seq_nr;
261 if (get_next_block(td, io_u, ddir, rw_seq_hit))
264 if (io_u->offset >= f->io_size) {
265 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
266 io_u->offset, f->io_size);
270 io_u->offset += f->file_offset;
271 if (io_u->offset >= f->real_file_size) {
272 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
273 io_u->offset, f->real_file_size);
280 static int get_next_offset(struct thread_data *td, struct io_u *io_u)
282 struct prof_io_ops *ops = &td->prof_io_ops;
284 if (ops->fill_io_u_off)
285 return ops->fill_io_u_off(td, io_u);
287 return __get_next_offset(td, io_u);
290 static inline int io_u_fits(struct thread_data *td, struct io_u *io_u,
293 struct fio_file *f = io_u->file;
295 return io_u->offset + buflen <= f->io_size + get_start_offset(td);
298 static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u)
300 const int ddir = io_u->ddir;
301 unsigned int buflen = 0;
302 unsigned int minbs, maxbs;
303 unsigned long r, rand_max;
305 assert(ddir_rw(ddir));
307 minbs = td->o.min_bs[ddir];
308 maxbs = td->o.max_bs[ddir];
314 * If we can't satisfy the min block size from here, then fail
316 if (!io_u_fits(td, io_u, minbs))
319 if (td->o.use_os_rand)
320 rand_max = OS_RAND_MAX;
322 rand_max = FRAND_MAX;
325 if (td->o.use_os_rand)
326 r = os_random_long(&td->bsrange_state);
328 r = __rand(&td->__bsrange_state);
330 if (!td->o.bssplit_nr[ddir]) {
331 buflen = 1 + (unsigned int) ((double) maxbs *
332 (r / (rand_max + 1.0)));
339 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
340 struct bssplit *bsp = &td->o.bssplit[ddir][i];
344 if ((r <= ((rand_max / 100L) * perc)) &&
345 io_u_fits(td, io_u, buflen))
350 if (!td->o.bs_unaligned && is_power_of_2(minbs))
351 buflen = (buflen + minbs - 1) & ~(minbs - 1);
353 } while (!io_u_fits(td, io_u, buflen));
358 static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
360 struct prof_io_ops *ops = &td->prof_io_ops;
362 if (ops->fill_io_u_size)
363 return ops->fill_io_u_size(td, io_u);
365 return __get_next_buflen(td, io_u);
368 static void set_rwmix_bytes(struct thread_data *td)
373 * we do time or byte based switch. this is needed because
374 * buffered writes may issue a lot quicker than they complete,
375 * whereas reads do not.
377 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
378 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
381 static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
386 if (td->o.use_os_rand) {
387 r = os_random_long(&td->rwmix_state);
388 v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
390 r = __rand(&td->__rwmix_state);
391 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
394 if (v <= td->o.rwmix[DDIR_READ])
400 static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
402 enum fio_ddir odir = ddir ^ 1;
406 assert(ddir_rw(ddir));
408 if (td->rate_pending_usleep[ddir] <= 0)
412 * We have too much pending sleep in this direction. See if we
417 * Other direction does not have too much pending, switch
419 if (td->rate_pending_usleep[odir] < 100000)
423 * Both directions have pending sleep. Sleep the minimum time
424 * and deduct from both.
426 if (td->rate_pending_usleep[ddir] <=
427 td->rate_pending_usleep[odir]) {
428 usec = td->rate_pending_usleep[ddir];
430 usec = td->rate_pending_usleep[odir];
434 usec = td->rate_pending_usleep[ddir];
437 * We are going to sleep, ensure that we flush anything pending as
438 * not to skew our latency numbers.
440 * Changed to only monitor 'in flight' requests here instead of the
441 * td->cur_depth, b/c td->cur_depth does not accurately represent
442 * io's that have been actually submitted to an async engine,
443 * and cur_depth is meaningless for sync engines.
445 if (td->io_u_in_flight) {
448 ret = io_u_queued_complete(td, td->io_u_in_flight, NULL);
451 fio_gettime(&t, NULL);
452 usec_sleep(td, usec);
453 usec = utime_since_now(&t);
455 td->rate_pending_usleep[ddir] -= usec;
458 if (td_rw(td) && __should_check_rate(td, odir))
459 td->rate_pending_usleep[odir] -= usec;
467 * Return the data direction for the next io_u. If the job is a
468 * mixed read/write workload, check the rwmix cycle and switch if
471 static enum fio_ddir get_rw_ddir(struct thread_data *td)
476 * see if it's time to fsync
478 if (td->o.fsync_blocks &&
479 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
480 td->io_issues[DDIR_WRITE] && should_fsync(td))
484 * see if it's time to fdatasync
486 if (td->o.fdatasync_blocks &&
487 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
488 td->io_issues[DDIR_WRITE] && should_fsync(td))
489 return DDIR_DATASYNC;
492 * see if it's time to sync_file_range
494 if (td->sync_file_range_nr &&
495 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
496 td->io_issues[DDIR_WRITE] && should_fsync(td))
497 return DDIR_SYNC_FILE_RANGE;
501 * Check if it's time to seed a new data direction.
503 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
505 * Put a top limit on how many bytes we do for
506 * one data direction, to avoid overflowing the
509 ddir = get_rand_ddir(td);
511 if (ddir != td->rwmix_ddir)
514 td->rwmix_ddir = ddir;
516 ddir = td->rwmix_ddir;
517 } else if (td_read(td))
519 else if (td_write(td))
524 td->rwmix_ddir = rate_ddir(td, ddir);
525 return td->rwmix_ddir;
528 static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
530 io_u->ddir = get_rw_ddir(td);
532 if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
533 td->o.barrier_blocks &&
534 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
535 td->io_issues[DDIR_WRITE])
536 io_u->flags |= IO_U_F_BARRIER;
539 void put_file_log(struct thread_data *td, struct fio_file *f)
541 int ret = put_file(td, f);
544 td_verror(td, ret, "file close");
547 void put_io_u(struct thread_data *td, struct io_u *io_u)
551 if (io_u->file && !(io_u->flags & IO_U_F_FREE_DEF))
552 put_file_log(td, io_u->file);
554 io_u->flags &= ~IO_U_F_FREE_DEF;
555 io_u->flags |= IO_U_F_FREE;
557 if (io_u->flags & IO_U_F_IN_CUR_DEPTH)
559 flist_del_init(&io_u->list);
560 flist_add(&io_u->list, &td->io_u_freelist);
562 td_io_u_free_notify(td);
565 void clear_io_u(struct thread_data *td, struct io_u *io_u)
567 io_u->flags &= ~IO_U_F_FLIGHT;
571 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
573 struct io_u *__io_u = *io_u;
575 dprint(FD_IO, "requeue %p\n", __io_u);
579 __io_u->flags |= IO_U_F_FREE;
580 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(__io_u->ddir))
581 td->io_issues[__io_u->ddir]--;
583 __io_u->flags &= ~IO_U_F_FLIGHT;
584 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH)
586 flist_del(&__io_u->list);
587 flist_add_tail(&__io_u->list, &td->io_u_requeues);
592 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
594 if (td->io_ops->flags & FIO_NOIO)
597 set_rw_ddir(td, io_u);
600 * fsync() or fdatasync() or trim etc, we are done
602 if (!ddir_rw(io_u->ddir))
606 * See if it's time to switch to a new zone
608 if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
610 io_u->file->file_offset += td->o.zone_range + td->o.zone_skip;
611 io_u->file->last_pos = io_u->file->file_offset;
612 td->io_skip_bytes += td->o.zone_skip;
616 * No log, let the seq/rand engine retrieve the next buflen and
619 if (get_next_offset(td, io_u)) {
620 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
624 io_u->buflen = get_next_buflen(td, io_u);
626 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
630 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
631 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
632 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
633 io_u->buflen, io_u->file->real_file_size);
638 * mark entry before potentially trimming io_u
640 if (td_random(td) && file_randommap(td, io_u->file))
641 mark_random_map(td, io_u);
644 * If using a write iolog, store this entry.
647 dprint_io_u(io_u, "fill_io_u");
648 td->zone_bytes += io_u->buflen;
653 static void __io_u_mark_map(unsigned int *map, unsigned int nr)
682 void io_u_mark_submit(struct thread_data *td, unsigned int nr)
684 __io_u_mark_map(td->ts.io_u_submit, nr);
685 td->ts.total_submit++;
688 void io_u_mark_complete(struct thread_data *td, unsigned int nr)
690 __io_u_mark_map(td->ts.io_u_complete, nr);
691 td->ts.total_complete++;
694 void io_u_mark_depth(struct thread_data *td, unsigned int nr)
698 switch (td->cur_depth) {
720 td->ts.io_u_map[idx] += nr;
723 static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
760 assert(idx < FIO_IO_U_LAT_U_NR);
761 td->ts.io_u_lat_u[idx]++;
764 static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
805 assert(idx < FIO_IO_U_LAT_M_NR);
806 td->ts.io_u_lat_m[idx]++;
809 static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
812 io_u_mark_lat_usec(td, usec);
814 io_u_mark_lat_msec(td, usec / 1000);
818 * Get next file to service by choosing one at random
820 static struct fio_file *get_next_file_rand(struct thread_data *td,
821 enum fio_file_flags goodf,
822 enum fio_file_flags badf)
831 if (td->o.use_os_rand) {
832 r = os_random_long(&td->next_file_state);
833 fno = (unsigned int) ((double) td->o.nr_files
834 * (r / (OS_RAND_MAX + 1.0)));
836 r = __rand(&td->__next_file_state);
837 fno = (unsigned int) ((double) td->o.nr_files
838 * (r / (FRAND_MAX + 1.0)));
842 if (fio_file_done(f))
845 if (!fio_file_open(f)) {
848 err = td_io_open_file(td, f);
854 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
855 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
859 td_io_close_file(td, f);
864 * Get next file to service by doing round robin between all available ones
866 static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
869 unsigned int old_next_file = td->next_file;
875 f = td->files[td->next_file];
878 if (td->next_file >= td->o.nr_files)
881 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
882 if (fio_file_done(f)) {
887 if (!fio_file_open(f)) {
890 err = td_io_open_file(td, f);
892 dprint(FD_FILE, "error %d on open of %s\n",
900 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
902 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
906 td_io_close_file(td, f);
909 } while (td->next_file != old_next_file);
911 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
915 static struct fio_file *__get_next_file(struct thread_data *td)
919 assert(td->o.nr_files <= td->files_index);
921 if (td->nr_done_files >= td->o.nr_files) {
922 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
923 " nr_files=%d\n", td->nr_open_files,
929 f = td->file_service_file;
930 if (f && fio_file_open(f) && !fio_file_closing(f)) {
931 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
933 if (td->file_service_left--)
937 if (td->o.file_service_type == FIO_FSERVICE_RR ||
938 td->o.file_service_type == FIO_FSERVICE_SEQ)
939 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
941 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
943 td->file_service_file = f;
944 td->file_service_left = td->file_service_nr - 1;
946 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
950 static struct fio_file *get_next_file(struct thread_data *td)
952 struct prof_io_ops *ops = &td->prof_io_ops;
954 if (ops->get_next_file)
955 return ops->get_next_file(td);
957 return __get_next_file(td);
960 static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
965 f = get_next_file(td);
972 if (!fill_io_u(td, io_u))
976 td_io_close_file(td, f);
978 fio_file_set_done(f);
980 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
981 td->nr_done_files, td->o.nr_files);
988 struct io_u *__get_io_u(struct thread_data *td)
990 struct io_u *io_u = NULL;
995 if (!flist_empty(&td->io_u_requeues))
996 io_u = flist_entry(td->io_u_requeues.next, struct io_u, list);
997 else if (!queue_full(td)) {
998 io_u = flist_entry(td->io_u_freelist.next, struct io_u, list);
1003 io_u->end_io = NULL;
1007 assert(io_u->flags & IO_U_F_FREE);
1008 io_u->flags &= ~(IO_U_F_FREE | IO_U_F_FREE_DEF);
1009 io_u->flags &= ~(IO_U_F_TRIMMED | IO_U_F_BARRIER);
1010 io_u->flags &= ~IO_U_F_VER_LIST;
1013 flist_del(&io_u->list);
1014 flist_add_tail(&io_u->list, &td->io_u_busylist);
1016 io_u->flags |= IO_U_F_IN_CUR_DEPTH;
1017 } else if (td->o.verify_async) {
1019 * We ran out, wait for async verify threads to finish and
1022 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1030 static int check_get_trim(struct thread_data *td, struct io_u *io_u)
1032 if (td->o.trim_backlog && td->trim_entries) {
1035 if (td->trim_batch) {
1038 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1039 td->last_ddir != DDIR_READ) {
1040 td->trim_batch = td->o.trim_batch;
1041 if (!td->trim_batch)
1042 td->trim_batch = td->o.trim_backlog;
1046 if (get_trim && !get_next_trim(td, io_u))
1053 static int check_get_verify(struct thread_data *td, struct io_u *io_u)
1055 if (td->o.verify_backlog && td->io_hist_len) {
1058 if (td->verify_batch)
1060 else if (!(td->io_hist_len % td->o.verify_backlog) &&
1061 td->last_ddir != DDIR_READ) {
1062 td->verify_batch = td->o.verify_batch;
1063 if (!td->verify_batch)
1064 td->verify_batch = td->o.verify_backlog;
1068 if (get_verify && !get_next_verify(td, io_u)) {
1078 * Fill offset and start time into the buffer content, to prevent too
1079 * easy compressible data for simple de-dupe attempts. Do this for every
1080 * 512b block in the range, since that should be the smallest block size
1081 * we can expect from a device.
1083 static void small_content_scramble(struct io_u *io_u)
1085 unsigned int i, nr_blocks = io_u->buflen / 512;
1086 unsigned long long boffset;
1087 unsigned int offset;
1094 boffset = io_u->offset;
1095 io_u->buf_filled_len = 0;
1097 for (i = 0; i < nr_blocks; i++) {
1099 * Fill the byte offset into a "random" start offset of
1100 * the buffer, given by the product of the usec time
1101 * and the actual offset.
1103 offset = (io_u->start_time.tv_usec ^ boffset) & 511;
1104 offset &= ~(sizeof(unsigned long long) - 1);
1105 if (offset >= 512 - sizeof(unsigned long long))
1106 offset -= sizeof(unsigned long long);
1107 memcpy(p + offset, &boffset, sizeof(boffset));
1109 end = p + 512 - sizeof(io_u->start_time);
1110 memcpy(end, &io_u->start_time, sizeof(io_u->start_time));
1117 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1118 * etc. The returned io_u is fully ready to be prepped and submitted.
1120 struct io_u *get_io_u(struct thread_data *td)
1124 int do_scramble = 0;
1126 io_u = __get_io_u(td);
1128 dprint(FD_IO, "__get_io_u failed\n");
1132 if (check_get_verify(td, io_u))
1134 if (check_get_trim(td, io_u))
1138 * from a requeue, io_u already setup
1144 * If using an iolog, grab next piece if any available.
1146 if (td->o.read_iolog_file) {
1147 if (read_iolog_get(td, io_u))
1149 } else if (set_io_u_file(td, io_u)) {
1150 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1155 assert(fio_file_open(f));
1157 if (ddir_rw(io_u->ddir)) {
1158 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
1159 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
1163 f->last_start = io_u->offset;
1164 f->last_pos = io_u->offset + io_u->buflen;
1166 if (io_u->ddir == DDIR_WRITE) {
1167 if (td->o.refill_buffers) {
1168 io_u_fill_buffer(td, io_u,
1169 io_u->xfer_buflen, io_u->xfer_buflen);
1170 } else if (td->o.scramble_buffers)
1172 if (td->o.verify != VERIFY_NONE) {
1173 populate_verify_io_u(td, io_u);
1176 } else if (io_u->ddir == DDIR_READ) {
1178 * Reset the buf_filled parameters so next time if the
1179 * buffer is used for writes it is refilled.
1181 io_u->buf_filled_len = 0;
1186 * Set io data pointers.
1188 io_u->xfer_buf = io_u->buf;
1189 io_u->xfer_buflen = io_u->buflen;
1193 if (!td_io_prep(td, io_u)) {
1194 if (!td->o.disable_slat)
1195 fio_gettime(&io_u->start_time, NULL);
1197 small_content_scramble(io_u);
1201 dprint(FD_IO, "get_io_u failed\n");
1206 void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1208 enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
1209 const char *msg[] = { "read", "write", "sync", "datasync",
1210 "sync_file_range", "wait", "trim" };
1212 if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1215 log_err("fio: io_u error");
1218 log_err(" on file %s", io_u->file->file_name);
1220 log_err(": %s\n", strerror(io_u->error));
1222 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
1223 io_u->offset, io_u->xfer_buflen);
1226 td_verror(td, io_u->error, "io_u error");
1229 static void account_io_completion(struct thread_data *td, struct io_u *io_u,
1230 struct io_completion_data *icd,
1231 const enum fio_ddir idx, unsigned int bytes)
1233 unsigned long lusec = 0;
1235 if (!td->o.disable_clat || !td->o.disable_bw)
1236 lusec = utime_since(&io_u->issue_time, &icd->time);
1238 if (!td->o.disable_lat) {
1239 unsigned long tusec;
1241 tusec = utime_since(&io_u->start_time, &icd->time);
1242 add_lat_sample(td, idx, tusec, bytes);
1244 if (td->o.max_latency && tusec > td->o.max_latency) {
1246 log_err("fio: latency of %lu usec exceeds specified max (%u usec)\n", tusec, td->o.max_latency);
1247 td_verror(td, ETIMEDOUT, "max latency exceeded");
1248 icd->error = ETIMEDOUT;
1252 if (!td->o.disable_clat) {
1253 add_clat_sample(td, idx, lusec, bytes);
1254 io_u_mark_latency(td, lusec);
1257 if (!td->o.disable_bw)
1258 add_bw_sample(td, idx, bytes, &icd->time);
1260 add_iops_sample(td, idx, &icd->time);
1263 static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
1265 unsigned long long secs, remainder, bps, bytes;
1266 bytes = td->this_io_bytes[ddir];
1267 bps = td->rate_bps[ddir];
1269 remainder = bytes % bps;
1270 return remainder * 1000000 / bps + secs * 1000000;
1273 static void io_completed(struct thread_data *td, struct io_u *io_u,
1274 struct io_completion_data *icd)
1278 dprint_io_u(io_u, "io complete");
1281 assert(io_u->flags & IO_U_F_FLIGHT);
1282 io_u->flags &= ~(IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
1285 if (ddir_sync(io_u->ddir)) {
1286 td->last_was_sync = 1;
1289 f->first_write = -1ULL;
1290 f->last_write = -1ULL;
1295 td->last_was_sync = 0;
1296 td->last_ddir = io_u->ddir;
1298 if (!io_u->error && ddir_rw(io_u->ddir)) {
1299 unsigned int bytes = io_u->buflen - io_u->resid;
1300 const enum fio_ddir idx = io_u->ddir;
1301 const enum fio_ddir odx = io_u->ddir ^ 1;
1304 td->io_blocks[idx]++;
1305 td->this_io_blocks[idx]++;
1306 td->io_bytes[idx] += bytes;
1308 if (!(io_u->flags & IO_U_F_VER_LIST))
1309 td->this_io_bytes[idx] += bytes;
1311 if (idx == DDIR_WRITE) {
1314 if (f->first_write == -1ULL ||
1315 io_u->offset < f->first_write)
1316 f->first_write = io_u->offset;
1317 if (f->last_write == -1ULL ||
1318 ((io_u->offset + bytes) > f->last_write))
1319 f->last_write = io_u->offset + bytes;
1323 if (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
1324 td->runstate == TD_VERIFYING)) {
1325 account_io_completion(td, io_u, icd, idx, bytes);
1327 if (__should_check_rate(td, idx)) {
1328 td->rate_pending_usleep[idx] =
1329 (usec_for_io(td, idx) -
1330 utime_since_now(&td->start));
1332 if (idx != DDIR_TRIM && __should_check_rate(td, odx))
1333 td->rate_pending_usleep[odx] =
1334 (usec_for_io(td, odx) -
1335 utime_since_now(&td->start));
1338 if (td_write(td) && idx == DDIR_WRITE &&
1340 td->o.verify != VERIFY_NONE)
1341 log_io_piece(td, io_u);
1343 icd->bytes_done[idx] += bytes;
1346 ret = io_u->end_io(td, io_u);
1347 if (ret && !icd->error)
1350 } else if (io_u->error) {
1351 icd->error = io_u->error;
1352 io_u_log_error(td, io_u);
1355 enum error_type_bit eb = td_error_type(io_u->ddir, icd->error);
1356 if (!td_non_fatal_error(td, eb, icd->error))
1359 * If there is a non_fatal error, then add to the error count
1360 * and clear all the errors.
1362 update_error_count(td, icd->error);
1369 static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1373 if (!td->o.disable_clat || !td->o.disable_bw)
1374 fio_gettime(&icd->time, NULL);
1379 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1380 icd->bytes_done[ddir] = 0;
1383 static void ios_completed(struct thread_data *td,
1384 struct io_completion_data *icd)
1389 for (i = 0; i < icd->nr; i++) {
1390 io_u = td->io_ops->event(td, i);
1392 io_completed(td, io_u, icd);
1394 if (!(io_u->flags & IO_U_F_FREE_DEF))
1400 * Complete a single io_u for the sync engines.
1402 int io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
1403 unsigned long *bytes)
1405 struct io_completion_data icd;
1407 init_icd(td, &icd, 1);
1408 io_completed(td, io_u, &icd);
1410 if (!(io_u->flags & IO_U_F_FREE_DEF))
1414 td_verror(td, icd.error, "io_u_sync_complete");
1421 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1422 bytes[ddir] += icd.bytes_done[ddir];
1429 * Called to complete min_events number of io for the async engines.
1431 int io_u_queued_complete(struct thread_data *td, int min_evts,
1432 unsigned long *bytes)
1434 struct io_completion_data icd;
1435 struct timespec *tvp = NULL;
1437 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
1439 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
1444 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete, tvp);
1446 td_verror(td, -ret, "td_io_getevents");
1451 init_icd(td, &icd, ret);
1452 ios_completed(td, &icd);
1454 td_verror(td, icd.error, "io_u_queued_complete");
1461 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1462 bytes[ddir] += icd.bytes_done[ddir];
1469 * Call when io_u is really queued, to update the submission latency.
1471 void io_u_queued(struct thread_data *td, struct io_u *io_u)
1473 if (!td->o.disable_slat) {
1474 unsigned long slat_time;
1476 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
1477 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen);
1482 * "randomly" fill the buffer contents
1484 void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1485 unsigned int min_write, unsigned int max_bs)
1487 io_u->buf_filled_len = 0;
1489 if (!td->o.zero_buffers) {
1490 unsigned int perc = td->o.compress_percentage;
1493 unsigned int seg = min_write;
1495 seg = min(min_write, td->o.compress_chunk);
1496 fill_random_buf_percentage(&td->buf_state, io_u->buf,
1499 fill_random_buf(&td->buf_state, io_u->buf, max_bs);
1501 memset(io_u->buf, 0, max_bs);