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