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