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