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