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 if (td->flags & TD_F_PROFILE_OPS) {
294 struct prof_io_ops *ops = &td->prof_io_ops;
296 if (ops->fill_io_u_off)
297 return ops->fill_io_u_off(td, io_u);
300 return __get_next_offset(td, io_u);
303 static inline int io_u_fits(struct thread_data *td, struct io_u *io_u,
306 struct fio_file *f = io_u->file;
308 return io_u->offset + buflen <= f->io_size + get_start_offset(td);
311 static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u)
313 const int ddir = io_u->ddir;
314 unsigned int buflen = 0;
315 unsigned int minbs, maxbs;
316 unsigned long r, rand_max;
318 assert(ddir_rw(ddir));
320 minbs = td->o.min_bs[ddir];
321 maxbs = td->o.max_bs[ddir];
327 * If we can't satisfy the min block size from here, then fail
329 if (!io_u_fits(td, io_u, minbs))
332 if (td->o.use_os_rand)
333 rand_max = OS_RAND_MAX;
335 rand_max = FRAND_MAX;
338 if (td->o.use_os_rand)
339 r = os_random_long(&td->bsrange_state);
341 r = __rand(&td->__bsrange_state);
343 if (!td->o.bssplit_nr[ddir]) {
344 buflen = 1 + (unsigned int) ((double) maxbs *
345 (r / (rand_max + 1.0)));
352 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
353 struct bssplit *bsp = &td->o.bssplit[ddir][i];
357 if ((r <= ((rand_max / 100L) * perc)) &&
358 io_u_fits(td, io_u, buflen))
363 if (!td->o.bs_unaligned && is_power_of_2(minbs))
364 buflen = (buflen + minbs - 1) & ~(minbs - 1);
366 } while (!io_u_fits(td, io_u, buflen));
371 static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
373 if (td->flags & TD_F_PROFILE_OPS) {
374 struct prof_io_ops *ops = &td->prof_io_ops;
376 if (ops->fill_io_u_size)
377 return ops->fill_io_u_size(td, io_u);
380 return __get_next_buflen(td, io_u);
383 static void set_rwmix_bytes(struct thread_data *td)
388 * we do time or byte based switch. this is needed because
389 * buffered writes may issue a lot quicker than they complete,
390 * whereas reads do not.
392 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
393 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
396 static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
401 if (td->o.use_os_rand) {
402 r = os_random_long(&td->rwmix_state);
403 v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
405 r = __rand(&td->__rwmix_state);
406 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
409 if (v <= td->o.rwmix[DDIR_READ])
415 static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
417 enum fio_ddir odir = ddir ^ 1;
421 assert(ddir_rw(ddir));
423 if (td->rate_pending_usleep[ddir] <= 0)
427 * We have too much pending sleep in this direction. See if we
432 * Other direction does not have too much pending, switch
434 if (td->rate_pending_usleep[odir] < 100000)
438 * Both directions have pending sleep. Sleep the minimum time
439 * and deduct from both.
441 if (td->rate_pending_usleep[ddir] <=
442 td->rate_pending_usleep[odir]) {
443 usec = td->rate_pending_usleep[ddir];
445 usec = td->rate_pending_usleep[odir];
449 usec = td->rate_pending_usleep[ddir];
452 * We are going to sleep, ensure that we flush anything pending as
453 * not to skew our latency numbers.
455 * Changed to only monitor 'in flight' requests here instead of the
456 * td->cur_depth, b/c td->cur_depth does not accurately represent
457 * io's that have been actually submitted to an async engine,
458 * and cur_depth is meaningless for sync engines.
460 if (td->io_u_in_flight) {
463 ret = io_u_queued_complete(td, td->io_u_in_flight, NULL);
466 fio_gettime(&t, NULL);
467 usec_sleep(td, usec);
468 usec = utime_since_now(&t);
470 td->rate_pending_usleep[ddir] -= usec;
473 if (td_rw(td) && __should_check_rate(td, odir))
474 td->rate_pending_usleep[odir] -= usec;
482 * Return the data direction for the next io_u. If the job is a
483 * mixed read/write workload, check the rwmix cycle and switch if
486 static enum fio_ddir get_rw_ddir(struct thread_data *td)
491 * see if it's time to fsync
493 if (td->o.fsync_blocks &&
494 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
495 td->io_issues[DDIR_WRITE] && should_fsync(td))
499 * see if it's time to fdatasync
501 if (td->o.fdatasync_blocks &&
502 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
503 td->io_issues[DDIR_WRITE] && should_fsync(td))
504 return DDIR_DATASYNC;
507 * see if it's time to sync_file_range
509 if (td->sync_file_range_nr &&
510 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
511 td->io_issues[DDIR_WRITE] && should_fsync(td))
512 return DDIR_SYNC_FILE_RANGE;
516 * Check if it's time to seed a new data direction.
518 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
520 * Put a top limit on how many bytes we do for
521 * one data direction, to avoid overflowing the
524 ddir = get_rand_ddir(td);
526 if (ddir != td->rwmix_ddir)
529 td->rwmix_ddir = ddir;
531 ddir = td->rwmix_ddir;
532 } else if (td_read(td))
534 else if (td_write(td))
539 td->rwmix_ddir = rate_ddir(td, ddir);
540 return td->rwmix_ddir;
543 static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
545 io_u->ddir = get_rw_ddir(td);
547 if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
548 td->o.barrier_blocks &&
549 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
550 td->io_issues[DDIR_WRITE])
551 io_u->flags |= IO_U_F_BARRIER;
554 void put_file_log(struct thread_data *td, struct fio_file *f)
556 int ret = put_file(td, f);
559 td_verror(td, ret, "file close");
562 void put_io_u(struct thread_data *td, struct io_u *io_u)
566 if (io_u->file && !(io_u->flags & IO_U_F_FREE_DEF))
567 put_file_log(td, io_u->file);
569 io_u->flags &= ~IO_U_F_FREE_DEF;
570 io_u->flags |= IO_U_F_FREE;
572 if (io_u->flags & IO_U_F_IN_CUR_DEPTH)
574 flist_del_init(&io_u->list);
575 flist_add(&io_u->list, &td->io_u_freelist);
577 td_io_u_free_notify(td);
580 void clear_io_u(struct thread_data *td, struct io_u *io_u)
582 io_u->flags &= ~IO_U_F_FLIGHT;
586 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
588 struct io_u *__io_u = *io_u;
590 dprint(FD_IO, "requeue %p\n", __io_u);
594 __io_u->flags |= IO_U_F_FREE;
595 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(__io_u->ddir))
596 td->io_issues[__io_u->ddir]--;
598 __io_u->flags &= ~IO_U_F_FLIGHT;
599 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH)
601 flist_del(&__io_u->list);
602 flist_add_tail(&__io_u->list, &td->io_u_requeues);
607 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
609 if (td->io_ops->flags & FIO_NOIO)
612 set_rw_ddir(td, io_u);
615 * fsync() or fdatasync() or trim etc, we are done
617 if (!ddir_rw(io_u->ddir))
621 * See if it's time to switch to a new zone
623 if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
625 io_u->file->file_offset += td->o.zone_range + td->o.zone_skip;
626 io_u->file->last_pos = io_u->file->file_offset;
627 td->io_skip_bytes += td->o.zone_skip;
631 * No log, let the seq/rand engine retrieve the next buflen and
634 if (get_next_offset(td, io_u)) {
635 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
639 io_u->buflen = get_next_buflen(td, io_u);
641 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
645 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
646 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
647 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
648 io_u->buflen, io_u->file->real_file_size);
653 * mark entry before potentially trimming io_u
655 if (td_random(td) && file_randommap(td, io_u->file))
656 mark_random_map(td, io_u);
659 * If using a write iolog, store this entry.
662 dprint_io_u(io_u, "fill_io_u");
663 td->zone_bytes += io_u->buflen;
668 static void __io_u_mark_map(unsigned int *map, unsigned int nr)
697 void io_u_mark_submit(struct thread_data *td, unsigned int nr)
699 __io_u_mark_map(td->ts.io_u_submit, nr);
700 td->ts.total_submit++;
703 void io_u_mark_complete(struct thread_data *td, unsigned int nr)
705 __io_u_mark_map(td->ts.io_u_complete, nr);
706 td->ts.total_complete++;
709 void io_u_mark_depth(struct thread_data *td, unsigned int nr)
713 switch (td->cur_depth) {
735 td->ts.io_u_map[idx] += nr;
738 static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
775 assert(idx < FIO_IO_U_LAT_U_NR);
776 td->ts.io_u_lat_u[idx]++;
779 static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
820 assert(idx < FIO_IO_U_LAT_M_NR);
821 td->ts.io_u_lat_m[idx]++;
824 static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
827 io_u_mark_lat_usec(td, usec);
829 io_u_mark_lat_msec(td, usec / 1000);
833 * Get next file to service by choosing one at random
835 static struct fio_file *get_next_file_rand(struct thread_data *td,
836 enum fio_file_flags goodf,
837 enum fio_file_flags badf)
846 if (td->o.use_os_rand) {
847 r = os_random_long(&td->next_file_state);
848 fno = (unsigned int) ((double) td->o.nr_files
849 * (r / (OS_RAND_MAX + 1.0)));
851 r = __rand(&td->__next_file_state);
852 fno = (unsigned int) ((double) td->o.nr_files
853 * (r / (FRAND_MAX + 1.0)));
857 if (fio_file_done(f))
860 if (!fio_file_open(f)) {
863 err = td_io_open_file(td, f);
869 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
870 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
874 td_io_close_file(td, f);
879 * Get next file to service by doing round robin between all available ones
881 static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
884 unsigned int old_next_file = td->next_file;
890 f = td->files[td->next_file];
893 if (td->next_file >= td->o.nr_files)
896 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
897 if (fio_file_done(f)) {
902 if (!fio_file_open(f)) {
905 err = td_io_open_file(td, f);
907 dprint(FD_FILE, "error %d on open of %s\n",
915 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
917 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
921 td_io_close_file(td, f);
924 } while (td->next_file != old_next_file);
926 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
930 static struct fio_file *__get_next_file(struct thread_data *td)
934 assert(td->o.nr_files <= td->files_index);
936 if (td->nr_done_files >= td->o.nr_files) {
937 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
938 " nr_files=%d\n", td->nr_open_files,
944 f = td->file_service_file;
945 if (f && fio_file_open(f) && !fio_file_closing(f)) {
946 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
948 if (td->file_service_left--)
952 if (td->o.file_service_type == FIO_FSERVICE_RR ||
953 td->o.file_service_type == FIO_FSERVICE_SEQ)
954 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
956 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
958 td->file_service_file = f;
959 td->file_service_left = td->file_service_nr - 1;
961 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
965 static struct fio_file *get_next_file(struct thread_data *td)
967 if (!(td->flags & TD_F_PROFILE_OPS)) {
968 struct prof_io_ops *ops = &td->prof_io_ops;
970 if (ops->get_next_file)
971 return ops->get_next_file(td);
974 return __get_next_file(td);
977 static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
982 f = get_next_file(td);
989 if (!fill_io_u(td, io_u))
993 td_io_close_file(td, f);
995 fio_file_set_done(f);
997 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
998 td->nr_done_files, td->o.nr_files);
1005 struct io_u *__get_io_u(struct thread_data *td)
1007 struct io_u *io_u = NULL;
1012 if (!flist_empty(&td->io_u_requeues))
1013 io_u = flist_entry(td->io_u_requeues.next, struct io_u, list);
1014 else if (!queue_full(td)) {
1015 io_u = flist_entry(td->io_u_freelist.next, struct io_u, list);
1020 io_u->end_io = NULL;
1024 assert(io_u->flags & IO_U_F_FREE);
1025 io_u->flags &= ~(IO_U_F_FREE | IO_U_F_FREE_DEF);
1026 io_u->flags &= ~(IO_U_F_TRIMMED | IO_U_F_BARRIER);
1027 io_u->flags &= ~IO_U_F_VER_LIST;
1030 flist_del(&io_u->list);
1031 flist_add_tail(&io_u->list, &td->io_u_busylist);
1033 io_u->flags |= IO_U_F_IN_CUR_DEPTH;
1034 } else if (td->o.verify_async) {
1036 * We ran out, wait for async verify threads to finish and
1039 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1047 static int check_get_trim(struct thread_data *td, struct io_u *io_u)
1049 if (!(td->flags & TD_F_TRIM_BACKLOG))
1052 if (td->trim_entries) {
1055 if (td->trim_batch) {
1058 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1059 td->last_ddir != DDIR_READ) {
1060 td->trim_batch = td->o.trim_batch;
1061 if (!td->trim_batch)
1062 td->trim_batch = td->o.trim_backlog;
1066 if (get_trim && !get_next_trim(td, io_u))
1073 static int check_get_verify(struct thread_data *td, struct io_u *io_u)
1075 if (!(td->flags & TD_F_VER_BACKLOG))
1078 if (td->io_hist_len) {
1081 if (td->verify_batch)
1083 else if (!(td->io_hist_len % td->o.verify_backlog) &&
1084 td->last_ddir != DDIR_READ) {
1085 td->verify_batch = td->o.verify_batch;
1086 if (!td->verify_batch)
1087 td->verify_batch = td->o.verify_backlog;
1091 if (get_verify && !get_next_verify(td, io_u)) {
1101 * Fill offset and start time into the buffer content, to prevent too
1102 * easy compressible data for simple de-dupe attempts. Do this for every
1103 * 512b block in the range, since that should be the smallest block size
1104 * we can expect from a device.
1106 static void small_content_scramble(struct io_u *io_u)
1108 unsigned int i, nr_blocks = io_u->buflen / 512;
1109 unsigned long long boffset;
1110 unsigned int offset;
1117 boffset = io_u->offset;
1118 io_u->buf_filled_len = 0;
1120 for (i = 0; i < nr_blocks; i++) {
1122 * Fill the byte offset into a "random" start offset of
1123 * the buffer, given by the product of the usec time
1124 * and the actual offset.
1126 offset = (io_u->start_time.tv_usec ^ boffset) & 511;
1127 offset &= ~(sizeof(unsigned long long) - 1);
1128 if (offset >= 512 - sizeof(unsigned long long))
1129 offset -= sizeof(unsigned long long);
1130 memcpy(p + offset, &boffset, sizeof(boffset));
1132 end = p + 512 - sizeof(io_u->start_time);
1133 memcpy(end, &io_u->start_time, sizeof(io_u->start_time));
1140 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1141 * etc. The returned io_u is fully ready to be prepped and submitted.
1143 struct io_u *get_io_u(struct thread_data *td)
1147 int do_scramble = 0;
1149 io_u = __get_io_u(td);
1151 dprint(FD_IO, "__get_io_u failed\n");
1155 if (check_get_verify(td, io_u))
1157 if (check_get_trim(td, io_u))
1161 * from a requeue, io_u already setup
1167 * If using an iolog, grab next piece if any available.
1169 if (td->flags & TD_F_READ_IOLOG) {
1170 if (read_iolog_get(td, io_u))
1172 } else if (set_io_u_file(td, io_u)) {
1173 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1178 assert(fio_file_open(f));
1180 if (ddir_rw(io_u->ddir)) {
1181 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
1182 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
1186 f->last_start = io_u->offset;
1187 f->last_pos = io_u->offset + io_u->buflen;
1189 if (io_u->ddir == DDIR_WRITE) {
1190 if (td->flags & TD_F_REFILL_BUFFERS) {
1191 io_u_fill_buffer(td, io_u,
1192 io_u->xfer_buflen, io_u->xfer_buflen);
1193 } else if (td->flags & TD_F_SCRAMBLE_BUFFERS)
1195 if (td->flags & TD_F_VER_NONE) {
1196 populate_verify_io_u(td, io_u);
1199 } else if (io_u->ddir == DDIR_READ) {
1201 * Reset the buf_filled parameters so next time if the
1202 * buffer is used for writes it is refilled.
1204 io_u->buf_filled_len = 0;
1209 * Set io data pointers.
1211 io_u->xfer_buf = io_u->buf;
1212 io_u->xfer_buflen = io_u->buflen;
1216 if (!td_io_prep(td, io_u)) {
1217 if (!td->o.disable_slat)
1218 fio_gettime(&io_u->start_time, NULL);
1220 small_content_scramble(io_u);
1224 dprint(FD_IO, "get_io_u failed\n");
1229 void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1231 enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
1232 const char *msg[] = { "read", "write", "sync", "datasync",
1233 "sync_file_range", "wait", "trim" };
1235 if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1238 log_err("fio: io_u error");
1241 log_err(" on file %s", io_u->file->file_name);
1243 log_err(": %s\n", strerror(io_u->error));
1245 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
1246 io_u->offset, io_u->xfer_buflen);
1249 td_verror(td, io_u->error, "io_u error");
1252 static void account_io_completion(struct thread_data *td, struct io_u *io_u,
1253 struct io_completion_data *icd,
1254 const enum fio_ddir idx, unsigned int bytes)
1256 unsigned long lusec = 0;
1258 if (!td->o.disable_clat || !td->o.disable_bw)
1259 lusec = utime_since(&io_u->issue_time, &icd->time);
1261 if (!td->o.disable_lat) {
1262 unsigned long tusec;
1264 tusec = utime_since(&io_u->start_time, &icd->time);
1265 add_lat_sample(td, idx, tusec, bytes);
1267 if (td->o.max_latency && tusec > td->o.max_latency) {
1269 log_err("fio: latency of %lu usec exceeds specified max (%u usec)\n", tusec, td->o.max_latency);
1270 td_verror(td, ETIMEDOUT, "max latency exceeded");
1271 icd->error = ETIMEDOUT;
1275 if (!td->o.disable_clat) {
1276 add_clat_sample(td, idx, lusec, bytes);
1277 io_u_mark_latency(td, lusec);
1280 if (!td->o.disable_bw)
1281 add_bw_sample(td, idx, bytes, &icd->time);
1283 add_iops_sample(td, idx, &icd->time);
1286 static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
1288 unsigned long long secs, remainder, bps, bytes;
1289 bytes = td->this_io_bytes[ddir];
1290 bps = td->rate_bps[ddir];
1292 remainder = bytes % bps;
1293 return remainder * 1000000 / bps + secs * 1000000;
1296 static void io_completed(struct thread_data *td, struct io_u *io_u,
1297 struct io_completion_data *icd)
1301 dprint_io_u(io_u, "io complete");
1304 assert(io_u->flags & IO_U_F_FLIGHT);
1305 io_u->flags &= ~(IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
1308 if (ddir_sync(io_u->ddir)) {
1309 td->last_was_sync = 1;
1312 f->first_write = -1ULL;
1313 f->last_write = -1ULL;
1318 td->last_was_sync = 0;
1319 td->last_ddir = io_u->ddir;
1321 if (!io_u->error && ddir_rw(io_u->ddir)) {
1322 unsigned int bytes = io_u->buflen - io_u->resid;
1323 const enum fio_ddir idx = io_u->ddir;
1324 const enum fio_ddir odx = io_u->ddir ^ 1;
1327 td->io_blocks[idx]++;
1328 td->this_io_blocks[idx]++;
1329 td->io_bytes[idx] += bytes;
1331 if (!(io_u->flags & IO_U_F_VER_LIST))
1332 td->this_io_bytes[idx] += bytes;
1334 if (idx == DDIR_WRITE) {
1337 if (f->first_write == -1ULL ||
1338 io_u->offset < f->first_write)
1339 f->first_write = io_u->offset;
1340 if (f->last_write == -1ULL ||
1341 ((io_u->offset + bytes) > f->last_write))
1342 f->last_write = io_u->offset + bytes;
1346 if (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
1347 td->runstate == TD_VERIFYING)) {
1348 account_io_completion(td, io_u, icd, idx, bytes);
1350 if (__should_check_rate(td, idx)) {
1351 td->rate_pending_usleep[idx] =
1352 (usec_for_io(td, idx) -
1353 utime_since_now(&td->start));
1355 if (idx != DDIR_TRIM && __should_check_rate(td, odx))
1356 td->rate_pending_usleep[odx] =
1357 (usec_for_io(td, odx) -
1358 utime_since_now(&td->start));
1361 if (td_write(td) && idx == DDIR_WRITE &&
1363 td->o.verify != VERIFY_NONE)
1364 log_io_piece(td, io_u);
1366 icd->bytes_done[idx] += bytes;
1369 ret = io_u->end_io(td, io_u);
1370 if (ret && !icd->error)
1373 } else if (io_u->error) {
1374 icd->error = io_u->error;
1375 io_u_log_error(td, io_u);
1378 enum error_type_bit eb = td_error_type(io_u->ddir, icd->error);
1379 if (!td_non_fatal_error(td, eb, icd->error))
1382 * If there is a non_fatal error, then add to the error count
1383 * and clear all the errors.
1385 update_error_count(td, icd->error);
1392 static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1396 if (!td->o.disable_clat || !td->o.disable_bw)
1397 fio_gettime(&icd->time, NULL);
1402 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1403 icd->bytes_done[ddir] = 0;
1406 static void ios_completed(struct thread_data *td,
1407 struct io_completion_data *icd)
1412 for (i = 0; i < icd->nr; i++) {
1413 io_u = td->io_ops->event(td, i);
1415 io_completed(td, io_u, icd);
1417 if (!(io_u->flags & IO_U_F_FREE_DEF))
1423 * Complete a single io_u for the sync engines.
1425 int io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
1426 unsigned long *bytes)
1428 struct io_completion_data icd;
1430 init_icd(td, &icd, 1);
1431 io_completed(td, io_u, &icd);
1433 if (!(io_u->flags & IO_U_F_FREE_DEF))
1437 td_verror(td, icd.error, "io_u_sync_complete");
1444 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1445 bytes[ddir] += icd.bytes_done[ddir];
1452 * Called to complete min_events number of io for the async engines.
1454 int io_u_queued_complete(struct thread_data *td, int min_evts,
1455 unsigned long *bytes)
1457 struct io_completion_data icd;
1458 struct timespec *tvp = NULL;
1460 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
1462 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
1467 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete, tvp);
1469 td_verror(td, -ret, "td_io_getevents");
1474 init_icd(td, &icd, ret);
1475 ios_completed(td, &icd);
1477 td_verror(td, icd.error, "io_u_queued_complete");
1484 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1485 bytes[ddir] += icd.bytes_done[ddir];
1492 * Call when io_u is really queued, to update the submission latency.
1494 void io_u_queued(struct thread_data *td, struct io_u *io_u)
1496 if (!td->o.disable_slat) {
1497 unsigned long slat_time;
1499 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
1500 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen);
1505 * "randomly" fill the buffer contents
1507 void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1508 unsigned int min_write, unsigned int max_bs)
1510 io_u->buf_filled_len = 0;
1512 if (!td->o.zero_buffers) {
1513 unsigned int perc = td->o.compress_percentage;
1516 unsigned int seg = min_write;
1518 seg = min(min_write, td->o.compress_chunk);
1519 fill_random_buf_percentage(&td->buf_state, io_u->buf,
1522 fill_random_buf(&td->buf_state, io_u->buf, max_bs);
1524 memset(io_u->buf, 0, max_bs);