RDMA IO engine
[fio.git] / io_u.c
CommitLineData
10ba535a
JA
1#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <signal.h>
5#include <time.h>
0c6e7517 6#include <assert.h>
10ba535a
JA
7
8#include "fio.h"
5973cafb 9#include "hash.h"
4f5af7b2 10#include "verify.h"
0d29de83 11#include "trim.h"
1fbbf72e 12#include "lib/rand.h"
10ba535a 13
97601024
JA
14struct io_completion_data {
15 int nr; /* input */
97601024
JA
16
17 int error; /* output */
18 unsigned long bytes_done[2]; /* output */
19 struct timeval time; /* output */
20};
21
10ba535a
JA
22/*
23 * The ->file_map[] contains a map of blocks we have or have not done io
24 * to yet. Used to make sure we cover the entire range in a fair fashion.
25 */
aec2de20 26static int random_map_free(struct fio_file *f, const unsigned long long block)
10ba535a 27{
aec2de20
JA
28 unsigned int idx = RAND_MAP_IDX(f, block);
29 unsigned int bit = RAND_MAP_BIT(f, block);
10ba535a 30
84422acd
JA
31 dprint(FD_RANDOM, "free: b=%llu, idx=%u, bit=%u\n", block, idx, bit);
32
0ce8b119 33 return (f->file_map[idx] & (1UL << bit)) == 0;
10ba535a
JA
34}
35
df415585
JA
36/*
37 * Mark a given offset as used in the map.
38 */
9bf2061e 39static void mark_random_map(struct thread_data *td, struct io_u *io_u)
df415585 40{
2dc1bbeb 41 unsigned int min_bs = td->o.rw_min_bs;
9bf2061e 42 struct fio_file *f = io_u->file;
a00735e6 43 unsigned long long block;
3e3357b1 44 unsigned int blocks, nr_blocks;
38dad62d 45 int busy_check;
df415585 46
b9c5b644 47 block = (io_u->offset - f->file_offset) / (unsigned long long) min_bs;
c685b5b2 48 nr_blocks = (io_u->buflen + min_bs - 1) / min_bs;
3e3357b1 49 blocks = 0;
38dad62d 50 busy_check = !(io_u->flags & IO_U_F_BUSY_OK);
c685b5b2 51
3e3357b1 52 while (nr_blocks) {
df415585 53 unsigned int idx, bit;
0ce8b119 54 unsigned long mask, this_blocks;
df415585 55
1e3d53ac
JA
56 /*
57 * If we have a mixed random workload, we may
58 * encounter blocks we already did IO to.
59 */
38dad62d
JA
60 if (!busy_check) {
61 blocks = nr_blocks;
62 break;
63 }
5736c10d 64 if ((td->o.ddir_seq_nr == 1) && !random_map_free(f, block))
df415585
JA
65 break;
66
aec2de20
JA
67 idx = RAND_MAP_IDX(f, block);
68 bit = RAND_MAP_BIT(f, block);
df415585 69
0032bf9f 70 fio_assert(td, idx < f->num_maps);
df415585 71
3e3357b1
JA
72 this_blocks = nr_blocks;
73 if (this_blocks + bit > BLOCKS_PER_MAP)
74 this_blocks = BLOCKS_PER_MAP - bit;
75
da3758bf
JA
76 do {
77 if (this_blocks == BLOCKS_PER_MAP)
0ce8b119 78 mask = -1UL;
da3758bf 79 else
0ce8b119 80 mask = ((1UL << this_blocks) - 1) << bit;
da3758bf
JA
81
82 if (!(f->file_map[idx] & mask))
83 break;
84
85 this_blocks--;
86 } while (this_blocks);
87
88 if (!this_blocks)
89 break;
3e3357b1 90
3e3357b1
JA
91 f->file_map[idx] |= mask;
92 nr_blocks -= this_blocks;
93 blocks += this_blocks;
c9dd34b2 94 block += this_blocks;
df415585
JA
95 }
96
a00735e6
JA
97 if ((blocks * min_bs) < io_u->buflen)
98 io_u->buflen = blocks * min_bs;
df415585
JA
99}
100
3e3357b1
JA
101static unsigned long long last_block(struct thread_data *td, struct fio_file *f,
102 enum fio_ddir ddir)
2ba1c290
JA
103{
104 unsigned long long max_blocks;
d9dd70f7 105 unsigned long long max_size;
2ba1c290 106
ff58fced
JA
107 assert(ddir_rw(ddir));
108
d9dd70f7
JA
109 /*
110 * Hmm, should we make sure that ->io_size <= ->real_file_size?
111 */
112 max_size = f->io_size;
113 if (max_size > f->real_file_size)
114 max_size = f->real_file_size;
115
2b7a01d0 116 max_blocks = max_size / (unsigned long long) td->o.ba[ddir];
2ba1c290
JA
117 if (!max_blocks)
118 return 0;
119
67778e88 120 return max_blocks;
2ba1c290
JA
121}
122
10ba535a
JA
123/*
124 * Return the next free block in the map.
125 */
126static int get_next_free_block(struct thread_data *td, struct fio_file *f,
4ba66134 127 enum fio_ddir ddir, unsigned long long *b)
10ba535a 128{
0ce8b119 129 unsigned long long block, min_bs = td->o.rw_min_bs, lastb;
10ba535a
JA
130 int i;
131
5e0baa7f
JA
132 lastb = last_block(td, f, ddir);
133 if (!lastb)
134 return 1;
135
c685b5b2 136 i = f->last_free_lookup;
0ce8b119
JA
137 block = i * BLOCKS_PER_MAP;
138 while (block * min_bs < f->real_file_size &&
139 block * min_bs < f->io_size) {
140 if (f->file_map[i] != -1UL) {
141 block += ffz(f->file_map[i]);
142 if (block > lastb)
2ba1c290 143 break;
c685b5b2 144 f->last_free_lookup = i;
0ce8b119 145 *b = block;
10ba535a
JA
146 return 0;
147 }
148
0ce8b119 149 block += BLOCKS_PER_MAP;
10ba535a
JA
150 i++;
151 }
152
2ba1c290 153 dprint(FD_IO, "failed finding a free block\n");
10ba535a
JA
154 return 1;
155}
156
ec4015da 157static int get_next_rand_offset(struct thread_data *td, struct fio_file *f,
4ba66134 158 enum fio_ddir ddir, unsigned long long *b)
ec4015da 159{
5e0baa7f 160 unsigned long long r, lastb;
ec4015da
JA
161 int loops = 5;
162
5e0baa7f
JA
163 lastb = last_block(td, f, ddir);
164 if (!lastb)
165 return 1;
166
0ce8b119
JA
167 if (f->failed_rands >= 200)
168 goto ffz;
169
ec4015da 170 do {
2615cc4b
JA
171 if (td->o.use_os_rand) {
172 r = os_random_long(&td->random_state);
173 *b = (lastb - 1) * (r / ((unsigned long long) OS_RAND_MAX + 1.0));
174 } else {
175 r = __rand(&td->__random_state);
176 *b = (lastb - 1) * (r / ((unsigned long long) FRAND_MAX + 1.0));
177 }
178
84422acd 179 dprint(FD_RANDOM, "off rand %llu\n", r);
2615cc4b 180
2ba1c290 181
43c63a78
JA
182 /*
183 * if we are not maintaining a random map, we are done.
184 */
303032ae 185 if (!file_randommap(td, f))
0ce8b119 186 goto ret_good;
43c63a78
JA
187
188 /*
84422acd 189 * calculate map offset and check if it's free
43c63a78 190 */
aec2de20 191 if (random_map_free(f, *b))
0ce8b119 192 goto ret_good;
43c63a78 193
84422acd
JA
194 dprint(FD_RANDOM, "get_next_rand_offset: offset %llu busy\n",
195 *b);
43c63a78 196 } while (--loops);
ec4015da 197
0ce8b119
JA
198 if (!f->failed_rands++)
199 f->last_free_lookup = 0;
200
ec4015da 201 /*
43c63a78
JA
202 * we get here, if we didn't suceed in looking up a block. generate
203 * a random start offset into the filemap, and find the first free
204 * block from there.
ec4015da 205 */
43c63a78
JA
206 loops = 10;
207 do {
dc873b6f
JA
208 f->last_free_lookup = (f->num_maps - 1) *
209 (r / (OS_RAND_MAX + 1.0));
4ba66134 210 if (!get_next_free_block(td, f, ddir, b))
0ce8b119 211 goto ret;
ec4015da 212
d1cea521
JA
213 if (td->o.use_os_rand)
214 r = os_random_long(&td->random_state);
215 else
216 r = __rand(&td->__random_state);
43c63a78
JA
217 } while (--loops);
218
219 /*
220 * that didn't work either, try exhaustive search from the start
221 */
222 f->last_free_lookup = 0;
0ce8b119
JA
223ffz:
224 if (!get_next_free_block(td, f, ddir, b))
225 return 0;
226 f->last_free_lookup = 0;
4ba66134 227 return get_next_free_block(td, f, ddir, b);
0ce8b119
JA
228ret_good:
229 f->failed_rands = 0;
230ret:
231 return 0;
ec4015da
JA
232}
233
38dad62d
JA
234static int get_next_rand_block(struct thread_data *td, struct fio_file *f,
235 enum fio_ddir ddir, unsigned long long *b)
236{
237 if (get_next_rand_offset(td, f, ddir, b)) {
238 dprint(FD_IO, "%s: rand offset failed, last=%llu, size=%llu\n",
239 f->file_name, f->last_pos, f->real_file_size);
240 return 1;
241 }
242
243 return 0;
244}
245
246static int get_next_seq_block(struct thread_data *td, struct fio_file *f,
247 enum fio_ddir ddir, unsigned long long *b)
248{
ff58fced
JA
249 assert(ddir_rw(ddir));
250
38dad62d
JA
251 if (f->last_pos < f->real_file_size) {
252 *b = (f->last_pos - f->file_offset) / td->o.min_bs[ddir];
253 return 0;
254 }
255
256 return 1;
257}
258
259static int get_next_block(struct thread_data *td, struct io_u *io_u,
260 enum fio_ddir ddir, int rw_seq, unsigned long long *b)
261{
262 struct fio_file *f = io_u->file;
263 int ret;
264
ff58fced
JA
265 assert(ddir_rw(ddir));
266
38dad62d
JA
267 if (rw_seq) {
268 if (td_random(td))
269 ret = get_next_rand_block(td, f, ddir, b);
270 else
271 ret = get_next_seq_block(td, f, ddir, b);
272 } else {
273 io_u->flags |= IO_U_F_BUSY_OK;
274
275 if (td->o.rw_seq == RW_SEQ_SEQ) {
276 ret = get_next_seq_block(td, f, ddir, b);
277 if (ret)
278 ret = get_next_rand_block(td, f, ddir, b);
279 } else if (td->o.rw_seq == RW_SEQ_IDENT) {
280 if (f->last_start != -1ULL)
0508320e
DN
281 *b = (f->last_start - f->file_offset)
282 / td->o.min_bs[ddir];
38dad62d
JA
283 else
284 *b = 0;
285 ret = 0;
286 } else {
287 log_err("fio: unknown rw_seq=%d\n", td->o.rw_seq);
288 ret = 1;
289 }
290 }
291
292 return ret;
293}
294
10ba535a
JA
295/*
296 * For random io, generate a random new block and see if it's used. Repeat
297 * until we find a free one. For sequential io, just return the end of
298 * the last io issued.
299 */
15dc1934 300static int __get_next_offset(struct thread_data *td, struct io_u *io_u)
10ba535a 301{
9bf2061e 302 struct fio_file *f = io_u->file;
ec4015da 303 unsigned long long b;
4ba66134 304 enum fio_ddir ddir = io_u->ddir;
38dad62d 305 int rw_seq_hit = 0;
10ba535a 306
ff58fced
JA
307 assert(ddir_rw(ddir));
308
38dad62d
JA
309 if (td->o.ddir_seq_nr && !--td->ddir_seq_nr) {
310 rw_seq_hit = 1;
5736c10d 311 td->ddir_seq_nr = td->o.ddir_seq_nr;
38dad62d 312 }
211097b2 313
0508320e 314 if (get_next_block(td, io_u, ddir, rw_seq_hit, &b))
38dad62d 315 return 1;
10ba535a 316
2b7a01d0 317 io_u->offset = b * td->o.ba[ddir];
009bd847
JA
318 if (io_u->offset >= f->io_size) {
319 dprint(FD_IO, "get_next_offset: offset %llu >= io_size %llu\n",
320 io_u->offset, f->io_size);
321 return 1;
322 }
323
324 io_u->offset += f->file_offset;
2ba1c290
JA
325 if (io_u->offset >= f->real_file_size) {
326 dprint(FD_IO, "get_next_offset: offset %llu >= size %llu\n",
327 io_u->offset, f->real_file_size);
10ba535a 328 return 1;
2ba1c290 329 }
10ba535a
JA
330
331 return 0;
332}
333
15dc1934
JA
334static int get_next_offset(struct thread_data *td, struct io_u *io_u)
335{
7eb36574
JA
336 struct prof_io_ops *ops = &td->prof_io_ops;
337
338 if (ops->fill_io_u_off)
339 return ops->fill_io_u_off(td, io_u);
15dc1934
JA
340
341 return __get_next_offset(td, io_u);
342}
343
79944128
JA
344static inline int io_u_fits(struct thread_data *td, struct io_u *io_u,
345 unsigned int buflen)
346{
347 struct fio_file *f = io_u->file;
348
349 return io_u->offset + buflen <= f->io_size + td->o.start_offset;
350}
351
15dc1934 352static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u)
10ba535a 353{
bca4ed4d 354 const int ddir = io_u->ddir;
f3f552b9 355 unsigned int uninitialized_var(buflen);
f3059de1 356 unsigned int minbs, maxbs;
1294c3ec 357 unsigned long r, rand_max;
10ba535a 358
ff58fced
JA
359 assert(ddir_rw(ddir));
360
f3059de1
JA
361 minbs = td->o.min_bs[ddir];
362 maxbs = td->o.max_bs[ddir];
363
79944128
JA
364 if (minbs == maxbs)
365 return minbs;
366
4c07ad86
JA
367 if (td->o.use_os_rand)
368 rand_max = OS_RAND_MAX;
369 else
370 rand_max = FRAND_MAX;
371
79944128 372 do {
4c07ad86
JA
373 if (td->o.use_os_rand)
374 r = os_random_long(&td->bsrange_state);
375 else
376 r = __rand(&td->__bsrange_state);
377
720e84ad 378 if (!td->o.bssplit_nr[ddir]) {
f3059de1 379 buflen = 1 + (unsigned int) ((double) maxbs *
4c07ad86 380 (r / (rand_max + 1.0)));
f3059de1
JA
381 if (buflen < minbs)
382 buflen = minbs;
5ec10eaa 383 } else {
564ca972
JA
384 long perc = 0;
385 unsigned int i;
386
720e84ad
JA
387 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
388 struct bssplit *bsp = &td->o.bssplit[ddir][i];
564ca972
JA
389
390 buflen = bsp->bs;
391 perc += bsp->perc;
79944128
JA
392 if ((r <= ((rand_max / 100L) * perc)) &&
393 io_u_fits(td, io_u, buflen))
564ca972
JA
394 break;
395 }
396 }
79944128 397
f3059de1
JA
398 if (!td->o.bs_unaligned && is_power_of_2(minbs))
399 buflen = (buflen + minbs - 1) & ~(minbs - 1);
10ba535a 400
79944128 401 } while (!io_u_fits(td, io_u, buflen));
6a5e6884 402
10ba535a
JA
403 return buflen;
404}
405
15dc1934
JA
406static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
407{
7eb36574
JA
408 struct prof_io_ops *ops = &td->prof_io_ops;
409
410 if (ops->fill_io_u_size)
411 return ops->fill_io_u_size(td, io_u);
15dc1934
JA
412
413 return __get_next_buflen(td, io_u);
414}
415
afe24a5a
JA
416static void set_rwmix_bytes(struct thread_data *td)
417{
afe24a5a
JA
418 unsigned int diff;
419
420 /*
421 * we do time or byte based switch. this is needed because
422 * buffered writes may issue a lot quicker than they complete,
423 * whereas reads do not.
424 */
e47f799f 425 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
04c540d9 426 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
e47f799f
JA
427}
428
429static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
430{
431 unsigned int v;
1294c3ec 432 unsigned long r;
e47f799f 433
4c07ad86
JA
434 if (td->o.use_os_rand) {
435 r = os_random_long(&td->rwmix_state);
436 v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
437 } else {
438 r = __rand(&td->__rwmix_state);
439 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
440 }
441
04c540d9 442 if (v <= td->o.rwmix[DDIR_READ])
e47f799f
JA
443 return DDIR_READ;
444
445 return DDIR_WRITE;
afe24a5a
JA
446}
447
581e7141
JA
448static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
449{
450 enum fio_ddir odir = ddir ^ 1;
451 struct timeval t;
452 long usec;
453
ff58fced
JA
454 assert(ddir_rw(ddir));
455
581e7141
JA
456 if (td->rate_pending_usleep[ddir] <= 0)
457 return ddir;
458
459 /*
460 * We have too much pending sleep in this direction. See if we
461 * should switch.
462 */
463 if (td_rw(td)) {
464 /*
465 * Other direction does not have too much pending, switch
466 */
467 if (td->rate_pending_usleep[odir] < 100000)
468 return odir;
469
470 /*
471 * Both directions have pending sleep. Sleep the minimum time
472 * and deduct from both.
473 */
474 if (td->rate_pending_usleep[ddir] <=
475 td->rate_pending_usleep[odir]) {
476 usec = td->rate_pending_usleep[ddir];
477 } else {
478 usec = td->rate_pending_usleep[odir];
479 ddir = odir;
480 }
481 } else
482 usec = td->rate_pending_usleep[ddir];
483
484 fio_gettime(&t, NULL);
485 usec_sleep(td, usec);
486 usec = utime_since_now(&t);
487
488 td->rate_pending_usleep[ddir] -= usec;
489
490 odir = ddir ^ 1;
491 if (td_rw(td) && __should_check_rate(td, odir))
492 td->rate_pending_usleep[odir] -= usec;
0b9d69ec 493
581e7141
JA
494 return ddir;
495}
496
10ba535a
JA
497/*
498 * Return the data direction for the next io_u. If the job is a
499 * mixed read/write workload, check the rwmix cycle and switch if
500 * necessary.
501 */
1e97cce9 502static enum fio_ddir get_rw_ddir(struct thread_data *td)
10ba535a 503{
581e7141
JA
504 enum fio_ddir ddir;
505
5f9099ea
JA
506 /*
507 * see if it's time to fsync
508 */
509 if (td->o.fsync_blocks &&
510 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
511 td->io_issues[DDIR_WRITE] && should_fsync(td))
512 return DDIR_SYNC;
513
514 /*
515 * see if it's time to fdatasync
516 */
517 if (td->o.fdatasync_blocks &&
518 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
519 td->io_issues[DDIR_WRITE] && should_fsync(td))
520 return DDIR_DATASYNC;
521
44f29692
JA
522 /*
523 * see if it's time to sync_file_range
524 */
525 if (td->sync_file_range_nr &&
526 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
527 td->io_issues[DDIR_WRITE] && should_fsync(td))
528 return DDIR_SYNC_FILE_RANGE;
529
10ba535a 530 if (td_rw(td)) {
10ba535a
JA
531 /*
532 * Check if it's time to seed a new data direction.
533 */
e4928662 534 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
e47f799f
JA
535 /*
536 * Put a top limit on how many bytes we do for
537 * one data direction, to avoid overflowing the
538 * ranges too much
539 */
540 ddir = get_rand_ddir(td);
e47f799f
JA
541
542 if (ddir != td->rwmix_ddir)
543 set_rwmix_bytes(td);
544
545 td->rwmix_ddir = ddir;
10ba535a 546 }
581e7141 547 ddir = td->rwmix_ddir;
10ba535a 548 } else if (td_read(td))
581e7141 549 ddir = DDIR_READ;
10ba535a 550 else
581e7141
JA
551 ddir = DDIR_WRITE;
552
553 td->rwmix_ddir = rate_ddir(td, ddir);
554 return td->rwmix_ddir;
10ba535a
JA
555}
556
1ef2b6be
JA
557static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
558{
559 io_u->ddir = get_rw_ddir(td);
560
561 if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
562 td->o.barrier_blocks &&
563 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
564 td->io_issues[DDIR_WRITE])
565 io_u->flags |= IO_U_F_BARRIER;
566}
567
e8462bd8 568void put_file_log(struct thread_data *td, struct fio_file *f)
60f2c658
JA
569{
570 int ret = put_file(td, f);
571
572 if (ret)
573 td_verror(td, ret, "file close");
574}
575
10ba535a
JA
576void put_io_u(struct thread_data *td, struct io_u *io_u)
577{
e8462bd8
JA
578 td_io_u_lock(td);
579
0c6e7517 580 io_u->flags |= IO_U_F_FREE;
e8462bd8 581 io_u->flags &= ~IO_U_F_FREE_DEF;
0c6e7517 582
60f2c658
JA
583 if (io_u->file)
584 put_file_log(td, io_u->file);
2dbdab7e 585
10ba535a 586 io_u->file = NULL;
0c41214f
RR
587 if (io_u->flags & IO_U_F_IN_CUR_DEPTH)
588 td->cur_depth--;
e8462bd8 589 flist_del_init(&io_u->list);
01743ee1 590 flist_add(&io_u->list, &td->io_u_freelist);
e8462bd8
JA
591 td_io_u_unlock(td);
592 td_io_u_free_notify(td);
10ba535a
JA
593}
594
f2bba182
RR
595void clear_io_u(struct thread_data *td, struct io_u *io_u)
596{
597 io_u->flags &= ~IO_U_F_FLIGHT;
598 put_io_u(td, io_u);
599}
600
755200a3
JA
601void requeue_io_u(struct thread_data *td, struct io_u **io_u)
602{
603 struct io_u *__io_u = *io_u;
604
465221b0
JA
605 dprint(FD_IO, "requeue %p\n", __io_u);
606
e8462bd8
JA
607 td_io_u_lock(td);
608
4d2e0f49 609 __io_u->flags |= IO_U_F_FREE;
ff58fced 610 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(__io_u->ddir))
e4f54adb 611 td->io_issues[__io_u->ddir]--;
5ec10eaa 612
4d2e0f49 613 __io_u->flags &= ~IO_U_F_FLIGHT;
0c41214f
RR
614 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH)
615 td->cur_depth--;
01743ee1
JA
616 flist_del(&__io_u->list);
617 flist_add_tail(&__io_u->list, &td->io_u_requeues);
e8462bd8 618 td_io_u_unlock(td);
755200a3
JA
619 *io_u = NULL;
620}
621
9bf2061e 622static int fill_io_u(struct thread_data *td, struct io_u *io_u)
10ba535a 623{
b4c5e1ac
JA
624 if (td->io_ops->flags & FIO_NOIO)
625 goto out;
626
1ef2b6be 627 set_rw_ddir(td, io_u);
5f9099ea 628
87dc1ab1 629 /*
ff58fced 630 * fsync() or fdatasync() or trim etc, we are done
87dc1ab1 631 */
ff58fced 632 if (!ddir_rw(io_u->ddir))
c38e9468 633 goto out;
a00735e6 634
48f5abd3
JA
635 /*
636 * See if it's time to switch to a new zone
637 */
638 if (td->zone_bytes >= td->o.zone_size) {
639 td->zone_bytes = 0;
640 io_u->file->last_pos += td->o.zone_skip;
641 td->io_skip_bytes += td->o.zone_skip;
642 }
643
10ba535a 644 /*
c685b5b2
JA
645 * No log, let the seq/rand engine retrieve the next buflen and
646 * position.
10ba535a 647 */
2ba1c290
JA
648 if (get_next_offset(td, io_u)) {
649 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
bca4ed4d 650 return 1;
2ba1c290 651 }
10ba535a 652
9bf2061e 653 io_u->buflen = get_next_buflen(td, io_u);
2ba1c290
JA
654 if (!io_u->buflen) {
655 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
bca4ed4d 656 return 1;
2ba1c290 657 }
bca4ed4d 658
2ba1c290
JA
659 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
660 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
4ba66134
JA
661 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
662 io_u->buflen, io_u->file->real_file_size);
6a5e6884 663 return 1;
2ba1c290 664 }
6a5e6884 665
bca4ed4d
JA
666 /*
667 * mark entry before potentially trimming io_u
668 */
303032ae 669 if (td_random(td) && file_randommap(td, io_u->file))
9bf2061e 670 mark_random_map(td, io_u);
bca4ed4d
JA
671
672 /*
673 * If using a write iolog, store this entry.
674 */
c38e9468 675out:
2ba1c290 676 dprint_io_u(io_u, "fill_io_u");
d9d91e39 677 td->zone_bytes += io_u->buflen;
f29b25a3 678 log_io_u(td, io_u);
bca4ed4d 679 return 0;
10ba535a
JA
680}
681
838bc709
JA
682static void __io_u_mark_map(unsigned int *map, unsigned int nr)
683{
2b13e716 684 int idx = 0;
838bc709
JA
685
686 switch (nr) {
687 default:
2b13e716 688 idx = 6;
838bc709
JA
689 break;
690 case 33 ... 64:
2b13e716 691 idx = 5;
838bc709
JA
692 break;
693 case 17 ... 32:
2b13e716 694 idx = 4;
838bc709
JA
695 break;
696 case 9 ... 16:
2b13e716 697 idx = 3;
838bc709
JA
698 break;
699 case 5 ... 8:
2b13e716 700 idx = 2;
838bc709
JA
701 break;
702 case 1 ... 4:
2b13e716 703 idx = 1;
838bc709
JA
704 case 0:
705 break;
706 }
707
2b13e716 708 map[idx]++;
838bc709
JA
709}
710
711void io_u_mark_submit(struct thread_data *td, unsigned int nr)
712{
713 __io_u_mark_map(td->ts.io_u_submit, nr);
714 td->ts.total_submit++;
715}
716
717void io_u_mark_complete(struct thread_data *td, unsigned int nr)
718{
719 __io_u_mark_map(td->ts.io_u_complete, nr);
720 td->ts.total_complete++;
721}
722
d8005759 723void io_u_mark_depth(struct thread_data *td, unsigned int nr)
71619dc2 724{
2b13e716 725 int idx = 0;
71619dc2
JA
726
727 switch (td->cur_depth) {
728 default:
2b13e716 729 idx = 6;
a783e61a 730 break;
71619dc2 731 case 32 ... 63:
2b13e716 732 idx = 5;
a783e61a 733 break;
71619dc2 734 case 16 ... 31:
2b13e716 735 idx = 4;
a783e61a 736 break;
71619dc2 737 case 8 ... 15:
2b13e716 738 idx = 3;
a783e61a 739 break;
71619dc2 740 case 4 ... 7:
2b13e716 741 idx = 2;
a783e61a 742 break;
71619dc2 743 case 2 ... 3:
2b13e716 744 idx = 1;
71619dc2
JA
745 case 1:
746 break;
747 }
748
2b13e716 749 td->ts.io_u_map[idx] += nr;
71619dc2
JA
750}
751
04a0feae
JA
752static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
753{
2b13e716 754 int idx = 0;
04a0feae
JA
755
756 assert(usec < 1000);
757
758 switch (usec) {
759 case 750 ... 999:
2b13e716 760 idx = 9;
04a0feae
JA
761 break;
762 case 500 ... 749:
2b13e716 763 idx = 8;
04a0feae
JA
764 break;
765 case 250 ... 499:
2b13e716 766 idx = 7;
04a0feae
JA
767 break;
768 case 100 ... 249:
2b13e716 769 idx = 6;
04a0feae
JA
770 break;
771 case 50 ... 99:
2b13e716 772 idx = 5;
04a0feae
JA
773 break;
774 case 20 ... 49:
2b13e716 775 idx = 4;
04a0feae
JA
776 break;
777 case 10 ... 19:
2b13e716 778 idx = 3;
04a0feae
JA
779 break;
780 case 4 ... 9:
2b13e716 781 idx = 2;
04a0feae
JA
782 break;
783 case 2 ... 3:
2b13e716 784 idx = 1;
04a0feae
JA
785 case 0 ... 1:
786 break;
787 }
788
2b13e716
JA
789 assert(idx < FIO_IO_U_LAT_U_NR);
790 td->ts.io_u_lat_u[idx]++;
04a0feae
JA
791}
792
793static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
ec118304 794{
2b13e716 795 int idx = 0;
ec118304
JA
796
797 switch (msec) {
798 default:
2b13e716 799 idx = 11;
04a0feae 800 break;
8abdce66 801 case 1000 ... 1999:
2b13e716 802 idx = 10;
04a0feae 803 break;
8abdce66 804 case 750 ... 999:
2b13e716 805 idx = 9;
04a0feae 806 break;
8abdce66 807 case 500 ... 749:
2b13e716 808 idx = 8;
04a0feae 809 break;
8abdce66 810 case 250 ... 499:
2b13e716 811 idx = 7;
04a0feae 812 break;
8abdce66 813 case 100 ... 249:
2b13e716 814 idx = 6;
04a0feae 815 break;
8abdce66 816 case 50 ... 99:
2b13e716 817 idx = 5;
04a0feae 818 break;
8abdce66 819 case 20 ... 49:
2b13e716 820 idx = 4;
04a0feae 821 break;
8abdce66 822 case 10 ... 19:
2b13e716 823 idx = 3;
04a0feae 824 break;
8abdce66 825 case 4 ... 9:
2b13e716 826 idx = 2;
04a0feae 827 break;
ec118304 828 case 2 ... 3:
2b13e716 829 idx = 1;
ec118304
JA
830 case 0 ... 1:
831 break;
832 }
833
2b13e716
JA
834 assert(idx < FIO_IO_U_LAT_M_NR);
835 td->ts.io_u_lat_m[idx]++;
04a0feae
JA
836}
837
838static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
839{
840 if (usec < 1000)
841 io_u_mark_lat_usec(td, usec);
842 else
843 io_u_mark_lat_msec(td, usec / 1000);
ec118304
JA
844}
845
0aabe160
JA
846/*
847 * Get next file to service by choosing one at random
848 */
2cc52930
JA
849static struct fio_file *get_next_file_rand(struct thread_data *td,
850 enum fio_file_flags goodf,
d6aed795 851 enum fio_file_flags badf)
0aabe160 852{
0aabe160 853 struct fio_file *f;
1c178180 854 int fno;
0aabe160
JA
855
856 do {
87b10676 857 int opened = 0;
1294c3ec 858 unsigned long r;
4c07ad86
JA
859
860 if (td->o.use_os_rand) {
861 r = os_random_long(&td->next_file_state);
862 fno = (unsigned int) ((double) td->o.nr_files
863 * (r / (OS_RAND_MAX + 1.0)));
864 } else {
865 r = __rand(&td->__next_file_state);
866 fno = (unsigned int) ((double) td->o.nr_files
867 * (r / (FRAND_MAX + 1.0)));
868 }
7c83c089 869
126d65c6 870 f = td->files[fno];
d6aed795 871 if (fio_file_done(f))
059e63c0 872 continue;
1c178180 873
d6aed795 874 if (!fio_file_open(f)) {
87b10676
JA
875 int err;
876
877 err = td_io_open_file(td, f);
878 if (err)
879 continue;
880 opened = 1;
881 }
882
2ba1c290
JA
883 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
884 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
0aabe160 885 return f;
2ba1c290 886 }
87b10676
JA
887 if (opened)
888 td_io_close_file(td, f);
0aabe160
JA
889 } while (1);
890}
891
892/*
893 * Get next file to service by doing round robin between all available ones
894 */
1c178180
JA
895static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
896 int badf)
3d7c391d
JA
897{
898 unsigned int old_next_file = td->next_file;
899 struct fio_file *f;
900
901 do {
87b10676
JA
902 int opened = 0;
903
126d65c6 904 f = td->files[td->next_file];
3d7c391d
JA
905
906 td->next_file++;
2dc1bbeb 907 if (td->next_file >= td->o.nr_files)
3d7c391d
JA
908 td->next_file = 0;
909
87b10676 910 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
d6aed795 911 if (fio_file_done(f)) {
d5ed68ea 912 f = NULL;
059e63c0 913 continue;
d5ed68ea 914 }
059e63c0 915
d6aed795 916 if (!fio_file_open(f)) {
87b10676
JA
917 int err;
918
919 err = td_io_open_file(td, f);
b5696bfc
JA
920 if (err) {
921 dprint(FD_FILE, "error %d on open of %s\n",
922 err, f->file_name);
87c27b45 923 f = NULL;
87b10676 924 continue;
b5696bfc 925 }
87b10676
JA
926 opened = 1;
927 }
928
0b9d69ec
JA
929 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
930 f->flags);
1c178180 931 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
3d7c391d
JA
932 break;
933
87b10676
JA
934 if (opened)
935 td_io_close_file(td, f);
936
3d7c391d
JA
937 f = NULL;
938 } while (td->next_file != old_next_file);
939
2ba1c290 940 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
3d7c391d
JA
941 return f;
942}
943
7eb36574 944static struct fio_file *__get_next_file(struct thread_data *td)
bdb4e2e9 945{
1907dbc6
JA
946 struct fio_file *f;
947
2dc1bbeb 948 assert(td->o.nr_files <= td->files_index);
1c178180 949
b5696bfc 950 if (td->nr_done_files >= td->o.nr_files) {
5ec10eaa
JA
951 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
952 " nr_files=%d\n", td->nr_open_files,
953 td->nr_done_files,
954 td->o.nr_files);
bdb4e2e9 955 return NULL;
2ba1c290 956 }
bdb4e2e9 957
1907dbc6 958 f = td->file_service_file;
d6aed795 959 if (f && fio_file_open(f) && !fio_file_closing(f)) {
a086c257
JA
960 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
961 goto out;
962 if (td->file_service_left--)
963 goto out;
964 }
1907dbc6 965
a086c257
JA
966 if (td->o.file_service_type == FIO_FSERVICE_RR ||
967 td->o.file_service_type == FIO_FSERVICE_SEQ)
d6aed795 968 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
bdb4e2e9 969 else
d6aed795 970 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1907dbc6
JA
971
972 td->file_service_file = f;
973 td->file_service_left = td->file_service_nr - 1;
2ba1c290 974out:
683023e8 975 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1907dbc6 976 return f;
bdb4e2e9
JA
977}
978
7eb36574
JA
979static struct fio_file *get_next_file(struct thread_data *td)
980{
981 struct prof_io_ops *ops = &td->prof_io_ops;
982
983 if (ops->get_next_file)
984 return ops->get_next_file(td);
985
986 return __get_next_file(td);
987}
988
429f6675
JA
989static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
990{
991 struct fio_file *f;
992
993 do {
994 f = get_next_file(td);
995 if (!f)
996 return 1;
997
429f6675
JA
998 io_u->file = f;
999 get_file(f);
1000
1001 if (!fill_io_u(td, io_u))
1002 break;
1003
b5696bfc 1004 put_file_log(td, f);
429f6675 1005 td_io_close_file(td, f);
b5696bfc 1006 io_u->file = NULL;
d6aed795 1007 fio_file_set_done(f);
429f6675 1008 td->nr_done_files++;
0b9d69ec
JA
1009 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
1010 td->nr_done_files, td->o.nr_files);
429f6675
JA
1011 } while (1);
1012
1013 return 0;
1014}
1015
1016
10ba535a
JA
1017struct io_u *__get_io_u(struct thread_data *td)
1018{
1019 struct io_u *io_u = NULL;
1020
e8462bd8
JA
1021 td_io_u_lock(td);
1022
1023again:
01743ee1
JA
1024 if (!flist_empty(&td->io_u_requeues))
1025 io_u = flist_entry(td->io_u_requeues.next, struct io_u, list);
755200a3 1026 else if (!queue_full(td)) {
01743ee1 1027 io_u = flist_entry(td->io_u_freelist.next, struct io_u, list);
10ba535a 1028
6040dabc 1029 io_u->buflen = 0;
10ba535a 1030 io_u->resid = 0;
755200a3 1031 io_u->file = NULL;
d7762cf8 1032 io_u->end_io = NULL;
755200a3
JA
1033 }
1034
1035 if (io_u) {
0c6e7517 1036 assert(io_u->flags & IO_U_F_FREE);
2ecc1b57 1037 io_u->flags &= ~(IO_U_F_FREE | IO_U_F_FREE_DEF);
1ef2b6be 1038 io_u->flags &= ~(IO_U_F_TRIMMED | IO_U_F_BARRIER);
0c6e7517 1039
755200a3 1040 io_u->error = 0;
01743ee1
JA
1041 flist_del(&io_u->list);
1042 flist_add(&io_u->list, &td->io_u_busylist);
10ba535a 1043 td->cur_depth++;
0c41214f 1044 io_u->flags |= IO_U_F_IN_CUR_DEPTH;
1dec3e07
JA
1045 } else if (td->o.verify_async) {
1046 /*
1047 * We ran out, wait for async verify threads to finish and
1048 * return one
1049 */
1050 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1051 goto again;
10ba535a
JA
1052 }
1053
e8462bd8 1054 td_io_u_unlock(td);
10ba535a
JA
1055 return io_u;
1056}
1057
0d29de83 1058static int check_get_trim(struct thread_data *td, struct io_u *io_u)
10ba535a 1059{
0d29de83
JA
1060 if (td->o.trim_backlog && td->trim_entries) {
1061 int get_trim = 0;
10ba535a 1062
0d29de83
JA
1063 if (td->trim_batch) {
1064 td->trim_batch--;
1065 get_trim = 1;
1066 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1067 td->last_ddir != DDIR_READ) {
1068 td->trim_batch = td->o.trim_batch;
1069 if (!td->trim_batch)
1070 td->trim_batch = td->o.trim_backlog;
1071 get_trim = 1;
1072 }
1073
1074 if (get_trim && !get_next_trim(td, io_u))
1075 return 1;
2ba1c290 1076 }
10ba535a 1077
0d29de83
JA
1078 return 0;
1079}
1080
1081static int check_get_verify(struct thread_data *td, struct io_u *io_u)
1082{
9e144189
JA
1083 if (td->o.verify_backlog && td->io_hist_len) {
1084 int get_verify = 0;
1085
1086 if (td->verify_batch) {
1087 td->verify_batch--;
1088 get_verify = 1;
1089 } else if (!(td->io_hist_len % td->o.verify_backlog) &&
1090 td->last_ddir != DDIR_READ) {
1091 td->verify_batch = td->o.verify_batch;
f8a75c99
JA
1092 if (!td->verify_batch)
1093 td->verify_batch = td->o.verify_backlog;
9e144189
JA
1094 get_verify = 1;
1095 }
1096
1097 if (get_verify && !get_next_verify(td, io_u))
0d29de83 1098 return 1;
9e144189
JA
1099 }
1100
0d29de83
JA
1101 return 0;
1102}
1103
1104/*
1105 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1106 * etc. The returned io_u is fully ready to be prepped and submitted.
1107 */
1108struct io_u *get_io_u(struct thread_data *td)
1109{
1110 struct fio_file *f;
1111 struct io_u *io_u;
1112
1113 io_u = __get_io_u(td);
1114 if (!io_u) {
1115 dprint(FD_IO, "__get_io_u failed\n");
1116 return NULL;
1117 }
1118
1119 if (check_get_verify(td, io_u))
1120 goto out;
1121 if (check_get_trim(td, io_u))
1122 goto out;
1123
755200a3
JA
1124 /*
1125 * from a requeue, io_u already setup
1126 */
1127 if (io_u->file)
77f392bf 1128 goto out;
755200a3 1129
429f6675
JA
1130 /*
1131 * If using an iolog, grab next piece if any available.
1132 */
1133 if (td->o.read_iolog_file) {
1134 if (read_iolog_get(td, io_u))
1135 goto err_put;
2ba1c290
JA
1136 } else if (set_io_u_file(td, io_u)) {
1137 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
429f6675 1138 goto err_put;
2ba1c290 1139 }
5ec10eaa 1140
429f6675 1141 f = io_u->file;
d6aed795 1142 assert(fio_file_open(f));
97af62ce 1143
ff58fced 1144 if (ddir_rw(io_u->ddir)) {
d0656a93 1145 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
2ba1c290 1146 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
429f6675 1147 goto err_put;
2ba1c290 1148 }
10ba535a 1149
38dad62d 1150 f->last_start = io_u->offset;
36167d82 1151 f->last_pos = io_u->offset + io_u->buflen;
10ba535a 1152
c311cd2a 1153 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_WRITE)
87dc1ab1 1154 populate_verify_io_u(td, io_u);
e4dad9c6
JA
1155 else if (td->o.refill_buffers && io_u->ddir == DDIR_WRITE)
1156 io_u_fill_buffer(td, io_u, io_u->xfer_buflen);
cbe8d756
RR
1157 else if (io_u->ddir == DDIR_READ) {
1158 /*
1159 * Reset the buf_filled parameters so next time if the
1160 * buffer is used for writes it is refilled.
1161 */
cbe8d756
RR
1162 io_u->buf_filled_len = 0;
1163 }
87dc1ab1 1164 }
10ba535a 1165
165faf16
JA
1166 /*
1167 * Set io data pointers.
1168 */
cec6b55d
JA
1169 io_u->xfer_buf = io_u->buf;
1170 io_u->xfer_buflen = io_u->buflen;
5973cafb 1171
6ac7a331 1172out:
0d29de83 1173 assert(io_u->file);
429f6675 1174 if (!td_io_prep(td, io_u)) {
993bf48b
JA
1175 if (!td->o.disable_slat)
1176 fio_gettime(&io_u->start_time, NULL);
429f6675 1177 return io_u;
36167d82 1178 }
429f6675 1179err_put:
2ba1c290 1180 dprint(FD_IO, "get_io_u failed\n");
429f6675
JA
1181 put_io_u(td, io_u);
1182 return NULL;
10ba535a
JA
1183}
1184
5451792e
JA
1185void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1186{
825f818e
JA
1187 const char *msg[] = { "read", "write", "sync", "datasync",
1188 "sync_file_range", "wait", "trim" };
1189
1190
5451792e
JA
1191
1192 log_err("fio: io_u error");
1193
1194 if (io_u->file)
1195 log_err(" on file %s", io_u->file->file_name);
1196
1197 log_err(": %s\n", strerror(io_u->error));
1198
5ec10eaa
JA
1199 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
1200 io_u->offset, io_u->xfer_buflen);
5451792e
JA
1201
1202 if (!td->error)
1203 td_verror(td, io_u->error, "io_u error");
1204}
1205
97601024
JA
1206static void io_completed(struct thread_data *td, struct io_u *io_u,
1207 struct io_completion_data *icd)
10ba535a 1208{
dbad30b0
JA
1209 /*
1210 * Older gcc's are too dumb to realize that usec is always used
1211 * initialized, silence that warning.
1212 */
1213 unsigned long uninitialized_var(usec);
44f29692 1214 struct fio_file *f;
10ba535a 1215
2ba1c290
JA
1216 dprint_io_u(io_u, "io complete");
1217
2ecc1b57 1218 td_io_u_lock(td);
0c6e7517 1219 assert(io_u->flags & IO_U_F_FLIGHT);
38dad62d 1220 io_u->flags &= ~(IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
2ecc1b57 1221 td_io_u_unlock(td);
0c6e7517 1222
5f9099ea 1223 if (ddir_sync(io_u->ddir)) {
87dc1ab1 1224 td->last_was_sync = 1;
44f29692
JA
1225 f = io_u->file;
1226 if (f) {
1227 f->first_write = -1ULL;
1228 f->last_write = -1ULL;
1229 }
87dc1ab1
JA
1230 return;
1231 }
1232
1233 td->last_was_sync = 0;
9e144189 1234 td->last_ddir = io_u->ddir;
87dc1ab1 1235
ff58fced 1236 if (!io_u->error && ddir_rw(io_u->ddir)) {
10ba535a 1237 unsigned int bytes = io_u->buflen - io_u->resid;
1e97cce9 1238 const enum fio_ddir idx = io_u->ddir;
ba3e4e0c 1239 const enum fio_ddir odx = io_u->ddir ^ 1;
b29ee5b3 1240 int ret;
10ba535a 1241
b29ee5b3
JA
1242 td->io_blocks[idx]++;
1243 td->io_bytes[idx] += bytes;
1244 td->this_io_bytes[idx] += bytes;
10ba535a 1245
44f29692
JA
1246 if (idx == DDIR_WRITE) {
1247 f = io_u->file;
1248 if (f) {
1249 if (f->first_write == -1ULL ||
1250 io_u->offset < f->first_write)
1251 f->first_write = io_u->offset;
1252 if (f->last_write == -1ULL ||
1253 ((io_u->offset + bytes) > f->last_write))
1254 f->last_write = io_u->offset + bytes;
1255 }
1256 }
1257
b29ee5b3 1258 if (ramp_time_over(td)) {
40e1a6f0 1259 unsigned long uninitialized_var(lusec);
40e1a6f0
JA
1260
1261 if (!td->o.disable_clat || !td->o.disable_bw)
1262 lusec = utime_since(&io_u->issue_time,
1263 &icd->time);
02af0988
JA
1264 if (!td->o.disable_lat) {
1265 unsigned long tusec;
721938ae 1266
02af0988
JA
1267 tusec = utime_since(&io_u->start_time,
1268 &icd->time);
1269 add_lat_sample(td, idx, tusec, bytes);
1270 }
9520ebb9 1271 if (!td->o.disable_clat) {
01a1caa5 1272 add_clat_sample(td, idx, lusec, bytes);
40e1a6f0 1273 io_u_mark_latency(td, lusec);
9520ebb9
JA
1274 }
1275 if (!td->o.disable_bw)
306ddc97 1276 add_bw_sample(td, idx, bytes, &icd->time);
b23b6a2f 1277 if (__should_check_rate(td, idx)) {
ba3e4e0c
RR
1278 td->rate_pending_usleep[idx] =
1279 ((td->this_io_bytes[idx] *
1280 td->rate_nsec_cycle[idx]) / 1000 -
1281 utime_since_now(&td->start));
b23b6a2f 1282 }
581e7141 1283 if (__should_check_rate(td, idx ^ 1))
ba3e4e0c
RR
1284 td->rate_pending_usleep[odx] =
1285 ((td->this_io_bytes[odx] *
1286 td->rate_nsec_cycle[odx]) / 1000 -
1287 utime_since_now(&td->start));
721938ae 1288 }
10ba535a 1289
660a1cb5 1290 if (td_write(td) && idx == DDIR_WRITE &&
8670579e 1291 td->o.do_verify &&
41128405 1292 td->o.verify != VERIFY_NONE)
10ba535a
JA
1293 log_io_piece(td, io_u);
1294
b29ee5b3 1295 icd->bytes_done[idx] += bytes;
3af6ef39 1296
d7762cf8 1297 if (io_u->end_io) {
36690c9b 1298 ret = io_u->end_io(td, io_u);
3af6ef39
JA
1299 if (ret && !icd->error)
1300 icd->error = ret;
1301 }
ff58fced 1302 } else if (io_u->error) {
10ba535a 1303 icd->error = io_u->error;
5451792e
JA
1304 io_u_log_error(td, io_u);
1305 }
f2bba182
RR
1306 if (td->o.continue_on_error && icd->error &&
1307 td_non_fatal_error(icd->error)) {
1308 /*
1309 * If there is a non_fatal error, then add to the error count
1310 * and clear all the errors.
1311 */
1312 update_error_count(td, icd->error);
1313 td_clear_error(td);
1314 icd->error = 0;
1315 io_u->error = 0;
1316 }
10ba535a
JA
1317}
1318
9520ebb9
JA
1319static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1320 int nr)
10ba535a 1321{
9520ebb9
JA
1322 if (!td->o.disable_clat || !td->o.disable_bw)
1323 fio_gettime(&icd->time, NULL);
02bcaa8c 1324
3af6ef39
JA
1325 icd->nr = nr;
1326
10ba535a
JA
1327 icd->error = 0;
1328 icd->bytes_done[0] = icd->bytes_done[1] = 0;
36167d82
JA
1329}
1330
97601024
JA
1331static void ios_completed(struct thread_data *td,
1332 struct io_completion_data *icd)
36167d82
JA
1333{
1334 struct io_u *io_u;
1335 int i;
1336
10ba535a
JA
1337 for (i = 0; i < icd->nr; i++) {
1338 io_u = td->io_ops->event(td, i);
1339
1340 io_completed(td, io_u, icd);
e8462bd8
JA
1341
1342 if (!(io_u->flags & IO_U_F_FREE_DEF))
1343 put_io_u(td, io_u);
10ba535a
JA
1344 }
1345}
97601024 1346
e7e6cfb4
JA
1347/*
1348 * Complete a single io_u for the sync engines.
1349 */
581e7141
JA
1350int io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
1351 unsigned long *bytes)
97601024
JA
1352{
1353 struct io_completion_data icd;
1354
9520ebb9 1355 init_icd(td, &icd, 1);
97601024 1356 io_completed(td, io_u, &icd);
e8462bd8
JA
1357
1358 if (!(io_u->flags & IO_U_F_FREE_DEF))
1359 put_io_u(td, io_u);
97601024 1360
581e7141
JA
1361 if (icd.error) {
1362 td_verror(td, icd.error, "io_u_sync_complete");
1363 return -1;
1364 }
97601024 1365
581e7141
JA
1366 if (bytes) {
1367 bytes[0] += icd.bytes_done[0];
1368 bytes[1] += icd.bytes_done[1];
1369 }
1370
1371 return 0;
97601024
JA
1372}
1373
e7e6cfb4
JA
1374/*
1375 * Called to complete min_events number of io for the async engines.
1376 */
581e7141
JA
1377int io_u_queued_complete(struct thread_data *td, int min_evts,
1378 unsigned long *bytes)
97601024 1379{
97601024 1380 struct io_completion_data icd;
00de55ef 1381 struct timespec *tvp = NULL;
97601024 1382 int ret;
4d06a338 1383 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
97601024 1384
4950421a 1385 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
b271fe62 1386
4950421a 1387 if (!min_evts)
00de55ef 1388 tvp = &ts;
97601024 1389
4950421a 1390 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete, tvp);
97601024 1391 if (ret < 0) {
e1161c32 1392 td_verror(td, -ret, "td_io_getevents");
97601024
JA
1393 return ret;
1394 } else if (!ret)
1395 return ret;
1396
9520ebb9 1397 init_icd(td, &icd, ret);
97601024 1398 ios_completed(td, &icd);
581e7141
JA
1399 if (icd.error) {
1400 td_verror(td, icd.error, "io_u_queued_complete");
1401 return -1;
1402 }
97601024 1403
581e7141
JA
1404 if (bytes) {
1405 bytes[0] += icd.bytes_done[0];
1406 bytes[1] += icd.bytes_done[1];
1407 }
1408
1409 return 0;
97601024 1410}
7e77dd02
JA
1411
1412/*
1413 * Call when io_u is really queued, to update the submission latency.
1414 */
1415void io_u_queued(struct thread_data *td, struct io_u *io_u)
1416{
9520ebb9
JA
1417 if (!td->o.disable_slat) {
1418 unsigned long slat_time;
7e77dd02 1419
9520ebb9 1420 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
29a90ddb 1421 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen);
9520ebb9 1422 }
7e77dd02 1423}
433afcb4 1424
5973cafb
JA
1425/*
1426 * "randomly" fill the buffer contents
1427 */
1428void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1429 unsigned int max_bs)
1430{
e84b371e
JA
1431 io_u->buf_filled_len = 0;
1432
637ef8d9
JA
1433 if (!td->o.zero_buffers)
1434 fill_random_buf(io_u->buf, max_bs);
1435 else
1436 memset(io_u->buf, 0, max_bs);
5973cafb 1437}