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