Fix for a race when fio prints I/O statistics periodically
[fio.git] / io_u.c
CommitLineData
10ba535a
JA
1#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <signal.h>
5#include <time.h>
0c6e7517 6#include <assert.h>
10ba535a
JA
7
8#include "fio.h"
5973cafb 9#include "hash.h"
4f5af7b2 10#include "verify.h"
0d29de83 11#include "trim.h"
1fbbf72e 12#include "lib/rand.h"
7ebd796f 13#include "lib/axmap.h"
002fe734 14#include "err.h"
10ba535a 15
97601024
JA
16struct io_completion_data {
17 int nr; /* input */
97601024
JA
18
19 int error; /* output */
100f49f1 20 uint64_t bytes_done[DDIR_RWDIR_CNT]; /* output */
97601024
JA
21 struct timeval time; /* output */
22};
23
10ba535a 24/*
7ebd796f 25 * The ->io_axmap contains a map of blocks we have or have not done io
10ba535a
JA
26 * to yet. Used to make sure we cover the entire range in a fair fashion.
27 */
1ae83d45 28static int random_map_free(struct fio_file *f, const uint64_t block)
10ba535a 29{
7ebd796f 30 return !axmap_isset(f->io_axmap, block);
10ba535a
JA
31}
32
df415585
JA
33/*
34 * Mark a given offset as used in the map.
35 */
9bf2061e 36static void mark_random_map(struct thread_data *td, struct io_u *io_u)
df415585 37{
2dc1bbeb 38 unsigned int min_bs = td->o.rw_min_bs;
9bf2061e 39 struct fio_file *f = io_u->file;
51ede0b1 40 unsigned int nr_blocks;
1ae83d45 41 uint64_t block;
df415585 42
1ae83d45 43 block = (io_u->offset - f->file_offset) / (uint64_t) min_bs;
c685b5b2 44 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
df415585 45
2ab9e98b 46 if (!(io_u->flags & IO_U_F_BUSY_OK))
7ebd796f 47 nr_blocks = axmap_set_nr(f->io_axmap, block, nr_blocks);
df415585 48
51ede0b1
JA
49 if ((nr_blocks * min_bs) < io_u->buflen)
50 io_u->buflen = nr_blocks * min_bs;
df415585
JA
51}
52
74776733
JA
53static uint64_t last_block(struct thread_data *td, struct fio_file *f,
54 enum fio_ddir ddir)
2ba1c290 55{
74776733
JA
56 uint64_t max_blocks;
57 uint64_t max_size;
2ba1c290 58
ff58fced
JA
59 assert(ddir_rw(ddir));
60
d9dd70f7
JA
61 /*
62 * Hmm, should we make sure that ->io_size <= ->real_file_size?
63 */
64 max_size = f->io_size;
65 if (max_size > f->real_file_size)
66 max_size = f->real_file_size;
67
ed335855
SN
68 if (td->o.zone_range)
69 max_size = td->o.zone_range;
70
1ae83d45 71 max_blocks = max_size / (uint64_t) td->o.ba[ddir];
2ba1c290
JA
72 if (!max_blocks)
73 return 0;
74
67778e88 75 return max_blocks;
2ba1c290
JA
76}
77
1ae83d45
JA
78struct rand_off {
79 struct flist_head list;
80 uint64_t off;
81};
82
e25839d4 83static int __get_next_rand_offset(struct thread_data *td, struct fio_file *f,
1ae83d45 84 enum fio_ddir ddir, uint64_t *b)
ec4015da 85{
1ae83d45 86 uint64_t r, lastb;
ec4015da 87
74776733
JA
88 lastb = last_block(td, f, ddir);
89 if (!lastb)
90 return 1;
5e0baa7f 91
74776733 92 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE) {
d6b72507 93 r = __rand(&td->random_state);
8055e41d 94
4b91ee8f 95 dprint(FD_RANDOM, "off rand %llu\n", (unsigned long long) r);
8055e41d 96
54a21917 97 *b = lastb * (r / ((uint64_t) FRAND_MAX + 1.0));
51ede0b1 98 } else {
8055e41d 99 uint64_t off = 0;
43c63a78 100
74776733 101 if (lfsr_next(&f->lfsr, &off, lastb))
8055e41d 102 return 1;
ec4015da 103
8055e41d
JA
104 *b = off;
105 }
0ce8b119 106
ec4015da 107 /*
51ede0b1 108 * if we are not maintaining a random map, we are done.
ec4015da 109 */
51ede0b1
JA
110 if (!file_randommap(td, f))
111 goto ret;
43c63a78
JA
112
113 /*
51ede0b1 114 * calculate map offset and check if it's free
43c63a78 115 */
51ede0b1
JA
116 if (random_map_free(f, *b))
117 goto ret;
118
4b91ee8f
JA
119 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
120 (unsigned long long) *b);
51ede0b1 121
7ebd796f 122 *b = axmap_next_free(f->io_axmap, *b);
51ede0b1
JA
123 if (*b == (uint64_t) -1ULL)
124 return 1;
0ce8b119
JA
125ret:
126 return 0;
ec4015da
JA
127}
128
925fee33
JA
129static int __get_next_rand_offset_zipf(struct thread_data *td,
130 struct fio_file *f, enum fio_ddir ddir,
1ae83d45 131 uint64_t *b)
e25839d4 132{
9c6f6316 133 *b = zipf_next(&f->zipf);
e25839d4
JA
134 return 0;
135}
136
925fee33
JA
137static int __get_next_rand_offset_pareto(struct thread_data *td,
138 struct fio_file *f, enum fio_ddir ddir,
1ae83d45 139 uint64_t *b)
925fee33 140{
9c6f6316 141 *b = pareto_next(&f->zipf);
925fee33
JA
142 return 0;
143}
144
1ae83d45
JA
145static int flist_cmp(void *data, struct flist_head *a, struct flist_head *b)
146{
147 struct rand_off *r1 = flist_entry(a, struct rand_off, list);
148 struct rand_off *r2 = flist_entry(b, struct rand_off, list);
149
150 return r1->off - r2->off;
151}
152
153static int get_off_from_method(struct thread_data *td, struct fio_file *f,
154 enum fio_ddir ddir, uint64_t *b)
e25839d4
JA
155{
156 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
157 return __get_next_rand_offset(td, f, ddir, b);
158 else if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
159 return __get_next_rand_offset_zipf(td, f, ddir, b);
925fee33
JA
160 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
161 return __get_next_rand_offset_pareto(td, f, ddir, b);
e25839d4
JA
162
163 log_err("fio: unknown random distribution: %d\n", td->o.random_distribution);
164 return 1;
165}
166
bcd5abfa
JA
167/*
168 * Sort the reads for a verify phase in batches of verifysort_nr, if
169 * specified.
170 */
171static inline int should_sort_io(struct thread_data *td)
172{
173 if (!td->o.verifysort_nr || !td->o.do_verify)
174 return 0;
175 if (!td_random(td))
176 return 0;
177 if (td->runstate != TD_VERIFYING)
178 return 0;
179 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE)
180 return 0;
181
182 return 1;
183}
184
d9472271 185static int should_do_random(struct thread_data *td, enum fio_ddir ddir)
211c9b89
JA
186{
187 unsigned int v;
188 unsigned long r;
189
d9472271 190 if (td->o.perc_rand[ddir] == 100)
211c9b89
JA
191 return 1;
192
d6b72507 193 r = __rand(&td->seq_rand_state[ddir]);
54a21917 194 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
211c9b89 195
d9472271 196 return v <= td->o.perc_rand[ddir];
211c9b89
JA
197}
198
1ae83d45
JA
199static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
200 enum fio_ddir ddir, uint64_t *b)
201{
202 struct rand_off *r;
203 int i, ret = 1;
204
bcd5abfa 205 if (!should_sort_io(td))
1ae83d45
JA
206 return get_off_from_method(td, f, ddir, b);
207
208 if (!flist_empty(&td->next_rand_list)) {
1ae83d45 209fetch:
9342d5f8 210 r = flist_first_entry(&td->next_rand_list, struct rand_off, list);
1ae83d45
JA
211 flist_del(&r->list);
212 *b = r->off;
213 free(r);
214 return 0;
215 }
216
217 for (i = 0; i < td->o.verifysort_nr; i++) {
218 r = malloc(sizeof(*r));
219
220 ret = get_off_from_method(td, f, ddir, &r->off);
221 if (ret) {
222 free(r);
223 break;
224 }
225
226 flist_add(&r->list, &td->next_rand_list);
227 }
228
229 if (ret && !i)
230 return ret;
231
232 assert(!flist_empty(&td->next_rand_list));
233 flist_sort(NULL, &td->next_rand_list, flist_cmp);
234 goto fetch;
235}
236
38dad62d 237static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
1ae83d45 238 enum fio_ddir ddir, uint64_t *b)
38dad62d 239{
c04e4661
DE
240 if (!get_next_rand_offset(td, f, ddir, b))
241 return 0;
242
243 if (td->o.time_based) {
33c48814 244 fio_file_reset(td, f);
c04e4661
DE
245 if (!get_next_rand_offset(td, f, ddir, b))
246 return 0;
38dad62d
JA
247 }
248
c04e4661 249 dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
4b91ee8f
JA
250 f->file_name, (unsigned long long) f->last_pos,
251 (unsigned long long) f->real_file_size);
c04e4661 252 return 1;
38dad62d
JA
253}
254
37cf9e3c 255static int get_next_seq_offset(struct thread_data *td, struct fio_file *f,
1ae83d45 256 enum fio_ddir ddir, uint64_t *offset)
38dad62d 257{
ac002339
JA
258 struct thread_options *o = &td->o;
259
ff58fced
JA
260 assert(ddir_rw(ddir));
261
ac002339
JA
262 if (f->last_pos >= f->io_size + get_start_offset(td, f) &&
263 o->time_based)
c04e4661
DE
264 f->last_pos = f->last_pos - f->io_size;
265
38dad62d 266 if (f->last_pos < f->real_file_size) {
1ae83d45 267 uint64_t pos;
059b0802 268
ac002339 269 if (f->last_pos == f->file_offset && o->ddir_seq_add < 0)
a66da7a2
JA
270 f->last_pos = f->real_file_size;
271
272 pos = f->last_pos - f->file_offset;
ac002339
JA
273 if (pos && o->ddir_seq_add) {
274 pos += o->ddir_seq_add;
275
276 /*
277 * If we reach beyond the end of the file
278 * with holed IO, wrap around to the
279 * beginning again.
280 */
281 if (pos >= f->real_file_size)
282 pos = f->file_offset;
283 }
059b0802 284
37cf9e3c 285 *offset = pos;
38dad62d
JA
286 return 0;
287 }
288
289 return 1;
290}
291
292static int get_next_block(struct thread_data *td, struct io_u *io_u,
6aca9b3d
JA
293 enum fio_ddir ddir, int rw_seq,
294 unsigned int *is_random)
38dad62d
JA
295{
296 struct fio_file *f = io_u->file;
1ae83d45 297 uint64_t b, offset;
38dad62d
JA
298 int ret;
299
ff58fced
JA
300 assert(ddir_rw(ddir));
301
37cf9e3c
JA
302 b = offset = -1ULL;
303
38dad62d 304 if (rw_seq) {
211c9b89 305 if (td_random(td)) {
6aca9b3d 306 if (should_do_random(td, ddir)) {
211c9b89 307 ret = get_next_rand_block(td, f, ddir, &b);
6aca9b3d
JA
308 *is_random = 1;
309 } else {
310 *is_random = 0;
211c9b89
JA
311 io_u->flags |= IO_U_F_BUSY_OK;
312 ret = get_next_seq_offset(td, f, ddir, &offset);
313 if (ret)
314 ret = get_next_rand_block(td, f, ddir, &b);
315 }
6aca9b3d
JA
316 } else {
317 *is_random = 0;
37cf9e3c 318 ret = get_next_seq_offset(td, f, ddir, &offset);
6aca9b3d 319 }
38dad62d
JA
320 } else {
321 io_u->flags |= IO_U_F_BUSY_OK;
6aca9b3d 322 *is_random = 0;
38dad62d
JA
323
324 if (td->o.rw_seq == RW_SEQ_SEQ) {
37cf9e3c 325 ret = get_next_seq_offset(td, f, ddir, &offset);
6aca9b3d 326 if (ret) {
37cf9e3c 327 ret = get_next_rand_block(td, f, ddir, &b);
6aca9b3d
JA
328 *is_random = 0;
329 }
38dad62d
JA
330 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
331 if (f->last_start != -1ULL)
37cf9e3c 332 offset = f->last_start - f->file_offset;
38dad62d 333 else
37cf9e3c 334 offset = 0;
38dad62d
JA
335 ret = 0;
336 } else {
337 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
338 ret = 1;
339 }
340 }
6d68b997 341
37cf9e3c
JA
342 if (!ret) {
343 if (offset != -1ULL)
344 io_u->offset = offset;
345 else if (b != -1ULL)
346 io_u->offset = b * td->o.ba[ddir];
347 else {
4e0a8fa2 348 log_err("fio: bug in offset generation: offset=%llu, b=%llu\n", (unsigned long long) offset, (unsigned long long) b);
37cf9e3c
JA
349 ret = 1;
350 }
351 }
352
38dad62d
JA
353 return ret;
354}
355
10ba535a
JA
356/*
357 * For random io, generate a random new block and see if it's used. Repeat
358 * until we find a free one. For sequential io, just return the end of
359 * the last io issued.
360 */
6aca9b3d
JA
361static int __get_next_offset(struct thread_data *td, struct io_u *io_u,
362 unsigned int *is_random)
10ba535a 363{
9bf2061e 364 struct fio_file *f = io_u->file;
4ba66134 365 enum fio_ddir ddir = io_u->ddir;
38dad62d 366 int rw_seq_hit = 0;
10ba535a 367
ff58fced
JA
368 assert(ddir_rw(ddir));
369
38dad62d
JA
370 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
371 rw_seq_hit = 1;
5736c10d 372 td->ddir_seq_nr = td->o.ddir_seq_nr;
38dad62d 373 }
211097b2 374
6aca9b3d 375 if (get_next_block(td, io_u, ddir, rw_seq_hit, is_random))
38dad62d 376 return 1;
10ba535a 377
009bd847
JA
378 if (io_u->offset >= f->io_size) {
379 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
4b91ee8f
JA
380 (unsigned long long) io_u->offset,
381 (unsigned long long) f->io_size);
009bd847
JA
382 return 1;
383 }
384
385 io_u->offset += f->file_offset;
2ba1c290
JA
386 if (io_u->offset >= f->real_file_size) {
387 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
4b91ee8f
JA
388 (unsigned long long) io_u->offset,
389 (unsigned long long) f->real_file_size);
10ba535a 390 return 1;
2ba1c290 391 }
10ba535a
JA
392
393 return 0;
394}
395
6aca9b3d
JA
396static int get_next_offset(struct thread_data *td, struct io_u *io_u,
397 unsigned int *is_random)
15dc1934 398{
d72be545
JA
399 if (td->flags & TD_F_PROFILE_OPS) {
400 struct prof_io_ops *ops = &td->prof_io_ops;
7eb36574 401
d72be545 402 if (ops->fill_io_u_off)
6aca9b3d 403 return ops->fill_io_u_off(td, io_u, is_random);
d72be545 404 }
15dc1934 405
6aca9b3d 406 return __get_next_offset(td, io_u, is_random);
15dc1934
JA
407}
408
79944128
JA
409static inline int io_u_fits(struct thread_data *td, struct io_u *io_u,
410 unsigned int buflen)
411{
412 struct fio_file *f = io_u->file;
413
bedc9dc2 414 return io_u->offset + buflen <= f->io_size + get_start_offset(td, f);
79944128
JA
415}
416
6aca9b3d
JA
417static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u,
418 unsigned int is_random)
10ba535a 419{
6aca9b3d 420 int ddir = io_u->ddir;
24d23ca7 421 unsigned int buflen = 0;
f3059de1 422 unsigned int minbs, maxbs;
54a21917 423 unsigned long r;
10ba535a 424
9ee1c647 425 assert(ddir_rw(ddir));
6aca9b3d
JA
426
427 if (td->o.bs_is_seq_rand)
428 ddir = is_random ? DDIR_WRITE: DDIR_READ;
ff58fced 429
f3059de1
JA
430 minbs = td->o.min_bs[ddir];
431 maxbs = td->o.max_bs[ddir];
432
79944128
JA
433 if (minbs == maxbs)
434 return minbs;
435
52c58027
JA
436 /*
437 * If we can't satisfy the min block size from here, then fail
438 */
439 if (!io_u_fits(td, io_u, minbs))
440 return 0;
441
79944128 442 do {
d6b72507 443 r = __rand(&td->bsrange_state);
4c07ad86 444
720e84ad 445 if (!td->o.bssplit_nr[ddir]) {
f3059de1 446 buflen = 1 + (unsigned int) ((double) maxbs *
54a21917 447 (r / (FRAND_MAX + 1.0)));
f3059de1
JA
448 if (buflen < minbs)
449 buflen = minbs;
5ec10eaa 450 } else {
564ca972
JA
451 long perc = 0;
452 unsigned int i;
453
720e84ad
JA
454 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
455 struct bssplit *bsp = &td->o.bssplit[ddir][i];
564ca972
JA
456
457 buflen = bsp->bs;
458 perc += bsp->perc;
54a21917 459 if ((r <= ((FRAND_MAX / 100L) * perc)) &&
79944128 460 io_u_fits(td, io_u, buflen))
564ca972
JA
461 break;
462 }
463 }
79944128 464
a9f70b1f
JB
465 if (td->o.do_verify && td->o.verify != VERIFY_NONE)
466 buflen = (buflen + td->o.verify_interval - 1) &
467 ~(td->o.verify_interval - 1);
468
f3059de1
JA
469 if (!td->o.bs_unaligned && is_power_of_2(minbs))
470 buflen = (buflen + minbs - 1) & ~(minbs - 1);
10ba535a 471
79944128 472 } while (!io_u_fits(td, io_u, buflen));
6a5e6884 473
10ba535a
JA
474 return buflen;
475}
476
6aca9b3d
JA
477static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u,
478 unsigned int is_random)
15dc1934 479{
d72be545
JA
480 if (td->flags & TD_F_PROFILE_OPS) {
481 struct prof_io_ops *ops = &td->prof_io_ops;
7eb36574 482
d72be545 483 if (ops->fill_io_u_size)
6aca9b3d 484 return ops->fill_io_u_size(td, io_u, is_random);
d72be545 485 }
15dc1934 486
6aca9b3d 487 return __get_next_buflen(td, io_u, is_random);
15dc1934
JA
488}
489
afe24a5a
JA
490static void set_rwmix_bytes(struct thread_data *td)
491{
afe24a5a
JA
492 unsigned int diff;
493
494 /*
495 * we do time or byte based switch. this is needed because
496 * buffered writes may issue a lot quicker than they complete,
497 * whereas reads do not.
498 */
e47f799f 499 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
04c540d9 500 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
e47f799f
JA
501}
502
503static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
504{
505 unsigned int v;
1294c3ec 506 unsigned long r;
e47f799f 507
d6b72507 508 r = __rand(&td->rwmix_state);
54a21917 509 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
4c07ad86 510
04c540d9 511 if (v <= td->o.rwmix[DDIR_READ])
e47f799f
JA
512 return DDIR_READ;
513
514 return DDIR_WRITE;
afe24a5a
JA
515}
516
002e7183
JA
517void io_u_quiesce(struct thread_data *td)
518{
519 /*
520 * We are going to sleep, ensure that we flush anything pending as
521 * not to skew our latency numbers.
522 *
523 * Changed to only monitor 'in flight' requests here instead of the
524 * td->cur_depth, b/c td->cur_depth does not accurately represent
525 * io's that have been actually submitted to an async engine,
526 * and cur_depth is meaningless for sync engines.
527 */
528 while (td->io_u_in_flight) {
529 int fio_unused ret;
530
531 ret = io_u_queued_complete(td, 1, NULL);
532 }
533}
534
581e7141
JA
535static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
536{
537 enum fio_ddir odir = ddir ^ 1;
538 struct timeval t;
539 long usec;
540
ff58fced
JA
541 assert(ddir_rw(ddir));
542
315fcfec 543 if (td->rate_pending_usleep[ddir] <= 0)
581e7141
JA
544 return ddir;
545
546 /*
547 * We have too much pending sleep in this direction. See if we
548 * should switch.
549 */
315fcfec 550 if (td_rw(td) && td->o.rwmix[odir]) {
581e7141
JA
551 /*
552 * Other direction does not have too much pending, switch
553 */
554 if (td->rate_pending_usleep[odir] < 100000)
555 return odir;
556
557 /*
558 * Both directions have pending sleep. Sleep the minimum time
559 * and deduct from both.
560 */
561 if (td->rate_pending_usleep[ddir] <=
562 td->rate_pending_usleep[odir]) {
563 usec = td->rate_pending_usleep[ddir];
564 } else {
565 usec = td->rate_pending_usleep[odir];
566 ddir = odir;
567 }
568 } else
569 usec = td->rate_pending_usleep[ddir];
570
002e7183 571 io_u_quiesce(td);
78c1eda5 572
581e7141
JA
573 fio_gettime(&t, NULL);
574 usec_sleep(td, usec);
575 usec = utime_since_now(&t);
576
577 td->rate_pending_usleep[ddir] -= usec;
578
579 odir = ddir ^ 1;
580 if (td_rw(td) && __should_check_rate(td, odir))
581 td->rate_pending_usleep[odir] -= usec;
0b9d69ec 582
6eaf09d6
SL
583 if (ddir_trim(ddir))
584 return ddir;
e0224c6b 585
581e7141
JA
586 return ddir;
587}
588
10ba535a
JA
589/*
590 * Return the data direction for the next io_u. If the job is a
591 * mixed read/write workload, check the rwmix cycle and switch if
592 * necessary.
593 */
1e97cce9 594static enum fio_ddir get_rw_ddir(struct thread_data *td)
10ba535a 595{
581e7141
JA
596 enum fio_ddir ddir;
597
5f9099ea
JA
598 /*
599 * see if it's time to fsync
600 */
601 if (td->o.fsync_blocks &&
602 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
603 td->io_issues[DDIR_WRITE] && should_fsync(td))
604 return DDIR_SYNC;
605
606 /*
607 * see if it's time to fdatasync
608 */
609 if (td->o.fdatasync_blocks &&
610 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
611 td->io_issues[DDIR_WRITE] && should_fsync(td))
612 return DDIR_DATASYNC;
613
44f29692
JA
614 /*
615 * see if it's time to sync_file_range
616 */
617 if (td->sync_file_range_nr &&
618 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
619 td->io_issues[DDIR_WRITE] && should_fsync(td))
620 return DDIR_SYNC_FILE_RANGE;
621
10ba535a 622 if (td_rw(td)) {
10ba535a
JA
623 /*
624 * Check if it's time to seed a new data direction.
625 */
e4928662 626 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
e47f799f
JA
627 /*
628 * Put a top limit on how many bytes we do for
629 * one data direction, to avoid overflowing the
630 * ranges too much
631 */
632 ddir = get_rand_ddir(td);
e47f799f
JA
633
634 if (ddir != td->rwmix_ddir)
635 set_rwmix_bytes(td);
636
637 td->rwmix_ddir = ddir;
10ba535a 638 }
581e7141 639 ddir = td->rwmix_ddir;
10ba535a 640 } else if (td_read(td))
581e7141 641 ddir = DDIR_READ;
6eaf09d6 642 else if (td_write(td))
581e7141 643 ddir = DDIR_WRITE;
6eaf09d6
SL
644 else
645 ddir = DDIR_TRIM;
581e7141
JA
646
647 td->rwmix_ddir = rate_ddir(td, ddir);
648 return td->rwmix_ddir;
10ba535a
JA
649}
650
1ef2b6be
JA
651static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
652{
bcd5abfa 653 io_u->ddir = io_u->acct_ddir = get_rw_ddir(td);
1ef2b6be
JA
654
655 if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
656 td->o.barrier_blocks &&
657 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
658 td->io_issues[DDIR_WRITE])
659 io_u->flags |= IO_U_F_BARRIER;
660}
661
e8462bd8 662void put_file_log(struct thread_data *td, struct fio_file *f)
60f2c658 663{
71b84caa 664 unsigned int ret = put_file(td, f);
60f2c658
JA
665
666 if (ret)
667 td_verror(td, ret, "file close");
668}
669
10ba535a
JA
670void put_io_u(struct thread_data *td, struct io_u *io_u)
671{
e8462bd8
JA
672 td_io_u_lock(td);
673
f8b0bd10 674 if (io_u->file && !(io_u->flags & IO_U_F_NO_FILE_PUT))
60f2c658 675 put_file_log(td, io_u->file);
f8b0bd10 676
10ba535a 677 io_u->file = NULL;
d7ee2a7d
SL
678 io_u->flags |= IO_U_F_FREE;
679
0c41214f
RR
680 if (io_u->flags & IO_U_F_IN_CUR_DEPTH)
681 td->cur_depth--;
2ae0b204 682 io_u_qpush(&td->io_u_freelist, io_u);
e8462bd8
JA
683 td_io_u_unlock(td);
684 td_io_u_free_notify(td);
10ba535a
JA
685}
686
f2bba182
RR
687void clear_io_u(struct thread_data *td, struct io_u *io_u)
688{
689 io_u->flags &= ~IO_U_F_FLIGHT;
690 put_io_u(td, io_u);
691}
692
755200a3
JA
693void requeue_io_u(struct thread_data *td, struct io_u **io_u)
694{
695 struct io_u *__io_u = *io_u;
bcd5abfa 696 enum fio_ddir ddir = acct_ddir(__io_u);
755200a3 697
465221b0
JA
698 dprint(FD_IO, "requeue %p\n", __io_u);
699
e8462bd8
JA
700 td_io_u_lock(td);
701
4d2e0f49 702 __io_u->flags |= IO_U_F_FREE;
bcd5abfa
JA
703 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(ddir))
704 td->io_issues[ddir]--;
5ec10eaa 705
4d2e0f49 706 __io_u->flags &= ~IO_U_F_FLIGHT;
0c41214f
RR
707 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH)
708 td->cur_depth--;
2ae0b204
JA
709
710 io_u_rpush(&td->io_u_requeues, __io_u);
e8462bd8 711 td_io_u_unlock(td);
755200a3
JA
712 *io_u = NULL;
713}
714
9bf2061e 715static int fill_io_u(struct thread_data *td, struct io_u *io_u)
10ba535a 716{
6aca9b3d
JA
717 unsigned int is_random;
718
b4c5e1ac
JA
719 if (td->io_ops->flags & FIO_NOIO)
720 goto out;
721
1ef2b6be 722 set_rw_ddir(td, io_u);
5f9099ea 723
87dc1ab1 724 /*
ff58fced 725 * fsync() or fdatasync() or trim etc, we are done
87dc1ab1 726 */
ff58fced 727 if (!ddir_rw(io_u->ddir))
c38e9468 728 goto out;
a00735e6 729
48f5abd3
JA
730 /*
731 * See if it's time to switch to a new zone
732 */
13af05ae 733 if (td->zone_bytes >= td->o.zone_size && td->o.zone_skip) {
4c8be5b1
JA
734 struct fio_file *f = io_u->file;
735
48f5abd3 736 td->zone_bytes = 0;
4c8be5b1
JA
737 f->file_offset += td->o.zone_range + td->o.zone_skip;
738
739 /*
740 * Wrap from the beginning, if we exceed the file size
741 */
742 if (f->file_offset >= f->real_file_size)
743 f->file_offset = f->real_file_size - f->file_offset;
744 f->last_pos = f->file_offset;
48f5abd3
JA
745 td->io_skip_bytes += td->o.zone_skip;
746 }
747
10ba535a 748 /*
c685b5b2
JA
749 * No log, let the seq/rand engine retrieve the next buflen and
750 * position.
10ba535a 751 */
6aca9b3d 752 if (get_next_offset(td, io_u, &is_random)) {
2ba1c290 753 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
bca4ed4d 754 return 1;
2ba1c290 755 }
10ba535a 756
6aca9b3d 757 io_u->buflen = get_next_buflen(td, io_u, is_random);
2ba1c290
JA
758 if (!io_u->buflen) {
759 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
bca4ed4d 760 return 1;
2ba1c290 761 }
bca4ed4d 762
2ba1c290
JA
763 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
764 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
4b91ee8f
JA
765 dprint(FD_IO, " off=%llu/%lu > %llu\n",
766 (unsigned long long) io_u->offset, io_u->buflen,
767 (unsigned long long) io_u->file->real_file_size);
6a5e6884 768 return 1;
2ba1c290 769 }
6a5e6884 770
bca4ed4d
JA
771 /*
772 * mark entry before potentially trimming io_u
773 */
303032ae 774 if (td_random(td) && file_randommap(td, io_u->file))
9bf2061e 775 mark_random_map(td, io_u);
bca4ed4d 776
c38e9468 777out:
2ba1c290 778 dprint_io_u(io_u, "fill_io_u");
d9d91e39 779 td->zone_bytes += io_u->buflen;
bca4ed4d 780 return 0;
10ba535a
JA
781}
782
838bc709
JA
783static void __io_u_mark_map(unsigned int *map, unsigned int nr)
784{
2b13e716 785 int idx = 0;
838bc709
JA
786
787 switch (nr) {
788 default:
2b13e716 789 idx = 6;
838bc709
JA
790 break;
791 case 33 ... 64:
2b13e716 792 idx = 5;
838bc709
JA
793 break;
794 case 17 ... 32:
2b13e716 795 idx = 4;
838bc709
JA
796 break;
797 case 9 ... 16:
2b13e716 798 idx = 3;
838bc709
JA
799 break;
800 case 5 ... 8:
2b13e716 801 idx = 2;
838bc709
JA
802 break;
803 case 1 ... 4:
2b13e716 804 idx = 1;
838bc709
JA
805 case 0:
806 break;
807 }
808
2b13e716 809 map[idx]++;
838bc709
JA
810}
811
812void io_u_mark_submit(struct thread_data *td, unsigned int nr)
813{
814 __io_u_mark_map(td->ts.io_u_submit, nr);
815 td->ts.total_submit++;
816}
817
818void io_u_mark_complete(struct thread_data *td, unsigned int nr)
819{
820 __io_u_mark_map(td->ts.io_u_complete, nr);
821 td->ts.total_complete++;
822}
823
d8005759 824void io_u_mark_depth(struct thread_data *td, unsigned int nr)
71619dc2 825{
2b13e716 826 int idx = 0;
71619dc2
JA
827
828 switch (td->cur_depth) {
829 default:
2b13e716 830 idx = 6;
a783e61a 831 break;
71619dc2 832 case 32 ... 63:
2b13e716 833 idx = 5;
a783e61a 834 break;
71619dc2 835 case 16 ... 31:
2b13e716 836 idx = 4;
a783e61a 837 break;
71619dc2 838 case 8 ... 15:
2b13e716 839 idx = 3;
a783e61a 840 break;
71619dc2 841 case 4 ... 7:
2b13e716 842 idx = 2;
a783e61a 843 break;
71619dc2 844 case 2 ... 3:
2b13e716 845 idx = 1;
71619dc2
JA
846 case 1:
847 break;
848 }
849
2b13e716 850 td->ts.io_u_map[idx] += nr;
71619dc2
JA
851}
852
04a0feae
JA
853static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
854{
2b13e716 855 int idx = 0;
04a0feae
JA
856
857 assert(usec < 1000);
858
859 switch (usec) {
860 case 750 ... 999:
2b13e716 861 idx = 9;
04a0feae
JA
862 break;
863 case 500 ... 749:
2b13e716 864 idx = 8;
04a0feae
JA
865 break;
866 case 250 ... 499:
2b13e716 867 idx = 7;
04a0feae
JA
868 break;
869 case 100 ... 249:
2b13e716 870 idx = 6;
04a0feae
JA
871 break;
872 case 50 ... 99:
2b13e716 873 idx = 5;
04a0feae
JA
874 break;
875 case 20 ... 49:
2b13e716 876 idx = 4;
04a0feae
JA
877 break;
878 case 10 ... 19:
2b13e716 879 idx = 3;
04a0feae
JA
880 break;
881 case 4 ... 9:
2b13e716 882 idx = 2;
04a0feae
JA
883 break;
884 case 2 ... 3:
2b13e716 885 idx = 1;
04a0feae
JA
886 case 0 ... 1:
887 break;
888 }
889
2b13e716
JA
890 assert(idx < FIO_IO_U_LAT_U_NR);
891 td->ts.io_u_lat_u[idx]++;
04a0feae
JA
892}
893
894static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
ec118304 895{
2b13e716 896 int idx = 0;
ec118304
JA
897
898 switch (msec) {
899 default:
2b13e716 900 idx = 11;
04a0feae 901 break;
8abdce66 902 case 1000 ... 1999:
2b13e716 903 idx = 10;
04a0feae 904 break;
8abdce66 905 case 750 ... 999:
2b13e716 906 idx = 9;
04a0feae 907 break;
8abdce66 908 case 500 ... 749:
2b13e716 909 idx = 8;
04a0feae 910 break;
8abdce66 911 case 250 ... 499:
2b13e716 912 idx = 7;
04a0feae 913 break;
8abdce66 914 case 100 ... 249:
2b13e716 915 idx = 6;
04a0feae 916 break;
8abdce66 917 case 50 ... 99:
2b13e716 918 idx = 5;
04a0feae 919 break;
8abdce66 920 case 20 ... 49:
2b13e716 921 idx = 4;
04a0feae 922 break;
8abdce66 923 case 10 ... 19:
2b13e716 924 idx = 3;
04a0feae 925 break;
8abdce66 926 case 4 ... 9:
2b13e716 927 idx = 2;
04a0feae 928 break;
ec118304 929 case 2 ... 3:
2b13e716 930 idx = 1;
ec118304
JA
931 case 0 ... 1:
932 break;
933 }
934
2b13e716
JA
935 assert(idx < FIO_IO_U_LAT_M_NR);
936 td->ts.io_u_lat_m[idx]++;
04a0feae
JA
937}
938
939static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
940{
941 if (usec < 1000)
942 io_u_mark_lat_usec(td, usec);
943 else
944 io_u_mark_lat_msec(td, usec / 1000);
ec118304
JA
945}
946
0aabe160
JA
947/*
948 * Get next file to service by choosing one at random
949 */
2cc52930
JA
950static struct fio_file *get_next_file_rand(struct thread_data *td,
951 enum fio_file_flags goodf,
d6aed795 952 enum fio_file_flags badf)
0aabe160 953{
0aabe160 954 struct fio_file *f;
1c178180 955 int fno;
0aabe160
JA
956
957 do {
87b10676 958 int opened = 0;
1294c3ec 959 unsigned long r;
4c07ad86 960
d6b72507 961 r = __rand(&td->next_file_state);
54a21917 962 fno = (unsigned int) ((double) td->o.nr_files
4c07ad86 963 * (r / (FRAND_MAX + 1.0)));
7c83c089 964
126d65c6 965 f = td->files[fno];
d6aed795 966 if (fio_file_done(f))
059e63c0 967 continue;
1c178180 968
d6aed795 969 if (!fio_file_open(f)) {
87b10676
JA
970 int err;
971
002fe734
JA
972 if (td->nr_open_files >= td->o.open_files)
973 return ERR_PTR(-EBUSY);
974
87b10676
JA
975 err = td_io_open_file(td, f);
976 if (err)
977 continue;
978 opened = 1;
979 }
980
2ba1c290
JA
981 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
982 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
0aabe160 983 return f;
2ba1c290 984 }
87b10676
JA
985 if (opened)
986 td_io_close_file(td, f);
0aabe160
JA
987 } while (1);
988}
989
990/*
991 * Get next file to service by doing round robin between all available ones
992 */
1c178180
JA
993static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
994 int badf)
3d7c391d
JA
995{
996 unsigned int old_next_file = td->next_file;
997 struct fio_file *f;
998
999 do {
87b10676
JA
1000 int opened = 0;
1001
126d65c6 1002 f = td->files[td->next_file];
3d7c391d
JA
1003
1004 td->next_file++;
2dc1bbeb 1005 if (td->next_file >= td->o.nr_files)
3d7c391d
JA
1006 td->next_file = 0;
1007
87b10676 1008 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
d6aed795 1009 if (fio_file_done(f)) {
d5ed68ea 1010 f = NULL;
059e63c0 1011 continue;
d5ed68ea 1012 }
059e63c0 1013
d6aed795 1014 if (!fio_file_open(f)) {
87b10676
JA
1015 int err;
1016
002fe734
JA
1017 if (td->nr_open_files >= td->o.open_files)
1018 return ERR_PTR(-EBUSY);
1019
87b10676 1020 err = td_io_open_file(td, f);
b5696bfc
JA
1021 if (err) {
1022 dprint(FD_FILE, "error %d on open of %s\n",
1023 err, f->file_name);
87c27b45 1024 f = NULL;
87b10676 1025 continue;
b5696bfc 1026 }
87b10676
JA
1027 opened = 1;
1028 }
1029
0b9d69ec
JA
1030 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
1031 f->flags);
1c178180 1032 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
3d7c391d
JA
1033 break;
1034
87b10676
JA
1035 if (opened)
1036 td_io_close_file(td, f);
1037
3d7c391d
JA
1038 f = NULL;
1039 } while (td->next_file != old_next_file);
1040
2ba1c290 1041 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
3d7c391d
JA
1042 return f;
1043}
1044
7eb36574 1045static struct fio_file *__get_next_file(struct thread_data *td)
bdb4e2e9 1046{
1907dbc6
JA
1047 struct fio_file *f;
1048
2dc1bbeb 1049 assert(td->o.nr_files <= td->files_index);
1c178180 1050
b5696bfc 1051 if (td->nr_done_files >= td->o.nr_files) {
5ec10eaa
JA
1052 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
1053 " nr_files=%d\n", td->nr_open_files,
1054 td->nr_done_files,
1055 td->o.nr_files);
bdb4e2e9 1056 return NULL;
2ba1c290 1057 }
bdb4e2e9 1058
1907dbc6 1059 f = td->file_service_file;
d6aed795 1060 if (f && fio_file_open(f) && !fio_file_closing(f)) {
a086c257
JA
1061 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
1062 goto out;
1063 if (td->file_service_left--)
1064 goto out;
1065 }
1907dbc6 1066
a086c257
JA
1067 if (td->o.file_service_type == FIO_FSERVICE_RR ||
1068 td->o.file_service_type == FIO_FSERVICE_SEQ)
d6aed795 1069 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
bdb4e2e9 1070 else
d6aed795 1071 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1907dbc6 1072
002fe734
JA
1073 if (IS_ERR(f))
1074 return f;
1075
1907dbc6
JA
1076 td->file_service_file = f;
1077 td->file_service_left = td->file_service_nr - 1;
2ba1c290 1078out:
0dac421f
JA
1079 if (f)
1080 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1081 else
1082 dprint(FD_FILE, "get_next_file: NULL\n");
1907dbc6 1083 return f;
bdb4e2e9
JA
1084}
1085
7eb36574
JA
1086static struct fio_file *get_next_file(struct thread_data *td)
1087{
372d8962 1088 if (td->flags & TD_F_PROFILE_OPS) {
d72be545 1089 struct prof_io_ops *ops = &td->prof_io_ops;
7eb36574 1090
d72be545
JA
1091 if (ops->get_next_file)
1092 return ops->get_next_file(td);
1093 }
7eb36574
JA
1094
1095 return __get_next_file(td);
1096}
1097
002fe734 1098static long set_io_u_file(struct thread_data *td, struct io_u *io_u)
429f6675
JA
1099{
1100 struct fio_file *f;
1101
1102 do {
1103 f = get_next_file(td);
002fe734
JA
1104 if (IS_ERR_OR_NULL(f))
1105 return PTR_ERR(f);
429f6675 1106
429f6675
JA
1107 io_u->file = f;
1108 get_file(f);
1109
1110 if (!fill_io_u(td, io_u))
1111 break;
1112
b5696bfc 1113 put_file_log(td, f);
429f6675 1114 td_io_close_file(td, f);
b5696bfc 1115 io_u->file = NULL;
d6aed795 1116 fio_file_set_done(f);
429f6675 1117 td->nr_done_files++;
0b9d69ec
JA
1118 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
1119 td->nr_done_files, td->o.nr_files);
429f6675
JA
1120 } while (1);
1121
1122 return 0;
1123}
1124
3e260a46
JA
1125static void lat_fatal(struct thread_data *td, struct io_completion_data *icd,
1126 unsigned long tusec, unsigned long max_usec)
1127{
1128 if (!td->error)
1129 log_err("fio: latency of %lu usec exceeds specified max (%lu usec)\n", tusec, max_usec);
1130 td_verror(td, ETIMEDOUT, "max latency exceeded");
1131 icd->error = ETIMEDOUT;
1132}
1133
1134static void lat_new_cycle(struct thread_data *td)
1135{
1136 fio_gettime(&td->latency_ts, NULL);
1137 td->latency_ios = ddir_rw_sum(td->io_blocks);
1138 td->latency_failed = 0;
1139}
1140
1141/*
1142 * We had an IO outside the latency target. Reduce the queue depth. If we
1143 * are at QD=1, then it's time to give up.
1144 */
1145static int __lat_target_failed(struct thread_data *td)
1146{
1147 if (td->latency_qd == 1)
1148 return 1;
1149
1150 td->latency_qd_high = td->latency_qd;
6bb58215
JA
1151
1152 if (td->latency_qd == td->latency_qd_low)
1153 td->latency_qd_low--;
1154
3e260a46
JA
1155 td->latency_qd = (td->latency_qd + td->latency_qd_low) / 2;
1156
1157 dprint(FD_RATE, "Ramped down: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
1158
1159 /*
1160 * When we ramp QD down, quiesce existing IO to prevent
1161 * a storm of ramp downs due to pending higher depth.
1162 */
1163 io_u_quiesce(td);
1164 lat_new_cycle(td);
1165 return 0;
1166}
1167
1168static int lat_target_failed(struct thread_data *td)
1169{
1170 if (td->o.latency_percentile.u.f == 100.0)
1171 return __lat_target_failed(td);
1172
1173 td->latency_failed++;
1174 return 0;
1175}
1176
1177void lat_target_init(struct thread_data *td)
1178{
6bb58215
JA
1179 td->latency_end_run = 0;
1180
3e260a46
JA
1181 if (td->o.latency_target) {
1182 dprint(FD_RATE, "Latency target=%llu\n", td->o.latency_target);
1183 fio_gettime(&td->latency_ts, NULL);
1184 td->latency_qd = 1;
1185 td->latency_qd_high = td->o.iodepth;
1186 td->latency_qd_low = 1;
1187 td->latency_ios = ddir_rw_sum(td->io_blocks);
1188 } else
1189 td->latency_qd = td->o.iodepth;
1190}
1191
6bb58215
JA
1192void lat_target_reset(struct thread_data *td)
1193{
1194 if (!td->latency_end_run)
1195 lat_target_init(td);
1196}
1197
3e260a46
JA
1198static void lat_target_success(struct thread_data *td)
1199{
1200 const unsigned int qd = td->latency_qd;
6bb58215 1201 struct thread_options *o = &td->o;
3e260a46
JA
1202
1203 td->latency_qd_low = td->latency_qd;
1204
1205 /*
1206 * If we haven't failed yet, we double up to a failing value instead
1207 * of bisecting from highest possible queue depth. If we have set
1208 * a limit other than td->o.iodepth, bisect between that.
1209 */
6bb58215 1210 if (td->latency_qd_high != o->iodepth)
3e260a46
JA
1211 td->latency_qd = (td->latency_qd + td->latency_qd_high) / 2;
1212 else
1213 td->latency_qd *= 2;
1214
6bb58215
JA
1215 if (td->latency_qd > o->iodepth)
1216 td->latency_qd = o->iodepth;
3e260a46
JA
1217
1218 dprint(FD_RATE, "Ramped up: %d %d %d\n", td->latency_qd_low, td->latency_qd, td->latency_qd_high);
6bb58215 1219
3e260a46 1220 /*
6bb58215
JA
1221 * Same as last one, we are done. Let it run a latency cycle, so
1222 * we get only the results from the targeted depth.
3e260a46 1223 */
6bb58215
JA
1224 if (td->latency_qd == qd) {
1225 if (td->latency_end_run) {
1226 dprint(FD_RATE, "We are done\n");
1227 td->done = 1;
1228 } else {
1229 dprint(FD_RATE, "Quiesce and final run\n");
1230 io_u_quiesce(td);
1231 td->latency_end_run = 1;
1232 reset_all_stats(td);
1233 reset_io_stats(td);
1234 }
1235 }
3e260a46
JA
1236
1237 lat_new_cycle(td);
1238}
1239
1240/*
1241 * Check if we can bump the queue depth
1242 */
1243void lat_target_check(struct thread_data *td)
1244{
1245 uint64_t usec_window;
1246 uint64_t ios;
1247 double success_ios;
1248
1249 usec_window = utime_since_now(&td->latency_ts);
1250 if (usec_window < td->o.latency_window)
1251 return;
1252
1253 ios = ddir_rw_sum(td->io_blocks) - td->latency_ios;
1254 success_ios = (double) (ios - td->latency_failed) / (double) ios;
1255 success_ios *= 100.0;
1256
1257 dprint(FD_RATE, "Success rate: %.2f%% (target %.2f%%)\n", success_ios, td->o.latency_percentile.u.f);
1258
1259 if (success_ios >= td->o.latency_percentile.u.f)
1260 lat_target_success(td);
1261 else
1262 __lat_target_failed(td);
1263}
1264
1265/*
1266 * If latency target is enabled, we might be ramping up or down and not
1267 * using the full queue depth available.
1268 */
effd6ff0 1269int queue_full(const struct thread_data *td)
3e260a46
JA
1270{
1271 const int qempty = io_u_qempty(&td->io_u_freelist);
1272
1273 if (qempty)
1274 return 1;
1275 if (!td->o.latency_target)
1276 return 0;
1277
1278 return td->cur_depth >= td->latency_qd;
1279}
429f6675 1280
10ba535a
JA
1281struct io_u *__get_io_u(struct thread_data *td)
1282{
0cae66f6 1283 struct io_u *io_u = NULL;
10ba535a 1284
e8462bd8
JA
1285 td_io_u_lock(td);
1286
1287again:
2ae0b204
JA
1288 if (!io_u_rempty(&td->io_u_requeues))
1289 io_u = io_u_rpop(&td->io_u_requeues);
3e260a46 1290 else if (!queue_full(td)) {
2ae0b204 1291 io_u = io_u_qpop(&td->io_u_freelist);
10ba535a 1292
225ba9e3 1293 io_u->file = NULL;
6040dabc 1294 io_u->buflen = 0;
10ba535a 1295 io_u->resid = 0;
d7762cf8 1296 io_u->end_io = NULL;
755200a3
JA
1297 }
1298
1299 if (io_u) {
0c6e7517 1300 assert(io_u->flags & IO_U_F_FREE);
f8b0bd10
JA
1301 io_u->flags &= ~(IO_U_F_FREE | IO_U_F_NO_FILE_PUT |
1302 IO_U_F_TRIMMED | IO_U_F_BARRIER |
1303 IO_U_F_VER_LIST);
0c6e7517 1304
755200a3 1305 io_u->error = 0;
bcd5abfa 1306 io_u->acct_ddir = -1;
10ba535a 1307 td->cur_depth++;
0c41214f 1308 io_u->flags |= IO_U_F_IN_CUR_DEPTH;
f9401285 1309 io_u->ipo = NULL;
1dec3e07
JA
1310 } else if (td->o.verify_async) {
1311 /*
1312 * We ran out, wait for async verify threads to finish and
1313 * return one
1314 */
1315 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1316 goto again;
10ba535a
JA
1317 }
1318
e8462bd8 1319 td_io_u_unlock(td);
10ba535a
JA
1320 return io_u;
1321}
1322
0d29de83 1323static int check_get_trim(struct thread_data *td, struct io_u *io_u)
10ba535a 1324{
d72be545
JA
1325 if (!(td->flags & TD_F_TRIM_BACKLOG))
1326 return 0;
1327
1328 if (td->trim_entries) {
0d29de83 1329 int get_trim = 0;
10ba535a 1330
0d29de83
JA
1331 if (td->trim_batch) {
1332 td->trim_batch--;
1333 get_trim = 1;
1334 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1335 td->last_ddir != DDIR_READ) {
1336 td->trim_batch = td->o.trim_batch;
1337 if (!td->trim_batch)
1338 td->trim_batch = td->o.trim_backlog;
1339 get_trim = 1;
1340 }
1341
1342 if (get_trim && !get_next_trim(td, io_u))
1343 return 1;
2ba1c290 1344 }
10ba535a 1345
0d29de83
JA
1346 return 0;
1347}
1348
1349static int check_get_verify(struct thread_data *td, struct io_u *io_u)
1350{
d72be545
JA
1351 if (!(td->flags & TD_F_VER_BACKLOG))
1352 return 0;
1353
1354 if (td->io_hist_len) {
9e144189
JA
1355 int get_verify = 0;
1356
d1ece0c7 1357 if (td->verify_batch)
9e144189 1358 get_verify = 1;
d1ece0c7 1359 else if (!(td->io_hist_len % td->o.verify_backlog) &&
9e144189
JA
1360 td->last_ddir != DDIR_READ) {
1361 td->verify_batch = td->o.verify_batch;
f8a75c99
JA
1362 if (!td->verify_batch)
1363 td->verify_batch = td->o.verify_backlog;
9e144189
JA
1364 get_verify = 1;
1365 }
1366
d1ece0c7
JA
1367 if (get_verify && !get_next_verify(td, io_u)) {
1368 td->verify_batch--;
0d29de83 1369 return 1;
d1ece0c7 1370 }
9e144189
JA
1371 }
1372
0d29de83
JA
1373 return 0;
1374}
1375
de789769
JA
1376/*
1377 * Fill offset and start time into the buffer content, to prevent too
23f394d5
JA
1378 * easy compressible data for simple de-dupe attempts. Do this for every
1379 * 512b block in the range, since that should be the smallest block size
1380 * we can expect from a device.
de789769
JA
1381 */
1382static void small_content_scramble(struct io_u *io_u)
1383{
23f394d5 1384 unsigned int i, nr_blocks = io_u->buflen / 512;
1ae83d45 1385 uint64_t boffset;
23f394d5
JA
1386 unsigned int offset;
1387 void *p, *end;
de789769 1388
23f394d5
JA
1389 if (!nr_blocks)
1390 return;
1391
1392 p = io_u->xfer_buf;
fba76ee8 1393 boffset = io_u->offset;
81f0366c 1394 io_u->buf_filled_len = 0;
fad82f76 1395
23f394d5
JA
1396 for (i = 0; i < nr_blocks; i++) {
1397 /*
1398 * Fill the byte offset into a "random" start offset of
1399 * the buffer, given by the product of the usec time
1400 * and the actual offset.
1401 */
fad82f76 1402 offset = (io_u->start_time.tv_usec ^ boffset) & 511;
1ae83d45
JA
1403 offset &= ~(sizeof(uint64_t) - 1);
1404 if (offset >= 512 - sizeof(uint64_t))
1405 offset -= sizeof(uint64_t);
fba76ee8 1406 memcpy(p + offset, &boffset, sizeof(boffset));
23f394d5
JA
1407
1408 end = p + 512 - sizeof(io_u->start_time);
1409 memcpy(end, &io_u->start_time, sizeof(io_u->start_time));
1410 p += 512;
fad82f76 1411 boffset += 512;
23f394d5 1412 }
de789769
JA
1413}
1414
0d29de83
JA
1415/*
1416 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1417 * etc. The returned io_u is fully ready to be prepped and submitted.
1418 */
1419struct io_u *get_io_u(struct thread_data *td)
1420{
1421 struct fio_file *f;
1422 struct io_u *io_u;
de789769 1423 int do_scramble = 0;
002fe734 1424 long ret = 0;
0d29de83
JA
1425
1426 io_u = __get_io_u(td);
1427 if (!io_u) {
1428 dprint(FD_IO, "__get_io_u failed\n");
1429 return NULL;
1430 }
1431
1432 if (check_get_verify(td, io_u))
1433 goto out;
1434 if (check_get_trim(td, io_u))
1435 goto out;
1436
755200a3
JA
1437 /*
1438 * from a requeue, io_u already setup
1439 */
1440 if (io_u->file)
77f392bf 1441 goto out;
755200a3 1442
429f6675
JA
1443 /*
1444 * If using an iolog, grab next piece if any available.
1445 */
d72be545 1446 if (td->flags & TD_F_READ_IOLOG) {
429f6675
JA
1447 if (read_iolog_get(td, io_u))
1448 goto err_put;
2ba1c290 1449 } else if (set_io_u_file(td, io_u)) {
002fe734 1450 ret = -EBUSY;
2ba1c290 1451 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
429f6675 1452 goto err_put;
2ba1c290 1453 }
5ec10eaa 1454
429f6675 1455 f = io_u->file;
002fe734
JA
1456 if (!f) {
1457 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
1458 goto err_put;
1459 }
1460
d6aed795 1461 assert(fio_file_open(f));
97af62ce 1462
ff58fced 1463 if (ddir_rw(io_u->ddir)) {
d0656a93 1464 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
2ba1c290 1465 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
429f6675 1466 goto err_put;
2ba1c290 1467 }
10ba535a 1468
38dad62d 1469 f->last_start = io_u->offset;
36167d82 1470 f->last_pos = io_u->offset + io_u->buflen;
10ba535a 1471
fd68418e 1472 if (io_u->ddir == DDIR_WRITE) {
d72be545 1473 if (td->flags & TD_F_REFILL_BUFFERS) {
9c42684e 1474 io_u_fill_buffer(td, io_u,
1066358a
JA
1475 td->o.min_bs[DDIR_WRITE],
1476 io_u->xfer_buflen);
bedc9dc2
JA
1477 } else if ((td->flags & TD_F_SCRAMBLE_BUFFERS) &&
1478 !(td->flags & TD_F_COMPRESS))
fd68418e 1479 do_scramble = 1;
d72be545 1480 if (td->flags & TD_F_VER_NONE) {
629f1d71
JA
1481 populate_verify_io_u(td, io_u);
1482 do_scramble = 0;
1483 }
fd68418e 1484 } else if (io_u->ddir == DDIR_READ) {
cbe8d756
RR
1485 /*
1486 * Reset the buf_filled parameters so next time if the
1487 * buffer is used for writes it is refilled.
1488 */
cbe8d756
RR
1489 io_u->buf_filled_len = 0;
1490 }
87dc1ab1 1491 }
10ba535a 1492
165faf16
JA
1493 /*
1494 * Set io data pointers.
1495 */
cec6b55d
JA
1496 io_u->xfer_buf = io_u->buf;
1497 io_u->xfer_buflen = io_u->buflen;
5973cafb 1498
6ac7a331 1499out:
0d29de83 1500 assert(io_u->file);
429f6675 1501 if (!td_io_prep(td, io_u)) {
993bf48b
JA
1502 if (!td->o.disable_slat)
1503 fio_gettime(&io_u->start_time, NULL);
de789769
JA
1504 if (do_scramble)
1505 small_content_scramble(io_u);
429f6675 1506 return io_u;
36167d82 1507 }
429f6675 1508err_put:
2ba1c290 1509 dprint(FD_IO, "get_io_u failed\n");
429f6675 1510 put_io_u(td, io_u);
002fe734 1511 return ERR_PTR(ret);
10ba535a
JA
1512}
1513
5451792e
JA
1514void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1515{
8b28bd41 1516 enum error_type_bit eb = td_error_type(io_u->ddir, io_u->error);
825f818e 1517
8b28bd41
DM
1518 if (td_non_fatal_error(td, eb, io_u->error) && !td->o.error_dump)
1519 return;
5451792e 1520
709c8313
RE
1521 log_err("fio: io_u error%s%s: %s: %s offset=%llu, buflen=%lu\n",
1522 io_u->file ? " on file " : "",
1523 io_u->file ? io_u->file->file_name : "",
1524 strerror(io_u->error),
1525 io_ddir_name(io_u->ddir),
1526 io_u->offset, io_u->xfer_buflen);
5451792e
JA
1527
1528 if (!td->error)
1529 td_verror(td, io_u->error, "io_u error");
1530}
1531
aba6c951
JA
1532static inline int gtod_reduce(struct thread_data *td)
1533{
729fe3af 1534 return td->o.disable_clat && td->o.disable_lat && td->o.disable_slat
b74b8208 1535 && td->o.disable_bw;
aba6c951
JA
1536}
1537
c8eeb9df
JA
1538static void account_io_completion(struct thread_data *td, struct io_u *io_u,
1539 struct io_completion_data *icd,
1540 const enum fio_ddir idx, unsigned int bytes)
1541{
24d23ca7 1542 unsigned long lusec = 0;
c8eeb9df 1543
aba6c951 1544 if (!gtod_reduce(td))
c8eeb9df
JA
1545 lusec = utime_since(&io_u->issue_time, &icd->time);
1546
1547 if (!td->o.disable_lat) {
1548 unsigned long tusec;
1549
1550 tusec = utime_since(&io_u->start_time, &icd->time);
ae588852 1551 add_lat_sample(td, idx, tusec, bytes, io_u->offset);
15501535 1552
d4afedfd
JA
1553 if (td->flags & TD_F_PROFILE_OPS) {
1554 struct prof_io_ops *ops = &td->prof_io_ops;
1555
1556 if (ops->io_u_lat)
1557 icd->error = ops->io_u_lat(td, tusec);
1558 }
1559
3e260a46
JA
1560 if (td->o.max_latency && tusec > td->o.max_latency)
1561 lat_fatal(td, icd, tusec, td->o.max_latency);
1562 if (td->o.latency_target && tusec > td->o.latency_target) {
1563 if (lat_target_failed(td))
1564 lat_fatal(td, icd, tusec, td->o.latency_target);
15501535 1565 }
c8eeb9df
JA
1566 }
1567
1568 if (!td->o.disable_clat) {
ae588852 1569 add_clat_sample(td, idx, lusec, bytes, io_u->offset);
c8eeb9df
JA
1570 io_u_mark_latency(td, lusec);
1571 }
1572
1573 if (!td->o.disable_bw)
1574 add_bw_sample(td, idx, bytes, &icd->time);
1575
aba6c951
JA
1576 if (!gtod_reduce(td))
1577 add_iops_sample(td, idx, bytes, &icd->time);
c8eeb9df
JA
1578}
1579
1b8dbf25
SL
1580static long long usec_for_io(struct thread_data *td, enum fio_ddir ddir)
1581{
1ae83d45
JA
1582 uint64_t secs, remainder, bps, bytes;
1583
1b8dbf25
SL
1584 bytes = td->this_io_bytes[ddir];
1585 bps = td->rate_bps[ddir];
1586 secs = bytes / bps;
1587 remainder = bytes % bps;
1588 return remainder * 1000000 / bps + secs * 1000000;
1589}
1590
f8b0bd10 1591static void io_completed(struct thread_data *td, struct io_u **io_u_ptr,
97601024 1592 struct io_completion_data *icd)
10ba535a 1593{
f8b0bd10
JA
1594 struct io_u *io_u = *io_u_ptr;
1595 enum fio_ddir ddir = io_u->ddir;
1596 struct fio_file *f = io_u->file;
10ba535a 1597
2ba1c290
JA
1598 dprint_io_u(io_u, "io complete");
1599
2ecc1b57 1600 td_io_u_lock(td);
0c6e7517 1601 assert(io_u->flags & IO_U_F_FLIGHT);
38dad62d 1602 io_u->flags &= ~(IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
f9401285
JA
1603
1604 /*
1605 * Mark IO ok to verify
1606 */
1607 if (io_u->ipo) {
890b6656
JA
1608 /*
1609 * Remove errored entry from the verification list
1610 */
1611 if (io_u->error)
1612 unlog_io_piece(td, io_u);
1613 else {
1614 io_u->ipo->flags &= ~IP_F_IN_FLIGHT;
1615 write_barrier();
1616 }
f9401285
JA
1617 }
1618
2ecc1b57 1619 td_io_u_unlock(td);
0c6e7517 1620
f8b0bd10 1621 if (ddir_sync(ddir)) {
87dc1ab1 1622 td->last_was_sync = 1;
44f29692
JA
1623 if (f) {
1624 f->first_write = -1ULL;
1625 f->last_write = -1ULL;
1626 }
87dc1ab1
JA
1627 return;
1628 }
1629
1630 td->last_was_sync = 0;
f8b0bd10 1631 td->last_ddir = ddir;
87dc1ab1 1632
f8b0bd10 1633 if (!io_u->error && ddir_rw(ddir)) {
10ba535a 1634 unsigned int bytes = io_u->buflen - io_u->resid;
f8b0bd10 1635 const enum fio_ddir oddir = ddir ^ 1;
b29ee5b3 1636 int ret;
10ba535a 1637
f8b0bd10
JA
1638 td->io_blocks[ddir]++;
1639 td->this_io_blocks[ddir]++;
1640 td->io_bytes[ddir] += bytes;
ae2fafc8 1641
1642 if (!(io_u->flags & IO_U_F_VER_LIST))
f8b0bd10
JA
1643 td->this_io_bytes[ddir] += bytes;
1644
1645 if (ddir == DDIR_WRITE && f) {
1646 if (f->first_write == -1ULL ||
1647 io_u->offset < f->first_write)
1648 f->first_write = io_u->offset;
1649 if (f->last_write == -1ULL ||
1650 ((io_u->offset + bytes) > f->last_write))
1651 f->last_write = io_u->offset + bytes;
44f29692
JA
1652 }
1653
6b1190fd
SL
1654 if (ramp_time_over(td) && (td->runstate == TD_RUNNING ||
1655 td->runstate == TD_VERIFYING)) {
f8b0bd10 1656 account_io_completion(td, io_u, icd, ddir, bytes);
40e1a6f0 1657
f8b0bd10
JA
1658 if (__should_check_rate(td, ddir)) {
1659 td->rate_pending_usleep[ddir] =
1660 (usec_for_io(td, ddir) -
ba3e4e0c 1661 utime_since_now(&td->start));
b23b6a2f 1662 }
f8b0bd10
JA
1663 if (ddir != DDIR_TRIM &&
1664 __should_check_rate(td, oddir)) {
1665 td->rate_pending_usleep[oddir] =
1666 (usec_for_io(td, oddir) -
ba3e4e0c 1667 utime_since_now(&td->start));
f8b0bd10 1668 }
721938ae 1669 }
10ba535a 1670
f8b0bd10 1671 icd->bytes_done[ddir] += bytes;
3af6ef39 1672
d7762cf8 1673 if (io_u->end_io) {
f8b0bd10
JA
1674 ret = io_u->end_io(td, io_u_ptr);
1675 io_u = *io_u_ptr;
3af6ef39
JA
1676 if (ret && !icd->error)
1677 icd->error = ret;
1678 }
ff58fced 1679 } else if (io_u->error) {
10ba535a 1680 icd->error = io_u->error;
5451792e
JA
1681 io_u_log_error(td, io_u);
1682 }
8b28bd41 1683 if (icd->error) {
f8b0bd10
JA
1684 enum error_type_bit eb = td_error_type(ddir, icd->error);
1685
8b28bd41
DM
1686 if (!td_non_fatal_error(td, eb, icd->error))
1687 return;
f8b0bd10 1688
f2bba182
RR
1689 /*
1690 * If there is a non_fatal error, then add to the error count
1691 * and clear all the errors.
1692 */
1693 update_error_count(td, icd->error);
1694 td_clear_error(td);
1695 icd->error = 0;
f8b0bd10
JA
1696 if (io_u)
1697 io_u->error = 0;
f2bba182 1698 }
10ba535a
JA
1699}
1700
9520ebb9
JA
1701static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1702 int nr)
10ba535a 1703{
6eaf09d6 1704 int ddir;
aba6c951
JA
1705
1706 if (!gtod_reduce(td))
9520ebb9 1707 fio_gettime(&icd->time, NULL);
02bcaa8c 1708
3af6ef39
JA
1709 icd->nr = nr;
1710
10ba535a 1711 icd->error = 0;
6eaf09d6
SL
1712 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1713 icd->bytes_done[ddir] = 0;
36167d82
JA
1714}
1715
97601024
JA
1716static void ios_completed(struct thread_data *td,
1717 struct io_completion_data *icd)
36167d82
JA
1718{
1719 struct io_u *io_u;
1720 int i;
1721
10ba535a
JA
1722 for (i = 0; i < icd->nr; i++) {
1723 io_u = td->io_ops->event(td, i);
1724
f8b0bd10 1725 io_completed(td, &io_u, icd);
e8462bd8 1726
f8b0bd10 1727 if (io_u)
e8462bd8 1728 put_io_u(td, io_u);
10ba535a
JA
1729 }
1730}
97601024 1731
e7e6cfb4
JA
1732/*
1733 * Complete a single io_u for the sync engines.
1734 */
581e7141 1735int io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
100f49f1 1736 uint64_t *bytes)
97601024
JA
1737{
1738 struct io_completion_data icd;
1739
9520ebb9 1740 init_icd(td, &icd, 1);
f8b0bd10 1741 io_completed(td, &io_u, &icd);
e8462bd8 1742
f8b0bd10 1743 if (io_u)
e8462bd8 1744 put_io_u(td, io_u);
97601024 1745
581e7141
JA
1746 if (icd.error) {
1747 td_verror(td, icd.error, "io_u_sync_complete");
1748 return -1;
1749 }
97601024 1750
581e7141 1751 if (bytes) {
6eaf09d6
SL
1752 int ddir;
1753
1754 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1755 bytes[ddir] += icd.bytes_done[ddir];
581e7141
JA
1756 }
1757
1758 return 0;
97601024
JA
1759}
1760
e7e6cfb4
JA
1761/*
1762 * Called to complete min_events number of io for the async engines.
1763 */
581e7141 1764int io_u_queued_complete(struct thread_data *td, int min_evts,
100f49f1 1765 uint64_t *bytes)
97601024 1766{
97601024 1767 struct io_completion_data icd;
00de55ef 1768 struct timespec *tvp = NULL;
97601024 1769 int ret;
4d06a338 1770 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
97601024 1771
4950421a 1772 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
b271fe62 1773
4950421a 1774 if (!min_evts)
00de55ef 1775 tvp = &ts;
5fb4b366
RE
1776 else if (min_evts > td->cur_depth)
1777 min_evts = td->cur_depth;
97601024 1778
4950421a 1779 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete, tvp);
97601024 1780 if (ret < 0) {
e1161c32 1781 td_verror(td, -ret, "td_io_getevents");
97601024
JA
1782 return ret;
1783 } else if (!ret)
1784 return ret;
1785
9520ebb9 1786 init_icd(td, &icd, ret);
97601024 1787 ios_completed(td, &icd);
581e7141
JA
1788 if (icd.error) {
1789 td_verror(td, icd.error, "io_u_queued_complete");
1790 return -1;
1791 }
97601024 1792
581e7141 1793 if (bytes) {
6eaf09d6
SL
1794 int ddir;
1795
1796 for (ddir = DDIR_READ; ddir < DDIR_RWDIR_CNT; ddir++)
1797 bytes[ddir] += icd.bytes_done[ddir];
581e7141
JA
1798 }
1799
1800 return 0;
97601024 1801}
7e77dd02
JA
1802
1803/*
1804 * Call when io_u is really queued, to update the submission latency.
1805 */
1806void io_u_queued(struct thread_data *td, struct io_u *io_u)
1807{
9520ebb9
JA
1808 if (!td->o.disable_slat) {
1809 unsigned long slat_time;
7e77dd02 1810
9520ebb9 1811 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
ae588852
JA
1812 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen,
1813 io_u->offset);
9520ebb9 1814 }
7e77dd02 1815}
433afcb4 1816
5c94b008
JA
1817/*
1818 * See if we should reuse the last seed, if dedupe is enabled
1819 */
1820static struct frand_state *get_buf_state(struct thread_data *td)
1821{
1822 unsigned int v;
1823 unsigned long r;
1824
1825 if (!td->o.dedupe_percentage)
1826 return &td->buf_state;
c0b69b92
JA
1827 else if (td->o.dedupe_percentage == 100)
1828 return &td->buf_state_prev;
5c94b008
JA
1829
1830 r = __rand(&td->dedupe_state);
1831 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
1832
1833 if (v <= td->o.dedupe_percentage)
1834 return &td->buf_state_prev;
1835
1836 return &td->buf_state;
1837}
1838
1839static void save_buf_state(struct thread_data *td, struct frand_state *rs)
1840{
1841 if (rs == &td->buf_state)
1842 frand_copy(&td->buf_state_prev, rs);
1843}
1844
cc86c395
JA
1845void fill_io_buffer(struct thread_data *td, void *buf, unsigned int min_write,
1846 unsigned int max_bs)
5973cafb 1847{
ce35b1ec
JA
1848 if (td->o.buffer_pattern_bytes)
1849 fill_buffer_pattern(td, buf, max_bs);
1850 else if (!td->o.zero_buffers) {
9c42684e 1851 unsigned int perc = td->o.compress_percentage;
5c94b008 1852 struct frand_state *rs;
1066358a 1853 unsigned int left = max_bs;
5c94b008 1854
1066358a
JA
1855 do {
1856 rs = get_buf_state(td);
9c42684e 1857
1066358a 1858 min_write = min(min_write, left);
f97a43a1 1859
1066358a
JA
1860 if (perc) {
1861 unsigned int seg = min_write;
cc86c395 1862
1066358a
JA
1863 seg = min(min_write, td->o.compress_chunk);
1864 if (!seg)
1865 seg = min_write;
1866
1867 fill_random_buf_percentage(rs, buf, perc, seg,
1868 min_write);
1869 } else
1870 fill_random_buf(rs, buf, min_write);
1871
1872 buf += min_write;
1873 left -= min_write;
5c94b008 1874 save_buf_state(td, rs);
1066358a 1875 } while (left);
9c42684e 1876 } else
cc86c395
JA
1877 memset(buf, 0, max_bs);
1878}
1879
1880/*
1881 * "randomly" fill the buffer contents
1882 */
1883void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1884 unsigned int min_write, unsigned int max_bs)
1885{
1886 io_u->buf_filled_len = 0;
1887 fill_io_buffer(td, io_u->buf, min_write, max_bs);
5973cafb 1888}