Constify l_opts strings
[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
344static unsigned int __get_next_buflen(struct thread_data *td, struct io_u *io_u)
10ba535a 345{
bca4ed4d 346 const int ddir = io_u->ddir;
f3f552b9 347 unsigned int uninitialized_var(buflen);
f3059de1 348 unsigned int minbs, maxbs;
1294c3ec 349 unsigned long r, rand_max;
10ba535a 350
ff58fced
JA
351 assert(ddir_rw(ddir));
352
f3059de1
JA
353 minbs = td->o.min_bs[ddir];
354 maxbs = td->o.max_bs[ddir];
355
4c07ad86
JA
356 if (td->o.use_os_rand)
357 rand_max = OS_RAND_MAX;
358 else
359 rand_max = FRAND_MAX;
360
f3059de1
JA
361 if (minbs == maxbs)
362 buflen = minbs;
10ba535a 363 else {
4c07ad86
JA
364 if (td->o.use_os_rand)
365 r = os_random_long(&td->bsrange_state);
366 else
367 r = __rand(&td->__bsrange_state);
368
720e84ad 369 if (!td->o.bssplit_nr[ddir]) {
f3059de1 370 buflen = 1 + (unsigned int) ((double) maxbs *
4c07ad86 371 (r / (rand_max + 1.0)));
f3059de1
JA
372 if (buflen < minbs)
373 buflen = minbs;
5ec10eaa 374 } else {
564ca972
JA
375 long perc = 0;
376 unsigned int i;
377
720e84ad
JA
378 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
379 struct bssplit *bsp = &td->o.bssplit[ddir][i];
564ca972
JA
380
381 buflen = bsp->bs;
382 perc += bsp->perc;
4c07ad86 383 if (r <= ((rand_max / 100L) * perc))
564ca972
JA
384 break;
385 }
386 }
f3059de1
JA
387 if (!td->o.bs_unaligned && is_power_of_2(minbs))
388 buflen = (buflen + minbs - 1) & ~(minbs - 1);
10ba535a
JA
389 }
390
4ba66134
JA
391 if (io_u->offset + buflen > io_u->file->real_file_size) {
392 dprint(FD_IO, "lower buflen %u -> %u (ddir=%d)\n", buflen,
f3059de1
JA
393 minbs, ddir);
394 buflen = minbs;
4ba66134 395 }
6a5e6884 396
10ba535a
JA
397 return buflen;
398}
399
15dc1934
JA
400static unsigned int get_next_buflen(struct thread_data *td, struct io_u *io_u)
401{
7eb36574
JA
402 struct prof_io_ops *ops = &td->prof_io_ops;
403
404 if (ops->fill_io_u_size)
405 return ops->fill_io_u_size(td, io_u);
15dc1934
JA
406
407 return __get_next_buflen(td, io_u);
408}
409
afe24a5a
JA
410static void set_rwmix_bytes(struct thread_data *td)
411{
afe24a5a
JA
412 unsigned int diff;
413
414 /*
415 * we do time or byte based switch. this is needed because
416 * buffered writes may issue a lot quicker than they complete,
417 * whereas reads do not.
418 */
e47f799f 419 diff = td->o.rwmix[td->rwmix_ddir ^ 1];
04c540d9 420 td->rwmix_issues = (td->io_issues[td->rwmix_ddir] * diff) / 100;
e47f799f
JA
421}
422
423static inline enum fio_ddir get_rand_ddir(struct thread_data *td)
424{
425 unsigned int v;
1294c3ec 426 unsigned long r;
e47f799f 427
4c07ad86
JA
428 if (td->o.use_os_rand) {
429 r = os_random_long(&td->rwmix_state);
430 v = 1 + (int) (100.0 * (r / (OS_RAND_MAX + 1.0)));
431 } else {
432 r = __rand(&td->__rwmix_state);
433 v = 1 + (int) (100.0 * (r / (FRAND_MAX + 1.0)));
434 }
435
04c540d9 436 if (v <= td->o.rwmix[DDIR_READ])
e47f799f
JA
437 return DDIR_READ;
438
439 return DDIR_WRITE;
afe24a5a
JA
440}
441
581e7141
JA
442static enum fio_ddir rate_ddir(struct thread_data *td, enum fio_ddir ddir)
443{
444 enum fio_ddir odir = ddir ^ 1;
445 struct timeval t;
446 long usec;
447
ff58fced
JA
448 assert(ddir_rw(ddir));
449
581e7141
JA
450 if (td->rate_pending_usleep[ddir] <= 0)
451 return ddir;
452
453 /*
454 * We have too much pending sleep in this direction. See if we
455 * should switch.
456 */
457 if (td_rw(td)) {
458 /*
459 * Other direction does not have too much pending, switch
460 */
461 if (td->rate_pending_usleep[odir] < 100000)
462 return odir;
463
464 /*
465 * Both directions have pending sleep. Sleep the minimum time
466 * and deduct from both.
467 */
468 if (td->rate_pending_usleep[ddir] <=
469 td->rate_pending_usleep[odir]) {
470 usec = td->rate_pending_usleep[ddir];
471 } else {
472 usec = td->rate_pending_usleep[odir];
473 ddir = odir;
474 }
475 } else
476 usec = td->rate_pending_usleep[ddir];
477
478 fio_gettime(&t, NULL);
479 usec_sleep(td, usec);
480 usec = utime_since_now(&t);
481
482 td->rate_pending_usleep[ddir] -= usec;
483
484 odir = ddir ^ 1;
485 if (td_rw(td) && __should_check_rate(td, odir))
486 td->rate_pending_usleep[odir] -= usec;
0b9d69ec 487
581e7141
JA
488 return ddir;
489}
490
10ba535a
JA
491/*
492 * Return the data direction for the next io_u. If the job is a
493 * mixed read/write workload, check the rwmix cycle and switch if
494 * necessary.
495 */
1e97cce9 496static enum fio_ddir get_rw_ddir(struct thread_data *td)
10ba535a 497{
581e7141
JA
498 enum fio_ddir ddir;
499
5f9099ea
JA
500 /*
501 * see if it's time to fsync
502 */
503 if (td->o.fsync_blocks &&
504 !(td->io_issues[DDIR_WRITE] % td->o.fsync_blocks) &&
505 td->io_issues[DDIR_WRITE] && should_fsync(td))
506 return DDIR_SYNC;
507
508 /*
509 * see if it's time to fdatasync
510 */
511 if (td->o.fdatasync_blocks &&
512 !(td->io_issues[DDIR_WRITE] % td->o.fdatasync_blocks) &&
513 td->io_issues[DDIR_WRITE] && should_fsync(td))
514 return DDIR_DATASYNC;
515
44f29692
JA
516 /*
517 * see if it's time to sync_file_range
518 */
519 if (td->sync_file_range_nr &&
520 !(td->io_issues[DDIR_WRITE] % td->sync_file_range_nr) &&
521 td->io_issues[DDIR_WRITE] && should_fsync(td))
522 return DDIR_SYNC_FILE_RANGE;
523
10ba535a 524 if (td_rw(td)) {
10ba535a
JA
525 /*
526 * Check if it's time to seed a new data direction.
527 */
e4928662 528 if (td->io_issues[td->rwmix_ddir] >= td->rwmix_issues) {
e47f799f
JA
529 /*
530 * Put a top limit on how many bytes we do for
531 * one data direction, to avoid overflowing the
532 * ranges too much
533 */
534 ddir = get_rand_ddir(td);
e47f799f
JA
535
536 if (ddir != td->rwmix_ddir)
537 set_rwmix_bytes(td);
538
539 td->rwmix_ddir = ddir;
10ba535a 540 }
581e7141 541 ddir = td->rwmix_ddir;
10ba535a 542 } else if (td_read(td))
581e7141 543 ddir = DDIR_READ;
10ba535a 544 else
581e7141
JA
545 ddir = DDIR_WRITE;
546
547 td->rwmix_ddir = rate_ddir(td, ddir);
548 return td->rwmix_ddir;
10ba535a
JA
549}
550
1ef2b6be
JA
551static void set_rw_ddir(struct thread_data *td, struct io_u *io_u)
552{
553 io_u->ddir = get_rw_ddir(td);
554
555 if (io_u->ddir == DDIR_WRITE && (td->io_ops->flags & FIO_BARRIER) &&
556 td->o.barrier_blocks &&
557 !(td->io_issues[DDIR_WRITE] % td->o.barrier_blocks) &&
558 td->io_issues[DDIR_WRITE])
559 io_u->flags |= IO_U_F_BARRIER;
560}
561
e8462bd8 562void put_file_log(struct thread_data *td, struct fio_file *f)
60f2c658
JA
563{
564 int ret = put_file(td, f);
565
566 if (ret)
567 td_verror(td, ret, "file close");
568}
569
10ba535a
JA
570void put_io_u(struct thread_data *td, struct io_u *io_u)
571{
e8462bd8
JA
572 td_io_u_lock(td);
573
0c6e7517 574 io_u->flags |= IO_U_F_FREE;
e8462bd8 575 io_u->flags &= ~IO_U_F_FREE_DEF;
0c6e7517 576
60f2c658
JA
577 if (io_u->file)
578 put_file_log(td, io_u->file);
2dbdab7e 579
10ba535a 580 io_u->file = NULL;
0c41214f
RR
581 if (io_u->flags & IO_U_F_IN_CUR_DEPTH)
582 td->cur_depth--;
e8462bd8 583 flist_del_init(&io_u->list);
01743ee1 584 flist_add(&io_u->list, &td->io_u_freelist);
e8462bd8
JA
585 td_io_u_unlock(td);
586 td_io_u_free_notify(td);
10ba535a
JA
587}
588
f2bba182
RR
589void clear_io_u(struct thread_data *td, struct io_u *io_u)
590{
591 io_u->flags &= ~IO_U_F_FLIGHT;
592 put_io_u(td, io_u);
593}
594
755200a3
JA
595void requeue_io_u(struct thread_data *td, struct io_u **io_u)
596{
597 struct io_u *__io_u = *io_u;
598
465221b0
JA
599 dprint(FD_IO, "requeue %p\n", __io_u);
600
e8462bd8
JA
601 td_io_u_lock(td);
602
4d2e0f49 603 __io_u->flags |= IO_U_F_FREE;
ff58fced 604 if ((__io_u->flags & IO_U_F_FLIGHT) && ddir_rw(__io_u->ddir))
e4f54adb 605 td->io_issues[__io_u->ddir]--;
5ec10eaa 606
4d2e0f49 607 __io_u->flags &= ~IO_U_F_FLIGHT;
0c41214f
RR
608 if (__io_u->flags & IO_U_F_IN_CUR_DEPTH)
609 td->cur_depth--;
01743ee1
JA
610 flist_del(&__io_u->list);
611 flist_add_tail(&__io_u->list, &td->io_u_requeues);
e8462bd8 612 td_io_u_unlock(td);
755200a3
JA
613 *io_u = NULL;
614}
615
9bf2061e 616static int fill_io_u(struct thread_data *td, struct io_u *io_u)
10ba535a 617{
b4c5e1ac
JA
618 if (td->io_ops->flags & FIO_NOIO)
619 goto out;
620
1ef2b6be 621 set_rw_ddir(td, io_u);
5f9099ea 622
87dc1ab1 623 /*
ff58fced 624 * fsync() or fdatasync() or trim etc, we are done
87dc1ab1 625 */
ff58fced 626 if (!ddir_rw(io_u->ddir))
c38e9468 627 goto out;
a00735e6 628
48f5abd3
JA
629 /*
630 * See if it's time to switch to a new zone
631 */
632 if (td->zone_bytes >= td->o.zone_size) {
633 td->zone_bytes = 0;
634 io_u->file->last_pos += td->o.zone_skip;
635 td->io_skip_bytes += td->o.zone_skip;
636 }
637
10ba535a 638 /*
c685b5b2
JA
639 * No log, let the seq/rand engine retrieve the next buflen and
640 * position.
10ba535a 641 */
2ba1c290
JA
642 if (get_next_offset(td, io_u)) {
643 dprint(FD_IO, "io_u %p, failed getting offset\n", io_u);
bca4ed4d 644 return 1;
2ba1c290 645 }
10ba535a 646
9bf2061e 647 io_u->buflen = get_next_buflen(td, io_u);
2ba1c290
JA
648 if (!io_u->buflen) {
649 dprint(FD_IO, "io_u %p, failed getting buflen\n", io_u);
bca4ed4d 650 return 1;
2ba1c290 651 }
bca4ed4d 652
2ba1c290
JA
653 if (io_u->offset + io_u->buflen > io_u->file->real_file_size) {
654 dprint(FD_IO, "io_u %p, offset too large\n", io_u);
4ba66134
JA
655 dprint(FD_IO, " off=%llu/%lu > %llu\n", io_u->offset,
656 io_u->buflen, io_u->file->real_file_size);
6a5e6884 657 return 1;
2ba1c290 658 }
6a5e6884 659
bca4ed4d
JA
660 /*
661 * mark entry before potentially trimming io_u
662 */
303032ae 663 if (td_random(td) && file_randommap(td, io_u->file))
9bf2061e 664 mark_random_map(td, io_u);
bca4ed4d
JA
665
666 /*
667 * If using a write iolog, store this entry.
668 */
c38e9468 669out:
2ba1c290 670 dprint_io_u(io_u, "fill_io_u");
d9d91e39 671 td->zone_bytes += io_u->buflen;
f29b25a3 672 log_io_u(td, io_u);
bca4ed4d 673 return 0;
10ba535a
JA
674}
675
838bc709
JA
676static void __io_u_mark_map(unsigned int *map, unsigned int nr)
677{
2b13e716 678 int idx = 0;
838bc709
JA
679
680 switch (nr) {
681 default:
2b13e716 682 idx = 6;
838bc709
JA
683 break;
684 case 33 ... 64:
2b13e716 685 idx = 5;
838bc709
JA
686 break;
687 case 17 ... 32:
2b13e716 688 idx = 4;
838bc709
JA
689 break;
690 case 9 ... 16:
2b13e716 691 idx = 3;
838bc709
JA
692 break;
693 case 5 ... 8:
2b13e716 694 idx = 2;
838bc709
JA
695 break;
696 case 1 ... 4:
2b13e716 697 idx = 1;
838bc709
JA
698 case 0:
699 break;
700 }
701
2b13e716 702 map[idx]++;
838bc709
JA
703}
704
705void io_u_mark_submit(struct thread_data *td, unsigned int nr)
706{
707 __io_u_mark_map(td->ts.io_u_submit, nr);
708 td->ts.total_submit++;
709}
710
711void io_u_mark_complete(struct thread_data *td, unsigned int nr)
712{
713 __io_u_mark_map(td->ts.io_u_complete, nr);
714 td->ts.total_complete++;
715}
716
d8005759 717void io_u_mark_depth(struct thread_data *td, unsigned int nr)
71619dc2 718{
2b13e716 719 int idx = 0;
71619dc2
JA
720
721 switch (td->cur_depth) {
722 default:
2b13e716 723 idx = 6;
a783e61a 724 break;
71619dc2 725 case 32 ... 63:
2b13e716 726 idx = 5;
a783e61a 727 break;
71619dc2 728 case 16 ... 31:
2b13e716 729 idx = 4;
a783e61a 730 break;
71619dc2 731 case 8 ... 15:
2b13e716 732 idx = 3;
a783e61a 733 break;
71619dc2 734 case 4 ... 7:
2b13e716 735 idx = 2;
a783e61a 736 break;
71619dc2 737 case 2 ... 3:
2b13e716 738 idx = 1;
71619dc2
JA
739 case 1:
740 break;
741 }
742
2b13e716 743 td->ts.io_u_map[idx] += nr;
71619dc2
JA
744}
745
04a0feae
JA
746static void io_u_mark_lat_usec(struct thread_data *td, unsigned long usec)
747{
2b13e716 748 int idx = 0;
04a0feae
JA
749
750 assert(usec < 1000);
751
752 switch (usec) {
753 case 750 ... 999:
2b13e716 754 idx = 9;
04a0feae
JA
755 break;
756 case 500 ... 749:
2b13e716 757 idx = 8;
04a0feae
JA
758 break;
759 case 250 ... 499:
2b13e716 760 idx = 7;
04a0feae
JA
761 break;
762 case 100 ... 249:
2b13e716 763 idx = 6;
04a0feae
JA
764 break;
765 case 50 ... 99:
2b13e716 766 idx = 5;
04a0feae
JA
767 break;
768 case 20 ... 49:
2b13e716 769 idx = 4;
04a0feae
JA
770 break;
771 case 10 ... 19:
2b13e716 772 idx = 3;
04a0feae
JA
773 break;
774 case 4 ... 9:
2b13e716 775 idx = 2;
04a0feae
JA
776 break;
777 case 2 ... 3:
2b13e716 778 idx = 1;
04a0feae
JA
779 case 0 ... 1:
780 break;
781 }
782
2b13e716
JA
783 assert(idx < FIO_IO_U_LAT_U_NR);
784 td->ts.io_u_lat_u[idx]++;
04a0feae
JA
785}
786
787static void io_u_mark_lat_msec(struct thread_data *td, unsigned long msec)
ec118304 788{
2b13e716 789 int idx = 0;
ec118304
JA
790
791 switch (msec) {
792 default:
2b13e716 793 idx = 11;
04a0feae 794 break;
8abdce66 795 case 1000 ... 1999:
2b13e716 796 idx = 10;
04a0feae 797 break;
8abdce66 798 case 750 ... 999:
2b13e716 799 idx = 9;
04a0feae 800 break;
8abdce66 801 case 500 ... 749:
2b13e716 802 idx = 8;
04a0feae 803 break;
8abdce66 804 case 250 ... 499:
2b13e716 805 idx = 7;
04a0feae 806 break;
8abdce66 807 case 100 ... 249:
2b13e716 808 idx = 6;
04a0feae 809 break;
8abdce66 810 case 50 ... 99:
2b13e716 811 idx = 5;
04a0feae 812 break;
8abdce66 813 case 20 ... 49:
2b13e716 814 idx = 4;
04a0feae 815 break;
8abdce66 816 case 10 ... 19:
2b13e716 817 idx = 3;
04a0feae 818 break;
8abdce66 819 case 4 ... 9:
2b13e716 820 idx = 2;
04a0feae 821 break;
ec118304 822 case 2 ... 3:
2b13e716 823 idx = 1;
ec118304
JA
824 case 0 ... 1:
825 break;
826 }
827
2b13e716
JA
828 assert(idx < FIO_IO_U_LAT_M_NR);
829 td->ts.io_u_lat_m[idx]++;
04a0feae
JA
830}
831
832static void io_u_mark_latency(struct thread_data *td, unsigned long usec)
833{
834 if (usec < 1000)
835 io_u_mark_lat_usec(td, usec);
836 else
837 io_u_mark_lat_msec(td, usec / 1000);
ec118304
JA
838}
839
0aabe160
JA
840/*
841 * Get next file to service by choosing one at random
842 */
2cc52930
JA
843static struct fio_file *get_next_file_rand(struct thread_data *td,
844 enum fio_file_flags goodf,
d6aed795 845 enum fio_file_flags badf)
0aabe160 846{
0aabe160 847 struct fio_file *f;
1c178180 848 int fno;
0aabe160
JA
849
850 do {
87b10676 851 int opened = 0;
1294c3ec 852 unsigned long r;
4c07ad86
JA
853
854 if (td->o.use_os_rand) {
855 r = os_random_long(&td->next_file_state);
856 fno = (unsigned int) ((double) td->o.nr_files
857 * (r / (OS_RAND_MAX + 1.0)));
858 } else {
859 r = __rand(&td->__next_file_state);
860 fno = (unsigned int) ((double) td->o.nr_files
861 * (r / (FRAND_MAX + 1.0)));
862 }
7c83c089 863
126d65c6 864 f = td->files[fno];
d6aed795 865 if (fio_file_done(f))
059e63c0 866 continue;
1c178180 867
d6aed795 868 if (!fio_file_open(f)) {
87b10676
JA
869 int err;
870
871 err = td_io_open_file(td, f);
872 if (err)
873 continue;
874 opened = 1;
875 }
876
2ba1c290
JA
877 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf)) {
878 dprint(FD_FILE, "get_next_file_rand: %p\n", f);
0aabe160 879 return f;
2ba1c290 880 }
87b10676
JA
881 if (opened)
882 td_io_close_file(td, f);
0aabe160
JA
883 } while (1);
884}
885
886/*
887 * Get next file to service by doing round robin between all available ones
888 */
1c178180
JA
889static struct fio_file *get_next_file_rr(struct thread_data *td, int goodf,
890 int badf)
3d7c391d
JA
891{
892 unsigned int old_next_file = td->next_file;
893 struct fio_file *f;
894
895 do {
87b10676
JA
896 int opened = 0;
897
126d65c6 898 f = td->files[td->next_file];
3d7c391d
JA
899
900 td->next_file++;
2dc1bbeb 901 if (td->next_file >= td->o.nr_files)
3d7c391d
JA
902 td->next_file = 0;
903
87b10676 904 dprint(FD_FILE, "trying file %s %x\n", f->file_name, f->flags);
d6aed795 905 if (fio_file_done(f)) {
d5ed68ea 906 f = NULL;
059e63c0 907 continue;
d5ed68ea 908 }
059e63c0 909
d6aed795 910 if (!fio_file_open(f)) {
87b10676
JA
911 int err;
912
913 err = td_io_open_file(td, f);
b5696bfc
JA
914 if (err) {
915 dprint(FD_FILE, "error %d on open of %s\n",
916 err, f->file_name);
87c27b45 917 f = NULL;
87b10676 918 continue;
b5696bfc 919 }
87b10676
JA
920 opened = 1;
921 }
922
0b9d69ec
JA
923 dprint(FD_FILE, "goodf=%x, badf=%x, ff=%x\n", goodf, badf,
924 f->flags);
1c178180 925 if ((!goodf || (f->flags & goodf)) && !(f->flags & badf))
3d7c391d
JA
926 break;
927
87b10676
JA
928 if (opened)
929 td_io_close_file(td, f);
930
3d7c391d
JA
931 f = NULL;
932 } while (td->next_file != old_next_file);
933
2ba1c290 934 dprint(FD_FILE, "get_next_file_rr: %p\n", f);
3d7c391d
JA
935 return f;
936}
937
7eb36574 938static struct fio_file *__get_next_file(struct thread_data *td)
bdb4e2e9 939{
1907dbc6
JA
940 struct fio_file *f;
941
2dc1bbeb 942 assert(td->o.nr_files <= td->files_index);
1c178180 943
b5696bfc 944 if (td->nr_done_files >= td->o.nr_files) {
5ec10eaa
JA
945 dprint(FD_FILE, "get_next_file: nr_open=%d, nr_done=%d,"
946 " nr_files=%d\n", td->nr_open_files,
947 td->nr_done_files,
948 td->o.nr_files);
bdb4e2e9 949 return NULL;
2ba1c290 950 }
bdb4e2e9 951
1907dbc6 952 f = td->file_service_file;
d6aed795 953 if (f && fio_file_open(f) && !fio_file_closing(f)) {
a086c257
JA
954 if (td->o.file_service_type == FIO_FSERVICE_SEQ)
955 goto out;
956 if (td->file_service_left--)
957 goto out;
958 }
1907dbc6 959
a086c257
JA
960 if (td->o.file_service_type == FIO_FSERVICE_RR ||
961 td->o.file_service_type == FIO_FSERVICE_SEQ)
d6aed795 962 f = get_next_file_rr(td, FIO_FILE_open, FIO_FILE_closing);
bdb4e2e9 963 else
d6aed795 964 f = get_next_file_rand(td, FIO_FILE_open, FIO_FILE_closing);
1907dbc6
JA
965
966 td->file_service_file = f;
967 td->file_service_left = td->file_service_nr - 1;
2ba1c290 968out:
683023e8 969 dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
1907dbc6 970 return f;
bdb4e2e9
JA
971}
972
7eb36574
JA
973static struct fio_file *get_next_file(struct thread_data *td)
974{
975 struct prof_io_ops *ops = &td->prof_io_ops;
976
977 if (ops->get_next_file)
978 return ops->get_next_file(td);
979
980 return __get_next_file(td);
981}
982
429f6675
JA
983static int set_io_u_file(struct thread_data *td, struct io_u *io_u)
984{
985 struct fio_file *f;
986
987 do {
988 f = get_next_file(td);
989 if (!f)
990 return 1;
991
429f6675
JA
992 io_u->file = f;
993 get_file(f);
994
995 if (!fill_io_u(td, io_u))
996 break;
997
b5696bfc 998 put_file_log(td, f);
429f6675 999 td_io_close_file(td, f);
b5696bfc 1000 io_u->file = NULL;
d6aed795 1001 fio_file_set_done(f);
429f6675 1002 td->nr_done_files++;
0b9d69ec
JA
1003 dprint(FD_FILE, "%s: is done (%d of %d)\n", f->file_name,
1004 td->nr_done_files, td->o.nr_files);
429f6675
JA
1005 } while (1);
1006
1007 return 0;
1008}
1009
1010
10ba535a
JA
1011struct io_u *__get_io_u(struct thread_data *td)
1012{
1013 struct io_u *io_u = NULL;
1014
e8462bd8
JA
1015 td_io_u_lock(td);
1016
1017again:
01743ee1
JA
1018 if (!flist_empty(&td->io_u_requeues))
1019 io_u = flist_entry(td->io_u_requeues.next, struct io_u, list);
755200a3 1020 else if (!queue_full(td)) {
01743ee1 1021 io_u = flist_entry(td->io_u_freelist.next, struct io_u, list);
10ba535a 1022
6040dabc 1023 io_u->buflen = 0;
10ba535a 1024 io_u->resid = 0;
755200a3 1025 io_u->file = NULL;
d7762cf8 1026 io_u->end_io = NULL;
755200a3
JA
1027 }
1028
1029 if (io_u) {
0c6e7517 1030 assert(io_u->flags & IO_U_F_FREE);
2ecc1b57 1031 io_u->flags &= ~(IO_U_F_FREE | IO_U_F_FREE_DEF);
1ef2b6be 1032 io_u->flags &= ~(IO_U_F_TRIMMED | IO_U_F_BARRIER);
0c6e7517 1033
755200a3 1034 io_u->error = 0;
01743ee1
JA
1035 flist_del(&io_u->list);
1036 flist_add(&io_u->list, &td->io_u_busylist);
10ba535a 1037 td->cur_depth++;
0c41214f 1038 io_u->flags |= IO_U_F_IN_CUR_DEPTH;
1dec3e07
JA
1039 } else if (td->o.verify_async) {
1040 /*
1041 * We ran out, wait for async verify threads to finish and
1042 * return one
1043 */
1044 pthread_cond_wait(&td->free_cond, &td->io_u_lock);
1045 goto again;
10ba535a
JA
1046 }
1047
e8462bd8 1048 td_io_u_unlock(td);
10ba535a
JA
1049 return io_u;
1050}
1051
0d29de83 1052static int check_get_trim(struct thread_data *td, struct io_u *io_u)
10ba535a 1053{
0d29de83
JA
1054 if (td->o.trim_backlog && td->trim_entries) {
1055 int get_trim = 0;
10ba535a 1056
0d29de83
JA
1057 if (td->trim_batch) {
1058 td->trim_batch--;
1059 get_trim = 1;
1060 } else if (!(td->io_hist_len % td->o.trim_backlog) &&
1061 td->last_ddir != DDIR_READ) {
1062 td->trim_batch = td->o.trim_batch;
1063 if (!td->trim_batch)
1064 td->trim_batch = td->o.trim_backlog;
1065 get_trim = 1;
1066 }
1067
1068 if (get_trim && !get_next_trim(td, io_u))
1069 return 1;
2ba1c290 1070 }
10ba535a 1071
0d29de83
JA
1072 return 0;
1073}
1074
1075static int check_get_verify(struct thread_data *td, struct io_u *io_u)
1076{
9e144189
JA
1077 if (td->o.verify_backlog && td->io_hist_len) {
1078 int get_verify = 0;
1079
1080 if (td->verify_batch) {
1081 td->verify_batch--;
1082 get_verify = 1;
1083 } else if (!(td->io_hist_len % td->o.verify_backlog) &&
1084 td->last_ddir != DDIR_READ) {
1085 td->verify_batch = td->o.verify_batch;
f8a75c99
JA
1086 if (!td->verify_batch)
1087 td->verify_batch = td->o.verify_backlog;
9e144189
JA
1088 get_verify = 1;
1089 }
1090
1091 if (get_verify && !get_next_verify(td, io_u))
0d29de83 1092 return 1;
9e144189
JA
1093 }
1094
0d29de83
JA
1095 return 0;
1096}
1097
1098/*
1099 * Return an io_u to be processed. Gets a buflen and offset, sets direction,
1100 * etc. The returned io_u is fully ready to be prepped and submitted.
1101 */
1102struct io_u *get_io_u(struct thread_data *td)
1103{
1104 struct fio_file *f;
1105 struct io_u *io_u;
1106
1107 io_u = __get_io_u(td);
1108 if (!io_u) {
1109 dprint(FD_IO, "__get_io_u failed\n");
1110 return NULL;
1111 }
1112
1113 if (check_get_verify(td, io_u))
1114 goto out;
1115 if (check_get_trim(td, io_u))
1116 goto out;
1117
755200a3
JA
1118 /*
1119 * from a requeue, io_u already setup
1120 */
1121 if (io_u->file)
77f392bf 1122 goto out;
755200a3 1123
429f6675
JA
1124 /*
1125 * If using an iolog, grab next piece if any available.
1126 */
1127 if (td->o.read_iolog_file) {
1128 if (read_iolog_get(td, io_u))
1129 goto err_put;
2ba1c290
JA
1130 } else if (set_io_u_file(td, io_u)) {
1131 dprint(FD_IO, "io_u %p, setting file failed\n", io_u);
429f6675 1132 goto err_put;
2ba1c290 1133 }
5ec10eaa 1134
429f6675 1135 f = io_u->file;
d6aed795 1136 assert(fio_file_open(f));
97af62ce 1137
ff58fced 1138 if (ddir_rw(io_u->ddir)) {
d0656a93 1139 if (!io_u->buflen && !(td->io_ops->flags & FIO_NOIO)) {
2ba1c290 1140 dprint(FD_IO, "get_io_u: zero buflen on %p\n", io_u);
429f6675 1141 goto err_put;
2ba1c290 1142 }
10ba535a 1143
38dad62d 1144 f->last_start = io_u->offset;
36167d82 1145 f->last_pos = io_u->offset + io_u->buflen;
10ba535a 1146
c311cd2a 1147 if (td->o.verify != VERIFY_NONE && io_u->ddir == DDIR_WRITE)
87dc1ab1 1148 populate_verify_io_u(td, io_u);
e4dad9c6
JA
1149 else if (td->o.refill_buffers && io_u->ddir == DDIR_WRITE)
1150 io_u_fill_buffer(td, io_u, io_u->xfer_buflen);
cbe8d756
RR
1151 else if (io_u->ddir == DDIR_READ) {
1152 /*
1153 * Reset the buf_filled parameters so next time if the
1154 * buffer is used for writes it is refilled.
1155 */
cbe8d756
RR
1156 io_u->buf_filled_len = 0;
1157 }
87dc1ab1 1158 }
10ba535a 1159
165faf16
JA
1160 /*
1161 * Set io data pointers.
1162 */
cec6b55d
JA
1163 io_u->xfer_buf = io_u->buf;
1164 io_u->xfer_buflen = io_u->buflen;
5973cafb 1165
6ac7a331 1166out:
0d29de83 1167 assert(io_u->file);
429f6675 1168 if (!td_io_prep(td, io_u)) {
993bf48b
JA
1169 if (!td->o.disable_slat)
1170 fio_gettime(&io_u->start_time, NULL);
429f6675 1171 return io_u;
36167d82 1172 }
429f6675 1173err_put:
2ba1c290 1174 dprint(FD_IO, "get_io_u failed\n");
429f6675
JA
1175 put_io_u(td, io_u);
1176 return NULL;
10ba535a
JA
1177}
1178
5451792e
JA
1179void io_u_log_error(struct thread_data *td, struct io_u *io_u)
1180{
825f818e
JA
1181 const char *msg[] = { "read", "write", "sync", "datasync",
1182 "sync_file_range", "wait", "trim" };
1183
1184
5451792e
JA
1185
1186 log_err("fio: io_u error");
1187
1188 if (io_u->file)
1189 log_err(" on file %s", io_u->file->file_name);
1190
1191 log_err(": %s\n", strerror(io_u->error));
1192
5ec10eaa
JA
1193 log_err(" %s offset=%llu, buflen=%lu\n", msg[io_u->ddir],
1194 io_u->offset, io_u->xfer_buflen);
5451792e
JA
1195
1196 if (!td->error)
1197 td_verror(td, io_u->error, "io_u error");
1198}
1199
97601024
JA
1200static void io_completed(struct thread_data *td, struct io_u *io_u,
1201 struct io_completion_data *icd)
10ba535a 1202{
dbad30b0
JA
1203 /*
1204 * Older gcc's are too dumb to realize that usec is always used
1205 * initialized, silence that warning.
1206 */
1207 unsigned long uninitialized_var(usec);
44f29692 1208 struct fio_file *f;
10ba535a 1209
2ba1c290
JA
1210 dprint_io_u(io_u, "io complete");
1211
2ecc1b57 1212 td_io_u_lock(td);
0c6e7517 1213 assert(io_u->flags & IO_U_F_FLIGHT);
38dad62d 1214 io_u->flags &= ~(IO_U_F_FLIGHT | IO_U_F_BUSY_OK);
2ecc1b57 1215 td_io_u_unlock(td);
0c6e7517 1216
5f9099ea 1217 if (ddir_sync(io_u->ddir)) {
87dc1ab1 1218 td->last_was_sync = 1;
44f29692
JA
1219 f = io_u->file;
1220 if (f) {
1221 f->first_write = -1ULL;
1222 f->last_write = -1ULL;
1223 }
87dc1ab1
JA
1224 return;
1225 }
1226
1227 td->last_was_sync = 0;
9e144189 1228 td->last_ddir = io_u->ddir;
87dc1ab1 1229
ff58fced 1230 if (!io_u->error && ddir_rw(io_u->ddir)) {
10ba535a 1231 unsigned int bytes = io_u->buflen - io_u->resid;
1e97cce9 1232 const enum fio_ddir idx = io_u->ddir;
ba3e4e0c 1233 const enum fio_ddir odx = io_u->ddir ^ 1;
b29ee5b3 1234 int ret;
10ba535a 1235
b29ee5b3
JA
1236 td->io_blocks[idx]++;
1237 td->io_bytes[idx] += bytes;
1238 td->this_io_bytes[idx] += bytes;
10ba535a 1239
44f29692
JA
1240 if (idx == DDIR_WRITE) {
1241 f = io_u->file;
1242 if (f) {
1243 if (f->first_write == -1ULL ||
1244 io_u->offset < f->first_write)
1245 f->first_write = io_u->offset;
1246 if (f->last_write == -1ULL ||
1247 ((io_u->offset + bytes) > f->last_write))
1248 f->last_write = io_u->offset + bytes;
1249 }
1250 }
1251
b29ee5b3 1252 if (ramp_time_over(td)) {
40e1a6f0 1253 unsigned long uninitialized_var(lusec);
40e1a6f0
JA
1254
1255 if (!td->o.disable_clat || !td->o.disable_bw)
1256 lusec = utime_since(&io_u->issue_time,
1257 &icd->time);
02af0988
JA
1258 if (!td->o.disable_lat) {
1259 unsigned long tusec;
721938ae 1260
02af0988
JA
1261 tusec = utime_since(&io_u->start_time,
1262 &icd->time);
1263 add_lat_sample(td, idx, tusec, bytes);
1264 }
9520ebb9 1265 if (!td->o.disable_clat) {
01a1caa5 1266 add_clat_sample(td, idx, lusec, bytes);
40e1a6f0 1267 io_u_mark_latency(td, lusec);
9520ebb9
JA
1268 }
1269 if (!td->o.disable_bw)
306ddc97 1270 add_bw_sample(td, idx, bytes, &icd->time);
b23b6a2f 1271 if (__should_check_rate(td, idx)) {
ba3e4e0c
RR
1272 td->rate_pending_usleep[idx] =
1273 ((td->this_io_bytes[idx] *
1274 td->rate_nsec_cycle[idx]) / 1000 -
1275 utime_since_now(&td->start));
b23b6a2f 1276 }
581e7141 1277 if (__should_check_rate(td, idx ^ 1))
ba3e4e0c
RR
1278 td->rate_pending_usleep[odx] =
1279 ((td->this_io_bytes[odx] *
1280 td->rate_nsec_cycle[odx]) / 1000 -
1281 utime_since_now(&td->start));
721938ae 1282 }
10ba535a 1283
660a1cb5 1284 if (td_write(td) && idx == DDIR_WRITE &&
8670579e 1285 td->o.do_verify &&
41128405 1286 td->o.verify != VERIFY_NONE)
10ba535a
JA
1287 log_io_piece(td, io_u);
1288
b29ee5b3 1289 icd->bytes_done[idx] += bytes;
3af6ef39 1290
d7762cf8 1291 if (io_u->end_io) {
36690c9b 1292 ret = io_u->end_io(td, io_u);
3af6ef39
JA
1293 if (ret && !icd->error)
1294 icd->error = ret;
1295 }
ff58fced 1296 } else if (io_u->error) {
10ba535a 1297 icd->error = io_u->error;
5451792e
JA
1298 io_u_log_error(td, io_u);
1299 }
f2bba182
RR
1300 if (td->o.continue_on_error && icd->error &&
1301 td_non_fatal_error(icd->error)) {
1302 /*
1303 * If there is a non_fatal error, then add to the error count
1304 * and clear all the errors.
1305 */
1306 update_error_count(td, icd->error);
1307 td_clear_error(td);
1308 icd->error = 0;
1309 io_u->error = 0;
1310 }
10ba535a
JA
1311}
1312
9520ebb9
JA
1313static void init_icd(struct thread_data *td, struct io_completion_data *icd,
1314 int nr)
10ba535a 1315{
9520ebb9
JA
1316 if (!td->o.disable_clat || !td->o.disable_bw)
1317 fio_gettime(&icd->time, NULL);
02bcaa8c 1318
3af6ef39
JA
1319 icd->nr = nr;
1320
10ba535a
JA
1321 icd->error = 0;
1322 icd->bytes_done[0] = icd->bytes_done[1] = 0;
36167d82
JA
1323}
1324
97601024
JA
1325static void ios_completed(struct thread_data *td,
1326 struct io_completion_data *icd)
36167d82
JA
1327{
1328 struct io_u *io_u;
1329 int i;
1330
10ba535a
JA
1331 for (i = 0; i < icd->nr; i++) {
1332 io_u = td->io_ops->event(td, i);
1333
1334 io_completed(td, io_u, icd);
e8462bd8
JA
1335
1336 if (!(io_u->flags & IO_U_F_FREE_DEF))
1337 put_io_u(td, io_u);
10ba535a
JA
1338 }
1339}
97601024 1340
e7e6cfb4
JA
1341/*
1342 * Complete a single io_u for the sync engines.
1343 */
581e7141
JA
1344int io_u_sync_complete(struct thread_data *td, struct io_u *io_u,
1345 unsigned long *bytes)
97601024
JA
1346{
1347 struct io_completion_data icd;
1348
9520ebb9 1349 init_icd(td, &icd, 1);
97601024 1350 io_completed(td, io_u, &icd);
e8462bd8
JA
1351
1352 if (!(io_u->flags & IO_U_F_FREE_DEF))
1353 put_io_u(td, io_u);
97601024 1354
581e7141
JA
1355 if (icd.error) {
1356 td_verror(td, icd.error, "io_u_sync_complete");
1357 return -1;
1358 }
97601024 1359
581e7141
JA
1360 if (bytes) {
1361 bytes[0] += icd.bytes_done[0];
1362 bytes[1] += icd.bytes_done[1];
1363 }
1364
1365 return 0;
97601024
JA
1366}
1367
e7e6cfb4
JA
1368/*
1369 * Called to complete min_events number of io for the async engines.
1370 */
581e7141
JA
1371int io_u_queued_complete(struct thread_data *td, int min_evts,
1372 unsigned long *bytes)
97601024 1373{
97601024 1374 struct io_completion_data icd;
00de55ef 1375 struct timespec *tvp = NULL;
97601024 1376 int ret;
4d06a338 1377 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0, };
97601024 1378
4950421a 1379 dprint(FD_IO, "io_u_queued_completed: min=%d\n", min_evts);
b271fe62 1380
4950421a 1381 if (!min_evts)
00de55ef 1382 tvp = &ts;
97601024 1383
4950421a 1384 ret = td_io_getevents(td, min_evts, td->o.iodepth_batch_complete, tvp);
97601024 1385 if (ret < 0) {
e1161c32 1386 td_verror(td, -ret, "td_io_getevents");
97601024
JA
1387 return ret;
1388 } else if (!ret)
1389 return ret;
1390
9520ebb9 1391 init_icd(td, &icd, ret);
97601024 1392 ios_completed(td, &icd);
581e7141
JA
1393 if (icd.error) {
1394 td_verror(td, icd.error, "io_u_queued_complete");
1395 return -1;
1396 }
97601024 1397
581e7141
JA
1398 if (bytes) {
1399 bytes[0] += icd.bytes_done[0];
1400 bytes[1] += icd.bytes_done[1];
1401 }
1402
1403 return 0;
97601024 1404}
7e77dd02
JA
1405
1406/*
1407 * Call when io_u is really queued, to update the submission latency.
1408 */
1409void io_u_queued(struct thread_data *td, struct io_u *io_u)
1410{
9520ebb9
JA
1411 if (!td->o.disable_slat) {
1412 unsigned long slat_time;
7e77dd02 1413
9520ebb9 1414 slat_time = utime_since(&io_u->start_time, &io_u->issue_time);
29a90ddb 1415 add_slat_sample(td, io_u->ddir, slat_time, io_u->xfer_buflen);
9520ebb9 1416 }
7e77dd02 1417}
433afcb4 1418
5973cafb
JA
1419/*
1420 * "randomly" fill the buffer contents
1421 */
1422void io_u_fill_buffer(struct thread_data *td, struct io_u *io_u,
1423 unsigned int max_bs)
1424{
e84b371e
JA
1425 io_u->buf_filled_len = 0;
1426
637ef8d9
JA
1427 if (!td->o.zero_buffers)
1428 fill_random_buf(io_u->buf, max_bs);
1429 else
1430 memset(io_u->buf, 0, max_bs);
5973cafb 1431}