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