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