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