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