14 struct io_completion_data {
17 int error; /* output */
18 uint64_t bytes_done[DDIR_RWDIR_CNT]; /* output */
19 struct timespec time; /* output */
23 * The ->io_axmap 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 bool random_map_free(struct fio_file *f, const uint64_t block)
28 return !axmap_isset(f->io_axmap, block);
32 * Mark a given offset as used in the map.
34 static uint64_t mark_random_map(struct thread_data *td, struct io_u *io_u,
35 uint64_t offset, uint64_t buflen)
37 unsigned long long min_bs = td->o.min_bs[io_u->ddir];
38 struct fio_file *f = io_u->file;
39 unsigned long long nr_blocks;
42 block = (offset - f->file_offset) / (uint64_t) min_bs;
43 nr_blocks = (buflen + min_bs - 1) / min_bs;
44 assert(nr_blocks > 0);
46 if (!(io_u->flags & IO_U_F_BUSY_OK)) {
47 nr_blocks = axmap_set_nr(f->io_axmap, block, nr_blocks);
48 assert(nr_blocks > 0);
51 if ((nr_blocks * min_bs) < buflen)
52 buflen = nr_blocks * min_bs;
57 static uint64_t last_block(struct thread_data *td, struct fio_file *f,
63 assert(ddir_rw(ddir));
66 * Hmm, should we make sure that ->io_size <= ->real_file_size?
67 * -> not for now since there is code assuming it could go either.
69 max_size = f->io_size;
70 if (max_size > f->real_file_size)
71 max_size = f->real_file_size;
73 if (td->o.zone_mode == ZONE_MODE_STRIDED && td->o.zone_range)
74 max_size = td->o.zone_range;
76 if (td->o.min_bs[ddir] > td->o.ba[ddir])
77 max_size -= td->o.min_bs[ddir] - td->o.ba[ddir];
79 max_blocks = max_size / (uint64_t) td->o.ba[ddir];
86 static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
87 enum fio_ddir ddir, uint64_t *b,
92 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE ||
93 td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64) {
95 r = __rand(&td->random_state);
97 dprint(FD_RANDOM, "off rand %llu\n", (unsigned long long) r);
99 *b = lastb * (r / (rand_max(&td->random_state) + 1.0));
103 assert(fio_file_lfsr(f));
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",
124 (unsigned long long) *b);
126 *b = axmap_next_free(f->io_axmap, *b);
127 if (*b == (uint64_t) -1ULL)
133 static int __get_next_rand_offset_zipf(struct thread_data *td,
134 struct fio_file *f, enum fio_ddir ddir,
137 *b = zipf_next(&f->zipf);
141 static int __get_next_rand_offset_pareto(struct thread_data *td,
142 struct fio_file *f, enum fio_ddir ddir,
145 *b = pareto_next(&f->zipf);
149 static int __get_next_rand_offset_gauss(struct thread_data *td,
150 struct fio_file *f, enum fio_ddir ddir,
153 *b = gauss_next(&f->gauss);
157 static int __get_next_rand_offset_zoned_abs(struct thread_data *td,
159 enum fio_ddir ddir, uint64_t *b)
161 struct zone_split_index *zsi;
162 uint64_t lastb, send, stotal;
165 lastb = last_block(td, f, ddir);
169 if (!td->o.zone_split_nr[ddir]) {
171 return __get_next_rand_offset(td, f, ddir, b, lastb);
175 * Generate a value, v, between 1 and 100, both inclusive
177 v = rand_between(&td->zone_state, 1, 100);
180 * Find our generated table. 'send' is the end block of this zone,
181 * 'stotal' is our start offset.
183 zsi = &td->zone_state_index[ddir][v - 1];
184 stotal = zsi->size_prev / td->o.ba[ddir];
185 send = zsi->size / td->o.ba[ddir];
188 * Should never happen
191 if (!fio_did_warn(FIO_WARN_ZONED_BUG))
192 log_err("fio: bug in zoned generation\n");
194 } else if (send > lastb) {
196 * This happens if the user specifies ranges that exceed
197 * the file/device size. We can't handle that gracefully,
200 log_err("fio: zoned_abs sizes exceed file size\n");
205 * Generate index from 0..send-stotal
207 if (__get_next_rand_offset(td, f, ddir, b, send - stotal) == 1)
214 static int __get_next_rand_offset_zoned(struct thread_data *td,
215 struct fio_file *f, enum fio_ddir ddir,
218 unsigned int v, send, stotal;
219 uint64_t offset, lastb;
220 struct zone_split_index *zsi;
222 lastb = last_block(td, f, ddir);
226 if (!td->o.zone_split_nr[ddir]) {
228 return __get_next_rand_offset(td, f, ddir, b, lastb);
232 * Generate a value, v, between 1 and 100, both inclusive
234 v = rand_between(&td->zone_state, 1, 100);
236 zsi = &td->zone_state_index[ddir][v - 1];
237 stotal = zsi->size_perc_prev;
238 send = zsi->size_perc;
241 * Should never happen
244 if (!fio_did_warn(FIO_WARN_ZONED_BUG))
245 log_err("fio: bug in zoned generation\n");
250 * 'send' is some percentage below or equal to 100 that
251 * marks the end of the current IO range. 'stotal' marks
252 * the start, in percent.
255 offset = stotal * lastb / 100ULL;
259 lastb = lastb * (send - stotal) / 100ULL;
262 * Generate index from 0..send-of-lastb
264 if (__get_next_rand_offset(td, f, ddir, b, lastb) == 1)
268 * Add our start offset, if any
276 static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
277 enum fio_ddir ddir, uint64_t *b)
279 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM) {
282 lastb = last_block(td, f, ddir);
286 return __get_next_rand_offset(td, f, ddir, b, lastb);
287 } else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
288 return __get_next_rand_offset_zipf(td, f, ddir, b);
289 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
290 return __get_next_rand_offset_pareto(td, f, ddir, b);
291 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
292 return __get_next_rand_offset_gauss(td, f, ddir, b);
293 else if (td->o.random_distribution == FIO_RAND_DIST_ZONED)
294 return __get_next_rand_offset_zoned(td, f, ddir, b);
295 else if (td->o.random_distribution == FIO_RAND_DIST_ZONED_ABS)
296 return __get_next_rand_offset_zoned_abs(td, f, ddir, b);
298 log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
302 static bool should_do_random(struct thread_data *td, enum fio_ddir ddir)
306 if (td->o.perc_rand[ddir] == 100)
309 v = rand_between(&td->seq_rand_state[ddir], 1, 100);
311 return v <= td->o.perc_rand[ddir];
314 static void loop_cache_invalidate(struct thread_data *td, struct fio_file *f)
316 struct thread_options *o = &td->o;
318 if (o->invalidate_cache && !o->odirect) {
321 ret = file_invalidate_cache(td, f);
325 static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
326 enum fio_ddir ddir, uint64_t *b)
328 if (!get_next_rand_offset(td, f, ddir, b))
331 if (td->o.time_based ||
332 (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)) {
333 fio_file_reset(td, f);
334 loop_cache_invalidate(td, f);
335 if (!get_next_rand_offset(td, f, ddir, b))
339 dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
340 f->file_name, (unsigned long long) f->last_pos[ddir],
341 (unsigned long long) f->real_file_size);
345 static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
346 enum fio_ddir ddir, uint64_t *offset)
348 struct thread_options *o = &td->o;
350 assert(ddir_rw(ddir));
353 * If we reach the end for a time based run, reset us back to 0
354 * and invalidate the cache, if we need to.
356 if (f->last_pos[ddir] >= f->io_size + get_start_offset(td, f) &&
358 f->last_pos[ddir] = f->file_offset;
359 loop_cache_invalidate(td, f);
362 if (f->last_pos[ddir] < f->real_file_size) {
366 * Only rewind if we already hit the end
368 if (f->last_pos[ddir] == f->file_offset &&
369 f->file_offset && o->ddir_seq_add < 0) {
370 if (f->real_file_size > f->io_size)
371 f->last_pos[ddir] = f->io_size;
373 f->last_pos[ddir] = f->real_file_size;
376 pos = f->last_pos[ddir] - f->file_offset;
377 if (pos && o->ddir_seq_add) {
378 pos += o->ddir_seq_add;
381 * If we reach beyond the end of the file
382 * with holed IO, wrap around to the
383 * beginning again. If we're doing backwards IO,
386 if (pos >= f->real_file_size) {
387 if (o->ddir_seq_add > 0)
388 pos = f->file_offset;
390 if (f->real_file_size > f->io_size)
393 pos = f->real_file_size;
395 pos += o->ddir_seq_add;
407 static int get_next_block(struct thread_data *td, struct io_u *io_u,
408 enum fio_ddir ddir, int rw_seq,
411 struct fio_file *f = io_u->file;
415 assert(ddir_rw(ddir));
421 if (should_do_random(td, ddir)) {
422 ret = get_next_rand_block(td, f, ddir, &b);
426 io_u_set(td, io_u, IO_U_F_BUSY_OK);
427 ret = get_next_seq_offset(td, f, ddir, &offset);
429 ret = get_next_rand_block(td, f, ddir, &b);
433 ret = get_next_seq_offset(td, f, ddir, &offset);
436 io_u_set(td, io_u, IO_U_F_BUSY_OK);
439 if (td->o.rw_seq == RW_SEQ_SEQ) {
440 ret = get_next_seq_offset(td, f, ddir, &offset);
442 ret = get_next_rand_block(td, f, ddir, &b);
445 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
446 if (f->last_start[ddir] != -1ULL)
447 offset = f->last_start[ddir] - f->file_offset;
452 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
459 io_u->offset = offset;
461 io_u->offset = b * td->o.ba[ddir];
463 log_err("fio: bug in offset generation: offset=%llu, b=%llu\n", (unsigned long long) offset, (unsigned long long) b);
472 * For random io, generate a random new block and see if it's used. Repeat
473 * until we find a free one. For sequential io, just return the end of
474 * the last io issued.
476 static int get_next_offset(struct thread_data *td, struct io_u *io_u,
479 struct fio_file *f = io_u->file;
480 enum fio_ddir ddir = io_u->ddir;
483 assert(ddir_rw(ddir));
485 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
487 td->ddir_seq_nr = td->o.ddir_seq_nr;
490 if (get_next_block(td, io_u, ddir, rw_seq_hit, is_random))
493 if (io_u->offset >= f->io_size) {
494 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
495 (unsigned long long) io_u->offset,
496 (unsigned long long) f->io_size);
500 io_u->offset += f->file_offset;
501 if (io_u->offset >= f->real_file_size) {
502 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
503 (unsigned long long) io_u->offset,
504 (unsigned long long) f->real_file_size);
511 static inline bool io_u_fits(struct thread_data *td, struct io_u *io_u,
512 unsigned long long buflen)
514 struct fio_file *f = io_u->file;
516 return io_u->offset + buflen <= f->io_size + get_start_offset(td, f);
519 static unsigned long long get_next_buflen(struct thread_data *td, struct io_u *io_u,
522 int ddir = io_u->ddir;
523 unsigned long long buflen = 0;
524 unsigned long long minbs, maxbs;
525 uint64_t frand_max, r;
528 assert(ddir_rw(ddir));
530 if (td->o.bs_is_seq_rand)
531 ddir = is_random ? DDIR_WRITE : DDIR_READ;
533 minbs = td->o.min_bs[ddir];
534 maxbs = td->o.max_bs[ddir];
540 * If we can't satisfy the min block size from here, then fail
542 if (!io_u_fits(td, io_u, minbs))
545 frand_max = rand_max(&td->bsrange_state[ddir]);
547 r = __rand(&td->bsrange_state[ddir]);
549 if (!td->o.bssplit_nr[ddir]) {
550 buflen = minbs + (unsigned long long) ((double) maxbs *
551 (r / (frand_max + 1.0)));
556 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
557 struct bssplit *bsp = &td->o.bssplit[ddir][i];
563 if ((r / perc <= frand_max / 100ULL) &&
564 io_u_fits(td, io_u, buflen))
569 power_2 = is_power_of_2(minbs);
570 if (!td->o.bs_unaligned && power_2)
571 buflen &= ~(minbs - 1);
572 else if (!td->o.bs_unaligned && !power_2)
573 buflen -= buflen % minbs;
574 } while (!io_u_fits(td, io_u, buflen));
579 static void set_rwmix_bytes(struct thread_data *td)
584 * we do time or byte based switch. this is needed because
585 * buffered writes may issue a lot quicker than they complete,
586 * whereas reads do not.
588 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
589 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
592 static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
596 v = rand_between(&td->rwmix_state, 1, 100);
598 if (v <= td->o.rwmix[DDIR_READ])
604 int io_u_quiesce(struct thread_data *td)
609 * We are going to sleep, ensure that we flush anything pending as
610 * not to skew our latency numbers.
612 * Changed to only monitor 'in flight' requests here instead of the
613 * td->cur_depth, b/c td->cur_depth does not accurately represent
614 * io's that have been actually submitted to an async engine,
615 * and cur_depth is meaningless for sync engines.
617 if (td->io_u_queued || td->cur_depth)
620 while (td->io_u_in_flight) {
623 ret = io_u_queued_complete(td, 1);
628 if (td->flags & TD_F_REGROW_LOGS)
634 static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
636 enum fio_ddir odir = ddir ^ 1;
640 assert(ddir_rw(ddir));
641 now = utime_since_now(&td->start);
644 * if rate_next_io_time is in the past, need to catch up to rate
646 if (td->rate_next_io_time[ddir] <= now)
650 * We are ahead of rate in this direction. See if we
653 if (td_rw(td) && td->o.rwmix[odir]) {
655 * Other direction is behind rate, switch
657 if (td->rate_next_io_time[odir] <= now)
661 * Both directions are ahead of rate. sleep the min,
662 * switch if necessary
664 if (td->rate_next_io_time[ddir] <=
665 td->rate_next_io_time[odir]) {
666 usec = td->rate_next_io_time[ddir] - now;
668 usec = td->rate_next_io_time[odir] - now;
672 usec = td->rate_next_io_time[ddir] - now;
674 if (td->o.io_submit_mode == IO_MODE_INLINE)
677 usec_sleep(td, usec);
682 * Return the data direction for the next io_u. If the job is a
683 * mixed read/write workload, check the rwmix cycle and switch if
686 static enum fio_ddir get_rw_ddir(struct thread_data *td)
691 * See if it's time to fsync/fdatasync/sync_file_range first,
692 * and if not then move on to check regular I/Os.
694 if (should_fsync(td)) {
695 if (td->o.fsync_blocks && td->io_issues[DDIR_WRITE] &&
696 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks))
699 if (td->o.fdatasync_blocks && td->io_issues[DDIR_WRITE] &&
700 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks))
701 return DDIR_DATASYNC;
703 if (td->sync_file_range_nr && td->io_issues[DDIR_WRITE] &&
704 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr))
705 return DDIR_SYNC_FILE_RANGE;
710 * Check if it's time to seed a new data direction.
712 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
714 * Put a top limit on how many bytes we do for
715 * one data direction, to avoid overflowing the
718 ddir = get_rand_ddir(td);
720 if (ddir != td->rwmix_ddir)
723 td->rwmix_ddir = ddir;
725 ddir = td->rwmix_ddir;
726 } else if (td_read(td))
728 else if (td_write(td))
730 else if (td_trim(td))
735 td->rwmix_ddir = rate_ddir(td, ddir);
736 return td->rwmix_ddir;
739 static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
741 enum fio_ddir ddir = get_rw_ddir(td);
743 if (td_trimwrite(td)) {
744 struct fio_file *f = io_u->file;
745 if (f->last_pos[DDIR_WRITE] == f->last_pos[DDIR_TRIM])
751 io_u->ddir = io_u->acct_ddir = ddir;
753 if (io_u->ddir == DDIR_WRITE && td_ioengine_flagged(td, FIO_BARRIER) &&
754 td->o.barrier_blocks &&
755 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
756 td->io_issues[DDIR_WRITE])
757 io_u_set(td, io_u, IO_U_F_BARRIER);
760 void put_file_log(struct thread_data *td, struct fio_file *f)
762 unsigned int ret = put_file(td, f);
765 td_verror(td, ret, "file close");
768 void put_io_u(struct thread_data *td, struct io_u *io_u)
770 if (io_u->post_submit) {
771 io_u->post_submit(io_u, io_u->error == 0);
772 io_u->post_submit = NULL;
780 if (io_u->file && !(io_u->flags & IO_U_F_NO_FILE_PUT))
781 put_file_log(td, io_u->file);
784 io_u_set(td, io_u, IO_U_F_FREE);
786 if (io_u->flags & IO_U_F_IN_CUR_DEPTH) {
788 assert(!(td->flags & TD_F_CHILD));
790 io_u_qpush(&td->io_u_freelist, io_u);
791 td_io_u_free_notify(td);
795 void clear_io_u(struct thread_data *td, struct io_u *io_u)
797 io_u_clear(td, io_u, IO_U_F_FLIGHT);
801 void requeue_io_u(struct thread_data *td, struct io_u **io_u)
803 struct io_u *__io_u = *io_u;
804 enum fio_ddir ddir = acct_ddir(__io_u);
806 dprint(FD_IO, "requeue %p\n", __io_u);
813 io_u_set(td, __io_u, IO_U_F_FREE);
814 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(ddir))
815 td->io_issues[ddir]--;
817 io_u_clear(td, __io_u, IO_U_F_FLIGHT);
818 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH) {
820 assert(!(td->flags & TD_F_CHILD));
823 io_u_rpush(&td->io_u_requeues, __io_u);
824 td_io_u_free_notify(td);
829 static void setup_strided_zone_mode(struct thread_data *td, struct io_u *io_u)
831 struct fio_file *f = io_u->file;
833 assert(td->o.zone_mode == ZONE_MODE_STRIDED);
834 assert(td->o.zone_size);
835 assert(td->o.zone_range);
838 * See if it's time to switch to a new zone
840 if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
842 f->file_offset += td->o.zone_range + td->o.zone_skip;
845 * Wrap from the beginning, if we exceed the file size
847 if (f->file_offset >= f->real_file_size)
848 f->file_offset = get_start_offset(td, f);
850 f->last_pos[io_u->ddir] = f->file_offset;
851 td->io_skip_bytes += td->o.zone_skip;
855 * If zone_size > zone_range, then maintain the same zone until
856 * zone_bytes >= zone_size.
858 if (f->last_pos[io_u->ddir] >= (f->file_offset + td->o.zone_range)) {
859 dprint(FD_IO, "io_u maintain zone offset=%" PRIu64 "/last_pos=%" PRIu64 "\n",
860 f->file_offset, f->last_pos[io_u->ddir]);
861 f->last_pos[io_u->ddir] = f->file_offset;
865 * For random: if 'norandommap' is not set and zone_size > zone_range,
866 * map needs to be reset as it's done with zone_range everytime.
868 if ((td->zone_bytes % td->o.zone_range) == 0)
869 fio_file_reset(td, f);
872 static int fill_io_u(struct thread_data *td, struct io_u *io_u)
876 if (td_ioengine_flagged(td, FIO_NOIO))
879 set_rw_ddir(td, io_u);
882 * fsync() or fdatasync() or trim etc, we are done
884 if (!ddir_rw(io_u->ddir))
887 if (td->o.zone_mode == ZONE_MODE_STRIDED)
888 setup_strided_zone_mode(td, io_u);
891 * No log, let the seq/rand engine retrieve the next buflen and
894 if (get_next_offset(td, io_u, &is_random)) {
895 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
899 io_u->buflen = get_next_buflen(td, io_u, is_random);
901 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
905 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
906 dprint(FD_IO, "io_u %p, off=0x%llx + len=0x%llx exceeds file size=0x%llx\n",
908 (unsigned long long) io_u->offset, io_u->buflen,
909 (unsigned long long) io_u->file->real_file_size);
914 * mark entry before potentially trimming io_u
916 if (td_random(td) && file_randommap(td, io_u->file))
917 io_u->buflen = mark_random_map(td, io_u, io_u->offset,
921 dprint_io_u(io_u, "fill");
922 td->zone_bytes += io_u->buflen;
926 static void __io_u_mark_map(uint64_t *map, unsigned int nr)
955 void io_u_mark_submit(struct thread_data *td, unsigned int nr)
957 __io_u_mark_map(td->ts.io_u_submit, nr);
958 td->ts.total_submit++;
961 void io_u_mark_complete(struct thread_data *td, unsigned int nr)
963 __io_u_mark_map(td->ts.io_u_complete, nr);
964 td->ts.total_complete++;
967 void io_u_mark_depth(struct thread_data *td, unsigned int nr)
971 switch (td->cur_depth) {
993 td->ts.io_u_map[idx] += nr;
996 static void io_u_mark_lat_nsec(struct thread_data *td, unsigned long long nsec)
1000 assert(nsec < 1000);
1033 assert(idx < FIO_IO_U_LAT_N_NR);
1034 td->ts.io_u_lat_n[idx]++;
1037 static void io_u_mark_lat_usec(struct thread_data *td, unsigned long long usec)
1041 assert(usec < 1000 && usec >= 1);
1074 assert(idx < FIO_IO_U_LAT_U_NR);
1075 td->ts.io_u_lat_u[idx]++;
1078 static void io_u_mark_lat_msec(struct thread_data *td, unsigned long long msec)
1121 assert(idx < FIO_IO_U_LAT_M_NR);
1122 td->ts.io_u_lat_m[idx]++;
1125 static void io_u_mark_latency(struct thread_data *td, unsigned long long nsec)
1128 io_u_mark_lat_nsec(td, nsec);
1129 else if (nsec < 1000000)
1130 io_u_mark_lat_usec(td, nsec / 1000);
1132 io_u_mark_lat_msec(td, nsec / 1000000);
1135 static unsigned int __get_next_fileno_rand(struct thread_data *td)
1137 unsigned long fileno;
1139 if (td->o.file_service_type == FIO_FSERVICE_RANDOM) {
1140 uint64_t frand_max = rand_max(&td->next_file_state);
1143 r = __rand(&td->next_file_state);
1144 return (unsigned int) ((double) td->o.nr_files
1145 * (r / (frand_max + 1.0)));
1148 if (td->o.file_service_type == FIO_FSERVICE_ZIPF)
1149 fileno = zipf_next(&td->next_file_zipf);
1150 else if (td->o.file_service_type == FIO_FSERVICE_PARETO)
1151 fileno = pareto_next(&td->next_file_zipf);
1152 else if (td->o.file_service_type == FIO_FSERVICE_GAUSS)
1153 fileno = gauss_next(&td->next_file_gauss);
1155 log_err("fio: bad file service type: %d\n", td->o.file_service_type);
1160 return fileno >> FIO_FSERVICE_SHIFT;
1164 * Get next file to service by choosing one at random
1166 static struct fio_file *get_next_file_rand(struct thread_data *td,
1167 enum fio_file_flags goodf,
1168 enum fio_file_flags badf)
1176 fno = __get_next_fileno_rand(td);
1179 if (fio_file_done(f))
1182 if (!fio_file_open(f)) {
1185 if (td->nr_open_files >= td->o.open_files)
1186 return ERR_PTR(-EBUSY);
1188 err = td_io_open_file(td, f);
1194 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
1195 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
1199 td_io_close_file(td, f);
1204 * Get next file to service by doing round robin between all available ones
1206 static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
1209 unsigned int old_next_file = td->next_file;
1215 f = td->files[td->next_file];
1218 if (td->next_file >= td->o.nr_files)
1221 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
1222 if (fio_file_done(f)) {
1227 if (!fio_file_open(f)) {
1230 if (td->nr_open_files >= td->o.open_files)
1231 return ERR_PTR(-EBUSY);
1233 err = td_io_open_file(td, f);
1235 dprint(FD_FILE, "error %d on open of %s\n",
1243 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
1245 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
1249 td_io_close_file(td, f);
1252 } while (td->next_file != old_next_file);
1254 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
1258 static struct fio_file *__get_next_file(struct thread_data *td)
1262 assert(td->o.nr_files <= td->files_index);
1264 if (td->nr_done_files >= td->o.nr_files) {
1265 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
1266 " nr_files=%d\n", td->nr_open_files,
1272 f = td->file_service_file;
1273 if (f && fio_file_open(f) && !fio_file_closing(f)) {
1274 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
1276 if (td->file_service_left--)
1280 if (td->o.file_service_type == FIO_FSERVICE_RR ||
1281 td->o.file_service_type == FIO_FSERVICE_SEQ)
1282 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
1284 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1289 td->file_service_file = f;
1290 td->file_service_left = td->file_service_nr - 1;
1293 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1295 dprint(FD_FILE, "get_next_file: NULL\n");
1299 static struct fio_file *get_next_file(struct thread_data *td)
1301 return __get_next_file(td);
1304 static long set_io_u_file(struct thread_data *td, struct io_u *io_u)
1309 f = get_next_file(td);
1310 if (IS_ERR_OR_NULL(f))
1316 if (!fill_io_u(td, io_u))
1319 if (io_u->post_submit) {
1320 io_u->post_submit(io_u, false);
1321 io_u->post_submit = NULL;
1324 put_file_log(td, f);
1325 td_io_close_file(td, f);
1327 if (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)
1328 fio_file_reset(td, f);
1330 fio_file_set_done(f);
1331 td->nr_done_files++;
1332 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
1333 td->nr_done_files, td->o.nr_files);
1340 static void lat_fatal(struct thread_data *td, struct io_completion_data *icd,
1341 unsigned long long tnsec, unsigned long long max_nsec)
1344 log_err("fio: latency of %llu nsec exceeds specified max (%llu nsec)\n", tnsec, max_nsec);
1345 td_verror(td, ETIMEDOUT, "max latency exceeded");
1346 icd->error = ETIMEDOUT;
1349 static void lat_new_cycle(struct thread_data *td)
1351 fio_gettime(&td->latency_ts, NULL);
1352 td->latency_ios = ddir_rw_sum(td->io_blocks);
1353 td->latency_failed = 0;
1357 * We had an IO outside the latency target. Reduce the queue depth. If we
1358 * are at QD=1, then it's time to give up.
1360 static bool __lat_target_failed(struct thread_data *td)
1362 if (td->latency_qd == 1)
1365 td->latency_qd_high = td->latency_qd;
1367 if (td->latency_qd == td->latency_qd_low)
1368 td->latency_qd_low--;
1370 td->latency_qd = (td->latency_qd + td->latency_qd_low) / 2;
1372 dprint(FD_RATE, "Ramped down: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
1375 * When we ramp QD down, quiesce existing IO to prevent
1376 * a storm of ramp downs due to pending higher depth.
1383 static bool lat_target_failed(struct thread_data *td)
1385 if (td->o.latency_percentile.u.f == 100.0)
1386 return __lat_target_failed(td);
1388 td->latency_failed++;
1392 void lat_target_init(struct thread_data *td)
1394 td->latency_end_run = 0;
1396 if (td->o.latency_target) {
1397 dprint(FD_RATE, "Latency target=%llu\n", td->o.latency_target);
1398 fio_gettime(&td->latency_ts, NULL);
1400 td->latency_qd_high = td->o.iodepth;
1401 td->latency_qd_low = 1;
1402 td->latency_ios = ddir_rw_sum(td->io_blocks);
1404 td->latency_qd = td->o.iodepth;
1407 void lat_target_reset(struct thread_data *td)
1409 if (!td->latency_end_run)
1410 lat_target_init(td);
1413 static void lat_target_success(struct thread_data *td)
1415 const unsigned int qd = td->latency_qd;
1416 struct thread_options *o = &td->o;
1418 td->latency_qd_low = td->latency_qd;
1421 * If we haven't failed yet, we double up to a failing value instead
1422 * of bisecting from highest possible queue depth. If we have set
1423 * a limit other than td->o.iodepth, bisect between that.
1425 if (td->latency_qd_high != o->iodepth)
1426 td->latency_qd = (td->latency_qd + td->latency_qd_high) / 2;
1428 td->latency_qd *= 2;
1430 if (td->latency_qd > o->iodepth)
1431 td->latency_qd = o->iodepth;
1433 dprint(FD_RATE, "Ramped up: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
1436 * Same as last one, we are done. Let it run a latency cycle, so
1437 * we get only the results from the targeted depth.
1439 if (td->latency_qd == qd) {
1440 if (td->latency_end_run) {
1441 dprint(FD_RATE, "We are done\n");
1444 dprint(FD_RATE, "Quiesce and final run\n");
1446 td->latency_end_run = 1;
1447 reset_all_stats(td);
1456 * Check if we can bump the queue depth
1458 void lat_target_check(struct thread_data *td)
1460 uint64_t usec_window;
1464 usec_window = utime_since_now(&td->latency_ts);
1465 if (usec_window < td->o.latency_window)
1468 ios = ddir_rw_sum(td->io_blocks) - td->latency_ios;
1469 success_ios = (double) (ios - td->latency_failed) / (double) ios;
1470 success_ios *= 100.0;
1472 dprint(FD_RATE, "Success rate: %.2f%% (target %.2f%%)\n", success_ios, td->o.latency_percentile.u.f);
1474 if (success_ios >= td->o.latency_percentile.u.f)
1475 lat_target_success(td);
1477 __lat_target_failed(td);
1481 * If latency target is enabled, we might be ramping up or down and not
1482 * using the full queue depth available.
1484 bool queue_full(const struct thread_data *td)
1486 const int qempty = io_u_qempty(&td->io_u_freelist);
1490 if (!td->o.latency_target)
1493 return td->cur_depth >= td->latency_qd;
1496 struct io_u *__get_io_u(struct thread_data *td)
1498 struct io_u *io_u = NULL;
1507 if (!io_u_rempty(&td->io_u_requeues))
1508 io_u = io_u_rpop(&td->io_u_requeues);
1509 else if (!queue_full(td)) {
1510 io_u = io_u_qpop(&td->io_u_freelist);
1515 io_u->end_io = NULL;
1519 assert(io_u->flags & IO_U_F_FREE);
1520 io_u_clear(td, io_u, IO_U_F_FREE | IO_U_F_NO_FILE_PUT |
1521 IO_U_F_TRIMMED | IO_U_F_BARRIER |
1525 io_u->acct_ddir = -1;
1527 assert(!(td->flags & TD_F_CHILD));
1528 io_u_set(td, io_u, IO_U_F_IN_CUR_DEPTH);
1530 } else if (td_async_processing(td)) {
1532 * We ran out, wait for async verify threads to finish and
1535 assert(!(td->flags & TD_F_CHILD));
1536 ret = pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1545 static bool check_get_trim(struct thread_data *td, struct io_u *io_u)
1547 if (!(td->flags & TD_F_TRIM_BACKLOG))
1549 if (!td->trim_entries)
1552 if (td->trim_batch) {
1554 if (get_next_trim(td, io_u))
1556 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1557 td->last_ddir != DDIR_READ) {
1558 td->trim_batch = td->o.trim_batch;
1559 if (!td->trim_batch)
1560 td->trim_batch = td->o.trim_backlog;
1561 if (get_next_trim(td, io_u))
1568 static bool check_get_verify(struct thread_data *td, struct io_u *io_u)
1570 if (!(td->flags & TD_F_VER_BACKLOG))
1573 if (td->io_hist_len) {
1576 if (td->verify_batch)
1578 else if (!(td->io_hist_len % td->o.verify_backlog) &&
1579 td->last_ddir != DDIR_READ) {
1580 td->verify_batch = td->o.verify_batch;
1581 if (!td->verify_batch)
1582 td->verify_batch = td->o.verify_backlog;
1586 if (get_verify && !get_next_verify(td, io_u)) {
1596 * Fill offset and start time into the buffer content, to prevent too
1597 * easy compressible data for simple de-dupe attempts. Do this for every
1598 * 512b block in the range, since that should be the smallest block size
1599 * we can expect from a device.
1601 static void small_content_scramble(struct io_u *io_u)
1603 unsigned long long i, nr_blocks = io_u->buflen >> 9;
1604 unsigned int offset;
1605 uint64_t boffset, *iptr;
1612 boffset = io_u->offset;
1614 if (io_u->buf_filled_len)
1615 io_u->buf_filled_len = 0;
1618 * Generate random index between 0..7. We do chunks of 512b, if
1619 * we assume a cacheline is 64 bytes, then we have 8 of those.
1620 * Scramble content within the blocks in the same cacheline to
1623 offset = (io_u->start_time.tv_nsec ^ boffset) & 7;
1625 for (i = 0; i < nr_blocks; i++) {
1627 * Fill offset into start of cacheline, time into end
1630 iptr = (void *) p + (offset << 6);
1633 iptr = (void *) p + 64 - 2 * sizeof(uint64_t);
1634 iptr[0] = io_u->start_time.tv_sec;
1635 iptr[1] = io_u->start_time.tv_nsec;
1643 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1644 * etc. The returned io_u is fully ready to be prepped, populated and submitted.
1646 struct io_u *get_io_u(struct thread_data *td)
1650 int do_scramble = 0;
1653 io_u = __get_io_u(td);
1655 dprint(FD_IO, "__get_io_u failed\n");
1659 if (check_get_verify(td, io_u))
1661 if (check_get_trim(td, io_u))
1665 * from a requeue, io_u already setup
1671 * If using an iolog, grab next piece if any available.
1673 if (td->flags & TD_F_READ_IOLOG) {
1674 if (read_iolog_get(td, io_u))
1676 } else if (set_io_u_file(td, io_u)) {
1678 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1684 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1688 assert(fio_file_open(f));
1690 if (ddir_rw(io_u->ddir)) {
1691 if (!io_u->buflen && !td_ioengine_flagged(td, FIO_NOIO)) {
1692 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
1696 f->last_start[io_u->ddir] = io_u->offset;
1697 f->last_pos[io_u->ddir] = io_u->offset + io_u->buflen;
1699 if (io_u->ddir == DDIR_WRITE) {
1700 if (td->flags & TD_F_REFILL_BUFFERS) {
1701 io_u_fill_buffer(td, io_u,
1702 td->o.min_bs[DDIR_WRITE],
1704 } else if ((td->flags & TD_F_SCRAMBLE_BUFFERS) &&
1705 !(td->flags & TD_F_COMPRESS) &&
1706 !(td->flags & TD_F_DO_VERIFY))
1708 } else if (io_u->ddir == DDIR_READ) {
1710 * Reset the buf_filled parameters so next time if the
1711 * buffer is used for writes it is refilled.
1713 io_u->buf_filled_len = 0;
1718 * Set io data pointers.
1720 io_u->xfer_buf = io_u->buf;
1721 io_u->xfer_buflen = io_u->buflen;
1725 if (!td_io_prep(td, io_u)) {
1726 if (!td->o.disable_lat)
1727 fio_gettime(&io_u->start_time, NULL);
1730 small_content_scramble(io_u);
1735 dprint(FD_IO, "get_io_u failed\n");
1737 return ERR_PTR(ret);
1740 static void __io_u_log_error(struct thread_data *td, struct io_u *io_u)
1742 enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
1744 if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1747 log_err("fio: io_u error%s%s: %s: %s offset=%llu, buflen=%llu\n",
1748 io_u->file ? " on file " : "",
1749 io_u->file ? io_u->file->file_name : "",
1750 strerror(io_u->error),
1751 io_ddir_name(io_u->ddir),
1752 io_u->offset, io_u->xfer_buflen);
1754 if (td->io_ops->errdetails) {
1755 char *err = td->io_ops->errdetails(io_u);
1757 log_err("fio: %s\n", err);
1762 td_verror(td, io_u->error, "io_u error");
1765 void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1767 __io_u_log_error(td, io_u);
1769 __io_u_log_error(td->parent, io_u);
1772 static inline bool gtod_reduce(struct thread_data *td)
1774 return (td->o.disable_clat && td->o.disable_slat && td->o.disable_bw)
1775 || td->o.gtod_reduce;
1778 static void account_io_completion(struct thread_data *td, struct io_u *io_u,
1779 struct io_completion_data *icd,
1780 const enum fio_ddir idx, unsigned int bytes)
1782 const int no_reduce = !gtod_reduce(td);
1783 unsigned long long llnsec = 0;
1788 if (!td->o.stats || td_ioengine_flagged(td, FIO_NOSTATS))
1792 llnsec = ntime_since(&io_u->issue_time, &icd->time);
1794 if (!td->o.disable_lat) {
1795 unsigned long long tnsec;
1797 tnsec = ntime_since(&io_u->start_time, &icd->time);
1798 add_lat_sample(td, idx, tnsec, bytes, io_u->offset);
1800 if (td->flags & TD_F_PROFILE_OPS) {
1801 struct prof_io_ops *ops = &td->prof_io_ops;
1804 icd->error = ops->io_u_lat(td, tnsec);
1807 if (td->o.max_latency && tnsec > td->o.max_latency)
1808 lat_fatal(td, icd, tnsec, td->o.max_latency);
1809 if (td->o.latency_target && tnsec > td->o.latency_target) {
1810 if (lat_target_failed(td))
1811 lat_fatal(td, icd, tnsec, td->o.latency_target);
1816 if (!td->o.disable_clat) {
1817 add_clat_sample(td, idx, llnsec, bytes, io_u->offset);
1818 io_u_mark_latency(td, llnsec);
1821 if (!td->o.disable_bw && per_unit_log(td->bw_log))
1822 add_bw_sample(td, io_u, bytes, llnsec);
1824 if (no_reduce && per_unit_log(td->iops_log))
1825 add_iops_sample(td, io_u, bytes);
1826 } else if (ddir_sync(idx) && !td->o.disable_clat)
1827 add_sync_clat_sample(&td->ts, llnsec);
1829 if (td->ts.nr_block_infos && io_u->ddir == DDIR_TRIM) {
1830 uint32_t *info = io_u_block_info(td, io_u);
1831 if (BLOCK_INFO_STATE(*info) < BLOCK_STATE_TRIM_FAILURE) {
1832 if (io_u->ddir == DDIR_TRIM) {
1833 *info = BLOCK_INFO(BLOCK_STATE_TRIMMED,
1834 BLOCK_INFO_TRIMS(*info) + 1);
1835 } else if (io_u->ddir == DDIR_WRITE) {
1836 *info = BLOCK_INFO_SET_STATE(BLOCK_STATE_WRITTEN,
1843 static void file_log_write_comp(const struct thread_data *td, struct fio_file *f,
1844 uint64_t offset, unsigned int bytes)
1851 if (f->first_write == -1ULL || offset < f->first_write)
1852 f->first_write = offset;
1853 if (f->last_write == -1ULL || ((offset + bytes) > f->last_write))
1854 f->last_write = offset + bytes;
1856 if (!f->last_write_comp)
1859 idx = f->last_write_idx++;
1860 f->last_write_comp[idx] = offset;
1861 if (f->last_write_idx == td->o.iodepth)
1862 f->last_write_idx = 0;
1865 static bool should_account(struct thread_data *td)
1867 return ramp_time_over(td) && (td->runstate == TD_RUNNING ||
1868 td->runstate == TD_VERIFYING);
1871 static void io_completed(struct thread_data *td, struct io_u **io_u_ptr,
1872 struct io_completion_data *icd)
1874 struct io_u *io_u = *io_u_ptr;
1875 enum fio_ddir ddir = io_u->ddir;
1876 struct fio_file *f = io_u->file;
1878 dprint_io_u(io_u, "complete");
1880 assert(io_u->flags & IO_U_F_FLIGHT);
1881 io_u_clear(td, io_u, IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
1884 * Mark IO ok to verify
1888 * Remove errored entry from the verification list
1891 unlog_io_piece(td, io_u);
1893 io_u->ipo->flags &= ~IP_F_IN_FLIGHT;
1898 if (ddir_sync(ddir)) {
1899 td->last_was_sync = true;
1901 f->first_write = -1ULL;
1902 f->last_write = -1ULL;
1904 if (should_account(td))
1905 account_io_completion(td, io_u, icd, ddir, io_u->buflen);
1909 td->last_was_sync = false;
1910 td->last_ddir = ddir;
1912 if (!io_u->error && ddir_rw(ddir)) {
1913 unsigned long long bytes = io_u->buflen - io_u->resid;
1916 td->io_blocks[ddir]++;
1917 td->io_bytes[ddir] += bytes;
1919 if (!(io_u->flags & IO_U_F_VER_LIST)) {
1920 td->this_io_blocks[ddir]++;
1921 td->this_io_bytes[ddir] += bytes;
1924 if (ddir == DDIR_WRITE)
1925 file_log_write_comp(td, f, io_u->offset, bytes);
1927 if (should_account(td))
1928 account_io_completion(td, io_u, icd, ddir, bytes);
1930 icd->bytes_done[ddir] += bytes;
1933 ret = io_u->end_io(td, io_u_ptr);
1935 if (ret && !icd->error)
1938 } else if (io_u->error) {
1939 icd->error = io_u->error;
1940 io_u_log_error(td, io_u);
1943 enum error_type_bit eb = td_error_type(ddir, icd->error);
1945 if (!td_non_fatal_error(td, eb, icd->error))
1949 * If there is a non_fatal error, then add to the error count
1950 * and clear all the errors.
1952 update_error_count(td, icd->error);
1960 static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1965 if (!gtod_reduce(td))
1966 fio_gettime(&icd->time, NULL);
1971 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
1972 icd->bytes_done[ddir] = 0;
1975 static void ios_completed(struct thread_data *td,
1976 struct io_completion_data *icd)
1981 for (i = 0; i < icd->nr; i++) {
1982 io_u = td->io_ops->event(td, i);
1984 io_completed(td, &io_u, icd);
1992 * Complete a single io_u for the sync engines.
1994 int io_u_sync_complete(struct thread_data *td, struct io_u *io_u)
1996 struct io_completion_data icd;
1999 init_icd(td, &icd, 1);
2000 io_completed(td, &io_u, &icd);
2006 td_verror(td, icd.error, "io_u_sync_complete");
2010 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
2011 td->bytes_done[ddir] += icd.bytes_done[ddir];
2017 * Called to complete min_events number of io for the async engines.
2019 int io_u_queued_complete(struct thread_data *td, int min_evts)
2021 struct io_completion_data icd;
2022 struct timespec *tvp = NULL;
2024 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
2026 dprint(FD_IO, "io_u_queued_complete: min=%d\n", min_evts);
2030 else if (min_evts > td->cur_depth)
2031 min_evts = td->cur_depth;
2033 /* No worries, td_io_getevents fixes min and max if they are
2034 * set incorrectly */
2035 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete_max, tvp);
2037 td_verror(td, -ret, "td_io_getevents");
2042 init_icd(td, &icd, ret);
2043 ios_completed(td, &icd);
2045 td_verror(td, icd.error, "io_u_queued_complete");
2049 for (ddir = 0; ddir < DDIR_RWDIR_CNT; ddir++)
2050 td->bytes_done[ddir] += icd.bytes_done[ddir];
2056 * Call when io_u is really queued, to update the submission latency.
2058 void io_u_queued(struct thread_data *td, struct io_u *io_u)
2060 if (!td->o.disable_slat && ramp_time_over(td) && td->o.stats) {
2061 unsigned long slat_time;
2063 slat_time = ntime_since(&io_u->start_time, &io_u->issue_time);
2068 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen,
2074 * See if we should reuse the last seed, if dedupe is enabled
2076 static struct frand_state *get_buf_state(struct thread_data *td)
2080 if (!td->o.dedupe_percentage)
2081 return &td->buf_state;
2082 else if (td->o.dedupe_percentage == 100) {
2083 frand_copy(&td->buf_state_prev, &td->buf_state);
2084 return &td->buf_state;
2087 v = rand_between(&td->dedupe_state, 1, 100);
2089 if (v <= td->o.dedupe_percentage)
2090 return &td->buf_state_prev;
2092 return &td->buf_state;
2095 static void save_buf_state(struct thread_data *td, struct frand_state *rs)
2097 if (td->o.dedupe_percentage == 100)
2098 frand_copy(rs, &td->buf_state_prev);
2099 else if (rs == &td->buf_state)
2100 frand_copy(&td->buf_state_prev, rs);
2103 void fill_io_buffer(struct thread_data *td, void *buf, unsigned long long min_write,
2104 unsigned long long max_bs)
2106 struct thread_options *o = &td->o;
2108 if (o->mem_type == MEM_CUDA_MALLOC)
2111 if (o->compress_percentage || o->dedupe_percentage) {
2112 unsigned int perc = td->o.compress_percentage;
2113 struct frand_state *rs;
2114 unsigned long long left = max_bs;
2115 unsigned long long this_write;
2118 rs = get_buf_state(td);
2120 min_write = min(min_write, left);
2123 this_write = min_not_zero(min_write,
2124 (unsigned long long) td->o.compress_chunk);
2126 fill_random_buf_percentage(rs, buf, perc,
2127 this_write, this_write,
2129 o->buffer_pattern_bytes);
2131 fill_random_buf(rs, buf, min_write);
2132 this_write = min_write;
2137 save_buf_state(td, rs);
2139 } else if (o->buffer_pattern_bytes)
2140 fill_buffer_pattern(td, buf, max_bs);
2141 else if (o->zero_buffers)
2142 memset(buf, 0, max_bs);
2144 fill_random_buf(get_buf_state(td), buf, max_bs);
2148 * "randomly" fill the buffer contents
2150 void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
2151 unsigned long long min_write, unsigned long long max_bs)
2153 io_u->buf_filled_len = 0;
2154 fill_io_buffer(td, io_u->buf, min_write, max_bs);
2157 static int do_sync_file_range(const struct thread_data *td,
2160 off64_t offset, nbytes;
2162 offset = f->first_write;
2163 nbytes = f->last_write - f->first_write;
2168 return sync_file_range(f->fd, offset, nbytes, td->o.sync_file_range);
2171 int do_io_u_sync(const struct thread_data *td, struct io_u *io_u)
2175 if (io_u->ddir == DDIR_SYNC) {
2176 ret = fsync(io_u->file->fd);
2177 } else if (io_u->ddir == DDIR_DATASYNC) {
2178 #ifdef CONFIG_FDATASYNC
2179 ret = fdatasync(io_u->file->fd);
2181 ret = io_u->xfer_buflen;
2182 io_u->error = EINVAL;
2184 } else if (io_u->ddir == DDIR_SYNC_FILE_RANGE)
2185 ret = do_sync_file_range(td, io_u->file);
2187 ret = io_u->xfer_buflen;
2188 io_u->error = EINVAL;
2192 io_u->error = errno;
2197 int do_io_u_trim(const struct thread_data *td, struct io_u *io_u)
2199 #ifndef FIO_HAVE_TRIM
2200 io_u->error = EINVAL;
2203 struct fio_file *f = io_u->file;
2206 ret = os_trim(f, io_u->offset, io_u->xfer_buflen);
2208 return io_u->xfer_buflen;