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